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
VSSprite Class Reference

#include <sprite.h>

Inheritance diagram for VSSprite:
Gauge VDU

Public Member Functions

 VSSprite (const char *file, enum FILTER texturefilter=BILINEAR, GFXBOOL force=GFXFALSE)
 
 VSSprite (Texture *surface, float xcenter, float ycenter, float width, float height, float s=0.f, float t=0.f, bool isAnimation=false)
 
 VSSprite (const VSSprite &source)
 
 ~VSSprite ()
 
bool LoadSuccess () const
 
void Draw ()
 
void DrawHere (Vector &ll, Vector &lr, Vector &ur, Vector &ul)
 
void Rotate (const float &rad)
 
void SetRotation (const float &rot)
 
void GetRotation (float &rot)
 
void ReadTexture (VSFileSystem::VSFile *f)
 
void GetST (float &s, float &t)
 
void SetST (const float s, const float t)
 
void SetTime (double newtime)
 
void SetPosition (const float &x1, const float &y1)
 
void GetPosition (float &x1, float &y1)
 
void SetSize (float s1, float s2)
 
void GetSize (float &x1, float &y1)
 
void SetTimeSource (SharedPtr< Audio::Source > source)
 
void ClearTimeSource ()
 
bool Done () const
 
void Reset ()
 
SharedPtr< Audio::SourceGetTimeSource () const
 
TexturegetTexture ()
 
const TexturegetTexture () const
 

Detailed Description

Definition at line 38 of file sprite.h.

Constructor & Destructor Documentation

VSSprite::VSSprite ( const char *  file,
enum FILTER  texturefilter = BILINEAR,
GFXBOOL  force = GFXFALSE 
)

Definition at line 89 of file sprite.cpp.

References cacheInsert(), cacheLookup(), Texture::Clone(), VSFileSystem::VSFile::Close(), f, VSFileSystem::VSFile::Fscanf(), g_game, GFXFALSE, GFXTRUE, Texture::LoadSuccess(), VSFileSystem::Ok, VSFileSystem::VSFile::OpenReadOnly(), BaseUtil::Texture(), TEXTURE2D, TEXTURE_2D, VSFileSystem::Unspecified, game_data_t::use_sprites, VSCONSTRUCT2, and VSFileSystem::VSSpriteFile.

90 {
91  VSCONSTRUCT2( 'S' )
92  xcenter = ycenter = 0;
93  widtho2 = heighto2 = 0;
94  rotation = 0;
95  surface = NULL;
96  maxs = maxt = 0;
97  isAnimation = false;
98 
99  //Check cache
100  {
101  std::pair< bool, VSSprite* >lkup = cacheLookup( file );
102  if (lkup.first) {
103  if (lkup.second) {
104  *this = *lkup.second;
105  if (surface != NULL)
106  surface = surface->Clone();
107  } else {
108  return;
109  }
110  }
111  }
112 
113  VSFile f;
114  VSError err = Unspecified;
115  if (file[0] != '\0')
116  err = f.OpenReadOnly( file, VSSpriteFile );
117  if (err <= Ok) {
118  char texture[127] = {0};
119  char texturea[127] = {0};
120  f.Fscanf( "%126s %126s", texture, texturea );
121  f.Fscanf( "%f %f", &widtho2, &heighto2 );
122  f.Fscanf( "%f %f", &xcenter, &ycenter );
123  texture[sizeof (texture)-sizeof (*texture)-1] = 0;
124  texturea[sizeof (texturea)-sizeof (*texturea)-1] = 0;
125 
126  widtho2 /= 2;
127  heighto2 /= -2;
128  surface = NULL;
129  if (g_game.use_sprites || force == GFXTRUE) {
130  int len = strlen( texture );
131  if (len > 4 && texture[len-1] == 'i' && texture[len-2] == 'n' && texture[len-3] == 'a' && texture[len-4] == '.') {
132  surface = new AnimatedTexture( f, 0, texturefilter, GFXFALSE );
133  isAnimation = true;
134  } else if (texturea[0] == '0') {
135  surface = new Texture( texture, 0, texturefilter, TEXTURE2D, TEXTURE_2D, GFXTRUE, 65536, GFXFALSE );
136  isAnimation = false;
137  } else {
138  surface = new Texture( texture,
139  texturea,
140  0,
141  texturefilter,
142  TEXTURE2D,
143  TEXTURE_2D,
144  1,
145  0,
146  GFXTRUE,
147  65536,
148  GFXFALSE );
149  isAnimation = false;
150  }
151  if ( !surface->LoadSuccess() ) {
152  delete surface;
153  surface = NULL;
154  VSSprite *newspr = new VSSprite();
155  *newspr = *this;
156  newspr->surface = NULL;
157  cacheInsert( file, newspr );
158  } else {
159  //Update cache
160  VSSprite *newspr = new VSSprite();
161  *newspr = *this;
162  newspr->surface = this->surface->Clone();
163  cacheInsert( file, newspr );
164  }
165  }
166  //Finally close file
167  f.Close();
168  } else {
169  cacheInsert( file, 0 ); //Mark bad file
170  widtho2 = heighto2 = 0;
171  xcenter = ycenter = 0;
172  }
173 }
VSSprite::VSSprite ( Texture surface,
float  xcenter,
float  ycenter,
float  width,
float  height,
float  s = 0.f,
float  t = 0.f,
bool  isAnimation = false 
)

