VGearsBackground2DFileXMLSerializer.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 "formats/field/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. String palette_name;
  78. ReadAttribute(node, "palette", palette_name, String());
  79. dest->SetPaletteName(palette_name);
  80. Ogre::Vector2 clip;
  81. ReadAttribute(node, "clip", clip, Ogre::Vector2(320, 240));
  82. dest->SetClip(clip);
  83. Ogre::Vector4 range;
  84. ReadAttribute(node, "range", range, Ogre::Vector4(
  85. -100000, -100000, 100000, 100000
  86. ));
  87. dest->SetRange(range);
  88. Ogre::Vector3 position;
  89. ReadAttribute(node, "position", position);
  90. dest->SetPosition(position);
  91. Ogre::Quaternion orientation;
  92. ReadAttribute(node, "orientation", orientation);
  93. dest->SetOrientation(orientation);
  94. Ogre::Real fov;
  95. ReadAttribute(node, "fov", fov, Ogre::Real(45));
  96. dest->SetFov(Ogre::Radian(Ogre::Degree(fov)));
  97. ReadVector(node, "tile", dest->GetTiles());
  98. }
  99. void Background2DFileXMLSerializer::readObject(TiXmlNode& node, Tile& dest){
  100. ReadAttribute(node, "width", dest.width);
  101. ReadAttribute(node, "height", dest.height);
  102. ReadAttribute(node, "destination", dest.destination);
  103. ReadAttribute(node, "uv", dest.uv);
  104. ReadAttribute(node, "depth", dest.depth);
  105. ReadAttribute(node, "blending", dest.blending, B_ALPHA);
  106. ReadAttribute(node, "area", dest.area, 0);
  107. ReadAttribute(node, "state", dest.state, 0);
  108. ReadMap(node, "animation", "name", dest.animations);
  109. }
  110. void Background2DFileXMLSerializer::readObject(
  111. TiXmlNode& node, Animation& dest
  112. ){
  113. ReadAttribute(node, "length", dest.length);
  114. ReadVector(node, "keyframe", dest.key_frames);
  115. }
  116. void Background2DFileXMLSerializer::readObject(
  117. TiXmlNode& node, KeyFrame& dest
  118. ){
  119. ReadAttribute(node, "time", dest.time);
  120. ReadAttribute(node, "uv", dest.uv);
  121. }
  122. }