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.
 
 
 
 
 
 

123 rivejä
3.3 KiB

  1. #include "IZ_video.h"
  2. bool IZ_VideoIsValidWidth(u16 width) {
  3. // TODO check screen size
  4. return (320 <= width && width <= 16384);
  5. }
  6. bool IZ_VideoIsValidHeight(u16 height) {
  7. // TODO check screen size
  8. return (240 <= height && height <= 8192);
  9. }
  10. bool IZ_VideoIsValidMaxFPS(u8 max_fps) {
  11. return (10 <= max_fps && max_fps <= 200);
  12. }
  13. static IZ_ConfigItem video_config_items[] = {
  14. {
  15. IZ_CONFIG_TYPE_U16,
  16. sizeof(u16),
  17. "Video",
  18. "Width",
  19. NULL,
  20. &IZ_VIDEO_DEFAULT_STATE.config.width,
  21. IZ_VideoIsValidWidth,
  22. },
  23. {
  24. IZ_CONFIG_TYPE_U16,
  25. sizeof(u16),
  26. "Video",
  27. "Height",
  28. NULL,
  29. &IZ_VIDEO_DEFAULT_STATE.config.height,
  30. IZ_VideoIsValidHeight,
  31. },
  32. {
  33. IZ_CONFIG_TYPE_U8,
  34. sizeof(u8),
  35. "Video",
  36. "MaxFps",
  37. "-f",
  38. &IZ_VIDEO_DEFAULT_STATE.config.max_fps,
  39. IZ_VideoIsValidMaxFPS,
  40. },
  41. IZ_CONFIG_ITEM_NULL,
  42. };
  43. void IZ_VideoBindStateToConfig(IZ_VideoState* state, IZ_ConfigItem config_items[]) {
  44. config_items[0].dest = &state->config.width;
  45. config_items[1].dest = &state->config.height;
  46. config_items[2].dest = &state->config.max_fps;
  47. }
  48. IZ_ProcedureResult IZ_VideoSaveConfig(IZ_VideoState* state, const char* config_path) {
  49. IZ_VideoBindStateToConfig(state, video_config_items);
  50. return IZ_ConfigSave(video_config_items, config_path);
  51. }
  52. IZ_ProcedureResult IZ_VideoInitializeConfig(IZ_VideoState* state, const char* config_path, u8 argc, const char* argv[]) {
  53. IZ_VideoBindStateToConfig(state, video_config_items);
  54. if (IZ_ConfigInitialize(video_config_items, config_path, argc, argv) < 0) {
  55. return -1;
  56. }
  57. return 0;
  58. }
  59. IZ_ProcedureResult IZ_VideoInitialize(IZ_VideoState* state, void* user_data, const char* config_path, u8 argc, const char* argv[]) {
  60. SDL_memcpy(state, &IZ_VIDEO_DEFAULT_STATE, sizeof(IZ_VideoState));
  61. if (IZ_VideoInitializeConfig(state, config_path, argc, argv) < 0) {
  62. return -2;
  63. }
  64. state->last_update_at = 0u;
  65. state->user_data = user_data;
  66. SDL_Window* window = SDL_CreateWindow(
  67. IZ_APP_NAME,
  68. SDL_WINDOWPOS_CENTERED,
  69. SDL_WINDOWPOS_CENTERED,
  70. state->config.width,
  71. state->config.height,
  72. SDL_WINDOW_SHOWN
  73. );
  74. if (window == NULL) {
  75. // fprintf_s(stderr, "Window could not be created! SDL_Error: %s\n", SDL_GetError());
  76. return -3;
  77. }
  78. state->window = window;
  79. state->renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
  80. return 0;
  81. }
  82. void IZ_VideoTeardown(IZ_VideoState* state) {
  83. SDL_DestroyWindow(state->window);
  84. }
  85. void IZ_VideoLoadTexture(IZ_VideoState* state, const char* dir, const char* filename, IZ_LoadedSprite* out) {
  86. char full_path[2048];
  87. sprintf(full_path, "%s/%s", dir, filename);
  88. FILE* f = fopen(full_path, "r");
  89. u32 sprite_length_bytes = ini_getl(dir, filename, 0, "assets.ini");
  90. u8* sprite = malloc(sprite_length_bytes + 1);
  91. fread(sprite, 1, sprite_length_bytes, f);
  92. SDL_SetRenderDrawBlendMode(state->renderer, SDL_BLENDMODE_ADD);
  93. SDL_Surface* test_surface = IMG_LoadSVG_RW(SDL_RWFromConstMem(sprite, sprite_length_bytes));
  94. free(sprite);
  95. if (test_surface) {
  96. out->texture = SDL_CreateTextureFromSurface(state->renderer, test_surface);
  97. out->original_width = test_surface->w;
  98. out->original_height = test_surface->h;
  99. SDL_FreeSurface(test_surface);
  100. }
  101. }
  102. void IZ_VideoTeardownTexture(IZ_LoadedSprite* sprite) {
  103. if (!sprite->texture) {
  104. return;
  105. }
  106. SDL_DestroyTexture(sprite->texture);
  107. sprite->original_width = 0;
  108. sprite->original_height = 0;
  109. }