Definition at line 69 of file sprite.cpp.

69  :
70  xcenter( _xcenter )
71  , ycenter( _ycenter )
72  , widtho2( _width/2 )
73  , heighto2( _height/2 )
74  , maxs( _s )
75  , maxt( _t )
76  , rotation( 0 )
77  , isAnimation(_isAnimation)
78 {
79  surface = _surface;
80 }
VSSprite::VSSprite ( const VSSprite source)

Definition at line 82 of file sprite.cpp.

References Texture::Clone().

83 {
84  *this = source;
85  if (surface != NULL)
86  surface = surface->Clone();
87 }
VSSprite::~VSSprite ( )

Definition at line 186 of file sprite.cpp.

References VSDESTRUCT2.

187 {
189  if (surface != NULL)
190  delete surface;
191 }

Member Function Documentation

void VSSprite::ClearTimeSource ( )

Definition at line 340 of file sprite.cpp.

341 {
342  if (isAnimation)
343  ( (AnimatedTexture*) surface )->ClearTimeSource();
344 }
bool VSSprite::Done ( ) const

Definition at line 346 of file sprite.cpp.

347 {
348  if (isAnimation)
349  return ( (AnimatedTexture*) surface )->Done();
350  else
351  return false;
352 }
void VSSprite::Draw ( )

Definition at line 232 of file sprite.cpp.

References CULLFACE, DrawHere(), GFXBegin(), GFXDisable(), GFXEnable(), GFXEnd(), GFXGetBlendMode(), GFXMODULATETEXTURE, GFXQUAD, GFXTexCoord2f(), GFXTexCoord4f(), GFXTextureCoordGenMode(), GFXTextureEnv(), GFXToggleTexture(), GFXVertexf(), gl_options, Texture::MakeActive(), Texture::maxtcoord, Texture::mintcoord, gl_options_t::Multitexture, NO_GEN, Texture::numLayers(), Texture::numPasses(), Texture::SetupPass(), Texture::texture_target, and Vector.

Referenced by Gauge::Draw(), Radar::SphereDisplay::Draw(), Radar::BubbleDisplay::Draw(), Radar::PlaneDisplay::Draw(), VDU::Draw(), WindowManager::draw(), GameCockpit::Draw(), BaseInterface::Room::Draw(), UnivMap::Draw(), and DrawGlutMouse().

