Skip to content

Instantly share code, notes, and snippets.

@armornick
Created August 23, 2012 08:47
Show Gist options
  • Save armornick/3434362 to your computer and use it in GitHub Desktop.
Save armornick/3434362 to your computer and use it in GitHub Desktop.
Draw an Image with SDL2
#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#define WIDTH 800
#define HEIGHT 600
#define IMG_PATH "exit.png"
int main (int argc, char *argv[]) {
// variable declarations
SDL_Window *win = NULL;
SDL_Renderer *renderer = NULL;
SDL_Texture *img = NULL;
int w, h; // texture width & height
// Initialize SDL.
if (SDL_Init(SDL_INIT_VIDEO) < 0)
return 1;
// create the window and renderer
// note that the renderer is accelerated
win = SDL_CreateWindow("Image Loading", 100, 100, WIDTH, HEIGHT, 0);
renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
// load our image
img = IMG_LoadTexture(renderer, IMG_PATH);
SDL_QueryTexture(img, NULL, NULL, &w, &h); // get the width and height of the texture
// put the location where we want the texture to be drawn into a rectangle
// I'm also scaling the texture 2x simply by setting the width and height
SDL_Rect texr; texr.x = WIDTH/2; texr.y = HEIGHT/2; texr.w = w*2; texr.h = h*2;
// main loop
while (1) {
// event handling
SDL_Event e;
if ( SDL_PollEvent(&e) ) {
if (e.type == SDL_QUIT)
break;
else if (e.type == SDL_KEYUP && e.key.keysym.sym == SDLK_ESCAPE)
break;
}
// clear the screen
SDL_RenderClear(renderer);
// copy the texture to the rendering context
SDL_RenderCopy(renderer, img, NULL, &texr);
// flip the backbuffer
// this means that everything that we prepared behind the screens is actually shown
SDL_RenderPresent(renderer);
}
SDL_DestroyTexture(img);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(win);
return 0;
}
@SH2282000
Copy link

That's better than any fucked explanations!

@andraantariksa
Copy link

@thesonpb
Copy link

thesonpb commented Apr 6, 2020

Better than any code i've ever seen!!

@sucrecacao
Copy link

How would you compile it ?

@SH2282000
Copy link

@sucrecacao
Copy link

https://gist.github.com/armornick/3434362#gistcomment-3409065

Use gcc?

Yes but what would be the full command, gcc filename.c doesn't work you need to include the library.
For me what worked was:

export LD_LIBRARY_PATH=/usr/local/lib
gcc file.c -I/usr/local/include -L/usr/local/lib -lSDL2 -lSDL2_image

@xyproto
Copy link

xyproto commented Aug 3, 2021

If you're on Arch Linux, here are two ways of compiling the above program.

Manually

gcc openicon.c -o openicon -O2 -pipe -fPIC -fno-plt -fstack-protector-strong -I/usr/include/SDL2 -D_GNU_SOURCE -D_REENTRANT -Wl,--as-needed -lSDL2 -lSDL2_image

Using CXX

If you have cxx installed, just run:

cxx

@obirn
Copy link

obirn commented Oct 20, 2021

i love you

@Elxde
Copy link

Elxde commented Mar 18, 2022

this is the holy grail of sdl coding <:

@Sunchock
Copy link

Remember that SDL2_image is an extension of SDL2 and must be downloaded and installed separately!

@PotatoGobbler
Copy link

