Clone of Bael'Zharon's Respite @ https://github.com/boardwalk/bzr

Environment.cpp 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Bael'Zharon's Respite
  3. * Copyright (C) 2014 Daniel Skorupski
  4. *
  5. * This program 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; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include "resource/Environment.h"
  19. #include "resource/BSP.h"
  20. #include "BinReader.h"
  21. #include "TriangleFan.h"
  22. #include "Vertex.h"
  23. CellStruct::CellStruct()
  24. {}
  25. CellStruct::CellStruct(CellStruct&& other)
  26. {
  27. vertices = move(other.vertices);
  28. triangleFans = move(other.triangleFans);
  29. hitTriangleFans = move(other.hitTriangleFans);
  30. hitTree = move(other.hitTree);
  31. }
  32. CellStruct::~CellStruct()
  33. {}
  34. CellStruct& CellStruct::operator=(CellStruct&& other)
  35. {
  36. vertices = move(other.vertices);
  37. triangleFans = move(other.triangleFans);
  38. hitTriangleFans = move(other.hitTriangleFans);
  39. hitTree = move(other.hitTree);
  40. return *this;
  41. }
  42. static void read(BinReader& reader, CellStruct& part)
  43. {
  44. uint32_t numTriangleFans = reader.readInt();
  45. part.triangleFans.resize(numTriangleFans);
  46. uint32_t numHitTriangleFans = reader.readInt();
  47. part.hitTriangleFans.resize(numHitTriangleFans);
  48. uint32_t numPortals = reader.readInt();
  49. uint32_t unk5 = reader.readInt();
  50. assert(unk5 == 1);
  51. UNUSED(unk5);
  52. uint32_t numVertices = reader.readInt();
  53. part.vertices.resize(numVertices);
  54. for(uint32_t i = 0; i < numVertices; i++)
  55. {
  56. uint16_t vertexNum = reader.readShort();
  57. assert(vertexNum == i);
  58. UNUSED(vertexNum);
  59. read(reader, part.vertices[i]);
  60. }
  61. for(uint32_t i = 0; i < numTriangleFans; i++)
  62. {
  63. uint16_t triangleFanNum = reader.readShort();
  64. assert(triangleFanNum == i);
  65. UNUSED(triangleFanNum);
  66. read(reader, part.triangleFans[i]);
  67. }
  68. for(uint32_t i = 0; i < numPortals; i++)
  69. {
  70. reader.readShort();
  71. }
  72. reader.align();
  73. unique_ptr<BSPNode> cellBSP;
  74. read(reader, cellBSP, BSPTreeType::kCell);
  75. for(uint32_t i = 0; i < numHitTriangleFans; i++)
  76. {
  77. uint16_t triangleFanNum = reader.readShort();
  78. assert(triangleFanNum == i);
  79. UNUSED(triangleFanNum);
  80. read(reader, part.hitTriangleFans[i]);
  81. }
  82. unique_ptr<BSPNode> physicsBSP;
  83. read(reader, physicsBSP, BSPTreeType::kPhysics);
  84. uint32_t hasDrawingBSP = reader.readInt();
  85. assert(hasDrawingBSP == 0 || hasDrawingBSP == 1);
  86. if(hasDrawingBSP)
  87. {
  88. unique_ptr<BSPNode> drawingBSP;
  89. read(reader, drawingBSP, BSPTreeType::kDrawing);
  90. }
  91. reader.align();
  92. }
  93. Environment::Environment(uint32_t id, const void* data, size_t size) : ResourceImpl{id}
  94. {
  95. BinReader reader(data, size);
  96. uint32_t resourceId = reader.readInt();
  97. assert(resourceId == id);
  98. UNUSED(resourceId);
  99. uint32_t numParts = reader.readInt();
  100. parts.resize(numParts);
  101. for(uint32_t i = 0; i < numParts; i++)
  102. {
  103. uint32_t partNum = reader.readInt();
  104. assert(partNum == i);
  105. UNUSED(partNum);
  106. read(reader, parts[i]);
  107. }
  108. assert(reader.remaining() == 0);
  109. }