233 {
234  if (surface) {
235  //don't do anything if no surface
236  size_t lyr;
237  size_t numlayers = surface->numLayers();
238  bool multitex = (numlayers > 1);
239  int numpasses = surface->numPasses();
240  GFXDisable( CULLFACE );
241  Vector ll, lr, ur, ul;
242  DrawHere( ll, lr, ur, ul );
243  BLENDFUNC src, dst;
244  GFXGetBlendMode( src, dst );
245  for (lyr = 0; (lyr < gl_options.Multitexture) || (lyr < numlayers); lyr++) {
246  GFXToggleTexture( (lyr < numlayers), lyr, surface->texture_target );
247  if (lyr < numlayers) GFXTextureCoordGenMode( lyr, NO_GEN, NULL, NULL );
248  }
249  for (int pass = 0; pass < numpasses; pass++) {
250  if ( surface->SetupPass( pass, 0, src, dst ) ) {
251  surface->MakeActive( 0, pass );
252 
253  // Keep below MakeActive, AnimatedTexture only sets
254  // the final effective coordinates there.
255  float ms = surface->mintcoord.i, Ms = surface->maxtcoord.i;
256  float mt = surface->mintcoord.j, Mt = surface->maxtcoord.j;
257  ms = (Ms-ms)*maxs+ms;
258  mt = (Mt-mt)*maxt+mt;
259 
261  GFXBegin( GFXQUAD );
262  if (!multitex)
263  GFXTexCoord2f( ms, Mt );
264  else
265  GFXTexCoord4f( ms, Mt, ms, Mt );
266  GFXVertexf( ll );
267  if (!multitex)
268  GFXTexCoord2f( Ms, Mt );
269  else
270  GFXTexCoord4f( Ms, Mt, Ms, Mt );
271  GFXVertexf( lr );
272  if (!multitex)
273  GFXTexCoord2f( Ms, mt );
274  else
275  GFXTexCoord4f( Ms, mt, Ms, mt );
276  GFXVertexf( ur );
277  if (!multitex)
278  GFXTexCoord2f( ms, mt );
279  else
280  GFXTexCoord4f( ms, mt, ms, mt );
281  GFXVertexf( ul );
282  GFXEnd();
283  }
284  }
285  surface->SetupPass( -1, 0, src, dst );
286  for (lyr = 0; lyr < numlayers; lyr++)
287  GFXToggleTexture( false, lyr, surface->texture_target );
288  GFXEnable( CULLFACE );
289  }
290 }
void VSSprite::DrawHere ( Vector ll,
Vector lr,
Vector ur,
Vector ul 
)

Draw at specified coordinates given by 4 endpoints.

Parameters
lllower-left corner
lrlower-right corner
urupper-right corner
ulupper-left corner
Note
Disregards sprite position but not maxs/maxt coordinates.

Definition at line 211 of file sprite.cpp.

References UniverseUtil::cos(), f, M_PI_2, UniverseUtil::sin(), and Vector.

Referenced by Draw(), Animation::DrawAsVSSprite(), and DrawHUDSprite().

212 {
213  if (rotation) {
214  const float cw = widtho2*cos( rotation );
215  const float sw = widtho2*sin( rotation );
216  const float ch = heighto2*cos( M_PI_2+rotation );
217  const float sh = heighto2*sin( M_PI_2+rotation );
218  const float wnew = cw+ch;
219  const float hnew = sw+sh;
220  ll = Vector( xcenter-wnew, ycenter+hnew, 0.00f );
221  lr = Vector( xcenter+wnew, ycenter+hnew, 0.00f );
222  ur = Vector( xcenter+wnew, ycenter-hnew, 0.00f );
223  ul = Vector( xcenter-wnew, ycenter-hnew, 0.00f );
224  } else {
225  ll = Vector( xcenter-widtho2, ycenter+heighto2, 0.00f );
226  lr = Vector( xcenter+widtho2, ycenter+heighto2, 0.00f );
227  ur = Vector( xcenter+widtho2, ycenter-heighto2, 0.00f );
228  ul = Vector( xcenter-widtho2, ycenter-heighto2, 0.00f );
229  }
230 }
void VSSprite::GetPosition ( float x1,
float y1 
)

Definition at line 298 of file sprite.cpp.

Referenced by Gauge::Draw(), VDU::Draw(), WindowManager::draw(), DrawGlutMouse(), DrawHUDSprite(), DrawShield(), and Radar::ViewArea::SetSprite().

299 {
300  x1 = xcenter;
301  y1 = ycenter;
302 }
void VSSprite::GetRotation ( float rot)

Definition at line 321 of file sprite.cpp.

322 {
323  rot = rotation;
324 }
void VSSprite::GetSize ( float x1,
float y1 
)

Definition at line 310 of file sprite.cpp.

Referenced by Gauge::Draw(), VDU::Draw(), WindowManager::draw(), BaseInterface::Room::Draw(), DrawGlutMouse(), DrawHUDSprite(), DrawShield(), Gauge::Gauge(), Gauge::GetSize(), and Radar::ViewArea::SetSprite().

