bool ckLowLevelAPI::toggleFullScreen(u16 width, u16 height)
{
    destroyFramebuffer();

    s_is_fullscreen = !s_is_fullscreen;

    return createFramebuffer(width, height);
}
Пример #2
0
FboRenderContext::FboRenderContext (RenderContext* context, const RenderConfig& config)
	: m_context				(context)
	, m_framebuffer			(0)
	, m_colorBuffer			(0)
	, m_depthStencilBuffer	(0)
	, m_renderTarget		()
{
	try
	{
		createFramebuffer(config);
	}
	catch (...)
	{
		destroyFramebuffer();
		throw;
	}
}
void ckLowLevelAPI::destroyApplication()
{
    destroyFramebuffer();
}
static bool createFramebuffer(u16 new_width, u16 new_height)
{
    /*
        create window
    */
    s_hInstance = GetModuleHandle(NULL);

    WNDCLASS wc;

    wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
    wc.lpfnWndProc = static_cast<WNDPROC>(WndProc);
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = s_hInstance;
    wc.hIcon = LoadIcon(s_hInstance, MAKEINTRESOURCE(101));
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = NULL;
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "Catcake";

    if (!RegisterClass(&wc))
    {
        return false;
    }

    if (s_is_fullscreen)
    {
        DEVMODE dmScreenSettings;
        ckLowLevelAPI::memset(&dmScreenSettings, 0, sizeof(dmScreenSettings));

        dmScreenSettings.dmSize = sizeof(dmScreenSettings);
        dmScreenSettings.dmPelsWidth = new_width;
        dmScreenSettings.dmPelsHeight = new_height;
        dmScreenSettings.dmBitsPerPel = 32;
        dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;

        if (ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
        {
            s_is_fullscreen = false;

            new_width = s_framebuffer_width;
            new_height = s_framebuffer_height;
        }
    }

    DWORD dwStyle, dwExStyle;

    if (s_is_fullscreen)
    {
        dwStyle = WS_POPUP;
        dwExStyle = WS_EX_APPWINDOW;
    }
    else
    {
        if (s_sys_flag & ckSysMgr::FLAG_VARIABLE_SIZE)
        {
            dwStyle = WS_OVERLAPPEDWINDOW;
        }
        else
        {
            dwStyle = WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX;
        }

        dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
    }

    RECT rect;

    rect.left = 0;
    rect.right = new_width;
    rect.top = 0;
    rect.bottom = new_height;

    AdjustWindowRectEx(&rect, dwStyle, false, dwExStyle);

    s_hWnd = CreateWindowEx( //
        dwExStyle, // dwExStyle
        "Catcake", // lpClassName
        s_app_name, // lpWindowName
        dwStyle | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, // dwStyle
        CW_USEDEFAULT, // x
        CW_USEDEFAULT, // y
        rect.right - rect.left, // nWidth
        rect.bottom - rect.top, // nHeight
        NULL, // hWndParent
        NULL, // hMenu
        s_hInstance, // hInstance
        NULL); // lpParam

    if (!s_hWnd)
    {
        destroyFramebuffer();
        return false;
    }

    s_hDC = GetDC(s_hWnd);

    if (!s_hDC)
    {
        destroyFramebuffer();
        return false;
    }

    /*
        initialize OpenGL
    */
    static const PIXELFORMATDESCRIPTOR pfd =
    {
        sizeof(PIXELFORMATDESCRIPTOR), // nSize
        1, // nVersion
        PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, // dwFlags
        PFD_TYPE_RGBA, // iPixelType
        24, // cColorBits
        0, 0, 0, 0, 0, 0, // Not used
        8, // cAlphaBits
        0, // Not used
        0, // cAccumBits
        0, 0, 0, 0, // Not used
        16, // cDepthBits
        0, // cStencilBits
        0, // cAuxBuffers
        PFD_MAIN_PLANE, // iLayerType
        0, 0, 0, 0, // Not used
    };

    int PixelFormat = ChoosePixelFormat(s_hDC, &pfd);

    if (!PixelFormat)
    {
        destroyFramebuffer();
        return false;
    }

    if (!SetPixelFormat(s_hDC, PixelFormat, &pfd))
    {
        destroyFramebuffer();
        return false;
    }

    s_hRC = wglCreateContext(s_hDC);

    if (!s_hRC)
    {
        destroyFramebuffer();
        return false;
    }

    if (!wglMakeCurrent(s_hDC, s_hRC))
    {
        destroyFramebuffer();
        return false;
    }

    /*
        wait vsync
    */
    typedef bool (WINAPI *PFNWGLSWAPINTERVALEXTPROC)(int interval);

    PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = reinterpret_cast<PFNWGLSWAPINTERVALEXTPROC>(wglGetProcAddress("wglSwapIntervalEXT"));

    if (wglSwapIntervalEXT)
    {
        wglSwapIntervalEXT(1);
    }

    /*
        show window
    */
    ShowWindow(s_hWnd, SW_SHOW);
    SetForegroundWindow(s_hWnd);
    SetFocus(s_hWnd);

    GetClientRect(s_hWnd, &rect);

    s_framebuffer_width = static_cast<u16>(rect.right);
    s_framebuffer_height = static_cast<u16>(rect.bottom);

    return true;
}