gpt4 book ai didi

c++ - 无法在 SDL2 (MAC M1) 中正确渲染三角形

转载 作者:行者123 更新时间:2023-12-05 04:26:47 31 4
gpt4 key购买 nike

我正在尝试在我的 MAC (M1) 上使用 SDL2 渲染一个三角形,但是,我能够生成的三角形像素化太多,并且正在渲染不必要的像素。

输出:

enter image description here

我的代码:

int main(int argc, char *argv[])
{
// returns zero on success else non-zero
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
printf("error initializing SDL: %s\n", SDL_GetError());
}
SDL_Window* win = SDL_CreateWindow("GAME",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
900, 900, SDL_WINDOW_ALLOW_HIGHDPI);
if(!win)
{
std::cout << "Failed to create window\n";
return -1;
}

int *w_t = new int ();
int *h_t = new int ();

SDL_GetWindowSize(win,w_t,h_t);

std::cout<<"width:"<<*w_t<<"height:"<<*h_t;

SDL_Renderer* brush = SDL_CreateRenderer(win, -1, 0);

SDL_SetRenderDrawColor(brush, 255, 0, 0, 0);
SDL_Point a = {0,500};
SDL_Point b = {800,500};
SDL_Point c = {250,250};
bool quit = false;
SDL_Event e;
while (!quit) {
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
quit = true;
}
}
SDL_RenderDrawLine(brush, a.x, a.y, b.x, b.y);
SDL_RenderDrawLine(brush, a.x, a.y, c.x, c.y);
SDL_RenderDrawLine(brush, b.x, b.y, c.x, c.y);
SDL_RenderPresent(brush);
}


return 0;
}
  • 操作系统:macOS Monterey
  • 集成开发环境:Xcode
  • 编译器:Clang

最佳答案

尝试在绘制线条之前清除渲染器:

SDL_SetRenderDrawColor(brush, 0, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderClear(brush);

SDL_SetRenderDrawColor(brush, 255, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderDrawLine(brush, a.x, a.y, b.x, b.y);
SDL_RenderDrawLine(brush, a.x, a.y, c.x, c.y);
SDL_RenderDrawLine(brush, b.x, b.y, c.x, c.y);
SDL_RenderPresent(brush);

关于c++ - 无法在 SDL2 (MAC M1) 中正确渲染三角形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72943770/

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