Advertisement
j0h

kek

j0h
Aug 13th, 2022
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. #include <sstream>
  2. #include <SDL2/SDL.h>
  3.  
  4. int main(int argc, char ** argv){
  5. // variables
  6.  
  7. bool quit = false;
  8. SDL_Event event;
  9. int x = 288;
  10. int y = 208;
  11.  
  12. // init SDL
  13.  
  14. SDL_Init(SDL_INIT_VIDEO);
  15. SDL_Window * window = SDL_CreateWindow("SDL2 Keyboard/Mouse events",
  16. SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
  17. SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0);
  18.  
  19. SDL_Surface * image = SDL_LoadBMP("spaceship.bmp");
  20. SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer, image);
  21. SDL_FreeSurface(image);
  22.  
  23. SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  24.  
  25. // handle events
  26.  
  27. while (!quit)
  28. {
  29. SDL_Delay(20);
  30. SDL_PollEvent(&event);
  31.  
  32. switch (event.type)
  33. {
  34. case SDL_QUIT:
  35. quit = true;
  36. break;
  37. case SDL_KEYDOWN:
  38. switch (event.key.keysym.sym)
  39. {
  40. case SDLK_LEFT: x--; break;
  41. case SDLK_RIGHT: x++; break;
  42. case SDLK_UP: y--; break;
  43. case SDLK_DOWN: y++; break;
  44. case SDLK_q: quit=true;
  45. }
  46. break;
  47. case SDL_MOUSEBUTTONDOWN:
  48. switch (event.button.button)
  49. {
  50. case SDL_BUTTON_LEFT:
  51. SDL_ShowSimpleMessageBox(0, "Mouse", "Left button was pressed!", window);
  52. break;
  53. case SDL_BUTTON_RIGHT:
  54. SDL_ShowSimpleMessageBox(0, "Mouse", "Right button was pressed!", window);
  55. break;
  56. default:
  57. SDL_ShowSimpleMessageBox(0, "Mouse", "Some other button was pressed!", window);
  58. break;
  59. }
  60. break;
  61. case SDL_MOUSEMOTION:
  62. int mouseX = event.motion.x;
  63. int mouseY = event.motion.y;
  64.  
  65. std::stringstream ss;
  66. ss << "X: " << mouseX << " Y: " << mouseY;
  67.  
  68. SDL_SetWindowTitle(window, ss.str().c_str());
  69. break;
  70. }
  71.  
  72. SDL_Rect dstrect = { x, y, 64, 64 };
  73.  
  74. SDL_RenderClear(renderer);
  75. SDL_RenderCopy(renderer, texture, NULL, &dstrect);
  76. SDL_RenderPresent(renderer);
  77. }
  78.  
  79. // cleanup SDL
  80.  
  81. SDL_DestroyTexture(texture);
  82. SDL_DestroyRenderer(renderer);
  83. SDL_DestroyWindow(window);
  84. SDL_Quit();
  85.  
  86. return 0;
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement