summaryrefslogtreecommitdiff
path: root/cObjectHandler.cpp
blob: 0b4bb65d2cba265b09590b3bcf2aa901ef12379b (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "cObjectHandler.h"

cObjectHandler::cObjectHandler(cRender *_render)
{
	render = _render;
	iActiveObject = 0;

	objects.push_back(NULL); //Create first Object as Catcher for Events

	buildHitmap();
}

int cObjectHandler::createObject(cObject *_object)
{
	objects.push_back(_object);

	buildHitmap();
	return objects.size() - 1;
}

int cObjectHandler::moveObject(int _object, sPos _pos, int _mode)
{
	if (_object >= objects.size()) //prevent segmentation faults
		return 1;

	if (!objects[_object])
		return 1;

	sPos objPosition = objects[_object]->getPosition();

	if (_mode == _MOVE_RELATIVE)
		objects[_object]->setPosition(sPos{ objPosition.x + _pos.x, objPosition.y + _pos.y });
	else if (_mode == _MOVE_ABSOLUTE)
		objects[_object]->setPosition(_pos);

	buildHitmap();
	return 0;
}

int cObjectHandler::destroyObject(int _object)
{
	delete objects[_object];
	objects[_object] = NULL;

	buildHitmap();
	return 0;
}

int cObjectHandler::write()
{
	render->clear();

	for (unsigned long int i = 0; i < objects.size(); i++)
	{
		if (objects[i]) // Check if objects[i] is existent
		{
			//Draw every Object
			sObject obj = objects[i]->getObject(); //get Object #i

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

	return 0;
}

int cObjectHandler::clickEvent(sPos _pos, unsigned int _button)
{
	if(_pos.x >= iHitMap.size())
		return 1;
	if(_pos.y >= iHitMap[_pos.x].size())
		return 1;


	if(objects[ iHitMap[_pos.x][_pos.y] ])
	{
		iActiveObject = iHitMap[_pos.x][_pos.y]; //Set active object
		objects[ iHitMap[_pos.x][_pos.y] ]->onClick(_pos, _button);
	}
	else
		return 1;

	return 0;
}

int cObjectHandler::charEvent(unsigned char _c)
{
	if(objects.size() > iActiveObject)
	{
		if(objects[iActiveObject])
		{
			objects[iActiveObject]->onChar(_c);
		}
		else
			return 1;
	}

	return 0;
}

void cObjectHandler::buildHitmap()
{
	//Rebuild 2D vector
	sPos size = render->getSize();

	vector<unsigned int> cp;

	while(size.y > cp.size())
	{
		cp.push_back(0);
	}

	while (size.x > iHitMap.size())
	{
		iHitMap.push_back(cp);
	}

	while (size.x <= iHitMap.size())
	{
		iHitMap.pop_back();
	}
	for(unsigned int x = 0; x < iHitMap.size(); x++)
	{
		for(unsigned int y = 0; y < iHitMap[x].size(); y++)
		{
			iHitMap[x][y] = 0;
		}
	}
	//Write object IDs to iHitMap
	for(unsigned int i = 0; i < objects.size(); i++)
	{
		if(objects[i])
		{
			sPos oPos = objects[i]->getPosition();
			sPos oSize = objects[i]->getSize();

			for(int x = oPos.x; x <= oPos.x + oSize.x; x++)
			{
				for(int y = oPos.y; y <= oPos.y + oSize.y; y++)
				{
					if(x < size.x && y < size.y) //Objects can be outside the screen.
						iHitMap[x][y] = i;
				}//for
			}//for
		}//if
	}//for
}//buildHitmap

void cObjectHandler::focusNext()
{
	iActiveObject++;

	if(iActiveObject >= objects.size())
		iActiveObject = 0;
}

void cObjectHandler::focus(unsigned int _id)
{
	if(_id >= objects.size())
		iActiveObject = objects.size();
	else
		iActiveObject = _id;
}