问题描述
我不太明白渲染器是什么.我可以有多个渲染器还是总是只有一个?
I dont exactly understand what renderer is. Can I have multiple renderers or is there always just one?
例如,如何使用渲染器在不同颜色的背景上绘制具有特定颜色的矩形?
For example, how can I draw a rectangle with a certain color on a background with a different color using a renderer?
我相信答案在于函数SDL_RenderDrawRect()
和SDL_RenderFillRect()
.我说得对吗?
I believe the answer lies in the functions SDL_RenderDrawRect()
and SDL_RenderFillRect()
. Am I right?
我知道表面和 bliting 是如何工作的,但我不知道渲染器究竟象征什么.
I know how surfaces and bliting works but I dont know what exactly the renderer symbolizes.
如果有人能告诉我如何绘制矩形,我想我会理解渲染器的工作原理.
If someone could show me how to draw a rectangle, I think i will understand how renderers work.
到目前为止我有这个:
#include <SDL.h>
int main(int argc, char* argv[]) {
//Initialization
SDL_Init(SDL_INIT_EVERYTHING);
//Window
SDL_Window *MainWindow = SDL_CreateWindow("My Game Window",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
640, 480,
SDL_WINDOW_SHOWN
);
//Renderer
SDL_Renderer *Background = SDL_CreateRenderer(MainWindow, -1, 0);
SDL_SetRenderDrawColor(Background, 255, 255, 255, 255);
SDL_RenderClear(Background);
SDL_Delay(3000);
//Clean up
SDL_DestroyWindow(MainWindow);
SDL_Quit();
return 0;
}
推荐答案
有关问题的第一部分,请参阅 这个问题.
for the first part of your question see this SO question.
至于为什么你的代码没有做太多:
as to why your code doesnt do much:
您是正确的,您需要使用 SDL_RenderDrawRect()
或 SDL_RenderFillRect()
.SDL_RenderDrawRect
将绘制一个未填充的矩形.SDL_RenderFillRect
将被填充(希望这是显而易见的).
you are correct that you need to use either SDL_RenderDrawRect()
, or SDL_RenderFillRect()
.SDL_RenderDrawRect
will draw an unfilled rectangle. SDL_RenderFillRect
will be filled (hopefully that is obvious).
使用SDL_renderer
,您需要调用SDL_RenderPresent
将场景"复制到屏幕上.
With SDL_renderer
you need to call SDL_RenderPresent
to copy the "scene" to the screen.
...
//Renderer
SDL_Renderer* renderer = SDL_CreateRenderer(MainWindow, -1, 0);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderClear(renderer); // fill the scene with white
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); // the rect color (solid red)
SDL_Rect rect(0, 0, 100, 50); // the rectangle
SDL_RenderFillRect(renderer, &rect);
SDL_RenderPresent(renderer); // copy to screen
SDL_Delay(3000);
...
这篇关于SDL2 中的渲染器究竟是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!