Skip to content

Instantly share code, notes, and snippets.

@Twinklebear
Created July 16, 2012 15:08
Show Gist options
  • Save Twinklebear/3123236 to your computer and use it in GitHub Desktop.
Save Twinklebear/3123236 to your computer and use it in GitHub Desktop.
SDL Rendering methods
//New rendering methods
//Now we use an SDL_Texture to use hardware accelerated rendering
//create window at 100, 100 with width 640 and height 480 and show it
SDL_Window *win = SDL_CreateWindow("Window", 100, 100, 640, 480, SDL_WINDOW_SHOWN);
//create a renderer that draws to win with hardware acceleration and vsync
SDL_Renderer *rend = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
//Load our image and convert to texture
SDL_Surface *img = SDL_LoadBMP("image.bmp");
//Create the texture, requires the rendering context so it knows what formatting we desire
SDL_Texture *tex = SDL_CreateTextureFromSurface(rend, img);
SDL_FreeSurface(img);
//Clear window
SDL_RenderClear(rend);
//Draw the texture the first NULL is the clip rect, the second NULL is the destination rect. Note that scaling w/h is supported!
//passing NULL destination rect will cause it to stretch the texture to fill screen
SDL_RenderCopy(rend, tex, NULL, NULL);
//Update screen
SDL_RenderPresent(rend);
//Old SDL method of rendering
SDL_Surface *surf = SDL_LoadBMP("image.bmp");
SDL_Surface *screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE) //and etc..
//draw with:
BlitSurface(surf, NULL, screen, NULL);
//And update screen
SDL_Flip(screen);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment