2D Run-and-gun shooter inspired by One Man's Doomsday, Counter-Strike, and Metal Slug.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IZ_video.c 3.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include "IZ_video.h"
  2. IZ_ProcedureResult IZ_VideoSaveConfig(IZ_VideoState* state, const char* config_path) {
  3. if (!ini_putl("Video", "Width", state->config.width, config_path)) {
  4. return -1;
  5. }
  6. if (!ini_putl("Video", "Height", state->config.height, config_path)) {
  7. return -1;
  8. }
  9. if (!ini_putl("Video", "MaxFps", state->config.max_fps, config_path)) {
  10. return -1;
  11. }
  12. return 0;
  13. }
  14. void IZ_VideoLoadConfig(IZ_VideoState* state, const char* config_path) {
  15. state->config.width = ini_getl("Video", "Width", IZ_DEFAULT_VIDEO_STATE.config.width, config_path);
  16. state->config.height = ini_getl("Video", "Height", IZ_DEFAULT_VIDEO_STATE.config.height, config_path);
  17. state->config.max_fps = ini_getl("Video", "MaxFps", IZ_DEFAULT_VIDEO_STATE.config.max_fps, config_path);
  18. }
  19. void IZ_VideoOverrideConfig(IZ_VideoState* state, u8 argc, const char* argv[]) {
  20. const char* cmdline_buffer;
  21. if ((cmdline_buffer = IZ_ConfigGetCommandlineOption(argc, argv, "-f"))) {
  22. state->config.max_fps = atoi(cmdline_buffer);
  23. }
  24. }
  25. IZ_ProcedureResult IZ_VideoInitialize(IZ_VideoState* state, const char* config_path, u8 argc, const char* argv[]) {
  26. SDL_memcpy(state, &IZ_DEFAULT_VIDEO_STATE, sizeof(IZ_VideoState));
  27. IZ_VideoLoadConfig(state, config_path);
  28. if (IZ_VideoSaveConfig(state, config_path)) {
  29. // fprintf_s(stderr, "Error committing video config.\n");
  30. }
  31. IZ_VideoOverrideConfig(state, argc, argv);
  32. state->last_update_at = 0u;
  33. SDL_Window* window = SDL_CreateWindow(
  34. IZ_APP_NAME,
  35. SDL_WINDOWPOS_CENTERED,
  36. SDL_WINDOWPOS_CENTERED,
  37. state->config.width,
  38. state->config.height,
  39. SDL_WINDOW_SHOWN
  40. );
  41. if (window == NULL) {
  42. // fprintf_s(stderr, "Window could not be created! SDL_Error: %s\n", SDL_GetError());
  43. return 1;
  44. }
  45. state->window = window;
  46. state->renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
  47. return 0;
  48. }
  49. void IZ_VideoUpdateForDebugTicks(IZ_VideoState* video_state, uint64_t ticks) {
  50. SDL_SetRenderDrawColor(video_state->renderer, 0x00, 0xff, 0xff, 0xff);
  51. u64 the_ticks = ticks;
  52. u8 column;
  53. u8 row;
  54. const u8 size = 4;
  55. u8 i;
  56. for (i = 0; i < 64; i += 1) {
  57. column = i % 32;
  58. row = i / 32;
  59. if (the_ticks & 0x1) {
  60. SDL_RenderFillRectF(video_state->renderer, &(SDL_FRect) {
  61. (f32) (video_state->config.width - ((column + 1) * size)),
  62. (f32) (video_state->config.height - ((row + 1) * size)),
  63. size,
  64. size
  65. });
  66. }
  67. the_ticks >>= 1;
  68. }
  69. }
  70. void IZ_VideoUpdateForDebugInput(IZ_VideoState* video_state, IZ_InputState* input_state) {
  71. SDL_SetRenderDrawColor(video_state->renderer, 0xff, 0xff, 0x00, 0xff);
  72. u8 column;
  73. u8 row;
  74. const u8 size = 4;
  75. u8 p;
  76. u8 i;
  77. for (p = 0; p < IZ_PLAYERS; p += 1) {
  78. IZ_Action the_action = input_state->action[p];
  79. for (i = 0; i < CONTROLS; i += 1) {
  80. column = (i % 4) + (p * 4);
  81. row = i / 4;
  82. if (the_action & 0x1) {
  83. SDL_RenderFillRectF(video_state->renderer, &(SDL_FRect) {
  84. (f32) (column * size),
  85. (f32) (row * size),
  86. size,
  87. size
  88. });
  89. }
  90. the_action >>= 1;
  91. }
  92. }
  93. }
  94. void IZ_VideoUpdateForDebug(IZ_VideoState* video_state, IZ_InputState* input_state, u64 ticks) {
  95. IZ_VideoUpdateForDebugTicks(video_state, ticks);
  96. IZ_VideoUpdateForDebugInput(video_state, input_state);
  97. }
  98. void IZ_VideoUpdate(IZ_VideoState* video_state, u64 ticks, IZ_InputState* input_state) {
  99. if (ticks - video_state->last_update_at > 1000 / video_state->config.max_fps) {
  100. // Update window
  101. SDL_SetRenderDrawColor(video_state->renderer, 0x00, 0x00, 0x00, 0xff);
  102. SDL_RenderClear(video_state->renderer);
  103. for (u8 i = 0; i < MAX_ACTIVE_SPRITES; i += 1) {
  104. if (!video_state->active_sprites[i]) {
  105. continue;
  106. }
  107. // TODO draw sprites
  108. }
  109. IZ_VideoUpdateForDebug(video_state, input_state, ticks);
  110. SDL_RenderPresent(video_state->renderer);
  111. video_state->last_update_at = ticks;
  112. }
  113. }
  114. void IZ_VideoTeardown(IZ_VideoState* state) {
  115. SDL_DestroyWindow(state->window);
  116. }