diff --git a/handmade.c b/handmade.c index 2ab9936..cacdede 100644 --- a/handmade.c +++ b/handmade.c @@ -59,8 +59,7 @@ static S32 RoundF32toS32(F32 Real32) static void DrawRectangle(game_offscreen_buffer *Buffer, F32 RealMinX, F32 RealMinY, F32 RealMaxX, F32 RealMaxY, - U32 Color) - /*F32 R, F32 G, F32 B)*/ + F32 R, F32 G, F32 B) { S32 MinX = RoundF32toS32(RealMinX); S32 MinY = RoundF32toS32(RealMinY); @@ -72,6 +71,7 @@ static void DrawRectangle(game_offscreen_buffer *Buffer, U8 *Row; S32 Y, X; + U32 Color; if(MinX < 0) MinX = 0; @@ -83,6 +83,10 @@ static void DrawRectangle(game_offscreen_buffer *Buffer, if(MaxY > Height) MaxY = Height; + Color = (U32)((RoundF32toS32(R * 255.0f) << 16) | + (RoundF32toS32(G * 255.0f) << 8) | + (RoundF32toS32(B * 255.0f) << 0)); + Row = ((U8 *)Buffer->Memory + MinX*Buffer->BytesPerPixel + MinY*Buffer->Pitch); for(Y = MinY; Y < MaxY; Y++) { @@ -123,6 +127,21 @@ void UpdateAndRender(thread_context *Thread, game_memory *Memory, { game_state *GameState; U32 ControllerIndex; + S32 Row, Column; + U32 TileMap[9][17] = { + {1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1}, + {1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1}, + {1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1}, + {1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1}, + {0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0}, + {1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1}, + {1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1}, + {1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1}, + {1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1}, + }; + F32 UpperLeftX, UpperLeftY, TileWidth, TileHeight; + F32 PlayerR, PlayerG, PlayerB, PlayerWidth, PlayerHeight, + PlayerLeft, PlayerTop; ASSERT((&Input->Controllers[0].u.buttons.Terminator - &Input->Controllers[0].u.Buttons[0]) == @@ -142,17 +161,75 @@ void UpdateAndRender(thread_context *Thread, game_memory *Memory, GetController(Input, ControllerIndex); if(Controller->IsAnalog) { } else { + F32 dPlayerX = 0.0f; + F32 dPlayerY = 0.0f; + + if(Controller->u.buttons.MoveUp.EndedDown) { + dPlayerY = -1.0f; + } + if(Controller->u.buttons.MoveDown.EndedDown) { + dPlayerY = 1.0f; + } + if(Controller->u.buttons.MoveLeft.EndedDown) { + dPlayerX = -1.0f; + } + if(Controller->u.buttons.MoveRight.EndedDown) { + dPlayerX = 1.0f; + } + + dPlayerX *= 64.0f; + dPlayerY *= 64.0f; + + GameState->PlayerX += Input->dtForFrame * dPlayerX; + GameState->PlayerY += Input->dtForFrame * dPlayerY; + + GameState->PlayerX = RoundF32toS32(GameState->PlayerX); + GameState->PlayerY = RoundF32toS32(GameState->PlayerY); } } - 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, 0.0f, 0.0f, + (F32)(Buffer->Width), (F32)(Buffer->Height), + 0.0f, 1.0f, 0.0f); + + UpperLeftX = 0.0f; + UpperLeftY = 0.0f; + TileWidth = 57.0f; + TileHeight = 57.0f; + + for(Row = 0; Row < 9; Row++) { + for(Column = 0; Column < 17; Column++) { + F32 MinX, MinY, MaxX, MaxY; + U32 TileID = TileMap[Row][Column]; + F32 Gray = 0.5f; + if(TileID) { + Gray = 1.0f; + } + MinX = UpperLeftX + (F32)(Column) * TileWidth; + MinY = UpperLeftY + (F32)(Row) * TileHeight; + MaxX = MinX + TileWidth; + MaxY = MinY + TileHeight; + DrawRectangle(Buffer, MinX, MinY, MaxX, MaxY, Gray, Gray, Gray); + } + } + + PlayerR = 1.0f; + PlayerG = 0.0f; + PlayerB = 0.0f; + PlayerWidth = 0.75f * TileWidth; + PlayerHeight = TileHeight; + PlayerLeft = GameState->PlayerX - 0.5f*PlayerWidth; + PlayerTop = GameState->PlayerY - PlayerHeight; + DrawRectangle(Buffer, + PlayerLeft, PlayerTop, + PlayerLeft + PlayerWidth, PlayerTop + PlayerHeight, + PlayerR, PlayerG, PlayerB); DrawRectangle(Buffer, (F32)(Input->MouseX), (F32)(Input->MouseY), (F32)(Input->MouseX) + 10.0f, (F32)(Input->MouseY) + 10.0f, - 0xFF0000); + 1.0f, 0.5f, 0.0f); + GameOutputSound(GameState, SoundBuffer, 400); } diff --git a/handmade.h b/handmade.h index 2e36063..3b01c61 100644 --- a/handmade.h +++ b/handmade.h @@ -87,7 +87,7 @@ typedef struct game_input { game_button_state MouseButtons[5]; S32 MouseX, MouseY, MouseZ; /* NOTE: MouseZ is the wheel. */ - F32 SecondsToAdvanceOverUpdate; + F32 dtForFrame; game_controller_input Controllers[5]; } game_input; @@ -137,7 +137,8 @@ void UpdateAndRender(thread_context *Thread, game_memory *Memory, */ typedef struct game_state { - void *filler; + F32 PlayerX; + F32 PlayerY; } game_state; #endif diff --git a/sdl_handmade.c b/sdl_handmade.c index 6039d4f..9251cc7 100644 --- a/sdl_handmade.c +++ b/sdl_handmade.c @@ -831,8 +831,6 @@ S32 main(S32 argc, char *argv[]) OldInput = &Input[1]; memset(&Input, 0, sizeof(Input)); - NewInput->SecondsToAdvanceOverUpdate = TargetSecondsPerFrame; - GlobalPerfCountFrequency = SDL_GetPerformanceFrequency(); Game = SDLLoadGameCode(SDLState.DLLPath); @@ -855,6 +853,8 @@ S32 main(S32 argc, char *argv[]) F64 MCPF; #endif + NewInput->dtForFrame = TargetSecondsPerFrame; + LastCounter = SDLGetWallClock(); #if __x86_64__ || __i386__