#include "core.h" #include "handmade.h" #include "sdl_handmade.h" #include "handmade_intrinsics.h" #include "handmade_random.h" #include "handmade_tile.h" #include "handmade_tile.cpp" #include /* memset */ static void GameOutputSound(game_state *GameState, game_sound_output_buffer *SoundBuffer, S32 ToneHz) { /* S16 ToneVolume, *SampleOut; S32 WavePeriod, SampleIndex; ToneVolume = 0; WavePeriod = SoundBuffer->SamplesPerSecond / ToneHz; SampleOut = SoundBuffer->Samples; for(SampleIndex = 0; SampleIndex < SoundBuffer->SampleCount; SampleIndex++) { F32 SineValue; S16 SampleValue; SineValue = sinf(GameState->tSine); SampleValue = (S16)(SineValue * ToneVolume); *SampleOut++ = SampleValue; *SampleOut++ = SampleValue; GameState->tSine += 2.0f * (F32)PI * 1.0f / (F32)WavePeriod; if(GameState->tSine > 2.0f*(F32)PI) { GameState->tSine -= 2.0f*(F32)PI; } } */ } 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 void DrawRectangle(game_offscreen_buffer *Buffer, F32 RealMinX, F32 RealMinY, F32 RealMaxX, F32 RealMaxY, 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; U32 Color; if(MinX < 0) MinX = 0; if(MinY < 0) MinY = 0; if(MaxX > Width) MaxX = Width; 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++) { U32 *Pixel = (U32 *)Row; for(X = MinX; X < MaxX; X++) { *(U32 *)Pixel = Color; Pixel++; } Row += Buffer->Pitch; } } void UpdateAndRender(thread_context *Thread, game_memory *Memory, game_input *Input, game_offscreen_buffer *Buffer, game_sound_output_buffer *SoundBuffer) { #define TILEMAP_COUNT_X 256 #define TILEMAP_COUNT_Y 256 game_state *GameState; U32 ControllerIndex; F32 PlayerR, PlayerG, PlayerB, PlayerWidth, PlayerHeight, PlayerLeft, PlayerTop; F32 ScreenCenterX, ScreenCenterY; 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)); ASSERT(sizeof(game_state) <= Memory->PermanentStorageSize); PlayerHeight = 1.4f; PlayerWidth = 0.75f * PlayerHeight; 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; GameState->PlayerP.TileRelY = 5.0f; InitializeArena(&GameState->WorldArena, Memory->PermanentStorageSize - sizeof(game_state), (U8 *)Memory->PermanentStorage + sizeof(game_state)); GameState->World = PUSH_STRUCT(&GameState->WorldArena, world); World = GameState->World; World->TileMap = PUSH_STRUCT(&GameState->WorldArena, tile_map); TileMap = World->TileMap; TileMap->ChunkShift = 4; TileMap->ChunkMask = (1 << TileMap->ChunkShift) - 1; TileMap->ChunkDim = (1 << TileMap->ChunkShift); TileMap->TileChunkCountX = 128; TileMap->TileChunkCountY = 128; TileMap->TileChunkCountZ = 2; TileMap->TileChunks = PUSH_ARRAY(&GameState->WorldArena, TileMap->TileChunkCountX * TileMap->TileChunkCountY * TileMap->TileChunkCountZ, tile_chunk); TileMap->TileSideInMeters = 1.4f; RandomNumberIndex = 0; TilesPerWidth = 17; TilesPerHeight = 9; ScreenX = 0; ScreenY = 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++; } } Memory->IsInitialized = TRUE; } World = GameState->World; TileMap = World->TileMap; TileSideInPixels = 60; MetersToPixels = (F32)TileSideInPixels / TileMap->TileSideInMeters; for(ControllerIndex = 0; ControllerIndex < ARRAY_SIZE(Input->Controllers); ControllerIndex++) { game_controller_input *Controller = GetController(Input, ControllerIndex); if(Controller->IsAnalog) { } else { F32 dPlayerX = 0.0f; F32 dPlayerY = 0.0f; tile_map_position NewPlayerP, PlayerLeft, PlayerRight; 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; } { F32 PlayerSpeed = 2.0f; if(Controller->u.buttons.ActionUp.EndedDown) PlayerSpeed = 10.0f; dPlayerX *= PlayerSpeed; dPlayerY *= PlayerSpeed; } NewPlayerP = GameState->PlayerP; NewPlayerP.TileRelX += Input->dtForFrame*dPlayerX; NewPlayerP.TileRelY += Input->dtForFrame*dPlayerY; NewPlayerP = RecannonicalizePosition(TileMap, NewPlayerP); PlayerLeft = NewPlayerP; PlayerLeft.TileRelX -= 0.5f*PlayerWidth; PlayerLeft = RecannonicalizePosition(TileMap, PlayerLeft); PlayerRight = NewPlayerP; PlayerRight.TileRelX += 0.5f*PlayerWidth; PlayerRight = RecannonicalizePosition(TileMap, PlayerRight); if(IsTileMapPointEmpty(TileMap, NewPlayerP) && IsTileMapPointEmpty(TileMap, PlayerLeft) && IsTileMapPointEmpty(TileMap, PlayerRight)) { GameState->PlayerP = NewPlayerP; } } } ScreenCenterX = 0.5f * (F32)Buffer->Width; ScreenCenterY = 0.5f * (F32)Buffer->Height; DrawRectangle(Buffer, 0.0f, 0.0f, (F32)Buffer->Width, (F32)Buffer->Height, 0.0f, 1.0f, 0.0f); { S32 RelRow, RelColumn; for(RelRow = -10; RelRow < 10; RelRow++) { for(RelColumn = -20; RelColumn < 20; RelColumn++) { U32 Column = GameState->PlayerP.AbsTileX + RelColumn; U32 Row = GameState->PlayerP.AbsTileY + RelRow; U32 TileID = GetTileValue(TileMap, Column, Row, GameState->PlayerP.AbsTileZ); 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 - MetersToPixels*GameState->PlayerP.TileRelX + (F32)RelColumn*(F32)TileSideInPixels; CenterY = ScreenCenterY + 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); } } } } PlayerR = 1.0f; PlayerG = 0.0f; PlayerB = 0.0f; PlayerLeft = ScreenCenterX - 0.5f * MetersToPixels*PlayerWidth; PlayerTop = ScreenCenterY - MetersToPixels*PlayerHeight; DrawRectangle(Buffer, PlayerLeft, PlayerTop, PlayerLeft + MetersToPixels*PlayerWidth, PlayerTop + MetersToPixels*PlayerHeight, PlayerR, PlayerG, PlayerB); GameOutputSound(GameState, SoundBuffer, 400); } /* static void RenderWeirdGradient(game_offscreen_buffer *Buffer, S32 XOffset, S32 YOffset) { S32 Width, Height, Y; U8 *Row; Width = Buffer->Width; Height = Buffer->Height; Row = (U8 *)Buffer->Memory; for(Y = 0; Y < Height; Y++) { U32 *Pixel = (U32 *)Row; S32 X; Pixel = (U32 *)Row; for(X = 0; X < Width; X++) { U8 Blue, Green; Blue = (U8)(X + XOffset); Green = (U8)(Y + YOffset); *Pixel = (Green << 8) | Blue; Pixel++; } Row += Buffer->Pitch; } } */