Files
handmadehero/handmade.h
T
2026-04-17 21:51:15 -07:00

177 lines
4.8 KiB
C

#ifndef HANDMADE_H_SENTRY
#define HANDMADE_H_SENTRY
#include "core.h"
#include "handmade_tile.h"
/* --------------------------------------------------------------------- */
/* NOTE: Servcies that the platform layer provides to the game. */
/* --------------------------------------------------------------------- */
typedef struct thread_context {
S32 Placeholder;
} thread_context;
#if BUILD_INTERNAL
typedef struct debug_read_file_result {
U32 ContentsSize;
void *Contents;
} debug_read_file_result;
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);
#endif
/* --------------------------------------------------------------------- */
/* NOTE: Servcies that the game provides to the platform layer. */
/* --------------------------------------------------------------------- */
typedef 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;
typedef struct game_sound_output_buffer {
S32 SamplesPerSecond;
S32 SampleCount;
S16 *Samples;
} game_sound_output_buffer;
typedef struct game_button_state {
S32 HalfTransitionCount;
B32 EndedDown;
} game_button_state;
typedef struct game_controller_input {
B32 IsConnected;
B32 IsAnalog;
F32 StickAverageX;
F32 StickAverageY;
union {
game_button_state Buttons[12];
struct {
game_button_state MoveUp;
game_button_state MoveDown;
game_button_state MoveLeft;
game_button_state MoveRight;
game_button_state ActionUp;
game_button_state ActionDown;
game_button_state ActionLeft;
game_button_state ActionRight;
game_button_state LeftShoulder;
game_button_state RightShoulder;
game_button_state Start;
game_button_state Back;
/* NOTE: All buttons must be added above this line. */
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. */
F32 dtForFrame;
game_controller_input Controllers[5];
} game_input;
game_controller_input *GetController(game_input *Input, S32 ControllerIndex)
{
game_controller_input *Result;
ASSERT((U32)ControllerIndex < ARRAY_SIZE(Input->Controllers));
Result = &(Input->Controllers[ControllerIndex]);
return Result;
}
typedef struct game_memory {
B32 IsInitialized;
U64 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.) */
#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 *);
#endif
} game_memory;
/*typedef struct game_clocks {
U64 Ticks;
F32 SecondsElapsed;
} game_clocks;*/
void UpdateAndRender(thread_context *Thread, game_memory *Memory,
game_input *Input, game_offscreen_buffer *Buffer,
game_sound_output_buffer *SoundBuffer);
/*
*
*/
typedef struct memory_arena {
U64 Size;
U8 *Base;
U64 Used;
} memory_arena;
static void InitializeArena(memory_arena *Arena, U64 Size, U8 *Base)
{
Arena->Size = Size;
Arena->Base = Base;
Arena->Used = 0;
}
#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)
{
void *Result;
ASSERT((Arena->Used + Size) <= Arena->Size);
Result = (void *)(Arena->Base + Arena->Used);
Arena->Used += Size;
return Result;
}
typedef struct world {
tile_map *TileMap;
} world;
typedef struct game_state {
memory_arena WorldArena;
world *World;
tile_map_position PlayerP;
U32 *PixelPointer;
} game_state;
#endif