简体   繁体   English

OpenGL独家模式全屏

[英]OpenGL exclusive mode fullscreen

DirectX allows an application to take exclusive hold of a GPU and the monitor its content is sent to. DirectX允许应用程序独占一个GPU并将其内容发送到监视器。 This is referred to as fullscreen. 这被称为全屏。 When using OpenGL, fullscreen is activated using ChangeDisplaySettings(&dv, CDS_FULLSCREEN) . 使用OpenGL时,使用ChangeDisplaySettings(&dv, CDS_FULLSCREEN)激活全屏。 However, the result of this is a "fake" fullscreen - a fullscreen window. 然而,这样做的结果是“假的”全屏 - 全屏窗口。 There are a few differences in how the two behave, particularly when alt-tabbing out of focus. 这两者的行为方式存在一些差异,特别是当alt-tabbing失焦时。

Is there a way to create a window in fullscreen the way DirectX does it using only the Win32 api and OpenGL, or is this a feature exclusive to DirectX? 有没有办法像DirectX一样只使用Win32 api和OpenGL来全屏创建一个窗口,或者这是DirectX独有的功能?

If you are willing to let GLUT do the windowing tasks for you, you can look here: Full screen in openGL 如果你愿意让GLUT为你做窗口任务,你可以看一下: 在openGL中全屏

If you want to go into WIN32 detail by yourself you can do the following: 如果您想自己进入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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM