C++ 什么是 SDL 渲染器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21007329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is an SDL renderer?
提问by prcastro
I'm starting with SDL2 and having some trouble trying to understand what an SDL_Renderer is.
我从 SDL2 开始,在试图理解 SDL_Renderer 是什么时遇到了一些麻烦。
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?
它是什么?它有什么作用?SDL_Renderer、SDL_Window、SDL_Surface 和 SDL_Texture 之间有什么区别以及它们之间的关系?
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.
我正在使用 Twinklebear 教程(在 SDL Wiki 上建议)并查看了 SDL Wiki 文档和一些书籍。但他们都假设我知道这些定义。
回答by olevegard
SDL_Window
SDL_Window
SDL_Window
is the struct that holds all info about the Window itself: size, position, full screen, borders etc.
SDL_Window
是保存有关窗口本身的所有信息的结构:大小、位置、全屏、边框等。
SDL_Renderer
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
SDL_Renderer
是一个处理所有渲染的结构体。它与 a 相关联,SDL_Window
因此只能在其中渲染SDL_Window
。它还跟踪与渲染相关的设置。有几个重要的功能与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 aboveSDL_RenderCopy(
This is probably the function you'll be using the most, it's used for rendering aSDL_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,
The part of the texture you want to render, NULL if you want to render the entire textureconst SDL_Rect* dstrect)
Where you want to render the texture in the window. If the width and height of thisSDL_Rect
is smaller or larger than the dimensions of the texture itself, the texture will be stretched according to thisSDL_Rect
SDL_RenderPresent(renderer);
The other SDL_Render* functions draws to a hidden target. This function will take all of that and draw it in the window tied to the renderer.
SDL_SetRenderDrawColor(renderer, r, g, b, a);
这会将您清除屏幕的颜色设置为(见下文)SDL_RenderClear(renderer);
这将使用上面设置的绘制颜色清除渲染目标SDL_RenderCopy(
这可能是您最常使用的函数,它用于渲染 aSDL_Texture
并具有以下参数:SDL_Renderer* renderer,
要用于渲染的渲染器。SDL_Texture* texture,
要渲染的纹理。const SDL_Rect* srcrect,
要渲染的纹理部分,如果要渲染整个纹理,则为 NULLconst SDL_Rect* dstrect)
要在窗口中渲染纹理的位置。如果 this 的宽度和高度SDL_Rect
小于或大于纹理本身的尺寸,则纹理将根据此拉伸SDL_Rect
SDL_RenderPresent(renderer);
其他 SDL_Render* 函数绘制到隐藏目标。此函数将获取所有这些并将其绘制在绑定到渲染器的窗口中。
SDL_Textures and SDL_Surface
SDL_Textures 和 SDL_Surface
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.
的SDL_Renderer
呈现SDL_Texture
,其存储一个元件的像素信息。它的新版本SDL_Surface
几乎相同。不同之处主要在于SDL_Surface
仅struct
包含像素信息,而SDL_Texture
像素数据的有效、特定于驱动程序的表示。
You can convert an SDL_Surface* to SDL_Texture using
您可以使用将 SDL_Surface* 转换为 SDL_Texture
SDL_Texture* SDL_CreateTextureFromSurface(SDL_Renderer* renderer,
SDL_Surface* surface)
After this, the SDL_Surface should be freed using
在此之后,应该使用释放 SDL_Surface
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).
另一个重要的区别是SDL_Surface
使用软件渲染(通过 CPU)而SDL_Texture
使用硬件渲染(通过 GPU)。
SDL_Rect
SDL_矩形
The simplest struct in SDL. It contains only four shorts. x, y
which holds the position and w, h
which holds width and height.
SDL 中最简单的结构。它只包含四条短裤。x, y
持有位置w, h
,持有宽度和高度。
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
需要注意的0, 0
是 SDL 中的左上角。所以更高的 -y
值意味着更低,并且右下角将有坐标x + w, y + h
You can read more about SDL2 on my blog.
回答by woolstar
Think of SDL_Window
as physical pixels, and SDL_Renderer
and a place to store settings/context.
将其SDL_Window
视为物理像素,以及SDL_Renderer
存储设置/上下文的位置。
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.
所以你创建了一堆资源,并将它们挂在渲染器上;然后当它准备好时,你告诉渲染器把它放在一起并将结果发送到窗口。