]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/bkgrnd2d/bkgrnd2d.cpp
Merge branch 'master' into divVerent/farplanedist-sky-fix
[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         refCount = 1;
37 }
38
39 CBackgroundRender::~CBackgroundRender(){
40 }
41
42 void CBackgroundRender::Register(){
43         g_QglTable.m_pfnHookGL2DWindow( this );
44 }
45
46 void CBackgroundRender::Draw2D( VIEWTYPE vt ){
47         switch ( vt )
48         {
49         case XY:
50                 backgroundXY.Render();
51                 break;
52         case XZ:
53                 backgroundXZ.Render();
54                 break;
55         case YZ:
56                 backgroundYZ.Render();
57                 break;
58         }
59 }
60
61
62 CBackgroundImage::CBackgroundImage( VIEWTYPE vt ){
63         m_tex = NULL;
64         m_alpha = 0.5;
65
66         // TODO, sensible defaults ? Or not show until we have extents ?
67         m_xmin = m_ymin = 0.0f;
68         m_xmax = m_ymax = 0.0f;
69
70         m_bActive = false;
71
72         m_vt = vt;
73
74         switch ( m_vt )
75         {
76         case XY:
77                 m_ix = 0;
78                 m_iy = 1;
79                 break;
80         case XZ:
81                 m_ix = 0;
82                 m_iy = 2;
83                 break;
84         case YZ:
85                 m_ix = 1;
86                 m_iy = 2;
87                 break;
88         }
89 }
90
91 /*
92  * should cleanup, but I don't think we can be sure it happens before our
93  * interfaces are gone
94    CBackgroundImage::~CBackgroundImage()
95    {
96    }
97  */
98
99 void CBackgroundImage::Cleanup(){
100         if ( m_tex ) {
101                 g_QglTable.m_pfn_qglDeleteTextures( 1,&m_tex->texture_number );
102                 g_free( m_tex );
103                 m_tex = NULL;
104         }
105 }
106
107 void CBackgroundImage::Render(){
108         if ( !m_bActive || !Valid() ) {
109                 return;
110         }
111         g_QglTable.m_pfn_qglPushAttrib( GL_ALL_ATTRIB_BITS );
112
113         g_QglTable.m_pfn_qglEnable( GL_TEXTURE_2D );
114         g_QglTable.m_pfn_qglEnable( GL_BLEND );
115         g_QglTable.m_pfn_qglBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
116         g_QglTable.m_pfn_qglTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
117         g_QglTable.m_pfn_qglTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
118         g_QglTable.m_pfn_qglTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
119
120         g_QglTable.m_pfn_qglPolygonMode( GL_FRONT,GL_FILL );
121         // TODO, just so we can tell if we end up going the wrong way
122         // g_QglTable.m_pfn_qglPolygonMode(GL_BACK,GL_LINE);
123         // TODO any other state we should not assume ?
124
125         g_QglTable.m_pfn_qglBindTexture( GL_TEXTURE_2D, m_tex->texture_number );
126         g_QglTable.m_pfn_qglBegin( GL_QUADS );
127
128         g_QglTable.m_pfn_qglColor4f( 1.0,1.0,1.0,m_alpha );
129         g_QglTable.m_pfn_qglTexCoord2f( 0.0,1.0 );
130         g_QglTable.m_pfn_qglVertex2f( m_xmin,m_ymin );
131
132         g_QglTable.m_pfn_qglTexCoord2f( 1.0,1.0 );
133         g_QglTable.m_pfn_qglVertex2f( m_xmax,m_ymin );
134
135         g_QglTable.m_pfn_qglTexCoord2f( 1.0,0.0 );
136         g_QglTable.m_pfn_qglVertex2f( m_xmax,m_ymax );
137
138         g_QglTable.m_pfn_qglTexCoord2f( 0.0,0.0 );
139         g_QglTable.m_pfn_qglVertex2f( m_xmin,m_ymax );
140
141         g_QglTable.m_pfn_qglEnd();
142         g_QglTable.m_pfn_qglBindTexture( GL_TEXTURE_2D, 0 );
143
144         g_QglTable.m_pfn_qglPopAttrib();
145 }
146
147 bool CBackgroundImage::Load( const char *filename ){
148         qtexture_t *newtex;
149
150         unsigned char *image = NULL; // gets allocated with what ? g_malloc
151         int width = 0, height = 0;
152
153         g_FuncTable.m_pfnLoadImage( filename,&image,&width,&height );
154
155         if ( !image ) {
156                 Syn_Printf( MSG_WARN "load %s failed\n",filename );
157                 return false;
158         }
159
160 // just in case we want to build for an old version
161 // http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=900
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;
166                 int i;
167                 for ( i = 3; i < size; i += 4 ) {
168                         image[i] = 255;
169                 }
170         }
171 #endif
172
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 );
176
177         g_free( image );
178
179         if ( !newtex ) {
180                 Syn_Printf( MSG_WARN "image to texture failed\n" );
181                 return false;
182         }
183
184         Cleanup();
185         m_tex = newtex;
186
187         g_FuncTable.m_pfnSysUpdateWindows( W_XY );
188
189         return true;
190 }
191
192 bool CBackgroundImage::SetExtentsMM(){
193         entity_s *worldentity;
194         const char *val;
195         int xmin = 0, ymin = 0, xmax = 0, ymax = 0;
196
197         worldentity = (entity_s *)g_FuncTable.m_pfnGetEntityHandle( 0 );
198         if ( !worldentity ) {
199                 Syn_Printf( MSG_WARN "SetExtentsMM worldspawn not found\n" );
200                 return false;
201         }
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" );
206                 return false;
207         }
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" );
212                 return false;
213         }
214
215         val = g_EntityTable.m_pfnValueForKey( worldentity,"mapcoordsmaxs" );
216         if ( !val || !val[0] ) {
217                 Syn_Printf( MSG_WARN "SetExtentsMM mapcoordsmaxs not found\n" );
218                 return false;
219         }
220         if ( sscanf( val, "%d %d",&xmax,&ymin ) != 2 ) {
221                 Syn_Printf( MSG_WARN "SetExtentsMM mapcoordsmaxs malformed\n" );
222                 return false;
223         }
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;
229
230         g_FuncTable.m_pfnSysUpdateWindows( W_XY );
231         return true;
232 }
233
234 // TODO, this should just be exported from core
235 // ripped directly from radiant/select.cpp:Select_GetBounds
236 //
237 static bool get_selection_bounds( vec3_t mins, vec3_t maxs ){
238         brush_t *b;
239         int i;
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" );
244                 return false;
245         }
246         // this should mean no selection
247         if ( selected_brushes == selected_brushes->next ) {
248                 Sys_Printf( MSG_PREFIX "nothing selected\n" );
249
250                 return false;
251         }
252
253         for ( i = 0 ; i < 3 ; i++ )
254         {
255                 mins[i] = 99999;
256                 maxs[i] = -99999;
257         }
258
259         for ( b = selected_brushes->next ; b != selected_brushes ; b = b->next )
260         {
261                 if ( b->owner->eclass->fixedsize ) {
262                         for ( i = 0 ; i < 3 ; i++ )
263                         {
264                                 if ( b->owner->origin[i] < mins[i] ) {
265                                         mins[i] = b->owner->origin[i];
266                                 }
267                                 if ( b->owner->origin[i] > maxs[i] ) {
268                                         maxs[i] = b->owner->origin[i];
269                                 }
270                         }
271                 }
272                 else
273                 {
274                         for ( i = 0 ; i < 3 ; i++ )
275                         {
276                                 if ( b->mins[i] < mins[i] ) {
277                                         mins[i] = b->mins[i];
278                                 }
279                                 if ( b->maxs[i] > maxs[i] ) {
280                                         maxs[i] = b->maxs[i];
281                                 }
282                         }
283                 }
284         }
285         return true;
286 }
287
288 bool CBackgroundImage::SetExtentsSel(){
289         vec3_t mins,maxs;
290
291         if ( !get_selection_bounds( mins,maxs ) ) {
292                 return false;
293         }
294
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" );
298                 return false;
299         }
300
301         m_xmin = mins[m_ix];
302         m_ymin = mins[m_iy];
303         m_xmax = maxs[m_ix];
304         m_ymax = maxs[m_iy];
305
306         g_FuncTable.m_pfnSysUpdateWindows( W_XY );
307
308         return true;
309 }