2014-09-15 10:32:27 +00:00
|
|
|
#include "PaintedGridData.h"
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include "PackStream.h"
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
PaintedGridData::PaintedGridData() {
|
2014-09-15 10:32:27 +00:00
|
|
|
rows_count = 0;
|
2015-08-12 17:29:28 +00:00
|
|
|
rows = (HeightMapRow *)malloc(sizeof(HeightMapRow));
|
2014-09-15 10:32:27 +00:00
|
|
|
memsize = 0;
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
PaintedGridData::~PaintedGridData() {
|
2014-09-15 10:32:27 +00:00
|
|
|
clear();
|
2015-08-12 17:29:28 +00:00
|
|
|
free(rows);
|
2014-09-15 10:32:27 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void PaintedGridData::copy(PaintedGridData *destination) const {
|
2014-09-15 10:32:27 +00:00
|
|
|
int i, j, n;
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
destination->clear();
|
|
|
|
|
|
|
|
destination->rows_count = this->rows_count;
|
2015-11-09 21:30:46 +00:00
|
|
|
if (destination->rows_count > 0) {
|
2014-09-15 10:32:27 +00:00
|
|
|
size = sizeof(HeightMapRow) * destination->rows_count;
|
2015-11-09 21:30:46 +00:00
|
|
|
destination->rows = (HeightMapRow *)realloc(destination->rows, size);
|
2014-09-15 10:32:27 +00:00
|
|
|
destination->memsize += size;
|
2015-11-09 21:30:46 +00:00
|
|
|
for (i = 0; i < destination->rows_count; i++) {
|
2014-09-15 10:32:27 +00:00
|
|
|
destination->rows[i].y = this->rows[i].y;
|
|
|
|
destination->rows[i].pixel_groups_count = this->rows[i].pixel_groups_count;
|
|
|
|
size = sizeof(HeightMapPixelGroup) * destination->rows[i].pixel_groups_count;
|
2015-11-09 21:30:46 +00:00
|
|
|
destination->rows[i].pixel_groups = (HeightMapPixelGroup *)malloc(size);
|
2014-09-15 10:32:27 +00:00
|
|
|
destination->memsize += size;
|
2015-11-09 21:30:46 +00:00
|
|
|
for (j = 0; j < destination->rows[i].pixel_groups_count; j++) {
|
2014-09-15 10:32:27 +00:00
|
|
|
destination->rows[i].pixel_groups[j].xstart = this->rows[i].pixel_groups[j].xstart;
|
|
|
|
destination->rows[i].pixel_groups[j].xend = this->rows[i].pixel_groups[j].xend;
|
|
|
|
n = destination->rows[i].pixel_groups[j].xend - destination->rows[i].pixel_groups[j].xstart + 1;
|
|
|
|
size = sizeof(double) * n;
|
2015-11-09 21:30:46 +00:00
|
|
|
destination->rows[i].pixel_groups[j].height = (double *)malloc(size);
|
2014-09-15 10:32:27 +00:00
|
|
|
destination->memsize += size;
|
|
|
|
memcpy(destination->rows[i].pixel_groups[j].height, this->rows[i].pixel_groups[j].height, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void PaintedGridData::save(PackStream *stream) const {
|
2014-09-15 10:32:27 +00:00
|
|
|
int i, j, k;
|
|
|
|
stream->write(&rows_count);
|
2015-11-09 21:30:46 +00:00
|
|
|
for (i = 0; i < rows_count; i++) {
|
2014-09-15 10:32:27 +00:00
|
|
|
stream->write(&rows[i].y);
|
|
|
|
stream->write(&rows[i].pixel_groups_count);
|
2015-11-09 21:30:46 +00:00
|
|
|
for (j = 0; j < rows[i].pixel_groups_count; j++) {
|
2014-09-15 10:32:27 +00:00
|
|
|
stream->write(&rows[i].pixel_groups[j].xstart);
|
|
|
|
stream->write(&rows[i].pixel_groups[j].xend);
|
2015-11-09 21:30:46 +00:00
|
|
|
for (k = 0; k < rows[i].pixel_groups[j].xend - rows[i].pixel_groups[j].xstart; k++) {
|
2014-09-15 10:32:27 +00:00
|
|
|
stream->write(&rows[i].pixel_groups[j].height[k]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void PaintedGridData::load(PackStream *stream) {
|
2014-09-15 10:32:27 +00:00
|
|
|
int i, j, k, n;
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
clear();
|
|
|
|
|
|
|
|
stream->read(&rows_count);
|
2015-11-09 21:30:46 +00:00
|
|
|
if (rows_count > 0) {
|
2014-09-15 10:32:27 +00:00
|
|
|
size = sizeof(HeightMapRow) * rows_count;
|
2015-11-09 21:30:46 +00:00
|
|
|
rows = (HeightMapRow *)realloc(rows, size);
|
2014-09-15 10:32:27 +00:00
|
|
|
memsize += size;
|
2015-11-09 21:30:46 +00:00
|
|
|
for (i = 0; i < rows_count; i++) {
|
2014-09-15 10:32:27 +00:00
|
|
|
stream->read(&rows[i].y);
|
|
|
|
stream->read(&rows[i].pixel_groups_count);
|
|
|
|
size = sizeof(HeightMapPixelGroup) * rows[i].pixel_groups_count;
|
2015-11-09 21:30:46 +00:00
|
|
|
rows[i].pixel_groups = (HeightMapPixelGroup *)malloc(size);
|
2014-09-15 10:32:27 +00:00
|
|
|
memsize += size;
|
2015-11-09 21:30:46 +00:00
|
|
|
for (j = 0; j < rows[i].pixel_groups_count; j++) {
|
2014-09-15 10:32:27 +00:00
|
|
|
stream->read(&rows[i].pixel_groups[j].xstart);
|
|
|
|
stream->read(&rows[i].pixel_groups[j].xend);
|
|
|
|
n = rows[i].pixel_groups[j].xend - rows[i].pixel_groups[j].xstart;
|
|
|
|
size = sizeof(double) * n;
|
2015-11-09 21:30:46 +00:00
|
|
|
rows[i].pixel_groups[j].height = (double *)malloc(size);
|
2014-09-15 10:32:27 +00:00
|
|
|
memsize += size;
|
2015-11-09 21:30:46 +00:00
|
|
|
for (k = 0; k < n; k++) {
|
2014-09-15 10:32:27 +00:00
|
|
|
stream->read(&rows[i].pixel_groups[j].height[k]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void PaintedGridData::clear() {
|
2014-09-15 10:32:27 +00:00
|
|
|
int i, j;
|
2015-11-09 21:30:46 +00:00
|
|
|
for (i = 0; i < rows_count; i++) {
|
|
|
|
for (j = 0; j < rows[i].pixel_groups_count; j++) {
|
2014-09-15 10:32:27 +00:00
|
|
|
free(rows[i].pixel_groups[j].height);
|
|
|
|
}
|
|
|
|
free(rows[i].pixel_groups);
|
|
|
|
}
|
|
|
|
rows_count = 0;
|
2015-08-12 17:29:28 +00:00
|
|
|
free(rows);
|
|
|
|
rows = (HeightMapRow *)malloc(sizeof(HeightMapRow));
|
2014-09-15 10:32:27 +00:00
|
|
|
memsize = 0;
|
|
|
|
}
|