common.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #ifndef COMMON_H_
  2. #define COMMON_H_
  3. #include <assert.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <errno.h>
  7. #include <SDL.h>
  8. #define GL_GLEXT_PROTOTYPES
  9. #include <SDL_opengl.h>
  10. #define SCREEN_WIDTH 800
  11. #define SCREEN_HEIGHT 600
  12. #define SCREEN_FPS 60
  13. #define DELTA_TIME_SEC (1.0f / SCREEN_FPS)
  14. #define DELTA_TIME_MS ((Uint32) floorf(DELTA_TIME_SEC * 1000.0f))
  15. #define MARKER_SIZE 15.0f
  16. #define BACKGROUND_COLOR 0x353535FF
  17. #define RED_COLOR 0xDA2C38FF
  18. #define GREEN_COLOR 0x87C38FFF
  19. #define BLUE_COLOR 0x748CABFF
  20. #define HEX_COLOR(hex) \
  21. ((hex) >> (3 * 8)) & 0xFF, \
  22. ((hex) >> (2 * 8)) & 0xFF, \
  23. ((hex) >> (1 * 8)) & 0xFF, \
  24. ((hex) >> (0 * 8)) & 0xFF
  25. int check_sdl_code(int code)
  26. {
  27. if (code < 0) {
  28. fprintf(stderr, "SDL error: %s\n", SDL_GetError());
  29. exit(1);
  30. }
  31. return code;
  32. }
  33. void *check_sdl_ptr(void *ptr)
  34. {
  35. if (ptr == NULL) {
  36. fprintf(stderr, "SDL error: %s\n", SDL_GetError());
  37. exit(1);
  38. }
  39. return ptr;
  40. }
  41. typedef struct {
  42. float x;
  43. float y;
  44. } Vec2;
  45. Vec2 vec2(float x, float y)
  46. {
  47. return (Vec2){x, y};
  48. }
  49. Vec2 vec2_sub(Vec2 a, Vec2 b)
  50. {
  51. return vec2(a.x - b.x, a.y - b.y);
  52. }
  53. Vec2 vec2_scale(Vec2 a, float s)
  54. {
  55. return vec2(a.x * s, a.y * s);
  56. }
  57. Vec2 vec2_add(Vec2 a, Vec2 b)
  58. {
  59. return vec2(a.x + b.x, a.y + b.y);
  60. }
  61. float vec2_length(Vec2 a)
  62. {
  63. return sqrtf(a.x * a.x + a.y * a.y);
  64. }
  65. float lerpf(float a, float b, float p)
  66. {
  67. return a + (b - a) * p;
  68. }
  69. Vec2 lerpv2(Vec2 a, Vec2 b, float p)
  70. {
  71. return vec2_add(a, vec2_scale(vec2_sub(b, a), p));
  72. }
  73. char *cstr_slurp_file(const char *file_path)
  74. {
  75. FILE *f = fopen(file_path, "r");
  76. if (f == NULL) {
  77. fprintf(stderr, "ERROR: Could not read file `%s`: %s\n",
  78. file_path, strerror(errno));
  79. exit(1);
  80. }
  81. if (fseek(f, 0, SEEK_END) < 0) {
  82. fprintf(stderr, "ERROR: Could not read file `%s`: %s\n",
  83. file_path, strerror(errno));
  84. exit(1);
  85. }
  86. long m = ftell(f);
  87. if (m < 0) {
  88. fprintf(stderr, "ERROR: Could not read file `%s`: %s\n",
  89. file_path, strerror(errno));
  90. exit(1);
  91. }
  92. char *buffer = malloc(m + 1);
  93. if (buffer == NULL) {
  94. fprintf(stderr, "ERROR: Could not allocate memory for file: %s\n",
  95. strerror(errno));
  96. exit(1);
  97. }
  98. if (fseek(f, 0, SEEK_SET) < 0) {
  99. fprintf(stderr, "ERROR: Could not read file `%s`: %s\n",
  100. file_path, strerror(errno));
  101. exit(1);
  102. }
  103. size_t n = fread(buffer, 1, m, f);
  104. if (ferror(f)) {
  105. fprintf(stderr, "ERROR: Could not read file `%s`: %s\n",
  106. file_path, strerror(errno));
  107. exit(1);
  108. }
  109. buffer[n] = '\0';
  110. fclose(f);
  111. return buffer;
  112. }
  113. #endif // COMMON_H_