SDL Toggling Fullscreen显示框架

时间:2011-12-03 12:31:05

标签: c++ sdl

将全屏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

来自全屏 - >窗口 - >全屏,错误如上。 从Windowed->全屏, 结果略有不同,我看到一个黑色条而不是框架......

1 个答案:

答案 0 :(得分:1)

您使用的是哪个版本的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调用之后,相同的列表导致无法绘制任何内容。