[英]SDL2 Rendering (flickering)
I'm having a problem with rendering a texture using SDL2. 我在使用SDL2渲染纹理时遇到问题。 In my program, there are a bunch of pixels which move around the screen, however they visibly flicker. 在我的程序中,有一堆像素在屏幕上移动,但是它们明显闪烁。 Here is my code: 这是我的代码:
Source.cpp Source.cpp
frameStart = SDL_GetTicks();
screen.update();
// Makes the screen black
for (int x = 0; x < Screen::SCREEN_WIDTH; x++)
{
for (int y = 0; y < Screen::SCREEN_HEIGHT; y++) {
screen.setPixel(x, y, 0, 0, 0);
}
}
// Draw + Update people (Code should be separated and moved)
for (Person &p : people) {
if (p.isAlive == true) {
p.Update(p);
cout << p.x << ", " << p.y << endl;
screen.setPixel(p.x, p.y, 255, 0, 0);
}
else {
screen.setPixel(p.x, p.y, 0, 0, 0);
}
}
// Manage events
if (screen.processEvents() == false) {
break;
}
frameTime = SDL_GetTicks() - frameStart;
if (frameDelay > frameTime) {
SDL_Delay(frameDelay - frameTime);
}
}
Screen.update points here: Screen.update点在这里:
void Screen::update() {
SDL_UpdateTexture(m_texture, NULL, m_buffer, SCREEN_WIDTH * sizeof(Uint32) );
SDL_RenderCopy(m_renderer, m_texture, NULL, NULL);
SDL_RenderPresent(m_renderer);
SDL_RenderClear(m_renderer);
}
And my renderer and texture setup are as follows: 我的渲染器和纹理设置如下:
m_renderer = SDL_CreateRenderer(m_window, -1, SDL_RENDERER_PRESENTVSYNC);
m_texture = SDL_CreateTexture(m_renderer, SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_STATIC, SCREEN_WIDTH, SCREEN_HEIGHT);
I've messed around alot with the flags for both the renderer and texture but have not found a solution that stops the flickering. 我把渲染器和纹理的标志都弄得乱七八糟,但是还没有找到阻止闪烁的解决方案。 Any help would be really appreciated. 任何帮助将非常感激。
The sequence you should follow is: 您应遵循的顺序是:
What you are doing is basically, Render and clear right the way. 基本上,您正在做的是正确渲染和清除路径。 If you make this way might work just fine 如果您这样做,可能会很好
frameStart = SDL_GetTicks();
SDL_RenderClear(m_renderer);
/* Do you rendering */
/*Manage your events*/
SDL_RenderPresent(m_renderer);
frameTime = SDL_GetTicks() - frameStart;
if (frameDelay > frameTime) {
SDL_Delay(frameDelay - frameTime);
}
Also about rendering pixels, when clearing the texture, you should use memset wich is faster. 同样关于渲染像素,清除纹理时,应使用速度更快的内存集。 But your way still works 但是你的方式仍然有效
memset(pixels,0,h*pitch);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.