Add more structure constructor tests.
[piglit/hramrach.git] / tests / glean / rc.cpp
blob8cc95b3d9dd286a101c5facb12591974fa422a8e
1 // BEGIN_COPYRIGHT -*- glean -*-
2 //
3 // Copyright (C) 1999 Allen Akin All Rights Reserved.
4 //
5 // Permission is hereby granted, free of charge, to any person
6 // obtaining a copy of this software and associated documentation
7 // files (the "Software"), to deal in the Software without
8 // restriction, including without limitation the rights to use,
9 // copy, modify, merge, publish, distribute, sublicense, and/or
10 // sell copies of the Software, and to permit persons to whom the
11 // Software is furnished to do so, subject to the following
12 // conditions:
13 //
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the
16 // Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
19 // KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
20 // WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
21 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL ALLEN AKIN BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 // AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
24 // OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 // DEALINGS IN THE SOFTWARE.
26 //
27 // END_COPYRIGHT
32 // rc.cpp: implementation of rendering context utilities
34 #include <iostream>
35 #include <algorithm>
36 #include "rc.h"
37 #include "dsconfig.h"
38 #include "winsys.h"
40 namespace {
42 #if defined (__WIN__)
44 // XXX
45 // wglCreateContext requires a handle to a device context.
46 // The ctor of RenderingContext doesn't know which window
47 // it is creating a surface for, only what the pixelformat
48 // of that window is. The hDC passed to wglCreateContext
49 // doesn't have to be the same as the one use in SwapBuffers
50 // or wglMakeCurrent, their pixelformats have to be the
51 // same though. A limitation is that the pixelformat of
52 // a window can only be set once. That is why a
53 // temporary window is created.
56 HGLRC create_context(GLEAN::DrawingSurfaceConfig &c)
58 HWND hwnd = CreateWindow("STATIC","temp",WS_POPUP,
59 CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
60 0,0,GetModuleHandle(NULL),0);
62 if (!hwnd)
63 return 0;
65 HDC hDC = GetDC(hwnd);
66 if (!hDC)
67 return 0;
69 PIXELFORMATDESCRIPTOR pfd;
71 if (!SetPixelFormat(hDC,c.pfdID,&pfd))
73 ReleaseDC(hwnd,hDC);
74 DestroyWindow(hwnd);
75 return 0;
78 HGLRC rc = wglCreateContext(hDC);
79 if (!rc)
80 return 0;
82 ReleaseDC(hwnd,hDC);
83 DestroyWindow(hwnd);
85 return rc;
88 #endif
90 } // anonymous namespace
93 namespace GLEAN {
95 ///////////////////////////////////////////////////////////////////////////////
96 // Constructors
97 ///////////////////////////////////////////////////////////////////////////////
98 RenderingContext::RenderingContext(WindowSystem& ws, DrawingSurfaceConfig& c,
99 RenderingContext* share, bool direct) {
101 // Link back to enclosing window system, so as to simplify bookkeeping:
102 winSys = &ws;
103 ws.contexts.push_back(this);
105 # if defined(__X11__)
107 # if defined(GLX_VERSION_1_3)
109 if (ws.GLXVersMajor == 1 && ws.GLXVersMinor < 3)
110 goto legacyMethod;
111 // XXX Need GLX 1.3 rc constructor code
112 // For now, just fall through.
114 # endif
116 legacyMethod:
117 // Create the rendering context:
118 rc = glXCreateContext(winSys->dpy, c.vi, (share? share->rc: 0),
119 direct? True: False);
120 if (!rc)
121 throw Error();
122 // XXX Ideally, we would deal with X11 and GLX errors here, too
123 // (Badmatch, BadValue, GLXBadContext, BadAlloc)
125 # elif defined(__WIN__)
127 rc = create_context(c);
128 if (!rc)
129 throw Error();
130 # elif defined(__AGL__)
131 rc = aglCreateContext(c.pf, NULL);
132 if(rc == NULL)
133 throw Error();
134 # endif
136 } // RenderingContext::RenderingContext
138 ///////////////////////////////////////////////////////////////////////////////
139 // Destructors
140 ///////////////////////////////////////////////////////////////////////////////
142 RenderingContext::~RenderingContext() {
143 remove(winSys->contexts.begin(), winSys->contexts.end(), this);
144 # if defined(__X11__)
145 # if defined(GLX_VERSION_1_3)
146 if (winSys->GLXVersMajor == 1 && winSys->GLXVersMinor < 3)
147 goto legacyMethod;
148 // XXX Need to write GLX 1.3 rc destructor
149 // For now, just fall through.
150 # endif
151 legacyMethod:
152 glXDestroyContext(winSys->dpy, rc);
153 # elif defined(__WIN__)
154 wglDeleteContext(rc);
155 # endif
156 } // RenderingContext::~RenderingContext
159 } // namespace GLEAN