2D Run-and-gun shooter inspired by One Man's Doomsday, Counter-Strike, and Metal Slug.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

112 linhas
2.4 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. app->quit = false;
  25. return 0;
  26. }
  27. void IZ_AppTeardown(IZ_App* app) {
  28. IZ_PoolTeardown(&app->pool);
  29. IZ_InputTeardown(&app->input_state);
  30. IZ_VideoTeardown(&app->video_state);
  31. SDL_Quit();
  32. }
  33. void IZ_AppHandleSDLEvents(IZ_App* app) {
  34. while (SDL_PollEvent(&app->sdl_event) != 0) {
  35. if (app->sdl_event.type == SDL_QUIT) {
  36. app->quit = true;
  37. break;
  38. }
  39. IZ_InputHandleSDLEvents(app->sdl_event, &app->input_state);
  40. }
  41. }
  42. void IZ_AppHandlePortMIDIEvents(IZ_App* app) {
  43. u8 player_index;
  44. i32* midi_events_count;
  45. u32 midi_event_index;
  46. for (player_index = 0; player_index < PLAYERS; player_index += 1) {
  47. midi_events_count = &app->input_state.midi_input_state[player_index].midi_events_count;
  48. *midi_events_count = Pm_Read(
  49. app->input_state.midi_input_state[player_index].stream,
  50. app->input_state.midi_input_state[player_index].event_buffer,
  51. 1024
  52. );
  53. if (*midi_events_count < 1) {
  54. continue;
  55. }
  56. for (midi_event_index = 0; midi_event_index < *midi_events_count; midi_event_index += 1) {
  57. IZ_InputHandlePortMIDIEvents(
  58. app->input_state.midi_input_state[player_index].event_buffer[midi_event_index],
  59. &app->input_state
  60. );
  61. }
  62. }
  63. }
  64. void IZ_AppHandleEvents(IZ_App* app) {
  65. IZ_AppHandleSDLEvents(app);
  66. IZ_AppHandlePortMIDIEvents(app);
  67. }
  68. IZ_ProcedureResult IZ_AppRun(IZ_App* app, u8 arg_count, char* arg_values[]) {
  69. printf_s("Args (%u):\n", arg_count);
  70. u8 arg_index;
  71. for (arg_index = 0; arg_index < arg_count; arg_index += 1) {
  72. printf_s(" %s\n", arg_values[arg_index]);
  73. }
  74. IZ_ProcedureResult init_result = IZ_AppInitialize(app);
  75. if (init_result) {
  76. return init_result;
  77. }
  78. while (true) {
  79. app->ticks = SDL_GetTicks64();
  80. // TODO do audio processing
  81. // TODO do networking?
  82. IZ_AppHandleEvents(app);
  83. if (app->quit) {
  84. break;
  85. }
  86. IZ_VideoUpdate(&app->video_state, &app->input_state, app->ticks);
  87. }
  88. IZ_AppTeardown(app);
  89. return 0;
  90. }