Also note that the SDL renderer does not have to rely on OpenGL (it may use Direct3D on Windows).
A structure that contains an efficient, driver-specific representation of pixel data.
An SDL surface is just an image data type that contains the pixels of an image along with all data needed to render it. SDL surfaces use software rendering which means it uses the CPU to render. It is possible to render hardware images but it's a bit more difficult so we're going to learn it the easy way first.
SDL_Window
is the struct that holds all info about the Window itself: size, position, full screen, borders etc.
SDL_Renderer
is a struct that handles all rendering. It is tied to a SDL_Window
so it can only render within that SDL_Window
. It also keeps track the settings related to the rendering. There are several important functions tied to the SDL_Renderer
SDL_SetRenderDrawColor(renderer, r, g, b, a);
This sets the color you clear the screen to ( see below )
SDL_RenderClear(renderer);
This clears the rendering target with the draw color set above
SDL_RenderCopy(
This is probably the function you'll be using the most, it's used for rendering a SDL_Texture
and has the following parameters :
SDL_Renderer* renderer,
SDL_Texture* texture,
const SDL_Rect* srcrect,
The part of the texture you want to render, NULL if you want to render the entire texture const SDL_Rect* dstrect)
SDL_Rect
is smaller or larger than the dimensions of the texture itself, the texture will be stretched according to this SDL_Rect
SDL_RenderPresent(renderer);
The SDL_Renderer
renders SDL_Texture
, which stores the pixel information of one element. It's the new version of SDL_Surface
which is much the same. The difference is mostly that SDL_Surface
is just a struct
containing pixel information, while SDL_Texture
is an efficient, driver-specific representation of pixel data.
You can convert an SDL_Surface* to SDL_Texture using
SDL_Texture* SDL_CreateTextureFromSurface(SDL_Renderer* renderer,
SDL_Surface* surface)
After this, the SDL_Surface should be freed using
SDL_FreeSurface( SDL_Surface* surface )
Another important difference is that SDL_Surface
uses software rendering (via CPU) while SDL_Texture
uses hardware rendering (via GPU).
The simplest struct in SDL. It contains only four shorts. x, y
which holds the position and w, h
which holds width and height.
It's important to note that 0, 0
is the upper-left corner in SDL. So a higher y
-value means lower, and the bottom-right corner will have the coordinate x + w, y + h
You can read more about SDL2 on my blog.
Think of SDL_Window
as physical pixels, and SDL_Renderer
and a place to store settings/context.
So you create a bunch of resources, and hang them off of the renderer; and then when its ready, you tell renderer to put it all together and send the results to the window.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With