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