在使用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的可能性的快速草图
发布于 2017-04-21 18:41:01
如果你正在寻找一个不需要使用opengl的答案,那么这可能是有用的:
SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" );发布于 2013-11-08 22:34:32
据我所知,只有在创建上下文之后才能设置这些值,所以如果您在创建窗口之前运行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;
}https://stackoverflow.com/questions/19859891
复制相似问题