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.

107 lines
2.4 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.video_config.width,
  24. app.video_config.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. screen_surface = SDL_GetWindowSurface(window);
  32. bool quit = false;
  33. SDL_Event e;
  34. while (true) {
  35. uint64_t ticks = SDL_GetTicks64();
  36. // TODO do audio processing
  37. // TODO do networking?
  38. // process events
  39. while (SDL_PollEvent(&e) != 0) {
  40. if (e.type == SDL_QUIT) {
  41. quit = true;
  42. break;
  43. }
  44. for (uint8_t p = 0; p < PLAYERS; p += 1) {
  45. IZ_HandleJoystickEvents(e, &(app.joystick_state[p]), &app.actions[p]);
  46. IZ_HandleKeyboardEvents(e, &(app.keyboard_state[p]), &app.actions[p]);
  47. }
  48. }
  49. if (quit) {
  50. break;
  51. }
  52. if (ticks - app.video_update_at > 1000 / app.video_config.max_fps) {
  53. // Update window
  54. SDL_FillRect(screen_surface, NULL, SDL_MapRGB(screen_surface->format, 0x00, 0x00, 0x00));
  55. for (uint8_t i = 0; i < 64; i += 1) {
  56. const uint8_t column = (64 - i) % 32;
  57. const uint8_t row = i / 32;
  58. const uint64_t bitflag = (0x1lu << i);
  59. const uint8_t size = 4;
  60. if (ticks & bitflag) {
  61. SDL_FillRect(screen_surface, &(SDL_Rect) {
  62. column * size,
  63. app.video_config.height - ((row + 1) * size),
  64. size,
  65. size
  66. }, SDL_MapRGB(screen_surface->format, 0x00, 0xff, 0xff));
  67. }
  68. }
  69. for (uint8_t p = 0; p < PLAYERS; p += 1) {
  70. for (uint8_t i = 0; i < CONTROLS; i += 1) {
  71. const uint8_t column = (i % 4) + (p * 4);
  72. const uint8_t row = i / 4;
  73. const IZ_Action bitflag = (0x1 << i);
  74. const uint8_t size = 4;
  75. if (app.actions[p] & bitflag) {
  76. SDL_FillRect(screen_surface, &(SDL_Rect) {
  77. column * size,
  78. row * size,
  79. size,
  80. size
  81. }, SDL_MapRGB(screen_surface->format, 0xff, 0xff, 0x00));
  82. }
  83. }
  84. }
  85. SDL_UpdateWindowSurface(window);
  86. app.video_update_at = ticks;
  87. }
  88. }
  89. SDL_DestroyWindow(window);
  90. SDL_Quit();
  91. return 0;
  92. }