ChunkMap.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. pixels = new MapPixel[CHUNK_SIZE * CHUNK_SIZE];
  14. memset(pixels, 0, sizeof(MapPixel) * CHUNK_SIZE * CHUNK_SIZE);
  15. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE; i++)
  16. {
  17. zReader->lese((char*)&pixels[i].len, 1);
  18. if (pixels[i].len > 0)
  19. {
  20. pixels[i].blocks = new MapBlock[pixels[i].len];
  21. zReader->lese(
  22. (char*)pixels[i].blocks, (int)sizeof(MapBlock) * pixels[i].len);
  23. }
  24. }
  25. rendered.neuBild(16, 16, 0xA0000000);
  26. setMaxHeight(255);
  27. }
  28. ChunkMap::~ChunkMap()
  29. {
  30. for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE; i++)
  31. {
  32. delete[] pixels[i].blocks;
  33. }
  34. delete[] pixels;
  35. delete[] heightMap;
  36. }
  37. void ChunkMap::setMaxHeight(unsigned char maxHeight)
  38. {
  39. if (maxHeight != this->maxHeight)
  40. {
  41. rendered.fillRegion(0, 0, CHUNK_SIZE, CHUNK_SIZE, 0xA0000000);
  42. memset(heightMap, 0, CHUNK_SIZE * CHUNK_SIZE);
  43. this->maxHeight = maxHeight;
  44. for (int x = 0; x < CHUNK_SIZE; x++)
  45. {
  46. for (int y = 0; y < CHUNK_SIZE; y++)
  47. {
  48. int i = x * CHUNK_SIZE + y;
  49. bool first = 1;
  50. for (int j = 0; j < pixels[i].len
  51. && pixels[i].blocks[j].height <= maxHeight;
  52. j++)
  53. {
  54. if (first)
  55. {
  56. rendered.setPixelDP(x, y, 0xFF000000);
  57. first = 0;
  58. }
  59. rendered.alphaPixel2D(x, y, pixels[i].blocks[j].color);
  60. heightMap[y * CHUNK_SIZE + x] = pixels[i].blocks[j].height;
  61. }
  62. }
  63. }
  64. }
  65. }
  66. const Framework::Bild& ChunkMap::getRenderedImage() const
  67. {
  68. return rendered;
  69. }
  70. unsigned char* ChunkMap::getHeightMap() const {
  71. return heightMap;
  72. }
  73. Framework::Punkt ChunkMap::getChunkCenter() const
  74. {
  75. return chunkCenter;
  76. }