Day 35 complete.

This commit is contained in:
igor
2026-04-16 18:10:54 -07:00
parent cd0ed29b30
commit dea811e073
5 changed files with 774 additions and 117 deletions
+167 -79
View File
@@ -3,11 +3,12 @@
#include "sdl_handmade.h"
#include "handmade_intrinsics.h"
#include <string.h> /* memset */
#include "handmade_random.h"
#include "handmade_tile.h"
#include "handmade_tile.cpp"
#include <string.h> /* memset */
static void GameOutputSound(game_state *GameState,
game_sound_output_buffer *SoundBuffer,
S32 ToneHz)
@@ -97,26 +98,6 @@ static void DrawRectangle(game_offscreen_buffer *Buffer,
}
}
static void InitializeArena(memory_arena *Arena, U64 Size, U8 *Base)
{
Arena->Size = Size;
Arena->Base = Base;
Arena->Used = 0;
}
#define PUSH_STRUCT(Arena, type) (type *)PushSize_(Arena, sizeof(type))
#define PUSH_ARRAY(Arena, Count, type) (type *)PushSize_(Arena, (Count)*sizeof(type))
static void *PushSize_(memory_arena *Arena, U64 Size)
{
void *Result;
ASSERT((Arena->Used + Size) <= Arena->Size);
Result = (void *)(Arena->Base + Arena->Used);
Arena->Used += Size;
return Result;
}
void UpdateAndRender(thread_context *Thread, game_memory *Memory,
game_input *Input, game_offscreen_buffer *Buffer,
game_sound_output_buffer *SoundBuffer)
@@ -135,6 +116,9 @@ void UpdateAndRender(thread_context *Thread, game_memory *Memory,
world *World;
tile_map *TileMap;
S32 TileSideInPixels;
F32 MetersToPixels;
ASSERT((&Input->Controllers[0].u.buttons.Terminator -
&Input->Controllers[0].u.Buttons[0]) ==
ARRAY_SIZE(Input->Controllers[0].u.Buttons));
@@ -146,10 +130,18 @@ void UpdateAndRender(thread_context *Thread, game_memory *Memory,
GameState = (game_state *)Memory->PermanentStorage;
if(!Memory->IsInitialized) {
U32 TilesPerWidth, TilesPerHeight;
U32 ScreenX, ScreenY;
U32 ScreenIndex;
tile_map *TileMap;
world *World;
U32 AbsTileZ;
B32 DoorTop, DoorBottom, DoorLeft, DoorRight, DoorUp, DoorDown;
U32 RandomNumberIndex;
GameState->PlayerP.AbsTileX = 2;
GameState->PlayerP.AbsTileY = 3;
GameState->PlayerP.TileRelX = 5.0f;
@@ -165,51 +157,136 @@ void UpdateAndRender(thread_context *Thread, game_memory *Memory,
TileMap = World->TileMap;
TileMap->ChunkShift = 8;
TileMap->ChunkShift = 4;
TileMap->ChunkMask = (1 << TileMap->ChunkShift) - 1;
TileMap->ChunkDim = 256;
TileMap->ChunkDim = (1 << TileMap->ChunkShift);
TileMap->TileChunkCountX = 4;
TileMap->TileChunkCountY = 4;
TileMap->TileChunkCountX = 128;
TileMap->TileChunkCountY = 128;
TileMap->TileChunkCountZ = 2;
TileMap->TileChunks =
PUSH_ARRAY(&GameState->WorldArena,
TileMap->TileChunkCountX*TileMap->TileChunkCountX,
TileMap->TileChunkCountX *
TileMap->TileChunkCountY *
TileMap->TileChunkCountZ,
tile_chunk);
{
U32 X, Y;
for(Y = 0; Y < TileMap->TileChunkCountY; Y++) {
for(X = 0; X < TileMap->TileChunkCountX; X++) {
TileMap->TileChunks[Y*TileMap->TileChunkCountX + X].Tiles =
PUSH_ARRAY(&GameState->WorldArena,
TileMap->ChunkDim*TileMap->ChunkDim,
U32);
}
}
}
TileMap->TileSideInMeters = 1.4f;
TileMap->TileSideInPixels = 60;
TileMap->MetersToPixels =
(F32)TileMap->TileSideInPixels / TileMap->TileSideInMeters;
RandomNumberIndex = 0;
TilesPerWidth = 17;
TilesPerHeight = 9;
for(ScreenY = 0; ScreenY < 32; ScreenY++) {
for(ScreenX = 0; ScreenX < 32; ScreenX++) {
U32 TileX, TileY;
for(TileY = 0; TileY < TilesPerWidth; TileY++) {
for(TileX = 0; TileX < TilesPerWidth; TileX++) {
U32 AbsTileX = ScreenX * TilesPerWidth + TileX;
U32 AbsTileY = ScreenY * TilesPerHeight + TileY;
ScreenX = 0;
ScreenY = 0;
SetTileValue(&GameState->WorldArena,
World->TileMap,
AbsTileX, AbsTileY,
(TileX == TileY) && (TileY % 2) ? 1 : 0);
}
AbsTileZ = 0;
DoorTop = FALSE;
DoorBottom = FALSE;
DoorLeft = FALSE;
DoorRight = FALSE;
DoorUp = FALSE;
DoorDown = FALSE;
for(ScreenIndex = 0; ScreenIndex < 100; ScreenIndex++) {
/* TODO: Random number generator. */
U32 TileX, TileY;
U32 RandomChoice;
ASSERT(RandomNumberIndex < ARRAY_SIZE(RandomNumberTable));
if(DoorUp || DoorDown) {
RandomChoice = RandomNumberTable[RandomNumberIndex++] % 2;
} else {
RandomChoice = RandomNumberTable[RandomNumberIndex++] % 3;
}
if(RandomChoice == 2) {
if(AbsTileZ == 0) {
DoorUp = TRUE;
} else {
DoorDown = TRUE;
}
} else if(RandomChoice == 1) {
DoorRight = TRUE;
} else {
DoorTop = TRUE;
}
for(TileY = 0; TileY < TilesPerHeight; TileY++) {
for(TileX = 0; TileX < TilesPerWidth; TileX++) {
U32 AbsTileX = ScreenX * TilesPerWidth + TileX;
U32 AbsTileY = ScreenY * TilesPerHeight + TileY;
U32 TileValue = 1;
if((TileX == 0) &&
(!DoorLeft || (TileY != (TilesPerHeight/2))))
{
TileValue = 2;
}
if(((TileX == (TilesPerWidth - 1)) &&
(!DoorRight || (TileY != (TilesPerHeight/2)))))
{
TileValue = 2;
}
if((TileY == 0) &&
(!DoorBottom || (TileX != (TilesPerWidth/2))))
{
TileValue = 2;
}
if((TileY == (TilesPerHeight - 1)) &&
(!DoorTop || (TileX != (TilesPerWidth/2))))
{
TileValue = 2;
}
if((TileX == 10) && (TileY == 6)) {
if(DoorUp) {
TileValue = 3;
}
if(DoorDown) {
TileValue = 4;
}
}
SetTileValue(&GameState->WorldArena,
World->TileMap,
AbsTileX, AbsTileY, AbsTileZ,
TileValue);
}
}
DoorLeft = DoorRight;
DoorBottom = DoorTop;
if(DoorUp) {
DoorDown = TRUE;
DoorUp = FALSE;
} else if (DoorDown) {
DoorUp = TRUE;
DoorDown = FALSE;
} else {
DoorUp = FALSE;
DoorDown = FALSE;
}
DoorTop = FALSE;
DoorRight = FALSE;
if(RandomChoice == 2) {
if(AbsTileZ == 0) {
AbsTileZ = 1;
} else {
AbsTileZ = 0;
}
} else if(RandomChoice == 1) {
ScreenX++;
} else {
ScreenY++;
}
}
@@ -219,6 +296,9 @@ void UpdateAndRender(thread_context *Thread, game_memory *Memory,
World = GameState->World;
TileMap = World->TileMap;
TileSideInPixels = 60;
MetersToPixels = (F32)TileSideInPixels / TileMap->TileSideInMeters;
for(ControllerIndex = 0;
ControllerIndex < ARRAY_SIZE(Input->Controllers);
ControllerIndex++)
@@ -290,32 +370,40 @@ void UpdateAndRender(thread_context *Thread, game_memory *Memory,
U32 Column = GameState->PlayerP.AbsTileX + RelColumn;
U32 Row = GameState->PlayerP.AbsTileY + RelRow;
U32 TileID = GetTileValue(TileMap, Column, Row);
F32 Gray = 0.5f;
U32 TileID = GetTileValue(TileMap,
Column,
Row,
GameState->PlayerP.AbsTileZ);
if(TileID) {
Gray = 1.0f;
}
if((Column == GameState->PlayerP.AbsTileX) &&
(Row == GameState->PlayerP.AbsTileY))
{
Gray = 0.0f;
}
{
if(TileID > 0) {
F32 CenterX, CenterY, MinX, MinY, MaxX, MaxY;
F32 Gray = 0.5f;
if(TileID == 2) {
Gray = 1.0f;
}
if(TileID > 2) {
Gray = 0.25f;
}
if((Column == GameState->PlayerP.AbsTileX) &&
(Row == GameState->PlayerP.AbsTileY))
{
Gray = 0.0f;
}
CenterX = ScreenCenterX -
TileMap->MetersToPixels*GameState->PlayerP.TileRelX +
(F32)RelColumn * (F32)TileMap->TileSideInPixels;
MetersToPixels*GameState->PlayerP.TileRelX +
(F32)RelColumn*(F32)TileSideInPixels;
CenterY = ScreenCenterY +
TileMap->MetersToPixels*GameState->PlayerP.TileRelY -
(F32)RelRow * (F32)TileMap->TileSideInPixels;
MinX = CenterX - 0.5f*(F32)TileMap->TileSideInPixels;
MinY = CenterY - 0.5f*(F32)TileMap->TileSideInPixels;
MaxX = CenterX + 0.5f*(F32)TileMap->TileSideInPixels;
MaxY = CenterY + 0.5f*(F32)TileMap->TileSideInPixels;
MetersToPixels*GameState->PlayerP.TileRelY -
(F32)RelRow*(F32)TileSideInPixels;
MinX = CenterX - 0.5f*(F32)TileSideInPixels;
MinY = CenterY - 0.5f*(F32)TileSideInPixels;
MaxX = CenterX + 0.5f*(F32)TileSideInPixels;
MaxY = CenterY + 0.5f*(F32)TileSideInPixels;
DrawRectangle(Buffer,
MinX, MinY, MaxX, MaxY,
Gray, Gray, Gray);
@@ -327,12 +415,12 @@ void UpdateAndRender(thread_context *Thread, game_memory *Memory,
PlayerR = 1.0f;
PlayerG = 0.0f;
PlayerB = 0.0f;
PlayerLeft = ScreenCenterX - 0.5f* TileMap->MetersToPixels*PlayerWidth;
PlayerTop = ScreenCenterY - TileMap->MetersToPixels*PlayerHeight;
PlayerLeft = ScreenCenterX - 0.5f * MetersToPixels*PlayerWidth;
PlayerTop = ScreenCenterY - MetersToPixels*PlayerHeight;
DrawRectangle(Buffer,
PlayerLeft, PlayerTop,
PlayerLeft + TileMap->MetersToPixels*PlayerWidth,
PlayerTop + TileMap->MetersToPixels*PlayerHeight,
PlayerLeft + MetersToPixels*PlayerWidth,
PlayerTop + MetersToPixels*PlayerHeight,
PlayerR, PlayerG, PlayerB);
GameOutputSound(GameState, SoundBuffer, 400);