QGearsBackground2DFileXMLSerializer.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. -----------------------------------------------------------------------------
  3. The MIT License (MIT)
  4. Copyright (c) 2013-08-26 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 "map/QGearsBackground2DFileXMLSerializer.h"
  23. #include <OgreLogManager.h>
  24. #include <OgreException.h>
  25. namespace QGears
  26. {
  27. //---------------------------------------------------------------------
  28. const String Background2DFileXMLSerializer::BLENDING_ALPHA( "alpha" );
  29. const String Background2DFileXMLSerializer::BLENDING_ADD ( "add" );
  30. //---------------------------------------------------------------------
  31. Background2DFileXMLSerializer::Background2DFileXMLSerializer() :
  32. XMLSerializer()
  33. {
  34. }
  35. //---------------------------------------------------------------------
  36. Background2DFileXMLSerializer::~Background2DFileXMLSerializer()
  37. {
  38. }
  39. //---------------------------------------------------------------------
  40. void
  41. Background2DFileXMLSerializer::readHeader( TiXmlNode *node )
  42. {
  43. if( node == nullptr || node->ValueStr() != "background2d" )
  44. {
  45. OGRE_EXCEPT(Ogre::Exception::ERR_INVALIDPARAMS
  46. ,"not a valid background2d file, no <background2d> in root"
  47. ,"Background2DFileXMLSerializer::readHeader" );
  48. }
  49. }
  50. //---------------------------------------------------------------------
  51. bool
  52. Background2DFileXMLSerializer::readAttribute( TiXmlNode &node, const String &attribute, Blending &pDest, const Blending &pDefault )
  53. {
  54. const String *value( readAttribute( node, attribute ) );
  55. if( value == nullptr )
  56. {
  57. pDest = pDefault;
  58. return false;
  59. }
  60. if( *value == BLENDING_ADD )
  61. {
  62. pDest = B_ADD;
  63. return true;
  64. }
  65. if( *value == BLENDING_ALPHA )
  66. {
  67. pDest = B_ALPHA;
  68. return true;
  69. }
  70. pDest = pDefault;
  71. return false;
  72. }
  73. //---------------------------------------------------------------------
  74. void
  75. Background2DFileXMLSerializer::importBackground2DFile( Ogre::DataStreamPtr &stream
  76. ,Background2DFile *pDest )
  77. {
  78. TiXmlDocument document;
  79. parse( stream, document );
  80. TiXmlNode *root_node( document.RootElement() );
  81. readHeader( root_node );
  82. TiXmlNode &node( *root_node );
  83. String texture_name;
  84. readAttribute( node, "image", texture_name );
  85. if( texture_name.empty() )
  86. {
  87. OGRE_EXCEPT(Ogre::Exception::ERR_INVALIDPARAMS
  88. ,"image attribute not set"
  89. ,"Background2DFileXMLSerializer::importBackground2DFile" );
  90. }
  91. pDest->setTextureName( texture_name );
  92. Ogre::Vector2 clip;
  93. readAttribute( node, "clip", clip, Ogre::Vector2( 320, 240 ) );
  94. pDest->setClip( clip );
  95. Ogre::Vector4 range;
  96. readAttribute( node, "range", range, Ogre::Vector4( -100000, -100000, 100000, 100000 ) );
  97. pDest->setRange( range );
  98. Ogre::Vector3 position;
  99. readAttribute( node, "position", position );
  100. pDest->setPosition( position );
  101. Ogre::Quaternion orientation;
  102. readAttribute( node, "orientation", orientation );
  103. pDest->setOrientation( orientation );
  104. Ogre::Real fov;
  105. readAttribute( node, "fov", fov, Ogre::Real( 45 ) );
  106. pDest->setFov( Ogre::Radian( Ogre::Degree( fov ) ) );
  107. readVector( node, "tile", pDest->getTiles() );
  108. }
  109. //---------------------------------------------------------------------
  110. void
  111. Background2DFileXMLSerializer::readObject( TiXmlNode& node, Tile& pDest )
  112. {
  113. readAttribute( node, "width", pDest.width );
  114. readAttribute( node, "height", pDest.height );
  115. readAttribute( node, "destination", pDest.destination );
  116. readAttribute( node, "uv", pDest.uv );
  117. readAttribute( node, "depth", pDest.depth );
  118. readAttribute( node, "blending", pDest.blending, B_ALPHA );
  119. readMap( node, "animation", "name", pDest.animations );
  120. }
  121. //---------------------------------------------------------------------
  122. void
  123. Background2DFileXMLSerializer::readObject( TiXmlNode& node, Animation& pDest )
  124. {
  125. readAttribute( node, "length", pDest.length );
  126. readVector( node, "keyframe", pDest.key_frames );
  127. }
  128. //---------------------------------------------------------------------
  129. void
  130. Background2DFileXMLSerializer::readObject( TiXmlNode& node, KeyFrame& pDest )
  131. {
  132. readAttribute( node, "time", pDest.time );
  133. readAttribute( node, "uv", pDest.uv );
  134. }
  135. //---------------------------------------------------------------------
  136. }