aboutsummaryrefslogtreecommitdiff
path: root/src/cRender.cpp
blob: 5e7677fdca534c6b6ae865fd68ae1ddcc61c337c (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
#include "cRender.h"


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

	cBackound = _backound;
	wBackColor = _color;

	uTargetFPS = 0;
	lastFrameTime = 0;
	lastRenderTime = 0;

	bMute = false;
	bLockScreenSize = false;
	bPrintDebugInfo = false;

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

	wDefColor = _COL_DEFAULT;

	//Set up console
	setAlternateBufferScreen(true);
	setConsoleCursor(false);

#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)

	setBufferSize({_sx,_sy});
#endif //_WIN32

	setConsoleEcho(false);
}//render()


cRender::cRender()
{
	cScreen = NULL;
	wColor = NULL;
	bBlockRender = false;
	cBackound = 0;
	wBackColor = 0;
	sizeX = sizeY = 0;
}

cRender::~cRender()
{
	if(bBlockRender) //Don't run destructor if inherited
		return;

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

	free(cScreen);
	free(wColor);
	free(bChanged);

	setConsoleEcho(true);

	#ifdef __linux__
	setConsoleCursor(true);
	setAlternateBufferScreen(false);
	#endif //__linux__
}

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

	cScreen[_pos.x][_pos.y] = _c;

	#ifdef _WIN32

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

	#elif __linux__

		wColor[_pos.x][_pos.y] = _color;

	#endif

	if(!bBlockRender) //Changemap is not allocated in inherited Classes
		bChanged[_pos.x][_pos.y] = true;

	return 0;
}

int cRender::drawLine(char _c, sPos _pos1, sPos _pos2,  WORD _color)
{
	if(_pos1.x > _pos2.x)
	{
		//Shit WILL go wrong
		return drawLine(_c, _pos2, _pos1, _color);
	}

	if (_pos1.x == _pos2.x)	{ //Horizontal line
		for (int i = _pos1.y; i <= _pos2.y; i++)
		{
			drawPoint(_c, sPos{_pos1.x, i}, _color);
		}
	}
	else if (_pos1.y == _pos2.y) { //Vertical line
		for (int i = _pos1.x; i <= _pos2.x; i++)
		{
			drawPoint(_c, sPos{ i, _pos1.y }, _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)}, _color); //+0.5 for rounding error

			if(std::abs(fGradient) > 1.0)
			{
				int dy = (int)(((i + 1) * fGradient + _pos1.y + 0.5) - (i * fGradient + _pos1.y + 0.5));

				if(dy > 0 && ((int)(i * fGradient + _pos1.y + 0.5) + dy) <= _pos2.y)
				{
					drawLine(_c,
						sPos{i + _pos1.x, (int)(i * fGradient + _pos1.y + 0.5)},
						sPos{i + _pos1.x, (int)(i * fGradient + _pos1.y + 0.5) + dy },
						_color);
				}//if
				else if(dy < 0 && ((int)(i * fGradient + _pos1.y + 0.5) + dy) >= (_pos2.y) )
				{
					drawLine(_c,
						sPos{i + _pos1.x, (int)(i * fGradient + _pos1.y + 0.5) + dy },
						sPos{i + _pos1.x, (int)(i * fGradient + _pos1.y + 0.5)},
						_color);
				}//else if
			}//if
		}//for
	}//else

	return 0;
}//drawLine

int cRender::drawText(string _s, sPos _pos, WORD _color)
{
	for (unsigned int i = 0; i < _s.length(); i++)
	{
		drawPoint(_s[i], sPos{ (int)i + _pos.x,_pos.y },  _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 }, _borderColor);
	drawLine(_border, _pos1, sPos{ _pos2.x, _pos1.y }, _borderColor);
	drawLine(_border, sPos{ _pos1.x, _pos2.y }, _pos2, _borderColor);
	drawLine(_border, sPos{ _pos2.x, _pos1.y }, _pos2, _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 }, _fillColor);
			}
		}
	}

	return 0;
}

int cRender::render(void)
{
	if (bBlockRender)
		return _ERR_RENDER_BLOCKED_BY_CHILD_;

	waitForFrametime();

	//Resize screenbuffer if needed
	setBufferSize( getConsoleWindowSize( ) );

	printDebugInfo();

	//For measuring render time
	clock_t startTime = clock();

	for (unsigned int i = 0; i < sizeY; i++) {
		for (unsigned int o = 0; o < sizeX; o++) {
			if(bChanged[o][i])
			{
				#ifdef _WIN32

				gotoxy(o,i);
				SetConsoleTextAttribute(hstdout, wColor[o][i] | _COL_INTENSITY);

				if(!bMute)
					printf("%c", cScreen[o][i]);

				#elif __linux__

				const unsigned int buffer_len = 40;
				const unsigned int colorstr_len = 30;

				char buffer[ buffer_len ];
				char colorstr[ colorstr_len ];

				uint8_t color[3] = {(uint8_t) (0x0000ff & wColor[o][i]),				//Color
														(uint8_t)((0x00ff00 & wColor[o][i]) >> 8),	//Background
														(uint8_t)((0xff0000 & wColor[o][i]) >> 16)};//Modifier

				{////
					int cc = 0;
					cc = cc + snprintf(&colorstr[cc], colorstr_len - cc - 1, "%u", color[0]);
					if(color[1])
					{
						colorstr[cc] = ';';
						colorstr[cc + 1] = color[1];
						cc += 1 + snprintf(&colorstr[cc + 1],colorstr_len - cc - 1, "%u", color[1]);
					}
					if(color[2])
					{
						colorstr[cc] = ';';
						cc += 1 + snprintf(&colorstr[cc + 1], colorstr_len - cc - 1, "%u", color[2]);
					}
				}////

				int cbuf = snprintf(buffer, buffer_len - 1,"\e[%u;%uH\e[%sm%c\e[0m", i + 1, o + 1, colorstr, cScreen[o][i]);
				//      										Position  Color        Origin is at 1,1

				if(!bMute)
					write (STDOUT_FILENO, buffer, cbuf);

				#endif //__linux__
			}
			bChanged[o][i] = false;
		}
	}

	//calc render time
	lastRenderTime = clock() - startTime;

	//calc time since last render
	timespec now;
	clock_gettime(CLOCK_MONOTONIC, &now);
	lastFrameTime = (now.tv_sec - lastRender.tv_sec);
	lastFrameTime += (now.tv_nsec - lastRender.tv_nsec) / 1000000000.0;
	//save current time
	clock_gettime(CLOCK_MONOTONIC, &lastRender);

	return 0;
}

int cRender::clear(bool _forceReRender)
{
	for (unsigned int i = 0; i < sizeY; i++) {
		for (unsigned int o = 0; o < sizeX; o++) {
			if(((cScreen[o][i] == cBackound) && (wColor[o][i] == wBackColor)) && !_forceReRender)
				bChanged[o][i] 	= false;
			else
			{
				cScreen[o][i] 	= cBackound;
				wColor[o][i] 		= wBackColor;
				bChanged[o][i] 	= true;
			}
		}
	}
	return 0;
}

int cRender::clear()
{
	return clear(false);
}

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

#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;
}

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

#elif __linux__

sPos cRender::getConsoleWindowSize()
{
	struct winsize w;
  ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);

	return {w.ws_col, w.ws_row};
}

void cRender::setAlternateBufferScreen(bool _enable)
{
	_enable ? write (STDOUT_FILENO, "\e[?47h", 6):write (STDOUT_FILENO, "\e[?47l", 6);
}

void cRender::setConsoleCursor(bool _enable)
{
	_enable ? write (STDOUT_FILENO, "\e[?25h", 6) : write (STDOUT_FILENO, "\e[?25l", 6);
}

#endif // __linux__

void cRender::setBufferSize(sPos _size)
{
	if(bLockScreenSize)
		return;

	if(_size.x == (int)sizeX && _size.y == (int)sizeY)
		return;

	if(_size.x < 0 || _size.y < 0)
		return;

	if(sizeX!=0 && sizeY!=0) //resize. delete first
	{
		for (unsigned int i = 0; i < sizeX; i++) {
			free(cScreen[i]);
			free(wColor[i]);
			free(bChanged[i]);
		}

		free(cScreen);
		free(wColor);
		free(bChanged);
	}

	sizeX = _size.x;
	sizeY = _size.y;

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

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

	bChanged = (bool**)malloc(sizeof *bChanged * sizeX);
	for (unsigned int i = 0; i < sizeX; i++)
		bChanged[i] = (bool*)malloc(sizeof *bChanged[i] * sizeY);

	clear(true);
}

sPos cRender::getSize()
{
	return {(int)sizeX, (int)sizeY};
}

void cRender::forceReRender()
{
	for (unsigned int i = 0; i < sizeY; i++) {
		for (unsigned int o = 0; o < sizeX; o++) {
				bChanged[o][i] 	= true;
		}
	}
}

void cRender::setConsoleEcho(bool _enable)
{
#ifdef WIN32
    HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
    DWORD mode;
    GetConsoleMode(hStdin, &mode);

    if( !_enable )
        mode &= ~ENABLE_ECHO_INPUT;
    else
        mode |= ENABLE_ECHO_INPUT;

    SetConsoleMode(hStdin, mode );

#elif __linux__
		_enable ? write (STDOUT_FILENO, "\e[?8h", 5) : write (STDOUT_FILENO, "\e[?8l", 5);
#endif //__linux__
}

double cRender::getFrametime()
{
	return lastFrameTime;
}

void cRender::setTargetFPS(unsigned int _fps)
{
	uTargetFPS = _fps;
}

void cRender::printDebugInfo()
{
	if(!bPrintDebugInfo)
		return;

	const unsigned int dbgtxt_len = 30;
	char dbgtxt[ dbgtxt_len ];

	double lrt = (double)lastFrameTime / CLOCKS_PER_SEC;
	double fps = getFrametime() ? 1/getFrametime() : 0;

	snprintf(dbgtxt, dbgtxt_len - 1, "R: %f F: %f ", lrt, fps);

	drawText(dbgtxt, {0,0}, _COL_BLACK | _COL_WHITE_BG);
}

void cRender::waitForFrametime()
{
	if(!uTargetFPS)
		return;

	timespec now;
	double elapsed;
	double left;
	clock_gettime(CLOCK_MONOTONIC, &now);
	elapsed = (now.tv_sec - lastRender.tv_sec);
	elapsed += (now.tv_nsec - lastRender.tv_nsec) / 1000000000.0;

	if(elapsed > (1/(double)uTargetFPS))
		return;

	left = (1/(double)uTargetFPS) - elapsed - (((double)lastRenderTime) / CLOCKS_PER_SEC);

	std::this_thread::sleep_for(std::chrono::milliseconds(int (left * 1000)));
}

void cRender::forceScreenSize(sPos _size)
{
	if(!(_size.x < 1 || _size.y < 1))
	{
		setBufferSize(_size);
		bLockScreenSize = true;
	}
	else
	{
		bLockScreenSize = false;
	}
}

void cRender::mute(bool _mute)
{
	bMute = _mute;
}

void cRender::enableDebugInfo(bool _enable)
{
	bPrintDebugInfo = _enable;
}