QGearsFLevelBackground2DLoader.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. -----------------------------------------------------------------------------
  3. The MIT License (MIT)
  4. Copyright (c) 2013-08-29 Tobias Peters <tobias.peters@kreativeffekt.at>
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. -----------------------------------------------------------------------------
  21. */
  22. #include "data/QGearsFLevelBackground2DLoader.h"
  23. #include "data/QGearsFLevelFile.h"
  24. #include "map/QGearsBackground2DFile.h"
  25. #include "FF7Common.h"
  26. namespace QGears
  27. {
  28. //-------------------------------------------------------------------------
  29. FLevelBackground2DLoader::FLevelBackground2DLoader( FLevelFile &flevel_file ) :
  30. m_flevel_file( flevel_file )
  31. {
  32. }
  33. //-------------------------------------------------------------------------
  34. FLevelBackground2DLoader::~FLevelBackground2DLoader()
  35. {
  36. }
  37. //-------------------------------------------------------------------------
  38. void
  39. FLevelBackground2DLoader::loadResource( Ogre::Resource *resource )
  40. {
  41. Background2DFile *background_2d( static_cast<Background2DFile *>(resource) );
  42. assert( background_2d );
  43. m_flevel_file.load();
  44. background_2d->_notifyOrigin( m_flevel_file.getName() );
  45. background_2d->setTextureName( m_flevel_file.getBackgroundTextureName() );
  46. background_2d->setClip( Ogre::Vector2( FF7::SCREEN_WIDTH, FF7::SCREEN_HEIGHT ) );
  47. CameraMatrixFilePtr camera_matrix( m_flevel_file.getCameraMatrix() );
  48. Ogre::Real scale(Ogre::Real(camera_matrix->getCount()));
  49. background_2d->setPosition( camera_matrix->getPosition() / -( scale * FF7::FIELD_POSITION_SCALE ) );
  50. background_2d->setOrientation( camera_matrix->getOrientation() );
  51. background_2d->setFov( camera_matrix->getFov( FF7::SCREEN_WIDTH ) );
  52. Ogre::Vector4 range( 0, 0, 0, 0 );
  53. TileList& tiles( background_2d->getTiles() );
  54. BackgroundFilePtr background( m_flevel_file.getBackground() );
  55. SpriteList sprites;
  56. background->addAllSprites( sprites );
  57. SpriteList::const_iterator it ( sprites.begin() );
  58. SpriteList::const_iterator it_end( sprites.end() );
  59. Ogre::Real step( BackgroundFile::SPRITE_WIDTH / 1024.0 );
  60. Ogre::Vector4 uv( 0, 0, step, step );
  61. Ogre::Vector4 col( step, 0, step, 0 );
  62. Ogre::Vector4 row( 0, step, 0, step );
  63. while( it != it_end )
  64. {
  65. const SpriteData& sprite( *it );
  66. Tile tile;
  67. tile.width = BackgroundFile::SPRITE_WIDTH;
  68. tile.height = BackgroundFile::SPRITE_HEIGHT;
  69. tile.blending = B_ALPHA;
  70. tile.depth = sprite.depth / ( scale * FF7::FIELD_DEPTH_SCALE );
  71. /*
  72. if depth >= 1
  73. if( ??? / 4.0 < depth )
  74. if( depth < 4095 )
  75. else
  76. depth = 0.9999
  77. else
  78. depth = depth / 1000.0
  79. else
  80. depth = 0.0001
  81. */
  82. tile.destination.x = sprite.dst.x;
  83. tile.destination.y = sprite.dst.y;
  84. tile.uv = uv;
  85. range.x = std::min( tile.destination.x, range.x );
  86. range.y = std::min( tile.destination.y, range.y );
  87. range.z = std::max( tile.destination.x + tile.width , range.z );
  88. range.w = std::max( tile.destination.y + tile.height, range.w );
  89. tiles.push_back( tile );
  90. uv += col;
  91. if( uv.x >= 1 )
  92. {
  93. uv.x = 0;
  94. uv.z = step;
  95. uv += row;
  96. }
  97. ++it;
  98. }
  99. background_2d->setRange( range );
  100. }
  101. //-------------------------------------------------------------------------
  102. }