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.

101 lines
2.2 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. IZ_Action action = 0;
  35. while (!quit) {
  36. SDL_FillRect(screen_surface, NULL, SDL_MapRGB(screen_surface->format, 0x00, 0x00, 0x00));
  37. uint64_t ticks = SDL_GetTicks64();
  38. for (uint8_t i = 0; i < 64; i += 1) {
  39. const uint8_t column = (64 - i) % 32;
  40. const uint8_t row = i / 32;
  41. const uint64_t bitflag = (0x1lu << i);
  42. const uint8_t size = 4;
  43. if (ticks & bitflag) {
  44. SDL_FillRect(screen_surface, &(SDL_Rect) {
  45. column * size,
  46. app.config.video.height - ((row + 1) * size),
  47. size,
  48. size
  49. }, SDL_MapRGB(screen_surface->format, 0x00, 0xff, 0xff));
  50. }
  51. }
  52. while (SDL_PollEvent(&e) != 0) {
  53. if (e.type == SDL_QUIT) {
  54. quit = true;
  55. }
  56. for (uint8_t i = 0; i < CONTROLS; i += 1) {
  57. // TODO do same for gamepad
  58. if (e.key.keysym.sym == app.config.controls[0].keyboard[i]) {
  59. const uint16_t bitflag = (0x1 << i);
  60. if (e.type == SDL_KEYDOWN) {
  61. action |= bitflag;
  62. } else if (e.type == SDL_KEYUP) {
  63. action &= ~bitflag;
  64. }
  65. }
  66. }
  67. for (unsigned char i = 0; i < CONTROLS; i += 1) {
  68. const uint8_t column = i % 4;
  69. const uint8_t row = i / 4;
  70. const IZ_Action bitflag = (0x1 << i);
  71. const uint8_t size = 4;
  72. if (action & bitflag) {
  73. SDL_FillRect(screen_surface, &(SDL_Rect) {
  74. column * size,
  75. row * size,
  76. size,
  77. size
  78. }, SDL_MapRGB(screen_surface->format, 0xff, 0xff, 0x00));
  79. }
  80. }
  81. SDL_UpdateWindowSurface(window);
  82. }
  83. }
  84. SDL_DestroyWindow(window);
  85. SDL_Quit();
  86. return 0;
  87. }