Fix GTK_CRITICAL errors
[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 "igl.h"
27
28 void (*GLWidget_sharedContextCreated)() = 0;
29
30 void (*GLWidget_sharedContextDestroyed)() = 0;
31
32 unsigned int g_context_count = 0;
33
34 ui::GLArea g_shared{ui::null};
35
36 static void _glwidget_context_created(ui::GLArea self, void *data)
37 {
38     if (++g_context_count == 1) {
39         g_shared = self;
40         g_object_ref(g_shared._handle);
41
42         glwidget_make_current(g_shared);
43         GlobalOpenGL().contextValid = true;
44
45         GLWidget_sharedContextCreated();
46     }
47 }
48
49 static void _glwidget_context_destroyed(ui::GLArea self, void *data)
50 {
51     if (--g_context_count == 0) {
52         GlobalOpenGL().contextValid = false;
53
54         GLWidget_sharedContextDestroyed();
55
56         g_shared.unref();
57         g_shared = ui::GLArea(ui::null);
58     }
59 }
60
61 void glwidget_destroy_context(ui::GLArea self)
62 {
63 }
64
65 void glwidget_create_context(ui::GLArea self)
66 {
67 }
68
69 #if GTK_TARGET == 3
70
71 #include <gtk/gtk.h>
72
73 static GdkGLContext *glwidget_context_created(ui::GLArea self)
74 {
75     _glwidget_context_created(self, nullptr);
76     return gtk_gl_area_get_context(self);
77 }
78
79 ui::GLArea glwidget_new(bool zbuffer)
80 {
81     auto self = ui::GLArea::from(GTK_GL_AREA(gtk_gl_area_new()));
82     gtk_gl_area_set_has_depth_buffer(self, zbuffer);
83     gtk_gl_area_set_auto_render(self, false);
84
85     self.connect("realize", G_CALLBACK(glwidget_context_created), nullptr);
86     return self;
87 }
88
89 bool glwidget_make_current(ui::GLArea self)
90 {
91 //    if (!g_context_count) {
92 //        glwidget_context_created(self);
93 //    }
94     gtk_gl_area_make_current(self);
95     auto valid = GlobalOpenGL().contextValid;
96     return true;
97 }
98
99 void glwidget_swap_buffers(ui::GLArea self)
100 {
101     g_assert(GTK_IS_GL_AREA(self));
102     gtk_gl_area_queue_render(self);
103 }
104
105 #endif
106
107 #if GTK_TARGET == 2
108
109 #include <gtk/gtk.h>
110 #include <gtk/gtkglwidget.h>
111
112 #include "pointer.h"
113
114 struct config_t {
115     const char *name;
116     int *attribs;
117 };
118 typedef const config_t *configs_iterator;
119
120 static int config_rgba32[] = {
121         GDK_GL_RGBA,
122         GDK_GL_DOUBLEBUFFER,
123         GDK_GL_BUFFER_SIZE, 24,
124         GDK_GL_ATTRIB_LIST_NONE,
125 };
126
127 static int config_rgba[] = {
128         GDK_GL_RGBA,
129         GDK_GL_DOUBLEBUFFER,
130         GDK_GL_BUFFER_SIZE, 16,
131         GDK_GL_ATTRIB_LIST_NONE,
132 };
133
134 static const config_t configs[] = {
135         {
136                 "colour-buffer = 32bpp, depth-buffer = none",
137                 config_rgba32,
138         },
139         {
140                 "colour-buffer = 16bpp, depth-buffer = none",
141                 config_rgba,
142         }
143 };
144
145 static GdkGLConfig *glconfig_new()
146 {
147     for (configs_iterator i = configs, end = configs + 2; i != end; ++i) {
148         if (auto glconfig = gdk_gl_config_new(i->attribs)) {
149             globalOutputStream() << "OpenGL window configuration: " << i->name << "\n";
150             return glconfig;
151         }
152     }
153     globalOutputStream() << "OpenGL window configuration: colour-buffer = auto, depth-buffer = none\n";
154     return gdk_gl_config_new_by_mode((GdkGLConfigMode) (GDK_GL_MODE_RGBA | GDK_GL_MODE_DOUBLE));
155 }
156
157 static int config_rgba32_depth32[] = {
158         GDK_GL_RGBA,
159         GDK_GL_DOUBLEBUFFER,
160         GDK_GL_BUFFER_SIZE,
161         24,
162         GDK_GL_DEPTH_SIZE,
163         32,
164         GDK_GL_ATTRIB_LIST_NONE,
165 };
166
167 static int config_rgba32_depth24[] = {
168         GDK_GL_RGBA,
169         GDK_GL_DOUBLEBUFFER,
170         GDK_GL_BUFFER_SIZE, 24,
171         GDK_GL_DEPTH_SIZE, 24,
172         GDK_GL_ATTRIB_LIST_NONE,
173 };
174
175 static int config_rgba32_depth16[] = {
176         GDK_GL_RGBA,
177         GDK_GL_DOUBLEBUFFER,
178         GDK_GL_BUFFER_SIZE, 24,
179         GDK_GL_DEPTH_SIZE, 16,
180         GDK_GL_ATTRIB_LIST_NONE,
181 };
182
183 static int config_rgba32_depth[] = {
184         GDK_GL_RGBA,
185         GDK_GL_DOUBLEBUFFER,
186         GDK_GL_BUFFER_SIZE, 24,
187         GDK_GL_DEPTH_SIZE, 1,
188         GDK_GL_ATTRIB_LIST_NONE,
189 };
190
191 static int config_rgba_depth16[] = {
192         GDK_GL_RGBA,
193         GDK_GL_DOUBLEBUFFER,
194         GDK_GL_BUFFER_SIZE, 16,
195         GDK_GL_DEPTH_SIZE, 16,
196         GDK_GL_ATTRIB_LIST_NONE,
197 };
198
199 static int config_rgba_depth[] = {
200         GDK_GL_RGBA,
201         GDK_GL_DOUBLEBUFFER,
202         GDK_GL_BUFFER_SIZE, 16,
203         GDK_GL_DEPTH_SIZE, 1,
204         GDK_GL_ATTRIB_LIST_NONE,
205 };
206
207 static const config_t configs_with_depth[] =
208         {
209                 {
210                         "colour-buffer = 32bpp, depth-buffer = 32bpp",
211                         config_rgba32_depth32,
212                 },
213                 {
214                         "colour-buffer = 32bpp, depth-buffer = 24bpp",
215                         config_rgba32_depth24,
216                 },
217                 {
218                         "colour-buffer = 32bpp, depth-buffer = 16bpp",
219                         config_rgba32_depth16,
220                 },
221                 {
222                         "colour-buffer = 32bpp, depth-buffer = auto",
223                         config_rgba32_depth,
224                 },
225                 {
226                         "colour-buffer = 16bpp, depth-buffer = 16bpp",
227                         config_rgba_depth16,
228                 },
229                 {
230                         "colour-buffer = auto, depth-buffer = auto",
231                         config_rgba_depth,
232                 },
233         };
234
235 static GdkGLConfig *glconfig_new_with_depth()
236 {
237     for (configs_iterator i = configs_with_depth, end = configs_with_depth + 6; i != end; ++i) {
238         if (auto glconfig = gdk_gl_config_new(i->attribs)) {
239             globalOutputStream() << "OpenGL window configuration: " << i->name << "\n";
240             return glconfig;
241         }
242     }
243     globalOutputStream() << "OpenGL window configuration: colour-buffer = auto, depth-buffer = auto (fallback)\n";
244     return gdk_gl_config_new_by_mode((GdkGLConfigMode) (GDK_GL_MODE_RGBA | GDK_GL_MODE_DOUBLE | GDK_GL_MODE_DEPTH));
245 }
246
247 static int glwidget_context_created(ui::GLArea self, void *data)
248 {
249     _glwidget_context_created(self, data);
250     return false;
251 }
252
253 int glwidget_context_destroyed(ui::GLArea self, void *data)
254 {
255     _glwidget_context_destroyed(self, data);
256     return false;
257 }
258
259 static bool glwidget_enable_gl(ui::GLArea self, ui::Widget root, gpointer data)
260 {
261     if (!root && !gtk_widget_is_gl_capable(self)) {
262         const auto zbuffer = g_object_get_data(G_OBJECT(self), "zbuffer");
263         GdkGLConfig *glconfig = zbuffer ? glconfig_new_with_depth() : glconfig_new();
264         ASSERT_MESSAGE(glconfig, "failed to create OpenGL config");
265
266         const auto share_list = g_shared ? gtk_widget_get_gl_context(g_shared) : nullptr;
267         gtk_widget_set_gl_capability(self, glconfig, share_list, true, GDK_GL_RGBA_TYPE);
268
269         gtk_widget_realize(self);
270         if (!g_shared) {
271             g_shared = self;
272         }
273         // free glconfig?
274     }
275     return false;
276 }
277
278 ui::GLArea glwidget_new(bool zbuffer)
279 {
280     auto self = ui::GLArea::from(gtk_drawing_area_new());
281
282     g_object_set_data(G_OBJECT(self), "zbuffer", gint_to_pointer(zbuffer));
283
284     self.connect("hierarchy-changed", G_CALLBACK(glwidget_enable_gl), 0);
285
286     self.connect("realize", G_CALLBACK(glwidget_context_created), 0);
287     self.connect("unrealize", G_CALLBACK(glwidget_context_destroyed), 0);
288
289     return self;
290 }
291
292 void glwidget_swap_buffers(ui::GLArea self)
293 {
294     GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(self);
295     gdk_gl_drawable_swap_buffers(gldrawable);
296 }
297
298 bool glwidget_make_current(ui::GLArea self)
299 {
300     GdkGLContext *glcontext = gtk_widget_get_gl_context(self);
301     GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(self);
302     return gdk_gl_drawable_gl_begin(gldrawable, glcontext);
303 }
304
305 #endif