From 88f235ede643535586cfc471466e94fbc1f6ed86 Mon Sep 17 00:00:00 2001 From: igor Date: Wed, 1 Apr 2026 19:26:21 -0700 Subject: [PATCH] Day 27 complete. --- handmade.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ handmade.h | 1 + sdl_handmade.c | 25 +++++++++++++------ 3 files changed, 84 insertions(+), 8 deletions(-) diff --git a/handmade.c b/handmade.c index 44aa6b9..2ab9936 100644 --- a/handmade.c +++ b/handmade.c @@ -37,6 +37,64 @@ static void GameOutputSound(game_state *GameState, */ } +static void ClearBackground(game_offscreen_buffer *Buffer) +{ + S32 Width, Height, X; + + Width = Buffer->Width; + Height = Buffer->Height; + + for(X = 0; X < Width * Height; X++) { + U32 *Pixel = &((U32 *)Buffer->Memory)[X]; + *Pixel = 0xFFFFFF; + } +} + +static S32 RoundF32toS32(F32 Real32) +{ + S32 Result = (S32)(Real32 + 0.5f); + return Result; +} + +static void DrawRectangle(game_offscreen_buffer *Buffer, + F32 RealMinX, F32 RealMinY, + F32 RealMaxX, F32 RealMaxY, + U32 Color) + /*F32 R, F32 G, F32 B)*/ +{ + S32 MinX = RoundF32toS32(RealMinX); + S32 MinY = RoundF32toS32(RealMinY); + S32 MaxX = RoundF32toS32(RealMaxX); + S32 MaxY = RoundF32toS32(RealMaxY); + + S32 Width = Buffer->Width; + S32 Height = Buffer->Height; + + U8 *Row; + S32 Y, X; + + if(MinX < 0) + MinX = 0; + if(MinY < 0) + MinY = 0; + + if(MaxX > Width) + MaxX = Width; + if(MaxY > Height) + MaxY = Height; + + Row = ((U8 *)Buffer->Memory + MinX*Buffer->BytesPerPixel + + MinY*Buffer->Pitch); + for(Y = MinY; Y < MaxY; Y++) { + U32 *Pixel = (U32 *)Row; + for(X = MinX; X < MaxX; X++) { + *(U32 *)Pixel = Color; + Pixel++; + } + Row += Buffer->Pitch; + } +} + static void RenderRectangle(game_offscreen_buffer *Buffer, S32 MouseX, S32 MouseY) { @@ -87,6 +145,14 @@ void UpdateAndRender(thread_context *Thread, game_memory *Memory, } } + DrawRectangle(Buffer, 0.0f, 0.0f, (F32)(Buffer->Width), + (F32)(Buffer->Height), 0x000000); + DrawRectangle(Buffer, 10.0f, 10.0f, 20.0f, 20.0f, 0xFFFFFF); + + DrawRectangle(Buffer, + (F32)(Input->MouseX), (F32)(Input->MouseY), + (F32)(Input->MouseX) + 10.0f, (F32)(Input->MouseY) + 10.0f, + 0xFF0000); GameOutputSound(GameState, SoundBuffer, 400); } diff --git a/handmade.h b/handmade.h index ae27746..2e36063 100644 --- a/handmade.h +++ b/handmade.h @@ -35,6 +35,7 @@ typedef struct game_offscreen_buffer { void *Memory; S32 Width; S32 Height; + S32 BytesPerPixel; S32 Pitch; } game_offscreen_buffer; diff --git a/sdl_handmade.c b/sdl_handmade.c index 440ae4c..6039d4f 100644 --- a/sdl_handmade.c +++ b/sdl_handmade.c @@ -25,6 +25,9 @@ #include #endif +#define RESOLUTION_WIDTH 960 +#define RESOLUTION_HEIGHT 540 + static B32 GlobalRunning; static U64 GlobalPerfCountFrequency; @@ -140,7 +143,6 @@ void DEBUGPlatformFreeFileMemory(thread_context *Thread, void *Memory) free(Memory); } - static sdl_window_dimension SDLGetWindowDimension(SDL_Window *Window) { sdl_window_dimension Dimension; @@ -186,8 +188,8 @@ static void SDLDisplayBufferInWindow(offscreen_buffer Buffer, /* SDL_RenderCopy(Renderer, Buffer.Texture, 0, 0); */ dest_rect.x = 0; dest_rect.y = 0; - dest_rect.w = 500; - dest_rect.h = 500; + dest_rect.w = RESOLUTION_WIDTH; + dest_rect.h = RESOLUTION_HEIGHT; SDL_RenderCopy(Renderer, Buffer.Texture, 0, (const SDL_Rect *)(&dest_rect)); SDL_RenderPresent(Renderer); } @@ -705,15 +707,20 @@ S32 main(S32 argc, char *argv[]) * protocol. However, SDL_WINDOW_ALWAYS_ON_TOP does work on KDE * under Wayland. I have not tested GNOME. */ Window = SDL_CreateWindow("Handmade Hero", SDL_WINDOWPOS_UNDEFINED, - SDL_WINDOWPOS_UNDEFINED, 500, 500, - SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALWAYS_ON_TOP); + SDL_WINDOWPOS_UNDEFINED, + RESOLUTION_WIDTH, RESOLUTION_HEIGHT, + SDL_WINDOW_RESIZABLE | + SDL_WINDOW_ALWAYS_ON_TOP); if(Window) { SDL_Renderer *Renderer; + SDL_DisplayMode display_mode; + SDL_GetCurrentDisplayMode(0, &display_mode); + Renderer = SDL_CreateRenderer(Window, -1, SDL_RENDERER_PRESENTVSYNC); - SDL_SetWindowPosition(Window, 900, 300); /* TODO: Check that this - works on Linux. */ + SDL_SetWindowPosition(Window, (display_mode.w - RESOLUTION_WIDTH), + (display_mode.h - RESOLUTION_HEIGHT)); /* "Wayland needs an event loop and rendering or it won't function." * https://github.com/libsdl-org/SDL/issues/7699#issuecomment-1545684792 */ /* NOTE: This means that we need to readraw to even resize the @@ -816,7 +823,8 @@ S32 main(S32 argc, char *argv[]) #endif MonitorRefreshHz = SDLGetWindowRefreshRate(Window); - GameUpdateHz = MonitorRefreshHz; + /*GameUpdateHz = MonitorRefreshHz;*/ + GameUpdateHz = 30; /* NOTE: Temporarily target 30 FPS. */ TargetSecondsPerFrame = 1.0f / (F32)GameUpdateHz; NewInput = &Input[0]; @@ -1070,6 +1078,7 @@ S32 main(S32 argc, char *argv[]) Buffer.Memory = GlobalBackbuffer.Memory; Buffer.Width = GlobalBackbuffer.Width; Buffer.Height = GlobalBackbuffer.Height; + Buffer.BytesPerPixel = 4; Buffer.Pitch = GlobalBackbuffer.Pitch; if(SDLState.InputRecordingIndex) {