Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <SDL2/SDL.h>
- int main(int argc, char ** argv) {
- size_t width = 800;
- size_t height = 600;
- bool quit = false;
- SDL_Init(SDL_INIT_VIDEO);
- SDL_Window * window = SDL_CreateWindow("SDL2 Pixel Drawing",
- SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, 0);
- SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0);
- SDL_Texture * texture = SDL_CreateTexture(renderer,
- SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, width, height);
- Uint32 * pixels = new Uint32[width * height];
- memset(pixels, 255, width * height * sizeof(Uint32));
- while(!quit) {
- SDL_UpdateTexture(texture, NULL, pixels, width * sizeof(Uint32));
- for(int n=0;n<1000;n++) {
- int x=rand()%width;
- int y=rand()%height;
- pixels[y * width + x] = rand()*100000;
- }
- SDL_Event event;
- while(SDL_PollEvent(&event)) {
- switch (event.type) {
- case SDL_QUIT:
- quit = true;
- break;
- }
- if(event.key.keysym.sym == SDLK_q) {
- quit = true;
- break;
- }
- else if(event.key.keysym.sym == SDLK_c) {
- memset(pixels, 255, width * height * sizeof(Uint32));
- }
- }
- 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;
- }
- CXX = g++
- # Update these paths to match your installation
- # You may also need to update the linker option rpath, which sets where to look for
- # the SDL2 libraries at runtime to match your install
- SDL_LIB = -L/usr/lib/x86_64-linux-gnu/ -lSDL2 -Wl,-rpath=/usr/lib/x86_64-linux-gnu/
- SDL_INCLUDE = -I/usr/include/
- # You may need to change -std=c++11 to -std=c++0x if your compiler is a bit older
- CXXFLAGS = -Wall -c -std=c++11 $(SDL_INCLUDE)
- LDFLAGS = $(SDL_LIB)
- EXE = rand_points
- all: $(EXE)
- $(EXE): rand_points.o
- $(CXX) $< $(LDFLAGS) -o $@
- main.o: rand_points.cpp
- $(CXX) $(CXXFLAGS) $< -o $@
- clean:
- rm *.o && rm $(EXE)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement