]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/bkgrnd2d/bkgrnd2d.cpp
merge branch work back into trunk
[xonotic/netradiant.git] / contrib / bkgrnd2d / bkgrnd2d.cpp
1 /*
2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
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 //
23 // bkgrnd2d Plugin
24 //
25 // Code by reyalP aka Reed Mideke
26 //
27 // Based on various other plugins
28 //
29
30 #include "bkgrnd2d.h"
31
32 CBackgroundRender render;
33
34 CBackgroundImage backgroundXY(XY),backgroundXZ(XZ),backgroundYZ(YZ);
35
36 CBackgroundRender::CBackgroundRender()
37 {
38         refCount = 1;
39 }
40
41 CBackgroundRender::~CBackgroundRender()
42 {
43 }
44
45 void CBackgroundRender::Register()
46 {
47         g_QglTable.m_pfnHookGL2DWindow( this );
48 }
49
50 void CBackgroundRender::Draw2D( VIEWTYPE vt )
51 {
52         switch(vt)
53         {
54         case XY:
55                 backgroundXY.Render();
56                 break;
57         case XZ:
58                 backgroundXZ.Render();
59                 break;
60         case YZ:
61                 backgroundYZ.Render();
62                 break;
63         }
64 }
65
66
67 CBackgroundImage::CBackgroundImage(VIEWTYPE vt)
68 {
69         m_tex = NULL;
70         m_alpha = 0.5;
71
72         // TODO, sensible defaults ? Or not show until we have extents ?
73         m_xmin = m_ymin = 0.0f;
74         m_xmax = m_ymax = 0.0f;
75
76         m_bActive = false;
77
78         m_vt = vt;
79
80         switch(m_vt)
81         {
82                 case XY:
83                         m_ix = 0;
84                         m_iy = 1;
85                         break;
86                 case XZ:
87                         m_ix = 0;
88                         m_iy = 2;
89                         break;
90                 case YZ:
91                         m_ix = 1;
92                         m_iy = 2;
93                         break;
94         }
95 }
96
97 /*
98  * should cleanup, but I don't think we can be sure it happens before our
99  * interfaces are gone
100 CBackgroundImage::~CBackgroundImage()
101 {
102 }
103 */
104
105 void CBackgroundImage::Cleanup()
106 {
107         if(m_tex) {
108                 g_QglTable.m_pfn_qglDeleteTextures(1,&m_tex->texture_number);
109                 g_free(m_tex);
110                 m_tex = NULL;
111         }
112 }
113
114 void CBackgroundImage::Render()
115 {
116         if (!m_bActive || !Valid())
117                 return;
118         g_QglTable.m_pfn_qglPushAttrib(GL_ALL_ATTRIB_BITS);
119
120         g_QglTable.m_pfn_qglEnable(GL_TEXTURE_2D);
121         g_QglTable.m_pfn_qglEnable(GL_BLEND);
122         g_QglTable.m_pfn_qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
123         g_QglTable.m_pfn_qglTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
124         g_QglTable.m_pfn_qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
125         g_QglTable.m_pfn_qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
126
127         g_QglTable.m_pfn_qglPolygonMode(GL_FRONT,GL_FILL);
128         // TODO, just so we can tell if we end up going the wrong way
129         // g_QglTable.m_pfn_qglPolygonMode(GL_BACK,GL_LINE);
130         // TODO any other state we should not assume ?
131
132         g_QglTable.m_pfn_qglBindTexture(GL_TEXTURE_2D, m_tex->texture_number);
133         g_QglTable.m_pfn_qglBegin(GL_QUADS);
134
135         g_QglTable.m_pfn_qglColor4f(1.0,1.0,1.0,m_alpha);
136         g_QglTable.m_pfn_qglTexCoord2f(0.0,1.0);
137         g_QglTable.m_pfn_qglVertex2f(m_xmin,m_ymin);
138
139         g_QglTable.m_pfn_qglTexCoord2f(1.0,1.0);
140         g_QglTable.m_pfn_qglVertex2f(m_xmax,m_ymin);
141
142         g_QglTable.m_pfn_qglTexCoord2f(1.0,0.0);
143         g_QglTable.m_pfn_qglVertex2f(m_xmax,m_ymax);
144
145         g_QglTable.m_pfn_qglTexCoord2f(0.0,0.0);
146         g_QglTable.m_pfn_qglVertex2f(m_xmin,m_ymax);
147
148         g_QglTable.m_pfn_qglEnd();
149         g_QglTable.m_pfn_qglBindTexture(GL_TEXTURE_2D, 0);
150
151         g_QglTable.m_pfn_qglPopAttrib();
152 }
153
154 bool CBackgroundImage::Load(const char *filename)
155 {
156         qtexture_t *newtex;
157
158         unsigned char *image = NULL; // gets allocated with what ? g_malloc
159         int width = 0, height = 0;
160
161         g_FuncTable.m_pfnLoadImage(filename,&image,&width,&height);
162
163         if(!image) {
164                 Syn_Printf(MSG_WARN "load %s failed\n",filename);
165                 return false;
166         }
167
168 // just in case we want to build for an old version
169 #ifdef BKGRND2D_JPG_WORKAROUND
170         if ( strlen(filename) > 4 && !strcmp(".jpg",filename + strlen(filename) - 4)) {
171                 Syn_Printf(MSG_PREFIX ".jpg workaround, clearing alpha channel\n");
172                 int size = width*height*4;
173                 int i;
174                 for (i = 3; i < size; i+=4) {
175                         image[i] = 255;
176                 }
177         }
178 #endif
179
180         //TODO bug for stored texture size
181         //TODO whose gl context are we in, anyway ?
182         newtex = g_FuncTable.m_pfnLoadTextureRGBA(image,width,height);
183
184         g_free(image);
185
186         if(!newtex) {
187                 Syn_Printf(MSG_WARN "image to texture failed\n");
188                 return false;
189         }
190
191         Cleanup();
192         m_tex = newtex;
193
194         g_FuncTable.m_pfnSysUpdateWindows(W_XY);
195
196         return true;
197 }
198
199 bool CBackgroundImage::SetExtentsMM()
200 {
201         entity_s *worldentity;
202         const char *val;
203         int xmin = 0, ymin = 0, xmax = 0, ymax = 0;
204
205         worldentity = (entity_s *)g_FuncTable.m_pfnGetEntityHandle(0);
206         if(!worldentity) {
207                 Syn_Printf(MSG_WARN "SetExtentsMM worldspawn not found\n");
208                 return false;
209         }
210         //TODO val is not NULL even if key does not exist
211         val = g_EntityTable.m_pfnValueForKey(worldentity,"mapcoordsmins");
212         if(!val || !val[0]) {
213                 Syn_Printf(MSG_WARN "SetExtentsMM mapcoordsmins not found\n");
214                 return false;
215         }
216 // we could be more robust
217 // note contortions due to splashs strange idea of min and max
218         if(sscanf(val, "%d %d",&xmin,&ymax) != 2)
219         {
220                 Syn_Printf(MSG_WARN "SetExtentsMM mapcoordsmins malformed\n");
221                 return false;
222         }
223
224         val = g_EntityTable.m_pfnValueForKey(worldentity,"mapcoordsmaxs");
225         if(!val || !val[0]) {
226                 Syn_Printf(MSG_WARN "SetExtentsMM mapcoordsmaxs not found\n");
227                 return false;
228         }
229         if(sscanf(val, "%d %d",&xmax,&ymin) != 2)
230         {
231                 Syn_Printf(MSG_WARN "SetExtentsMM mapcoordsmaxs malformed\n");
232                 return false;
233         }
234         //might do sanity check before we commit
235         m_xmin = (float)xmin;
236         m_ymin = (float)ymin;
237         m_xmax = (float)xmax;
238         m_ymax = (float)ymax;
239
240         g_FuncTable.m_pfnSysUpdateWindows(W_XY);
241         return true;
242 }
243
244 // TODO, this should just be exported from core
245 // ripped directly from radiant/select.cpp:Select_GetBounds
246 //
247 static bool get_selection_bounds (vec3_t mins, vec3_t maxs)
248 {
249         brush_t *b;
250         int             i;
251         brush_t *selected_brushes = g_DataTable.m_pfnSelectedBrushes();
252         //TODO should never happen
253         if(!selected_brushes) {
254           Sys_Printf (MSG_PREFIX "selected_brushes = NULL\n");
255           return false;
256         }
257         // this should mean no selection
258         if(selected_brushes == selected_brushes->next) {
259           Sys_Printf (MSG_PREFIX "nothing selected\n");
260
261           return false;
262         }
263
264         for (i=0 ; i<3 ; i++)
265         {
266                 mins[i] = 99999;
267                 maxs[i] = -99999;
268         }
269
270         for (b=selected_brushes->next ; b != selected_brushes ; b=b->next)
271         {
272                 if (b->owner->eclass->fixedsize)
273                 {
274                         for (i=0 ; i<3 ; i++)
275                         {
276                                 if (b->owner->origin[i] < mins[i])
277                                         mins[i] = b->owner->origin[i];
278                                 if (b->owner->origin[i] > maxs[i])
279                                         maxs[i] = b->owner->origin[i];
280                         }
281                 }
282                 else
283                 {
284                         for (i=0 ; i<3 ; i++)
285                         {
286                                 if (b->mins[i] < mins[i])
287                                         mins[i] = b->mins[i];
288                                 if (b->maxs[i] > maxs[i])
289                                         maxs[i] = b->maxs[i];
290                         }
291                 }
292         }
293   return true;
294 }
295
296 bool CBackgroundImage::SetExtentsSel()
297 {
298         vec3_t mins,maxs;
299
300         if(!get_selection_bounds(mins,maxs))
301                 return false;
302
303         if(((int)mins[m_ix] == (int)maxs[m_ix]) ||
304      ((int)mins[m_iy] == (int)maxs[m_iy])) {
305                 Syn_Printf(MSG_PREFIX "tiny selection\n");
306                 return false;
307         }
308
309         m_xmin = mins[m_ix];
310         m_ymin = mins[m_iy];
311         m_xmax = maxs[m_ix];
312         m_ymax = maxs[m_iy];
313
314         g_FuncTable.m_pfnSysUpdateWindows(W_XY);
315
316   return true;
317 }
318