summaryrefslogtreecommitdiff
path: root/src/cObject.cpp
blob: 5440c283793d19688e654d9ac219032a89e0efbe (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "cObject.h"

cObject::cObject(int _sx, int _sy)  : pos({0,0}) , bSizeSet(false)
{
	setSize(_sx, _sy);
}

cObject::~cObject()
{
	destruct();
}

sPos cObject::getPosition()
{
	return pos;
}

void cObject::setPosition(sPos _pos)
{
	pos = _pos;
}


void cObject::setPosition(int _x, int _y)
{
	pos.x = _x;
	pos.y = _y;
}

void cObject::write(cRender *_render, sPos _cameraPosition)
{
	if(!bSizeSet)
		return;

	for (unsigned int o = 0; o < sizeY; o++) { //y axis
		for (unsigned int p = 0; p < sizeX; p++) { //x axis
			if (cScreen[p][o]) { //Dont overwrite empty pixels
				sPos npos{ pos.x + (int)p - _cameraPosition.x,
									pos.y + (int)o - _cameraPosition.y };
				_render->drawPoint(cScreen[p][o], npos, wColor[p][o]);
			}
		}
	}
}

//protected
cObject::cObject() : pos({0,0}) , bSizeSet(false){}

void cObject::setSize(unsigned int _sx, unsigned int _sy)
{
	if(bSizeSet)
		return;

	bBlockRender = true; //Block inherited render capabilities of parent

	sizeX = _sx;
	sizeY = _sy;

	//Initialize 2D array
	cScreen = (char**) malloc(sizeof *cScreen * _sx);
	for (unsigned int i = 0; i < _sx; i++)
		cScreen[i] = (char*)malloc(sizeof *cScreen[i] * _sy);

	wColor = (uint16_t**)malloc(sizeof *wColor * _sx);
	for (unsigned int i = 0; i < _sx; i++)
		wColor[i] = (uint16_t*)malloc(sizeof *wColor[i] * _sy);

	for (unsigned int i = 0; i < sizeY; i++) {
		for (unsigned int o = 0; o < sizeX; o++) {
			cScreen[o][i] = 0;
			wColor[o][i] = _COL_DEFAULT;
		}
	}

	bSizeSet = true;
}

void cObject::destruct()
{
	if(!bSizeSet)
		return;

	for (unsigned int i = 0; i < sizeX; i++) {
		free(cScreen[i]);
		free(wColor[i]);
	}

	free(cScreen);
	free(wColor);

	bSizeSet = false;
}