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