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"
22 GLOptions::GLOptions():
30 GLContext::GLContext(Window &wnd, const GLOptions &opts):
31 display(wnd.get_display()),
35 HDC dc=GetDC(window.get_handle());
37 PIXELFORMATDESCRIPTOR pfd;
38 memset(&pfd, 0, sizeof(pfd));
40 pfd.nSize=sizeof(pfd);
42 pfd.dwFlags=PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
44 pfd.dwFlags|=PFD_DOUBLEBUFFER;
45 pfd.iPixelType=PFD_TYPE_RGBA;
52 int pf_index=ChoosePixelFormat(dc, &pfd);
54 throw Exception("Couldn't find a suitable pixel format");
55 SetPixelFormat(dc, pf_index, &pfd);
57 context=wglCreateContext(dc);
58 wglMakeCurrent(dc, context);
60 ReleaseDC(window.get_handle(), dc);
62 std::vector<int> attribs;
64 attribs.push_back(GLX_RGBA);
65 attribs.push_back(GLX_DEPTH_SIZE);
70 attribs.push_back(GLX_ALPHA_SIZE);
76 attribs.push_back(GLX_STENCIL_SIZE);
81 attribs.push_back(GLX_DOUBLEBUFFER);
83 if(opts.multisample>0)
85 attribs.push_back(GLX_SAMPLE_BUFFERS_ARB);
86 attribs.push_back(opts.multisample);
91 ::Display *dpy=display.get_display();
93 XVisualInfo *vi=glXChooseVisual(dpy, DefaultScreen(dpy), &attribs.front());
95 throw Exception("Couldn't find a suitable GLX visual");
96 context=glXCreateContext(dpy, vi, 0, true);
98 XSetWindowAttributes attr;
99 attr.colormap=XCreateColormap(dpy, DefaultRootWindow(dpy), vi->visual, AllocNone);
101 subwnd=XCreateWindow(dpy, window.get_handle(), 0, 0, window.get_width(), window.get_height(), 0, vi->depth, InputOutput, vi->visual, CWColormap, &attr);
102 XMapWindow(display.get_display(), subwnd);
106 glXMakeCurrent(dpy, subwnd, context);
109 window.signal_resize.connect(sigc::mem_fun(this, &GLContext::window_resized));
112 GLContext::~GLContext()
115 wglMakeCurrent(0, 0);
116 wglDeleteContext(context);
118 ::Display *dpy=display.get_display();
120 glXMakeCurrent(dpy, 0, 0);
121 glXDestroyContext(dpy, context);
122 XDestroyWindow(dpy, subwnd);
126 void GLContext::swap_buffers()
129 HDC dc=GetDC(window.get_handle());
131 ReleaseDC(window.get_handle(), dc);
133 glXSwapBuffers(display.get_display(), subwnd);
137 void GLContext::window_resized(unsigned w, unsigned h)
140 XMoveResizeWindow(display.get_display(), subwnd, 0, 0, w, h);
142 glViewport(0, 0, w, h);
145 } // namespace Graphics