我开始制作 Conway 的 Game of Life 的 SDL 版本。我以前使用过 SDL,但从未直接访问过像素。我写了下面的代码,它的执行速度很慢。我试过使用 UpdateRect 而不是 Flip,两者之间没有区别。我用SWSURFACE 初始化了SDL。我可以做些什么来改善它吗?我想到了解决方法(使用 bmp 来获得更大的像素等)。绘图数组是 640x480 的 Cell 对象,它只包含一个可以通过 getter/setter 访问的成员“alive”。我只想让鼠标有效地绘制像素。我需要使用 OpenGL 来执行此操作吗?我想澄清一下,我意识到现代硬件并不适合按像素访问。但鉴于这正是我所需要的,我正在寻找解决方案。
while (quit == false)
{
int pX = 0;
int pY = 0;
bool did_set = false;
if (SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_MOUSEBUTTONDOWN:
plot[event.button.y][event.button.x].setAlive(true);
pX = event.button.x;
pY = event.button.y;
did_set = true;
break;
case SDL_QUIT:
quit = true;
break;
}
}
if (did_set)
{
SDL_LockSurface(screen);
Uint32 p = SDL_MapRGB(screen->format, 255,255,255);
putpixel(screen, pX, pY, p);
SDL_UnlockSurface(screen);
}
SDL_Flip(screen);
}
最佳答案
每个“帧”只处理一个事件。
尝试将 SDL_PollEvent()
if
更改为 while
:
const Uint32 p = SDL_MapRGB(screen->format, 255,255,255);
while (quit == false)
{
while (SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_MOUSEBUTTONDOWN:
{
int pX = event.button.x;
int pY = event.button.y;
plot[pY][pX].setAlive(true);
SDL_LockSurface(screen);
putpixel(screen, pX, pY, p);
SDL_UnlockSurface(screen);
}
break;
case SDL_QUIT:
quit = true;
break;
}
}
SDL_Flip(screen);
}
关于c++ - 提高SDL的逐像素访问速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11509237/