Skip to content

Instantly share code, notes, and snippets.

@xaxxon
Created January 28, 2016 06:35
Show Gist options
  • Save xaxxon/40652451190a2dcbaa2a to your computer and use it in GitHub Desktop.
Save xaxxon/40652451190a2dcbaa2a to your computer and use it in GitHub Desktop.
#include <SDL.h> // include SDL header
int main(int argc, char* argv[])
{
SDL_Surface *screen; // even with SDL2, we can still bring ancient code back
SDL_Window *window;
SDL_Surface *image;
SDL_Init(SDL_INIT_VIDEO); // init video
// create the window like normal
window = SDL_CreateWindow("SDL2 Example", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
// but instead of creating a renderer, we can draw directly to the screen
screen = SDL_GetWindowSurface(window);
int format = screen->format->format;
printf("%d %d %d %d\n", SDL_PIXELTYPE(format), SDL_PIXELORDER(format), SDL_PIXELLAYOUT(format), SDL_BITSPERPIXEL(format));
printf("%d %d %d %d\n", SDL_BYTESPERPIXEL(format), SDL_ISPIXELFORMAT_INDEXED(format), SDL_ISPIXELFORMAT_ALPHA(format), SDL_ISPIXELFORMAT_FOURCC(format));
printf("blend mode result: %d\n", SDL_SetSurfaceBlendMode(screen, SDL_BLENDMODE_BLEND));
SDL_Surface *surface;
Uint32 rmask, gmask, bmask, amask;
/* SDL interprets each pixel as a 32-bit number, so our masks must depend
on the endianness (byte order) of the machine */
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif
surface = SDL_CreateRGBSurface(0, 100, 100, 32,
rmask, gmask, bmask, amask);
if(surface == NULL) {
fprintf(stderr, "CreateRGBSurface failed: %s\n", SDL_GetError());
exit(1);
}
SDL_FillRect(surface, NULL, SDL_MapRGBA(surface->format, 255, 0, 0, 128));
SDL_BlitSurface(surface, NULL, screen, NULL);
// let's just show some classic code for reference
// image = SDL_LoadBMP("box.bmp"); // loads image
// SDL_BlitSurface(image, NULL, screen, NULL); // blit it to the screen
// SDL_FreeSurface(image);
// this works just like SDL_Flip() in SDL 1.2
SDL_UpdateWindowSurface(window);
// show image for 2 seconds
SDL_Delay(4000);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment