Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Source: https://bitbucket.org/dandago/gigilabs/src/master/Sdl2PixelDrawing/Sdl2PixelDrawing/main.cpp
- #include <unistd.h>
- #include <iostream>
- #include <SDL2/SDL.h>
- //Define window height, width
- #define height 1280
- #define width 760
- int main(){
- bool leftMouseButtonDown = false;
- bool quit = false;
- SDL_Event event;
- SDL_Init(SDL_INIT_VIDEO);
- SDL_Window * window = SDL_CreateWindow("SDL2 Pixel Drawing", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, height, width, SDL_WINDOW_FULLSCREEN);
- SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
- //SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0);
- SDL_Texture * texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, height, width);
- Uint32 * pixels = new Uint32[height * width];
- Uint32 colour = 0xFF0000;
- memset(pixels, 255, height * width * sizeof(Uint32)); //int is bg color
- while (!quit){
- SDL_UpdateTexture(texture, NULL, pixels, height * sizeof(Uint32));
- SDL_WaitEvent(&event);
- switch (event.type){
- case SDL_QUIT:
- quit = true;
- break;
- case SDL_MOUSEBUTTONUP:
- if (event.button.button == SDL_BUTTON_LEFT)
- leftMouseButtonDown = true;
- break;
- case SDL_MOUSEBUTTONDOWN:
- if(event.button.button ==SDL_BUTTON_RIGHT ){
- memset(pixels, 23, height * width * sizeof(Uint32)); //int is bg color
- }
- if (event.button.button == SDL_BUTTON_LEFT)
- leftMouseButtonDown = true;
- case SDL_MOUSEMOTION:
- if (leftMouseButtonDown){
- int mouseX = event.motion.x;
- int mouseY = event.motion.y;
- colour+=mouseY*8+mouseX*8; //makes a nice scaling effect.
- if(colour>=0xFFFFFF)
- colour=0xFF0000;
- pixels[mouseY * height+ mouseX] = colour;
- }
- break;
- }
- SDL_RenderClear(renderer);
- SDL_RenderCopy(renderer, texture, NULL, NULL);
- SDL_RenderPresent(renderer);
- }
- delete[] pixels;
- SDL_DestroyTexture(texture);
- SDL_DestroyRenderer(renderer);
- SDL_DestroyWindow(window);
- SDL_Quit();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement