SDL2 AntiAliasing
SDL2 AntiAliasing
本文关键字:AntiAliasing SDL2 更新时间:2023-10-16
使用 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
据我所知,在创建上下文之前不会设置值,因此如果您在创建窗口之前运行SDL_GL_GetAttribute
行,您将像目前一样返回未初始化的值。
因此,要获得正确的值,请在创建上下文后使用 SDL_GL_GetAttribute
调用,它应该可以正常工作。
让我知道你过得怎么样,如果你需要更多的帮助/信息,我会尽我所能提供帮助。
补遗:
您看起来在设置其属性之前已经创建了窗口,我已经粘贴了一些修改后的代码,这些代码应该运行良好(抱歉,在我访问家用PC之前我无法对其进行测试)。
重新排列的代码:
#include <windows.h>
#include <iostream>
#include <SDL2/include/SDL.h>
#include <SDL2/include/SDL_image.h>
#include <gl/include/glew.h>
using namespace std;
void myInit()
{
// SDL Init
SDL_Init(SDL_INIT_EVERYTHING);
// Settings
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 2);
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
glEnable(GL_MULTISAMPLE);
}
int main( int argc, char * args[] )
{
myInit();
// Window Create
SDL_Window *win = nullptr;
win = SDL_CreateWindow("abc", 100, 100, 800, 600, SDL_WINDOW_SHOWN);
if(win == nullptr) return 1;
// Create Renderer
SDL_Renderer *ren = nullptr;
ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (ren == nullptr) 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 texture
SDL_Texture *tex = nullptr;
tex = IMG_LoadTexture(ren, "circle.png");
SDL_SetTextureAlphaMod(tex, 100);
SDL_SetTextureColorMod(tex, 255,0,0);
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 = 220;
c.x = c.y = 110;
// Event Queue
SDL_Event e;
bool quit = false;
int angle = 45.0*M_PI/180;
while(!quit)
{
while (SDL_PollEvent(&e)){
//If user closes the window
if (e.type == SDL_QUIT)
quit = true;
}
// Render
SDL_RenderClear(ren);
SDL_RenderCopyEx(ren, tex, &s, &d, angle, &c, SDL_FLIP_NONE);
SDL_RenderPresent(ren);
}
// Release
SDL_DestroyTexture(tex);
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
// Quit
SDL_Quit();
return 0;
}
相关文章:
- 如何创建事件驱动的 SDL2 应用程序
- 使用 SDL2 和SDL_image C++加载图像
- 将 SDL2 与 CMake 链接起来
- SDL2 透明背景
- 如何在使用 SDL2 的 Eclipse 中构建 c++ 程序
- 不带轮询的 SDL2 事件回调
- C++ SDL2:如何将矩形渲染到多个视口中
- SDL2 调整窗口大小后如何缩放鼠标坐标?
- SDL2 二维纹理分配/池化
- 如何使用 sdl2 快速绘制像素网格?
- 看不到文件'SDL2.lib'
- 我的 SDL2 程序需要哪些二进制文件,以便它在另一台未安装 SDL2 的计算机中工作
- SDL2 无法正确绘制矩形
- 实施英特尔实感和 SDL2 时出现问题
- 找不到 sdl2.dll
- SDL2 程序只写入部分字符串
- 为什么在使用 SDL2 时仍然收到'undefined reference'链接器错误?
- 在另一台 PC 上执行程序时加载 SDL2 共享库时出错
- SDL2 (C++) 如何使图像变小
- SDL2 AntiAliasing