Removed the 80 character column rule. Becoming impractical.

This commit is contained in:
igor
2026-04-17 03:19:13 -07:00
parent a8a3b57f8a
commit 9bc7fa3b52
4 changed files with 163 additions and 464 deletions
+50 -107
View File
@@ -9,9 +9,7 @@
#include <string.h> /* memset */ #include <string.h> /* memset */
static void GameOutputSound(game_state *GameState, static void GameOutputSound(game_state *GameState, game_sound_output_buffer *SoundBuffer, S32 ToneHz)
game_sound_output_buffer *SoundBuffer,
S32 ToneHz)
{ {
/* /*
S16 ToneVolume, *SampleOut; S16 ToneVolume, *SampleOut;
@@ -53,10 +51,7 @@ static void ClearBackground(game_offscreen_buffer *Buffer)
} }
} }
static void DrawRectangle(game_offscreen_buffer *Buffer, static void DrawRectangle(game_offscreen_buffer *Buffer, F32 RealMinX, F32 RealMinY, F32 RealMaxX, F32 RealMaxY, F32 R, F32 G, F32 B)
F32 RealMinX, F32 RealMinY,
F32 RealMaxX, F32 RealMaxY,
F32 R, F32 G, F32 B)
{ {
S32 MinX = RoundF32toS32(RealMinX); S32 MinX = RoundF32toS32(RealMinX);
S32 MinY = RoundF32toS32(RealMinY); S32 MinY = RoundF32toS32(RealMinY);
@@ -76,12 +71,9 @@ static void DrawRectangle(game_offscreen_buffer *Buffer,
if(MaxY > Height) if(MaxY > Height)
MaxY = Height; MaxY = Height;
U32 Color = (U32)((RoundF32toS32(R * 255.0f) << 16) | U32 Color = (U32)((RoundF32toS32(R * 255.0f) << 16) | (RoundF32toS32(G * 255.0f) << 8) | (RoundF32toS32(B * 255.0f) << 0));
(RoundF32toS32(G * 255.0f) << 8) |
(RoundF32toS32(B * 255.0f) << 0));
U8 *Row = ((U8 *)Buffer->Memory + MinX*Buffer->BytesPerPixel + U8 *Row = ((U8 *)Buffer->Memory + MinX*Buffer->BytesPerPixel + MinY*Buffer->Pitch);
MinY*Buffer->Pitch);
for(S32 Y = MinY; Y < MaxY; Y++) { for(S32 Y = MinY; Y < MaxY; Y++) {
U32 *Pixel = (U32 *)Row; U32 *Pixel = (U32 *)Row;
for(S32 X = MinX; X < MaxX; X++) { for(S32 X = MinX; X < MaxX; X++) {
@@ -92,14 +84,10 @@ static void DrawRectangle(game_offscreen_buffer *Buffer,
} }
} }
void UpdateAndRender(thread_context *Thread, game_memory *Memory, void UpdateAndRender(thread_context *Thread, game_memory *Memory, game_input *Input, game_offscreen_buffer *Buffer, game_sound_output_buffer *SoundBuffer)
game_input *Input, game_offscreen_buffer *Buffer,
game_sound_output_buffer *SoundBuffer)
{ {
ASSERT((&Input->Controllers[0].u.buttons.Terminator - ASSERT((&Input->Controllers[0].Terminator - &Input->Controllers[0].Buttons[0]) == ARRAY_SIZE(Input->Controllers[0].Buttons));
&Input->Controllers[0].u.Buttons[0]) ==
ARRAY_SIZE(Input->Controllers[0].u.Buttons));
ASSERT(sizeof(game_state) <= Memory->PermanentStorageSize); ASSERT(sizeof(game_state) <= Memory->PermanentStorageSize);
#define TILEMAP_COUNT_X 256 #define TILEMAP_COUNT_X 256
@@ -117,9 +105,7 @@ void UpdateAndRender(thread_context *Thread, game_memory *Memory,
GameState->PlayerP.OffsetX = 5.0f; GameState->PlayerP.OffsetX = 5.0f;
GameState->PlayerP.OffsetY = 5.0f; GameState->PlayerP.OffsetY = 5.0f;
InitializeArena(&GameState->WorldArena, InitializeArena(&GameState->WorldArena, Memory->PermanentStorageSize - sizeof(game_state), (U8 *)Memory->PermanentStorage + sizeof(game_state));
Memory->PermanentStorageSize - sizeof(game_state),
(U8 *)Memory->PermanentStorage + sizeof(game_state));
GameState->World = PUSH_STRUCT(&GameState->WorldArena, world); GameState->World = PUSH_STRUCT(&GameState->WorldArena, world);
world *World = GameState->World; world *World = GameState->World;
@@ -135,12 +121,7 @@ void UpdateAndRender(thread_context *Thread, game_memory *Memory,
TileMap->TileChunkCountY = 128; TileMap->TileChunkCountY = 128;
TileMap->TileChunkCountZ = 2; TileMap->TileChunkCountZ = 2;
TileMap->TileChunks = TileMap->TileChunks = PUSH_ARRAY(&GameState->WorldArena, TileMap->TileChunkCountX * TileMap->TileChunkCountY * TileMap->TileChunkCountZ, tile_chunk);
PUSH_ARRAY(&GameState->WorldArena,
TileMap->TileChunkCountX *
TileMap->TileChunkCountY *
TileMap->TileChunkCountZ,
tile_chunk);
TileMap->TileSideInMeters = 1.4f; TileMap->TileSideInMeters = 1.4f;
@@ -192,27 +173,19 @@ void UpdateAndRender(thread_context *Thread, game_memory *Memory,
U32 AbsTileY = ScreenY * TilesPerHeight + TileY; U32 AbsTileY = ScreenY * TilesPerHeight + TileY;
U32 TileValue = 1; U32 TileValue = 1;
if((TileX == 0) && if((TileX == 0) && (!DoorLeft || (TileY != (TilesPerHeight/2)))) {
(!DoorLeft || (TileY != (TilesPerHeight/2))))
{
TileValue = 2; TileValue = 2;
} }
if(((TileX == (TilesPerWidth - 1)) && if(((TileX == (TilesPerWidth - 1)) && (!DoorRight || (TileY != (TilesPerHeight/2))))) {
(!DoorRight || (TileY != (TilesPerHeight/2)))))
{
TileValue = 2; TileValue = 2;
} }
if((TileY == 0) && if((TileY == 0) && (!DoorBottom || (TileX != (TilesPerWidth/2)))) {
(!DoorBottom || (TileX != (TilesPerWidth/2))))
{
TileValue = 2; TileValue = 2;
} }
if((TileY == (TilesPerHeight - 1)) && if((TileY == (TilesPerHeight - 1)) && (!DoorTop || (TileX != (TilesPerWidth/2)))) {
(!DoorTop || (TileX != (TilesPerWidth/2))))
{
TileValue = 2; TileValue = 2;
} }
@@ -225,10 +198,7 @@ void UpdateAndRender(thread_context *Thread, game_memory *Memory,
} }
} }
SetTileValue(&GameState->WorldArena, SetTileValue(&GameState->WorldArena, World->TileMap, AbsTileX, AbsTileY, AbsTileZ, TileValue);
World->TileMap,
AbsTileX, AbsTileY, AbsTileZ,
TileValue);
} }
} }
@@ -268,33 +238,29 @@ void UpdateAndRender(thread_context *Thread, game_memory *Memory,
S32 TileSideInPixels = 60; S32 TileSideInPixels = 60;
F32 MetersToPixels = (F32)TileSideInPixels / TileMap->TileSideInMeters; F32 MetersToPixels = (F32)TileSideInPixels / TileMap->TileSideInMeters;
for(U32 ControllerIndex = 0; for(U32 ControllerIndex = 0; ControllerIndex < ARRAY_SIZE(Input->Controllers); ControllerIndex++) {
ControllerIndex < ARRAY_SIZE(Input->Controllers); game_controller_input *Controller = GetController(Input, ControllerIndex);
ControllerIndex++)
{
game_controller_input *Controller =
GetController(Input, ControllerIndex);
if(Controller->IsAnalog) { if(Controller->IsAnalog) {
} else { } else {
F32 dPlayerX = 0.0f; F32 dPlayerX = 0.0f;
F32 dPlayerY = 0.0f; F32 dPlayerY = 0.0f;
if(Controller->u.buttons.MoveUp.EndedDown) { if(Controller->MoveUp.EndedDown) {
dPlayerY = 1.0f; dPlayerY = 1.0f;
} }
if(Controller->u.buttons.MoveDown.EndedDown) { if(Controller->MoveDown.EndedDown) {
dPlayerY = -1.0f; dPlayerY = -1.0f;
} }
if(Controller->u.buttons.MoveLeft.EndedDown) { if(Controller->MoveLeft.EndedDown) {
dPlayerX = -1.0f; dPlayerX = -1.0f;
} }
if(Controller->u.buttons.MoveRight.EndedDown) { if(Controller->MoveRight.EndedDown) {
dPlayerX = 1.0f; dPlayerX = 1.0f;
} }
F32 PlayerSpeed = 2.0f; F32 PlayerSpeed = 2.0f;
if(Controller->u.buttons.ActionUp.EndedDown) if(Controller->ActionUp.EndedDown)
PlayerSpeed = 10.0f; PlayerSpeed = 10.0f;
dPlayerX *= PlayerSpeed; dPlayerX *= PlayerSpeed;
@@ -316,13 +282,9 @@ void UpdateAndRender(thread_context *Thread, game_memory *Memory,
PlayerRight.OffsetX += 0.5f*PlayerWidth; PlayerRight.OffsetX += 0.5f*PlayerWidth;
PlayerRight = RecannonicalizePosition(TileMap, PlayerRight); PlayerRight = RecannonicalizePosition(TileMap, PlayerRight);
if(IsTileMapPointEmpty(TileMap, NewPlayerP) && if(IsTileMapPointEmpty(TileMap, NewPlayerP) && IsTileMapPointEmpty(TileMap, PlayerLeft) && IsTileMapPointEmpty(TileMap, PlayerRight)) {
IsTileMapPointEmpty(TileMap, PlayerLeft) &&
IsTileMapPointEmpty(TileMap, PlayerRight))
{
if(!AreOnSameTile(&GameState->PlayerP, &NewPlayerP)) { if(!AreOnSameTile(&GameState->PlayerP, &NewPlayerP)) {
U32 NewTileValue = U32 NewTileValue = GetTileValueByPos(TileMap, NewPlayerP);
GetTileValueByPos(TileMap, NewPlayerP);
if(NewTileValue == 3) { if(NewTileValue == 3) {
NewPlayerP.AbsTileZ++; NewPlayerP.AbsTileZ++;
@@ -338,51 +300,36 @@ void UpdateAndRender(thread_context *Thread, game_memory *Memory,
F32 ScreenCenterX = 0.5f * (F32)Buffer->Width; F32 ScreenCenterX = 0.5f * (F32)Buffer->Width;
F32 ScreenCenterY = 0.5f * (F32)Buffer->Height; F32 ScreenCenterY = 0.5f * (F32)Buffer->Height;
DrawRectangle(Buffer, 0.0f, 0.0f, DrawRectangle(Buffer, 0.0f, 0.0f, (F32)Buffer->Width, (F32)Buffer->Height, 0.0f, 1.0f, 0.0f);
(F32)Buffer->Width, (F32)Buffer->Height, for(S32 RelRow = -10; RelRow < 10; RelRow++) {
0.0f, 1.0f, 0.0f); for(S32 RelColumn = -20; RelColumn < 20; RelColumn++) {
{ U32 Column = GameState->PlayerP.AbsTileX + RelColumn;
for(S32 RelRow = -10; RelRow < 10; RelRow++) { U32 Row = GameState->PlayerP.AbsTileY + RelRow;
for(S32 RelColumn = -20; RelColumn < 20; RelColumn++) {
U32 Column = GameState->PlayerP.AbsTileX + RelColumn;
U32 Row = GameState->PlayerP.AbsTileY + RelRow;
U32 TileID = GetTileValue(TileMap, U32 TileID = GetTileValue(TileMap, Column, Row, GameState->PlayerP.AbsTileZ);
Column,
Row,
GameState->PlayerP.AbsTileZ);
if(TileID > 0) { if(TileID > 0) {
F32 Gray = 0.5f; F32 Gray = 0.5f;
if(TileID == 2) { if(TileID == 2) {
Gray = 1.0f; Gray = 1.0f;
}
if(TileID > 2) {
Gray = 0.25f;
}
if((Column == GameState->PlayerP.AbsTileX) &&
(Row == GameState->PlayerP.AbsTileY))
{
Gray = 0.0f;
}
F32 CenterX = ScreenCenterX -
MetersToPixels*GameState->PlayerP.OffsetX +
(F32)RelColumn*(F32)TileSideInPixels;
F32 CenterY = ScreenCenterY +
MetersToPixels*GameState->PlayerP.OffsetY -
(F32)RelRow*(F32)TileSideInPixels;
F32 MinX = CenterX - 0.5f*(F32)TileSideInPixels;
F32 MinY = CenterY - 0.5f*(F32)TileSideInPixels;
F32 MaxX = CenterX + 0.5f*(F32)TileSideInPixels;
F32 MaxY = CenterY + 0.5f*(F32)TileSideInPixels;
DrawRectangle(Buffer,
MinX, MinY, MaxX, MaxY,
Gray, Gray, Gray);
} }
if(TileID > 2) {
Gray = 0.25f;
}
if((Column == GameState->PlayerP.AbsTileX) && (Row == GameState->PlayerP.AbsTileY)) {
Gray = 0.0f;
}
F32 CenterX = ScreenCenterX - MetersToPixels*GameState->PlayerP.OffsetX + (F32)RelColumn*(F32)TileSideInPixels;
F32 CenterY = ScreenCenterY + MetersToPixels*GameState->PlayerP.OffsetY - (F32)RelRow*(F32)TileSideInPixels;
F32 MinX = CenterX - 0.5f*(F32)TileSideInPixels;
F32 MinY = CenterY - 0.5f*(F32)TileSideInPixels;
F32 MaxX = CenterX + 0.5f*(F32)TileSideInPixels;
F32 MaxY = CenterY + 0.5f*(F32)TileSideInPixels;
DrawRectangle(Buffer, MinX, MinY, MaxX, MaxY, Gray, Gray, Gray);
} }
} }
} }
@@ -392,11 +339,7 @@ void UpdateAndRender(thread_context *Thread, game_memory *Memory,
F32 PlayerB = 0.0f; F32 PlayerB = 0.0f;
F32 PlayerLeft = ScreenCenterX - 0.5f * MetersToPixels*PlayerWidth; F32 PlayerLeft = ScreenCenterX - 0.5f * MetersToPixels*PlayerWidth;
F32 PlayerTop = ScreenCenterY - MetersToPixels*PlayerHeight; F32 PlayerTop = ScreenCenterY - MetersToPixels*PlayerHeight;
DrawRectangle(Buffer, DrawRectangle(Buffer, PlayerLeft, PlayerTop, PlayerLeft + MetersToPixels*PlayerWidth, PlayerTop + MetersToPixels*PlayerHeight, PlayerR, PlayerG, PlayerB);
PlayerLeft, PlayerTop,
PlayerLeft + MetersToPixels*PlayerWidth,
PlayerTop + MetersToPixels*PlayerHeight,
PlayerR, PlayerG, PlayerB);
GameOutputSound(GameState, SoundBuffer, 400); GameOutputSound(GameState, SoundBuffer, 400);
} }
+4 -6
View File
@@ -80,8 +80,8 @@ typedef struct game_controller_input {
/* NOTE: All buttons must be added above this line. */ /* NOTE: All buttons must be added above this line. */
game_button_state Terminator; game_button_state Terminator;
} buttons; };
} u; };
} game_controller_input; } game_controller_input;
typedef struct game_input { typedef struct game_input {
@@ -116,10 +116,8 @@ typedef struct game_memory {
layer.) */ layer.) */
#if BUILD_INTERNAL #if BUILD_INTERNAL
debug_read_file_result (*DEBUGPlatformReadEntireFile)( debug_read_file_result (*DEBUGPlatformReadEntireFile)(thread_context *Thread,const char *);
thread_context *Thread,const char *); B32 (*DEBUGPlatformWriteEntireFile)(thread_context *Thread, const char *, U32, void *);
B32 (*DEBUGPlatformWriteEntireFile)(thread_context *Thread,
const char *, U32, void *);
void (*DEBUGPlatformFreeFileMemory)(thread_context *Thread, void *); void (*DEBUGPlatformFreeFileMemory)(thread_context *Thread, void *);
#endif #endif
} game_memory; } game_memory;
+21 -72
View File
@@ -1,9 +1,6 @@
#include "handmade_tile.h" #include "handmade_tile.h"
static tile_chunk *GetTileChunk(tile_map *TileMap, static tile_chunk *GetTileChunk(tile_map *TileMap, U32 TileChunkX, U32 TileChunkY, U32 TileChunkZ)
U32 TileChunkX,
U32 TileChunkY,
U32 TileChunkZ)
{ {
tile_chunk *TileChunk = NULL; tile_chunk *TileChunk = NULL;
@@ -11,20 +8,13 @@ static tile_chunk *GetTileChunk(tile_map *TileMap,
(TileChunkY >= 0) && (TileChunkY < TileMap->TileChunkCountY) && (TileChunkY >= 0) && (TileChunkY < TileMap->TileChunkCountY) &&
(TileChunkZ >= 0) && (TileChunkZ < TileMap->TileChunkCountZ)) (TileChunkZ >= 0) && (TileChunkZ < TileMap->TileChunkCountZ))
{ {
TileChunk = TileChunk = &TileMap->TileChunks[ (TileChunkZ*TileMap->TileChunkCountY*TileMap->TileChunkCountX) + (TileChunkY*TileMap->TileChunkCountX) + TileChunkX];
&TileMap->TileChunks[
(TileChunkZ*TileMap->TileChunkCountY*TileMap->TileChunkCountX) +
(TileChunkY*TileMap->TileChunkCountX) +
TileChunkX];
} }
return TileChunk; return TileChunk;
} }
static tile_chunk_position GetChunkPositionFor(tile_map *TileMap, static tile_chunk_position GetChunkPositionFor(tile_map *TileMap, U32 AbsTileX, U32 AbsTileY, U32 AbsTileZ)
U32 AbsTileX,
U32 AbsTileY,
U32 AbsTileZ)
{ {
tile_chunk_position Result; tile_chunk_position Result;
@@ -37,8 +27,7 @@ static tile_chunk_position GetChunkPositionFor(tile_map *TileMap,
return Result; return Result;
} }
static U32 GetTileValueUnchecked(tile_map *TileMap, tile_chunk *TileChunk, static U32 GetTileValueUnchecked(tile_map *TileMap, tile_chunk *TileChunk, U32 TileX, U32 TileY)
U32 TileX, U32 TileY)
{ {
ASSERT(TileChunk); ASSERT(TileChunk);
ASSERT(TileX < TileMap->ChunkDim); ASSERT(TileX < TileMap->ChunkDim);
@@ -48,49 +37,32 @@ static U32 GetTileValueUnchecked(tile_map *TileMap, tile_chunk *TileChunk,
return TileChunkValue; return TileChunkValue;
} }
static U32 GetTileChunkValue(tile_map *TileMap, tile_chunk *TileChunk, static U32 GetTileChunkValue(tile_map *TileMap, tile_chunk *TileChunk, U32 TestTileX, U32 TestTileY)
U32 TestTileX, U32 TestTileY)
{ {
U32 TileChunkValue = 0; U32 TileChunkValue = 0;
if(TileChunk && TileChunk->Tiles) { if(TileChunk && TileChunk->Tiles) {
TileChunkValue = GetTileValueUnchecked(TileMap, TileChunkValue = GetTileValueUnchecked(TileMap, TileChunk, TestTileX, TestTileY);
TileChunk,
TestTileX,
TestTileY);
} }
return TileChunkValue; return TileChunkValue;
} }
static U32 GetTileValue(tile_map *TileMap, static U32 GetTileValue(tile_map *TileMap, U32 AbsTileX, U32 AbsTileY, U32 AbsTileZ)
U32 AbsTileX, U32 AbsTileY, U32 AbsTileZ)
{ {
tile_chunk_position ChunkPos = GetChunkPositionFor(TileMap, tile_chunk_position ChunkPos = GetChunkPositionFor(TileMap, AbsTileX, AbsTileY, AbsTileZ);
AbsTileX, tile_chunk *TileChunk = GetTileChunk(TileMap, ChunkPos.TileChunkX, ChunkPos.TileChunkY, ChunkPos.TileChunkZ);
AbsTileY, U32 TileChunkValue = GetTileChunkValue(TileMap, TileChunk, ChunkPos.RelTileX, ChunkPos.RelTileY);
AbsTileZ);
tile_chunk *TileChunk = GetTileChunk(TileMap,
ChunkPos.TileChunkX,
ChunkPos.TileChunkY,
ChunkPos.TileChunkZ);
U32 TileChunkValue = GetTileChunkValue(TileMap, TileChunk,
ChunkPos.RelTileX,
ChunkPos.RelTileY);
return TileChunkValue; return TileChunkValue;
} }
static U32 GetTileValueByPos(tile_map *TileMap, tile_map_position Pos) static U32 GetTileValueByPos(tile_map *TileMap, tile_map_position Pos)
{ {
U32 TileChunkValue = GetTileValue(TileMap, U32 TileChunkValue = GetTileValue(TileMap, Pos.AbsTileX, Pos.AbsTileY, Pos.AbsTileZ);
Pos.AbsTileX,
Pos.AbsTileY,
Pos.AbsTileZ);
return TileChunkValue; return TileChunkValue;
} }
static void SetTileValueUnchecked(tile_map *TileMap, tile_chunk *TileChunk, static void SetTileValueUnchecked(tile_map *TileMap, tile_chunk *TileChunk, U32 TileX, U32 TileY, U32 TileValue)
U32 TileX, U32 TileY, U32 TileValue)
{ {
ASSERT(TileChunk); ASSERT(TileChunk);
ASSERT(TileX < TileMap->ChunkDim); ASSERT(TileX < TileMap->ChunkDim);
@@ -99,41 +71,25 @@ static void SetTileValueUnchecked(tile_map *TileMap, tile_chunk *TileChunk,
TileChunk->Tiles[TileY * TileMap->ChunkDim + TileX] = TileValue; TileChunk->Tiles[TileY * TileMap->ChunkDim + TileX] = TileValue;
} }
static void SetTileChunkValue(tile_map *TileMap, tile_chunk *TileChunk, static void SetTileChunkValue(tile_map *TileMap, tile_chunk *TileChunk, U32 TestTileX, U32 TestTileY, U32 TileValue)
U32 TestTileX, U32 TestTileY,
U32 TileValue)
{ {
if(TileChunk && TileChunk->Tiles) { if(TileChunk && TileChunk->Tiles) {
SetTileValueUnchecked(TileMap, SetTileValueUnchecked(TileMap, TileChunk, TestTileX, TestTileY, TileValue);
TileChunk,
TestTileX, TestTileY,
TileValue);
} }
} }
static B32 IsTileMapPointEmpty(tile_map *TileMap, tile_map_position Pos) static B32 IsTileMapPointEmpty(tile_map *TileMap, tile_map_position Pos)
{ {
U32 TileChunkValue = GetTileValueByPos(TileMap, Pos); U32 TileChunkValue = GetTileValueByPos(TileMap, Pos);
B32 Empty = ((TileChunkValue == 1) || B32 Empty = ((TileChunkValue == 1) || (TileChunkValue == 3) || (TileChunkValue == 4));
(TileChunkValue == 3) ||
(TileChunkValue == 4));
return Empty; return Empty;
} }
static void SetTileValue(memory_arena *Arena, static void SetTileValue(memory_arena *Arena, tile_map *TileMap, U32 AbsTileX, U32 AbsTileY, U32 AbsTileZ, U32 TileValue)
tile_map *TileMap,
U32 AbsTileX, U32 AbsTileY, U32 AbsTileZ,
U32 TileValue)
{ {
tile_chunk_position ChunkPos = GetChunkPositionFor(TileMap, tile_chunk_position ChunkPos = GetChunkPositionFor(TileMap, AbsTileX, AbsTileY, AbsTileZ);
AbsTileX, tile_chunk *TileChunk = GetTileChunk(TileMap, ChunkPos.TileChunkX, ChunkPos.TileChunkY, ChunkPos.TileChunkZ);
AbsTileY,
AbsTileZ);
tile_chunk *TileChunk = GetTileChunk(TileMap,
ChunkPos.TileChunkX,
ChunkPos.TileChunkY,
ChunkPos.TileChunkZ);
ASSERT(TileChunk); ASSERT(TileChunk);
if(!TileChunk->Tiles) { if(!TileChunk->Tiles) {
@@ -143,13 +99,9 @@ static void SetTileValue(memory_arena *Arena,
for(U32 TileIndex = 0; TileIndex < TileCount; TileIndex++) { for(U32 TileIndex = 0; TileIndex < TileCount; TileIndex++) {
TileChunk->Tiles[TileIndex] = 1; TileChunk->Tiles[TileIndex] = 1;
} }
} }
SetTileChunkValue(TileMap, TileChunk, SetTileChunkValue(TileMap, TileChunk, ChunkPos.RelTileX, ChunkPos.RelTileY, TileValue);
ChunkPos.RelTileX,
ChunkPos.RelTileY,
TileValue);
} }
// //
@@ -168,8 +120,7 @@ static void CannonicalizeCoord(tile_map *TileMap, U32 *Tile, F32 *TileRel)
ASSERT(*TileRel <= 0.5f*TileMap->TileSideInMeters); ASSERT(*TileRel <= 0.5f*TileMap->TileSideInMeters);
} }
static tile_map_position RecannonicalizePosition(tile_map *TileMap, static tile_map_position RecannonicalizePosition(tile_map *TileMap, tile_map_position Pos)
tile_map_position Pos)
{ {
tile_map_position Result = Pos; tile_map_position Result = Pos;
@@ -181,8 +132,6 @@ static tile_map_position RecannonicalizePosition(tile_map *TileMap,
static B32 AreOnSameTile(tile_map_position *A, tile_map_position *B) static B32 AreOnSameTile(tile_map_position *A, tile_map_position *B)
{ {
B32 Result = ((A->AbsTileX == B->AbsTileX) && B32 Result = ((A->AbsTileX == B->AbsTileX) && (A->AbsTileY == B->AbsTileY) && (A->AbsTileZ == B->AbsTileZ));
(A->AbsTileY == B->AbsTileY) &&
(A->AbsTileZ == B->AbsTileZ));
return Result; return Result;
} }
+88 -279
View File
@@ -55,8 +55,7 @@ static U32 SafeTruncateUInt64(U64 Value)
return Result; return Result;
} }
debug_read_file_result DEBUGPlatformReadEntireFile(thread_context *Thread, debug_read_file_result DEBUGPlatformReadEntireFile(thread_context *Thread, const char *Filename)
const char *Filename)
{ {
debug_read_file_result Result; debug_read_file_result Result;
memset(&Result, 0, sizeof(Result)); memset(&Result, 0, sizeof(Result));
@@ -100,12 +99,9 @@ debug_read_file_result DEBUGPlatformReadEntireFile(thread_context *Thread,
return Result; return Result;
} }
B32 DEBUGPlatformWriteEntireFile(thread_context *Thread, B32 DEBUGPlatformWriteEntireFile(thread_context *Thread, const char *Filename, U32 MemorySize, void *Memory)
const char *Filename, U32 MemorySize,
void *Memory)
{ {
S32 FileHandle = open(Filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S32 FileHandle = open(Filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
S_IRGRP | S_IROTH);
if(FileHandle == -1) if(FileHandle == -1)
return FALSE; return FALSE;
@@ -141,8 +137,7 @@ static sdl_window_dimension SDLGetWindowDimension(SDL_Window *Window)
return Dimension; return Dimension;
} }
static void SDLResizeTexture(offscreen_buffer *Buffer, static void SDLResizeTexture(offscreen_buffer *Buffer, SDL_Renderer *Renderer, int Width, int Height)
SDL_Renderer *Renderer, int Width, int Height)
{ {
if(Buffer->Texture) if(Buffer->Texture)
SDL_DestroyTexture(Buffer->Texture); SDL_DestroyTexture(Buffer->Texture);
@@ -152,9 +147,7 @@ static void SDLResizeTexture(offscreen_buffer *Buffer,
S32 BytesPerPixel = 4; S32 BytesPerPixel = 4;
Buffer->Texture = SDL_CreateTexture(Renderer, SDL_PIXELFORMAT_ARGB8888, Buffer->Texture = SDL_CreateTexture(Renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, Width, Height);
SDL_TEXTUREACCESS_STREAMING,
Width, Height);
Buffer->Memory = malloc(Width * Height * BytesPerPixel); Buffer->Memory = malloc(Width * Height * BytesPerPixel);
Buffer->Height = Height; Buffer->Height = Height;
@@ -162,9 +155,7 @@ static void SDLResizeTexture(offscreen_buffer *Buffer,
Buffer->Pitch = Width * BytesPerPixel; Buffer->Pitch = Width * BytesPerPixel;
} }
static void SDLDisplayBufferInWindow(offscreen_buffer Buffer, static void SDLDisplayBufferInWindow(offscreen_buffer Buffer, SDL_Window *Window, SDL_Renderer *Renderer)
SDL_Window *Window,
SDL_Renderer *Renderer)
{ {
S32 OffsetX = 10; S32 OffsetX = 10;
S32 OffsetY = 10; S32 OffsetY = 10;
@@ -179,8 +170,7 @@ static void SDLDisplayBufferInWindow(offscreen_buffer Buffer,
/* TODO: We temporarily introduce the target rectangle to avoid /* TODO: We temporarily introduce the target rectangle to avoid
* stretching the canvas. */ * stretching the canvas. */
/* SDL_RenderCopy(Renderer, Buffer.Texture, 0, 0); */ /* SDL_RenderCopy(Renderer, Buffer.Texture, 0, 0); */
SDL_Rect dest_rect = { OffsetX, OffsetY, SDL_Rect dest_rect = { OffsetX, OffsetY, RESOLUTION_WIDTH, RESOLUTION_HEIGHT };
RESOLUTION_WIDTH, RESOLUTION_HEIGHT };
SDL_RenderCopy(Renderer, Buffer.Texture, 0, (const SDL_Rect *)(&dest_rect)); SDL_RenderCopy(Renderer, Buffer.Texture, 0, (const SDL_Rect *)(&dest_rect));
SDL_RenderPresent(Renderer); SDL_RenderPresent(Renderer);
} }
@@ -194,8 +184,7 @@ static void SDLInitControllers()
continue; continue;
if(ControllerIndex >= MAX_CONTROLLERS) if(ControllerIndex >= MAX_CONTROLLERS)
break; break;
ControllerHandles[ControllerIndex] = ControllerHandles[ControllerIndex] = SDL_GameControllerOpen(JoystickIndex);
SDL_GameControllerOpen(JoystickIndex);
ControllerIndex++; ControllerIndex++;
} }
} }
@@ -203,10 +192,7 @@ static void SDLInitControllers()
/* /*
static void SDLDeinitControllers() static void SDLDeinitControllers()
{ {
for(S32 ControllerIndex = 0; for(S32 ControllerIndex = 0; ControllerIndex < MAX_CONTROLLERS; ControllerIndex++) {
ControllerIndex < MAX_CONTROLLERS;
ControllerIndex++)
{
if(ControllerHandles[ControllerIndex]) if(ControllerHandles[ControllerIndex])
SDL_GameControllerClose(ControllerHandles[ControllerIndex]); SDL_GameControllerClose(ControllerHandles[ControllerIndex]);
} }
@@ -230,8 +216,7 @@ static void SDLInitSound(S32 SamplesPerSecond, S32 BufferSize)
} }
} }
static void SDLFillSoundBuffer(sdl_sound_output *SoundOutput, static void SDLFillSoundBuffer(sdl_sound_output *SoundOutput, S32 BytesToWrite)
S32 BytesToWrite)
{ {
SDL_QueueAudio(1, SoundOutput->Samples, BytesToWrite); SDL_QueueAudio(1, SoundOutput->Samples, BytesToWrite);
} }
@@ -241,8 +226,7 @@ static void SDLClearSoundBuffer(sdl_sound_output *SoundOutput)
memset(SoundOutput->Samples, 0, SoundOutput->SecondaryBufferSize); memset(SoundOutput->Samples, 0, SoundOutput->SecondaryBufferSize);
} }
static void SDLProcessKeyboardMessage(game_button_state *NewState, static void SDLProcessKeyboardMessage(game_button_state *NewState, B32 IsDown)
B32 IsDown)
{ {
if(NewState->EndedDown != IsDown) { if(NewState->EndedDown != IsDown) {
NewState->EndedDown = IsDown; NewState->EndedDown = IsDown;
@@ -250,14 +234,10 @@ static void SDLProcessKeyboardMessage(game_button_state *NewState,
} }
} }
static void SDLProcessInputDigitalButton(SDL_GameController *Controller, static void SDLProcessInputDigitalButton(SDL_GameController *Controller, game_button_state *OldState, SDL_GameControllerButton Button, game_button_state *NewState)
game_button_state *OldState,
SDL_GameControllerButton Button,
game_button_state *NewState)
{ {
NewState->EndedDown = SDL_GameControllerGetButton(Controller, Button); NewState->EndedDown = SDL_GameControllerGetButton(Controller, Button);
NewState->HalfTransitionCount = NewState->HalfTransitionCount = (OldState->EndedDown != NewState->EndedDown) ? 1 : 0;
(OldState->EndedDown != NewState->EndedDown) ? 1 : 0;
} }
static void HandleEvent(SDL_Event *Event) static void HandleEvent(SDL_Event *Event)
@@ -274,14 +254,12 @@ static void HandleEvent(SDL_Event *Event)
switch(Event->window.event) { switch(Event->window.event) {
/* TODO: We temporarily disable for ease of writing the /* TODO: We temporarily disable for ease of writing the
* rendering code. */ * rendering code. */
#if 0
case SDL_WINDOWEVENT_SIZE_CHANGED: {
sdl_window_dimension Dimension;
Dimension = SDLGetWindowDimension(Window); #if 0
/* TODO: For now we fix the width and height. */ /* TODO: For now we fix the width and height. */
SDLResizeTexture(&GlobalBackbuffer, Renderer, case SDL_WINDOWEVENT_SIZE_CHANGED: {
Dimension.Width, Dimension.Height); sdl_window_dimension Dimension = SDLGetWindowDimension(Window);
SDLResizeTexture(&GlobalBackbuffer, Renderer, Dimension.Width, Dimension.Height);
} break; } break;
#endif #endif
@@ -328,53 +306,29 @@ static void SDLProcessMessages(sdl_state *SDLState,
if(Event.key.repeat == 0) { if(Event.key.repeat == 0) {
if(KeyCode == SDLK_w) { if(KeyCode == SDLK_w) {
SDLProcessKeyboardMessage( SDLProcessKeyboardMessage(&KeyboardController->MoveUp, IsDown);
&KeyboardController->u.buttons.MoveUp,
IsDown);
} else if(KeyCode == SDLK_s) { } else if(KeyCode == SDLK_s) {
SDLProcessKeyboardMessage( SDLProcessKeyboardMessage(&KeyboardController->MoveDown, IsDown);
&KeyboardController->u.buttons.MoveDown,
IsDown);
} else if(KeyCode == SDLK_a) { } else if(KeyCode == SDLK_a) {
SDLProcessKeyboardMessage( SDLProcessKeyboardMessage(&KeyboardController->MoveLeft, IsDown);
&KeyboardController->u.buttons.MoveLeft,
IsDown);
} else if(KeyCode == SDLK_d) { } else if(KeyCode == SDLK_d) {
SDLProcessKeyboardMessage( SDLProcessKeyboardMessage(&KeyboardController->MoveRight, IsDown);
&KeyboardController->u.buttons.MoveRight,
IsDown);
} else if(KeyCode == SDLK_q) { } else if(KeyCode == SDLK_q) {
SDLProcessKeyboardMessage( SDLProcessKeyboardMessage(&KeyboardController->LeftShoulder, IsDown);
&KeyboardController->u.buttons.LeftShoulder,
IsDown);
} else if(KeyCode == SDLK_e) { } else if(KeyCode == SDLK_e) {
SDLProcessKeyboardMessage( SDLProcessKeyboardMessage(&KeyboardController->RightShoulder, IsDown);
&KeyboardController->u.buttons.RightShoulder,
IsDown);
} else if(KeyCode == SDLK_UP) { } else if(KeyCode == SDLK_UP) {
SDLProcessKeyboardMessage( SDLProcessKeyboardMessage(&KeyboardController->ActionUp, IsDown);
&KeyboardController->u.buttons.ActionUp,
IsDown);
} else if(KeyCode == SDLK_DOWN) { } else if(KeyCode == SDLK_DOWN) {
SDLProcessKeyboardMessage( SDLProcessKeyboardMessage(&KeyboardController->ActionDown, IsDown);
&KeyboardController->u.buttons.ActionDown,
IsDown);
} else if(KeyCode == SDLK_LEFT) { } else if(KeyCode == SDLK_LEFT) {
SDLProcessKeyboardMessage( SDLProcessKeyboardMessage(&KeyboardController->ActionLeft, IsDown);
&KeyboardController->u.buttons.ActionLeft,
IsDown);
} else if(KeyCode == SDLK_RIGHT) { } else if(KeyCode == SDLK_RIGHT) {
SDLProcessKeyboardMessage( SDLProcessKeyboardMessage(&KeyboardController->ActionRight, IsDown);
&KeyboardController->u.buttons.ActionRight,
IsDown);
} else if(KeyCode == SDLK_ESCAPE) { } else if(KeyCode == SDLK_ESCAPE) {
SDLProcessKeyboardMessage( SDLProcessKeyboardMessage(&KeyboardController->Start, IsDown);
&KeyboardController->u.buttons.Start,
IsDown);
} else if(KeyCode == SDLK_SPACE) { } else if(KeyCode == SDLK_SPACE) {
SDLProcessKeyboardMessage( SDLProcessKeyboardMessage(&KeyboardController->Back, IsDown);
&KeyboardController->u.buttons.Back,
IsDown);
} }
} }
@@ -404,11 +358,9 @@ static F32 SDLProcessInputStickValue(F32 Value, S32 DeadZoneThreshold)
{ {
F32 Result = 0.0f; F32 Result = 0.0f;
if(Value < -(F32)DeadZoneThreshold) if(Value < -(F32)DeadZoneThreshold)
Result = ((Value + (F32)DeadZoneThreshold) / Result = ((Value + (F32)DeadZoneThreshold) / (32768.0f - (F32)DeadZoneThreshold));
(32768.0f - (F32)DeadZoneThreshold));
else if(Value > (F32)DeadZoneThreshold) else if(Value > (F32)DeadZoneThreshold)
Result = ((Value + (F32)DeadZoneThreshold) / Result = ((Value + (F32)DeadZoneThreshold) / (32767.0f - (F32)DeadZoneThreshold));
(32767.0f - (F32)DeadZoneThreshold));
return Result; return Result;
} }
@@ -441,9 +393,7 @@ typedef struct sdl_game_code {
void *GameCodeDLL; void *GameCodeDLL;
U32 DLLLastWriteTime; U32 DLLLastWriteTime;
void (*UpdateAndRender)(thread_context *Thread, game_memory *, void (*UpdateAndRender)(thread_context *Thread, game_memory *, game_input *, game_offscreen_buffer *, game_sound_output_buffer *);
game_input *, game_offscreen_buffer *,
game_sound_output_buffer *);
B32 IsValid; B32 IsValid;
} sdl_game_code; } sdl_game_code;
@@ -470,10 +420,7 @@ static sdl_game_code SDLLoadGameCode(char *DLLPath)
if(Result.GameCodeDLL) { if(Result.GameCodeDLL) {
Result.DLLLastWriteTime = GetLastWriteTime(DLLPath); Result.DLLLastWriteTime = GetLastWriteTime(DLLPath);
Result.UpdateAndRender = Result.UpdateAndRender = (void(*)(thread_context *, game_memory *, game_input *, game_offscreen_buffer *, game_sound_output_buffer *)) dlsym(Result.GameCodeDLL, "UpdateAndRender");
(void(*)(thread_context *, game_memory *, game_input *,
game_offscreen_buffer *, game_sound_output_buffer *))
dlsym(Result.GameCodeDLL, "UpdateAndRender");
if(!Result.UpdateAndRender) { if(!Result.UpdateAndRender) {
/* TODO: Diagnostic. dlerror() */ /* TODO: Diagnostic. dlerror() */
} }
@@ -590,12 +537,9 @@ S32 main(S32 argc, char *argv[])
memset(&SDLState, 0, sizeof(SDLState)); memset(&SDLState, 0, sizeof(SDLState));
SDLGetEXEDirPath(SDLState.EXEDirPath, sizeof(SDLState.EXEDirPath)); SDLGetEXEDirPath(SDLState.EXEDirPath, sizeof(SDLState.EXEDirPath));
SDLGetDLLPath(SDLState.DLLPath, sizeof(SDLState.DLLPath), SDLGetDLLPath(SDLState.DLLPath, sizeof(SDLState.DLLPath), SDLState.EXEDirPath);
SDLState.EXEDirPath);
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER | if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER | SDL_INIT_AUDIO)) {
SDL_INIT_AUDIO))
{
/* TODO: This didn't work . . . */ /* TODO: This didn't work . . . */
} }
@@ -604,11 +548,7 @@ S32 main(S32 argc, char *argv[])
/* NOTE: Floating windows are not supported oficially by Wayland /* NOTE: Floating windows are not supported oficially by Wayland
* protocol. However, SDL_WINDOW_ALWAYS_ON_TOP does work on KDE * protocol. However, SDL_WINDOW_ALWAYS_ON_TOP does work on KDE
* under Wayland. I have not tested GNOME. */ * under Wayland. I have not tested GNOME. */
SDL_Window *Window = SDL_CreateWindow("Handmade Hero", SDL_Window *Window = SDL_CreateWindow("Handmade Hero", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, RESOLUTION_WIDTH, RESOLUTION_HEIGHT, SDL_WINDOW_RESIZABLE);
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
RESOLUTION_WIDTH, RESOLUTION_HEIGHT,
SDL_WINDOW_RESIZABLE);
/*SDL_WINDOW_RESIZABLE | /*SDL_WINDOW_RESIZABLE |
SDL_WINDOW_ALWAYS_ON_TOP);*/ SDL_WINDOW_ALWAYS_ON_TOP);*/
@@ -616,10 +556,8 @@ S32 main(S32 argc, char *argv[])
SDL_DisplayMode display_mode; SDL_DisplayMode display_mode;
SDL_GetCurrentDisplayMode(0, &display_mode); SDL_GetCurrentDisplayMode(0, &display_mode);
SDL_Renderer *Renderer = SDL_Renderer *Renderer = SDL_CreateRenderer(Window, -1, SDL_RENDERER_PRESENTVSYNC);
SDL_CreateRenderer(Window, -1, SDL_RENDERER_PRESENTVSYNC); SDL_SetWindowPosition(Window, (display_mode.w - RESOLUTION_WIDTH), (display_mode.h - RESOLUTION_HEIGHT));
SDL_SetWindowPosition(Window, (display_mode.w - RESOLUTION_WIDTH),
(display_mode.h - RESOLUTION_HEIGHT));
/* "Wayland needs an event loop and rendering or it won't function." /* "Wayland needs an event loop and rendering or it won't function."
* https://github.com/libsdl-org/SDL/issues/7699#issuecomment-1545684792 */ * https://github.com/libsdl-org/SDL/issues/7699#issuecomment-1545684792 */
@@ -644,27 +582,20 @@ S32 main(S32 argc, char *argv[])
GlobalRunning = TRUE; GlobalRunning = TRUE;
sdl_window_dimension Dimension = SDLGetWindowDimension(Window); sdl_window_dimension Dimension = SDLGetWindowDimension(Window);
SDLResizeTexture(&GlobalBackbuffer, Renderer, Dimension.Width, SDLResizeTexture(&GlobalBackbuffer, Renderer, Dimension.Width, Dimension.Height);
Dimension.Height);
sdl_sound_output SoundOutput; sdl_sound_output SoundOutput;
memset(&SoundOutput, 0, sizeof(SoundOutput)); memset(&SoundOutput, 0, sizeof(SoundOutput));
SoundOutput.SamplesPerSecond = 48000; SoundOutput.SamplesPerSecond = 48000;
SoundOutput.BytesPerSample = sizeof(S16) * 2; SoundOutput.BytesPerSample = sizeof(S16) * 2;
SoundOutput.SecondaryBufferSize = SoundOutput.SecondaryBufferSize = SoundOutput.SamplesPerSecond * SoundOutput.BytesPerSample;
SoundOutput.SamplesPerSecond * SoundOutput.BytesPerSample;
SoundOutput.tSine = 0; SoundOutput.tSine = 0;
SoundOutput.LatencySampleCount = SoundOutput.LatencySampleCount = SoundOutput.SamplesPerSecond / 15;
SoundOutput.SamplesPerSecond / 15;
SDLInitSound(SoundOutput.SamplesPerSecond, SDLInitSound(SoundOutput.SamplesPerSecond, SoundOutput.SamplesPerSecond * SoundOutput.BytesPerSample / 60);
SoundOutput.SamplesPerSecond * SoundOutput.BytesPerSample /
60);
SDL_PauseAudio(0); SDL_PauseAudio(0);
SoundOutput.Samples = SoundOutput.Samples = calloc(SoundOutput.SamplesPerSecond, SoundOutput.BytesPerSample);
calloc(SoundOutput.SamplesPerSecond,
SoundOutput.BytesPerSample);
/* SoundOutput.Samples = /* SoundOutput.Samples =
* malloc(SoundOutput.SecondaryBufferSize); */ * malloc(SoundOutput.SecondaryBufferSize); */
/* NOTE: calloc auto clears to zero */ /* NOTE: calloc auto clears to zero */
@@ -674,30 +605,18 @@ S32 main(S32 argc, char *argv[])
memset(&GameMemory, 0, sizeof(GameMemory)); memset(&GameMemory, 0, sizeof(GameMemory));
GameMemory.PermanentStorageSize = MB(64); GameMemory.PermanentStorageSize = MB(64);
GameMemory.TransientStorageSize = GB(1); GameMemory.TransientStorageSize = GB(1);
GameMemory.DEBUGPlatformReadEntireFile = GameMemory.DEBUGPlatformReadEntireFile = &DEBUGPlatformReadEntireFile;
&DEBUGPlatformReadEntireFile; GameMemory.DEBUGPlatformWriteEntireFile = &DEBUGPlatformWriteEntireFile;
GameMemory.DEBUGPlatformWriteEntireFile = GameMemory.DEBUGPlatformFreeFileMemory = &DEBUGPlatformFreeFileMemory;
&DEBUGPlatformWriteEntireFile;
GameMemory.DEBUGPlatformFreeFileMemory =
&DEBUGPlatformFreeFileMemory;
SDLState.TotalSize = SDLState.TotalSize = GameMemory.PermanentStorageSize + GameMemory.TransientStorageSize;
GameMemory.PermanentStorageSize +
GameMemory.TransientStorageSize;
/* NOTE: On MacOS and Linux, mmap seems to zero-fill anonymous /* NOTE: On MacOS and Linux, mmap seems to zero-fill anonymous
* memory as a side-effect of security. */ * memory as a side-effect of security. */
SDLState.GameMmemoryBlock = SDLState.GameMmemoryBlock = mmap(BaseAddress, (size_t)SDLState.TotalSize, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
mmap(BaseAddress, (size_t)SDLState.TotalSize, PROT_READ |
PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
GameMemory.PermanentStorage = SDLState.GameMmemoryBlock; GameMemory.PermanentStorage = SDLState.GameMmemoryBlock;
GameMemory.TransientStorage = GameMemory.TransientStorage = (U8 *)(GameMemory.PermanentStorage) + GameMemory.PermanentStorageSize;
(U8 *)(GameMemory.PermanentStorage) +
GameMemory.PermanentStorageSize;
if(SoundOutput.Samples && if(SoundOutput.Samples && GameMemory.PermanentStorage && GameMemory.TransientStorage) {
GameMemory.PermanentStorage &&
GameMemory.TransientStorage)
{
game_input Input[2]; game_input Input[2];
U64 LastCounter; U64 LastCounter;
@@ -734,193 +653,83 @@ S32 main(S32 argc, char *argv[])
Game = SDLLoadGameCode(SDLState.DLLPath); Game = SDLLoadGameCode(SDLState.DLLPath);
} }
U32 SDLMouseButtons = U32 SDLMouseButtons = SDL_GetMouseState(&(NewInput->MouseX), &(NewInput->MouseY));
SDL_GetMouseState(&(NewInput->MouseX),
&(NewInput->MouseY));
NewInput->MouseZ = 0; /* TODO: Support mouse wheel? */ NewInput->MouseZ = 0; /* TODO: Support mouse wheel? */
SDLProcessKeyboardMessage(&(NewInput->MouseButtons[0]), SDLProcessKeyboardMessage(&(NewInput->MouseButtons[0]), SDL_BUTTON_LMASK & SDLMouseButtons);
SDL_BUTTON_LMASK & SDLMouseButtons); SDLProcessKeyboardMessage(&(NewInput->MouseButtons[1]), SDL_BUTTON_MMASK & SDLMouseButtons);
SDLProcessKeyboardMessage(&(NewInput->MouseButtons[1]), SDLProcessKeyboardMessage(&(NewInput->MouseButtons[2]), SDL_BUTTON_RMASK & SDLMouseButtons);
SDL_BUTTON_MMASK & SDLMouseButtons); /* WARNING: TODO: SDL_BUTTON_X1MASK and SDL_BUTTON_X2MASK cannot be on at the
SDLProcessKeyboardMessage(&(NewInput->MouseButtons[2]),
SDL_BUTTON_RMASK & SDLMouseButtons);
/* WARNING: TODO: SDL_BUTTON_X1MASK and
* SDL_BUTTON_X2MASK cannot be on at the
* same time for some reason. */ * same time for some reason. */
SDLProcessKeyboardMessage(&(NewInput->MouseButtons[3]), SDLProcessKeyboardMessage(&(NewInput->MouseButtons[3]), SDL_BUTTON_X1MASK & SDLMouseButtons);
SDL_BUTTON_X1MASK & SDLMouseButtons); SDLProcessKeyboardMessage(&(NewInput->MouseButtons[4]), SDL_BUTTON_X2MASK & SDLMouseButtons);
SDLProcessKeyboardMessage(&(NewInput->MouseButtons[4]),
SDL_BUTTON_X2MASK & SDLMouseButtons);
game_controller_input *OldKeyboardController = game_controller_input *OldKeyboardController = GetController(OldInput, 0);
GetController(OldInput, 0); game_controller_input *NewKeyboardController = GetController(NewInput, 0);
game_controller_input *NewKeyboardController =
GetController(NewInput, 0);
game_controller_input ZeroController; game_controller_input ZeroController;
memset(&ZeroController, 0, sizeof(ZeroController)); memset(&ZeroController, 0, sizeof(ZeroController));
*NewKeyboardController = ZeroController; *NewKeyboardController = ZeroController;
NewKeyboardController->IsConnected = TRUE; NewKeyboardController->IsConnected = TRUE;
for(U32 ButtonIndex = 0; for(U32 ButtonIndex = 0; ButtonIndex < ARRAY_SIZE(NewKeyboardController->Buttons); ButtonIndex++) {
ButtonIndex < ARRAY_SIZE( NewKeyboardController->Buttons[ButtonIndex].EndedDown = OldKeyboardController->Buttons[ButtonIndex].EndedDown;
NewKeyboardController->u.Buttons);
ButtonIndex++)
{
NewKeyboardController->
u.Buttons[ButtonIndex].EndedDown =
OldKeyboardController->
u.Buttons[ButtonIndex].
EndedDown;
} }
SDLProcessMessages(&SDLState, NewKeyboardController); SDLProcessMessages(&SDLState, NewKeyboardController);
for(U32 ControllerIndex = 0; for(U32 ControllerIndex = 0; ControllerIndex < MAX_CONTROLLERS; ControllerIndex++) {
ControllerIndex < MAX_CONTROLLERS; game_controller_input *OldController = GetController(OldInput, ControllerIndex+1);
ControllerIndex++) game_controller_input *NewController = GetController(NewInput, ControllerIndex+1);
{
game_controller_input *OldController,
*NewController;
OldController = if(ControllerHandles[ControllerIndex] != 0 && SDL_GameControllerGetAttached(ControllerHandles[ControllerIndex])) {
GetController(OldInput, ControllerIndex+1); NewController->IsAnalog = OldController->IsAnalog;
NewController =
GetController(NewInput, ControllerIndex+1);
if(ControllerHandles[ControllerIndex] != 0 &&
SDL_GameControllerGetAttached(
ControllerHandles[ControllerIndex]))
{
NewController->IsAnalog =
OldController->IsAnalog;
NewController->IsConnected = TRUE; NewController->IsConnected = TRUE;
S16 StickX = S16 StickX = SDL_GameControllerGetAxis(ControllerHandles[ControllerIndex], SDL_CONTROLLER_AXIS_LEFTX);
SDL_GameControllerGetAxis( S16 StickY = SDL_GameControllerGetAxis(ControllerHandles[ControllerIndex], SDL_CONTROLLER_AXIS_LEFTY);
ControllerHandles[ControllerIndex],
SDL_CONTROLLER_AXIS_LEFTX);
S16 StickY =
SDL_GameControllerGetAxis(
ControllerHandles[ControllerIndex],
SDL_CONTROLLER_AXIS_LEFTY);
NewController->IsAnalog = TRUE; NewController->IsAnalog = TRUE;
NewController->StickAverageX = NewController->StickAverageX = SDLProcessInputStickValue((F32)StickX, CONTROLLER_LEFT_THUMB_DEADZONE);
SDLProcessInputStickValue((F32)StickX, NewController->StickAverageY = SDLProcessInputStickValue((F32)StickY, CONTROLLER_LEFT_THUMB_DEADZONE);
CONTROLLER_LEFT_THUMB_DEADZONE);
NewController->StickAverageY =
SDLProcessInputStickValue((F32)StickY,
CONTROLLER_LEFT_THUMB_DEADZONE);
if((NewController->StickAverageX != 0.0f) || if((NewController->StickAverageX != 0.0f) || (NewController->StickAverageY != 0.0f)) {
(NewController->StickAverageY != 0.0f))
{
NewController->IsAnalog = TRUE; NewController->IsAnalog = TRUE;
} }
if(SDL_GameControllerGetButton( if(SDL_GameControllerGetButton(ControllerHandles[ControllerIndex], SDL_CONTROLLER_BUTTON_DPAD_UP)) {
ControllerHandles[ControllerIndex],
SDL_CONTROLLER_BUTTON_DPAD_UP))
{
NewController->StickAverageY = -1.0f; NewController->StickAverageY = -1.0f;
NewController->IsAnalog = FALSE; NewController->IsAnalog = FALSE;
} }
if(SDL_GameControllerGetButton( if(SDL_GameControllerGetButton(ControllerHandles[ControllerIndex], SDL_CONTROLLER_BUTTON_DPAD_DOWN)) {
ControllerHandles[ControllerIndex],
SDL_CONTROLLER_BUTTON_DPAD_DOWN))
{
NewController->StickAverageY = 1.0f; NewController->StickAverageY = 1.0f;
NewController->IsAnalog = FALSE; NewController->IsAnalog = FALSE;
} }
if(SDL_GameControllerGetButton( if(SDL_GameControllerGetButton(ControllerHandles[ControllerIndex], SDL_CONTROLLER_BUTTON_DPAD_LEFT)) {
ControllerHandles[ControllerIndex],
SDL_CONTROLLER_BUTTON_DPAD_LEFT))
{
NewController->StickAverageX = -1.0f; NewController->StickAverageX = -1.0f;
NewController->IsAnalog = FALSE; NewController->IsAnalog = FALSE;
} }
if(SDL_GameControllerGetButton( if(SDL_GameControllerGetButton(ControllerHandles[ControllerIndex], SDL_CONTROLLER_BUTTON_DPAD_RIGHT)) {
ControllerHandles[ControllerIndex],
SDL_CONTROLLER_BUTTON_DPAD_RIGHT))
{
NewController->StickAverageX = 1.0f; NewController->StickAverageX = 1.0f;
NewController->IsAnalog = FALSE; NewController->IsAnalog = FALSE;
} }
F32 Threshold = 0.5f; F32 Threshold = 0.5f;
SDLProcessInputDigitalButton( SDLProcessInputDigitalButton(ControllerHandles[(NewController->StickAverageY < -Threshold) ? 1 : 0], &OldController->MoveUp, SDL_CONTROLLER_BUTTON_A, &NewController->MoveUp);
ControllerHandles[ SDLProcessInputDigitalButton(ControllerHandles[(NewController->StickAverageY > Threshold) ? 1 : 0], &OldController->MoveDown, SDL_CONTROLLER_BUTTON_A, &NewController->MoveDown);
(NewController->StickAverageY < SDLProcessInputDigitalButton(ControllerHandles[(NewController->StickAverageX < -Threshold) ? 1 : 0], &OldController->MoveLeft, SDL_CONTROLLER_BUTTON_A, &NewController->MoveLeft);
-Threshold) ? 1 : 0], SDLProcessInputDigitalButton(ControllerHandles[(NewController->StickAverageX > Threshold) ? 1 : 0], &OldController->MoveRight, SDL_CONTROLLER_BUTTON_A, &NewController->MoveRight);
&OldController->u.buttons.MoveUp,
SDL_CONTROLLER_BUTTON_A,
&NewController->u.buttons.MoveUp);
SDLProcessInputDigitalButton(
ControllerHandles[
(NewController->StickAverageY >
Threshold) ? 1 : 0],
&OldController->u.buttons.MoveDown,
SDL_CONTROLLER_BUTTON_A,
&NewController->u.buttons.MoveDown);
SDLProcessInputDigitalButton(
ControllerHandles[
(NewController->StickAverageX <
-Threshold) ? 1 : 0],
&OldController->u.buttons.MoveLeft,
SDL_CONTROLLER_BUTTON_A,
&NewController->u.buttons.MoveLeft);
SDLProcessInputDigitalButton(
ControllerHandles[
(NewController->StickAverageX >
Threshold) ? 1 : 0],
&OldController->u.buttons.MoveRight,
SDL_CONTROLLER_BUTTON_A,
&NewController->u.buttons.MoveRight);
SDLProcessInputDigitalButton( SDLProcessInputDigitalButton(ControllerHandles[ControllerIndex], &OldController->ActionDown, SDL_CONTROLLER_BUTTON_A, &NewController->ActionDown);
ControllerHandles[ControllerIndex], SDLProcessInputDigitalButton(ControllerHandles[ControllerIndex], &OldController->ActionRight, SDL_CONTROLLER_BUTTON_B, &NewController->ActionRight);
&OldController->u.buttons.ActionDown, SDLProcessInputDigitalButton(ControllerHandles[ControllerIndex], &OldController->ActionLeft, SDL_CONTROLLER_BUTTON_X, &NewController->ActionLeft);
SDL_CONTROLLER_BUTTON_A, SDLProcessInputDigitalButton(ControllerHandles[ControllerIndex], &OldController->ActionUp, SDL_CONTROLLER_BUTTON_Y, &NewController->ActionUp);
&NewController->u.buttons.ActionDown); SDLProcessInputDigitalButton(ControllerHandles[ControllerIndex], &OldController->LeftShoulder, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, &NewController->LeftShoulder);
SDLProcessInputDigitalButton( SDLProcessInputDigitalButton( ControllerHandles[ControllerIndex], &OldController->RightShoulder, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, &NewController->RightShoulder);
ControllerHandles[ControllerIndex],
&OldController->u.buttons.ActionRight,
SDL_CONTROLLER_BUTTON_B,
&NewController->u.buttons.ActionRight);
SDLProcessInputDigitalButton(
ControllerHandles[ControllerIndex],
&OldController->u.buttons.ActionLeft,
SDL_CONTROLLER_BUTTON_X,
&NewController->u.buttons.ActionLeft);
SDLProcessInputDigitalButton(
ControllerHandles[ControllerIndex],
&OldController->u.buttons.ActionUp,
SDL_CONTROLLER_BUTTON_Y,
&NewController->u.buttons.ActionUp);
SDLProcessInputDigitalButton(
ControllerHandles[ControllerIndex],
&OldController->u.buttons.LeftShoulder,
SDL_CONTROLLER_BUTTON_LEFTSHOULDER,
&NewController->u.buttons.LeftShoulder);
SDLProcessInputDigitalButton(
ControllerHandles[ControllerIndex],
&OldController->u.buttons.RightShoulder,
SDL_CONTROLLER_BUTTON_RIGHTSHOULDER,
&NewController->u.buttons.RightShoulder);
SDLProcessInputDigitalButton( SDLProcessInputDigitalButton(ControllerHandles[ControllerIndex], &OldController->Start, SDL_CONTROLLER_BUTTON_START, &NewController->Start);
ControllerHandles[ControllerIndex], SDLProcessInputDigitalButton( ControllerHandles[ControllerIndex], &OldController->Back, SDL_CONTROLLER_BUTTON_BACK, &NewController->Back);
&OldController->u.buttons.Start,
SDL_CONTROLLER_BUTTON_START,
&NewController->u.buttons.Start);
SDLProcessInputDigitalButton(
ControllerHandles[ControllerIndex],
&OldController->u.buttons.Back,
SDL_CONTROLLER_BUTTON_BACK,
&NewController->u.buttons.Back);
} else { } else {
/* NOTE: This controller is not plugged in. */ /* NOTE: This controller is not plugged in. */
NewController->IsConnected = FALSE; NewController->IsConnected = FALSE;