2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
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.
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.
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
25 // Code by reyalP aka Reed Mideke
27 // Based on various other plugins
32 CBackgroundRender render;
34 CBackgroundImage backgroundXY( XY ),backgroundXZ( XZ ),backgroundYZ( YZ );
36 CBackgroundRender::CBackgroundRender(){
40 CBackgroundRender::~CBackgroundRender(){
43 void CBackgroundRender::Register(){
44 g_QglTable.m_pfnHookGL2DWindow( this );
47 void CBackgroundRender::Draw2D( VIEWTYPE vt ){
51 backgroundXY.Render();
54 backgroundXZ.Render();
57 backgroundYZ.Render();
63 CBackgroundImage::CBackgroundImage( VIEWTYPE vt ){
67 // TODO, sensible defaults ? Or not show until we have extents ?
68 m_xmin = m_ymin = 0.0f;
69 m_xmax = m_ymax = 0.0f;
93 * should cleanup, but I don't think we can be sure it happens before our
95 CBackgroundImage::~CBackgroundImage()
100 void CBackgroundImage::Cleanup(){
102 g_QglTable.m_pfn_qglDeleteTextures( 1,&m_tex->texture_number );
108 void CBackgroundImage::Render(){
109 if ( !m_bActive || !Valid() ) {
112 g_QglTable.m_pfn_qglPushAttrib( GL_ALL_ATTRIB_BITS );
114 g_QglTable.m_pfn_qglEnable( GL_TEXTURE_2D );
115 g_QglTable.m_pfn_qglEnable( GL_BLEND );
116 g_QglTable.m_pfn_qglBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
117 g_QglTable.m_pfn_qglTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
118 g_QglTable.m_pfn_qglTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
119 g_QglTable.m_pfn_qglTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
121 g_QglTable.m_pfn_qglPolygonMode( GL_FRONT,GL_FILL );
122 // TODO, just so we can tell if we end up going the wrong way
123 // g_QglTable.m_pfn_qglPolygonMode(GL_BACK,GL_LINE);
124 // TODO any other state we should not assume ?
126 g_QglTable.m_pfn_qglBindTexture( GL_TEXTURE_2D, m_tex->texture_number );
127 g_QglTable.m_pfn_qglBegin( GL_QUADS );
129 g_QglTable.m_pfn_qglColor4f( 1.0,1.0,1.0,m_alpha );
130 g_QglTable.m_pfn_qglTexCoord2f( 0.0,1.0 );
131 g_QglTable.m_pfn_qglVertex2f( m_xmin,m_ymin );
133 g_QglTable.m_pfn_qglTexCoord2f( 1.0,1.0 );
134 g_QglTable.m_pfn_qglVertex2f( m_xmax,m_ymin );
136 g_QglTable.m_pfn_qglTexCoord2f( 1.0,0.0 );
137 g_QglTable.m_pfn_qglVertex2f( m_xmax,m_ymax );
139 g_QglTable.m_pfn_qglTexCoord2f( 0.0,0.0 );
140 g_QglTable.m_pfn_qglVertex2f( m_xmin,m_ymax );
142 g_QglTable.m_pfn_qglEnd();
143 g_QglTable.m_pfn_qglBindTexture( GL_TEXTURE_2D, 0 );
145 g_QglTable.m_pfn_qglPopAttrib();
148 bool CBackgroundImage::Load( const char *filename ){
151 unsigned char *image = NULL; // gets allocated with what ? g_malloc
152 int width = 0, height = 0;
154 g_FuncTable.m_pfnLoadImage( filename,&image,&width,&height );
157 Syn_Printf( MSG_WARN "load %s failed\n",filename );
161 // just in case we want to build for an old version
162 #ifdef BKGRND2D_JPG_WORKAROUND
163 if ( strlen( filename ) > 4 && !strcmp( ".jpg",filename + strlen( filename ) - 4 ) ) {
164 Syn_Printf( MSG_PREFIX ".jpg workaround, clearing alpha channel\n" );
165 int size = width * height * 4;
167 for ( i = 3; i < size; i += 4 ) {
173 //TODO bug for stored texture size
174 //TODO whose gl context are we in, anyway ?
175 newtex = g_FuncTable.m_pfnLoadTextureRGBA( image,width,height );
180 Syn_Printf( MSG_WARN "image to texture failed\n" );
187 g_FuncTable.m_pfnSysUpdateWindows( W_XY );
192 bool CBackgroundImage::SetExtentsMM(){
193 entity_s *worldentity;
195 int xmin = 0, ymin = 0, xmax = 0, ymax = 0;
197 worldentity = (entity_s *)g_FuncTable.m_pfnGetEntityHandle( 0 );
198 if ( !worldentity ) {
199 Syn_Printf( MSG_WARN "SetExtentsMM worldspawn not found\n" );
202 //TODO val is not NULL even if key does not exist
203 val = g_EntityTable.m_pfnValueForKey( worldentity,"mapcoordsmins" );
204 if ( !val || !val[0] ) {
205 Syn_Printf( MSG_WARN "SetExtentsMM mapcoordsmins not found\n" );
208 // we could be more robust
209 // note contortions due to splashs strange idea of min and max
210 if ( sscanf( val, "%d %d",&xmin,&ymax ) != 2 ) {
211 Syn_Printf( MSG_WARN "SetExtentsMM mapcoordsmins malformed\n" );
215 val = g_EntityTable.m_pfnValueForKey( worldentity,"mapcoordsmaxs" );
216 if ( !val || !val[0] ) {
217 Syn_Printf( MSG_WARN "SetExtentsMM mapcoordsmaxs not found\n" );
220 if ( sscanf( val, "%d %d",&xmax,&ymin ) != 2 ) {
221 Syn_Printf( MSG_WARN "SetExtentsMM mapcoordsmaxs malformed\n" );
224 //might do sanity check before we commit
225 m_xmin = (float)xmin;
226 m_ymin = (float)ymin;
227 m_xmax = (float)xmax;
228 m_ymax = (float)ymax;
230 g_FuncTable.m_pfnSysUpdateWindows( W_XY );
234 // TODO, this should just be exported from core
235 // ripped directly from radiant/select.cpp:Select_GetBounds
237 static bool get_selection_bounds( vec3_t mins, vec3_t maxs ){
240 brush_t *selected_brushes = g_DataTable.m_pfnSelectedBrushes();
241 //TODO should never happen
242 if ( !selected_brushes ) {
243 Sys_Printf( MSG_PREFIX "selected_brushes = NULL\n" );
246 // this should mean no selection
247 if ( selected_brushes == selected_brushes->next ) {
248 Sys_Printf( MSG_PREFIX "nothing selected\n" );
253 for ( i = 0 ; i < 3 ; i++ )
259 for ( b = selected_brushes->next ; b != selected_brushes ; b = b->next )
261 if ( b->owner->eclass->fixedsize ) {
262 for ( i = 0 ; i < 3 ; i++ )
264 if ( b->owner->origin[i] < mins[i] ) {
265 mins[i] = b->owner->origin[i];
267 if ( b->owner->origin[i] > maxs[i] ) {
268 maxs[i] = b->owner->origin[i];
274 for ( i = 0 ; i < 3 ; i++ )
276 if ( b->mins[i] < mins[i] ) {
277 mins[i] = b->mins[i];
279 if ( b->maxs[i] > maxs[i] ) {
280 maxs[i] = b->maxs[i];
288 bool CBackgroundImage::SetExtentsSel(){
291 if ( !get_selection_bounds( mins,maxs ) ) {
295 if ( ( (int)mins[m_ix] == (int)maxs[m_ix] ) ||
296 ( (int)mins[m_iy] == (int)maxs[m_iy] ) ) {
297 Syn_Printf( MSG_PREFIX "tiny selection\n" );
306 g_FuncTable.m_pfnSysUpdateWindows( W_XY );