VGearsBackground2DFileXMLSerializer.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <OgreLogManager.h>
  16. #include <OgreException.h>
  17. #include "map/VGearsBackground2DFileXMLSerializer.h"
  18. namespace VGears{
  19. const String Background2DFileXMLSerializer::BLENDING_ALPHA("alpha");
  20. const String Background2DFileXMLSerializer::BLENDING_ADD("add");
  21. const String Background2DFileXMLSerializer::BLENDING_SUBTRACT("subtract");
  22. Background2DFileXMLSerializer::Background2DFileXMLSerializer() :
  23. XMLSerializer(){
  24. }
  25. Background2DFileXMLSerializer::~Background2DFileXMLSerializer(){}
  26. void Background2DFileXMLSerializer::ReadHeader(TiXmlNode *node){
  27. if(node == nullptr || node->ValueStr() != "background2d"){
  28. OGRE_EXCEPT(
  29. Ogre::Exception::ERR_INVALIDPARAMS,
  30. "not a valid background2d file, no <background2d> in root",
  31. "Background2DFileXMLSerializer::readHeader"
  32. );
  33. }
  34. }
  35. bool Background2DFileXMLSerializer::ReadAttribute(
  36. TiXmlNode &node, const String &attribute, Blending &dest,
  37. const Blending &def
  38. ){
  39. const String *value(ReadAttribute(node, attribute));
  40. if (value == nullptr){
  41. dest = def;
  42. return false;
  43. }
  44. if (*value == BLENDING_ADD){
  45. dest = B_ADD;
  46. return true;
  47. }
  48. if (*value == BLENDING_ALPHA){
  49. dest = B_ALPHA;
  50. return true;
  51. }
  52. if (*value == BLENDING_SUBTRACT){
  53. dest = B_SUBTRACT;
  54. return true;
  55. }
  56. dest = def;
  57. return false;
  58. }
  59. void Background2DFileXMLSerializer::ImportBackground2DFile(
  60. Ogre::DataStreamPtr &stream, Background2DFile *dest
  61. ){
  62. TiXmlDocument document;
  63. Parse(stream, document);
  64. TiXmlNode *root_node(document.RootElement());
  65. ReadHeader(root_node);
  66. TiXmlNode &node(*root_node);
  67. String texture_name;
  68. ReadAttribute(node, "image", texture_name);
  69. if (texture_name.empty()){
  70. OGRE_EXCEPT(
  71. Ogre::Exception::ERR_INVALIDPARAMS,
  72. "image attribute not set",
  73. "Background2DFileXMLSerializer::ImportBackground2DFile"
  74. );
  75. }
  76. dest->SetTextureName(texture_name);
  77. Ogre::Vector2 clip;
  78. ReadAttribute(node, "clip", clip, Ogre::Vector2(320, 240));
  79. dest->SetClip(clip);
  80. Ogre::Vector4 range;
  81. ReadAttribute(node, "range", range, Ogre::Vector4(
  82. -100000, -100000, 100000, 100000
  83. ));
  84. dest->SetRange(range);
  85. Ogre::Vector3 position;
  86. ReadAttribute(node, "position", position);
  87. dest->SetPosition(position);
  88. Ogre::Quaternion orientation;
  89. ReadAttribute(node, "orientation", orientation);
  90. dest->SetOrientation(orientation);
  91. Ogre::Real fov;
  92. ReadAttribute(node, "fov", fov, Ogre::Real(45));
  93. dest->SetFov(Ogre::Radian(Ogre::Degree(fov)));
  94. ReadVector(node, "tile", dest->GetTiles());
  95. }
  96. void Background2DFileXMLSerializer::readObject(TiXmlNode& node, Tile& dest){
  97. ReadAttribute(node, "width", dest.width);
  98. ReadAttribute(node, "height", dest.height);
  99. ReadAttribute(node, "destination", dest.destination);
  100. ReadAttribute(node, "uv", dest.uv);
  101. ReadAttribute(node, "depth", dest.depth);
  102. ReadAttribute(node, "blending", dest.blending, B_ALPHA);
  103. ReadAttribute(node, "area", dest.area, 0);
  104. ReadAttribute(node, "state", dest.state, 0);
  105. ReadMap(node, "animation", "name", dest.animations);
  106. }
  107. void Background2DFileXMLSerializer::readObject(
  108. TiXmlNode& node, Animation& dest
  109. ){
  110. ReadAttribute(node, "length", dest.length);
  111. ReadVector(node, "keyframe", dest.key_frames);
  112. }
  113. void Background2DFileXMLSerializer::readObject(
  114. TiXmlNode& node, KeyFrame& dest
  115. ){
  116. ReadAttribute(node, "time", dest.time);
  117. ReadAttribute(node, "uv", dest.uv);
  118. }
  119. }