Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #if !defined(HANDMADE_H)
- #include <math.h>
- #include <stdint.h>
- #define internal static
- #define local_persist static
- #define global_variable static
- // If the expression isn't true, write to the NULL pointer and crash the code. This should raise an exception.
- // Dev: Could also explore DebugBreak() for platform independence.
- #if HANDMADE_SLOW
- #define Assert(expression) if (!(expression)) {*(int *)0 = 0;}
- #else
- #define Assert(expression)
- #endif
- #define elementsInArray(Array) (sizeof(Array) / sizeof((Array)[0]))
- #define kilobytes(value) (value * 1024LL)
- #define megabytes(value) (kilobytes(value) * 1024LL)
- #define gigabytes(value) (megabytes(value) * 1024LL)
- #define terabytes(value) (gigabytes(value) * 1024LL)
- /** Calls that the platform layer provides to the game. **/
- #if HANDMADE_INTERNAL
- struct debug_read_file_result
- {
- uint32_t contentSize;
- void* Contents;
- };
- // Instead of defining these functions, we define what types they and assume someone passes pointers of them to the game.
- #define DEBUG_PLATFORM_FREE_FILE_MEMORY(name) void name(void *Memory)
- typedef DEBUG_PLATFORM_FREE_FILE_MEMORY(debug_platform_free_file_memory);
- #define DEBUG_PLATFORM_READ_ENTIRE_FILE(name) debug_read_file_result name(char *Filename)
- typedef DEBUG_PLATFORM_READ_ENTIRE_FILE(debug_platform_read_entire_file);
- #define DEBUG_PLATFORM_WRITE_ENTIRE_FILE(name) bool name(char *Filename, uint32_t memorySize, void *Memory)
- typedef DEBUG_PLATFORM_WRITE_ENTIRE_FILE(debug_platform_write_entire_file);
- #endif
- inline uint32_t SafeTruncateUInt64(uint64_t value)
- {
- // Assume we never read a file larger than the maximum 32-bit value (~4 GB).
- // Todo: Create defines for maximum values for uint32_max.
- Assert(value <= 0xFFFFFFFF);
- uint32_t result = (uint32_t)value;
- return result;
- }
- // A struct that returns the buffer we draw to. Pixels are always 32-bits wide, 0xBBGGRRPP.
- struct game_offscreen_buffer
- {
- void *Memory;
- int width;
- int height;
- int pitch;
- };
- struct game_sound_output_buffer
- {
- int samplesPerSecond;
- int sampleCount;
- int16_t *Samples;
- };
- struct game_button_state
- {
- int halfTransitionCount;
- bool endedDown;
- };
- struct game_controller_input
- {
- bool isConnected;
- bool isAnalog;
- float stickAverageX;
- float 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;
- // All buttons must be added above this line.
- game_button_state terminator;
- };
- };
- };
- struct game_input
- {
- game_controller_input Controllers[5];
- };
- // Provides a bounds check, to make sure that non-existant controllers are never referenced.
- inline game_controller_input *GetController(game_input *Input, int controllerIndex)
- {
- Assert(controllerIndex < elementsInArray(Input->Controllers));
- game_controller_input *result = &Input->Controllers[controllerIndex];
- return result;
- }
- // Defines our memory partitions. Game is responsible for sub-partitioning the memory and making sure it operates in a fixed space.
- // VirtualAlloc() default clears our storage spaces at initialization.
- struct game_memory
- {
- bool isIntialized;
- uint64_t permanantStorageSize;
- void *permanentStorage;
- uint64_t transientStorageSize;
- void *transientStorage;
- debug_platform_read_entire_file *DEBUGPlatformReadEntireFile;
- debug_platform_free_file_memory *DEBUGPlatformFreeFileMemory;
- debug_platform_write_entire_file *DEBUGPlatformWriteEntireFile;
- };
- struct game_clock
- {
- // Todo: Insert clock timing here.
- float secondsElapsed;
- };
- // Variables that track the game state.
- struct game_state
- {
- int toneHz;
- int greenOffset;
- int blueOffset;
- };
- // Needs to eventually take in 3 things - what time we're at in the game, the controller input, and the bitmap buffer to use.
- #define GAME_UPDATE_AND_RENDER(name) void name(game_memory *Memory, game_input *Input, game_offscreen_buffer *Buffer)
- typedef GAME_UPDATE_AND_RENDER(game_update_and_render);
- GAME_UPDATE_AND_RENDER(GameUpdateAndRenderStub)
- {
- }
- // Note: Has to be a very fast function - cannot be more than 1-2ms to keep sound latency down. Could be useful to measure performance.
- #define GAME_GET_SOUND_SAMPLES(name) void name(game_memory *Memory, game_sound_output_buffer *SoundBuffer)
- typedef GAME_GET_SOUND_SAMPLES(game_get_sound_samples);
- GAME_GET_SOUND_SAMPLES(GameGetSoundSamplesStub)
- {
- }
- #define HANDMADE_H
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement