From a5cff85e227dd557f71950c03240623c0f4a7698 Mon Sep 17 00:00:00 2001 From: igor Date: Sun, 15 Mar 2026 00:05:54 -0700 Subject: [PATCH] Clean up after core.h updates. --- handmade.cpp | 16 ++++---- handmade.h | 10 ++--- sdl_handmade.cpp | 105 ++++++++++++++++++++++++----------------------- 3 files changed, 68 insertions(+), 63 deletions(-) diff --git a/handmade.cpp b/handmade.cpp index da904e6..eef79fd 100644 --- a/handmade.cpp +++ b/handmade.cpp @@ -1,8 +1,10 @@ #include "handmade.h" -internal void GameOutputSound(game_sound_output_buffer *SoundBuffer, S32 ToneHz) +#include + +static void GameOutputSound(game_sound_output_buffer *SoundBuffer, S32 ToneHz) { - local_persist F32 tSine; + static F32 tSine; S16 ToneVolume = 8000; S32 WavePeriod = SoundBuffer->SamplesPerSecond / ToneHz; @@ -13,11 +15,11 @@ internal void GameOutputSound(game_sound_output_buffer *SoundBuffer, S32 ToneHz) *SampleOut++ = SampleValue; *SampleOut++ = SampleValue; - tSine += 2.0f * pi32 * 1.0f / (F32)WavePeriod; + tSine += 2.0f * PI * 1.0f / (F32)WavePeriod; } } -internal void RenderWeirdGradient(game_offscreen_buffer *Buffer, S32 XOffset, S32 YOffset) +static void RenderWeirdGradient(game_offscreen_buffer *Buffer, S32 XOffset, S32 YOffset) { S32 Width = Buffer->Width; S32 Height = Buffer->Height; @@ -35,9 +37,9 @@ internal void RenderWeirdGradient(game_offscreen_buffer *Buffer, S32 XOffset, S3 } } -internal void GameUpdateAndRender(game_memory *Memory, game_input *Input, game_offscreen_buffer *Buffer, game_sound_output_buffer *SoundBuffer) +static void GameUpdateAndRender(game_memory *Memory, game_input *Input, game_offscreen_buffer *Buffer, game_sound_output_buffer *SoundBuffer) { - Assert((&Input->Controllers[0].Terminator - &Input->Controllers[0].Buttons[0]) == ArrayCount(Input->Controllers[0].Buttons)); + Assert((&Input->Controllers[0].Terminator - &Input->Controllers[0].Buttons[0]) == ARRAY_SIZE(Input->Controllers[0].Buttons)); Assert(sizeof(game_state) <= Memory->PermanentStorageSize); game_state *GameState = (game_state *)Memory->PermanentStorage; @@ -58,7 +60,7 @@ internal void GameUpdateAndRender(game_memory *Memory, game_input *Input, game_o // GameState.GreenOffset; // GameState.BlueOffset; - for(U32 ControllerIndex = 0; ControllerIndex < ArrayCount(Input->Controllers); ControllerIndex++) { + for(U32 ControllerIndex = 0; ControllerIndex < ARRAY_SIZE(Input->Controllers); ControllerIndex++) { game_controller_input *Controller = GetController(Input, ControllerIndex); if(Controller->IsAnalog) { GameState->GreenOffset += (int)(4.0f*(Controller->StickAverageY)); diff --git a/handmade.h b/handmade.h index 74a1ba5..aa65503 100644 --- a/handmade.h +++ b/handmade.h @@ -13,9 +13,9 @@ typedef struct debug_read_file_result { void *Contents; } debug_read_file_result; -internal debug_read_file_result DEBUGPlatformReadEntireFile(const char *Filename); -internal B32 DEBUGPlatformWriteEntireFile(const char *Filename, U32 MemorySize, void *Memory); -internal void DEBUGPlatformFreeFileMemory(void *Memory); +static debug_read_file_result DEBUGPlatformReadEntireFile(const char *Filename); +static B32 DEBUGPlatformWriteEntireFile(const char *Filename, U32 MemorySize, void *Memory); +static void DEBUGPlatformFreeFileMemory(void *Memory); #endif /////////////////////////////////////////////////////////////////////////////// @@ -81,7 +81,7 @@ typedef struct game_input { inline game_controller_input *GetController(game_input *Input, S32 ControllerIndex) { - Assert((U32)ControllerIndex < ArrayCount(Input->Controllers)); + Assert((U32)ControllerIndex < ARRAY_SIZE(Input->Controllers)); game_controller_input *Result = &(Input->Controllers[ControllerIndex]); return Result; } @@ -101,7 +101,7 @@ typedef struct game_memory { // F32 SecondsElapsed; // } game_clocks; -internal void GameUpdateAndRender(game_memory *Memory, game_input *Input, game_offscreen_buffer *Buffer, game_sound_output_buffer *SoundBuffer); +static void GameUpdateAndRender(game_memory *Memory, game_input *Input, game_offscreen_buffer *Buffer, game_sound_output_buffer *SoundBuffer); // // diff --git a/sdl_handmade.cpp b/sdl_handmade.cpp index fa32fa6..7cf9b28 100644 --- a/sdl_handmade.cpp +++ b/sdl_handmade.cpp @@ -1,10 +1,12 @@ -#include "base_core.h" +#include "core.h" #include "sdl_handmade.h" #include "handmade.cpp" #include #include // mmap #include // malloc, calloc +#include // memset +#include #include #include // fstat @@ -16,24 +18,24 @@ #include #endif -global bool GlobalRunning; +static bool GlobalRunning; -global U64 GlobalPerfCountFrequency; +static U64 GlobalPerfCountFrequency; #define CONTROLLER_LEFT_THUMB_DEADZONE 8000 #define MAX_CONTROLLERS 4 SDL_GameController *ControllerHandles[MAX_CONTROLLERS]; -global offscreen_buffer GlobalBackbuffer; +static offscreen_buffer GlobalBackbuffer; -internal U32 SafeTruncateUInt64(U64 Value) +static U32 SafeTruncateUInt64(U64 Value) { Assert(Value <= 0xFFFFFFFF); U32 Result = (U32)Value; return Result; } -internal debug_read_file_result DEBUGPlatformReadEntireFile(const char *Filename) +static debug_read_file_result DEBUGPlatformReadEntireFile(const char *Filename) { debug_read_file_result Result = {0}; S32 FileHandle = open(Filename, O_RDONLY); @@ -75,7 +77,7 @@ internal debug_read_file_result DEBUGPlatformReadEntireFile(const char *Filename return Result; } -internal B32 DEBUGPlatformWriteEntireFile(const char *Filename, U32 MemorySize, void *Memory) +static B32 DEBUGPlatformWriteEntireFile(const char *Filename, U32 MemorySize, void *Memory) { S32 FileHandle = open(Filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); if(FileHandle == -1) @@ -98,21 +100,21 @@ internal B32 DEBUGPlatformWriteEntireFile(const char *Filename, U32 MemorySize, return true; } -internal void DEBUGPlatformFreeFileMemory(void *Memory) +static void DEBUGPlatformFreeFileMemory(void *Memory) { if(Memory) free(Memory); } -sdl_window_dimension SDLGetWindowDimension(SDL_Window *Window) +static sdl_window_dimension SDLGetWindowDimension(SDL_Window *Window) { sdl_window_dimension Dimension; SDL_GetWindowSize(Window, &Dimension.Width, &Dimension.Height); return Dimension; } -internal void SDLResizeTexture(offscreen_buffer *Buffer, SDL_Renderer *Renderer, int Width, int Height) +static void SDLResizeTexture(offscreen_buffer *Buffer, SDL_Renderer *Renderer, int Width, int Height) { if(Buffer->Texture) SDL_DestroyTexture(Buffer->Texture); @@ -130,7 +132,7 @@ internal void SDLResizeTexture(offscreen_buffer *Buffer, SDL_Renderer *Renderer, Buffer->Pitch = Width * BytesPerPixel; } -internal void SDLDisplayBufferInWindow(offscreen_buffer Buffer, SDL_Window *Window, SDL_Renderer *Renderer) +static void SDLDisplayBufferInWindow(offscreen_buffer Buffer, SDL_Window *Window, SDL_Renderer *Renderer) { if(SDL_UpdateTexture(Buffer.Texture, 0, Buffer.Memory, Buffer.Pitch)) { // TODO: Do something about this error! @@ -140,7 +142,7 @@ internal void SDLDisplayBufferInWindow(offscreen_buffer Buffer, SDL_Window *Wind SDL_RenderPresent(Renderer); } -internal void SDLInitControllers() +static void SDLInitControllers() { int MaxJoysticks = SDL_NumJoysticks(); int ControllerIndex = 0; @@ -155,7 +157,7 @@ internal void SDLInitControllers() } /* -internal void SDLDeinitControllers() +static void SDLDeinitControllers() { for(int ControllerIndex = 0; ControllerIndex < MAX_CONTROLLERS; ControllerIndex++) { if (ControllerHandles[ControllerIndex]) @@ -165,7 +167,7 @@ internal void SDLDeinitControllers() */ -internal void SDLInitSound(S32 SamplesPerSecond, S32 BufferSize) +static void SDLInitSound(S32 SamplesPerSecond, S32 BufferSize) { SDL_AudioSpec AudioSettings = {0}; @@ -181,31 +183,31 @@ internal void SDLInitSound(S32 SamplesPerSecond, S32 BufferSize) } } -internal void SDLFillSoundBuffer(sdl_sound_output *SoundOutput, S32 BytesToWrite) +static void SDLFillSoundBuffer(sdl_sound_output *SoundOutput, S32 BytesToWrite) { SDL_QueueAudio(1, SoundOutput->Samples, BytesToWrite); } -internal void SDLClearSoundBuffer(sdl_sound_output *SoundOutput) +static void SDLClearSoundBuffer(sdl_sound_output *SoundOutput) { memset(SoundOutput->Samples, 0, SoundOutput->SecondaryBufferSize); } -internal void SDLProcessKeyboardMessage(game_button_state *NewState, bool IsDown) +static void SDLProcessKeyboardMessage(game_button_state *NewState, bool IsDown) { Assert(NewState->EndedDown != IsDown); NewState->EndedDown = IsDown; ++NewState->HalfTransitionCount; } -internal void SDLProcessInputDigitalButton(SDL_GameController *Controller, game_button_state *OldState, +static void SDLProcessInputDigitalButton(SDL_GameController *Controller, game_button_state *OldState, SDL_GameControllerButton Button, game_button_state *NewState) { NewState->EndedDown = SDL_GameControllerGetButton(Controller, Button); NewState->HalfTransitionCount = (OldState->EndedDown != NewState->EndedDown) ? 1 : 0; } -internal void HandleEvent(SDL_Event *Event) +static void HandleEvent(SDL_Event *Event) { switch(Event->type) { case SDL_QUIT: { @@ -229,7 +231,7 @@ internal void HandleEvent(SDL_Event *Event) } } -internal void SDLProcessMessages(game_controller_input *KeyboardController) +static void SDLProcessMessages(game_controller_input *KeyboardController) { SDL_Event Event; while(SDL_PollEvent(&Event)) { @@ -286,7 +288,7 @@ internal void SDLProcessMessages(game_controller_input *KeyboardController) } } -internal F32 SDLProcessInputStickValue(F32 Value, S32 DeadZoneThreshold) +static float SDLProcessInputStickValue(F32 Value, S32 DeadZoneThreshold) { F32 Result = 0.0f; if(Value < -DeadZoneThreshold) @@ -297,7 +299,7 @@ internal F32 SDLProcessInputStickValue(F32 Value, S32 DeadZoneThreshold) } #define DEFAULT_REFRESH_RATE 60 -internal S32 SDLGetWindowRefreshRate(SDL_Window *Window) +static int SDLGetWindowRefreshRate(SDL_Window *Window) { int DisplayIndex = SDL_GetWindowDisplayIndex(Window); SDL_DisplayMode Mode; @@ -309,19 +311,20 @@ internal S32 SDLGetWindowRefreshRate(SDL_Window *Window) return Mode.refresh_rate; } -internal U64 SDLGetWallClock() +static unsigned long long SDLGetWallClock() { return SDL_GetPerformanceCounter(); } -internal F32 SDLGetSecondsElapsed(U64 Start, U64 End) +// WARNING: Might lose lots of precision because of conversion from 64-bit to float. +static float SDLGetSecondsElapsed(unsigned long long Start, unsigned long long End) { - return (F32)(End - Start) / (F32)GlobalPerfCountFrequency; + return (float)(End - Start) / (float)GlobalPerfCountFrequency; } -S32 main(int argc, char *argv[]) +int main(int argc, char *argv[]) { - if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_GAMECONTROLLER|SDL_INIT_AUDIO)) { + if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER | SDL_INIT_AUDIO)) { /* TODO: This didn't work . . . */ } @@ -379,9 +382,9 @@ S32 main(int argc, char *argv[]) GlobalPerfCountFrequency = SDL_GetPerformanceFrequency(); - U64 LastCounter; + unsigned long long LastCounter; #if __x86_64__ || __i386__ - U64 LastCycleCount; + unsigned long long LastCycleCount; #endif while(GlobalRunning) { @@ -391,9 +394,9 @@ S32 main(int argc, char *argv[]) LastCycleCount = __rdtsc(); // NOTE: rdtsc reports clock cylces, however it is not meant for really precise profiler work as the value returned is varied. #endif // Also, __rdtsc is not available on MacOS ARM; I have not checked MacOS x64. - S32 MonitorRefreshHz = SDLGetWindowRefreshRate(Window); - S32 GameUpdateHz = MonitorRefreshHz / 2; - F32 TargetSecondsPerFrame = 1.0f / (F32)GameUpdateHz; + int MonitorRefreshHz = SDLGetWindowRefreshRate(Window); + int GameUpdateHz = MonitorRefreshHz / 2; + float TargetSecondsPerFrame = 1.0f / (F32)GameUpdateHz; game_controller_input *OldKeyboardController = GetController(OldInput, 0); game_controller_input *NewKeyboardController = GetController(NewInput, 0); @@ -401,21 +404,21 @@ S32 main(int argc, char *argv[]) *NewKeyboardController = ZeroController; NewKeyboardController->IsConnected = true; - for(U32 ButtonIndex = 0; ButtonIndex < ArrayCount(NewKeyboardController->Buttons); ButtonIndex++) { + for(unsigned int ButtonIndex = 0; ButtonIndex < ARRAY_SIZE(NewKeyboardController->Buttons); ButtonIndex++) { NewKeyboardController->Buttons[ButtonIndex].EndedDown = OldKeyboardController->Buttons[ButtonIndex].EndedDown; } SDLProcessMessages(NewKeyboardController); - for(S32 ControllerIndex = 0; ControllerIndex < MAX_CONTROLLERS; ControllerIndex++) { + for(unsigned int ControllerIndex = 0; ControllerIndex < MAX_CONTROLLERS; ControllerIndex++) { game_controller_input *OldController = GetController(OldInput, ControllerIndex+1); game_controller_input *NewController = GetController(NewInput, ControllerIndex+1); if(ControllerHandles[ControllerIndex] != 0 && SDL_GameControllerGetAttached(ControllerHandles[ControllerIndex])) { NewController->IsConnected = true; - S16 StickX = SDL_GameControllerGetAxis(ControllerHandles[ControllerIndex], SDL_CONTROLLER_AXIS_LEFTX); - S16 StickY = SDL_GameControllerGetAxis(ControllerHandles[ControllerIndex], SDL_CONTROLLER_AXIS_LEFTY); + short StickX = SDL_GameControllerGetAxis(ControllerHandles[ControllerIndex], SDL_CONTROLLER_AXIS_LEFTX); + short StickY = SDL_GameControllerGetAxis(ControllerHandles[ControllerIndex], SDL_CONTROLLER_AXIS_LEFTY); NewController->IsAnalog = true; NewController->StickAverageX = SDLProcessInputStickValue((F32)StickX, CONTROLLER_LEFT_THUMB_DEADZONE); @@ -445,7 +448,7 @@ S32 main(int argc, char *argv[]) NewController->IsAnalog = false; } - F32 Threshold = 0.5f; + float Threshold = 0.5f; SDLProcessInputDigitalButton(ControllerHandles[(NewController->StickAverageY < -Threshold) ? 1 : 0], &OldController->MoveUp, SDL_CONTROLLER_BUTTON_A, &NewController->MoveUp); SDLProcessInputDigitalButton(ControllerHandles[(NewController->StickAverageY > Threshold) ? 1 : 0], &OldController->MoveDown, SDL_CONTROLLER_BUTTON_A, &NewController->MoveDown); SDLProcessInputDigitalButton(ControllerHandles[(NewController->StickAverageX < -Threshold) ? 1 : 0], &OldController->MoveLeft, SDL_CONTROLLER_BUTTON_A, &NewController->MoveLeft); @@ -466,12 +469,12 @@ S32 main(int argc, char *argv[]) } } - S32 TargetQueueBytes = SoundOutput.LatencySampleCount * SoundOutput.BytesPerSample; - S32 BytesToWrite = TargetQueueBytes - SDL_GetQueuedAudioSize(1); + int TargetQueueBytes = SoundOutput.LatencySampleCount * SoundOutput.BytesPerSample; + int BytesToWrite = TargetQueueBytes - SDL_GetQueuedAudioSize(1); game_sound_output_buffer SoundBuffer; SoundBuffer.SamplesPerSecond = SoundOutput.SamplesPerSecond; SoundBuffer.SampleCount = BytesToWrite / SoundOutput.BytesPerSample; - SoundBuffer.Samples = (S16 *)SoundOutput.Samples; + SoundBuffer.Samples = (short *)SoundOutput.Samples; game_offscreen_buffer Buffer; Buffer.Memory = GlobalBackbuffer.Memory; @@ -481,40 +484,40 @@ S32 main(int argc, char *argv[]) GameUpdateAndRender(&GameMemory, NewInput, &Buffer, &SoundBuffer); SDLFillSoundBuffer(&SoundOutput, BytesToWrite); - U64 WorkCounter = SDLGetWallClock(); - F32 WorkSecondsElapsed = SDLGetSecondsElapsed(LastCounter, WorkCounter); + unsigned long WorkCounter = SDLGetWallClock(); + float WorkSecondsElapsed = SDLGetSecondsElapsed(LastCounter, WorkCounter); - F32 SecondsElapsedForFrame = WorkSecondsElapsed; + float SecondsElapsedForFrame = WorkSecondsElapsed; if(SecondsElapsedForFrame < TargetSecondsPerFrame) { while(SecondsElapsedForFrame < TargetSecondsPerFrame) { SecondsElapsedForFrame = SDLGetSecondsElapsed(LastCounter, SDLGetWallClock()); - SDL_Delay((U32)((TargetSecondsPerFrame - SecondsElapsedForFrame) * 1000.0f)); + SDL_Delay((unsigned int)((TargetSecondsPerFrame - SecondsElapsedForFrame) * 1000.0)); } } else { // TODO: Missed frame rate! // TODO: Logging. } - U64 EndCounter = SDLGetWallClock(); + unsigned long long EndCounter = SDLGetWallClock(); SDLDisplayBufferInWindow(GlobalBackbuffer, Window, Renderer); - F64 MSPerFrame = 1000.0f * SDLGetSecondsElapsed(LastCounter, EndCounter); - F64 FPS = 0.0f; + double MSPerFrame = 1000.0 * SDLGetSecondsElapsed(LastCounter, EndCounter); + double FPS = 0.0; LastCounter = EndCounter; #if __x86_64__ || __i386__ - U64 EndCycleCount = __rdtsc(); - U64 CyclesElapsed = EndCycleCount - LastCycleCount; - F64 MCPF = ((F64)CyclesElapsed / (1000.0f * 1000.0f)); + unsigned long long EndCycleCount = __rdtsc(); + unsigned long long CyclesElapsed = EndCycleCount - LastCycleCount; + double MCPF = ((double)CyclesElapsed / (1000.0 * 1000.0)); fprintf(stderr, "%.02f ms/f, %.02f/s, %.02f mc/f\n", MSPerFrame, FPS, MCPF); LastCycleCount = EndCycleCount; #else fprintf(stderr, "%.02f ms/f, %.02f/s\n", MSPerFrame, FPS); #endif - Swap(game_input *, NewInput, OldInput); + SWAP(game_input *, NewInput, OldInput); } } else { // TODO: Failed to allocate Samples and Sound memory . . .