- Updated UFA:Plugin (mattn2)
[xonotic/netradiant.git] / contrib / bkgrnd2d / bkgrnd2d.cpp
1 /*
2 Copyright (C) 2003 Reed Mideke.
3
4 This file is part of GtkRadiant.
5
6 GtkRadiant is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 GtkRadiant is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GtkRadiant; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19 */
20
21 //
22 // bkgrnd2d Plugin
23 //
24 // Code by reyalP aka Reed Mideke
25 //
26 // Based on various other plugins
27 //
28
29 #include "bkgrnd2d.h"
30
31 CBackgroundRender render;
32
33 CBackgroundImage backgroundXY(XY),backgroundXZ(XZ),backgroundYZ(YZ);
34
35 CBackgroundRender::CBackgroundRender()
36 {
37         refCount = 1;
38 }
39
40 CBackgroundRender::~CBackgroundRender()
41 {
42 }
43
44 void CBackgroundRender::Register()
45 {
46         g_QglTable.m_pfnHookGL2DWindow( this );
47 }
48
49 void CBackgroundRender::Draw2D( VIEWTYPE vt )
50 {
51         switch(vt)
52         {
53         case XY:
54                 backgroundXY.Render();
55                 break;
56         case XZ:
57                 backgroundXZ.Render();
58                 break;
59         case YZ:
60                 backgroundYZ.Render();
61                 break;
62         }
63 }
64
65
66 CBackgroundImage::CBackgroundImage(VIEWTYPE vt)
67 {
68         m_tex = NULL;
69         m_alpha = 0.5;
70
71         // TODO, sensible defaults ? Or not show until we have extents ?
72         m_xmin = m_ymin = 0.0f;
73         m_xmax = m_ymax = 0.0f;
74
75         m_bActive = false;
76
77         m_vt = vt;
78
79         switch(m_vt)
80         {
81                 case XY:
82                         m_ix = 0;
83                         m_iy = 1;
84                         break;
85                 case XZ:
86                         m_ix = 0;
87                         m_iy = 2;
88                         break;
89                 case YZ:
90                         m_ix = 1;
91                         m_iy = 2;
92                         break;
93         }
94 }
95
96 /*
97  * should cleanup, but I don't think we can be sure it happens before our 
98  * interfaces are gone
99 CBackgroundImage::~CBackgroundImage()
100 {
101 }
102 */
103
104 void CBackgroundImage::Cleanup()
105 {
106         if(m_tex) {
107                 g_QglTable.m_pfn_qglDeleteTextures(1,&m_tex->texture_number);
108                 g_free(m_tex);
109                 m_tex = NULL;
110         }
111 }
112
113 void CBackgroundImage::Render()
114 {
115         if (!m_bActive || !Valid())
116                 return;
117         g_QglTable.m_pfn_qglPushAttrib(GL_ALL_ATTRIB_BITS);
118
119         g_QglTable.m_pfn_qglEnable(GL_TEXTURE_2D);
120         g_QglTable.m_pfn_qglEnable(GL_BLEND);
121         g_QglTable.m_pfn_qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
122         g_QglTable.m_pfn_qglTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
123         g_QglTable.m_pfn_qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
124         g_QglTable.m_pfn_qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
125
126         g_QglTable.m_pfn_qglPolygonMode(GL_FRONT,GL_FILL);
127         // TODO, just so we can tell if we end up going the wrong way
128         // g_QglTable.m_pfn_qglPolygonMode(GL_BACK,GL_LINE);
129         // TODO any other state we should not assume ?
130
131         g_QglTable.m_pfn_qglBindTexture(GL_TEXTURE_2D, m_tex->texture_number);
132         g_QglTable.m_pfn_qglBegin(GL_QUADS);
133
134         g_QglTable.m_pfn_qglColor4f(1.0,1.0,1.0,m_alpha);
135         g_QglTable.m_pfn_qglTexCoord2f(0.0,1.0);
136         g_QglTable.m_pfn_qglVertex2f(m_xmin,m_ymin);
137
138         g_QglTable.m_pfn_qglTexCoord2f(1.0,1.0);
139         g_QglTable.m_pfn_qglVertex2f(m_xmax,m_ymin);
140
141         g_QglTable.m_pfn_qglTexCoord2f(1.0,0.0);
142         g_QglTable.m_pfn_qglVertex2f(m_xmax,m_ymax);
143
144         g_QglTable.m_pfn_qglTexCoord2f(0.0,0.0);
145         g_QglTable.m_pfn_qglVertex2f(m_xmin,m_ymax);
146
147         g_QglTable.m_pfn_qglEnd();
148         g_QglTable.m_pfn_qglBindTexture(GL_TEXTURE_2D, 0);
149
150         g_QglTable.m_pfn_qglPopAttrib();
151 }
152
153 bool CBackgroundImage::Load(const char *filename)
154 {
155         qtexture_t *newtex;
156         
157         unsigned char *image = NULL; // gets allocated with what ? g_malloc
158         int width = 0, height = 0;
159
160         g_FuncTable.m_pfnLoadImage(filename,&image,&width,&height);
161
162         if(!image) {
163                 Syn_Printf(MSG_WARN "load %s failed\n",filename);
164                 return false;
165         }
166
167 // just in case we want to build for an old version
168 // http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=900
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