You can also try Visual C++ (but you'll have to deal with the settings).

@nikeedev
Copy link

nikeedev commented Sep 6, 2022

This guy has published the code 10 years ago, but still, it's better than any tutorial or website.

@nikeedev
Copy link

nikeedev commented Sep 6, 2022

That's better than any fucked explanations!

accurate 👍

@ibgp2
Copy link

ibgp2 commented Dec 6, 2022

Thanks for the gist, it really helped me. Possible improvements:

  • To center the image:
    SDL_Rect rectangle;
    SDL_QueryTexture(texture, NULL, NULL, &rectangle.w, &rectangle.h);
    rectangle.x = (WIDTH - rectangle.w) / 2;
    rectangle.y = (HEIGHT - rectangle.h) / 2;
    while (1) {
        // https://stackoverflow.com/questions/12770098/how-to-keep-the-cpu-usage-down-while-running-an-sdl-program
        SDL_Event e;
        if (SDL_WaitEvent(&e)) {
            if (e.type == SDL_QUIT)
                break;
            else if (e.type == SDL_KEYUP && e.key.keysym.sym == SDLK_ESCAPE)
                break;
        }
        
        SDL_RenderClear(renderer);
        SDL_RenderCopy(renderer, texture, NULL, &rectangle);
        SDL_RenderPresent(renderer);
    }
  • Do not forget to free the surface in the end
    SDL_DestroyTexture(texture);
    SDL_FreeSurface(surface);
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(win);

@mew
Copy link

mew commented Dec 8, 2022

saved my life i'm gonna pass my course now thanks my brother

@nikeedev
Copy link

nikeedev commented Dec 8, 2022

Thanks for the gist, it really helped me. Possible improvements:

  • To center the image:
    SDL_Rect rectangle;
    SDL_QueryTexture(texture, NULL, NULL, &rectangle.w, &rectangle.h);
    rectangle.x = (WIDTH - rectangle.w) / 2;
    rectangle.y = (HEIGHT - rectangle.h) / 2;
    while (1) {
        // https://stackoverflow.com/questions/12770098/how-to-keep-the-cpu-usage-down-while-running-an-sdl-program
        SDL_Event e;
        if (SDL_WaitEvent(&e)) {
            if (e.type == SDL_QUIT)
                break;
            else if (e.type == SDL_KEYUP && e.key.keysym.sym == SDLK_ESCAPE)
                break;
        }
        
        SDL_RenderClear(renderer);
        SDL_RenderCopy(renderer, texture, NULL, &rectangle);
        SDL_RenderPresent(renderer);
    }
  • Do not forget to free the surface in the end
    SDL_DestroyTexture(texture);
    SDL_FreeSurface(surface);
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(win);

You write some more code that could be added and changed later, for now the gist code is for setting up fast a project. But you believe too, that it is a good gist to use?

@ibgp2
Copy link

ibgp2 commented Dec 9, 2022

You write some more code that could be added and changed later, for now the gist code is for setting up fast a project. But you believe too, that it is a good gist to use?

IMHO, the gist is good once you apply the changes I suggested. The code will remain short because this is mainly updating few lines of the existing code. In the detail:

  1. The first change (SDL_rect) is cosmetic. Without doing this, the image is in general not centered
  2. The second change (SDL_WaitEvent) is very important. If you use SDL_PollEvent, the program uses 100% CPU (because it permanently refreshes the displayed image, which is useless). With the change I suggest, this problem disappears
  3. The third change (SDL_FreeSurface) is recommended because otherwise you might have a memory leak as you don't destroy the object.

Copy link

ghost commented Oct 4, 2023

WaitEvent, waits for events, which is more for applications than games, because if you use it in a game, the entire game would hang until it gets an event, so there is no definitive option, hence you can choose

the CPU of the example can be very easly bypassed while still using poll events, just add an SDL_Delay
best also to explain that if someone were to make a game using this, that itd be best to use PollEvents with a frame-limiter/vsync

@nikeedev
Copy link

nikeedev commented Apr 5, 2024

i love you

gay

How the heck is he or even she gay by loving someone??

@SH2282000
Copy link

SH2282000 commented May 2, 2024

Damn, why are all those people still here. Bros need to get a life.

Go contribute to some open-source projects!

@nikeedev
Copy link

nikeedev commented May 3, 2024

i love you

gay

How the heck is he or even she gay by loving someone??

i was joking

very funny

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment