summaryrefslogtreecommitdiff
path: root/AmpelJonas/cRender.cpp
blob: 4f18df5da9a0c55bf0b6155630c4284e810228e0 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "cRender.h"


cRender::cRender(char _backound, WORD _color, int _sx, int _sy)
{
	bBlockRender = false; //If this Constructor is used, this instance is not inherited, thus render() doesn't need to be blocked
	iLastError = _OK_;



#ifdef __linux__ //In Linux, setting Console size is not supported, so it gets Size of Console (Window) instead.

	struct winsize w;
  ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);

	sizeX = w.ws_row;
	sizeY = w.ws_col;

	if(sizeX < _sx || sizeY < _sy) //Notify Program tha screen is too small for desired Size
		iLastError = _ERR_SCREEN_TOO_SMALL_;

#elif _WIN32 //Windows Specific Code
	hstdout = GetStdHandle(STD_OUTPUT_HANDLE); //get handle

	GetConsoleScreenBufferInfo(hstdout, &csbi); //get current console settings
	wDefColor = csbi.wAttributes; //Get default console color

	SetConsoleWindowSize(_sx + 1, _sy + 1); //set the windows size to _sx * _sy (+1 so no scrolling accurs)

	sizeX = _sx;
	sizeY = _sy;
#endif



	cBackound = _backound;
	wBackColor = _color;

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

	wColor = (WORD**)malloc(sizeof *wColor * sizeX);
	for (int i = 0; i < _sx; i++)
		wColor[i] = (WORD*)malloc(sizeof *wColor[i] * sizeY);

	clear(); //Init backround array
}//render() WINDOWS


cRender::cRender() {}

cRender::~cRender()
{
	//Free allocated memory
	for (int i = 0; i < sizeX; i++) {
		free(cScreen[i]);
		free(wColor[i]);
	}

	free(cScreen);
	free(wColor);
}

int cRender::drawPoint(char _c, sPos _pos, bool _overrideCollision, WORD _color)
{
	if (_pos.x >= sizeX || _pos.y >= sizeY || _pos.x < 0 || _pos.y < 0)
		return _ERR_COORDINATES_INVALID_;

	if (cScreen[_pos.x][_pos.y] != cBackound && _overrideCollision != true) //detect Collsision
		return _COLLISION_;

	cScreen[_pos.x][_pos.y] = _c;
	if (_color == _COL_DEFAULT) //_COL_DEFAULT is NOT a proper colorcode!
		wColor[_pos.x][_pos.y] = wDefColor;
	else
		wColor[_pos.x][_pos.y] = _color;

	return 0;
}

int cRender::drawLine(char _c, sPos _pos1, sPos _pos2, bool _overrideCollision, WORD _color)
{
	if (_pos1.x == _pos2.x)	{ //Horizontal line
		for (int i = _pos1.y; i <= _pos2.y; i++)
		{
			drawPoint(_c, sPos{_pos1.x, i}, _overrideCollision, _color);
		}
	}
	else if (_pos1.y == _pos2.y) { //Vertical line
		for (int i = _pos1.x; i <= _pos2.x; i++)
		{
			drawPoint(_c, sPos{ i, _pos1.y }, _overrideCollision, _color);
		}
	}
	else { //Diagonal Line
		int dX = _pos1.x - _pos2.x;
		int dY = _pos1.y - _pos2.y;
		float fGradient = (float)dY / (float)dX;

		for (int i = 0; i <= abs(dX); i++)
		{
			drawPoint(_c, sPos{i + _pos1.x, (int)(i * fGradient + _pos1.y + 0.5)}, _overrideCollision, _color); //+0.5 for rounding error
		}
	}

	return 0;
}

int cRender::drawText(string _s, sPos _pos, WORD _color)
{
	for (int i = 0; i < _s.length(); i++)
	{
		drawPoint(_s[i], sPos{ i + _pos.x,_pos.y }, true,  _color);
	}
	return 0;
}

int cRender::drawRectangle(char _border, char _fill, sPos _pos1, sPos _pos2, WORD _borderColor, WORD _fillColor)
{
	//Draw the four outside lines
	drawLine(_border, _pos1, sPos{ _pos1.x, _pos2.y }, true, _borderColor);
	drawLine(_border, _pos1, sPos{ _pos2.x, _pos1.y }, true, _borderColor);
	drawLine(_border, sPos{ _pos1.x, _pos2.y }, _pos2, true, _borderColor);
	drawLine(_border, sPos{ _pos2.x, _pos1.y }, _pos2, true, _borderColor);

	//Fill rectangle if _fill isn't NULL
	if (_fill) {
		for (int i = _pos1.y + 1; i < _pos2.y; i++) {
			for (int o = _pos1.x + 1; o < _pos2.x; o++) {
				drawPoint(_fill, sPos{ o,i }, true, _fillColor);
			}
		}
	}

	return 0;
}

int cRender::render(void)
{
	gotoxy(0,0);

	if (bBlockRender)
		return _ERR_RENDER_BLOCKED_BY_CHILD_;

	for (int i = 0; i < sizeY; i++) {
		for (int o = 0; o < sizeX; o++) {
			#ifdef _WIN32
			SetConsoleTextAttribute(hstdout, wColor[o][i] | _COL_INTENSITY);
			cout << cScreen[o][i];
			#elif __linux__
			cout << "\033["<< wColor[o][i] <<"m"<< cScreen[o][i] <<"\033[0m";
			#endif
		}
		cout << endl; //New Line Feed
	}
	return 0;
}

int cRender::clear(void)
{
	for (int i = 0; i < sizeY; i++) {
		for (int o = 0; o < sizeX; o++) {
			cScreen[o][i] = cBackound;
			wColor[o][i] = wBackColor;
		}
	}
	return 0;
}

#ifdef _WIN32
//Source: http://www.cplusplus.com/forum/windows/121444/
int cRender::SetConsoleWindowSize(int x, int y)
{
	HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);

	if (h == INVALID_HANDLE_VALUE)
		return 1;

	CONSOLE_SCREEN_BUFFER_INFO bufferInfo;
	if (!GetConsoleScreenBufferInfo(h, &bufferInfo))
		return 1;

	SMALL_RECT& winInfo = bufferInfo.srWindow;
	COORD windowSize = { winInfo.Right - winInfo.Left + 1, winInfo.Bottom - winInfo.Top + 1 };

	if (windowSize.X > x || windowSize.Y > y)
	{
		// window size needs to be adjusted before the buffer size can be reduced.
		SMALL_RECT info =
		{
			0, 0,
			x < windowSize.X ? x - 1 : windowSize.X - 1,
			y < windowSize.Y ? y - 1 : windowSize.Y - 1
		};

		if (!SetConsoleWindowInfo(h, TRUE, &info))
			return 1;
	}

	COORD size = { x, y };
	if (!SetConsoleScreenBufferSize(h, size))
		return 1;

	SMALL_RECT info = { 0, 0, x - 1, y - 1 };
	if (!SetConsoleWindowInfo(h, TRUE, &info))
		return 1;
}
#endif

int cRender::getLastError()
{
	return iLastError;
}

#ifdef _WIN32
void cRender::gotoxy( int x, int y )
{
  COORD p = { x, y };
  SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), p );
}

#elif __linux__

void cRender::gotoxy( int x, int y )
{
  int err;
  if (!cur_term)
    setupterm( NULL, STDOUT_FILENO, &err );
  putp( tparm( tigetstr( "cup" ), y, x, 0, 0, 0, 0, 0, 0, 0 ) );
}
#endif