aboutsummaryrefslogtreecommitdiff
path: root/AmpelJonas
diff options
context:
space:
mode:
authorGravatar jonas <himself@jonasgunz.de> 2018-12-07 12:16:48 +0100
committerGravatar jonas <himself@jonasgunz.de> 2018-12-07 12:16:48 +0100
commit8c81604b5a22a72885ff5d6270a251172fa90319 (patch)
tree18044b6f1fa4f57797157662a873f6cef43777d8 /AmpelJonas
parent2c12d19204aa198bf8537bcdb137b40f0c7317e9 (diff)
downloadtermgl-8c81604b5a22a72885ff5d6270a251172fa90319.tar.gz
Started port to linux
NON-Working
Diffstat (limited to 'AmpelJonas')
-rw-r--r--AmpelJonas/Makefile11
-rw-r--r--AmpelJonas/cRender.cpp72
-rw-r--r--AmpelJonas/cRender.h73
-rw-r--r--AmpelJonas/stdafx.h12
4 files changed, 137 insertions, 31 deletions
diff --git a/AmpelJonas/Makefile b/AmpelJonas/Makefile
new file mode 100644
index 0000000..a581b30
--- /dev/null
+++ b/AmpelJonas/Makefile
@@ -0,0 +1,11 @@
+CC = /usr/bin/g++
+CFLAGS = -Wall -g
+LDFLAGS = -lm -lpthread
+
+OBJ = main.o cCar.o cCrossroad.o cObject.o cObjectHandler.o cPeasant.o cPeasantTrafficLight.o cRender.o cTrafficLight.o
+
+prog: $(OBJ)
+ $(CC) $(CFLAGS) -o prog $(OBJ) $(LDFLAGS)
+
+%.o: %.cpp
+ $(CC) $(CFLAGS) -c $<
diff --git a/AmpelJonas/cRender.cpp b/AmpelJonas/cRender.cpp
index 7df1320..4f18df5 100644
--- a/AmpelJonas/cRender.cpp
+++ b/AmpelJonas/cRender.cpp
@@ -1,32 +1,53 @@
#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)
- cBackound = _backound;
- wBackColor = _color;
-
sizeX = _sx;
sizeY = _sy;
+#endif
+
+
+
+ cBackound = _backound;
+ wBackColor = _color;
//Initialize 2D array
- cScreen = (char**)malloc(sizeof *cScreen * _sx);
+ cScreen = (char**)malloc(sizeof *cScreen * sizeX);
for (int i = 0; i < _sx; i++)
- cScreen[i] = (char*)malloc(sizeof *cScreen[i] * _sy);
+ cScreen[i] = (char*)malloc(sizeof *cScreen[i] * sizeY);
- wColor = (WORD**)malloc(sizeof *wColor * _sx);
+ wColor = (WORD**)malloc(sizeof *wColor * sizeX);
for (int i = 0; i < _sx; i++)
- wColor[i] = (WORD*)malloc(sizeof *wColor[i] * _sy);
+ wColor[i] = (WORD*)malloc(sizeof *wColor[i] * sizeY);
clear(); //Init backround array
-}
+}//render() WINDOWS
+
cRender::cRender() {}
@@ -83,7 +104,7 @@ int cRender::drawLine(char _c, sPos _pos1, sPos _pos2, bool _overrideCollision,
drawPoint(_c, sPos{i + _pos1.x, (int)(i * fGradient + _pos1.y + 0.5)}, _overrideCollision, _color); //+0.5 for rounding error
}
}
-
+
return 0;
}
@@ -118,15 +139,19 @@ int cRender::drawRectangle(char _border, char _fill, sPos _pos1, sPos _pos2, WOR
int cRender::render(void)
{
- SetConsoleCursorPosition(hstdout, COORD{ 0,0 }); //Set cursor position to be able to alter the image without the effect of it shifting to the top
+ 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
}
@@ -144,6 +169,7 @@ int cRender::clear(void)
return 0;
}
+#ifdef _WIN32
//Source: http://www.cplusplus.com/forum/windows/121444/
int cRender::SetConsoleWindowSize(int x, int y)
{
@@ -180,4 +206,28 @@ int cRender::SetConsoleWindowSize(int x, int y)
SMALL_RECT info = { 0, 0, x - 1, y - 1 };
if (!SetConsoleWindowInfo(h, TRUE, &info))
return 1;
-} \ No newline at end of file
+}
+#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
diff --git a/AmpelJonas/cRender.h b/AmpelJonas/cRender.h
index dadae40..1ff3416 100644
--- a/AmpelJonas/cRender.h
+++ b/AmpelJonas/cRender.h
@@ -1,30 +1,58 @@
#pragma once
#include <string>
-#include <Windows.h>
#include <math.h>
#include <iostream>
+#ifdef __linux__
+ #include <unistd.h>
+ #include <term.h>
+ #include <sys/ioctl.h>
+
+ typedef char WORD;
+#elif _WIN32
+ #include <Windows.h>
+#else
+ #error "Platforn not supported"
+#endif
//errors
#define _OK_ 0
#define _ERR_ 1
#define _ERR_COORDINATES_INVALID_ 2
#define _ERR_RENDER_BLOCKED_BY_CHILD_ 3
+#define _ERR_SCREEN_TOO_SMALL_ 4
#define _COLLISION_ 255
//Colors
-#define _COL_BLACK 0x00
-#define _COL_BLUE 0x01
-#define _COL_GREEN 0x02
-#define _COL_YELLOW 0x0E
-#define _COL_RED 0x04
-#define _COL_WHITE 0x0F
-#define _COL_DARK_WHITE 0x07
-#define _COL_INTENSITY 0x08
-#define _COL_DEFAULT 0xFF
-
+#ifdef _WIN32
+ #define _COL_BLACK 0x00
+ #define _COL_BLUE 0x01
+ #define _COL_GREEN 0x02
+ #define _COL_YELLOW 0x0E
+ #define _COL_RED 0x04
+ #define _COL_WHITE 0x0F
+ #define _COL_DARK_WHITE 0x07
+ #define _COL_INTENSITY 0x08
+ #define _COL_DEFAULT 0xFF
+#elif __linux__
+ #define _COL_BLACK 30
+ #define _COL_BLUE 34
+ #define _COL_GREEN 32
+ #define _COL_YELLOW 33
+ #define _COL_RED 31
+ #define _COL_WHITE 37
+ #define _COL_DEFAULT 0
+
+ //Linux Specific
+ #define _COL_BOLD 1
+ #define _COL_BOLD_OFF 21
+ #define _COL_UNDERLINE 4
+ #define _COL_UNDERLINE_OFF 24
+ #define _COL_INVERSE 7
+ #define _COL_INVERSE_OFF 27
+#endif
using namespace std;
struct sPos
@@ -39,7 +67,8 @@ public:
cRender(char _backound, WORD _color, int _sx, int _sy);
//Constructor
//sets cBackround[][] to _backround & wColor[][] to _color
- //Resizes console window
+ //Resizes console window for Windows
+ //Sets Size to Console Window Size for Linux. Writes Error for _sx or _sy smaller than Screen. Get by getLastError()
~cRender();
//frees allocated memory
@@ -63,24 +92,36 @@ public:
int clear(void);
//clears cScreen + wColor
-
+
+ int getLastError();
+ //Returns last Error that was not returnable
+
protected:
cRender(); //Empty Constructor for being inheritable
-
+
bool bBlockRender; //Used by children to block render function
char **cScreen; //Pixel Map
- WORD **wColor; //Color Map
+ WORD **wColor; //Color Map
char cBackound; //Default backround
WORD wBackColor;
int sizeX, sizeY;
+#ifdef _WIN32
HANDLE hstdout;
CONSOLE_SCREEN_BUFFER_INFO csbi;
+#endif
+
WORD wDefColor; //Default Color
+
+ int iLastError;
+
private:
+#ifdef _WIN32
int SetConsoleWindowSize(int x, int y);
//Slightly adapted from: http://www.cplusplus.com/forum/windows/121444/
-}; \ No newline at end of file
+#endif
+ void gotoxy( int x, int y );
+};
diff --git a/AmpelJonas/stdafx.h b/AmpelJonas/stdafx.h
index 9c52c37..986bfde 100644
--- a/AmpelJonas/stdafx.h
+++ b/AmpelJonas/stdafx.h
@@ -1,6 +1,6 @@
-// stdafx.h: Includedatei für Standardsystem-Includedateien
-// oder häufig verwendete projektspezifische Includedateien,
-// die nur in unregelmäßigen Abständen geändert werden.
+// stdafx.h: Includedatei f�r Standardsystem-Includedateien
+// oder h�ufig verwendete projektspezifische Includedateien,
+// die nur in unregelm��igen Abst�nden ge�ndert werden.
//
#pragma once
@@ -8,7 +8,11 @@
#include <stdio.h>
#include <iostream>
+
+#ifdef _WIN32
#include <tchar.h>
+#endif
+
#include <vector>
@@ -21,4 +25,4 @@
#include "cCar.h"
#include "cPeasant.h"
-using namespace std; \ No newline at end of file
+using namespace std;