2D Run-and-gun shooter inspired by One Man's Doomsday, Counter-Strike, and Metal Slug.
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

113 lines
2.5 KiB

  1. #include "IZ_app.h"
  2. IZ_ProcedureResult IZ_AppInitialize(IZ_App* app) {
  3. u32 flags = (
  4. SDL_INIT_VIDEO
  5. | SDL_INIT_GAMECONTROLLER
  6. | SDL_INIT_EVENTS
  7. );
  8. if (SDL_Init(flags) < 0) {
  9. // TODO fix logging
  10. fprintf_s(stderr, "SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
  11. return 1;
  12. }
  13. char config_path[128];
  14. IZ_ConfigGetPath(config_path, 128);
  15. if (IZ_VideoInitialize(config_path, &app->video_state)) {
  16. return 2;
  17. }
  18. if (IZ_InputInitialize(config_path, &app->input_state)) {
  19. return 3;
  20. }
  21. IZ_PoolInitialize(&app->pool, POOL_MAX_SIZE);
  22. // TODO put into its timer module
  23. app->ticks = 0;
  24. return 0;
  25. }
  26. void IZ_AppTeardown(IZ_App* app) {
  27. IZ_PoolTeardown(&app->pool);
  28. IZ_InputTeardown(&app->input_state);
  29. IZ_VideoTeardown(&app->video_state);
  30. SDL_Quit();
  31. }
  32. IZ_ProcedureResult IZ_AppHandleSDLEvents(IZ_App* app) {
  33. while (SDL_PollEvent(&app->input_state.sdl_event) != 0) {
  34. if (app->input_state.sdl_event.type == SDL_QUIT) {
  35. return 1;
  36. }
  37. IZ_InputHandleSDLEvents(&app->input_state);
  38. }
  39. return 0;
  40. }
  41. void IZ_AppHandlePortMIDIEvents(IZ_App* app) {
  42. u8 player_index;
  43. i32* midi_events_count;
  44. u32 midi_event_index;
  45. for (player_index = 0; player_index < PLAYERS; player_index += 1) {
  46. midi_events_count = &app->input_state.midi_input_state[player_index].midi_events_count;
  47. *midi_events_count = Pm_Read(
  48. app->input_state.midi_input_state[player_index].stream,
  49. app->input_state.midi_input_state[player_index].event_buffer,
  50. 1024
  51. );
  52. if (*midi_events_count < 1) {
  53. continue;
  54. }
  55. for (midi_event_index = 0; midi_event_index < *midi_events_count; midi_event_index += 1) {
  56. IZ_InputHandlePortMIDIEvents(
  57. app->input_state.midi_input_state[player_index].event_buffer[midi_event_index],
  58. &app->input_state
  59. );
  60. }
  61. }
  62. }
  63. IZ_ProcedureResult IZ_AppHandleEvents(IZ_App* app) {
  64. if (IZ_AppHandleSDLEvents(app)) {
  65. return 1;
  66. }
  67. IZ_AppHandlePortMIDIEvents(app);
  68. return 0;
  69. }
  70. IZ_ProcedureResult IZ_AppRun(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_AppInitialize(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. if (IZ_AppHandleEvents(app)) {
  85. break;
  86. }
  87. IZ_VideoUpdate(&app->video_state, &app->input_state, app->ticks);
  88. }
  89. IZ_AppTeardown(app);
  90. return 0;
  91. }