1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- #include "ChunkMap.h"
- #include "Constants.h"
- ChunkMap::ChunkMap(Framework::StreamReader* zReader)
- : ReferenceCounter(),
- maxHeight(0)
- {
- pixels = new MapPixel[CHUNK_SIZE * CHUNK_SIZE];
- memset(pixels, 0, sizeof(MapPixel) * CHUNK_SIZE * CHUNK_SIZE);
- heightMap = new unsigned char[CHUNK_SIZE * CHUNK_SIZE];
- memset(heightMap, 0, CHUNK_SIZE * CHUNK_SIZE);
- zReader->lese((char*)&chunkCenter.x, 4);
- zReader->lese((char*)&chunkCenter.y, 4);
- for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE; i++)
- {
- zReader->lese((char*)&pixels[i].len, 1);
- if (pixels[i].len > 0)
- {
- pixels[i].blocks = new MapBlock[pixels[i].len];
- zReader->lese(
- (char*)pixels[i].blocks, (int)sizeof(MapBlock) * pixels[i].len);
- }
- }
- rendered.neuBild(16, 16, 0xA0000000);
- setMaxHeight(255);
- }
- ChunkMap::ChunkMap(Framework::Punkt center)
- : ReferenceCounter(),
- chunkCenter(center),
- maxHeight(0)
- {
- pixels = new MapPixel[CHUNK_SIZE * CHUNK_SIZE];
- memset(pixels, 0, sizeof(MapPixel) * CHUNK_SIZE * CHUNK_SIZE);
- heightMap = new unsigned char[CHUNK_SIZE * CHUNK_SIZE];
- memset(heightMap, 0, CHUNK_SIZE * CHUNK_SIZE);
- rendered.neuBild(16, 16, 0xA0000000);
- setMaxHeight(255);
- }
- ChunkMap::~ChunkMap()
- {
- for (int i = 0; i < CHUNK_SIZE * CHUNK_SIZE; i++)
- {
- delete[] pixels[i].blocks;
- }
- delete[] pixels;
- delete[] heightMap;
- }
- void ChunkMap::setMaxHeight(unsigned char maxHeight)
- {
- if (maxHeight != this->maxHeight)
- {
- rendered.fillRegion(0, 0, CHUNK_SIZE, CHUNK_SIZE, 0xA0000000);
- memset(heightMap, 0, CHUNK_SIZE * CHUNK_SIZE);
- this->maxHeight = maxHeight;
- for (int x = 0; x < CHUNK_SIZE; x++)
- {
- for (int y = 0; y < CHUNK_SIZE; y++)
- {
- int i = x * CHUNK_SIZE + y;
- bool first = 1;
- for (int j = 0; j < pixels[i].len
- && pixels[i].blocks[j].height <= maxHeight;
- j++)
- {
- if (first)
- {
- rendered.setPixelDP(x, y, 0xFF000000);
- first = 0;
- }
- rendered.alphaPixel2D(x, y, pixels[i].blocks[j].color);
- heightMap[y * CHUNK_SIZE + x] = pixels[i].blocks[j].height;
- }
- }
- }
- }
- }
- const Framework::Bild& ChunkMap::getRenderedImage() const
- {
- return rendered;
- }
- unsigned char* ChunkMap::getHeightMap() const
- {
- return heightMap;
- }
- Framework::Punkt ChunkMap::getChunkCenter() const
- {
- return chunkCenter;
- }
|