more eol-style
[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 // http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=900
170 #ifdef BKGRND2D_JPG_WORKAROUND
171         if ( strlen(filename) > 4 && !strcmp(".jpg",filename + strlen(filename) - 4)) {
172                 Syn_Printf(MSG_PREFIX ".jpg workaround, clearing alpha channel\n");
173                 int size = width*height*4;
174                 int i;
175                 for (i = 3; i < size; i+=4) {
176                         image[i] = 255;
177                 }
178         }
179 #endif
180         
181         //TODO bug for stored texture size
182         //TODO whose gl context are we in, anyway ?
183         newtex = g_FuncTable.m_pfnLoadTextureRGBA(image,width,height);
184
185         g_free(image);
186
187         if(!newtex) {
188                 Syn_Printf(MSG_WARN "image to texture failed\n");
189                 return false;
190         }
191
192         Cleanup();
193         m_tex = newtex;
194
195         g_FuncTable.m_pfnSysUpdateWindows(W_XY);
196
197         return true;
198 }
199
200 bool CBackgroundImage::SetExtentsMM()
201 {
202         entity_s *worldentity;
203         const char *val;
204         int xmin = 0, ymin = 0, xmax = 0, ymax = 0;
205
206         worldentity = (entity_s *)g_FuncTable.m_pfnGetEntityHandle(0);
207         if(!worldentity) {
208                 Syn_Printf(MSG_WARN "SetExtentsMM worldspawn not found\n");
209                 return false;
210         }
211         //TODO val is not NULL even if key does not exist
212         val = g_EntityTable.m_pfnValueForKey(worldentity,"mapcoordsmins");
213         if(!val || !val[0]) {
214                 Syn_Printf(MSG_WARN "SetExtentsMM mapcoordsmins not found\n");
215                 return false;
216         }
217 // we could be more robust
218 // note contortions due to splashs strange idea of min and max
219         if(sscanf(val, "%d %d",&xmin,&ymax) != 2)
220         {
221                 Syn_Printf(MSG_WARN "SetExtentsMM mapcoordsmins malformed\n");
222                 return false;
223         }
224
225         val = g_EntityTable.m_pfnValueForKey(worldentity,"mapcoordsmaxs");
226         if(!val || !val[0]) {
227                 Syn_Printf(MSG_WARN "SetExtentsMM mapcoordsmaxs not found\n");
228                 return false;
229         }
230         if(sscanf(val, "%d %d",&xmax,&ymin) != 2)
231         {
232                 Syn_Printf(MSG_WARN "SetExtentsMM mapcoordsmaxs malformed\n");
233                 return false;
234         }
235         //might do sanity check before we commit
236         m_xmin = (float)xmin;
237         m_ymin = (float)ymin;
238         m_xmax = (float)xmax;
239         m_ymax = (float)ymax;
240
241         g_FuncTable.m_pfnSysUpdateWindows(W_XY);
242         return true;
243 }
244
245 // TODO, this should just be exported from core
246 // ripped directly from radiant/select.cpp:Select_GetBounds
247 //
248 static bool get_selection_bounds (vec3_t mins, vec3_t maxs)
249 {
250         brush_t *b;
251         int             i;
252         brush_t *selected_brushes = g_DataTable.m_pfnSelectedBrushes();
253         //TODO should never happen
254         if(!selected_brushes) {
255           Sys_Printf (MSG_PREFIX "selected_brushes = NULL\n"); 
256           return false;
257         }
258         // this should mean no selection
259         if(selected_brushes == selected_brushes->next) {
260           Sys_Printf (MSG_PREFIX "nothing selected\n"); 
261
262           return false;
263         }
264
265         for (i=0 ; i<3 ; i++)
266         {
267                 mins[i] = 99999;
268                 maxs[i] = -99999;
269         }
270
271         for (b=selected_brushes->next ; b != selected_brushes ; b=b->next)
272         {
273                 if (b->owner->eclass->fixedsize)
274                 {
275                         for (i=0 ; i<3 ; i++)
276                         {
277                                 if (b->owner->origin[i] < mins[i])
278                                         mins[i] = b->owner->origin[i];
279                                 if (b->owner->origin[i] > maxs[i])
280                                         maxs[i] = b->owner->origin[i];
281                         }
282                 }
283                 else
284                 {
285                         for (i=0 ; i<3 ; i++)
286                         {
287                                 if (b->mins[i] < mins[i])
288                                         mins[i] = b->mins[i];
289                                 if (b->maxs[i] > maxs[i])
290                                         maxs[i] = b->maxs[i];
291                         }
292                 }
293         }
294   return true;
295 }
296
297 bool CBackgroundImage::SetExtentsSel()
298 {
299         vec3_t mins,maxs;
300
301         if(!get_selection_bounds(mins,maxs)) 
302                 return false;
303
304         if(((int)mins[m_ix] == (int)maxs[m_ix]) ||
305      ((int)mins[m_iy] == (int)maxs[m_iy])) {
306                 Syn_Printf(MSG_PREFIX "tiny selection\n");
307                 return false;
308         }
309
310         m_xmin = mins[m_ix];
311         m_ymin = mins[m_iy];
312         m_xmax = maxs[m_ix];
313         m_ymax = maxs[m_iy];
314
315         g_FuncTable.m_pfnSysUpdateWindows(W_XY);
316
317   return true;
318 }
319