What is an SDL render?

I start with SDL2 and am experiencing some problems trying to figure out what SDL_Renderer is.

What is it? What does it do? What is the difference between SDL_Renderer, SDL_Window, SDL_Surface and SDL_Texture and how they are related?

I had problems with this while trying to understand this input code:

#include <iostream> #include <SDL2/SDL.h> int main() { /* Starting SDL */ if (SDL_Init(SDL_INIT_EVERYTHING) != 0) { std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl; return 1; } /* Create a Window */ SDL_Window *window = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN); if (window == nullptr) { std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl; return 1; } /* Create a Render */ SDL_Renderer *render = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); if (render == nullptr) { std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl; return 1; } /* Load bitmap image */ SDL_Surface *bmp = SDL_LoadBMP("./Tutorial/res/Lesson1/hello.bmp"); if (bmp == nullptr) { std::cout << "SDL_LoadBMP Error: " << SDL_GetError() << std::endl; return 1; } /* Upload surface to render, and then, free the surface */ SDL_Texture *texture = SDL_CreateTextureFromSurface(render, bmp); SDL_FreeSurface(bmp); if (texture == nullptr){ std::cout << "SDL_CreateTextureFromSurface Error: " << SDL_GetError() << std::endl; return 1; } /* Draw the render on window */ SDL_RenderClear(render); // Fill render with color SDL_RenderCopy(render, texture, NULL, NULL); // Copy the texture into render SDL_RenderPresent(render); // Show render on window /* Wait 2 seconds */ SDL_Delay(5000); /* Free all objects*/ SDL_DestroyTexture(texture); SDL_DestroyRenderer(render); SDL_DestroyWindow(window); /* Quit program */ SDL_Quit(); return 0; } 

I used the Twinklebear tutorial (suggested on the SDL Wiki) and also looked at the SDL Wiki documentation and some books. But they all suggest that I know these definitions.

+73
c ++ sdl sdl-2
Jan 08
source share
2 answers

SDL_Window

SDL_Window is a structure that contains all the information about the window itself: size, position, full screen, borders, etc.




SDL_Renderer

SDL_Renderer is a structure that handles all rendering. It is bound to SDL_Window , so it can only display inside SDL_Window . It also keeps track of settings related to rendering. There are several important functions related to SDL_Renderer

  • SDL_SetRenderDrawColor(renderer, r, g, b, a);
    This sets the color to which you clear the screen (see below).

  • SDL_RenderClear(renderer);
    This clears the render target with the drawing level set above.

  • SDL_RenderCopy(
    This is probably the function that you will use the most, it is used to render SDL_Texture and has the following parameters:

    • SDL_Renderer* renderer,
      The renderer you want to use for rendering.
    • SDL_Texture* texture,
      The texture you want to render.
    • const SDL_Rect* srcrect, part of the texture you want to render is NULL if you want to display the entire texture
    • const SDL_Rect* dstrect)
      Where do you want to visualize the texture in the window. If the width and height of this SDL_Rect smaller or larger than the size of the texture itself, the texture will be stretched in accordance with this SDL_Rect

  • SDL_RenderPresent(renderer);
    Other functions of SDL_Render * refer to a hidden target. These functions will take it all and draw in the window associated with the renderer.



SDL_Textures and SDL_Surface

SDL_Renderer , displays the SDL_Texture . SDL_Texture is the pixel information of one element. This is the new version of SDL_Surface , which is almost the same. The main difference is that SDL_Surface is just a struct containing pixel information, while SDL_Texture is an efficient, driver-specific representation of the pixel data.

You can convert SDL_Surface * to SDL_Texture using

 SDL_Texture* SDL_CreateTextureFromSurface(SDL_Renderer* renderer, SDL_Surface* surface) 

After that, SDL_Surface should be freed using

 SDL_FreeSurface( SDL_Surface* surface ) 



SDL_Rect

The simplest structure in SDL. It contains only four shorts. x, y , which holds the position, and w, h , which has a width and height.

It is important to note that 0, 0 is the upper left corner in the SDL. Thus, a higher y value means a lower value, and the lower right corner will have the coordinate x + w, y + h




Read more about SDL2 on the blog.

+139
Jan 08 '14 at
source share

Think of SDL_Window as physical pixels and SDL_Renderer and where the settings / context is stored.

So, you create a bunch of resources and disconnect them from rendering; and then when it's ready, you tell the renderer to put it all together and send the results to the window.

+4
Jan 08
source share



All Articles