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.
 
 
 
 
 
 

112 lines
2.8 KiB

  1. #include "IZ_app.h"
  2. IZ_ProcedureResult IZ_InitializeApp(IZ_App* app) {
  3. if (SDL_Init(
  4. SDL_INIT_VIDEO
  5. | SDL_INIT_GAMECONTROLLER
  6. | SDL_INIT_EVENTS
  7. ) < 0) {
  8. fprintf_s(stderr, "SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
  9. return 1;
  10. }
  11. char config_path[128];
  12. IZ_GetConfigPath(config_path, 128);
  13. if (IZ_InitializeVideo(config_path, &app->video_state)) {
  14. return 2;
  15. }
  16. IZ_InitializeInput(config_path, &app->input_state);
  17. IZ_InitializePool(&app->memory_pool);
  18. // void* p1 = IZ_PoolAllocate(&app->memory_pool, sizeof(u16), 0)->pointer;
  19. // void* p2 = IZ_PoolAllocate(&app->memory_pool, sizeof(u8), 0)->pointer;
  20. // void* p3 = IZ_PoolAllocate(&app->memory_pool, sizeof(u64), 0)->pointer;
  21. // void* p4 = IZ_PoolAllocate(&app->memory_pool, sizeof(u32), 0)->pointer;
  22. // printf("\n%p %p %p %p\n", p1, p2, p3, p4);
  23. // IZ_PoolDeallocate(p1);
  24. // void* p5 = IZ_PoolAllocate(&app->memory_pool, sizeof(u16), 0)->pointer;
  25. // printf("\n%p\n", p5);
  26. app->quit = false;
  27. app->ticks = 0;
  28. return 0;
  29. }
  30. void IZ_TeardownApp(IZ_App* app) {
  31. IZ_TeardownInput(&app->input_state);
  32. IZ_TeardownVideo(&app->video_state);
  33. SDL_Quit();
  34. }
  35. void IZ_HandleSDLEvents(IZ_App* app) {
  36. while (SDL_PollEvent(&app->sdl_event) != 0) {
  37. if (app->sdl_event.type == SDL_QUIT) {
  38. app->quit = true;
  39. break;
  40. }
  41. IZ_HandleSDLInputEvents(app->sdl_event, &app->input_state);
  42. }
  43. }
  44. void IZ_HandlePortMIDIEvents(IZ_App* app) {
  45. u8 player_index;
  46. i32* midi_events_count;
  47. u32 midi_event_index;
  48. for (player_index = 0; player_index < PLAYERS; player_index += 1) {
  49. midi_events_count = &app->input_state.midi_input_state[player_index].midi_events_count;
  50. *midi_events_count = Pm_Read(
  51. app->input_state.midi_input_state[player_index].stream,
  52. app->input_state.midi_input_state[player_index].event_buffer,
  53. 1024
  54. );
  55. if (*midi_events_count < 1) {
  56. continue;
  57. }
  58. for (midi_event_index = 0; midi_event_index < *midi_events_count; midi_event_index += 1) {
  59. IZ_HandlePortMIDIInputEvents(
  60. app->input_state.midi_input_state[player_index].event_buffer[midi_event_index],
  61. &app->input_state
  62. );
  63. }
  64. }
  65. }
  66. void IZ_HandleEvents(IZ_App* app) {
  67. IZ_HandleSDLEvents(app);
  68. IZ_HandlePortMIDIEvents(app);
  69. }
  70. IZ_ProcedureResult IZ_RunApp(IZ_App* app, u8 arg_count, char* arg_values[]) {
  71. printf_s("Args (%u):\n", arg_count);
  72. u8 arg_index;
  73. for (arg_index = 0; arg_index < arg_count; arg_index += 1) {
  74. printf_s(" %s\n", arg_values[arg_index]);
  75. }
  76. IZ_ProcedureResult init_result = IZ_InitializeApp(app);
  77. if (init_result) {
  78. return init_result;
  79. }
  80. while (true) {
  81. app->ticks = SDL_GetTicks64();
  82. // TODO do audio processing
  83. // TODO do networking?
  84. IZ_HandleEvents(app);
  85. if (app->quit) {
  86. break;
  87. }
  88. IZ_UpdateVideo(&app->video_state, &app->input_state, app->ticks);
  89. }
  90. IZ_TeardownApp(app);
  91. return 0;
  92. }