diff --git a/handmade.c b/handmade.c index 3e2fcf2..9a35b8f 100644 --- a/handmade.c +++ b/handmade.c @@ -138,56 +138,44 @@ static B32 IsTileMapPointEmpty(world *World, tile_map *TileMap, return Empty; } -static cannonical_position GetCannonicalPosition(world *World, - raw_position Pos) +static void CannonicalizeCoord(world *World, S32 TileCount, + S32 *TileMap, S32 *Tile, F32 *TileRel) { - cannonical_position Result; - F32 X, Y; + S32 Offset = FloorF32toS32(*TileRel / World->TileSideInMeters); + *Tile += Offset; + *TileRel -= (F32)Offset * World->TileSideInMeters; - Result.TileMapX = Pos.TileMapX; - Result.TileMapY = Pos.TileMapY; + ASSERT(*TileRel >= 0); + ASSERT(*TileRel < World->TileSideInMeters); - X = Pos.X - World->UpperLeftX; - Y = Pos.Y - World->UpperLeftY; - Result.TileX = FloorF32toS32(X / (F32)World->TileSideInPixels); - Result.TileY = FloorF32toS32(Y / (F32)World->TileSideInPixels); - - Result.X = X - (F32)Result.TileX*(F32)World->TileSideInPixels; - Result.Y = Y - (F32)Result.TileY*(F32)World->TileSideInPixels; - - ASSERT(Result.X >= 0); - ASSERT(Result.Y >= 0); - ASSERT(Result.X < (F32)World->TileSideInPixels); - ASSERT(Result.Y < (F32)World->TileSideInPixels); - - if(Result.TileX < 0) { - Result.TileX = World->CountX + Result.TileX; - Result.TileMapX--; + if(*Tile < 0) { + *Tile = TileCount + *Tile; + (*TileMap)--; } - if(Result.TileY < 0) { - Result.TileY = World->CountY + Result.TileY; - Result.TileMapY--; - } - if(Result.TileX >= World->CountX) { - Result.TileX = Result.TileX - World->CountX; - Result.TileMapX++; - } - if(Result.TileY >= World->CountY) { - Result.TileY = Result.TileY - World->CountY; - Result.TileMapY++; + if(*Tile >= World->CountX) { + *Tile = *Tile - TileCount; + (*TileMap)++; } +} + +static cannonical_position RecannonicalizePosition(world *World, + cannonical_position Pos) +{ + cannonical_position Result = Pos; + + CannonicalizeCoord(World, World->TileMapCountX, + &Result.TileMapX, &Result.TileX, &Result.TileRelX); + CannonicalizeCoord(World, World->TileMapCountY, + &Result.TileMapY, &Result.TileY, &Result.TileRelY); return Result; } -static B32 IsWorldPointEmpty(world *World, raw_position TestPos) +static B32 IsWorldPointEmpty(world *World, cannonical_position CanPos) { B32 Empty = FALSE; - tile_map *TileMap; - - cannonical_position CanPos = GetCannonicalPosition(World, TestPos); - TileMap = GetTileMap(World, CanPos.TileMapX, CanPos.TileMapY); + tile_map *TileMap = GetTileMap(World, CanPos.TileMapX, CanPos.TileMapY); Empty = IsTileMapPointEmpty(World, TileMap, CanPos.TileX, CanPos.TileY); return Empty; @@ -273,23 +261,29 @@ void UpdateAndRender(thread_context *Thread, game_memory *Memory, World.CountY = TILEMAP_COUNT_Y; World.TileSideInMeters = 1.4f; World.TileSideInPixels = 60; + World.MetersToPixels = + (F32)World.TileSideInPixels / World.TileSideInMeters; World.UpperLeftX = -((F32)World.TileSideInPixels / 2.0f); World.UpperLeftY = 0.0f; World.TileMaps = (tile_map *)TileMaps; - PlayerWidth = 0.75f * (F32)World.TileSideInPixels; - PlayerHeight = (F32)World.TileSideInPixels; + PlayerHeight = 1.4f; + PlayerWidth = 0.75f * PlayerHeight; GameState = (game_state *)Memory->PermanentStorage; if(!Memory->IsInitialized) { - GameState->PlayerX = 200.0f; - GameState->PlayerY = 200.0f; + GameState->PlayerP.TileMapX = 0; + GameState->PlayerP.TileMapY = 0; + GameState->PlayerP.TileX = 3; + GameState->PlayerP.TileY = 3; + GameState->PlayerP.TileRelX = 5.0f; + GameState->PlayerP.TileRelY = 5.0f; Memory->IsInitialized = TRUE; } - TileMap = GetTileMap(&World, GameState->PlayerTileMapX, - GameState->PlayerTileMapY); + TileMap = GetTileMap(&World, GameState->PlayerP.TileMapX, + GameState->PlayerP.TileMapY); ASSERT(TileMap); for(ControllerIndex = 0; @@ -303,9 +297,7 @@ void UpdateAndRender(thread_context *Thread, game_memory *Memory, F32 dPlayerX = 0.0f; F32 dPlayerY = 0.0f; - F32 NewPlayerX, NewPlayerY; - - raw_position PlayerPos, PlayerLeft, PlayerRight; + cannonical_position NewPlayerP, PlayerLeft, PlayerRight; if(Controller->u.buttons.MoveUp.EndedDown) { dPlayerY = -1.0f; @@ -320,38 +312,27 @@ void UpdateAndRender(thread_context *Thread, game_memory *Memory, dPlayerX = 1.0f; } - dPlayerX *= 200.0f; - dPlayerY *= 200.0f; + dPlayerX *= 10.0f; + dPlayerY *= 10.0f; - NewPlayerX = GameState->PlayerX + Input->dtForFrame*dPlayerX; - NewPlayerY = GameState->PlayerY + Input->dtForFrame*dPlayerY; + NewPlayerP = GameState->PlayerP; + NewPlayerP.TileRelX += Input->dtForFrame*dPlayerX; + NewPlayerP.TileRelY += Input->dtForFrame*dPlayerY; + NewPlayerP = RecannonicalizePosition(&World, NewPlayerP); - PlayerPos.TileMapX = GameState->PlayerTileMapX; - PlayerPos.TileMapY = GameState->PlayerTileMapY; - PlayerPos.X = NewPlayerX; - PlayerPos.Y = NewPlayerY; + PlayerLeft = NewPlayerP; + PlayerLeft.TileRelX -= 0.5f*PlayerWidth; + PlayerLeft = RecannonicalizePosition(&World, PlayerLeft); - PlayerLeft = PlayerPos; - PlayerLeft.X -= 0.5f*PlayerWidth; + PlayerRight = NewPlayerP; + PlayerRight.TileRelX += 0.5f*PlayerWidth; + PlayerRight = RecannonicalizePosition(&World, PlayerRight); - PlayerRight = PlayerPos; - PlayerRight.X += 0.5f*PlayerWidth; - - if(IsWorldPointEmpty(&World, PlayerPos) && + if(IsWorldPointEmpty(&World, NewPlayerP) && IsWorldPointEmpty(&World, PlayerLeft) && IsWorldPointEmpty(&World, PlayerRight)) { - cannonical_position CanPos = - GetCannonicalPosition(&World, PlayerPos); - - GameState->PlayerTileMapX = CanPos.TileMapX; - GameState->PlayerTileMapY = CanPos.TileMapY; - GameState->PlayerX = World.UpperLeftX + - (F32)World.TileSideInPixels * (F32)CanPos.TileX + - CanPos.X; - GameState->PlayerY = World.UpperLeftY + - (F32)World.TileSideInPixels * (F32)CanPos.TileY + - CanPos.Y; + GameState->PlayerP = NewPlayerP; } } } @@ -381,18 +362,22 @@ void UpdateAndRender(thread_context *Thread, game_memory *Memory, PlayerR = 1.0f; PlayerG = 0.0f; PlayerB = 0.0f; - PlayerLeft = GameState->PlayerX - 0.5f*PlayerWidth; - PlayerTop = GameState->PlayerY - PlayerHeight; + PlayerLeft = World.UpperLeftX + + (F32)World.TileSideInPixels*(F32)GameState->PlayerP.TileX + + (F32)GameState->PlayerP.TileX + + World.MetersToPixels*GameState->PlayerP.TileRelX - + 0.5f*PlayerWidth; + PlayerTop = World.UpperLeftY + + (F32)World.TileSideInPixels*(F32)GameState->PlayerP.TileY + + (F32)GameState->PlayerP.TileY + + World.MetersToPixels*GameState->PlayerP.TileRelY - + PlayerHeight; DrawRectangle(Buffer, PlayerLeft, PlayerTop, - PlayerLeft + PlayerWidth, PlayerTop + PlayerHeight, + PlayerLeft + World.MetersToPixels*PlayerWidth, + PlayerTop + World.MetersToPixels*PlayerHeight, PlayerR, PlayerG, PlayerB); - DrawRectangle(Buffer, - (F32)(Input->MouseX) - 15.0f, (F32)(Input->MouseY) - 15.0f, - (F32)(Input->MouseX) + 5.0f, (F32)(Input->MouseY) + 5.0f, - 1.0f, 0.5f, 0.0f); - GameOutputSound(GameState, SoundBuffer, 400); } diff --git a/handmade.h b/handmade.h index 71756da..39272c1 100644 --- a/handmade.h +++ b/handmade.h @@ -105,12 +105,12 @@ typedef struct game_memory { B32 IsInitialized; U64 PermanentStorageSize; - void *PermanentStorage; /* WARNING: This memory is REQUIRED to be zero + void *PermanentStorage; /* WARNING: This memory is REQUIRED to be zero initialized!!! (Done by the OS layer.) */ U64 TransientStorageSize; - void *TransientStorage; /* WARNING: This memory is REQUIRED to be zero + void *TransientStorage; /* WARNING: This memory is REQUIRED to be zero initialized!!! (Done by the OS layer.) */ @@ -137,26 +137,22 @@ void UpdateAndRender(thread_context *Thread, game_memory *Memory, */ typedef struct cannonical_position { +#if 1 S32 TileMapX; S32 TileMapY; S32 TileX; S32 TileY; +#else + U32 _TileX; + U32 _TileY; +#endif /* NOTE: Tile-relative X and Y. */ - F32 X; - F32 Y; + F32 TileRelX; + F32 TileRelY; } cannonical_position; -typedef struct raw_position { - S32 TileMapX; - S32 TileMapY; - - /* NOTE: Tile-map relative X and Y. */ - F32 X; - F32 Y; -} raw_position; - typedef struct tile_map { U32 *Tiles; } tile_map; @@ -164,6 +160,7 @@ typedef struct tile_map { typedef struct world { F32 TileSideInMeters; S32 TileSideInPixels; + F32 MetersToPixels; S32 CountX; S32 CountY; @@ -178,11 +175,7 @@ typedef struct world { } world; typedef struct game_state { - S32 PlayerTileMapX; - S32 PlayerTileMapY; - - F32 PlayerX; - F32 PlayerY; + cannonical_position PlayerP; } game_state; #endif diff --git a/handmade_intrinsics.h b/handmade_intrinsics.h index 950a089..1c9fd46 100644 --- a/handmade_intrinsics.h +++ b/handmade_intrinsics.h @@ -36,4 +36,5 @@ F32 ATan2(F32 Y, F32 X) return Result; } -#endif \ No newline at end of file +#endif + diff --git a/sdl_handmade.c b/sdl_handmade.c index 3b74851..625e3aa 100644 --- a/sdl_handmade.c +++ b/sdl_handmade.c @@ -313,6 +313,7 @@ static void HandleEvent(SDL_Event *Event) case SDL_WINDOWEVENT_EXPOSED: { } break; +#if 0 case SDL_WINDOWEVENT_FOCUS_GAINED: { if(SDL_SetWindowOpacity(Window, 1.0f) != 0) { /* TODO: This didn't work . . . SDL_GetError() */ @@ -324,6 +325,7 @@ static void HandleEvent(SDL_Event *Event) /* TODO: This didn't work . . . SDL_GetError() */ } } break; +#endif } } break; } @@ -759,8 +761,9 @@ S32 main(S32 argc, char *argv[]) Window = SDL_CreateWindow("Handmade Hero", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, RESOLUTION_WIDTH, RESOLUTION_HEIGHT, - SDL_WINDOW_RESIZABLE | - SDL_WINDOW_ALWAYS_ON_TOP); + SDL_WINDOW_RESIZABLE); + /*SDL_WINDOW_RESIZABLE | + SDL_WINDOW_ALWAYS_ON_TOP);*/ if(Window) { SDL_Renderer *Renderer;