40 Win32Context::~Win32Context()
44 void Win32Context::shareOpenGLResources(HGLRC hGLRC)
46 if (hwnd() && mHDC && mHGLRC)
47 wglShareLists(hglrc(), hGLRC);
50 void Win32Context::makeCurrent()
53 wglMakeCurrent(mHDC, mHGLRC);
56 void Win32Context::update()
59 PostMessage(hwnd(), WM_PAINT, 0, 0);
62 void Win32Context::quitApplication()
67 void Win32Context::setMouseVisible(
bool visible)
69 mMouseVisible = visible;
71 while(ShowCursor(TRUE ) < 0) {}
73 while(ShowCursor(FALSE) >= 0) {}
76 void Win32Context::setPosition(
int x,
int y)
79 SetWindowPos(hwnd(), 0, x, y, 0, 0, SWP_NOSIZE );
82 void Win32Context::setSize(
int w,
int h)
86 RECT windowRect = { 0, 0, w, h };
87 AdjustWindowRectEx(&windowRect, (DWORD)GetWindowLongPtr(hwnd(), GWL_STYLE), 0, (DWORD)GetWindowLongPtr(hwnd(), GWL_EXSTYLE) );
89 int cx = windowRect.right - windowRect.left;
90 int cy = windowRect.bottom - windowRect.top;
91 SetWindowPos(hwnd(), 0, 0, 0, cx, cy, SWP_NOMOVE );
95 void Win32Context::setWindowSize(
int w,
int h)
100 SetWindowPos(hwnd(), 0, 0, 0, w, h, SWP_NOMOVE);
107 GetWindowRect(hwnd(), &r);
115 GetWindowRect(hwnd(), &r);
116 return vl::ivec2(r.right - r.left, r.bottom - r.top);
123 GetClientRect(hwnd(), &r);
124 return vl::ivec2(r.right - r.left, r.bottom - r.top);
128 void Win32Context::setWindowTitle(
const String& title)
131 SetWindowText(hwnd(), (
wchar_t*)title.
ptr());
134 void Win32Context::show()
137 ShowWindow(hwnd(), SW_SHOW);
140 void Win32Context::hide()
143 ShowWindow(hwnd(), SW_HIDE);
146 void Win32Context::getFocus()
152 void Win32Context::setMousePosition(
int x,
int y)
157 ClientToScreen( hwnd(), &pt );
158 SetCursorPos(pt.x, pt.y);
162 void Win32Context::swapBuffers()
168 bool Win32Context::setFullscreen(
bool fullscreen_on)
173 if (fullscreen_on == fullscreen())
178 SetWindowLongPtr(hwnd(), GWL_STYLE, mNormFlags);
180 if (!((mNormFlags & WS_MAXIMIZE) || (mNormFlags & WS_MINIMIZE)))
182 setPosition(mNormPosit.x(),mNormPosit.y());
183 setSize(mNormSize.x(), mNormSize.y());
186 SetWindowPos(hwnd(), 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE);
189 ChangeDisplaySettings(
NULL, 0);
194 EnumDisplaySettings(
NULL,ENUM_CURRENT_SETTINGS,&devmode);
199 devmode.dmBitsPerPel = openglContextInfo().bitsPerPixel();
200 devmode.dmFields |= DM_BITSPERPEL;
202 mNormFlags = (
unsigned int)GetWindowLongPtr(hwnd(), GWL_STYLE);
203 mNormPosit = position();
206 switch( ChangeDisplaySettings(&devmode, CDS_FULLSCREEN) )
208 case DISP_CHANGE_SUCCESSFUL:
210 RECT windowRect = { 0, 0, devmode.dmPelsWidth, devmode.dmPelsHeight };
211 SetWindowLongPtr(hwnd(), GWL_STYLE, WS_POPUP | WS_VISIBLE );
212 AdjustWindowRectEx(&windowRect, (DWORD)GetWindowLongPtr(hwnd(), GWL_STYLE), 0, (DWORD)GetWindowLongPtr(hwnd(), GWL_EXSTYLE) );
213 SetWindowPos(hwnd(), HWND_TOP, windowRect.left, windowRect.top, windowRect.right - windowRect.left, windowRect.bottom - windowRect.top, SWP_FRAMECHANGED );
216 #if(_WIN32_WINNT >= 0x0501) 217 case DISP_CHANGE_BADDUALVIEW:
218 MessageBox(
NULL, L
"Full-screen mode switch failed: DISP_CHANGE_BADDUALVIEW", L
"Win32Context::setFullscreen() error!", MB_OK | MB_ICONEXCLAMATION);
221 case DISP_CHANGE_BADFLAGS:
222 MessageBox(
NULL, L
"Full-screen mode switch failed: DISP_CHANGE_BADFLAGS", L
"Win32Context::setFullscreen() error!", MB_OK | MB_ICONEXCLAMATION);
224 case DISP_CHANGE_BADMODE:
225 MessageBox(
NULL, L
"Full-screen mode switch failed: DISP_CHANGE_BADMODE", L
"Win32Context::setFullscreen() error!", MB_OK | MB_ICONEXCLAMATION);
227 case DISP_CHANGE_BADPARAM:
228 MessageBox(
NULL, L
"Full-screen mode switch failed: DISP_CHANGE_BADPARAM", L
"Win32Context::setFullscreen() error!", MB_OK | MB_ICONEXCLAMATION);
230 case DISP_CHANGE_FAILED:
231 MessageBox(
NULL, L
"Full-screen mode switch failed: DISP_CHANGE_FAILED", L
"Win32Context::setFullscreen() error!", MB_OK | MB_ICONEXCLAMATION);
233 case DISP_CHANGE_NOTUPDATED:
234 MessageBox(
NULL, L
"Full-screen mode switch failed: DISP_CHANGE_NOTUPDATED", L
"Win32Context::setFullscreen() error!", MB_OK | MB_ICONEXCLAMATION);
236 case DISP_CHANGE_RESTART:
237 MessageBox(
NULL, L
"Full-screen mode switch failed: DISP_CHANGE_RESTART", L
"Win32Context::setFullscreen() error!", MB_OK | MB_ICONEXCLAMATION);
244 mFullscreen = fullscreen_on;
258 InOutContract(
Win32Context* context): mContext(context), mOK(
true)
274 DeleteDC(mContext->
mHDC);
281 if ( wglDeleteContext(mContext->
mHGLRC) == FALSE )
283 MessageBox(
NULL, L
"OpenGL context cleanup failed.\n" 284 L
"The handle either doesn't specify a valid context or the context is being used by another thread.",
285 L
"Win32Context::init() error!", MB_OK);
296 framebuffer()->setWidth(width);
297 framebuffer()->setHeight(height);
301 MessageBox(
NULL, L
"Cannot create OpenGL context: null HWND.", L
"Win32Context::init() error!", MB_OK);
302 return contract.mOK =
false;
305 setWindowTitle(title);
308 mHDC = ::GetDC(hwnd());
311 MessageBox(
NULL, L
"Device context acquisition failed.", L
"Win32Context::init() error!", MB_OK);
312 return contract.mOK =
false;
316 if (pixel_format_index == -1)
318 MessageBox(
NULL, L
"No suitable pixel fmt found.", L
"Win32Context::init() error!", MB_OK);
319 return contract.mOK =
false;
322 if (SetPixelFormat(mHDC, pixel_format_index,
NULL) == FALSE)
324 MessageBox(
NULL, L
"Pixel fmt setup failed.", L
"Win32Context::init() error!", MB_OK);
325 return contract.mOK =
false;
330 if (wglCreateContextAttribsARB && mContextAttribs.size() > 1)
333 VL_CHECK(mContextAttribs.back() == 0);
335 mHGLRC = wglCreateContextAttribsARB(mHDC, 0, &mContextAttribs[0]);
340 mHGLRC = wglCreateContext(mHDC);
345 MessageBox(
NULL, L
"OpenGL rendering context creation failed.", L
"Win32Context::init() error!", MB_OK);
346 return contract.mOK =
false;
350 if( ! initGLContext() )
351 return contract.mOK =
false;
360 setSize(width, height);
362 if (Has_GL_EXT_swap_control)
363 wglSwapIntervalEXT( fmt.
vSync() ? 1 : 0 );
366 shareOpenGLResources(share_context);
371 return contract.mOK =
true;
374 void Win32Context::setContextAttribs(
const int* attribs,
int size)
376 mContextAttribs.resize(size);
377 for(
int i = 0; i < size; ++i)
378 mContextAttribs[ i ] = attribs[ i ];
395 HWND hWnd = CreateWindowEx(
396 WS_EX_APPWINDOW | WS_EX_ACCEPTFILES,
399 WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
400 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
405 if (verbose) MessageBox(
NULL, L
"choosePixelFormat() critical failure: could not create window.", L
"Visualization Library error", MB_OK);
409 HDC hDC = GetDC(hWnd);
412 if (verbose) MessageBox(
NULL, L
"choosePixelFormat() critical failure: could not create HDC.", L
"Visualization Library error", MB_OK);
417 PIXELFORMATDESCRIPTOR pfd;
418 memset(&pfd, 0,
sizeof(pfd));
419 pfd.nSize =
sizeof(pfd);
421 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
422 pfd.dwFlags |= fmt.
doubleBuffer() ? PFD_DOUBLEBUFFER : 0;
423 pfd.dwFlags |= fmt.
stereo() ? PFD_STEREO : 0;
424 pfd.iPixelType = PFD_TYPE_RGBA;
427 pfd.cGreenBits = (BYTE)fmt.
rgbaBits().
g();
428 pfd.cBlueBits = (BYTE)fmt.
rgbaBits().
b();
429 pfd.cAlphaBits = (BYTE)fmt.
rgbaBits().
a();
436 pfd.iLayerType = PFD_MAIN_PLANE;
438 int pixel_format_index = ChoosePixelFormat(hDC, &pfd);
440 if (pixel_format_index == 0)
442 if (verbose) MessageBox(
NULL, L
"choosePixelFormat() critical failure: could not choose temporary format.", L
"Visualization Library error", MB_OK);
448 if (SetPixelFormat(hDC, pixel_format_index, &pfd) == FALSE)
450 if (verbose) MessageBox(
NULL, L
"choosePixelFormat() critical failure: could not set temporary format.", L
"Visualization Library error", MB_OK);
457 HGLRC hGLRC = wglCreateContext(hDC);
460 if (verbose) MessageBox(
NULL, L
"choosePixelFormat() critical failure: could not create temporary OpenGL context.", L
"Visualization Library error", MB_OK);
466 wglMakeCurrent(hDC, hGLRC);
470 fprintf(stderr,
"Error initializing OpenGL!\n");
481 float fAttributes[] = { 0, 0 };
485 WGL_SAMPLE_BUFFERS_ARB, GL_TRUE,
488 WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
489 WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
490 WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB,
492 WGL_RED_BITS_ARB, pfd.cRedBits,
493 WGL_GREEN_BITS_ARB, pfd.cGreenBits,
494 WGL_BLUE_BITS_ARB, pfd.cBlueBits,
495 WGL_ALPHA_BITS_ARB, pfd.cAlphaBits,
497 WGL_ACCUM_RED_BITS_ARB, pfd.cAccumRedBits,
498 WGL_ACCUM_GREEN_BITS_ARB, pfd.cAccumGreenBits,
499 WGL_ACCUM_BLUE_BITS_ARB, pfd.cAccumBlueBits,
500 WGL_ACCUM_ALPHA_BITS_ARB, pfd.cAccumAlphaBits,
502 WGL_DEPTH_BITS_ARB, pfd.cDepthBits,
503 WGL_DOUBLE_BUFFER_ARB, fmt.
doubleBuffer() ? GL_TRUE : GL_FALSE,
505 WGL_STENCIL_BITS_ARB, pfd.cStencilBits,
507 WGL_STEREO_ARB, fmt.
stereo() ? GL_TRUE : GL_FALSE,
514 iAttributes[3] = samples;
515 pixel_format_index = -1;
516 UINT num_formats = 0;
517 if ( wglChoosePixelFormatARB(hDC,iAttributes,fAttributes,1,&pixel_format_index,&num_formats) && num_formats >= 1 )
520 pixel_format_index = -1;
525 if ( wglDeleteContext(hGLRC) == FALSE )
526 if (verbose) MessageBox(
NULL, L
"Error deleting temporary OpenGL context, wglDeleteContext(hGLRC) failed.", L
"Visualization Library error", MB_OK);
532 if(pixel_format_index == -1)
537 #if defined(DEBUG) || !defined(NDEBUG) 538 DescribePixelFormat(hDC, pixel_format_index,
sizeof(PIXELFORMATDESCRIPTOR), &pfd);
542 vl::Log::debug(
vl::Say(
"RGBA Bits = %n %n %n %n\n") << pfd.cRedBits << pfd.cGreenBits << pfd.cBlueBits << pfd.cAlphaBits);
545 vl::Log::debug(
vl::Say(
"Double Buffer = %s\n") << (pfd.dwFlags & PFD_DOUBLEBUFFER ?
"Yes" :
"No") );
553 return pixel_format_index;
static void debug(const String &message)
Use this function to provide extra information useful to investigate and solve problems.
Vector2< int > ivec2
A 2 components vector with int precision.
int stencilBufferBits() const
A simple String formatting class.
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
int multisampleSamples() const
const T_Scalar & r() const
static void error(const String &message)
Use this function to provide information about run-time errors: file not found, out of memory...
const wchar_t * ptr() const
Returns the 0-terminated utf16 string.
Visualization Library main namespace.
const T_Scalar & g() const
The OpenGLContextFormat class encapsulates the settings of an OpenGL rendering context.
const ivec4 & rgbaBits() const
VLGRAPHICS_EXPORT bool initializeOpenGL()
To test whether OpenGL has been initialized at least once check vl::Is_OpenGL_Initialized.
const ivec4 & accumRGBABits() const
VLWIN32_EXPORT int choosePixelFormat(const vl::OpenGLContextFormat &fmt, bool verbose=true)
const T_Scalar & b() const
The Win32Context class implements an OpenGLContext using the Win32 API.
int depthBufferBits() const
The Win32 bindings namespace.
bool doubleBuffer() const
const wchar_t * gWin32WindowClassName
const T_Scalar & a() const