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