3 This file is part of libmspgbase
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
13 #include <msp/core/application.h>
14 #include <msp/core/except.h>
16 #include "glcontext.h"
25 GLOptions::GLOptions():
33 GLContext::GLContext(Window &wnd, const GLOptions &opts):
34 display(wnd.get_display()),
38 HDC dc=GetDC(window.get_handle());
40 PIXELFORMATDESCRIPTOR pfd;
41 memset(&pfd, 0, sizeof(pfd));
43 pfd.nSize=sizeof(pfd);
45 pfd.dwFlags=PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
47 pfd.dwFlags|=PFD_DOUBLEBUFFER;
48 pfd.iPixelType=PFD_TYPE_RGBA;
55 int pf_index=ChoosePixelFormat(dc, &pfd);
57 throw Exception("Couldn't find a suitable pixel format");
58 SetPixelFormat(dc, pf_index, &pfd);
60 context=wglCreateContext(dc);
61 wglMakeCurrent(dc, context);
63 ReleaseDC(window.get_handle(), dc);
65 std::vector<int> attribs;
67 attribs.push_back(GLX_RGBA);
68 attribs.push_back(GLX_DEPTH_SIZE);
73 attribs.push_back(GLX_ALPHA_SIZE);
79 attribs.push_back(GLX_STENCIL_SIZE);
84 attribs.push_back(GLX_DOUBLEBUFFER);
86 if(opts.multisample>0)
88 attribs.push_back(GLX_SAMPLE_BUFFERS_ARB);
89 attribs.push_back(opts.multisample);
94 ::Display *dpy=display.get_display();
96 XVisualInfo *vi=glXChooseVisual(dpy, DefaultScreen(dpy), &attribs.front());
98 throw Exception("Couldn't find a suitable GLX visual");
99 context=glXCreateContext(dpy, vi, 0, true);
101 XSetWindowAttributes attr;
102 attr.colormap=XCreateColormap(dpy, DefaultRootWindow(dpy), vi->visual, AllocNone);
104 subwnd=XCreateWindow(dpy, window.get_handle(), 0, 0, window.get_width(), window.get_height(), 0, vi->depth, InputOutput, vi->visual, CWColormap, &attr);
105 XMapWindow(display.get_display(), subwnd);
109 glXMakeCurrent(dpy, subwnd, context);
111 window.signal_resize.connect(sigc::mem_fun(this, &GLContext::window_resized));
115 GLContext::~GLContext()
118 wglMakeCurrent(0, 0);
119 wglDeleteContext(context);
121 ::Display *dpy=display.get_display();
123 glXMakeCurrent(dpy, 0, 0);
124 glXDestroyContext(dpy, context);
125 XDestroyWindow(dpy, subwnd);
129 void GLContext::swap_buffers()
132 HDC dc=GetDC(window.get_handle());
134 ReleaseDC(window.get_handle(), dc);
136 glXSwapBuffers(display.get_display(), subwnd);
140 void GLContext::window_resized(unsigned w, unsigned h)
143 XMoveResizeWindow(display.get_display(), subwnd, 0, 0, w, h);
145 glViewport(0, 0, w, h);
148 } // namespace Graphics