Comply with ansi C and clean up.

This commit is contained in:
2026-07-15 13:26:36 -07:00
parent ffc4310646
commit a829ac8647
7 changed files with 465 additions and 291 deletions
+189 -116
View File
@@ -1,7 +1,6 @@
#include "core.h"
// Remove SDL's entry point.
#if OS_WINDOWS
#if OS_WINDOWS /* Remove SDL's entry point. */
#define SDL_MAIN_HANDLED
#endif
@@ -35,7 +34,7 @@
/* NOTE: This is so it compiles on ARM. */
#if ARCHITECTURE_X64 || ARCHITECTURE_X86
#if OS_MACOS || OS_LINUX
#include <x86intrin.h> // TODO: What is the equivalent on Windows?
#include <x86intrin.h>
#endif
#endif
@@ -72,8 +71,12 @@ static int str_len(char *str)
static unsigned int SafeTruncateUInt64(unsigned long long Value)
{
unsigned int Result;
ASSERT(Value <= 0xFFFFFFFF);
unsigned int Result = (unsigned int)Value;
Result = (unsigned int)Value;
return Result;
}
@@ -81,14 +84,19 @@ struct debug_read_file_result
DEBUGPlatformReadEntireFile(struct thread_context *Thread,
const char *Filename)
{
(void)Thread;
struct debug_read_file_result Result;
SDL_RWops *FileHandle;
memset(&Result, 0, sizeof(Result));
SDL_RWops *FileHandle = SDL_RWFromFile(Filename, "r");
FileHandle = SDL_RWFromFile(Filename, "r");
if(FileHandle) {
long long FileSize = SDL_RWsize(FileHandle);
long long FileSize;
unsigned int ObjectsRead;
FileSize = SDL_RWsize(FileHandle);
if(FileSize >= 0) {
Result.ContentsSize = SafeTruncateUInt64(FileSize);
} else {
@@ -104,7 +112,7 @@ DEBUGPlatformReadEntireFile(struct thread_context *Thread,
return Result;
}
unsigned int ObjectsRead = SDL_RWread(FileHandle,
ObjectsRead = SDL_RWread(FileHandle,
(void *)Result.Contents, Result.ContentsSize, 1);
if(ObjectsRead == 0) {
free(Result.Contents);
@@ -225,13 +233,15 @@ static void
SDLResizeTexture(struct offscreen_buffer *Buffer,
SDL_Renderer *Renderer, int Width, int Height)
{
int BytesPerPixel;
if(Buffer->Texture)
SDL_DestroyTexture(Buffer->Texture);
if(Buffer->Memory)
free(Buffer->Memory);
int BytesPerPixel = 4;
BytesPerPixel = 4;
Buffer->Texture = SDL_CreateTexture(Renderer,
SDL_PIXELFORMAT_ARGB8888,
@@ -248,8 +258,12 @@ static void SDLDisplayBufferInWindow(struct offscreen_buffer *Buffer,
SDL_Window *Window,
SDL_Renderer *Renderer)
{
int OffsetX = 10;
int OffsetY = 10;
int OffsetX, OffsetY;
struct sdl_window_size WinSize;
OffsetX = 10;
OffsetY = 10;
SDL_SetRenderDrawColor(Renderer, 0, 0, 0, 255);
SDL_RenderClear(Renderer);
@@ -261,17 +275,23 @@ static void SDLDisplayBufferInWindow(struct offscreen_buffer *Buffer,
/* TODO: We temporarily introduce the target rectangle to avoid
* stretching the canvas. */
/* SDL_RenderCopy(Renderer, Buffer.Texture, 0, 0); */
struct sdl_window_size WinSize = SDLGetBorderedWindowSize(Window);
WinSize = SDLGetBorderedWindowSize(Window);
if(WinSize.Width >= Buffer->Width*2 &&
WinSize.Height >= Buffer->Height*2)
{
SDL_Rect dest_rect =
{ OffsetX, OffsetY, Buffer->Width*2, Buffer->Height*2 };
SDL_Rect dest_rect;
dest_rect.x = OffsetX;
dest_rect.y = OffsetY;
dest_rect.w = Buffer->Width*2;
dest_rect.h = Buffer->Height*2;
SDL_RenderCopy(Renderer, Buffer->Texture, 0,
(const SDL_Rect *)(&dest_rect));
} else {
SDL_Rect dest_rect =
{ OffsetX, OffsetY, RESOLUTION_WIDTH, RESOLUTION_HEIGHT };
SDL_Rect dest_rect;
dest_rect.x = OffsetX;
dest_rect.y = OffsetY;
dest_rect.w = RESOLUTION_WIDTH;
dest_rect.h = RESOLUTION_HEIGHT;
SDL_RenderCopy(Renderer, Buffer->Texture, 0,
(const SDL_Rect *)(&dest_rect));
}
@@ -282,7 +302,9 @@ static void SDLInitControllers()
{
int MaxJoysticks = SDL_NumJoysticks();
int ControllerIndex = 0;
for(int JoystickIndex = 0; JoystickIndex < MaxJoysticks; JoystickIndex++)
int JoystickIndex;
for(JoystickIndex = 0; JoystickIndex < MaxJoysticks; JoystickIndex++)
{
if(!SDL_IsGameController(JoystickIndex))
continue;
@@ -460,40 +482,40 @@ static void SDLProcessMessages(struct sdl_state *SDLState,
if(Event.key.repeat == 0) {
if(KeyCode == SDLK_w) {
SDLProcessKeyboardMessage(
&KeyboardController->MoveUp, IsDown);
&KeyboardController->u.s.MoveUp, IsDown);
} else if(KeyCode == SDLK_s) {
SDLProcessKeyboardMessage(
&KeyboardController->MoveDown, IsDown);
&KeyboardController->u.s.MoveDown, IsDown);
} else if(KeyCode == SDLK_a) {
SDLProcessKeyboardMessage(
&KeyboardController->MoveLeft, IsDown);
&KeyboardController->u.s.MoveLeft, IsDown);
} else if(KeyCode == SDLK_d) {
SDLProcessKeyboardMessage(
&KeyboardController->MoveRight, IsDown);
&KeyboardController->u.s.MoveRight, IsDown);
} else if(KeyCode == SDLK_q) {
SDLProcessKeyboardMessage(
&KeyboardController->LeftShoulder, IsDown);
&KeyboardController->u.s.LeftShoulder, IsDown);
} else if(KeyCode == SDLK_e) {
SDLProcessKeyboardMessage(
&KeyboardController->RightShoulder, IsDown);
&KeyboardController->u.s.RightShoulder, IsDown);
} else if(KeyCode == SDLK_UP) {
SDLProcessKeyboardMessage(
&KeyboardController->ActionUp, IsDown);
&KeyboardController->u.s.ActionUp, IsDown);
} else if(KeyCode == SDLK_DOWN) {
SDLProcessKeyboardMessage(
&KeyboardController->ActionDown, IsDown);
&KeyboardController->u.s.ActionDown, IsDown);
} else if(KeyCode == SDLK_LEFT) {
SDLProcessKeyboardMessage(
&KeyboardController->ActionLeft, IsDown);
&KeyboardController->u.s.ActionLeft, IsDown);
} else if(KeyCode == SDLK_RIGHT) {
SDLProcessKeyboardMessage(
&KeyboardController->ActionRight, IsDown);
&KeyboardController->u.s.ActionRight, IsDown);
} else if(KeyCode == SDLK_ESCAPE) {
SDLProcessKeyboardMessage(
&KeyboardController->Back, IsDown);
&KeyboardController->u.s.Back, IsDown);
} else if(KeyCode == SDLK_SPACE) {
SDLProcessKeyboardMessage(
&KeyboardController->Start, IsDown);
&KeyboardController->u.s.Start, IsDown);
}
}
@@ -689,10 +711,6 @@ void GetExecutablePath(char *path, size_t path_size)
static void SDLGetEXEDirPath(char *path, size_t path_size)
{
GetExecutablePath(path, path_size);
unsigned int len = str_len(path);
unsigned int i = len;
#if OS_MACOS || OS_LINUX
char delimeter = '/';
#elif OS_WINDOWS
@@ -701,6 +719,12 @@ static void SDLGetEXEDirPath(char *path, size_t path_size)
#error Unknown OS: OS uses forward or backward slashes for paths?
#endif
unsigned int len, i;
GetExecutablePath(path, path_size);
len = str_len(path);
i = len;
while(path[i] != delimeter || i == 0) {
i--;
}
@@ -722,6 +746,9 @@ U64 __rdtsc()
int main(int argc, char *argv[])
{
struct sdl_state SDLState;
SDL_Window *Window;
memset(&SDLState, 0, sizeof(SDLState));
SDLGetEXEDirPath(SDLState.EXEDirPath, sizeof(SDLState.EXEDirPath));
@@ -738,19 +765,21 @@ int main(int argc, char *argv[])
* before everything is ready. */
/* TODO: SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, in non-debug
* build */
SDL_Window *Window = SDL_CreateWindow("Handmade Hero",
Window = SDL_CreateWindow("Handmade Hero",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
RESOLUTION_WIDTH+20, RESOLUTION_HEIGHT+20,
SDL_WINDOW_HIDDEN | SDL_WINDOW_RESIZABLE/* |
SDL_WINDOW_ALWAYS_ON_TOP*/);
if(Window) {
SDL_DisplayMode display_mode;
SDL_Renderer *Renderer;
LastWindow = Window;
SDL_DisplayMode display_mode;
SDL_GetCurrentDisplayMode(0, &display_mode);
SDL_Renderer *Renderer =
SDL_CreateRenderer(Window, -1, SDL_RENDERER_PRESENTVSYNC);
Renderer = SDL_CreateRenderer(Window, -1, SDL_RENDERER_PRESENTVSYNC);
/* NOTE: For future reference, the custom window bar should use
* SDL_SetWindowHitTest to define areas that will be used to drag
@@ -764,11 +793,27 @@ int main(int argc, char *argv[])
/* SDL_RenderPresent(Renderer) is enough to display the window. */
if(Renderer) {
struct thread_context TempContext;
SDL_Cursor *PointerCursor;
SDL_Rect UsableDisplayRect;
struct sdl_window_size CurrentWindowSize;
struct sdl_sound_output SoundOutput;
#if BUILD_DEBUG
void *BaseAddress = (void *)TB(2);
#else
void *BaseAddress = (void *)(0);
#endif
struct game_memory GameMemory;
memset(&TempContext, 0, sizeof(TempContext));
/* TODO: Make it a global. */
SDL_Cursor *PointerCursor =
SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
PointerCursor = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
SDL_SetCursor(PointerCursor);
#if !BUILD_INTERNAL
if(SDL_ShowCursor(SDL_DISABLE) < 0) {
@@ -776,10 +821,8 @@ int main(int argc, char *argv[])
}
#endif
SDL_Rect UsableDisplayRect;
SDL_GetDisplayUsableBounds(0, &UsableDisplayRect);
struct sdl_window_size CurrentWindowSize =
SDLGetBorderedWindowSize(Window);
CurrentWindowSize = SDLGetBorderedWindowSize(Window);
SDLSetBorderedWindowPosition(Window,
(UsableDisplayRect.x + UsableDisplayRect.w -
CurrentWindowSize.Width),
@@ -795,7 +838,6 @@ int main(int argc, char *argv[])
SDLResizeTexture(&GlobalBackbuffer, Renderer,
RESOLUTION_WIDTH, RESOLUTION_HEIGHT);
struct sdl_sound_output SoundOutput;
memset(&SoundOutput, 0, sizeof(SoundOutput));
SoundOutput.SamplesPerSecond = 48000;
SoundOutput.BytesPerSample = sizeof(short) * 2;
@@ -815,14 +857,7 @@ int main(int argc, char *argv[])
/*SoundOutput.Samples = malloc(SoundOutput.SecondaryBufferSize);*/
/* NOTE: calloc auto clears to zero */
/* SDLClearSoundBuffer(&SoundOutput); */
#if BUILD_DEBUG
void *BaseAddress = (void *)TB(2);
#else
void *BaseAddress = (void *)(0);
#endif
struct game_memory GameMemory;
memset(&GameMemory, 0, sizeof(GameMemory));
GameMemory.PermanentStorageSize = MB(64);
GameMemory.TransientStorageSize = GB(1);
@@ -863,24 +898,65 @@ int main(int argc, char *argv[])
unsigned long long LastCounter;
unsigned long long LastCycleCount;
int MonitorRefreshHz = SDLGetWindowRefreshRate(Window);
/*GameUpdateHz = MonitorRefreshHz;*/
int GameUpdateHz = 30; /* NOTE: Temporarily target 30 FPS. */
float TargetSecondsPerFrame = 1.0f / (float)GameUpdateHz;
int MonitorRefreshHz, GameUpdateHz;
float TargetSecondsPerFrame;
struct game_input *NewInput = &Input[0];
struct game_input *OldInput = &Input[1];
struct game_input *NewInput, *OldInput;
struct sdl_game_code Game;
unsigned long long FPSLastCounter;
MonitorRefreshHz = SDLGetWindowRefreshRate(Window);
/*GameUpdateHz = MonitorRefreshHz;*/
GameUpdateHz = 30; /* NOTE: Temporarily target 30 FPS. */
TargetSecondsPerFrame = 1.0f / (float)GameUpdateHz;
NewInput = &Input[0];
OldInput = &Input[1];
memset(Input, 0, sizeof(Input));
GlobalPerfCountFrequency = SDL_GetPerformanceFrequency();
struct sdl_game_code Game = SDLLoadGameCode(SDLState.DLLPath);
Game = SDLLoadGameCode(SDLState.DLLPath);
SDL_ShowWindow(Window); // Everything is ready; display the window.
unsigned long long FPSLastCounter = SDLGetWallClock();
FPSLastCounter = SDLGetWallClock();
while(GlobalRunning) {
unsigned int NewDLLWriteTime;
int MouseX, MouseY;
int WindowX, WindowY;
unsigned int SDLMouseButtons;
struct game_controller_input *OldKeyboardController,
*NewKeyboardController;
struct game_controller_input ZeroController;
unsigned int ButtonIndex;
unsigned int ControllerIndex;
int TargetQueueBytes;
int BytesToWrite;
struct game_sound_output_buffer SoundBuffer;
struct thread_context Context;
struct game_offscreen_buffer Buffer;
unsigned long long WorkCounter;
float WorkSecondsElapsed;
float SecondsElapsedForFrame;
unsigned long long EndCounter;
float MSPerFrame;
float FPS;
NewInput->dtForFrame = TargetSecondsPerFrame;
LastCounter = SDLGetWallClock();
@@ -891,24 +967,20 @@ int main(int argc, char *argv[])
* on MacOS ARM; I have not checked MacOS x64. */
LastCycleCount = __rdtsc();
unsigned int NewDLLWriteTime =
GetLastWriteTime(SDLState.DLLPath);
NewDLLWriteTime = GetLastWriteTime(SDLState.DLLPath);
if(NewDLLWriteTime > Game.DLLLastWriteTime) {
SDLUnloadGameCode(&Game);
Game = SDLLoadGameCode(SDLState.DLLPath);
}
int MouseX, MouseY;
SDL_GetGlobalMouseState(&MouseX, &MouseY);
int WindowX, WindowY;
SDL_GetWindowPosition(Window, &WindowX, &WindowY);
NewInput->MouseX = MouseX - WindowX;
NewInput->MouseY = MouseY - WindowY;
unsigned int SDLMouseButtons =
SDL_GetMouseState(NULL, NULL);
SDLMouseButtons = SDL_GetMouseState(NULL, NULL);
NewInput->MouseZ = 0; /* TODO: Support mouse wheel? */
SDLProcessKeyboardMessage(&(NewInput->MouseButtons[0]),
SDL_BUTTON_LMASK & SDLMouseButtons);
@@ -923,49 +995,53 @@ int main(int argc, char *argv[])
SDLProcessKeyboardMessage(&(NewInput->MouseButtons[4]),
SDL_BUTTON_X2MASK & SDLMouseButtons);
struct game_controller_input *OldKeyboardController =
GetController(OldInput, 0);
struct game_controller_input *NewKeyboardController =
GetController(NewInput, 0);
struct game_controller_input ZeroController;
OldKeyboardController = GetController(OldInput, 0);
NewKeyboardController = GetController(NewInput, 0);
memset(&ZeroController, 0, sizeof(ZeroController));
*NewKeyboardController = ZeroController;
NewKeyboardController->IsConnected = 1;
for(unsigned int ButtonIndex = 0;
for(ButtonIndex = 0;
ButtonIndex <
ARRAY_SIZE(NewKeyboardController->Buttons);
ARRAY_SIZE(NewKeyboardController->u.Buttons);
ButtonIndex++)
{
NewKeyboardController->
NewKeyboardController->u.
Buttons[ButtonIndex].EndedDown =
OldKeyboardController->
OldKeyboardController->u.
Buttons[ButtonIndex].EndedDown;
}
SDLProcessMessages(&SDLState, NewKeyboardController);
for(unsigned int ControllerIndex = 0;
for(ControllerIndex = 0;
ControllerIndex < MAX_CONTROLLERS;
ControllerIndex++)
{
struct game_controller_input *OldController =
struct game_controller_input *OldController,
*NewController;
OldController =
GetController(OldInput, ControllerIndex+1);
struct game_controller_input *NewController =
NewController =
GetController(NewInput, ControllerIndex+1);
if(ControllerHandles[ControllerIndex] != 0 &&
SDL_GameControllerGetAttached(
ControllerHandles[ControllerIndex]))
{
short StickX, StickY;
float Threshold;
NewController->IsAnalog =
OldController->IsAnalog;
NewController->IsConnected = 1;
short StickX = SDL_GameControllerGetAxis(
StickX = SDL_GameControllerGetAxis(
ControllerHandles[ControllerIndex],
SDL_CONTROLLER_AXIS_LEFTX);
short StickY = SDL_GameControllerGetAxis(
StickY = SDL_GameControllerGetAxis(
ControllerHandles[ControllerIndex],
SDL_CONTROLLER_AXIS_LEFTY);
@@ -1015,94 +1091,91 @@ int main(int argc, char *argv[])
NewController->IsAnalog = 0;
}
float Threshold = 0.5f;
Threshold = 0.5f;
SDLProcessInputDigitalButton(
ControllerHandles[(NewController->
StickAverageY < -Threshold) ? 1 : 0],
&OldController->MoveUp,
&OldController->u.s.MoveUp,
SDL_CONTROLLER_BUTTON_A,
&NewController->MoveUp);
&NewController->u.s.MoveUp);
SDLProcessInputDigitalButton(
ControllerHandles[(NewController->
StickAverageY > Threshold) ? 1 : 0],
&OldController->MoveDown,
&OldController->u.s.MoveDown,
SDL_CONTROLLER_BUTTON_A,
&NewController->MoveDown);
&NewController->u.s.MoveDown);
SDLProcessInputDigitalButton(
ControllerHandles[(NewController->
StickAverageX < -Threshold) ? 1 : 0],
&OldController->MoveLeft,
&OldController->u.s.MoveLeft,
SDL_CONTROLLER_BUTTON_A,
&NewController->MoveLeft);
&NewController->u.s.MoveLeft);
SDLProcessInputDigitalButton(
ControllerHandles[(NewController->
StickAverageX > Threshold) ? 1 : 0],
&OldController->MoveRight,
&OldController->u.s.MoveRight,
SDL_CONTROLLER_BUTTON_A,
&NewController->MoveRight);
&NewController->u.s.MoveRight);
SDLProcessInputDigitalButton(
ControllerHandles[ControllerIndex],
&OldController->ActionDown,
&OldController->u.s.ActionDown,
SDL_CONTROLLER_BUTTON_A,
&NewController->ActionDown);
&NewController->u.s.ActionDown);
SDLProcessInputDigitalButton(
ControllerHandles[ControllerIndex],
&OldController->ActionRight,
&OldController->u.s.ActionRight,
SDL_CONTROLLER_BUTTON_B,
&NewController->ActionRight);
&NewController->u.s.ActionRight);
SDLProcessInputDigitalButton(
ControllerHandles[ControllerIndex],
&OldController->ActionLeft,
&OldController->u.s.ActionLeft,
SDL_CONTROLLER_BUTTON_X,
&NewController->ActionLeft);
&NewController->u.s.ActionLeft);
SDLProcessInputDigitalButton(
ControllerHandles[ControllerIndex],
&OldController->ActionUp,
&OldController->u.s.ActionUp,
SDL_CONTROLLER_BUTTON_Y,
&NewController->ActionUp);
&NewController->u.s.ActionUp);
SDLProcessInputDigitalButton(
ControllerHandles[ControllerIndex],
&OldController->LeftShoulder,
&OldController->u.s.LeftShoulder,
SDL_CONTROLLER_BUTTON_LEFTSHOULDER,
&NewController->LeftShoulder);
&NewController->u.s.LeftShoulder);
SDLProcessInputDigitalButton(
ControllerHandles[ControllerIndex],
&OldController->RightShoulder,
&OldController->u.s.RightShoulder,
SDL_CONTROLLER_BUTTON_RIGHTSHOULDER,
&NewController->RightShoulder);
&NewController->u.s.RightShoulder);
SDLProcessInputDigitalButton(
ControllerHandles[ControllerIndex],
&OldController->Start,
&OldController->u.s.Start,
SDL_CONTROLLER_BUTTON_START,
&NewController->Start);
&NewController->u.s.Start);
SDLProcessInputDigitalButton(
ControllerHandles[ControllerIndex],
&OldController->Back,
&OldController->u.s.Back,
SDL_CONTROLLER_BUTTON_BACK,
&NewController->Back);
&NewController->u.s.Back);
} else {
/* NOTE: This controller is not plugged in. */
NewController->IsConnected = 0;
}
}
int TargetQueueBytes = SoundOutput.LatencySampleCount *
TargetQueueBytes = SoundOutput.LatencySampleCount *
SoundOutput.BytesPerSample;
int BytesToWrite =
BytesToWrite =
TargetQueueBytes - SDL_GetQueuedAudioSize(1);
struct game_sound_output_buffer SoundBuffer;
SoundBuffer.SamplesPerSecond =
SoundOutput.SamplesPerSecond;
SoundBuffer.SampleCount =
BytesToWrite / SoundOutput.BytesPerSample;
SoundBuffer.Samples = (short *)SoundOutput.Samples;
struct thread_context Context;
memset(&Context, 0, sizeof(Context));
struct game_offscreen_buffer Buffer;
Buffer.Memory = GlobalBackbuffer.Memory;
Buffer.Width = GlobalBackbuffer.Width;
Buffer.Height = GlobalBackbuffer.Height;
@@ -1117,11 +1190,11 @@ int main(int argc, char *argv[])
SDLFillSoundBuffer(&SoundOutput, BytesToWrite);
unsigned long long WorkCounter = SDLGetWallClock();
float WorkSecondsElapsed =
WorkCounter = SDLGetWallClock();
WorkSecondsElapsed =
SDLGetSecondsElapsed(LastCounter, WorkCounter);
float SecondsElapsedForFrame = WorkSecondsElapsed;
SecondsElapsedForFrame = WorkSecondsElapsed;
if(SecondsElapsedForFrame < TargetSecondsPerFrame) {
while(SecondsElapsedForFrame < TargetSecondsPerFrame)
{
@@ -1141,15 +1214,15 @@ int main(int argc, char *argv[])
/* TODO: Logging. */
}
unsigned long long EndCounter = SDLGetWallClock();
EndCounter = SDLGetWallClock();
SDLDisplayBufferInWindow(&GlobalBackbuffer, Window,
Renderer);
float MSPerFrame =
MSPerFrame =
1000.0f *
SDLGetSecondsElapsed(LastCounter, EndCounter);
float FPS = 1000.0f / MSPerFrame;
FPS = 1000.0f / MSPerFrame;
LastCounter = EndCounter;