What is an SDL renderer?

I'm starting with SDL2 and having some trouble trying to understand what an SDL_Renderer is.

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

I had issues with this when trying to understand this introductory 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 was using Twinklebear tutorial (suggested on SDL Wiki) and looked also on SDL Wiki Documentation and some books. But all of them assume that I know these definitions.

63722 次浏览

可以将 SDL_Window看作物理像素,将 SDL_Renderer看作存储设置/上下文的地方。

所以你创建了一堆资源,并把它们挂在渲染器上; 当它准备好时,你告诉渲染器把它们放在一起并把结果发送到窗口。

SDL _ Window

SDL_Window是一个结构,它包含了窗口本身的所有信息: 大小、位置、全屏、边框等等。


SDL_Renderer

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


  • This sets the color you clear the screen to ( see below )


  • 这将清除绘制颜色设置在

  • 之上的渲染目标

  • 这可能是你使用最多的函数,它用于呈现 SDL_Texture,并且有以下参数:

    • SDL_Renderer* renderer,
      要用于呈现的呈现程序。
    • SDL_Texture* texture,
      要呈现的纹理。
    • 要呈现的纹理部分,如果要呈现整个纹理,则为 NULL
    • const SDL_Rect* dstrect)
      要在窗口中呈现纹理的位置。如果此 SDL_Rect的宽度和高度小于或大于纹理本身的尺寸,则纹理将根据此 SDL_Rect进行拉伸
  • SDL_RenderPresent(renderer);
    其他 SDL _ Render * 函数绘制到隐藏目标。这个函数将获取所有这些信息,并在与渲染器绑定的窗口中绘制它。

SDL_Textures and SDL_Surface

SDL_Renderer呈现 SDL_Texture,它存储一个元素的像素信息。这是 SDL_Surface的新版本,也差不多。区别主要在于 SDL_Surface只是一个包含像素信息的 struct,而 SDL_Texture是一个高效的、特定于驱动程序的像素数据表示。

您可以使用以下方法将 SDL _ Surface * 转换为 SDL _ Texture

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

在此之后,应该使用

SDL_FreeSurface( SDL_Surface* surface )

另一个重要的区别是,SDL_Surface使用软件渲染(通过 CPU) ,而 SDL_Texture使用硬件渲染(通过 GPU)。


SDL _ Rect

SDL 中最简单的结构。里面只有四条短裤。保持位置的 x, y和保持宽度和高度的 w, h

值得注意的是,0, 0是 SDL 中的左上角。因此,较高的 y值意味着较低,并且右下角将有坐标 x + w, y + h


你可以在我的 博客。上阅读更多关于 SDL2的内容