ChunkMap.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "ChunkMap.h"
  2. #include "Constants.h"
  3. ChunkMap::ChunkMap(Framework::StreamReader* zReader)
  4. : ReferenceCounter(),
  5. maxHeight(0)
  6. {
  7. pixels = new MapPixel[CHUNK_SIZE * CHUNK_SIZE];
  8. memset(pixels, 0, sizeof(MapPixel) * CHUNK_SIZE * CHUNK_SIZE);
  9. heightMap = new unsigned char[CHUNK_SIZE * CHUNK_SIZE];
  10. memset(heightMap, 0, CHUNK_SIZE * CHUNK_SIZE);
  11. zReader->lese((char*)&chunkCenter.x, 4);
  12. zReader->lese((char*)&chunkCenter.y, 4);
  13. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE; i++)
  14. {
  15. zReader->lese((char*)&pixels[i].len, 1);
  16. if (pixels[i].len > 0)
  17. {
  18. pixels[i].blocks = new MapBlock[pixels[i].len];
  19. zReader->lese(
  20. (char*)pixels[i].blocks, (int)sizeof(MapBlock) * pixels[i].len);
  21. }
  22. }
  23. rendered.neuBild(16, 16, 0xA0000000);
  24. setMaxHeight(255);
  25. }
  26. ChunkMap::ChunkMap(Framework::Punkt center)
  27. : ReferenceCounter(),
  28. chunkCenter(center),
  29. maxHeight(0)
  30. {
  31. pixels = new MapPixel[CHUNK_SIZE * CHUNK_SIZE];
  32. memset(pixels, 0, sizeof(MapPixel) * CHUNK_SIZE * CHUNK_SIZE);
  33. heightMap = new unsigned char[CHUNK_SIZE * CHUNK_SIZE];
  34. memset(heightMap, 0, CHUNK_SIZE * CHUNK_SIZE);
  35. rendered.neuBild(16, 16, 0xA0000000);
  36. setMaxHeight(255);
  37. }
  38. ChunkMap::~ChunkMap()
  39. {
  40. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE; i++)
  41. {
  42. delete[] pixels[i].blocks;
  43. }
  44. delete[] pixels;
  45. delete[] heightMap;
  46. }
  47. void ChunkMap::setMaxHeight(unsigned char maxHeight)
  48. {
  49. if (maxHeight != this->maxHeight)
  50. {
  51. rendered.fillRegion(0, 0, CHUNK_SIZE, CHUNK_SIZE, 0xA0000000);
  52. memset(heightMap, 0, CHUNK_SIZE * CHUNK_SIZE);
  53. this->maxHeight = maxHeight;
  54. for (int x = 0; x < CHUNK_SIZE; x++)
  55. {
  56. for (int y = 0; y < CHUNK_SIZE; y++)
  57. {
  58. int i = x * CHUNK_SIZE + y;
  59. bool first = 1;
  60. for (int j = 0; j < pixels[i].len
  61. && pixels[i].blocks[j].height <= maxHeight;
  62. j++)
  63. {
  64. if (first)
  65. {
  66. rendered.setPixelDP(x, y, 0xFF000000);
  67. first = 0;
  68. }
  69. rendered.alphaPixel2D(x, y, pixels[i].blocks[j].color);
  70. heightMap[y * CHUNK_SIZE + x] = pixels[i].blocks[j].height;
  71. }
  72. }
  73. }
  74. }
  75. }
  76. const Framework::Bild& ChunkMap::getRenderedImage() const
  77. {
  78. return rendered;
  79. }
  80. unsigned char* ChunkMap::getHeightMap() const
  81. {
  82. return heightMap;
  83. }
  84. Framework::Punkt ChunkMap::getChunkCenter() const
  85. {
  86. return chunkCenter;
  87. }