OpenGL独家模式全屏

时间:2014-07-14 23:48:40

标签: c++ windows winapi opengl

DirectX允许应用程序独占持有GPU及其内容发送到的监视器。这被称为全屏。使用OpenGL时,使用ChangeDisplaySettings(&dv, CDS_FULLSCREEN)激活全屏。然而,这样做的结果是“假的”全屏 - 全屏窗口。这两者的表现存在一些差异,特别是当alt-tabbing失焦时。

有没有办法像DirectX一样只使用Win32 api和OpenGL来全屏创建窗口,或者这是DirectX独有的功能?

1 个答案:

答案 0 :(得分:2)

如果您愿意让GLUT为您执行窗口任务,您可以在此处查看:Full screen in openGL

如果您想自己进入WIN32详细信息,可以执行以下操作:

#include <stdlib.h>

#include <Windows.h>

#include "glew.h"
#include <gl/GL.h>
#include <gl/GLU.h>


int main()
{
    HWND hwnd;
    HDC hdc;
    int pixelFormat;
    PIXELFORMATDESCRIPTOR pfd;

    // First create the full screen window
    hwnd = CreateWindowEx(
        0 ,"STATIC","", WS_VISIBLE|WS_EX_TOPMOST,
        0,0,640,480, 0, 0, GetModuleHandle(NULL), 0
    );
    WINDOWPLACEMENT g_wpPrev = { sizeof(g_wpPrev) };
    DWORD dwStyle = GetWindowLong(hwnd, GWL_STYLE);
    MONITORINFO mi = { sizeof(mi) };
    if (
        GetWindowPlacement(hwnd, &g_wpPrev) &&
        GetMonitorInfo(MonitorFromWindow(hwnd,MONITOR_DEFAULTTOPRIMARY), &mi)
    ) {
        SetWindowLong(hwnd, GWL_STYLE, dwStyle & ~WS_OVERLAPPEDWINDOW);
        SetWindowPos(
            hwnd, HWND_TOP,
            mi.rcMonitor.left, mi.rcMonitor.top,
            mi.rcMonitor.right  - mi.rcMonitor.left,
            mi.rcMonitor.bottom - mi.rcMonitor.top,
            SWP_NOOWNERZORDER | SWP_FRAMECHANGED
        );
    }

    // Describe the pixel format
    memset(&pfd,0,sizeof(PIXELFORMATDESCRIPTOR));
    pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 32;
    pfd.cDepthBits = 16;
    pfd.iLayerType = PFD_MAIN_PLANE;

    // Create the device context and rendering context
    hdc = GetDC(hwnd);
    pixelFormat = ChoosePixelFormat(hdc,&pfd);
    SetPixelFormat(hdc,pixelFormat,&pfd);
    HGLRC rendering_context = wglCreateContext(hdc);
    BOOL rc = wglMakeCurrent(hdc, rendering_context);
    GLenum err = glewInit();
    if (GLEW_OK != err) { /*do something*/ }

    // Paint the back buffer red
    glClearColor(1,0,0,0);
    glClear(GL_COLOR_BUFFER_BIT);
    glFlush();

    // Show on screen
    rc = SwapBuffers(hdc);

    while (1)
    {
        // Do something ...
    }

    return 0;
}