gpt4 book ai didi

c++ - SDL2 无法正确绘制矩形

转载 作者:行者123 更新时间:2023-11-30 02:18:13 26 4
gpt4 key购买 nike

我正在创建一个窗口并绘制一个框,但出于某种原因,屏幕没有绘制一个框,而是更改为该颜色。我附上了一张窗口外观的照片,我将附上源代码。

#include <iostream>
#include <SDL.h>
#undef main

using namespace std;
int SCREEN_WIDTH = 650;
int SCREEN_HEIGHT = 650;

int main() {
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;

return 1;
}

SDL_Window *window = SDL_CreateWindow("Cells", 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == nullptr) {
std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;

SDL_Quit();

return 1;
}

SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (renderer == nullptr) {
std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;

SDL_DestroyWindow(window);
SDL_Quit();

return 1;
}

SDL_Event event;
bool quit = false;

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

SDL_RenderClear(renderer);
// renderTextures
SDL_Rect fillRect = { 122, 122, 122, 122};
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderFillRect(renderer, &fillRect);
SDL_RenderPresent(renderer);
}

return 0;
}

Doesn't Draw Correctly

最佳答案

SDL_RenderClear使用您修改的当前绘制颜色,因此您的透明颜色和矩形颜色相同。使用例如设置不同的清晰颜色(您想要在没有绘制其他任何东西的背景中使用的颜色)

    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
// now draw your rectangles with different col

关于c++ - SDL2 无法正确绘制矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52490937/

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