2013-01-04 21:33:24 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007-2012 Gabest
|
|
|
|
* http://www.gabest.org
|
|
|
|
*
|
|
|
|
* This Program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This Program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with GNU Make; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "GSWndWGL.h"
|
|
|
|
|
|
|
|
#ifdef _WINDOWS
|
|
|
|
GSWndWGL::GSWndWGL()
|
2013-06-16 08:43:50 +00:00
|
|
|
: m_NativeWindow(NULL), m_NativeDisplay(NULL), m_context(NULL)
|
2013-01-04 21:33:24 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GSWndWGL::CreateContext(int major, int minor)
|
|
|
|
{
|
|
|
|
if ( !m_NativeDisplay || !m_NativeWindow )
|
|
|
|
{
|
|
|
|
fprintf( stderr, "Wrong display/window\n" );
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// GL2 context are quite easy but we need GL3 which is another painful story...
|
2014-01-26 00:58:21 +00:00
|
|
|
m_context = wglCreateContext(m_NativeDisplay);
|
|
|
|
if (!m_context) {
|
2013-01-17 17:14:33 +00:00
|
|
|
fprintf(stderr, "Failed to create a 2.0 context\n");
|
2013-01-17 16:58:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-01-04 21:33:24 +00:00
|
|
|
|
|
|
|
// FIXME test it
|
|
|
|
// Note: albeit every tutorial said that we need an opengl context to use the GL function wglCreateContextAttribsARB
|
|
|
|
// On linux it works without the extra temporary context, not sure the limitation still applied
|
|
|
|
if (major >= 3) {
|
|
|
|
AttachContext();
|
|
|
|
|
|
|
|
// Create a context
|
|
|
|
int context_attribs[] =
|
|
|
|
{
|
|
|
|
WGL_CONTEXT_MAJOR_VERSION_ARB, major,
|
|
|
|
WGL_CONTEXT_MINOR_VERSION_ARB, minor,
|
|
|
|
// FIXME : Request a debug context to ease opengl development
|
|
|
|
// Note: don't support deprecated feature (pre openg 3.1)
|
|
|
|
//GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_DEBUG_BIT_ARB | GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
|
2014-11-20 21:11:30 +00:00
|
|
|
#ifdef ENABLE_OGL_DEBUG
|
2013-01-04 21:33:24 +00:00
|
|
|
WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_DEBUG_BIT_ARB,
|
2014-11-20 21:11:30 +00:00
|
|
|
#endif
|
|
|
|
WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
|
2013-01-04 23:02:48 +00:00
|
|
|
0
|
2013-01-04 21:33:24 +00:00
|
|
|
};
|
|
|
|
|
2013-01-04 23:02:48 +00:00
|
|
|
PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");
|
2013-01-17 16:58:48 +00:00
|
|
|
if (!wglCreateContextAttribsARB) {
|
2013-01-17 17:14:33 +00:00
|
|
|
fprintf(stderr, "Failed to init wglCreateContextAttribsARB function pointer\n");
|
2013-01-17 16:58:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-01-04 21:33:24 +00:00
|
|
|
|
|
|
|
HGLRC context30 = wglCreateContextAttribsARB(m_NativeDisplay, NULL, context_attribs);
|
2013-01-17 16:58:48 +00:00
|
|
|
if (!context30) {
|
2013-01-17 17:14:33 +00:00
|
|
|
fprintf(stderr, "Failed to create a 3.x context\n");
|
2013-01-17 16:58:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-01-04 21:33:24 +00:00
|
|
|
|
|
|
|
DetachContext();
|
|
|
|
wglDeleteContext(m_context);
|
|
|
|
|
|
|
|
m_context = context30;
|
2013-01-11 13:54:15 +00:00
|
|
|
fprintf(stderr, "3.x GL context successfully created\n");
|
2013-01-04 21:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSWndWGL::AttachContext()
|
|
|
|
{
|
|
|
|
if (!IsContextAttached()) {
|
|
|
|
wglMakeCurrent(m_NativeDisplay, m_context);
|
|
|
|
m_ctx_attached = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSWndWGL::DetachContext()
|
|
|
|
{
|
|
|
|
if (IsContextAttached()) {
|
|
|
|
wglMakeCurrent(NULL, NULL);
|
|
|
|
m_ctx_attached = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: DROP ???
|
|
|
|
void GSWndWGL::CheckContext()
|
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
int glxMajorVersion, glxMinorVersion;
|
|
|
|
glXQueryVersion(m_NativeDisplay, &glxMajorVersion, &glxMinorVersion);
|
|
|
|
if (glXIsDirect(m_NativeDisplay, m_context))
|
|
|
|
fprintf(stderr, "glX-Version %d.%d with Direct Rendering\n", glxMajorVersion, glxMinorVersion);
|
|
|
|
else
|
|
|
|
fprintf(stderr, "glX-Version %d.%d with Indirect Rendering !!! It won't support properly opengl\n", glxMajorVersion, glxMinorVersion);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GSWndWGL::Attach(void* handle, bool managed)
|
|
|
|
{
|
|
|
|
m_NativeWindow = (HWND)handle;
|
|
|
|
m_managed = managed;
|
|
|
|
|
|
|
|
if (!OpenWGLDisplay()) return false;
|
|
|
|
|
2013-01-11 13:54:15 +00:00
|
|
|
if (!CreateContext(3, 3)) return false;
|
2013-01-04 21:33:24 +00:00
|
|
|
|
|
|
|
AttachContext();
|
|
|
|
|
|
|
|
CheckContext();
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
//m_swapinterval = (PFNGLXSWAPINTERVALMESAPROC)glXGetProcAddress((const GLubyte*) "glXSwapIntervalMESA");
|
|
|
|
//PFNGLXSWAPINTERVALMESAPROC m_swapinterval = (PFNGLXSWAPINTERVALMESAPROC)glXGetProcAddress((const GLubyte*) "glXSwapInterval");
|
|
|
|
|
2013-06-16 08:43:50 +00:00
|
|
|
PopulateGlFunction();
|
|
|
|
|
2013-01-04 21:33:24 +00:00
|
|
|
UpdateWindow(m_NativeWindow);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSWndWGL::Detach()
|
|
|
|
{
|
|
|
|
// Actually the destructor is not called when there is only a GSclose/GSshutdown
|
|
|
|
// The window still need to be closed
|
|
|
|
DetachContext();
|
|
|
|
|
2013-01-04 23:02:48 +00:00
|
|
|
if (m_context) wglDeleteContext(m_context);
|
2013-01-17 16:58:48 +00:00
|
|
|
m_context = NULL;
|
2013-01-04 21:33:24 +00:00
|
|
|
|
|
|
|
CloseWGLDisplay();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GSWndWGL::OpenWGLDisplay()
|
|
|
|
{
|
|
|
|
GLuint PixelFormat; // Holds The Results After Searching For A Match
|
|
|
|
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
|
|
|
|
24, // 24Bit Z-Buffer (Depth Buffer)
|
|
|
|
8, // 8bit Stencil Buffer
|
|
|
|
0, // No Auxiliary Buffer
|
|
|
|
PFD_MAIN_PLANE, // Main Drawing Layer
|
|
|
|
0, // Reserved
|
|
|
|
0, 0, 0 // Layer Masks Ignored
|
|
|
|
};
|
|
|
|
|
2014-01-26 00:58:21 +00:00
|
|
|
m_NativeDisplay = GetDC(m_NativeWindow);
|
|
|
|
if (!m_NativeDisplay)
|
2013-01-04 21:33:24 +00:00
|
|
|
{
|
2013-01-04 23:02:48 +00:00
|
|
|
MessageBox(NULL, "(1) Can't Create A GL Device Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
|
2013-01-04 21:33:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-01-26 00:58:21 +00:00
|
|
|
PixelFormat = ChoosePixelFormat(m_NativeDisplay, &pfd);
|
|
|
|
if (!PixelFormat)
|
2013-01-04 21:33:24 +00:00
|
|
|
{
|
2013-01-04 23:02:48 +00:00
|
|
|
MessageBox(NULL, "(2) Can't Find A Suitable PixelFormat.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
|
2013-01-04 21:33:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!SetPixelFormat(m_NativeDisplay, PixelFormat, &pfd))
|
|
|
|
{
|
2013-01-04 23:02:48 +00:00
|
|
|
MessageBox(NULL, "(3) Can't Set The PixelFormat.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
|
2013-01-04 21:33:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSWndWGL::CloseWGLDisplay()
|
|
|
|
{
|
|
|
|
if (m_NativeDisplay && !ReleaseDC(m_NativeWindow, m_NativeDisplay)) // Are We Able To Release The DC
|
|
|
|
{
|
2013-01-04 23:02:48 +00:00
|
|
|
MessageBox(NULL, "Release Device Context Failed.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
|
2013-01-04 21:33:24 +00:00
|
|
|
}
|
2013-01-17 16:58:48 +00:00
|
|
|
m_NativeDisplay = NULL; // Set DC To NULL
|
2013-01-04 21:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: GSopen 1 => Drop?
|
|
|
|
bool GSWndWGL::Create(const string& title, int w, int h)
|
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
if(m_NativeWindow) return false;
|
|
|
|
|
|
|
|
if(w <= 0 || h <= 0) {
|
|
|
|
w = theApp.GetConfig("ModeWidth", 640);
|
|
|
|
h = theApp.GetConfig("ModeHeight", 480);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_managed = true;
|
|
|
|
|
|
|
|
// note this part must be only executed when replaying .gs debug file
|
|
|
|
m_NativeDisplay = XOpenDisplay(NULL);
|
|
|
|
|
|
|
|
int attrListDbl[] = { GLX_RGBA, GLX_DOUBLEBUFFER,
|
|
|
|
GLX_RED_SIZE, 8,
|
|
|
|
GLX_GREEN_SIZE, 8,
|
|
|
|
GLX_BLUE_SIZE, 8,
|
|
|
|
GLX_DEPTH_SIZE, 24,
|
|
|
|
None
|
|
|
|
};
|
|
|
|
XVisualInfo* vi = glXChooseVisual(m_NativeDisplay, DefaultScreen(m_NativeDisplay), attrListDbl);
|
|
|
|
|
|
|
|
/* create a color map */
|
|
|
|
XSetWindowAttributes attr;
|
|
|
|
attr.colormap = XCreateColormap(m_NativeDisplay, RootWindow(m_NativeDisplay, vi->screen),
|
|
|
|
vi->visual, AllocNone);
|
|
|
|
attr.border_pixel = 0;
|
|
|
|
attr.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask |
|
|
|
|
StructureNotifyMask | SubstructureRedirectMask | SubstructureNotifyMask |
|
|
|
|
EnterWindowMask | LeaveWindowMask | FocusChangeMask ;
|
|
|
|
|
|
|
|
// Create a window at the last position/size
|
|
|
|
m_NativeWindow = XCreateWindow(m_NativeDisplay, RootWindow(m_NativeDisplay, vi->screen),
|
|
|
|
0 , 0 , w, h, 0, vi->depth, InputOutput, vi->visual,
|
|
|
|
CWBorderPixel | CWColormap | CWEventMask, &attr);
|
|
|
|
|
|
|
|
XMapWindow (m_NativeDisplay, m_NativeWindow);
|
|
|
|
XFree(vi);
|
|
|
|
|
|
|
|
if (!CreateContext(3, 3)) return false;
|
|
|
|
|
|
|
|
AttachContext();
|
|
|
|
|
|
|
|
return (m_NativeWindow != 0);
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
//Same as DX
|
|
|
|
GSVector4i GSWndWGL::GetClientRect()
|
|
|
|
{
|
|
|
|
GSVector4i r;
|
|
|
|
|
|
|
|
::GetClientRect(m_NativeWindow, r);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-08-03 08:29:01 +00:00
|
|
|
void* GSWndWGL::GetProcAddress(const char* name, bool opt)
|
2013-06-16 08:43:50 +00:00
|
|
|
{
|
|
|
|
void* ptr = (void*)wglGetProcAddress(name);
|
|
|
|
if (ptr == NULL) {
|
|
|
|
fprintf(stderr, "Failed to find %s\n", name);
|
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2013-01-04 21:33:24 +00:00
|
|
|
//TODO: check extensions supported or not
|
2013-01-04 23:02:48 +00:00
|
|
|
//FIXME : extension allocation
|
2013-01-04 21:33:24 +00:00
|
|
|
void GSWndWGL::SetVSync(bool enable)
|
|
|
|
{
|
|
|
|
// m_swapinterval uses an integer as parameter
|
|
|
|
// 0 -> disable vsync
|
|
|
|
// n -> wait n frame
|
|
|
|
//if (m_swapinterval) m_swapinterval((int)enable);
|
2013-01-04 23:02:48 +00:00
|
|
|
// wglSwapIntervalEXT(!enable);
|
2013-01-04 21:33:24 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSWndWGL::Flip()
|
|
|
|
{
|
|
|
|
SwapBuffers(m_NativeDisplay);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSWndWGL::Show()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSWndWGL::Hide()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSWndWGL::HideFrame()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns FALSE if the window has no title, or if th window title is under the strict
|
|
|
|
// management of the emulator.
|
|
|
|
|
|
|
|
bool GSWndWGL::SetWindowText(const char* title)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|