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

标签 c++ macos sdl

我正在尝试在我的 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/

相关文章:

c++ - 与不带dynamic_pointer_cast的智能指针复合

c++ - 使用英特尔 MKL 和英特尔 IPP 的 FFT

c++ - 如何确保我可以读取该文件并且它没有被另一个进程锁定

macos - Mac OS 10.8 支持 GLSL 3.30?

c++ - 后缀运算符重载中虚拟参数的用途? C++

c++ - 在 C++ 中创建密码

objective-c - 在沙盒 OSX 应用程序中询问用户管理员密码

python - SDL/Pygame 无法使用 cx_Freeze 加载 PNG 图像

sdl - 什么是掩码值?

python - 如何在 Python 中抑制控制台输出?