FF7WalkmeshFileSerializer.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. -----------------------------------------------------------------------------
  3. Copyright (c) 2013-09-05 Tobias Peters <tobias.peters@kreativeffekt.at>
  4. This file is part of Q-Gears
  5. Q-Gears is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, version 2.0 (GPLv2) of the License.
  8. Q-Gears is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. -----------------------------------------------------------------------------
  13. */
  14. #include "map/FF7WalkmeshFileSerializer.h"
  15. #include <OgreLogManager.h>
  16. #include <OgreException.h>
  17. namespace QGears
  18. {
  19. namespace FF7
  20. {
  21. //----------------------------------------------------------------------
  22. WalkmeshFileSerializer::WalkmeshFileSerializer() :
  23. Serializer()
  24. {
  25. }
  26. //----------------------------------------------------------------------
  27. WalkmeshFileSerializer::~WalkmeshFileSerializer()
  28. {
  29. }
  30. //----------------------------------------------------------------------
  31. template<typename ValueType>
  32. void
  33. WalkmeshFileSerializer::readVector( Ogre::DataStreamPtr &stream
  34. ,std::vector<ValueType> &pDest
  35. ,size_t count )
  36. {
  37. pDest.clear();
  38. pDest.reserve( count );
  39. for( size_t i( count ); i--; )
  40. {
  41. ValueType in_tmp;
  42. readObject( stream, in_tmp );
  43. pDest.push_back( in_tmp );
  44. }
  45. }
  46. //----------------------------------------------------------------------
  47. void
  48. WalkmeshFileSerializer::importWalkmeshFile( Ogre::DataStreamPtr &stream
  49. ,WalkmeshFile *pDest )
  50. {
  51. uint32 triangle_count( 0 );
  52. readUInt32( stream, triangle_count );
  53. TriangleList triangles;
  54. readVector( stream, triangles, triangle_count );
  55. AccessList access;
  56. readVector( stream, access, triangle_count );
  57. WalkmeshTriangleList &triangle_list( pDest->getTriangles() );
  58. triangle_list.clear();
  59. triangle_list.reserve( triangle_count );
  60. for( uint32 i(0); i < triangle_count; ++i )
  61. {
  62. Triangle &in_triangle( triangles[i] );
  63. Access &in_access( access[i] );
  64. WalkmeshTriangle tmp_triangle;
  65. tmp_triangle.a = in_triangle.a;
  66. tmp_triangle.b = in_triangle.b;
  67. tmp_triangle.c = in_triangle.c;
  68. tmp_triangle.access_side[0] = in_access.a;
  69. tmp_triangle.access_side[1] = in_access.b;
  70. tmp_triangle.access_side[2] = in_access.c;
  71. triangle_list.push_back( tmp_triangle );
  72. }
  73. }
  74. //----------------------------------------------------------------------
  75. void WalkmeshFileSerializer::readObject( Ogre::DataStreamPtr &stream
  76. ,WalkmeshFileSerializer::Triangle &pDest)
  77. {
  78. readObject( stream, pDest.a );
  79. readObject( stream, pDest.b );
  80. readObject( stream, pDest.c );
  81. }
  82. //----------------------------------------------------------------------
  83. void WalkmeshFileSerializer::readObject( Ogre::DataStreamPtr &stream
  84. ,Ogre::Vector3 &pDest )
  85. {
  86. sint16 tmp[ VERTEX_COMPONENT_COUNT ];
  87. readShorts( stream, reinterpret_cast<uint16*>( tmp ), VERTEX_COMPONENT_COUNT );
  88. pDest.x = static_cast<Ogre::Real>( tmp[0] );
  89. pDest.y = static_cast<Ogre::Real>( tmp[1] );
  90. pDest.z = static_cast<Ogre::Real>( tmp[2] );
  91. }
  92. //----------------------------------------------------------------------
  93. void WalkmeshFileSerializer::readObject( Ogre::DataStreamPtr &stream
  94. ,WalkmeshFileSerializer::Access &pDest )
  95. {
  96. uint16 tmp[ ACCESS_COMPONENT_COUNT ];
  97. readShorts( stream, tmp, ACCESS_COMPONENT_COUNT );
  98. pDest.a = tmp[0];
  99. pDest.b = tmp[1];
  100. pDest.c = tmp[2];
  101. }
  102. //----------------------------------------------------------------------
  103. }
  104. }