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