小编iri*_*iac的帖子

正确地将 SDL 矩形渲染到屏幕上

这是我的代码,我正在关注SDL上的lazyfoos教程,这是我正在关注的确切教程 - http://lazyfoo.net/tutorials/SDL/08_geometry_rendering/index.php

请注意对 SDL_SetRenderDrawColor 的调用。我们使用 255 红色和 255 绿色,它们组合在一起形成黄色。还记得循环顶部对 SDL_SetRenderDrawColor 的调用吗?如果不存在,屏幕将被清除为最后使用 SDL_SetRenderDrawColor 设置的任何颜色,在这种情况下会导致黄色背景。

Lazyfoo 确实在上面解释了这一点,但对我来说仍然没有意义。

在屏幕上绘制一个填充的矩形是一项非常简单的任务,但有时会引起很多混乱,例如您需要调用 SDL_SetRenderDrawColor() 不是一次而是两次,一次在清除渲染器之前,也一次在清除渲染器之前您调用 SDL_RenderFillRect()。

为什么需要调用 SDL_SetRenderDrawColor() 两次以及为什么按这个顺序?我注意到,如果我在调用 SDL_RenderFillRect() 之前注释掉第一个 SDL_SetRenderDrawColor() ,整个窗口将是您设置矩形的颜色,但是当您按照我指定的顺序包含对 SDL_SetRenderDrawColor() 的两个调用时窗口在屏幕中央显示一个彩色矩形,屏幕的其余部分为白色(第一个 SDL_SetRenderDrawColor() 调用)。

这是我进行调用的游戏循环。

while( !quit )
{
    while( SDL_PollEvent( &event ) != 0 )
    {
        if( event.type == SDL_QUIT )
        {
            quit = true;

        }
    }

    SDL_SetRenderDrawColor( renderer, 255, 255, 255, 0 ); // line of code in question
    SDL_RenderClear( renderer );

    SDL_Rect fillRect = { 500 / 4, 500 / …
Run Code Online (Sandbox Code Playgroud)

c++ sdl game-development sdl-2

4
推荐指数
1
解决办法
4255
查看次数

标签 统计

c++ ×1

game-development ×1

sdl ×1

sdl-2 ×1