SDL:无法翻转多个表面
我一直在尝试翻转表面,并且如果我只翻转单个表面(与我从 SDL_SetVideoMode
返回的同一表面),就会成功。如果我尝试翻转从 SDL_DisplayFormat
返回的表面,则不会发生任何事情。我附上了演示代码来演示我的问题:
#include <stdio.h>
#include <stdlib.h>
#include "SDL/SDL.h"
void main()
{
int i;
SDL_Surface *mysurface1;
SDL_Surface *mysurface2;
char *pxl;
SDL_Init( SDL_INIT_EVERYTHING );
mysurface1 = SDL_SetVideoMode( 640, 480, 8, SDL_DOUBLEBUF|SDL_HWSURFACE );
for (i = 0; i < 20; i++)
{
pxl = (char *)mysurface1->pixels + i*mysurface1->pitch + i;
*pxl = 100; // Red Line
}
SDL_Flip(mysurface1); // Works, we see a red line
sleep(5);
printf("Sleeping for 5...\n");
mysurface2 = SDL_DisplayFormat(mysurface1);
for (i = 0; i < 20; i++)
{
pxl = (char *)mysurface2->pixels + i*mysurface2->pitch + i;
*pxl = 255; // White line
}
SDL_Flip(mysurface2); // White line doesnt appear
printf("Done... No white line\n");
sleep(10);
}
以前有人见过这个吗?再次,我认为我追踪到了如果它是我从 SDL_DisplayFormat
返回的表面,则不会显示的表面。如果我在表面上执行此操作,我会从 SDL_SetVideoMode
返回,然后我会看到红线,并且一切正常。
I've been trying to flip surfaces and have been successful if I'm only flipping a single surface (the same surface I got back from SDL_SetVideoMode
). If I try to flip the surface I get back from SDL_DisplayFormat
, nothing happens. I've attached demo code that demonstrates my problem:
#include <stdio.h>
#include <stdlib.h>
#include "SDL/SDL.h"
void main()
{
int i;
SDL_Surface *mysurface1;
SDL_Surface *mysurface2;
char *pxl;
SDL_Init( SDL_INIT_EVERYTHING );
mysurface1 = SDL_SetVideoMode( 640, 480, 8, SDL_DOUBLEBUF|SDL_HWSURFACE );
for (i = 0; i < 20; i++)
{
pxl = (char *)mysurface1->pixels + i*mysurface1->pitch + i;
*pxl = 100; // Red Line
}
SDL_Flip(mysurface1); // Works, we see a red line
sleep(5);
printf("Sleeping for 5...\n");
mysurface2 = SDL_DisplayFormat(mysurface1);
for (i = 0; i < 20; i++)
{
pxl = (char *)mysurface2->pixels + i*mysurface2->pitch + i;
*pxl = 255; // White line
}
SDL_Flip(mysurface2); // White line doesnt appear
printf("Done... No white line\n");
sleep(10);
}
Has anyone ever seen this before? Again, I think I tracked it down to surfaces that wont display if its a surface I got back from SDL_DisplayFormat
. If I do it on the surface I get back from SDL_SetVideoMode
, then I see the red line and everything works fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您只能翻转主显示表面(使用
SDL_SetVideoMode
创建的显示表面)。为了使其他表面可见,您需要将其传输到主表面上。有关如何执行此操作的详细信息,请查找SDL_BlitSurface
。You can only flip the main display surface (the one created with
SDL_SetVideoMode
). In order to make your other surface visible, you need to blit it onto the main surface. LookupSDL_BlitSurface
for details on how to do that.将屏幕传递给
SDL_Flip
函数。 Flip 函数修改 screen->pixels 的值,使其指向屏幕上不可见的表面。不过,这仅适用于 SVGA 和 DGA 等视频设备。在 X11 上,调用
SDL_Flip(screen)
相当于调用SDL_UpdateRect(screen, 0, 0, 0, 0)
。在我的 Linux 笔记本上,打印:
screen->pixels
的值是相同的,但这只是因为在 X11 上翻转操作是无操作。在 SVGA 或 DGA 等视频设备上,这两个值会不同。Pass the screen to the
SDL_Flip
function. The flip function modifies the value ofscreen->pixels
so that it points to the surface that isn't visible on the screen.However, this is only applicable to video devices such as SVGA and DGA. On X11, calling
SDL_Flip(screen)
is equivalent to callingSDL_UpdateRect(screen, 0, 0, 0, 0)
.On my Linux notebook, this prints:
The value of
screen->pixels
is the same, but this is only because on X11 the flip operation is a no-operation. On a video device such as SVGA or DGA, the two values would be different.首先,
SDL_Flip()
似乎仅适用于与屏幕或窗口相对应的表面,例如SDL_SetVideoMode()
创建的表面。你的另一个表面在屏幕外;对它进行双缓冲(或翻转它)没有多大意义,而且它很可能无论如何都不是双缓冲的。作为一个离屏表面,它不会出现,直到您使用SDL_BlitSurface()
或类似函数将其传输到显示表面 - 然后,下次翻转显示表面时,更改将可见。本质上,
mysurface2
实际上并不在您的显示器上,直到您将其位图传输到显示器上的表面上。如果您将以下内容替换为:
...那么您的代码可能会按您的预期工作。
First, it seems
SDL_Flip()
only works on surfaces that correspond to the screen or a window, like those created bySDL_SetVideoMode()
. Your other surface is off-screen; it doesn't make much sense to double-buffer it (or flip it), and it most likely isn't double-buffered anyway. Being an off screen surface, it won't appear until you blit it to your display surface withSDL_BlitSurface()
or a similar function -- then, the changes will be visible next time you flip the display surface.Essentially,
mysurface2
isn't actually on your display until you put it there, by blitting it onto a surface that is on your display. If you replace the following:With this:
...then your code will probably work as you expect.