gpt4 book ai didi

c++ - 更改 SDL2 窗口的背景?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:06:46 25 4
gpt4 key购买 nike

我可以创建一个 SDL2 窗口,但我不知道如何更改此窗口的背景颜色。

我的代码:

#include "SDL.h"

SDL_Window *window;

void main()
{
window = SDL_CreateWindow("TEST", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);

SDL_Delay(3000);
}

如何将此窗口的背景颜色更改为黑色?

最佳答案

您应该使用 SDL_SetRenderDrawColor 设置绘图颜色,然后使用 SDL_RenderClear:

(代码直接来自SDL wiki)

int main(int argc, char* argv[])
{
SDL_Window* window;
SDL_Renderer* renderer;

// Initialize SDL.
if (SDL_Init(SDL_INIT_VIDEO) < 0)
return 1;

// Create the window where we will draw.
window = SDL_CreateWindow("SDL_RenderClear",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
512, 512,
0);

// We must call SDL_CreateRenderer in order for draw calls to affect this window.
renderer = SDL_CreateRenderer(window, -1, 0);

// Select the color for drawing. It is set to red here.
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);

// Clear the entire screen to our selected color.
SDL_RenderClear(renderer);

// Up until now everything was drawn behind the scenes.
// This will show the new, red contents of the window.
SDL_RenderPresent(renderer);

// Give us time to see the window.
SDL_Delay(5000);

// Always be sure to clean up
SDL_Quit();
return 0;
}

关于c++ - 更改 SDL2 窗口的背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30377708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com