From 8c81604b5a22a72885ff5d6270a251172fa90319 Mon Sep 17 00:00:00 2001 From: jonas Date: Fri, 7 Dec 2018 12:16:48 +0100 Subject: Started port to linux NON-Working --- AmpelJonas/Makefile | 11 ++++++++ AmpelJonas/cRender.cpp | 72 +++++++++++++++++++++++++++++++++++++++++-------- AmpelJonas/cRender.h | 73 +++++++++++++++++++++++++++++++++++++++----------- AmpelJonas/stdafx.h | 12 ++++++--- 4 files changed, 137 insertions(+), 31 deletions(-) create mode 100644 AmpelJonas/Makefile 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 -#include #include #include +#ifdef __linux__ + #include + #include + #include + + typedef char WORD; +#elif _WIN32 + #include +#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 #include + +#ifdef _WIN32 #include +#endif + #include @@ -21,4 +25,4 @@ #include "cCar.h" #include "cPeasant.h" -using namespace std; \ No newline at end of file +using namespace std; -- cgit v1.2.3