Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
guitexture.cpp
Go to the documentation of this file.
1 /*
2  * Vega Strike
3  * Copyright (C) 2003 Mike Byron.
4  * Some code borrowed from David Ranger.
5  *
6  * http://vegastrike.sourceforge.net/
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */
22 
23 #include "vegastrike.h"
24 
25 #include "guitexture.h"
26 
27 #include <gnuhash.h>
28 
29 #include <string>
30 #include <png.h>
31 #include "vsfilesystem.h"
32 #include "gfx/vsimage.h"
33 #include "gldrv/gl_globals.h"
34 #include "gfx/aux_texture.h"
35 
36 using namespace VSFileSystem;
37 
38 //Read a texture from a file and bind it.
39 bool GuiTexture::read( const std::string &fileName )
40 {
41  Texture*oldTexture( m_texture );
42  m_texture = new Texture( fileName.c_str(), 0, BILINEAR );
43  if (m_texture && !m_texture->LoadSuccess() && oldTexture) {
44  delete m_texture;
45  m_texture = oldTexture;
46  } else {
47  delete oldTexture;
48  }
49  return m_texture->LoadSuccess();
50 }
51 
52 //Draw this texture, stretching to fit the rect.
53 void GuiTexture::draw( const Rect &rect ) const
54 {
55  //Don't draw unless there is something usable.
56  if ( m_texture == NULL || !m_texture->LoadSuccess() )
57  return;
58  m_texture->MakeActive();
59  GFXBegin( GFXQUAD );
60  GFXColor4f( 1, 1, 1, 1 );
61  GFXTexCoord2f( 0, 1 );
62  GFXVertexf( Vector( rect.left(), rect.top(), 0.00f ) );
63  GFXTexCoord2f( 0, 0 );
64  GFXVertexf( Vector( rect.left(), rect.bottom(), 0.00f ) );
65  GFXTexCoord2f( 1, 0 );
66  GFXVertexf( Vector( rect.right(), rect.bottom(), 0.00f ) );
67  GFXTexCoord2f( 1, 1 );
68  GFXVertexf( Vector( rect.right(), rect.top(), 0.00f ) );
69  GFXEnd();
70 }
71 
72 //CONSTRUCTION
74  m_texture( NULL )
75 {}
76 
78 {
79  if (m_texture) delete m_texture;
80 }
81