c++ - SDL2 抗锯齿

标签 c++ opengl sdl antialiasing

使用 SDL_RenderCopyEx 时如何在 SDL2 中打开抗锯齿?

我找到一些建议使用的文章:

SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 2);

glEnable(GL_MULTISAMPLE);

但这没有任何效果。有什么想法吗?

int Buffers, Samples;
SDL_GL_GetAttribute( SDL_GL_MULTISAMPLEBUFFERS, &Buffers );
SDL_GL_GetAttribute( SDL_GL_MULTISAMPLESAMPLES, &Samples );
cout << "buf = " << Buffers << ", samples = " << Samples;

返回

buf = -858993460,样本 = -858993460。

编辑:代码:

   #include <windows.h>
#include <iostream>

#include <SDL2/include/SDL.h>
#include <SDL2/include/SDL_image.h>

using namespace std;


int main( int argc, char * args[] )
{
    // Inicjacja SDL'a
    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
    SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 8);

    SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); 

    // Tworzenie okna
    SDL_Window *win = nullptr;
    win = SDL_CreateWindow("abc", 100, 100, 800, 600, SDL_WINDOW_FULLSCREEN | SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);

    if (win == nullptr) 
    {
        std::cout << SDL_GetError() << std::endl;
        system("pause");
        return 1;
    }


    int Buffers, Samples;
    SDL_GL_GetAttribute( SDL_GL_MULTISAMPLEBUFFERS, &Buffers );
    SDL_GL_GetAttribute( SDL_GL_MULTISAMPLESAMPLES, &Samples );
    cout << "buf = " << Buffers << ", samples = " << Samples << ".";

    // Create Renderer
    SDL_Renderer *ren = nullptr;
    ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);

    if (ren == nullptr)
    {
        std::cout << SDL_GetError() << std::endl;
        return 1;
    }

    // Create texture
    SDL_Texture *tex = nullptr;
    tex = IMG_LoadTexture(ren, "circle.png");

    SDL_SetTextureAlphaMod(tex, 100);

    SDL_Rect s,d;
    SDL_Point c;
    s.x = s.y = 0;
    s.w = s.h = 110;

    d.x = 320;
    d.y = 240;
    d.w = d.h = 110;

    c.x = c.y = 55;

    // Event Queue
    SDL_Event e;
    bool quit = false;
    int angle = 0;

    while(!quit)
    {
        while (SDL_PollEvent(&e)){
            //If user closes he window
            if (e.type == SDL_KEYDOWN)
                quit = true;
        }
        angle += 2;

        float a = (angle/255.0)/M_PI*180.0;

        // Render
        SDL_RenderClear(ren);
        SDL_RenderCopyEx(ren, tex, &s, &d, a, &c, SDL_FLIP_NONE);
        SDL_RenderPresent(ren);


    }

    // Release
    SDL_DestroyTexture(tex);
    SDL_DestroyRenderer(ren);
    SDL_DestroyWindow(win);

    // Quit
    SDL_Quit();

    return 0;
}

不要担心与内存释放等相关的样式或错误。这是测试 SDL'a 可能性的快速草图

最佳答案

如果您正在寻找不需要使用 opengl 的答案,那么这可能会有用:

SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" );

https://wiki.libsdl.org/SDL_HINT_RENDER_SCALE_QUALITY

关于c++ - SDL2 抗锯齿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19859891/

相关文章:

c++ - 如何知道连接到 QTcpServer 的客户端是否已关闭连接?

java - 渲染和眼空间未对准导致光线转换不精确

c++ - glutSpecialFunc 只捕获一些键

ffmpeg - 可以在使用 libavcodec 解码期间裁剪帧大小吗?

c++ - C++中的屏幕分辨率

C++ - 混合默认成员初始值设定项和成员初始化列表 - 坏主意?

C++:局部范围内的类属性

c++ - 函数和数组有问题

c++ - 为什么 GLUT_DEPTH 破坏了我旋转茶壶的渲染?

c++ - 如何修复 'The procedure entry point SDL_RWclose could not be located in the dynamic link library'