00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "OSDLSprite.h"
00028
00029 #include "OSDLBoundingBox2D.h"
00030 #include "OSDLGLTexture.h"
00031
00032
00033 using namespace Ceylan::Log ;
00034
00035 using namespace OSDL::Rendering ;
00036 using namespace OSDL::Video ;
00037
00038 using std::string ;
00039
00040
00041
00042 #ifdef OSDL_USES_CONFIG_H
00043 #include <OSDLConfig.h>
00044 #endif // OSDL_USES_CONFIG_H
00045
00046 #if OSDL_ARCH_NINTENDO_DS
00047 #include "OSDLConfigForNintendoDS.h"
00048 #endif // OSDL_ARCH_NINTENDO_DS
00049
00050
00051 #if OSDL_USES_SDL
00052 #include "SDL.h"
00053 #endif // OSDL_USES_SDL
00054
00055
00056
00057 #if OSDL_DEBUG_SPRITE
00058
00059 #define OSDL_RENDER_LOG(message) LogPlug::debug( message ) ;
00060
00061 #else // OSDL_DEBUG_SPRITE
00062
00063 #define OSDL_RENDER_LOG(message)
00064
00065 #endif // OSDL_DEBUG_SPRITE
00066
00067
00068
00069
00070 const Sprite::Shape Sprite::EightTimesEight = 1 ;
00071 const Sprite::Shape Sprite::SixteenTimesEight = 2 ;
00072 const Sprite::Shape Sprite::ThirtyTwoTimesEight = 3 ;
00073
00074 const Sprite::Shape Sprite::EightTimesSixteen = 4 ;
00075 const Sprite::Shape Sprite::SixteenTimesSixteen = 5 ;
00076 const Sprite::Shape Sprite::ThirtyTwoTimesSixteen = 6 ;
00077
00078 const Sprite::Shape Sprite::EightTimesThirtyTwo = 7 ;
00079 const Sprite::Shape Sprite::SixteenTimesThirtyTwo = 8 ;
00080 const Sprite::Shape Sprite::ThirtyTwoTimesThirtyTwo = 9 ;
00081 const Sprite::Shape Sprite::SixtyFourTimesThirtyTwo = 10 ;
00082
00083 const Sprite::Shape Sprite::ThirtyTwoTimesSixtyFour = 11 ;
00084 const Sprite::Shape Sprite::SixtyFourTimesSixtyFour = 12 ;
00085
00086 const Sprite::Shape Sprite::PowersOfTwo = 13 ;
00087
00088
00089
00090
00091 SpriteException::SpriteException( const string & message ) :
00092 VideoRenderingException( "Sprite exception: " + message )
00093 {
00094
00095 }
00096
00097
00098
00099 SpriteException::~SpriteException() throw()
00100 {
00101
00102 }
00103
00104
00105
00106
00107 Sprite::Sprite( const string & frameFilename )
00108 {
00109
00110
00111 }
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133 Sprite::~Sprite() throw()
00134 {
00135
00136 if ( _ownBoundingBox && ( _box != 0 ) )
00137 delete _box ;
00138
00139 #if OSDL_USES_OPENGL
00140
00141 if ( _texture != 0 )
00142 delete _texture ;
00143
00144 #endif // OSDL_USES_OPENGL
00145
00146 }
00147
00148
00149
00150 const string Sprite::toString( Ceylan::VerbosityLevels level ) const
00151 {
00152
00153 string res = "Sprite which " ;
00154
00155 if ( _ownBoundingBox )
00156 res += "owns" ;
00157 else
00158 res += " does not own" ;
00159
00160 res += " its bounding box. " + View::toString( level ) ;
00161
00162 res += ". Its shape is: " + DescribeShape( _shape ) ;
00163
00164 #if OSDL_USES_OPENGL
00165
00166 if ( _texture == 0 )
00167 res += ". No texture is associated with this sprite" ;
00168 else
00169 res += ". A texture is associated with this sprite; "
00170 + _texture->toString() ;
00171
00172 #endif // OSDL_USES_OPENGL
00173
00174 return res ;
00175
00176 }
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186 string Sprite::DescribeShape( Shape shape )
00187 {
00188
00189
00190 switch( shape )
00191 {
00192
00193 case EightTimesEight:
00194 return "8x8" ;
00195 break ;
00196
00197 case SixteenTimesEight:
00198 return "16x8" ;
00199 break ;
00200
00201 case ThirtyTwoTimesEight:
00202 return "32x8" ;
00203 break ;
00204
00205
00206 case EightTimesSixteen:
00207 return "8x16" ;
00208 break ;
00209
00210 case SixteenTimesSixteen:
00211 return "16x16" ;
00212 break ;
00213
00214 case ThirtyTwoTimesSixteen:
00215 return "32x16" ;
00216 break ;
00217
00218
00219 case EightTimesThirtyTwo:
00220 return "8x32" ;
00221 break ;
00222
00223 case SixteenTimesThirtyTwo:
00224 return "16x32" ;
00225 break ;
00226
00227 case ThirtyTwoTimesThirtyTwo:
00228 return "32x32" ;
00229 break ;
00230
00231 case SixtyFourTimesThirtyTwo:
00232 return "64x32" ;
00233 break ;
00234
00235
00236 case ThirtyTwoTimesSixtyFour:
00237 return "32x64" ;
00238 break ;
00239
00240 case SixtyFourTimesSixtyFour:
00241 return "64x64" ;
00242 break ;
00243
00244 case PowersOfTwo:
00245 return "both dimensions are powers of two" ;
00246 break ;
00247
00248 default:
00249 throw SpriteException( "Sprite::DescribeShape failed: "
00250 "unknown shape (" + Ceylan::toNumericalString( shape ) + ")" ) ;
00251 break ;
00252
00253
00254 }
00255
00256 }
00257
00258
00259
00260 Sprite::Shape Sprite::GetSmallestEnclosingShape( Length width, Length height )
00261 {
00262
00263 #if OSDL_ARCH_NINTENDO_DS
00264
00265 #ifdef OSDL_RUNS_ON_ARM7
00266
00267 throw SpriteException( "Sprite::GetSmallestEnclosingShape failed: "
00268 "not supported on the ARM7." ) ;
00269
00270 #elif defined(OSDL_RUNS_ON_ARM9)
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282 if ( width > 64 || height > 64 )
00283 throw SpriteException( "Sprite::GetSmallestEnclosingShape failed: "
00284 "size " + Ceylan::toString(width) + "x" + Ceylan::toString(height)
00285 + " too large for standard shapes" ) ;
00286
00287
00288 if ( width > 32 )
00289 if ( height > 32 )
00290 return SixtyFourTimesSixtyFour ;
00291 else
00292 return SixtyFourTimesThirtyTwo ;
00293
00294
00295
00296 if ( height > 32 )
00297 return ThirtyTwoTimesSixtyFour ;
00298
00299
00300
00301
00302 if ( width > 16 )
00303 if ( height > 16 )
00304 return ThirtyTwoTimesThirtyTwo ;
00305 else if ( height > 8 )
00306 return ThirtyTwoTimesSixteen ;
00307 else
00308 return ThirtyTwoTimesEight ;
00309
00310
00311
00312 if ( width > 8 )
00313 if ( height > 16 )
00314 return SixteenTimesThirtyTwo ;
00315 else if ( height > 8 )
00316 return SixteenTimesSixteen ;
00317 else
00318 return SixteenTimesEight ;
00319
00320
00321
00322 if ( height > 16 )
00323 return EightTimesThirtyTwo ;
00324 else if ( height > 8 )
00325 return EightTimesSixteen ;
00326 else
00327 return EightTimesEight ;
00328
00329 #endif // defined(OSDL_RUNS_ON_ARM9)
00330
00331 #else // OSDL_ARCH_NINTENDO_DS
00332
00333 LogPlug::error( "Sprite::GetSmallestEnclosingShape failed: "
00334 "not to be used if not on the Nintendo DS." ) ;
00335
00336
00337 return 1 ;
00338
00339 #endif // OSDL_ARCH_NINTENDO_DS
00340
00341 }
00342
00343
00344
00345 Length Sprite::GetShapeWidthFor( Shape shape )
00346 {
00347
00348 switch( shape )
00349 {
00350
00351 case EightTimesEight:
00352 case EightTimesSixteen:
00353 case EightTimesThirtyTwo:
00354 return 8 ;
00355 break ;
00356
00357 case SixteenTimesEight:
00358 case SixteenTimesSixteen:
00359 case SixteenTimesThirtyTwo:
00360 return 16 ;
00361 break ;
00362
00363 case ThirtyTwoTimesEight:
00364 case ThirtyTwoTimesSixteen:
00365 case ThirtyTwoTimesThirtyTwo:
00366 case ThirtyTwoTimesSixtyFour:
00367 return 32 ;
00368 break ;
00369
00370 case SixtyFourTimesThirtyTwo:
00371 case SixtyFourTimesSixtyFour:
00372 return 64 ;
00373 break ;
00374
00375 default:
00376 throw SpriteException( "Sprite::GetShapeWidthFor failed: "
00377 "unknown shape (" + Ceylan::toNumericalString( shape ) + ")" ) ;
00378 break ;
00379 }
00380
00381 }
00382
00383
00384
00385 Length Sprite::GetShapeHeightFor( Shape shape )
00386 {
00387
00388 switch( shape )
00389 {
00390
00391 case EightTimesEight:
00392 case SixteenTimesEight:
00393 case ThirtyTwoTimesEight:
00394 return 8 ;
00395 break ;
00396
00397 case EightTimesSixteen:
00398 case SixteenTimesSixteen:
00399 case ThirtyTwoTimesSixteen:
00400 return 16 ;
00401 break ;
00402
00403 case EightTimesThirtyTwo:
00404 case SixteenTimesThirtyTwo:
00405 case ThirtyTwoTimesThirtyTwo:
00406 case SixtyFourTimesThirtyTwo:
00407 return 32 ;
00408 break ;
00409
00410 case ThirtyTwoTimesSixtyFour:
00411 case SixtyFourTimesSixtyFour:
00412 return 64 ;
00413 break ;
00414
00415 default:
00416 throw SpriteException( "Sprite::GetShapeHeightFor failed: "
00417 "unknown shape (" + Ceylan::toNumericalString( shape ) + ")" ) ;
00418 break ;
00419 }
00420
00421 }
00422