]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/glwidget.cpp
* added filter api to create new filters from within plugins
[xonotic/netradiant.git] / radiant / glwidget.cpp
1 /*
2 Copyright (c) 2001, Loki software, inc.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification, 
6 are permitted provided that the following conditions are met:
7
8 Redistributions of source code must retain the above copyright notice, this list 
9 of conditions and the following disclaimer.
10
11 Redistributions in binary form must reproduce the above copyright notice, this
12 list of conditions and the following disclaimer in the documentation and/or
13 other materials provided with the distribution.
14
15 Neither the name of Loki software nor the names of its contributors may be used 
16 to endorse or promote products derived from this software without specific prior 
17 written permission. 
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' 
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
22 DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY 
23 DIRECT,INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
26 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
29 */
30
31 // OpenGL widget based on GtkGLExt
32
33 #include "stdafx.h"
34
35 #include <gtk/gtkgl.h>
36 #include "glwidget.h"
37 #include "qgl.h"
38
39 typedef int* attribs_t;
40 typedef const attribs_t* configs_iterator;
41
42 int config_rgba32[] = {
43   GDK_GL_RGBA,
44   GDK_GL_DOUBLEBUFFER,
45   GDK_GL_RED_SIZE, 8,
46   GDK_GL_BLUE_SIZE, 8,
47   GDK_GL_GREEN_SIZE, 8,
48   GDK_GL_ALPHA_SIZE, 8,
49   GDK_GL_ATTRIB_LIST_NONE,
50 };
51
52 int config_rgba[] = {
53   GDK_GL_RGBA,
54   GDK_GL_DOUBLEBUFFER,
55   GDK_GL_RED_SIZE, 1,
56   GDK_GL_BLUE_SIZE, 1,
57   GDK_GL_GREEN_SIZE, 1,
58   GDK_GL_ALPHA_SIZE, 1,
59   GDK_GL_ATTRIB_LIST_NONE,
60 };
61
62 const attribs_t configs[] = {
63   config_rgba32,
64   config_rgba,
65 };
66
67 GdkGLConfig* glconfig_new()
68 {
69   GdkGLConfig* glconfig = NULL;
70
71   for(configs_iterator i = configs, end = configs + 2; i != end && glconfig == NULL; ++i)
72   {
73     glconfig = gdk_gl_config_new(*i);
74   }
75
76   if(glconfig == NULL)
77   {
78     return gdk_gl_config_new_by_mode((GdkGLConfigMode)(GDK_GL_MODE_RGBA | GDK_GL_MODE_DOUBLE));
79   }
80
81   return glconfig;
82 }
83
84 int config_rgba32_depth32[] = {
85   GDK_GL_RGBA,
86   GDK_GL_DOUBLEBUFFER,
87   GDK_GL_RED_SIZE, 8,
88   GDK_GL_BLUE_SIZE, 8,
89   GDK_GL_GREEN_SIZE, 8,
90   GDK_GL_ALPHA_SIZE, 8,
91   GDK_GL_DEPTH_SIZE, 32,
92   GDK_GL_ATTRIB_LIST_NONE,
93 };
94
95 int config_rgba32_depth24[] = {
96   GDK_GL_RGBA,
97   GDK_GL_DOUBLEBUFFER,
98   GDK_GL_RED_SIZE, 8,
99   GDK_GL_BLUE_SIZE, 8,
100   GDK_GL_GREEN_SIZE, 8,
101   GDK_GL_ALPHA_SIZE, 8,
102   GDK_GL_DEPTH_SIZE, 24,
103   GDK_GL_ATTRIB_LIST_NONE,
104 };
105
106 int config_rgba32_depth16[] = {
107   GDK_GL_RGBA,
108   GDK_GL_DOUBLEBUFFER,
109   GDK_GL_RED_SIZE, 8,
110   GDK_GL_BLUE_SIZE, 8,
111   GDK_GL_GREEN_SIZE, 8,
112   GDK_GL_ALPHA_SIZE, 8,
113   GDK_GL_DEPTH_SIZE, 16,
114   GDK_GL_ATTRIB_LIST_NONE,
115 };
116
117 int config_rgba32_depth[] = {
118   GDK_GL_RGBA,
119   GDK_GL_DOUBLEBUFFER,
120   GDK_GL_RED_SIZE, 8,
121   GDK_GL_BLUE_SIZE, 8,
122   GDK_GL_GREEN_SIZE, 8,
123   GDK_GL_ALPHA_SIZE, 8,
124   GDK_GL_DEPTH_SIZE, 1,
125   GDK_GL_ATTRIB_LIST_NONE,
126 };
127
128 int config_rgba_depth16[] = {
129   GDK_GL_RGBA,
130   GDK_GL_DOUBLEBUFFER,
131   GDK_GL_RED_SIZE, 1,
132   GDK_GL_BLUE_SIZE, 1,
133   GDK_GL_GREEN_SIZE, 1,
134   GDK_GL_ALPHA_SIZE, 1,
135   GDK_GL_DEPTH_SIZE, 16,
136   GDK_GL_ATTRIB_LIST_NONE,
137 };
138
139 int config_rgba_depth[] = {
140   GDK_GL_RGBA,
141   GDK_GL_DOUBLEBUFFER,
142   GDK_GL_RED_SIZE, 1,
143   GDK_GL_BLUE_SIZE, 1,
144   GDK_GL_GREEN_SIZE, 1,
145   GDK_GL_ALPHA_SIZE, 1,
146   GDK_GL_DEPTH_SIZE, 1,
147   GDK_GL_ATTRIB_LIST_NONE,
148 };
149
150 const attribs_t configs_with_depth[] = 
151 {
152   config_rgba32_depth32,
153   config_rgba32_depth24,
154   config_rgba32_depth16,
155   config_rgba32_depth,
156   config_rgba_depth16,
157   config_rgba_depth,
158 };
159
160 GdkGLConfig* glconfig_new_with_depth()
161 {
162   GdkGLConfig* glconfig = NULL;
163
164   for(configs_iterator i = configs_with_depth, end = configs_with_depth + 6; i != end && glconfig == NULL; ++i)
165   {
166     glconfig = gdk_gl_config_new(*i);
167   }
168
169   if(glconfig == NULL)
170   {
171     return gdk_gl_config_new_by_mode((GdkGLConfigMode)(GDK_GL_MODE_RGBA | GDK_GL_MODE_DOUBLE | GDK_GL_MODE_DEPTH));
172   }
173
174   return glconfig;
175 }
176
177 GtkWidget* WINAPI gtk_glwidget_new (gboolean zbuffer, GtkWidget* share)
178 {
179   GtkWidget* drawing_area = gtk_drawing_area_new();
180   GdkGLConfig* glconfig = (zbuffer) ? glconfig_new_with_depth() : glconfig_new();
181   GdkGLContext* shared_context = (share) ? gtk_widget_get_gl_context(share) : NULL;
182
183   gtk_widget_set_gl_capability (drawing_area, glconfig, shared_context, TRUE, GDK_GL_RGBA_TYPE);
184
185   return drawing_area;
186 }
187
188 void WINAPI gtk_glwidget_destroy_context (GtkWidget *widget)
189 {
190 }
191
192 void WINAPI gtk_glwidget_create_context (GtkWidget *widget)
193 {
194 }
195
196 void WINAPI gtk_glwidget_swap_buffers (GtkWidget *widget)
197 {
198   GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable (widget);
199   gdk_gl_drawable_swap_buffers (gldrawable);
200 }
201
202 gboolean WINAPI gtk_glwidget_make_current (GtkWidget *widget)
203 {
204   GdkGLContext *glcontext = gtk_widget_get_gl_context (widget);
205   GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable (widget);
206   return gdk_gl_drawable_gl_begin (gldrawable, glcontext);
207 }
208
209 GLuint font_list_base;
210 static gchar font_string[] = "courier 8";
211 static gint font_height;
212
213 void gtk_glwidget_create_font (GtkWidget *widget)
214 {
215   GdkGLContext *glcontext = gtk_widget_get_gl_context (widget);
216   GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable (widget);
217
218   PangoFontDescription *font_desc;
219   PangoFont *font;
220   PangoFontMetrics *font_metrics;
221
222   font_list_base = qglGenLists (256);
223
224   font_desc = pango_font_description_from_string (font_string);
225
226   font = gdk_gl_font_use_pango_font (font_desc, 0, 256, font_list_base);
227
228   if(font != NULL)
229   {
230     font_metrics = pango_font_get_metrics (font, NULL);
231
232     font_height = pango_font_metrics_get_ascent (font_metrics) +
233                   pango_font_metrics_get_descent (font_metrics);
234     font_height = PANGO_PIXELS (font_height);
235
236     pango_font_metrics_unref (font_metrics);
237   }
238
239   pango_font_description_free (font_desc);
240 }
241
242
243 void gtk_glwidget_print_string(const char *s)
244 {
245   qglListBase(font_list_base);
246   qglCallLists(strlen(s), GL_UNSIGNED_BYTE, (unsigned char *)s);
247 }
248
249 void gtk_glwidget_print_char(char s)
250 {
251   qglListBase(font_list_base);
252   qglCallLists(1, GL_UNSIGNED_BYTE, (unsigned char *) &s);
253 }
254