]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/gtkutil/glwidget.cpp
fix invalid use of <glib/...> headers
[xonotic/netradiant.git] / libs / gtkutil / glwidget.cpp
1 /*
2    Copyright (C) 2001-2006, William Joseph.
3    All Rights Reserved.
4
5    This file is part of GtkRadiant.
6
7    GtkRadiant is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    GtkRadiant is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GtkRadiant; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 // OpenGL widget based on GtkGLExt
23
24 #include "glwidget.h"
25
26 #include "debugging/debugging.h"
27
28 #include "igl.h"
29
30 #include <gtk/gtkdrawingarea.h>
31 #include <gtk/gtkglwidget.h>
32
33 #include "pointer.h"
34
35 void ( *GLWidget_sharedContextCreated )() = 0;
36 void ( *GLWidget_sharedContextDestroyed )() = 0;
37
38
39 typedef int* attribs_t;
40 struct config_t
41 {
42         const char* name;
43         attribs_t attribs;
44 };
45 typedef const config_t* configs_iterator;
46
47 int config_rgba32[] = {
48         GDK_GL_RGBA,
49         GDK_GL_DOUBLEBUFFER,
50         GDK_GL_BUFFER_SIZE, 24,
51         GDK_GL_ATTRIB_LIST_NONE,
52 };
53
54 int config_rgba[] = {
55         GDK_GL_RGBA,
56         GDK_GL_DOUBLEBUFFER,
57         GDK_GL_BUFFER_SIZE, 16,
58         GDK_GL_ATTRIB_LIST_NONE,
59 };
60
61 const config_t configs[] = {
62         {
63                 "colour-buffer = 32bpp, depth-buffer = none",
64                 config_rgba32,
65         },
66         {
67                 "colour-buffer = 16bpp, depth-buffer = none",
68                 config_rgba,
69         }
70 };
71
72 GdkGLConfig* glconfig_new(){
73         GdkGLConfig* glconfig = 0;
74
75         for ( configs_iterator i = configs, end = configs + 2; i != end; ++i )
76         {
77                 glconfig = gdk_gl_config_new( ( *i ).attribs );
78                 if ( glconfig != 0 ) {
79                         globalOutputStream() << "OpenGL window configuration: " << ( *i ).name << "\n";
80                         return glconfig;
81                 }
82         }
83
84         globalOutputStream() << "OpenGL window configuration: colour-buffer = auto, depth-buffer = none\n";
85         return gdk_gl_config_new_by_mode( (GdkGLConfigMode)( GDK_GL_MODE_RGBA | GDK_GL_MODE_DOUBLE ) );
86 }
87
88 int config_rgba32_depth32[] = {
89         GDK_GL_RGBA,
90         GDK_GL_DOUBLEBUFFER,
91         GDK_GL_BUFFER_SIZE, 24,
92         GDK_GL_DEPTH_SIZE, 32,
93         GDK_GL_ATTRIB_LIST_NONE,
94 };
95
96 int config_rgba32_depth24[] = {
97         GDK_GL_RGBA,
98         GDK_GL_DOUBLEBUFFER,
99         GDK_GL_BUFFER_SIZE, 24,
100         GDK_GL_DEPTH_SIZE, 24,
101         GDK_GL_ATTRIB_LIST_NONE,
102 };
103
104 int config_rgba32_depth16[] = {
105         GDK_GL_RGBA,
106         GDK_GL_DOUBLEBUFFER,
107         GDK_GL_BUFFER_SIZE, 24,
108         GDK_GL_DEPTH_SIZE, 16,
109         GDK_GL_ATTRIB_LIST_NONE,
110 };
111
112 int config_rgba32_depth[] = {
113         GDK_GL_RGBA,
114         GDK_GL_DOUBLEBUFFER,
115         GDK_GL_BUFFER_SIZE, 24,
116         GDK_GL_DEPTH_SIZE, 1,
117         GDK_GL_ATTRIB_LIST_NONE,
118 };
119
120 int config_rgba_depth16[] = {
121         GDK_GL_RGBA,
122         GDK_GL_DOUBLEBUFFER,
123         GDK_GL_BUFFER_SIZE, 16,
124         GDK_GL_DEPTH_SIZE, 16,
125         GDK_GL_ATTRIB_LIST_NONE,
126 };
127
128 int config_rgba_depth[] = {
129         GDK_GL_RGBA,
130         GDK_GL_DOUBLEBUFFER,
131         GDK_GL_BUFFER_SIZE, 16,
132         GDK_GL_DEPTH_SIZE, 1,
133         GDK_GL_ATTRIB_LIST_NONE,
134 };
135
136 const config_t configs_with_depth[] =
137 {
138         {
139                 "colour-buffer = 32bpp, depth-buffer = 32bpp",
140                 config_rgba32_depth32,
141         },
142         {
143                 "colour-buffer = 32bpp, depth-buffer = 24bpp",
144                 config_rgba32_depth24,
145         },
146         {
147                 "colour-buffer = 32bpp, depth-buffer = 16bpp",
148                 config_rgba32_depth16,
149         },
150         {
151                 "colour-buffer = 32bpp, depth-buffer = auto",
152                 config_rgba32_depth,
153         },
154         {
155                 "colour-buffer = 16bpp, depth-buffer = 16bpp",
156                 config_rgba_depth16,
157         },
158         {
159                 "colour-buffer = auto, depth-buffer = auto",
160                 config_rgba_depth,
161         },
162 };
163
164 GdkGLConfig* glconfig_new_with_depth(){
165         GdkGLConfig* glconfig = 0;
166
167         for ( configs_iterator i = configs_with_depth, end = configs_with_depth + 6; i != end; ++i )
168         {
169                 glconfig = gdk_gl_config_new( ( *i ).attribs );
170                 if ( glconfig != 0 ) {
171                         globalOutputStream() << "OpenGL window configuration: " << ( *i ).name << "\n";
172                         return glconfig;
173                 }
174         }
175
176         globalOutputStream() << "OpenGL window configuration: colour-buffer = auto, depth-buffer = auto (fallback)\n";
177         return gdk_gl_config_new_by_mode( (GdkGLConfigMode)( GDK_GL_MODE_RGBA | GDK_GL_MODE_DOUBLE | GDK_GL_MODE_DEPTH ) );
178 }
179
180 unsigned int g_context_count = 0;
181
182 namespace
183 {
184 GtkWidget* g_shared = 0;
185 }
186
187 gint glwidget_context_created( GtkWidget* widget, gpointer data ){
188         if ( ++g_context_count == 1 ) {
189                 g_shared = widget;
190                 gtk_widget_ref( g_shared );
191
192                 glwidget_make_current( g_shared );
193                 GlobalOpenGL().contextValid = true;
194
195                 GLWidget_sharedContextCreated();
196         }
197         return FALSE;
198 }
199
200 gint glwidget_context_destroyed( GtkWidget* widget, gpointer data ){
201         if ( --g_context_count == 0 ) {
202                 GlobalOpenGL().contextValid = false;
203
204                 GLWidget_sharedContextDestroyed();
205
206                 gtk_widget_unref( g_shared );
207                 g_shared = 0;
208         }
209         return FALSE;
210 }
211
212 gboolean glwidget_enable_gl( GtkWidget* widget, GtkWidget* widget2, gpointer data ){
213         if ( widget2 == 0 && !gtk_widget_is_gl_capable( widget ) ) {
214                 GdkGLConfig* glconfig = ( g_object_get_data( G_OBJECT( widget ), "zbuffer" ) ) ? glconfig_new_with_depth() : glconfig_new();
215                 ASSERT_MESSAGE( glconfig != 0, "failed to create OpenGL config" );
216
217                 gtk_widget_set_gl_capability( widget, glconfig, g_shared != 0 ? gtk_widget_get_gl_context( g_shared ) : 0,  TRUE, GDK_GL_RGBA_TYPE );
218
219                 gtk_widget_realize( widget );
220                 if ( g_shared == 0 ) {
221                         g_shared = widget;
222                 }
223
224                 // free glconfig?
225         }
226         return FALSE;
227 }
228
229 GtkWidget* glwidget_new( gboolean zbuffer ){
230         GtkWidget* widget = gtk_drawing_area_new();
231
232         g_object_set_data( G_OBJECT( widget ), "zbuffer", gint_to_pointer( zbuffer ) );
233
234         g_signal_connect( G_OBJECT( widget ), "hierarchy-changed", G_CALLBACK( glwidget_enable_gl ), 0 );
235
236         g_signal_connect( G_OBJECT( widget ), "realize", G_CALLBACK( glwidget_context_created ), 0 );
237         g_signal_connect( G_OBJECT( widget ), "unrealize", G_CALLBACK( glwidget_context_destroyed ), 0 );
238
239         return widget;
240 }
241
242 void glwidget_destroy_context( GtkWidget *widget ){
243 }
244
245 void glwidget_create_context( GtkWidget *widget ){
246 }
247
248 void glwidget_swap_buffers( GtkWidget *widget ){
249         GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable( widget );
250         gdk_gl_drawable_swap_buffers( gldrawable );
251 }
252
253 gboolean glwidget_make_current( GtkWidget *widget ){
254         GdkGLContext *glcontext = gtk_widget_get_gl_context( widget );
255         GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable( widget );
256         return gdk_gl_drawable_gl_begin( gldrawable, glcontext );
257 }