311 {
312  x1 = widtho2*2;
313  y1 = heighto2*2;
314 }
void VSSprite::GetST ( float s,
float t 
)

Definition at line 199 of file sprite.cpp.

200 {
201  s = maxs;
202  t = maxt;
203 }
Texture* VSSprite::getTexture ( )
inline

Definition at line 94 of file sprite.h.

Referenced by buildShipDescription(), and DrawHUDSprite().

95  {
96  return surface;
97  }
const Texture* VSSprite::getTexture ( ) const
inline

Definition at line 98 of file sprite.h.

99  {
100  return surface;
101  }
SharedPtr< Audio::Source > VSSprite::GetTimeSource ( ) const

Definition at line 332 of file sprite.cpp.

333 {
334  if (isAnimation)
335  return ( (AnimatedTexture*) surface )->GetTimeSource();
336  else
337  return SharedPtr<Audio::Source>();
338 }
bool VSSprite::LoadSuccess ( ) const

Definition at line 360 of file sprite.cpp.

References Texture::LoadSuccess().

Referenced by BaseInterface::Room::BaseVSMovie::BaseVSMovie(), GameCockpit::Draw(), isVSSpriteLoaded(), and BaseUtil::VideoStream().

361 {
362  return surface != NULL && surface->LoadSuccess();
363 }
void VSSprite::ReadTexture ( VSFileSystem::VSFile f)

Definition at line 175 of file sprite.cpp.

References BaseUtil::Texture(), and VSFileSystem::VSFile::Valid().

176 {
177  if ( !f->Valid() ) {
178  widtho2 = heighto2 = 0;
179  xcenter = ycenter = 0;
180  std::cerr<<"VSSprite::ReadTexture error : VSFile not valid"<<std::endl;
181  return;
182  }
183  surface = new Texture( f );
184 }
void VSSprite::Reset ( )

Definition at line 354 of file sprite.cpp.

Referenced by BaseInterface::Room::BaseVSMovie::BaseVSMovie(), and BaseUtil::Video().

355 {
356  if (isAnimation)
357  ( (AnimatedTexture*) surface )->Reset();
358 }
void VSSprite::Rotate ( const float rad)
inline

Definition at line 72 of file sprite.h.

73  {
74  rotation += rad;
75  }
void VSSprite::SetPosition ( const float x1,
const float y1 
)

Definition at line 292 of file sprite.cpp.

Referenced by Gauge::Draw(), WindowManager::draw(), GameCockpit::Draw(), BaseInterface::Room::Draw(), DrawGlutMouse(), DrawHUDSprite(), and BaseInterface::Room::BaseVSSprite::SetPos().

293 {
294  xcenter = x1;
295  ycenter = y1;
296 }
void VSSprite::SetRotation ( const float rot)

Definition at line 316 of file sprite.cpp.

Referenced by BaseInterface::Room::BaseVSMovie::SetMovie().

317 {
318  rotation = rot;
319 }
void VSSprite::SetSize ( float  s1,
float  s2 
)

Definition at line 304 of file sprite.cpp.

Referenced by Gauge::Draw(), DrawHUDSprite(), Gauge::SetSize(), and BaseInterface::Room::BaseVSSprite::SetSize().

305 {
306  widtho2 = x1/2;
307  heighto2 = y1/2;
308 }
void VSSprite::SetST ( const float  s,
const float  t 
)

Definition at line 193 of file sprite.cpp.

Referenced by Gauge::Draw().

194 {
195  maxs = s;
196  maxt = t;
197 }
void VSSprite::SetTime ( double  newtime)

Definition at line 205 of file sprite.cpp.

References Texture::setTime().

Referenced by Gauge::Draw(), and BaseInterface::Room::BaseVSSprite::SetTime().

206 {
207  if (surface)
208  surface->setTime( newtime );
209 }
void VSSprite::SetTimeSource ( SharedPtr< Audio::Source source)

Definition at line 326 of file sprite.cpp.

Referenced by BaseInterface::Room::BaseVSMovie::BaseVSMovie(), and BaseUtil::Video().

327 {
328  if (isAnimation)
329  ( (AnimatedTexture*) surface )->SetTimeSource( source );
330 }

The documentation for this class was generated from the following files: