Advertisement
j0h

pixleDrawSDL2

j0h
Aug 6th, 2022 (edited)
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. //Source: https://bitbucket.org/dandago/gigilabs/src/master/Sdl2PixelDrawing/Sdl2PixelDrawing/main.cpp
  2. #include <unistd.h>
  3. #include <iostream>
  4. #include <SDL2/SDL.h>
  5. //Define window height, width
  6. #define height 1280
  7. #define width 760
  8. int main(){
  9. bool leftMouseButtonDown = false;
  10. bool quit = false;
  11. SDL_Event event;
  12. SDL_Init(SDL_INIT_VIDEO);
  13. SDL_Window * window = SDL_CreateWindow("SDL2 Pixel Drawing", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, height, width, SDL_WINDOW_FULLSCREEN);
  14. SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
  15. //SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0);
  16. SDL_Texture * texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, height, width);
  17. Uint32 * pixels = new Uint32[height * width];
  18. Uint32 colour = 0xFF0000;
  19.  
  20. memset(pixels, 255, height * width * sizeof(Uint32)); //int is bg color
  21.  
  22. while (!quit){
  23. SDL_UpdateTexture(texture, NULL, pixels, height * sizeof(Uint32));
  24. SDL_WaitEvent(&event);
  25.  
  26. switch (event.type){
  27. case SDL_QUIT:
  28. quit = true;
  29. break;
  30. case SDL_MOUSEBUTTONUP:
  31. if (event.button.button == SDL_BUTTON_LEFT)
  32. leftMouseButtonDown = true;
  33. break;
  34. case SDL_MOUSEBUTTONDOWN:
  35. if(event.button.button ==SDL_BUTTON_RIGHT ){
  36. memset(pixels, 23, height * width * sizeof(Uint32)); //int is bg color
  37. }
  38.  
  39. if (event.button.button == SDL_BUTTON_LEFT)
  40. leftMouseButtonDown = true;
  41. case SDL_MOUSEMOTION:
  42. if (leftMouseButtonDown){
  43. int mouseX = event.motion.x;
  44. int mouseY = event.motion.y;
  45. colour+=mouseY*8+mouseX*8; //makes a nice scaling effect.
  46. if(colour>=0xFFFFFF)
  47. colour=0xFF0000;
  48. pixels[mouseY * height+ mouseX] = colour;
  49.  
  50. }
  51. break;
  52. }
  53. SDL_RenderClear(renderer);
  54. SDL_RenderCopy(renderer, texture, NULL, NULL);
  55. SDL_RenderPresent(renderer);
  56. }
  57.  
  58. delete[] pixels;
  59. SDL_DestroyTexture(texture);
  60. SDL_DestroyRenderer(renderer);
  61.  
  62. SDL_DestroyWindow(window);
  63. SDL_Quit();
  64.  
  65. return 0;
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement