C语言 使用 SDL2 绘制矩形
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21890627/
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
Drawing a rectangle with SDL2
提问by Utundu
I just started using SDL2 and I already have a problem. I want to create a window and paint it in red. But it remains white, and I don't understand why.
我刚开始使用 SDL2,但我已经遇到了问题。我想创建一个窗口并将其涂成红色。但它仍然是白色的,我不明白为什么。
Here is the code :
这是代码:
int main (int argc, char** argv) {
SDL_Window* pWindow = NULL;
pWindow = SDL_CreateWindow("Jeu de la vie", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
640,
480,
SDL_WINDOW_SHOWN);
SDL_Surface* pSurface = NULL;
pSurface = SDL_GetWindowSurface(pWindow);
SDL_FillRect(pSurface, NULL, SDL_MapRGB(pSurface->format, 255, 0, 0));
while(1);
SDL_FreeSurface(pSurface);
SDL_DestroyWindow(pWindow);
SDL_Quit();
return EXIT_SUCCESS;
}
回答by olevegard
There are several issues with your code, I'll try to address most of them.
您的代码有几个问题,我将尝试解决其中的大部分问题。
Initialize SDL
初始化 SDL
SDLand SDL2needs to be initialized before you can use it. The way to initialize SDL is the following function.
SDL并且SDL2需要初始化后才能使用。初始化 SDL 的方法是以下函数。
int SDL_Init(Uint32 flags)
Where flagscan be a different value for different subsystems. Use SDL_INIT_EVERYTHING, to initialize everything.
flags对于不同的子系统,Where可以是不同的值。使用SDL_INIT_EVERYTHING, 来初始化一切。
int SDL_Init(SDL_INIT_EVERYTHING)
Read more about it here..
Initialize SDL_Windowand SDL_Renderer
初始化SDL_Window和SDL_Renderer
SDL_Rendererand SDL_Windowneeds to be set up before you can use them. You already create your window properly, so I won't cover that. Here's how to set up an SDL_Renderer
SDL_Renderer并且SDL_Window需要先进行设置,然后才能使用它们。你已经正确地创建了你的窗口,所以我不会覆盖它。以下是如何设置SDL_Renderer
SDL_Renderer* SDL_CreateRenderer(SDL_Window* window,
int index,
Uint32 flags)
indexdefines what driver to use. Set it to -1to use the first driver that supports the other arguments. flagsare used to make the rendering optimized, software rendiring, prevent vsync, etc.. Set it to SDL_RENDERER_ACCELERATED.
index定义要使用的驱动程序。将其设置-1为使用支持其他参数的第一个驱动程序。flags用于使渲染优化、软件渲染、防止 vsync 等。将其设置为SDL_RENDERER_ACCELERATED.
Read more about SDL_CreateRendererhere.
阅读更多关于SDL_CreateRenderer这里。
Mixing SDLand SDL2
混合SDL和SDL2
SDL_Surfaceis primarily something used in SDL, not SDL2. SDL2_image, SDL2_ttfetc still used SDL_Surface, but they are converted into an SDL_Texturebefore they can be used.
SDL_Surface主要是在 中使用的东西SDL,而不是SDL2. SDL2_image, SDL2_ttfetc 仍然使用SDL_Surface,但SDL_Texture在它们可以使用之前它们被转换为 an 。
SDL_FillRect(...);is also mostly a SDLthing. But as stated above, the SDL_Surfacecan be used, but you need to convert it to an SDL_Texturefirst :
SDL_FillRect(...);也大多是SDL一回事。但是如上所述,SDL_Surface可以使用 ,但您需要将其转换为SDL_Texture第一个:
SDL_Texture* SDL_CreateTextureFromSurface(SDL_Renderer* renderer,
SDL_Surface* surface)
Read more here.
在这里阅读更多。
And use
并使用
int SDL_RenderCopy(SDL_Renderer* renderer,
SDL_Texture* texture,
const SDL_Rect* srcrect,
const SDL_Rect* dstrect)
To render it, read more here.
要渲染它,请在此处阅读更多内容。
Infinite loop ( while(1);)
无限循环 ( while(1);)
You REALLY shouldn't do this, it'll just loop forever. Use SDL_Delay( 5000 );to pause for 5000msec or 5 seconds.
你真的不应该这样做,它会永远循环。使用SDL_Delay( 5000 );暂停为5000msec或5秒。
A simpler way
更简单的方法
You can use
您可以使用
int SDL_RenderDrawRect(SDL_Renderer* renderer,
const SDL_Rect* rect)
To draw a rect.
画一个矩形。
You should use
你应该使用
int SDL_SetRenderDrawColor(SDL_Renderer* renderer,
Uint8 r,
Uint8 g,
Uint8 b,
Uint8 a)
To set the color of what you are drawing, use
要设置您正在绘制的颜色,请使用
int SDL_RenderClear(SDL_Renderer* renderer)
After which you call your SDL_RenderDrawRect()
之后你打电话给你 SDL_RenderDrawRect()
Up until this point, everything has been drawn "behind the scenes." To render it to the screen, use
到目前为止,一切都已在“幕后”绘制。要将其渲染到屏幕,请使用
void SDL_RenderPresent(SDL_Renderer* renderer)
Example
例子
#include <SDL2/SDL.h>
int main (int argc, char** argv)
{
SDL_Window* window = NULL;
window = SDL_CreateWindow
(
"Jeu de la vie", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
640,
480,
SDL_WINDOW_SHOWN
);
// Setup renderer
SDL_Renderer* renderer = NULL;
renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED);
// Set render color to red ( background will be rendered in this color )
SDL_SetRenderDrawColor( renderer, 255, 0, 0, 255 );
// Clear winow
SDL_RenderClear( renderer );
// Creat a rect at pos ( 50, 50 ) that's 50 pixels wide and 50 pixels high.
SDL_Rect r;
r.x = 50;
r.y = 50;
r.w = 50;
r.h = 50;
// Set render color to blue ( rect will be rendered in this color )
SDL_SetRenderDrawColor( renderer, 0, 0, 255, 255 );
// Render rect
SDL_RenderFillRect( renderer, &r );
// Render the rect to the screen
SDL_RenderPresent(renderer);
// Wait for 5 sec
SDL_Delay( 5000 );
SDL_DestroyWindow(window);
SDL_Quit();
return EXIT_SUCCESS;
}
Result
结果
You can read more about SDL2 on my blog.


