gpt4 book ai didi

c++ - 无法使用 SDL2 gfx 绘制实心圆

转载 作者:行者123 更新时间:2023-11-30 03:53:23 25 4
gpt4 key购买 nike

有如下代码。期望的结果是创建一个窗口并绘制一个实心圆:

#include <SDL2/SDL.h>
#include <iostream>
#include <SDL2/SDL2_gfxPrimitives.h>

int main()
{
SDL_Window* window = nullptr;
SDL_Renderer* renderer = nullptr;

if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
std::cout << "Could not initialise" << std::endl;
return 1;
}

window = SDL_CreateWindow("MyGame",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
640,
480,
SDL_WINDOW_SHOWN);
if(!window)
{
std::cout << "Could not create the window" << std::endl;
return 1;
}

renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_RenderClear(renderer);

Sint16 circleR = 100;
Sint16 circleX = 300;
Sint16 circleY = 300;
SDL_Surface* windowSurface = SDL_GetWindowSurface(window);
Uint32 circleColour = SDL_MapRGB(windowSurface->format, 255, 0, 0);

int result = filledCircleColor(renderer, circleX, circleY, circleR, circleColour);

std::cout << "drawing the circle r " << circleR << " x " << circleX << " y " << circleY << " circleColour " << circleColour << std::endl;
std::cout << "draw circle result " << result << std::endl;

SDL_RenderPresent(renderer);

bool run = true;
while(run)
{
SDL_Event event;
while (SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
run = false;
}
}

}

SDL_Quit();
return 0;
}

问题是圆没有画出来——窗口全黑了。

输出:

drawing the circle r 100 x 300 y 300 circleColour 16711680
draw circle result 0

filledCircleColor 返回 0,这意味着没有错误。

应该怎样做才能画出圆?我在带有 SDL2 gfx 扩展的 Ubuntu 上使用 SDL 2.0.2。

最佳答案

关于 SDL_GetWindowSurfacethe documentation说:“您不能在此窗口上将其与 3D 或渲染 API 结合使用。”

对我来说,在您的示例中,SDL_GetWindowSurface 返回 null。

filledCircleColor()函数采用 0xRRGGBBAA 形式的颜色。

移除表面部分后,它会工作,并更改为:

int result = filledCircleColor(renderer, circleX, circleY, circleR, 0xFF0000FF);

关于c++ - 无法使用 SDL2 gfx 绘制实心圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30133297/

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