2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
\r
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
\r
5 This file is part of GtkRadiant.
\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
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
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
25 // Code by reyalP aka Reed Mideke
\r
27 // Based on various other plugins
\r
30 #include "bkgrnd2d.h"
\r
32 CBackgroundRender render;
\r
34 CBackgroundImage backgroundXY(XY),backgroundXZ(XZ),backgroundYZ(YZ);
\r
36 CBackgroundRender::CBackgroundRender()
\r
41 CBackgroundRender::~CBackgroundRender()
\r
45 void CBackgroundRender::Register()
\r
47 g_QglTable.m_pfnHookGL2DWindow( this );
\r
50 void CBackgroundRender::Draw2D( VIEWTYPE vt )
\r
55 backgroundXY.Render();
\r
58 backgroundXZ.Render();
\r
61 backgroundYZ.Render();
\r
67 CBackgroundImage::CBackgroundImage(VIEWTYPE vt)
\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
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
105 void CBackgroundImage::Cleanup()
\r
108 g_QglTable.m_pfn_qglDeleteTextures(1,&m_tex->texture_number);
\r
114 void CBackgroundImage::Render()
\r
116 if (!m_bActive || !Valid())
\r
118 g_QglTable.m_pfn_qglPushAttrib(GL_ALL_ATTRIB_BITS);
\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
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
132 g_QglTable.m_pfn_qglBindTexture(GL_TEXTURE_2D, m_tex->texture_number);
\r
133 g_QglTable.m_pfn_qglBegin(GL_QUADS);
\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
139 g_QglTable.m_pfn_qglTexCoord2f(1.0,1.0);
\r
140 g_QglTable.m_pfn_qglVertex2f(m_xmax,m_ymin);
\r
142 g_QglTable.m_pfn_qglTexCoord2f(1.0,0.0);
\r
143 g_QglTable.m_pfn_qglVertex2f(m_xmax,m_ymax);
\r
145 g_QglTable.m_pfn_qglTexCoord2f(0.0,0.0);
\r
146 g_QglTable.m_pfn_qglVertex2f(m_xmin,m_ymax);
\r
148 g_QglTable.m_pfn_qglEnd();
\r
149 g_QglTable.m_pfn_qglBindTexture(GL_TEXTURE_2D, 0);
\r
151 g_QglTable.m_pfn_qglPopAttrib();
\r
154 bool CBackgroundImage::Load(const char *filename)
\r
156 qtexture_t *newtex;
\r
158 unsigned char *image = NULL; // gets allocated with what ? g_malloc
\r
159 int width = 0, height = 0;
\r
161 g_FuncTable.m_pfnLoadImage(filename,&image,&width,&height);
\r
164 Syn_Printf(MSG_WARN "load %s failed\n",filename);
\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
175 for (i = 3; i < size; i+=4) {
\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
188 Syn_Printf(MSG_WARN "image to texture failed\n");
\r
195 g_FuncTable.m_pfnSysUpdateWindows(W_XY);
\r
200 bool CBackgroundImage::SetExtentsMM()
\r
202 entity_s *worldentity;
\r
204 int xmin = 0, ymin = 0, xmax = 0, ymax = 0;
\r
206 worldentity = (entity_s *)g_FuncTable.m_pfnGetEntityHandle(0);
\r
208 Syn_Printf(MSG_WARN "SetExtentsMM worldspawn not found\n");
\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
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
221 Syn_Printf(MSG_WARN "SetExtentsMM mapcoordsmins malformed\n");
\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
230 if(sscanf(val, "%d %d",&xmax,&ymin) != 2)
\r
232 Syn_Printf(MSG_WARN "SetExtentsMM mapcoordsmaxs malformed\n");
\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
241 g_FuncTable.m_pfnSysUpdateWindows(W_XY);
\r
245 // TODO, this should just be exported from core
\r
246 // ripped directly from radiant/select.cpp:Select_GetBounds
\r
248 static bool get_selection_bounds (vec3_t mins, vec3_t maxs)
\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
258 // this should mean no selection
\r
259 if(selected_brushes == selected_brushes->next) {
\r
260 Sys_Printf (MSG_PREFIX "nothing selected\n");
\r
265 for (i=0 ; i<3 ; i++)
\r
271 for (b=selected_brushes->next ; b != selected_brushes ; b=b->next)
\r
273 if (b->owner->eclass->fixedsize)
\r
275 for (i=0 ; i<3 ; i++)
\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
285 for (i=0 ; i<3 ; i++)
\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
297 bool CBackgroundImage::SetExtentsSel()
\r
301 if(!get_selection_bounds(mins,maxs))
\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
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
315 g_FuncTable.m_pfnSysUpdateWindows(W_XY);
\r