Guest User

handmade.h

a guest
Oct 14th, 2018
87
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #if !defined(HANDMADE_H)
  2.  
  3. #include <math.h>
  4. #include <stdint.h>
  5.  
  6. #define internal static
  7. #define local_persist static
  8. #define global_variable static
  9.  
  10. // If the expression isn't true, write to the NULL pointer and crash the code. This should raise an exception.
  11. // Dev: Could also explore DebugBreak() for platform independence.
  12. #if HANDMADE_SLOW
  13. #define Assert(expression) if (!(expression)) {*(int *)0 = 0;}
  14. #else
  15. #define Assert(expression)
  16. #endif
  17.  
  18. #define elementsInArray(Array) (sizeof(Array) / sizeof((Array)[0]))
  19. #define kilobytes(value) (value * 1024LL)
  20. #define megabytes(value) (kilobytes(value) * 1024LL)
  21. #define gigabytes(value) (megabytes(value) * 1024LL)
  22. #define terabytes(value) (gigabytes(value) * 1024LL)
  23.  
  24. /** Calls that the platform layer provides to the game. **/
  25. #if HANDMADE_INTERNAL
  26. struct debug_read_file_result
  27. {
  28.     uint32_t contentSize;
  29.     void* Contents;
  30. };
  31.  
  32. // Instead of defining these functions, we define what types they and assume someone passes pointers of them to the game.
  33. #define DEBUG_PLATFORM_FREE_FILE_MEMORY(name) void name(void *Memory)
  34. typedef DEBUG_PLATFORM_FREE_FILE_MEMORY(debug_platform_free_file_memory);
  35.  
  36. #define DEBUG_PLATFORM_READ_ENTIRE_FILE(name) debug_read_file_result name(char *Filename)
  37. typedef DEBUG_PLATFORM_READ_ENTIRE_FILE(debug_platform_read_entire_file);
  38.  
  39. #define DEBUG_PLATFORM_WRITE_ENTIRE_FILE(name) bool name(char *Filename, uint32_t memorySize, void *Memory)
  40. typedef DEBUG_PLATFORM_WRITE_ENTIRE_FILE(debug_platform_write_entire_file);
  41.  
  42. #endif
  43.  
  44. inline uint32_t SafeTruncateUInt64(uint64_t value)
  45. {
  46.     // Assume we never read a file larger than the maximum 32-bit value (~4 GB).
  47.     // Todo: Create defines for maximum values for uint32_max.
  48.     Assert(value <= 0xFFFFFFFF);
  49.     uint32_t result = (uint32_t)value;
  50.     return result;
  51. }
  52.  
  53. // A struct that returns the buffer we draw to. Pixels are always 32-bits wide, 0xBBGGRRPP.
  54. struct game_offscreen_buffer
  55. {
  56.     void *Memory;
  57.     int width;
  58.     int height;
  59.     int pitch;
  60. };
  61.  
  62. struct game_sound_output_buffer
  63. {
  64.     int samplesPerSecond;
  65.     int sampleCount;
  66.     int16_t *Samples;
  67. };
  68.  
  69. struct game_button_state
  70. {
  71.     int halfTransitionCount;
  72.     bool endedDown;
  73. };
  74.  
  75. struct game_controller_input
  76. {
  77.     bool isConnected;
  78.     bool isAnalog;
  79.     float stickAverageX;
  80.     float stickAverageY;
  81.  
  82.     union {
  83.         game_button_state Buttons[12];
  84.         struct {
  85.             game_button_state moveUp;
  86.             game_button_state moveDown;
  87.             game_button_state moveLeft;
  88.             game_button_state moveRight;
  89.  
  90.             game_button_state actionUp;
  91.             game_button_state actionDown;
  92.             game_button_state actionLeft;
  93.             game_button_state actionRight;
  94.  
  95.             game_button_state leftShoulder;
  96.             game_button_state rightShoulder;
  97.  
  98.             game_button_state start;
  99.             game_button_state back;
  100.            
  101.             // All buttons must be added above this line.
  102.             game_button_state terminator;
  103.         };
  104.     };
  105. };
  106.  
  107. struct game_input
  108. {
  109.     game_controller_input Controllers[5];
  110. };
  111.  
  112. // Provides a bounds check, to make sure that non-existant controllers are never referenced.
  113. inline game_controller_input *GetController(game_input *Input, int controllerIndex)
  114. {
  115.     Assert(controllerIndex < elementsInArray(Input->Controllers));
  116.     game_controller_input *result = &Input->Controllers[controllerIndex];
  117.     return result;
  118. }
  119.  
  120. // Defines our memory partitions. Game is responsible for sub-partitioning the memory and making sure it operates in a fixed space.
  121. // VirtualAlloc() default clears our storage spaces at initialization.
  122. struct game_memory
  123. {
  124.     bool isIntialized;
  125.  
  126.     uint64_t permanantStorageSize;
  127.     void *permanentStorage;
  128.  
  129.     uint64_t transientStorageSize;
  130.     void *transientStorage;
  131.  
  132.     debug_platform_read_entire_file *DEBUGPlatformReadEntireFile;
  133.     debug_platform_free_file_memory *DEBUGPlatformFreeFileMemory;
  134.     debug_platform_write_entire_file *DEBUGPlatformWriteEntireFile;
  135. };
  136.  
  137. struct game_clock
  138. {
  139.     // Todo: Insert clock timing here.
  140.     float secondsElapsed;
  141. };
  142.  
  143. // Variables that track the game state.
  144. struct game_state
  145. {
  146.     int toneHz;
  147.     int greenOffset;
  148.     int blueOffset;
  149. };
  150.  
  151. // Needs to eventually take in 3 things - what time we're at in the game, the controller input, and the bitmap buffer to use.
  152. #define GAME_UPDATE_AND_RENDER(name) void name(game_memory *Memory, game_input *Input, game_offscreen_buffer *Buffer)
  153. typedef GAME_UPDATE_AND_RENDER(game_update_and_render);
  154. GAME_UPDATE_AND_RENDER(GameUpdateAndRenderStub)
  155. {
  156. }
  157.  
  158. // 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.
  159. #define GAME_GET_SOUND_SAMPLES(name) void name(game_memory *Memory, game_sound_output_buffer *SoundBuffer)
  160. typedef GAME_GET_SOUND_SAMPLES(game_get_sound_samples);
  161. GAME_GET_SOUND_SAMPLES(GameGetSoundSamplesStub)
  162. {
  163. }
  164.  
  165. #define HANDMADE_H
  166. #endif
RAW Paste Data