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.

120 lines
2.6 KiB

  1. #include <SDL.h>
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. const char* APP_NAME = "SDL2";
  5. const unsigned int SCREEN_WIDTH = 640;
  6. const unsigned int SCREEN_HEIGHT = 480;
  7. const unsigned char CONTROLS = 16;
  8. const SDL_KeyCode KEYBOARD_CONTROLS[CONTROLS] = {
  9. SDLK_RIGHT,
  10. SDLK_DOWN,
  11. SDLK_LEFT,
  12. SDLK_UP,
  13. SDLK_RETURN, // yes
  14. SDLK_BACKSPACE, // no
  15. SDLK_a, // action0
  16. SDLK_s, // action1
  17. SDLK_d, // action2
  18. SDLK_f, // action3
  19. SDLK_z, // action4
  20. SDLK_x, // action5
  21. SDLK_c, // action6
  22. SDLK_v, // action7
  23. SDLK_w, // action8
  24. SDLK_e, // action9
  25. };
  26. int main(int argc, char* args[]) {
  27. SDL_Window* window = NULL;
  28. SDL_Surface* screenSurface = NULL;
  29. if (SDL_Init(
  30. SDL_INIT_VIDEO
  31. | SDL_INIT_GAMECONTROLLER
  32. | SDL_INIT_EVENTS
  33. ) < 0) {
  34. printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
  35. return -1;
  36. }
  37. window = SDL_CreateWindow(
  38. APP_NAME,
  39. SDL_WINDOWPOS_UNDEFINED,
  40. SDL_WINDOWPOS_UNDEFINED,
  41. SCREEN_WIDTH,
  42. SCREEN_HEIGHT,
  43. SDL_WINDOW_SHOWN
  44. );
  45. if (window == NULL) {
  46. printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
  47. return -2;
  48. }
  49. bool quit = false;
  50. SDL_Event e;
  51. screenSurface = SDL_GetWindowSurface(window);
  52. unsigned short action = 0;
  53. while (!quit) {
  54. SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0x00, 0x00, 0x00));
  55. uint64_t ticks = SDL_GetTicks64();
  56. for (unsigned char i = 0; i < 64; i += 1) {
  57. const unsigned char column = (64 - i) % 32;
  58. const unsigned char row = i / 32;
  59. const uint64_t bitflag = (0x1lu << i);
  60. const unsigned char size = 4;
  61. if (ticks & bitflag) {
  62. SDL_FillRect(screenSurface, &(SDL_Rect) {
  63. column * size,
  64. SCREEN_HEIGHT - ((row + 1) * size),
  65. size,
  66. size
  67. }, SDL_MapRGB(screenSurface->format, 0x00, 0xff, 0xff));
  68. }
  69. }
  70. while (SDL_PollEvent(&e) != 0) {
  71. if (e.type == SDL_QUIT) {
  72. quit = true;
  73. }
  74. for (unsigned char i = 0; i < CONTROLS; i += 1) {
  75. // TODO do same for gamepad
  76. if (e.key.keysym.sym == KEYBOARD_CONTROLS[i]) {
  77. const unsigned short bitflag = (0x1 << i);
  78. if (e.type == SDL_KEYDOWN) {
  79. action |= bitflag;
  80. } else if (e.type == SDL_KEYUP) {
  81. action &= ~bitflag;
  82. }
  83. }
  84. }
  85. for (unsigned char i = 0; i < CONTROLS; i += 1) {
  86. const unsigned char column = i % 4;
  87. const unsigned char row = i / 4;
  88. const unsigned short bitflag = (0x1 << i);
  89. const unsigned char size = 4;
  90. if (action & bitflag) {
  91. SDL_FillRect(screenSurface, &(SDL_Rect) {
  92. column * size,
  93. row * size,
  94. size,
  95. size
  96. }, SDL_MapRGB(screenSurface->format, 0xff, 0xff, 0x00));
  97. }
  98. }
  99. SDL_UpdateWindowSurface(window);
  100. }
  101. }
  102. SDL_DestroyWindow(window);
  103. SDL_Quit();
  104. return 0;
  105. }