Starter project for SDL2.
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.

165 lines
4.8 KiB

  1. #include <SDL.h>
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include "IZ_action.h"
  5. #include "IZ_app.h"
  6. int main(int argc, char* args[]) {
  7. SDL_Window* window = NULL;
  8. SDL_Surface* screen_surface = NULL;
  9. IZ_App app;
  10. IZ_InitializeApp(&app);
  11. if (SDL_Init(
  12. SDL_INIT_VIDEO
  13. | SDL_INIT_GAMECONTROLLER
  14. | SDL_INIT_EVENTS
  15. ) < 0) {
  16. printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
  17. return -1;
  18. }
  19. window = SDL_CreateWindow(
  20. APP_NAME,
  21. SDL_WINDOWPOS_CENTERED,
  22. SDL_WINDOWPOS_CENTERED,
  23. app.config.video.width,
  24. app.config.video.height,
  25. SDL_WINDOW_SHOWN
  26. );
  27. if (window == NULL) {
  28. printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
  29. return -2;
  30. }
  31. bool quit = false;
  32. SDL_Event e;
  33. screen_surface = SDL_GetWindowSurface(window);
  34. while (!quit) {
  35. SDL_FillRect(screen_surface, NULL, SDL_MapRGB(screen_surface->format, 0x00, 0x00, 0x00));
  36. uint64_t ticks = SDL_GetTicks64();
  37. for (uint8_t i = 0; i < 64; i += 1) {
  38. const uint8_t column = (64 - i) % 32;
  39. const uint8_t row = i / 32;
  40. const uint64_t bitflag = (0x1lu << i);
  41. const uint8_t size = 4;
  42. if (ticks & bitflag) {
  43. SDL_FillRect(screen_surface, &(SDL_Rect) {
  44. column * size,
  45. app.config.video.height - ((row + 1) * size),
  46. size,
  47. size
  48. }, SDL_MapRGB(screen_surface->format, 0x00, 0xff, 0xff));
  49. }
  50. }
  51. while (SDL_PollEvent(&e) != 0) {
  52. if (e.type == SDL_QUIT) {
  53. quit = true;
  54. }
  55. // Handle joystick events
  56. for (uint8_t current_player = 0; current_player < PLAYERS; current_player += 1) {
  57. if (e.type == SDL_JOYDEVICEADDED) {
  58. if (SDL_NumJoysticks() <= PLAYERS && !app.assigned_joysticks[current_player]) {
  59. app.assigned_joysticks[current_player] = SDL_JoystickOpen(e.jdevice.which);
  60. }
  61. }
  62. if (e.type == SDL_JOYDEVICEREMOVED) {
  63. if (
  64. app.assigned_joysticks[current_player]
  65. && SDL_JoystickInstanceID(app.assigned_joysticks[current_player]) == e.jdevice.which
  66. ) {
  67. app.assigned_joysticks[current_player] = NULL;
  68. }
  69. }
  70. if (e.type == SDL_JOYAXISMOTION) {
  71. if (e.jaxis.axis == 0 || e.jaxis.axis == 3) {
  72. app.actions[current_player] &= ~(0x1 << IZ_ACTION_INDEX_RIGHT);
  73. app.actions[current_player] &= ~(0x1 << IZ_ACTION_INDEX_LEFT);
  74. if (e.jaxis.value > app.config.input.gamepad_axis_threshold) {
  75. app.actions[current_player] |= (0x1 << IZ_ACTION_INDEX_RIGHT);
  76. } else if (e.jaxis.value <= -app.config.input.gamepad_axis_threshold) {
  77. app.actions[current_player] |= (0x1 << IZ_ACTION_INDEX_LEFT);
  78. }
  79. }
  80. if (e.jaxis.axis == 1 || e.jaxis.axis == 4) {
  81. app.actions[current_player] &= ~(0x1 << IZ_ACTION_INDEX_UP);
  82. app.actions[current_player] &= ~(0x1 << IZ_ACTION_INDEX_DOWN);
  83. if (e.jaxis.value > app.config.input.gamepad_axis_threshold) {
  84. app.actions[current_player] |= (0x1 << IZ_ACTION_INDEX_DOWN);
  85. } else if (e.jaxis.value <= -app.config.input.gamepad_axis_threshold) {
  86. app.actions[current_player] |= (0x1 << IZ_ACTION_INDEX_UP);
  87. }
  88. }
  89. }
  90. if (e.type == SDL_JOYHATMOTION) {
  91. app.actions[current_player] &= ~(0x1 << IZ_ACTION_INDEX_UP);
  92. app.actions[current_player] &= ~(0x1 << IZ_ACTION_INDEX_RIGHT);
  93. app.actions[current_player] &= ~(0x1 << IZ_ACTION_INDEX_DOWN);
  94. app.actions[current_player] &= ~(0x1 << IZ_ACTION_INDEX_LEFT);
  95. if (e.jhat.value != 0) {
  96. app.actions[current_player] |= e.jhat.value;
  97. }
  98. }
  99. }
  100. // Handle keyboard events
  101. for (uint8_t current_player = 0; current_player < PLAYERS; current_player += 1) {
  102. for (uint8_t i = 0; i < CONTROLS; i += 1) {
  103. if (e.key.keysym.sym == app.config.controls[current_player].keyboard[i]) {
  104. const uint16_t bitflag = (0x1 << i);
  105. if (e.type == SDL_KEYDOWN) {
  106. app.actions[current_player] |= bitflag;
  107. } else if (e.type == SDL_KEYUP) {
  108. app.actions[current_player] &= ~bitflag;
  109. }
  110. }
  111. if (i >= 4) {
  112. if (e.jbutton.button == app.config.controls[current_player].gamepad[i]) {
  113. const uint16_t bitflag = (0x1 << i);
  114. if (e.type == SDL_JOYBUTTONDOWN) {
  115. app.actions[current_player] |= bitflag;
  116. } else if (e.type == SDL_JOYBUTTONUP) {
  117. app.actions[current_player] &= ~bitflag;
  118. }
  119. }
  120. }
  121. }
  122. }
  123. }
  124. // Update window
  125. for (uint8_t current_player = 0; current_player < PLAYERS; current_player += 1) {
  126. for (uint8_t i = 0; i < CONTROLS; i += 1) {
  127. const uint8_t column = i % 4;
  128. const uint8_t row = i / 4;
  129. const IZ_Action bitflag = (0x1 << i);
  130. const uint8_t size = 4;
  131. if (app.actions[current_player] & bitflag) {
  132. SDL_FillRect(screen_surface, &(SDL_Rect) {
  133. column * size,
  134. row * size,
  135. size,
  136. size
  137. }, SDL_MapRGB(screen_surface->format, 0xff, 0xff, 0x00));
  138. }
  139. }
  140. }
  141. SDL_UpdateWindowSurface(window);
  142. }
  143. SDL_DestroyWindow(window);
  144. SDL_Quit();
  145. return 0;
  146. }