2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* context_gl_win.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 21:01:57 +00:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#if defined(OPENGL_ENABLED) || defined(LEGACYGL_ENABLED) || defined(GLES2_ENABLED)
|
|
|
|
|
|
|
|
//
|
|
|
|
// C++ Implementation: context_gl_x11
|
|
|
|
//
|
2016-03-08 23:00:52 +00:00
|
|
|
// Description:
|
2014-02-10 01:10:30 +00:00
|
|
|
//
|
|
|
|
//
|
|
|
|
// Author: Juan Linietsky <reduzio@gmail.com>, (C) 2008
|
|
|
|
//
|
|
|
|
// Copyright: See COPYING file that comes with this distribution
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "context_gl_win.h"
|
|
|
|
|
|
|
|
//#include "drivers/opengl/glwrapper.h"
|
|
|
|
//#include "ctxgl_procaddr.h"
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
#define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091
|
|
|
|
#define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092
|
|
|
|
#define WGL_CONTEXT_FLAGS_ARB 0x2094
|
2014-02-10 01:10:30 +00:00
|
|
|
#define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x00000002
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
typedef HGLRC(APIENTRY *PFNWGLCREATECONTEXTATTRIBSARBPROC)(HDC, HGLRC, const int *);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
void ContextGL_Win::release_current() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
wglMakeCurrent(hDC, NULL);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ContextGL_Win::make_current() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
wglMakeCurrent(hDC, hRC);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ContextGL_Win::get_window_width() {
|
|
|
|
|
|
|
|
return OS::get_singleton()->get_video_mode().width;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ContextGL_Win::get_window_height() {
|
|
|
|
|
|
|
|
return OS::get_singleton()->get_video_mode().height;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContextGL_Win::swap_buffers() {
|
|
|
|
|
|
|
|
SwapBuffers(hDC);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
static GLWrapperFuncPtr wrapper_get_proc_address(const char* p_function) {
|
|
|
|
|
|
|
|
print_line(String()+"getting proc of: "+p_function);
|
|
|
|
GLWrapperFuncPtr func=(GLWrapperFuncPtr)get_gl_proc_address(p_function);
|
|
|
|
if (!func) {
|
|
|
|
print_line("Couldn't find function: "+String(p_function));
|
|
|
|
print_line("error: "+itos(GetLastError()));
|
|
|
|
}
|
|
|
|
return func;
|
|
|
|
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2016-06-05 22:14:33 +00:00
|
|
|
void ContextGL_Win::set_use_vsync(bool p_use) {
|
|
|
|
|
|
|
|
if (wglSwapIntervalEXT) {
|
2017-03-05 15:44:50 +00:00
|
|
|
wglSwapIntervalEXT(p_use ? 1 : 0);
|
2016-06-05 22:14:33 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
use_vsync = p_use;
|
2016-06-05 22:14:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ContextGL_Win::is_using_vsync() const {
|
|
|
|
|
|
|
|
return use_vsync;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
#define _WGL_CONTEXT_DEBUG_BIT_ARB 0x0001
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Error ContextGL_Win::initialize() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
static PIXELFORMATDESCRIPTOR pfd = {
|
|
|
|
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
|
2014-02-10 01:10:30 +00:00
|
|
|
1,
|
2017-03-05 15:44:50 +00:00
|
|
|
PFD_DRAW_TO_WINDOW | // Format Must Support Window
|
|
|
|
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
|
|
|
|
PFD_DOUBLEBUFFER,
|
2016-03-08 23:00:52 +00:00
|
|
|
PFD_TYPE_RGBA,
|
2014-02-10 01:10:30 +00:00
|
|
|
24,
|
2017-03-05 15:44:50 +00:00
|
|
|
0, 0, 0, 0, 0, 0, // Color Bits Ignored
|
|
|
|
0, // No Alpha Buffer
|
|
|
|
0, // Shift Bit Ignored
|
|
|
|
0, // No Accumulation Buffer
|
|
|
|
0, 0, 0, 0, // Accumulation Bits Ignored
|
|
|
|
24, // 24Bit Z-Buffer (Depth Buffer)
|
|
|
|
0, // No Stencil Buffer
|
|
|
|
0, // No Auxiliary Buffer
|
2014-02-10 01:10:30 +00:00
|
|
|
PFD_MAIN_PLANE, // Main Drawing Layer
|
2017-03-05 15:44:50 +00:00
|
|
|
0, // Reserved
|
|
|
|
0, 0, 0 // Layer Masks Ignored
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (!(hDC = GetDC(hWnd))) {
|
|
|
|
MessageBox(NULL, "Can't Create A GL Device Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
|
|
|
|
return ERR_CANT_CREATE; // Return FALSE
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (!(pixel_format = ChoosePixelFormat(hDC, &pfd))) // Did Windows Find A Matching Pixel Format?
|
2014-02-10 01:10:30 +00:00
|
|
|
{
|
2017-03-05 15:44:50 +00:00
|
|
|
MessageBox(NULL, "Can't Find A Suitable pixel_format.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
|
|
|
|
return ERR_CANT_CREATE; // Return FALSE
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (!SetPixelFormat(hDC, pixel_format, &pfd)) // Are We Able To Set The Pixel Format?
|
2014-02-10 01:10:30 +00:00
|
|
|
{
|
2017-03-05 15:44:50 +00:00
|
|
|
MessageBox(NULL, "Can't Set The pixel_format.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
|
|
|
|
return ERR_CANT_CREATE; // Return FALSE
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (!(hRC = wglCreateContext(hDC))) // Are We Able To Get A Rendering Context?
|
2014-02-10 01:10:30 +00:00
|
|
|
{
|
2017-03-05 15:44:50 +00:00
|
|
|
MessageBox(NULL, "Can't Create A Temporary GL Rendering Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
|
|
|
|
return ERR_CANT_CREATE; // Return FALSE
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
wglMakeCurrent(hDC, hRC);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (opengl_3_context) {
|
|
|
|
|
|
|
|
int attribs[] = {
|
2017-03-05 15:44:50 +00:00
|
|
|
WGL_CONTEXT_MAJOR_VERSION_ARB, 3, //we want a 3.3 context
|
|
|
|
WGL_CONTEXT_MINOR_VERSION_ARB, 3,
|
|
|
|
//and it shall be forward compatible so that we can only use up to date functionality
|
|
|
|
WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB | _WGL_CONTEXT_DEBUG_BIT_ARB,
|
|
|
|
0
|
|
|
|
}; //zero indicates the end of the array
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB = NULL; //pointer to the method
|
2017-03-05 15:44:50 +00:00
|
|
|
wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (wglCreateContextAttribsARB == NULL) //OpenGL 3.0 is not supported
|
2014-02-10 01:10:30 +00:00
|
|
|
{
|
2017-03-05 15:44:50 +00:00
|
|
|
MessageBox(NULL, "Cannot get Proc Adress for CreateContextAttribs", "ERROR", MB_OK | MB_ICONEXCLAMATION);
|
2014-02-10 01:10:30 +00:00
|
|
|
wglDeleteContext(hRC);
|
|
|
|
return ERR_CANT_CREATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
HGLRC new_hRC;
|
2017-03-05 15:44:50 +00:00
|
|
|
if (!(new_hRC = wglCreateContextAttribsARB(hDC, 0, attribs))) {
|
2014-02-10 01:10:30 +00:00
|
|
|
wglDeleteContext(hRC);
|
2017-03-05 15:44:50 +00:00
|
|
|
MessageBox(NULL, "Can't Create An OpenGL 3.3 Rendering Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
|
|
|
|
return ERR_CANT_CREATE; // Return false
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
wglMakeCurrent(hDC, NULL);
|
2014-02-10 01:10:30 +00:00
|
|
|
wglDeleteContext(hRC);
|
2017-03-05 15:44:50 +00:00
|
|
|
hRC = new_hRC;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (!wglMakeCurrent(hDC, hRC)) // Try To Activate The Rendering Context
|
2014-02-10 01:10:30 +00:00
|
|
|
{
|
2017-03-05 15:44:50 +00:00
|
|
|
MessageBox(NULL, "Can't Activate The GL 3.3 Rendering Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
|
|
|
|
return ERR_CANT_CREATE; // Return FALSE
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2016-12-21 05:29:58 +00:00
|
|
|
printf("Activated GL 3.3 context");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT");
|
2017-01-14 11:26:56 +00:00
|
|
|
//glWrapperInit(wrapper_get_proc_address);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ContextGL_Win::ContextGL_Win(HWND hwnd, bool p_opengl_3_context) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
opengl_3_context = p_opengl_3_context;
|
|
|
|
hWnd = hwnd;
|
|
|
|
use_vsync = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ContextGL_Win::~ContextGL_Win() {
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|