Day 29 complete.

This commit is contained in:
2026-04-03 18:05:35 -07:00
parent 457ff9707f
commit cd8f62f530
3 changed files with 209 additions and 43 deletions
+171 -25
View File
@@ -3,6 +3,7 @@
#include "sdl_handmade.h"
#include <math.h>
#include <string.h> /* memset */
static void GameOutputSound(game_state *GameState,
game_sound_output_buffer *SoundBuffer,
@@ -121,35 +122,176 @@ static void RenderRectangle(game_offscreen_buffer *Buffer, S32 MouseX,
}
}
static tile_map *GetTileMap(world *World, S32 TileMapX, S32 TileMapY)
{
tile_map *TileMap = NULL;
if((TileMapX >= 0) && (TileMapX < World->TileMapCountX) &&
(TileMapY >= 0) && (TileMapY < World->TileMapCountY))
{
TileMap =
&World->TileMaps[TileMapY*World->TileMapCountX + TileMapX];
}
return TileMap;
}
static U32 GetTileValueUnchecked(tile_map *TileMap, S32 TileX, S32 TileY)
{
U32 TileMapValue = TileMap->Tiles[TileY*TileMap->CountX + TileX];
return TileMapValue;
}
static B32 IsTileMapPointEmpty(tile_map *TileMap, F32 TestX, F32 TestY)
{
B32 Empty = FALSE;
S32 PlayerTileX =
(S32)((TestX - TileMap->UpperLeftX) / TileMap->TileWidth);
S32 PlayerTileY =
(S32)((TestY - TileMap->UpperLeftY) / TileMap->TileHeight);
if((PlayerTileX >= 0) && (PlayerTileX < TileMap->CountX) &&
(PlayerTileY >= 0) && (PlayerTileY < TileMap->CountY))
{
U32 TileMapValue =
GetTileValueUnchecked(TileMap, PlayerTileX, PlayerTileY);
Empty = (TileMapValue == 0);
}
return Empty;
}
static B32 IsWorldPointEmpty(world *World,
S32 TileMapX, S32 TileMapY,
F32 TestX, F32 TestY)
{
B32 Empty = FALSE;
tile_map *TileMap = GetTileMap(World, TileMapX, TileMapY);
S32 PlayerTileX, PlayerTileY;
TileMap = GetTileMap(World, TileMapX, TileMapY);
if(TileMap) {
S32 PlayerTileX =
(S32)((TestX - TileMap->UpperLeftX) / TileMap->TileWidth);
S32 PlayerTileY =
(S32)((TestY - TileMap->UpperLeftY) / TileMap->TileHeight);
if((PlayerTileX >= 0) && (PlayerTileX < TileMap->CountX) &&
(PlayerTileY >= 0) && (PlayerTileY < TileMap->CountY))
{
U32 TileMapValue =
GetTileValueUnchecked(TileMap, PlayerTileX, PlayerTileY);
Empty = (TileMapValue == 0);
}
}
return Empty;
}
void UpdateAndRender(thread_context *Thread, game_memory *Memory,
game_input *Input, game_offscreen_buffer *Buffer,
game_sound_output_buffer *SoundBuffer)
{
#define TILEMAP_COUNT_X 17
#define TILEMAP_COUNT_Y 9
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},
U32 Tiles00[TILEMAP_COUNT_Y][TILEMAP_COUNT_X] = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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, 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},
{1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1},
};
F32 UpperLeftX, UpperLeftY, TileWidth, TileHeight;
U32 Tiles01[TILEMA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COUNT_Y][TILEMAP_COUNT_X] = {
{1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
};
F32 PlayerR, PlayerG, PlayerB, PlayerWidth, PlayerHeight,
PlayerLeft, PlayerTop;
tile_map TileMaps[2][2];
tile_map *TileMap;
world World;
ASSERT((&Input->Controllers[0].u.buttons.Terminator -
&Input->Controllers[0].u.Buttons[0]) ==
ARRAY_SIZE(Input->Controllers[0].u.Buttons));
ASSERT(sizeof(game_state) <= Memory->PermanentStorageSize);
memset(&World, 0, sizeof(World));
TileMaps[0][0].CountX = TILEMAP_COUNT_X;
TileMaps[0][0].CountY = TILEMAP_COUNT_Y;
TileMaps[0][0].UpperLeftX = 0.0f;
TileMaps[0][0].UpperLeftY = 0.0f;
TileMaps[0][0].TileWidth = 57.0f;
TileMaps[0][0].TileHeight = 57.0f;
TileMaps[0][0].Tiles = (U32 *)Tiles00;
TileMaps[0][1] = TileMaps[0][0];
TileMaps[0][1].Tiles = (U32 *)Tiles01;
TileMaps[1][0] = TileMaps[0][0];
TileMaps[1][0].Tiles = (U32 *)Tiles10;
TileMaps[1][1] = TileMaps[0][0];
TileMaps[1][1].Tiles = (U32 *)Tiles11;
TileMap = &TileMaps[0][0];
World.TileMaps = (tile_map *)TileMaps;
PlayerWidth = 0.75f * TileMap->TileWidth;
PlayerHeight = TileMap->TileHeight;
GameState = (game_state *)Memory->PermanentStorage;
if(!Memory->IsInitialized) {
GameState->PlayerX = 200.0f;
GameState->PlayerY = 200.0f;
Memory->IsInitialized = TRUE;
}
@@ -164,6 +306,8 @@ void UpdateAndRender(thread_context *Thread, game_memory *Memory,
F32 dPlayerX = 0.0f;
F32 dPlayerY = 0.0f;
F32 NewPlayerX, NewPlayerY;
if(Controller->u.buttons.MoveUp.EndedDown) {
dPlayerY = -1.0f;
}
@@ -177,14 +321,23 @@ void UpdateAndRender(thread_context *Thread, game_memory *Memory,
dPlayerX = 1.0f;
}
dPlayerX *= 64.0f;
dPlayerY *= 64.0f;
dPlayerX *= 100.0f;
dPlayerY *= 100.0f;
GameState->PlayerX += Input->dtForFrame * dPlayerX;
GameState->PlayerY += Input->dtForFrame * dPlayerY;
NewPlayerX = RoundF32toS32(GameState->PlayerX +
Input->dtForFrame*dPlayerX);
NewPlayerY = RoundF32toS32(GameState->PlayerY +
Input->dtForFrame*dPlayerY);
GameState->PlayerX = RoundF32toS32(GameState->PlayerX);
GameState->PlayerY = RoundF32toS32(GameState->PlayerY);
if(IsTileMapPointEmpty(TileMap, NewPlayerX - 0.5f*PlayerWidth,
NewPlayerY) &&
IsTileMapPointEmpty(TileMap, NewPlayerX + 0.5f*PlayerWidth,
NewPlayerY) &&
IsTileMapPointEmpty(TileMap, NewPlayerX, NewPlayerY))
{
GameState->PlayerX = NewPlayerX;
GameState->PlayerY = NewPlayerY;
}
}
}
@@ -192,23 +345,18 @@ void UpdateAndRender(thread_context *Thread, game_memory *Memory,
(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++) {
for(Row = 0; Row < TILEMAP_COUNT_Y; Row++) {
for(Column = 0; Column < TILEMAP_COUNT_X; Column++) {
F32 MinX, MinY, MaxX, MaxY;
U32 TileID = TileMap[Row][Column];
U32 TileID = GetTileValueUnchecked(TileMap, Column, Row);
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;
MinX = TileMap->UpperLeftX + (F32)(Column) * TileMap->TileWidth;
MinY = TileMap->UpperLeftY + (F32)(Row) * TileMap->TileHeight;
MaxX = MinX + TileMap->TileWidth;
MaxY = MinY + TileMap->TileHeight;
DrawRectangle(Buffer, MinX, MinY, MaxX, MaxY, Gray, Gray, Gray);
}
}
@@ -216,8 +364,6 @@ void UpdateAndRender(thread_context *Thread, game_memory *Memory,
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,