dolphin/Source/Core/VideoBackends/OGL/GLInterface/WGL.cpp

183 lines
4.9 KiB
C++
Raw Normal View History

// Copyright 2013 Dolphin Emulator Project
// Licensed under GPLv2
// Refer to the license.txt file included.
2012-12-17 21:01:52 +00:00
#include <string>
#include "Core/Host.h"
2012-12-17 21:01:52 +00:00
#include "VideoBackends/OGL/GLInterface/WGL.h"
#include "VideoCommon/RenderBase.h"
#include "VideoCommon/VertexShaderManager.h"
#include "VideoCommon/VideoConfig.h"
2012-12-17 21:01:52 +00:00
static HDC hDC = nullptr; // Private GDI Device Context
static HGLRC hRC = nullptr; // Permanent Rendering Context
static HINSTANCE dllHandle = nullptr; // Handle to OpenGL32.dll
2012-12-17 21:01:52 +00:00
// typedef from wglext.h
typedef BOOL(WINAPI * PFNWGLSWAPINTERVALEXTPROC) (int interval);
static PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = nullptr;
void cInterfaceWGL::SwapInterval(int Interval)
{
if (wglSwapIntervalEXT)
wglSwapIntervalEXT(Interval);
else
ERROR_LOG(VIDEO, "No support for SwapInterval (framerate clamped to monitor refresh rate).");
}
2012-12-26 06:34:09 +00:00
void cInterfaceWGL::Swap()
2012-12-17 21:01:52 +00:00
{
SwapBuffers(hDC);
}
void* cInterfaceWGL::GetFuncAddress(const std::string& name)
{
void* func = (void*)wglGetProcAddress((LPCSTR)name.c_str());
if (func == nullptr)
func = (void*)GetProcAddress(dllHandle, (LPCSTR)name.c_str());
return func;
}
2012-12-17 21:01:52 +00:00
// Draw messages on top of the screen
bool cInterfaceWGL::PeekMessages()
{
// TODO: peekmessage
MSG msg;
while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
return FALSE;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return TRUE;
}
// Create rendering window.
// Call browser: Core.cpp:EmuThread() > main.cpp:Video_Initialize()
bool cInterfaceWGL::Create(void *window_handle)
2012-12-17 21:01:52 +00:00
{
if (window_handle == nullptr)
return false;
HWND window_handle_reified = reinterpret_cast<HWND>(window_handle);
RECT window_rect = {0};
if (!GetClientRect(window_handle_reified, &window_rect))
return false;
2012-12-17 21:01:52 +00:00
// Control window size and picture scaling
int twidth = (window_rect.right - window_rect.left);
int theight = (window_rect.bottom - window_rect.top);
s_backbuffer_width = twidth;
s_backbuffer_height = theight;
2012-12-17 21:01:52 +00:00
m_window_handle = window_handle_reified;
#ifdef _WIN32
dllHandle = LoadLibrary(TEXT("OpenGL32.dll"));
#endif
2012-12-17 21:01:52 +00:00
PIXELFORMATDESCRIPTOR pfd = // pfd Tells Windows How We Want Things To Be
{
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
1, // Version Number
PFD_DRAW_TO_WINDOW | // Format Must Support Window
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
PFD_DOUBLEBUFFER, // Must Support Double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
32, // Select Our Color Depth
0, 0, 0, 0, 0, 0, // Color Bits Ignored
0, // 8bit Alpha Buffer
0, // Shift Bit Ignored
0, // No Accumulation Buffer
0, 0, 0, 0, // Accumulation Bits Ignored
0, // 0Bit Z-Buffer (Depth Buffer)
0, // 0bit Stencil Buffer
2012-12-17 21:01:52 +00:00
0, // No Auxiliary Buffer
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved
0, 0, 0 // Layer Masks Ignored
};
int PixelFormat; // Holds The Results After Searching For A Match
2012-12-17 21:01:52 +00:00
2014-08-30 21:01:19 +00:00
if (!(hDC = GetDC(window_handle_reified)))
{
2012-12-17 21:01:52 +00:00
PanicAlert("(1) Can't create an OpenGL Device context. Fail.");
return false;
}
2014-08-30 21:01:19 +00:00
if (!(PixelFormat = ChoosePixelFormat(hDC, &pfd)))
{
2012-12-17 21:01:52 +00:00
PanicAlert("(2) Can't find a suitable PixelFormat.");
return false;
}
2014-08-30 21:01:19 +00:00
if (!SetPixelFormat(hDC, PixelFormat, &pfd))
{
2012-12-17 21:01:52 +00:00
PanicAlert("(3) Can't set the PixelFormat.");
return false;
}
2014-08-30 21:01:19 +00:00
if (!(hRC = wglCreateContext(hDC)))
{
2012-12-17 21:01:52 +00:00
PanicAlert("(4) Can't create an OpenGL rendering context.");
return false;
}
2012-12-17 21:01:52 +00:00
return true;
}
bool cInterfaceWGL::MakeCurrent()
{
2014-06-27 21:50:51 +00:00
bool success = wglMakeCurrent(hDC, hRC) ? true : false;
if (success)
{
// Grab the swap interval function pointer
wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)GLInterface->GetFuncAddress("wglSwapIntervalEXT");
}
return success;
}
2014-06-27 21:50:51 +00:00
bool cInterfaceWGL::ClearCurrent()
{
return wglMakeCurrent(hDC, nullptr) ? true : false;
}
2012-12-17 21:01:52 +00:00
// Update window width, size and etc. Called from Render.cpp
void cInterfaceWGL::Update()
{
RECT rcWindow;
GetClientRect(m_window_handle, &rcWindow);
2012-12-17 21:01:52 +00:00
// Get the new window width and height
s_backbuffer_width = (rcWindow.right - rcWindow.left);
s_backbuffer_height = (rcWindow.bottom - rcWindow.top);
2012-12-17 21:01:52 +00:00
}
// Close backend
void cInterfaceWGL::Shutdown()
{
if (hRC)
{
if (!wglMakeCurrent(nullptr, nullptr))
2012-12-17 21:01:52 +00:00
NOTICE_LOG(VIDEO, "Could not release drawing context.");
if (!wglDeleteContext(hRC))
ERROR_LOG(VIDEO, "Attempt to release rendering context failed.");
2012-12-17 21:01:52 +00:00
hRC = nullptr;
2012-12-17 21:01:52 +00:00
}
if (hDC && !ReleaseDC(m_window_handle, hDC))
2012-12-17 21:01:52 +00:00
{
ERROR_LOG(VIDEO, "Attempt to release device context failed.");
hDC = nullptr;
2012-12-17 21:01:52 +00:00
}
}