Clean up.

This commit is contained in:
2026-07-14 13:21:04 -07:00
parent 943ed21776
commit ffc4310646
12 changed files with 1445 additions and 955 deletions
+2
View File
@@ -1,4 +1,6 @@
.DS_Store
tags
handmadehero
handmadehero.dSYM
libhandmade.so
+3 -3
View File
@@ -45,7 +45,7 @@ endif
EXE=handmadehero
default: libhandmade $(EXE)
default: tags libhandmade $(EXE)
libhandmade: handmade.c
@ $(CC) $(FLAGS) $(UFLAGS) -fPIC -shared $(INCLUDE) $^ $(LIBS) $(FRAMEWORKS) -o libhandmade.so
@@ -57,7 +57,7 @@ run:
@ ./$(EXE)
tags: $(OBJS)
ctags -R *.h *.c
@ ctags -R *.h *.c
clean:
@rm -rf $(EXE) $(EXE).dSYM libhandmade.so libhandmade.so.dSYM
@rm -rf $(EXE) $(EXE).dSYM libhandmade.so libhandmade.so.dSYM tags
+13 -82
View File
@@ -1,8 +1,6 @@
#ifndef CORE_H
#define CORE_H
#include <stdint.h>
/* ---- Switches ------------------------------------------------------- */
#ifndef BUILD_DEBUG
@@ -18,7 +16,7 @@
/* Architecture */
#ifndef ARCHITECTURE_X64
#define ARCHITECTURE_X64 0
#define ARCHITECTURE_X64 1
#endif
#ifndef ARCHITECTURE_X86
@@ -33,24 +31,12 @@
#define ARCHITECTURE_ARM32 0
#endif
#if defined(__x86_64__) || defined(__x86_64) || defined(__amd64__) || defined(__amd64) || defined(_M_AMD64)
#undef ARCHITECTURE_X64
#define ARCHITECTURE_X64 1
#elif defined(i386) || defined(__i386) || defined(__i386__) || defined(_M_I86)
#undef ARCHITECTURE_X86
#define ARCHITECTURE_X86 1
#elif defined(__aarch64__)
#undef ARCHITECTURE_ARM64
#define ARCHITECTURE_ARM64 1
#elif defined(__arm__)
#undef ARCHITECTURE_ARM32
#define ARCHITECTURE_ARM32 1
#else
#error Unknown architecture . . .
#endif
/* OS */
#ifndef OS_LINUX
#define OS_LINUX 1
#endif
#ifndef OS_WINDOWS
#define OS_WINDOWS 0
#endif
@@ -59,81 +45,26 @@
#define OS_MACOS 0
#endif
#ifndef OS_LINUX
#define OS_LINUX 0
#endif
#if defined(_WIN32)
#undef OS_WINDOWS
#define OS_WINDOWS 1
#elif defined(__APPLE__) && defined(__MACH__)
#undef OS_MACOS
#define OS_MACOS 1
#elif defined(__linux__)
#undef OS_LINUX
#define OS_LINUX 1
#else
#error Unknown operating system . . .
#endif
/* Compiler */
#ifndef COMPILER_CLANG
#define COMPILER_CLANG 1
#endif
#ifndef COMPILER_MSVC
#define COMPILER_MSVC 0
#endif
#ifndef COMPILER_CLANG
#define COMPILER_CLANG 0
#endif
#ifndef COMPILER_GCC
#define COMPILER_GCC 0
#endif
#if _MSC_VER
#undef COMPILER_MSVC
#define COMPILER_MSVC 1
#elif __clang__
#undef COMPILER_CLANG
#define COMPILER_CLANG 1
#elif __GNUC__
#undef COMPILER_GCC
#define COMPILER_GCC 1
#else
#error Unknown compiler . . .
#endif
/* ---- Types ---------------------------------------------------------- */
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
typedef uint8_t U8;
typedef uint16_t U16;
typedef uint32_t U32;
typedef uint64_t U64;
typedef int8_t S8;
typedef int16_t S16;
typedef int32_t S32;
typedef int64_t S64;
typedef S8 B8;
typedef S16 B16;
typedef S32 B32;
typedef S64 B64;
typedef float F32;
typedef double F64;
/* ---- Units ---------------------------------------------------------- */
#define KB(n) (((U64)(n)) << 10)
#define MB(n) (((U64)(n)) << 20)
#define GB(n) (((U64)(n)) << 30)
#define TB(n) (((U64)(n)) << 40)
#define KB(n) (((unsigned long long)(n)) << 10)
#define MB(n) (((unsigned long long)(n)) << 20)
#define GB(n) (((unsigned long long)(n)) << 30)
#define TB(n) (((unsigned long long)(n)) << 40)
/* ---- Clamps, Min/Max ------------------------------------------------ */
+564 -331
View File
File diff suppressed because it is too large Load Diff
+138 -107
View File
@@ -7,137 +7,154 @@
/* NOTE: Servcies that the platform layer provides to the game. */
/* --------------------------------------------------------------------- */
typedef struct thread_context {
S32 Placeholder;
} thread_context;
struct thread_context {
int Placeholder;
};
#if BUILD_INTERNAL
typedef struct debug_read_file_result {
U32 ContentsSize;
void *Contents;
} debug_read_file_result;
struct debug_read_file_result {
unsigned int ContentsSize;
void *Contents;
};
debug_read_file_result DEBUGPlatformReadEntireFile(thread_context *Thread, const char *Filename);
B32 DEBUGPlatformWriteEntireFile(thread_context *Thread, const char *Filename, U32 MemorySize, void *Memory);
void DEBUGPlatformFreeFileMemory(thread_context *Thread, void *Memory);
struct debug_read_file_result
DEBUGPlatformReadEntireFile(struct thread_context *Thread,
const char *Filename);
int DEBUGPlatformWriteEntireFile(struct thread_context *Thread,
const char *Filename,
unsigned int MemorySize, void *Memory);
void DEBUGPlatformFreeFileMemory(struct thread_context *Thread,
void *Memory);
#endif
typedef struct game_offscreen_buffer game_offscreen_buffer;
void SetProgramIcon(game_offscreen_buffer *ProgramIcon);
struct game_offscreen_buffer game_offscreen_buffer;
void SetProgramIcon(struct game_offscreen_buffer *ProgramIcon);
/* --------------------------------------------------------------------- */
/* NOTE: Servcies that the game provides to the platform layer. */
/* --------------------------------------------------------------------- */
typedef struct game_offscreen_buffer {
struct game_offscreen_buffer {
/* Pixels are always 32-bit and have the bytes in BGRX (little-endian). */
void *Memory;
S32 Width;
S32 Height;
S32 BytesPerPixel;
S32 Pitch;
} game_offscreen_buffer;
int Width;
int Height;
int BytesPerPixel;
int Pitch;
};
typedef struct game_sound_output_buffer {
S32 SamplesPerSecond;
S32 SampleCount;
S16 *Samples;
} game_sound_output_buffer;
struct game_sound_output_buffer {
int SamplesPerSecond;
int SampleCount;
short *Samples;
};
typedef struct game_button_state {
S32 HalfTransitionCount;
B32 EndedDown;
} game_button_state;
struct game_button_state {
int HalfTransitionCount;
int EndedDown;
};
typedef struct game_controller_input {
B32 IsConnected;
B32 IsAnalog;
struct game_controller_input {
int IsConnected;
int IsAnalog;
F32 StickAverageX;
F32 StickAverageY;
float StickAverageX;
float StickAverageY;
union {
game_button_state Buttons[12];
struct game_button_state Buttons[12];
struct {
game_button_state MoveUp;
game_button_state MoveDown;
game_button_state MoveLeft;
game_button_state MoveRight;
struct game_button_state MoveUp;
struct game_button_state MoveDown;
struct game_button_state MoveLeft;
struct game_button_state MoveRight;
game_button_state ActionUp;
game_button_state ActionDown;
game_button_state ActionLeft;
game_button_state ActionRight;
struct game_button_state ActionUp;
struct game_button_state ActionDown;
struct game_button_state ActionLeft;
struct game_button_state ActionRight;
game_button_state LeftShoulder;
game_button_state RightShoulder;
struct game_button_state LeftShoulder;
struct game_button_state RightShoulder;
game_button_state Start;
game_button_state Back;
struct game_button_state Start;
struct game_button_state Back;
/* NOTE: All buttons must be added above this line. */
game_button_state Terminator;
struct game_button_state Terminator;
};
};
} game_controller_input;
};
typedef struct game_input {
game_button_state MouseButtons[5];
S32 MouseX, MouseY, MouseZ; /* NOTE: MouseZ is the wheel. */
struct game_input {
struct game_button_state MouseButtons[5];
int MouseX, MouseY, MouseZ; /* NOTE: MouseZ is the wheel. */
F32 dtForFrame;
float dtForFrame;
game_controller_input Controllers[5];
} game_input;
struct game_controller_input Controllers[5];
};
game_controller_input *GetController(game_input *Input, S32 ControllerIndex)
struct game_controller_input *GetController(struct game_input *Input,
int ControllerIndex)
{
game_controller_input *Result;
struct game_controller_input *Result;
ASSERT((U32)ControllerIndex < ARRAY_SIZE(Input->Controllers));
ASSERT((unsigned int)ControllerIndex < ARRAY_SIZE(Input->Controllers));
Result = &(Input->Controllers[ControllerIndex]);
return Result;
}
typedef struct game_memory {
B32 IsInitialized;
struct game_memory {
int IsInitialized;
U64 PermanentStorageSize;
void *PermanentStorage; /* WARNING: This memory is REQUIRED to be zero initialized!!! (Done by the OS layer.) */
unsigned long long PermanentStorageSize;
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 initialized!!! (Done by the OS layer.) */
unsigned long long TransientStorageSize;
void *TransientStorage; /* WARNING: This memory is REQUIRED to be zero
initialized!!! (Done by the OS layer.) */
#if BUILD_INTERNAL
debug_read_file_result (*DEBUGPlatformReadEntireFile)(thread_context *Thread,const char *);
B32 (*DEBUGPlatformWriteEntireFile)(thread_context *Thread, const char *, U32, void *);
void (*DEBUGPlatformFreeFileMemory)(thread_context *Thread, void *);
void (*SetProgramIcon)(game_offscreen_buffer *ProgramIcon);
struct debug_read_file_result
(*DEBUGPlatformReadEntireFile)(struct thread_context *Thread,
const char *);
int (*DEBUGPlatformWriteEntireFile)(struct thread_context *Thread,
const char *, unsigned int, void *);
void (*DEBUGPlatformFreeFileMemory)(struct thread_context *Thread,
void *);
void (*SetProgramIcon)(struct game_offscreen_buffer *ProgramIcon);
#endif
} game_memory;
};
/*typedef struct game_clocks {
U64 Ticks;
F32 SecondsElapsed;
} game_clocks;*/
/*struct game_clocks {
unsigned long long Ticks;
float SecondsElapsed;
};*/
#if OS_WINDOWS
__declspec(dllexport)
#endif
void UpdateAndRender(thread_context *Thread, game_memory *Memory, game_input *Input, game_offscreen_buffer *Buffer, game_sound_output_buffer *SoundBuffer);
void UpdateAndRender(struct thread_context *Thread,
struct game_memory *Memory,
struct game_input *Input,
struct game_offscreen_buffer *Buffer,
struct game_sound_output_buffer *SoundBuffer);
/*
*
*/
typedef struct memory_arena {
U64 Size;
U8 *Base;
U64 Used;
} memory_arena;
struct memory_arena {
unsigned long long Size;
unsigned char *Base;
unsigned long long Used;
};
static void InitializeArena(memory_arena *Arena, U64 Size, U8 *Base)
static void InitializeArena(struct memory_arena *Arena,
unsigned long long Size, unsigned char *Base)
{
Arena->Size = Size;
Arena->Base = Base;
@@ -145,8 +162,9 @@ static void InitializeArena(memory_arena *Arena, U64 Size, U8 *Base)
}
#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)
#define PUSH_ARRAY(Arena, Count, type) \
(type *)PushSize_(Arena, (Count)*sizeof(type))
static void *PushSize_(struct memory_arena *Arena, unsigned long long Size)
{
void *Result;
@@ -157,41 +175,54 @@ static void *PushSize_(memory_arena *Arena, U64 Size)
return Result;
}
typedef struct world {
tile_map *TileMap;
} world;
struct world {
struct tile_map *TileMap;
};
typedef struct loaded_bitmap {
S32 Width;
S32 Height;
U32 *Pixels;
} loaded_bitmap;
struct loaded_bitmap {
int Width;
int Height;
unsigned int *Pixels;
};
typedef struct hero_bitmaps {
S32 AlignX;
S32 AlignY;
struct hero_bitmaps {
int AlignX;
int AlignY;
loaded_bitmap Head;
loaded_bitmap Cape;
loaded_bitmap Torso;
} hero_bitmaps;
struct loaded_bitmap Head;
struct loaded_bitmap Cape;
struct loaded_bitmap Torso;
};
typedef struct game_state {
game_offscreen_buffer ProgramIcon;
loaded_bitmap ProgramIconBMP;
struct entity {
int Exists;
struct tile_map_position P;
struct v2 dP; // velocity
unsigned int FacingDirection;
float Height, Width;
};
memory_arena WorldArena;
world *World;
struct game_state {
struct game_offscreen_buffer ProgramIcon;
struct loaded_bitmap ProgramIconBMP;
tile_map_position CameraP;
tile_map_position PlayerP;
v2 dPlayerP; // velocity
struct memory_arena WorldArena;
struct world *World;
unsigned int CameraFollowingEntityIndex;
struct tile_map_position CameraP;
loaded_bitmap Backdrop;
/*unsigned int
*PlayerIndexForController[ARRAY_SIZE(((game_input *)0)->Controllers];*/
unsigned int PlayerIndexForController[5];
unsigned int EntityCount;
struct entity Entities[256];
U32 FacingDirection;
hero_bitmaps HeroBitmaps[4];
} game_state;
struct loaded_bitmap Backdrop;
struct hero_bitmaps HeroBitmaps[4];
struct v2 rect_pos;
};
#endif
+31 -23
View File
@@ -7,63 +7,71 @@
#include <math.h>
#include <string.h> // memset
S32 RoundF32toS32(F32 Real32)
float AbsoluteValue(float Real32)
{
S32 Result = (S32)roundf(Real32);
float Result = fabs(Real32);
return Result;
}
S32 FloorF32toS32(F32 Real32)
int RoundFloatToInt(float Real32)
{
S32 Result = (S32)floorf(Real32);
int Result = (int)roundf(Real32);
return Result;
}
F32 Sin(F32 Angle)
int FloorFloatToInt(float Real32)
{
F32 Result = sinf(Angle);
int Result = (int)floorf(Real32);
return Result;
}
F32 Cos(F32 Angle)
float Sin(float Angle)
{
F32 Result = cosf(Angle);
float Result = sinf(Angle);
return Result;
}
F32 ATan2(F32 Y, F32 X)
float Cos(float Angle)
{
F32 Result = atan2f(Y, X);
float Result = cosf(Angle);
return Result;
}
typedef struct bit_scan_result {
B32 Found;
U32 Index;
} bit_scan_result;
static bit_scan_result FindLeastSignificantSetBit(U32 Value)
float ATan2(float Y, float X)
{
bit_scan_result Result;
float Result = atan2f(Y, X);
return Result;
}
struct bit_scan_result {
int Found;
unsigned int Index;
};
static struct bit_scan_result FindLeastSignificantSetBit(unsigned int Value)
{
struct bit_scan_result Result;
memset(&Result, 0, sizeof(Result));
#if COMPILER_MSVC
unsigned long Index;
Result.Found = _BitScanForward(&Index, Value);
Result.Index = (U32)Index;
Result.Index = (unsigned int)Index;
#elif COMPILER_CLANG
Result.Index = __builtin_ffsll(Value); // NOTE: It is a GCC function, but is supported by clang compiler.
// https://gcc.gnu.org/onlinedocs/gcc/Bit-Operation-Builtins.html
Result.Index = __builtin_ffsll(Value); /* NOTE: It is a GCC function,
but is supported by clang
compiler.
https://gcc.gnu.org/onlinedocs/gcc/Bit-Operation-Builtins.html*/
if(Result.Index) {
Result.Found = TRUE;
Result.Found = 1;
if(Result.Index > 0)
Result.Index--;
}
#else
for(U32 Test = 0; Test < 32; Test++) {
for(unsigned int Test = 0; Test < 32; Test++) {
if(Value & (1 << Test)) {
Result.Index = Test;
Result.Found = TRUE;
Result.Found = 1;
break;
}
}
+20 -20
View File
@@ -2,14 +2,14 @@
#define HANDMADE_MATH_H
// TODO: Make these static.
typedef struct v2 {
F32 X, Y;
F32 E[2];
} v2;
struct v2 {
float X, Y;
float E[2];
};
v2 V2(F32 X, F32 Y)
struct v2 V2(float X, float Y)
{
v2 Result;
struct v2 Result;
Result.X = X;
Result.Y = Y;
@@ -17,9 +17,9 @@ v2 V2(F32 X, F32 Y)
return Result;
}
v2 V2Multiply(F32 A, v2 B)
struct v2 V2Multiply(float A, struct v2 B)
{
v2 Result;
struct v2 Result;
Result.X = A * B.X;
Result.Y = A * B.Y;
@@ -27,9 +27,9 @@ v2 V2Multiply(F32 A, v2 B)
return Result;
}
v2 V2Unary(v2 A)
struct v2 V2Unary(struct v2 A)
{
v2 Result;
struct v2 Result;
Result.X = -A.X;
Result.Y = -A.Y;
@@ -37,9 +37,9 @@ v2 V2Unary(v2 A)
return Result;
}
v2 V2Add(v2 A, v2 B)
struct v2 V2Add(struct v2 A, struct v2 B)
{
v2 Result;
struct v2 Result;
Result.X = A.X + B.X;
Result.Y = A.Y + B.Y;
@@ -47,9 +47,9 @@ v2 V2Add(v2 A, v2 B)
return Result;
}
v2 V2Subtract(v2 A, v2 B)
struct v2 V2Subtract(struct v2 A, struct v2 B)
{
v2 Result;
struct v2 Result;
Result.X = A.X - B.X;
Result.Y = A.Y - B.Y;
@@ -57,24 +57,24 @@ v2 V2Subtract(v2 A, v2 B)
return Result;
}
F32 Square(F32 A)
float Square(float A)
{
F32 Result;
float Result;
Result = A * A;
return Result;
}
F32 InnerProduct(v2 A, v2 B)
float InnerProduct(struct v2 A, struct v2 B)
{
F32 Result = A.X*B.X + A.Y*B.Y;
float Result = A.X*B.X + A.Y*B.Y;
return Result;
}
F32 LengthSq(v2 A)
float LengthSq(struct v2 A)
{
F32 Result = InnerProduct(A, A);
float Result = InnerProduct(A, A);
return Result;
}
+1 -1
View File
@@ -1,7 +1,7 @@
#ifndef HANDMADE_RANDOM_H_SENTRY
#define HANDMADE_RANDOM_H_SENTRY
static U32 RandomNumberTable[] = {
static unsigned int RandomNumberTable[] = {
0x215cd69f, 0x33960616, 0x200b7d2b, 0x1f908c2d, 0x264590df, 0x0a3cfc19, 0x2fd85a9a, 0x19bd3968,
0x1a36d278, 0x3552bed4, 0x1cf24f52, 0x388993cc, 0x028fb3ea, 0x2c777ded, 0x39ca8471, 0x1badf924,
0x0f95267e, 0x2a4ff029, 0x2c1b26ea, 0x0212c345, 0x304d7fe0, 0x3b4e6efe, 0x11124fce, 0x2352fc4c,
+102 -45
View File
@@ -1,22 +1,31 @@
#include "handmade_tile.h"
static tile_chunk *GetTileChunk(tile_map *TileMap, U32 TileChunkX, U32 TileChunkY, U32 TileChunkZ)
static struct tile_chunk *
GetTileChunk(struct tile_map *TileMap,
unsigned int TileChunkX, unsigned int TileChunkY,
unsigned int TileChunkZ)
{
tile_chunk *TileChunk = NULL;
struct tile_chunk *TileChunk = NULL;
if((TileChunkX >= 0) && (TileChunkX < TileMap->TileChunkCountX) &&
(TileChunkY >= 0) && (TileChunkY < TileMap->TileChunkCountY) &&
(TileChunkZ >= 0) && (TileChunkZ < TileMap->TileChunkCountZ))
{
TileChunk = &TileMap->TileChunks[(TileChunkZ*TileMap->TileChunkCountY*TileMap->TileChunkCountX) + (TileChunkY*TileMap->TileChunkCountX) + TileChunkX];
TileChunk =
&TileMap->TileChunks[(TileChunkZ * TileMap->TileChunkCountY *
TileMap->TileChunkCountX) +
(TileChunkY*TileMap->TileChunkCountX) + TileChunkX];
}
return TileChunk;
}
static tile_chunk_position GetChunkPositionFor(tile_map *TileMap, U32 AbsTileX, U32 AbsTileY, U32 AbsTileZ)
static struct tile_chunk_position
GetChunkPositionFor(struct tile_map *TileMap,
unsigned int AbsTileX, unsigned int AbsTileY,
unsigned int AbsTileZ)
{
tile_chunk_position Result;
struct tile_chunk_position Result;
Result.TileChunkX = AbsTileX >> TileMap->ChunkShift;
Result.TileChunkY = AbsTileY >> TileMap->ChunkShift;
@@ -27,48 +36,68 @@ static tile_chunk_position GetChunkPositionFor(tile_map *TileMap, U32 AbsTileX,
return Result;
}
static U32 GetTileValueUnchecked(tile_map *TileMap, tile_chunk *TileChunk, U32 TileX, U32 TileY)
static unsigned int
GetTileValueUnchecked(struct tile_map *TileMap,
struct tile_chunk *TileChunk,
unsigned int TileX, unsigned int TileY)
{
ASSERT(TileChunk);
ASSERT(TileX < TileMap->ChunkDim);
ASSERT(TileY < TileMap->ChunkDim);
U32 TileChunkValue = TileChunk->Tiles[TileY * TileMap->ChunkDim + TileX];
unsigned int TileChunkValue =
TileChunk->Tiles[TileY * TileMap->ChunkDim + TileX];
return TileChunkValue;
}
static U32 GetTileChunkValue(tile_map *TileMap, tile_chunk *TileChunk, U32 TestTileX, U32 TestTileY)
static unsigned int
GetTileChunkValue(struct tile_map *TileMap, struct tile_chunk *TileChunk,
unsigned int TestTileX, unsigned int TestTileY)
{
U32 TileChunkValue = 0;
unsigned int TileChunkValue = 0;
if(TileChunk && TileChunk->Tiles) {
TileChunkValue = GetTileValueUnchecked(TileMap, TileChunk, TestTileX, TestTileY);
TileChunkValue =
GetTileValueUnchecked(TileMap, TileChunk, TestTileX, TestTileY);
}
return TileChunkValue;
}
static U32 GetTileValue(tile_map *TileMap, U32 AbsTileX, U32 AbsTileY, U32 AbsTileZ)
static unsigned int
GetTileValue(struct tile_map *TileMap,
unsigned int AbsTileX, unsigned int AbsTileY,
unsigned int AbsTileZ)
{
tile_chunk_position ChunkPos = GetChunkPositionFor(TileMap, AbsTileX, AbsTileY, AbsTileZ);
tile_chunk *TileChunk = GetTileChunk(TileMap, ChunkPos.TileChunkX, ChunkPos.TileChunkY, ChunkPos.TileChunkZ);
U32 TileChunkValue = GetTileChunkValue(TileMap, TileChunk, ChunkPos.RelTileX, ChunkPos.RelTileY);
struct tile_chunk_position ChunkPos =
GetChunkPositionFor(TileMap, AbsTileX, AbsTileY, AbsTileZ);
struct tile_chunk *TileChunk =
GetTileChunk(TileMap, ChunkPos.TileChunkX, ChunkPos.TileChunkY,
ChunkPos.TileChunkZ);
unsigned int TileChunkValue =
GetTileChunkValue(TileMap, TileChunk, ChunkPos.RelTileX,
ChunkPos.RelTileY);
return TileChunkValue;
}
static B32 IsTileValueEmpty(U32 TileValue)
static int IsTileValueEmpty(unsigned int TileValue)
{
B32 Empty = ((TileValue == 1) || (TileValue == 3) || (TileValue == 4));
int Empty = ((TileValue == 1) || (TileValue == 3) || (TileValue == 4));
return Empty;
}
static U32 GetTileValueByPos(tile_map *TileMap, tile_map_position Pos)
static unsigned int GetTileValueByPos(struct tile_map *TileMap,
struct tile_map_position Pos)
{
U32 TileChunkValue = GetTileValue(TileMap, Pos.AbsTileX, Pos.AbsTileY, Pos.AbsTileZ);
unsigned int TileChunkValue =
GetTileValue(TileMap, Pos.AbsTileX, Pos.AbsTileY, Pos.AbsTileZ);
return TileChunkValue;
}
static void SetTileValueUnchecked(tile_map *TileMap, tile_chunk *TileChunk, U32 TileX, U32 TileY, U32 TileValue)
static void SetTileValueUnchecked(struct tile_map *TileMap,
struct tile_chunk *TileChunk,
unsigned int TileX, unsigned int TileY,
unsigned int TileValue)
{
ASSERT(TileChunk);
ASSERT(TileX < TileMap->ChunkDim);
@@ -77,58 +106,76 @@ static void SetTileValueUnchecked(tile_map *TileMap, tile_chunk *TileChunk, U32
TileChunk->Tiles[TileY * TileMap->ChunkDim + TileX] = TileValue;
}
static void SetTileChunkValue(tile_map *TileMap, tile_chunk *TileChunk, U32 TestTileX, U32 TestTileY, U32 TileValue)
static void SetTileChunkValue(struct tile_map *TileMap,
struct tile_chunk *TileChunk,
unsigned int TestTileX, unsigned int TestTileY,
unsigned int TileValue)
{
if(TileChunk && TileChunk->Tiles) {
SetTileValueUnchecked(TileMap, TileChunk, TestTileX, TestTileY, TileValue);
SetTileValueUnchecked(TileMap, TileChunk, TestTileX, TestTileY,
TileValue);
}
}
static B32 IsTileMapPointEmpty(tile_map *TileMap, tile_map_position Pos)
static int IsTileMapPointEmpty(struct tile_map *TileMap,
struct tile_map_position Pos)
{
U32 TileChunkValue = GetTileValueByPos(TileMap, Pos);
B32 Empty = IsTileValueEmpty(TileChunkValue);
unsigned int TileChunkValue = GetTileValueByPos(TileMap, Pos);
int Empty = IsTileValueEmpty(TileChunkValue);
return Empty;
}
static void SetTileValue(memory_arena *Arena, tile_map *TileMap, U32 AbsTileX, U32 AbsTileY, U32 AbsTileZ, U32 TileValue)
static void SetTileValue(struct memory_arena *Arena,
struct tile_map *TileMap,
unsigned int AbsTileX, unsigned int AbsTileY,
unsigned int AbsTileZ, unsigned int TileValue)
{
tile_chunk_position ChunkPos = GetChunkPositionFor(TileMap, AbsTileX, AbsTileY, AbsTileZ);
tile_chunk *TileChunk = GetTileChunk(TileMap, ChunkPos.TileChunkX, ChunkPos.TileChunkY, ChunkPos.TileChunkZ);
struct tile_chunk_position ChunkPos =
GetChunkPositionFor(TileMap, AbsTileX, AbsTileY, AbsTileZ);
struct tile_chunk *TileChunk =
GetTileChunk(TileMap, ChunkPos.TileChunkX, ChunkPos.TileChunkY,
ChunkPos.TileChunkZ);
ASSERT(TileChunk);
if(!TileChunk->Tiles) {
U32 TileCount = TileMap->ChunkDim*TileMap->ChunkDim;
unsigned int TileCount = TileMap->ChunkDim*TileMap->ChunkDim;
TileChunk->Tiles = PUSH_ARRAY(Arena, TileCount, U32);
for(U32 TileIndex = 0; TileIndex < TileCount; TileIndex++) {
TileChunk->Tiles = PUSH_ARRAY(Arena, TileCount, unsigned int);
for(unsigned int TileIndex = 0;
TileIndex < TileCount;
TileIndex++)
{
TileChunk->Tiles[TileIndex] = 1;
}
}
SetTileChunkValue(TileMap, TileChunk, ChunkPos.RelTileX, ChunkPos.RelTileY, TileValue);
SetTileChunkValue(TileMap, TileChunk, ChunkPos.RelTileX,
ChunkPos.RelTileY, TileValue);
}
//
// TODO: Do these belong more in a "positioning" or "geometry" file?
//
static void CannonicalizeCoord(tile_map *TileMap, U32 *Tile, F32 *TileRel)
static void CannonicalizeCoord(struct tile_map *TileMap, unsigned int *Tile,
float *TileRel)
{
// NOTE: The world is assumed to be toroidal topology. If you step off
// one end, you come back on the other.
S32 Offset = RoundF32toS32(*TileRel / TileMap->TileSideInMeters);
int Offset = RoundFloatToInt(*TileRel / TileMap->TileSideInMeters);
*Tile += Offset;
*TileRel -= (F32)Offset * TileMap->TileSideInMeters;
*TileRel -= (float)Offset * TileMap->TileSideInMeters;
ASSERT(*TileRel >= -0.5f*TileMap->TileSideInMeters);
ASSERT(*TileRel <= 0.5f*TileMap->TileSideInMeters);
}
static tile_map_position RecannonicalizePosition(tile_map *TileMap, tile_map_position Pos)
static struct tile_map_position
RecannonicalizePosition(struct tile_map *TileMap,
struct tile_map_position Pos)
{
tile_map_position Result = Pos;
struct tile_map_position Result = Pos;
CannonicalizeCoord(TileMap, &Result.AbsTileX, &Result.Offset.X);
CannonicalizeCoord(TileMap, &Result.AbsTileY, &Result.Offset.Y);
@@ -136,20 +183,28 @@ static tile_map_position RecannonicalizePosition(tile_map *TileMap, tile_map_pos
return Result;
}
static B32 AreOnSameTile(tile_map_position *A, tile_map_position *B)
static int AreOnSameTile(struct tile_map_position *A,
struct tile_map_position *B)
{
B32 Result = ((A->AbsTileX == B->AbsTileX) && (A->AbsTileY == B->AbsTileY) && (A->AbsTileZ == B->AbsTileZ));
int Result = ((A->AbsTileX == B->AbsTileX) &&
(A->AbsTileY == B->AbsTileY) &&
(A->AbsTileZ == B->AbsTileZ));
return Result;
}
static tile_map_difference SubtractInF32(tile_map *TileMap, tile_map_position *A, tile_map_position *B)
static struct tile_map_difference
SubtractInFloat(struct tile_map *TileMap,
struct tile_map_position *A, struct tile_map_position *B)
{
tile_map_difference Result;
struct tile_map_difference Result;
v2 dTileXY = { ((F32)A->AbsTileX - (F32)B->AbsTileX), ((F32)A->AbsTileY - (F32)B->AbsTileY) };
F32 dTileZ = (F32)A->AbsTileZ - (F32)B->AbsTileZ;
struct v2 dTileXY = { ((float)A->AbsTileX - (float)B->AbsTileX),
((float)A->AbsTileY - (float)B->AbsTileY) };
float dTileZ = (float)A->AbsTileZ - (float)B->AbsTileZ;
Result.dXY = V2Add(V2Multiply(TileMap->TileSideInMeters, dTileXY), V2Subtract(A->Offset, B->Offset));
Result.dXY = V2Add(V2Multiply(TileMap->TileSideInMeters,
dTileXY),
V2Subtract(A->Offset, B->Offset));
// TODO: Incomplete . . .
Result.dZ = TileMap->TileSideInMeters*dTileZ;
@@ -157,9 +212,11 @@ static tile_map_difference SubtractInF32(tile_map *TileMap, tile_map_position *A
return Result;
}
static tile_map_position CenteredTilePoint(U32 AbsTileX, U32 AbsTileY, U32 AbsTileZ)
static struct tile_map_position
CenteredTilePoint(unsigned int AbsTileX, unsigned int AbsTileY,
unsigned int AbsTileZ)
{
tile_map_position Result;
struct tile_map_position Result;
memset(&Result, 0, sizeof(Result));
Result.AbsTileX = AbsTileX;
+30 -30
View File
@@ -3,46 +3,46 @@
#include "handmade_math.h"
typedef struct tile_map_difference {
v2 dXY;
F32 dZ;
} tile_map_difference;
struct tile_map_difference {
struct v2 dXY;
float dZ;
};
typedef struct tile_map_position {
U32 AbsTileX;
U32 AbsTileY;
U32 AbsTileZ;
struct tile_map_position {
unsigned int AbsTileX;
unsigned int AbsTileY;
unsigned int AbsTileZ;
/* NOTE: X and Y relative to tile center. */
v2 Offset;
} tile_map_position;
struct v2 Offset;
};
typedef struct tile_chunk_position {
U32 TileChunkX;
U32 TileChunkY;
U32 TileChunkZ;
struct tile_chunk_position {
unsigned int TileChunkX;
unsigned int TileChunkY;
unsigned int TileChunkZ;
U32 RelTileX;
U32 RelTileY;
} tile_chunk_position;
unsigned int RelTileX;
unsigned int RelTileY;
};
typedef struct tile_chunk {
U32 *Tiles;
} tile_chunk;
struct tile_chunk {
unsigned int *Tiles;
};
typedef struct tile_map {
U32 ChunkShift;
U32 ChunkMask;
U32 ChunkDim;
struct tile_map {
unsigned int ChunkShift;
unsigned int ChunkMask;
unsigned int ChunkDim;
F32 TileSideInMeters;
float TileSideInMeters;
/* TODO: Real sparseness. */
U32 TileChunkCountX;
U32 TileChunkCountY;
U32 TileChunkCountZ;
unsigned int TileChunkCountX;
unsigned int TileChunkCountY;
unsigned int TileChunkCountZ;
tile_chunk *TileChunks;
} tile_map;
struct tile_chunk *TileChunks;
};
#endif
+513 -285
View File
File diff suppressed because it is too large Load Diff
+28 -28
View File
@@ -6,50 +6,50 @@
#include "core.h"
#include "handmade.h"
typedef struct sdl_border_size {
S32 Top;
S32 Bottom;
S32 Left;
S32 Right;
} sdl_border_size;
struct sdl_border_size {
int Top;
int Bottom;
int Left;
int Right;
};
typedef struct sdl_window_size {
S32 Width;
S32 Height;
} sdl_window_size;
struct sdl_window_size {
int Width;
int Height;
};
typedef struct sdl_window_position {
S32 X;
S32 Y;
} sdl_window_position;
struct sdl_window_position {
int X;
int Y;
};
typedef struct offscreen_buffer {
struct offscreen_buffer {
/* Pixels are always 32-bit and have the bytes in BGRX (little-endian). */
SDL_Texture *Texture;
void *Memory;
S32 Width;
S32 Height;
S32 Pitch;
} offscreen_buffer;
int Width;
int Height;
int Pitch;
};
typedef struct sdl_sound_output {
S32 SamplesPerSecond;
S32 BytesPerSample;
S32 SecondaryBufferSize;
S32 LatencySampleCount;
struct sdl_sound_output {
int SamplesPerSecond;
int BytesPerSample;
int SecondaryBufferSize;
int LatencySampleCount;
void *Samples;
} sdl_sound_output;
};
/* TODO: We choose 1024 only because MacOS's MAXPATHLEN says such a value.
* For example, linux/limits.h PATH_MAX says 4096. Windows has MAX_PATH. */
#define SDL_STATE_PATH_SIZE 1024
typedef struct sdl_state {
U64 TotalSize;
struct sdl_state {
unsigned long long TotalSize;
void *GameMmemoryBlock;
char EXEDirPath[SDL_STATE_PATH_SIZE];
char DLLPath[SDL_STATE_PATH_SIZE];
} sdl_state;
};
#endif