Advertisement
Guest User

Simple C++ SDL2 example

a guest
Feb 18th, 2015
580
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <SDL2/SDL.h>
  3.  
  4. int main(int argc, char ** argv) {
  5.    
  6.     size_t width = 800;
  7.     size_t height = 600;
  8.     bool quit = false;
  9.  
  10.     SDL_Init(SDL_INIT_VIDEO);
  11.  
  12.     SDL_Window * window = SDL_CreateWindow("SDL2 Pixel Drawing",
  13.         SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, 0);
  14.  
  15.     SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0);
  16.     SDL_Texture * texture = SDL_CreateTexture(renderer,
  17.         SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, width, height);
  18.     Uint32 * pixels = new Uint32[width * height];
  19.     memset(pixels, 255, width * height * sizeof(Uint32));
  20.  
  21.     while(!quit) {
  22.         SDL_UpdateTexture(texture, NULL, pixels, width * sizeof(Uint32));
  23.  
  24.         for(int n=0;n<1000;n++) {
  25.             int x=rand()%width;
  26.             int y=rand()%height;
  27.             pixels[y * width + x] = rand()*100000;
  28.         }
  29.  
  30.         SDL_Event event;
  31.         while(SDL_PollEvent(&event)) {
  32.             switch (event.type) {
  33.                 case SDL_QUIT:
  34.                     quit = true;
  35.                     break;
  36.             }
  37.             if(event.key.keysym.sym == SDLK_q) {
  38.                 quit = true;
  39.                 break;
  40.             }
  41.             else if(event.key.keysym.sym == SDLK_c) {
  42.                 memset(pixels, 255, width * height * sizeof(Uint32));
  43.             }
  44.        
  45.         }
  46.        
  47.         SDL_RenderClear(renderer);
  48.         SDL_RenderCopy(renderer, texture, NULL, NULL);
  49.         SDL_RenderPresent(renderer);
  50.     }
  51.  
  52.     delete[] pixels;
  53.     SDL_DestroyTexture(texture);
  54.     SDL_DestroyRenderer(renderer);
  55.     SDL_DestroyWindow(window);
  56.     SDL_Quit();
  57.  
  58.     return 0;
  59. }
  60.  
  61. CXX = g++
  62. # Update these paths to match your installation
  63. # You may also need to update the linker option rpath, which sets where to look for
  64. # the SDL2 libraries at runtime to match your install
  65. SDL_LIB = -L/usr/lib/x86_64-linux-gnu/ -lSDL2 -Wl,-rpath=/usr/lib/x86_64-linux-gnu/
  66. SDL_INCLUDE = -I/usr/include/
  67. # You may need to change -std=c++11 to -std=c++0x if your compiler is a bit older
  68. CXXFLAGS = -Wall -c -std=c++11 $(SDL_INCLUDE)
  69. LDFLAGS = $(SDL_LIB)
  70. EXE = rand_points
  71.  
  72. all: $(EXE)
  73.  
  74. $(EXE): rand_points.o
  75.     $(CXX) $< $(LDFLAGS) -o $@
  76.  
  77. main.o: rand_points.cpp
  78.     $(CXX) $(CXXFLAGS) $< -o $@
  79.  
  80. clean:
  81.     rm *.o && rm $(EXE)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement