Clean up after core.h updates.

This commit is contained in:
2026-03-15 00:05:54 -07:00
parent 58b3e04129
commit a5cff85e22
3 changed files with 68 additions and 63 deletions
+54 -51
View File
@@ -1,10 +1,12 @@
#include "base_core.h"
#include "core.h"
#include "sdl_handmade.h"
#include "handmade.cpp"
#include <SDL.h>
#include <sys/mman.h> // mmap
#include <stdlib.h> // malloc, calloc
#include <string.h> // memset
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h> // fstat
@@ -16,24 +18,24 @@
#include <x86intrin.h>
#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 . . .