Skip to content

Instantly share code, notes, and snippets.

@KennFatt
Created September 18, 2019 02:42
Show Gist options
  • Select an option

  • Save KennFatt/c274382503b252e85d3c2eb8d639a31c to your computer and use it in GitHub Desktop.

Select an option

Save KennFatt/c274382503b252e85d3c2eb8d639a31c to your computer and use it in GitHub Desktop.
SDL2 Create Window & Renderer, Load BMP image.
#include <SDL2/SDL.h>
int throw_sdl_err(const char* fmt)
{
SDL_LogError(
SDL_LOG_CATEGORY_APPLICATION,
fmt,
SDL_GetError()
);
return 3; // constant error code.
}
int main(int argc, char** argv)
{
SDL_Window* window;
SDL_Renderer* renderer;
SDL_Surface* surface;
SDL_Texture* texture;
SDL_Event ev;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
return throw_sdl_err("Could not init the SDL: %s");
}
if (SDL_CreateWindowAndRenderer(
320, 280,
SDL_WINDOW_RESIZABLE,
&window, &renderer
)) { return throw_sdl_err("Could not create new window and renderer: %s"); }
surface = SDL_LoadBMP("white.bmp");
if (!surface) {
return throw_sdl_err("Could not load BMP image: %s");
}
texture = SDL_CreateTextureFromSurface(renderer, surface);
if (!texture) {
return throw_sdl_err("Could not create new texture from surface: %s");
}
SDL_FreeSurface(surface); // free a RGB surface (?)
while (1) {
SDL_PollEvent(&ev);
if (ev.type == SDL_QUIT) {
break;
} // break the loop
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
} // window loop
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
// vscode
// Linux 64-bit 5.2.14
// gcc 9.1.0
// libsdl 2.0.10-1
// flags: -Wall -lSDL2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment