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.
 
 
 
 
 
 

131 lines
3.1 KiB

  1. #include "IZ_app.h"
  2. u64 IZ_AppGetTicks(struct IZ_App* app) {
  3. return app->ticks;
  4. }
  5. void IZ_AppBindConnection(struct IZ_App* app, struct lws* wsi) {
  6. app->net_state.binding.connection = wsi;
  7. }
  8. IZ_NetClientState* IZ_AppGetNetState(struct IZ_App* app) {
  9. return &app->net_state;
  10. }
  11. IZ_InputState* IZ_AppGetInputState(struct IZ_App* app) {
  12. return &app->input_state;
  13. }
  14. IZ_ProcedureResult IZ_AppInitialize(struct IZ_App* app, u8 argc, const char* argv[]) {
  15. memset(app, 0, sizeof(struct IZ_App));
  16. const char* cmdline_buffer;
  17. char config_path[128];
  18. // TODO abstract command line args parsing
  19. if ((cmdline_buffer = IZ_ConfigGetCommandlineOption(argc, argv, "-c"))) {
  20. memcpy_s(config_path, 128, cmdline_buffer, 128);
  21. } else {
  22. IZ_ConfigGetDefaultPath(config_path, 128);
  23. }
  24. u32 flags = (
  25. SDL_INIT_VIDEO
  26. | SDL_INIT_GAMECONTROLLER
  27. | SDL_INIT_EVENTS
  28. );
  29. if (SDL_Init(flags) < 0) {
  30. SDL_LogError(SDL_LOG_CATEGORY_ERROR, "SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
  31. return IZ_APP_RUN_SDL_INIT_ERROR;
  32. }
  33. if (IZ_VideoInitialize(&app->video_state, app, config_path, argc, argv)) {
  34. return IZ_APP_RUN_VIDEO_INIT_ERROR;
  35. }
  36. if (IZ_InputInitialize(&app->input_state, config_path, argc, argv)) {
  37. return IZ_APP_RUN_INPUT_INIT_ERROR;
  38. }
  39. if (IZ_NetClientInitialize(&app->net_state, app, IZ_AppRunNetworkingThread, config_path, argc, argv)) {
  40. return IZ_APP_RUN_NETWORKING_ERROR;
  41. }
  42. IZ_PoolInitialize(&app->pool, POOL_MAX_SIZE);
  43. app->ticks = 0;
  44. return IZ_APP_RUN_RESULT_OK;
  45. }
  46. void IZ_AppTeardown(struct IZ_App* app) {
  47. IZ_NetClientDisconnect(&app->net_state);
  48. IZ_PoolTeardown(&app->pool);
  49. IZ_InputTeardown(&app->input_state);
  50. IZ_VideoTeardown(&app->video_state);
  51. SDL_Quit();
  52. }
  53. void IZ_AppPrintHelpOptions() {
  54. printf("\n");
  55. printf("Options:\n");
  56. printf("\n");
  57. printf(
  58. "\n"
  59. "Options:\n"
  60. "\n"
  61. " -c <path> Specifies the path to the config file. (default: \"./config-game.ini\")\n"
  62. " -f <value> Specifies the frames per second. (default: 30)\n"
  63. " -h Displays this help screen.\n"
  64. " -i <value> Specifies the interval of sending packets (default: 200)\n"
  65. " in milliseconds.\n"
  66. );
  67. }
  68. void IZ_AppPrintHelpUsage() {
  69. printf("\n");
  70. printf("Usage:");
  71. printf(" %s [options]\n", "game.exe");
  72. }
  73. void IZ_AppPrintHelpHeader() {
  74. printf("\n");
  75. printf("%s - %s\n", IZ_APP_NAME, IZ_APP_DESCRIPTION);
  76. }
  77. void IZ_AppPrintHelp() {
  78. IZ_AppPrintHelpHeader();
  79. IZ_AppPrintHelpUsage();
  80. IZ_AppPrintHelpOptions();
  81. }
  82. IZ_ProcedureResult IZ_AppRun(struct IZ_App* app, u8 argc, const char* argv[]) {
  83. // TODO have a config subsystem that handles these.
  84. if (IZ_ConfigGetCommandlineOption(argc, argv, "-h")) {
  85. IZ_AppPrintHelp();
  86. return IZ_APP_RUN_RESULT_OK;
  87. }
  88. IZ_ProcedureResult init_result = IZ_AppInitialize(app, argc, argv);
  89. if (init_result) {
  90. return init_result;
  91. }
  92. while (true) {
  93. app->ticks = SDL_GetTicks64();
  94. // TODO do audio processing
  95. // TODO do networking
  96. if (IZ_AppHandleInputEvents(app)) {
  97. break;
  98. }
  99. IZ_AppHandleOutboundNetworking(app);
  100. IZ_VideoUpdate(&app->video_state);
  101. }
  102. IZ_AppTeardown(app);
  103. return IZ_APP_RUN_RESULT_OK;
  104. }