SDL切换全屏显示帧

SDL Toggling Fullscreen reveals frame

本文关键字:显示 SDL      更新时间:2023-10-16

我在全屏和窗口切换SDL应用程序时遇到了一个问题:

SDL_SetVideoMode( (int)screenSize.x, (int)screenSize.y, 32, SDL_NOFRAME | SDL_FULLSCREEN | SDL_OPENGL );
// now upload all active textures back into the GPU
// toggle from fullscreen to windowed
SDL_SetVideoMode( (int)screenSize.x, (int)screenSize.y, 32, SDL_OPENGL );

这里的问题是"SDL_NOFRAME"标志似乎不起作用-我实际上在全屏应用程序中看到了一个框架!

注意:我所说的"框架"是指在标题栏上带有应用程序名称的窗口框架,以及最小化、恢复和关闭按钮。

如果我在全屏或窗口模式下直接启动应用程序,一切都可以正常工作,只是切换不工作。如果我在窗口和全屏模式下都使用SDL_NOFRAME,它会像预期的那样工作,但这不是我想要的。

编辑:这个函数的文档可以在这里读到:http://www.libsdl.org/cgi/docwiki.cgi/SDL_SetVideoMode

从全屏->窗口->全屏,错误如上所示。从视窗化->全屏,结果略有不同,我看到一个黑色的条而不是框架…

您使用的是哪个版本的SDL ?我假设它是SDL-1.2.14,并在该版本中尝试了以下操作:

#include "SDL.h"
int main(int argc, char *argv[])
{
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Surface *screen;
#if 1
    screen = SDL_SetVideoMode(640, 480, 32, SDL_OPENGL);
    SDL_Delay(2000);
    screen = SDL_SetVideoMode(640, 480, 32, SDL_OPENGL | SDL_FULLSCREEN);
    SDL_Delay(5000);
    screen = SDL_SetVideoMode(640, 480, 32, SDL_OPENGL);
    SDL_Delay(5000);
#else
    screen = SDL_SetVideoMode(640, 480, 32, SDL_OPENGL | SDL_FULLSCREEN);
    SDL_Delay(2000);
    screen = SDL_SetVideoMode(640, 480, 32, SDL_OPENGL);
    SDL_Delay(5000);
    screen = SDL_SetVideoMode(640, 480, 32, SDL_OPENGL | SDL_FULLSCREEN);
    SDL_Delay(5000);
#endif
    SDL_Quit();
    return 0;
}

根据预处理器标志集,代码从窗口切换到全屏,或者反过来。

在全屏模式下我没有得到一个窗口框架,而忽略SDL_FULLSCREEN时有一个。我还将SDL_SetVideoMode的返回值赋给一个变量,因为这可能会改变。当切换到全屏窗口时,可能仍然会显示在窗口模式下碰巧显示的屏幕内容。在全屏模式下,我看到的SDL_app窗口位于屏幕的左上角。你必须清除屏幕,以确保只显示你自己的东西。

还有一个SDL_WM_ToggleFullscreen函数。它不能在Windows上工作,但是文档的链接包含一个如何以可移植的方式处理窗口/全屏切换的示例。SDL文档中的代码示例如下:

/* Anyone may copy and alter this fragment of pseudo-code and use it however they wish */
#include "SDL.h" /* The only library needed to do this */
Uint32 flags = SDL_SWSURFACE; /* Start with whatever flags you prefer */
SDL_Surface *screen = SDL_SetVideoMode(640, 480, 32, flags); /* Start with whatever settings you prefer */
/* -- Portable Fullscreen Toggling --
As of SDL 1.2.10, if width and height are both 0, SDL_SetVideoMode will use the
width and height of the current video mode (or the desktop mode, if no mode has been set).
Use 0 for Height, Width, and Color Depth to keep the current values. */
flags = screen->flags; /* Save the current flags in case toggling fails */
screen = SDL_SetVideoMode(0, 0, 0, screen->flags ^ SDL_FULLSCREEN); /*Toggles FullScreen Mode */
if(screen == NULL) screen = SDL_SetVideoMode(0, 0, 0, flags); /* If toggle FullScreen failed, then switch back */
if(screen == NULL) exit(1); /* If you can't switch back for some reason, then epic fail */

示例没有提到在切换窗口模式时使用SDL_OPENGL标志会发生什么。我担心OpenGL上下文可能会被破坏并重新创建,这意味着你必须重新初始化OpenGL并在切换时重新加载所有纹理和几何窗口模式。

编辑:

我能够确认,OpenGL上下文在切换显示模式后被破坏:

创建显示列表

GLuint list = glGenLists(1);
glNewList(list, GL_COMPILE);
glBegin(GL_LINES);
glVertex2f(0.0f, 0.0f);
glVertex2f(1.0f, 1.0f);
glEnd();
glEndList();

并用

调用
glClear(GL_COLOR_BUFFER_BIT);
glCallList(list);

。在调用SDL_SetVideoMode之后,相同的列表没有被绘制。