aboutsummaryrefslogtreecommitdiff
path: root/Canvas.cpp
blob: 46d2f03b91b054daea0970493929fa5c47fb9ac2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <cstdio>

#include "Canvas.h"

Tile & Canvas::get_tile(unsigned x, unsigned y) {
	return *this->tiles[this->pos_to_index(x, y)];
}

void Canvas::set_tile(unsigned x, unsigned y, TileData t) {
	printf("%s(%d, %d)\n", __FUNCTION__, x, y);
	size_t index = this->pos_to_index(x, y);
	if (this->tiles[index] != nullptr)
		delete this->tiles[index];
	this->tiles[index] = new Tile(t);
}

size_t Canvas::pos_to_index(unsigned x, unsigned y) {
	size_t index = y * this->data.columns + x;
	return index;
}

void Canvas::update() {
	this->tiles.resize(this->data.rows * this->data.columns);
	for (size_t i = 0; i < this->tiles.size(); i++) {
		if (this->tiles[i] != nullptr) continue;
		this->tiles[i] = new Tile();
	}
}

Canvas::~Canvas() {
	for (size_t i = 0; i < this->tiles.size(); i++) {
		if (this->tiles[i] == nullptr) continue;
		delete this->tiles[i];
		this->tiles[i] = nullptr;
	}
}