From 7015245de4af673af3e6fa62d5d0f9c2e0c2a8a1 Mon Sep 17 00:00:00 2001 From: jonas Date: Thu, 27 Dec 2018 00:26:34 +0100 Subject: cleaned working tree --- .gitignore | 2 +- AmpelJonas/Makefile | 17 -- AmpelJonas/cInput.cpp | 74 --------- AmpelJonas/cInput.h | 48 ------ AmpelJonas/cObject.cpp | 59 ------- AmpelJonas/cObject.h | 36 ----- AmpelJonas/cObjectHandler.cpp | 63 -------- AmpelJonas/cObjectHandler.h | 37 ----- AmpelJonas/cRender.cpp | 351 ------------------------------------------ AmpelJonas/cRender.h | 148 ------------------ AmpelJonas/main.cpp | 136 ---------------- Makefile | 17 ++ cInput.cpp | 74 +++++++++ cInput.h | 48 ++++++ cObject.cpp | 59 +++++++ cObject.h | 36 +++++ cObjectHandler.cpp | 63 ++++++++ cObjectHandler.h | 37 +++++ cRender.cpp | 351 ++++++++++++++++++++++++++++++++++++++++++ cRender.h | 148 ++++++++++++++++++ main.cpp | 136 ++++++++++++++++ 21 files changed, 970 insertions(+), 970 deletions(-) delete mode 100644 AmpelJonas/Makefile delete mode 100644 AmpelJonas/cInput.cpp delete mode 100644 AmpelJonas/cInput.h delete mode 100644 AmpelJonas/cObject.cpp delete mode 100644 AmpelJonas/cObject.h delete mode 100644 AmpelJonas/cObjectHandler.cpp delete mode 100644 AmpelJonas/cObjectHandler.h delete mode 100644 AmpelJonas/cRender.cpp delete mode 100644 AmpelJonas/cRender.h delete mode 100644 AmpelJonas/main.cpp create mode 100644 Makefile create mode 100644 cInput.cpp create mode 100644 cInput.h create mode 100644 cObject.cpp create mode 100644 cObject.h create mode 100644 cObjectHandler.cpp create mode 100644 cObjectHandler.h create mode 100644 cRender.cpp create mode 100644 cRender.h create mode 100644 main.cpp diff --git a/.gitignore b/.gitignore index 14c9f09..9c1e36a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ *.o -Engine +bin/* diff --git a/AmpelJonas/Makefile b/AmpelJonas/Makefile deleted file mode 100644 index ab2707a..0000000 --- a/AmpelJonas/Makefile +++ /dev/null @@ -1,17 +0,0 @@ -CC = /usr/bin/g++ -CFLAGS = -Wall -g -LDFLAGS = -lm -lcurses -OUTPUT = Engine - -OBJ = main.o cObject.o cObjectHandler.o cRender.o cInput.o - -prog: $(OBJ) - $(CC) $(CFLAGS) -o $(OUTPUT) $(OBJ) $(LDFLAGS) - -%.o: %.cpp - $(CC) $(CFLAGS) -c $< - -.PHONY: clean - -clean: - rm -f $(OUTPUT) *.o diff --git a/AmpelJonas/cInput.cpp b/AmpelJonas/cInput.cpp deleted file mode 100644 index e0aa233..0000000 --- a/AmpelJonas/cInput.cpp +++ /dev/null @@ -1,74 +0,0 @@ -#include "cInput.h" - -cInput::cInput() -{ - // Save original serial communication configuration for stdin - tcgetattr( STDIN_FILENO, &original); - - // Put stdin in raw mode so keys get through directly without - // requiring pressing enter. - cfmakeraw (&raw); - tcsetattr (STDIN_FILENO, TCSANOW, &raw); - - // Switch to the alternate buffer screen - write (STDOUT_FILENO, "\e[?47h", 6); - - // Enable mouse tracking - write (STDOUT_FILENO, "\e[?9h", 5); -} - -cInput::~cInput() -{ - //revert changes to console - write (STDOUT_FILENO, "\e[?9l", 5); - write (STDOUT_FILENO, "\e[?47l", 6); - tcsetattr (STDIN_FILENO, TCSANOW, &original); -} - -sInputEvent cInput::poll() -{ - sInputEvent ret; - unsigned char buff [6]; - - //setup for select - fd_set rfds; - struct timeval tv; - FD_ZERO(&rfds); - FD_SET(STDIN_FILENO, &rfds); - tv.tv_sec = 0; - tv.tv_usec = 0; - - ret.type = _EVENT_NULL; - - //Check for Input. return of none - if(!select(1, &rfds, NULL, NULL, &tv)) - return ret; - - read (STDIN_FILENO, &buff, 1); - - if (buff[0] == '\x1B') //Escape sequence - { - read (STDIN_FILENO, &buff, 5); - if(buff[0] == '[') - { - if(buff[1] == 'M') //Mouse Event - { - ret.b = buff[2] - 32; - ret.x = buff[3] - 32; - ret.y = buff[4] - 32; - ret.type = _EVENT_MOUSE; - } - else //e.g. Arrow Keys - { - ret.c = buff[1]; - ret.type = _EVENT_KEY; - } - } - } - else - { - ret.type = _EVENT_CHAR; - ret.c = buff[0]; - } - return ret; -} diff --git a/AmpelJonas/cInput.h b/AmpelJonas/cInput.h deleted file mode 100644 index 8e3481c..0000000 --- a/AmpelJonas/cInput.h +++ /dev/null @@ -1,48 +0,0 @@ -/* -* cInput is responsible for Handling everything related to setting up and managing the console window -* as well as decoding keyboard and mouse events. -* Compatible with xterm compatible terminal emulators -*/ - -#ifndef CINPUT_H_ -#define CINPUT_H_ - -#include -#include -#include -#include - -#ifdef __linux__ -#elif _WIN32 - #error "Platforn not supported" -#else - #error "Platforn not supported" -#endif - -#define _EVENT_NULL 0 -#define _EVENT_CHAR 1 -#define _EVENT_KEY 2 -#define _EVENT_MOUSE 3 - -struct sInputEvent -{ - unsigned int type; - unsigned char c; - unsigned int b; - unsigned int x, y; -}; - -class cInput -{ -public: - cInput(); - - ~cInput(); - - sInputEvent poll(); - -private: - struct termios original, raw; -}; - -#endif /* end of include guard: */ diff --git a/AmpelJonas/cObject.cpp b/AmpelJonas/cObject.cpp deleted file mode 100644 index 8b18e41..0000000 --- a/AmpelJonas/cObject.cpp +++ /dev/null @@ -1,59 +0,0 @@ -#include "cObject.h" - -cObject::cObject(int _sx, int _sy) -{ - bBlockRender = true; //Block inherited render capabilities of parent - - sizeX = _sx; - sizeY = _sy; - - //Initialize 2D array - cScreen = (char**) malloc(sizeof *cScreen * _sx); - for (int i = 0; i < _sx; i++) - cScreen[i] = (char*)malloc(sizeof *cScreen[i] * _sy); - - wColor = (WORD**)malloc(sizeof *wColor * _sx); - for (int i = 0; i < _sx; i++) - wColor[i] = (WORD*)malloc(sizeof *wColor[i] * _sy); - - for (int i = 0; i < sizeY; i++) { - for (int o = 0; o < sizeX; o++) { - cScreen[o][i] = NULL; - wColor[o][i] = _COL_DEFAULT; - } - } -} - -cObject::~cObject() -{ - for (int i = 0; i < sizeX; i++) { - free(cScreen[i]); - free(wColor[i]); - } - - free(cScreen); - free(wColor); -} - -sPos cObject::getPosition() -{ - return pos; -} - -void cObject::setPosition(sPos _pos) -{ - pos = _pos; -} - - -void cObject::setPosition(int _x, int _y) -{ - pos.x = _x; - pos.y = _y; -} - - -sObject cObject::getObject() -{ - return sObject{pos, wColor, cScreen, sizeX, sizeY}; -} diff --git a/AmpelJonas/cObject.h b/AmpelJonas/cObject.h deleted file mode 100644 index c3114c3..0000000 --- a/AmpelJonas/cObject.h +++ /dev/null @@ -1,36 +0,0 @@ -#pragma once -#include - -#include "cRender.h" - -struct sObject - -{ - sPos pos; - WORD **wColor; - char **cScreen; - int sizeX; - int sizeY; -}; - -class cObject : public cRender -{ -public: - cObject(int _sx, int _sy); - //_sx : SizeX - //_sy : SizeY - - ~cObject(); - - sPos getPosition(); - - void setPosition(sPos _pos); - - void setPosition(int _x, int _y); - - sObject getObject(); - -private: - //wColor, cScreen, sizeX and sizeY are inherited from cRender - sPos pos; -}; diff --git a/AmpelJonas/cObjectHandler.cpp b/AmpelJonas/cObjectHandler.cpp deleted file mode 100644 index d35653a..0000000 --- a/AmpelJonas/cObjectHandler.cpp +++ /dev/null @@ -1,63 +0,0 @@ -#include "cObjectHandler.h" - -cObjectHandler::cObjectHandler(cRender *_render) -{ - render = _render; -} - -int cObjectHandler::createObject(cObject *_object) -{ - objects.push_back(_object); - 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_ABSOULUTE) - objects[_object]->setPosition(_pos); - - return 0; -} - -int cObjectHandler::destroyObject(int _object) -{ - delete objects[_object]; - objects[_object] = NULL; - - 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; -} diff --git a/AmpelJonas/cObjectHandler.h b/AmpelJonas/cObjectHandler.h deleted file mode 100644 index 1749c9b..0000000 --- a/AmpelJonas/cObjectHandler.h +++ /dev/null @@ -1,37 +0,0 @@ -#pragma once - -#include - -#include "cObject.h" - -//movemodes -#define _MOVE_RELATIVE 0 -#define _MOVE_ABSOULUTE 1 - -using namespace std; - -class cObject; //Circular dependency break (Bad practice. I Know.) - -class cObjectHandler -{ -public: - cObjectHandler(cRender *_render); - - int createObject(cObject *_object); - //Adds _object to managed objects vector - //returns Identifier for newly created vector - - int moveObject(int _object, sPos _pos, int _mode); - //Alters position of _object by _pos either relative to old position or Absolute - //Depending on selected _mode (_MOVE_RELATIVE / _MOVE_ABSOLUTE). - - int destroyObject(int _object); - //removes _object from vector after deleting it - - int write(); - //writes all objects in objects[] to render buffer - -private: - vector objects; - cRender *render; -}; diff --git a/AmpelJonas/cRender.cpp b/AmpelJonas/cRender.cpp deleted file mode 100644 index 6d7a70a..0000000 --- a/AmpelJonas/cRender.cpp +++ /dev/null @@ -1,351 +0,0 @@ -#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_; - sizeX = 0; - sizeY = 0; - - cBackound = _backound; - wBackColor = _color; - -#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); - - wDefColor = _COL_DEFAULT; - - setBufferSize( getConsoleWindowSize() ); - - if(sizeX < _sx || sizeY < _sy) //Notify Program tha screen is too small for desired Size - iLastError = _ERR_SCREEN_TOO_SMALL_; - - 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 - - setConsoleEcho(false); - clear(true); //Init backround array - //forceReRender(); -}//render() - - -cRender::cRender() {} - -cRender::~cRender() -{ - //Free allocated memory - for (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); - #endif -} - -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; - - 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, 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) -{ - if (bBlockRender) - return _ERR_RENDER_BLOCKED_BY_CHILD_; - - setBufferSize(getConsoleWindowSize()); - - for (int i = 0; i < sizeY; i++) { - for (int o = 0; o < sizeX; o++) { - gotoxy(o,i); //Moving this out of if fixed Render problem... Not optimal, though better for performance - if(bChanged[o][i]) - { - #ifdef _WIN32 - - SetConsoleTextAttribute(hstdout, wColor[o][i] | _COL_INTENSITY); - //cout << cScreen[o][i]; - printf("%c", cScreen[o][i]); - - #elif __linux__ - //cout << "\033["<< wColor[o][i] <<"m"<< cScreen[o][i]; - printf("\033[%im%c", wColor[o][i], cScreen[o][i]); - #endif - } - bChanged[o][i] = false; - } - - } - - gotoxy(sizeX - 1, sizeY - 1); - - return 0; -} - -int cRender::clear(bool _forceReRender) -{ - for (int i = 0; i < sizeY; i++) { - for (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); -} - - -#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 ) ); -} - -sPos cRender::getConsoleWindowSize() -{ - struct winsize w; - ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); - - return {w.ws_col, w.ws_row - 1}; -} -#endif - -void cRender::setBufferSize(sPos _size) -{ - if(_size.x == sizeX && _size.y == sizeY) - return; - - if(sizeX!=0 && sizeY!=0) //resize. delete first - { - for (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 (int i = 0; i < sizeX; i++) - cScreen[i] = (char*)malloc(sizeof *cScreen[i] * sizeY); - - wColor = (WORD**)malloc(sizeof *wColor * sizeX); - for (int i = 0; i < sizeX; i++) - wColor[i] = (WORD*)malloc(sizeof *wColor[i] * sizeY); - - bChanged = (bool**)malloc(sizeof *bChanged * sizeX); - for (int i = 0; i < sizeX; i++) - bChanged[i] = (bool*)malloc(sizeof *bChanged[i] * sizeY); - - clear(true); -} - -sPos cRender::getSize() -{ - return {sizeX, sizeY}; -} - -void cRender::forceReRender() -{ - for (int i = 0; i < sizeY; i++) { - for (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 ); - -#else - struct termios tty; - tcgetattr(STDIN_FILENO, &tty); - if( !_enable ) - tty.c_lflag &= ~ECHO; - else - tty.c_lflag |= ECHO; - - (void) tcsetattr(STDIN_FILENO, TCSANOW, &tty); -#endif -} - -void cRender::setConsoleCursor(bool _enable) -{ - _enable ? write (STDOUT_FILENO, "\e[?25h", 6) : write (STDOUT_FILENO, "\e[?25l", 6); -} diff --git a/AmpelJonas/cRender.h b/AmpelJonas/cRender.h deleted file mode 100644 index c30a901..0000000 --- a/AmpelJonas/cRender.h +++ /dev/null @@ -1,148 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -#include - -#ifdef __linux__ - #include - #include - #include - - typedef int 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 -#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 -{ - int x; - int y; -}; - -class cRender -{ -public: - cRender(char _backound, WORD _color, int _sx, int _sy); - //Constructor - //sets cBackround[][] to _backround & wColor[][] to _color - //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 - - int drawPoint(char _c, sPos _pos, bool _overrideCollision, WORD _color); - //Draws _c @ _pos - //Returns _COLLOSION_ if _pos is already set to !cBackround && _overrideCollision isnt set - - int drawLine(char _c, sPos _pos1, sPos _pos2, bool _overrideCollision, WORD _color); - //x Value of pos1 MUSTNT exceed the x value of pos2! - - int drawText(string _s, sPos _pos, WORD _color); - //Draws Text _s @ _pos - //First char is @ _pos - - int drawRectangle(char _border, char _fill, sPos _pos1, sPos _pos2, WORD _borderColor, WORD _fillColor); - //x Value of pos1 MUSTNT exceed the x value of pos2! - - int render(void); - //Prints cScreen - - int clear(); - int clear(bool _forceReRender); - //clears cScreen + wColor - // for _forceReRender == true, the bChanged[][] is set to true to force Re-Render of whole Screen - // clear(void) calls clear(_forceReRender = false) - - int getLastError(); - //Returns last Error that was not returnable - - sPos getSize(); - //Returns actual Size of screen - - -protected: - cRender(); //Empty Constructor for being inheritable - - void setBufferSize(sPos _size); - - bool bBlockRender; //Used by children to block render function - - char **cScreen; //Pixel Map - WORD **wColor; //Color Map - bool **bChanged; //Pixel Change 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/ -#endif - void gotoxy( int x, int y ); - - void forceReRender(); - - void setConsoleEcho(bool _enable); -#ifdef __linux__ - sPos getConsoleWindowSize(); - - void setConsoleCursor(bool _enable); -#endif -}; diff --git a/AmpelJonas/main.cpp b/AmpelJonas/main.cpp deleted file mode 100644 index f0f6f50..0000000 --- a/AmpelJonas/main.cpp +++ /dev/null @@ -1,136 +0,0 @@ -#include - -#include "cRender.h" -#include "cObject.h" -#include "cObjectHandler.h" -#include "cInput.h" - -int main() -{ - cRender a(' ', _COL_DEFAULT, 20,20); - cInput in; - a.render(); - - sPos size = a.getSize(); - sPos pos = {size.x / 2, size.y / 2}; - - while(1) - { - sInputEvent ie = in.poll(); - if(ie.type != _EVENT_NULL) - { - if(ie.type == _EVENT_KEY) - { - switch (ie.c) - { - case 'A': - pos.y--; - break; - case 'B': - pos.y++; - break; - case 'C': - pos.x++; - break; - case 'D': - pos.x--; - break; - }; - } - else if (ie.type == _EVENT_MOUSE) - { - pos.x = ie.x; - pos.y = ie.y; - } - - a.clear(); - a.drawPoint('X', pos, true, _COL_GREEN); - a.render(); - } - } - - /*unsigned long int framecounter = 0; - cRender a(' ', _COL_DEFAULT, 10,10); - a.render(); - - sPos pos = {0,10}; - int dirX = 1; - int dirY = -1; - - while(1) - { - pos.x += 2 * dirX; - pos.y += 1 * dirY; - - if(pos.x >= a.getSize().x) { - pos.x = a.getSize().x; - dirX *= -1; - } - if(pos.x <= 0) { - pos.x = 0; - dirX *= -1; - } - - if(pos.y >= a.getSize().y) { - pos.y = a.getSize().y; - dirY *= -1; - } - if(pos.y <= 0) { - pos.y = 0; - dirY *= -1; - } - - a.clear(); - a.drawPoint('X', pos, true, _COL_GREEN); - a.drawText(to_string(framecounter), {0,0}, _COL_RED); - a.render(); - - framecounter++; - //cin.get(); - //usleep(100*1000); - if(framecounter >= 1000) - break; - }*/ - - /*cObjectHandler b(&a); - cObject x(1,1); - - int i = b.createObject(&x); - int dir1 = 1; - int dir2 = -1; - int cntr = 0; - - a.clear(true); - b.moveObject(i, {0,30}, _MOVE_ABSOULUTE); - x.drawPoint('X', {0,0}, true,_COL_GREEN); - - while(1) - { - b.moveObject(i, {2 * dir1, 1 * dir2}, _MOVE_RELATIVE); - - b.write(); - a.drawText(to_string(framecounter), {0,0}, _COL_RED); - a.render(); - - if(x.getPosition().x <= 0) - dir1 *= -1; - else if(x.getPosition().x >= a.getSize().x) - { - dir1 *= -1; - x.setPosition({a.getSize().x, x.getPosition().y}); - } - - if(x.getPosition().y <= 0) - dir2 *= -1; - else if(x.getPosition().y >= a.getSize().y) - { - dir2 *= -1; - x.setPosition({x.getPosition().x, a.getSize().y}); - } - - framecounter++; - //cin.get(); - usleep(100*1000); - }*/ - return 0; -} diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..132f2ec --- /dev/null +++ b/Makefile @@ -0,0 +1,17 @@ +CC = /usr/bin/g++ +CFLAGS = -Wall -g +LDFLAGS = -lm -lcurses +OUTPUT = bin/Engine + +OBJ = main.o cObject.o cObjectHandler.o cRender.o cInput.o + +prog: $(OBJ) + $(CC) $(CFLAGS) -o $(OUTPUT) $(OBJ) $(LDFLAGS) + +%.o: %.cpp + $(CC) $(CFLAGS) -c $< + +.PHONY: clean + +clean: + rm -f $(OUTPUT) *.o diff --git a/cInput.cpp b/cInput.cpp new file mode 100644 index 0000000..e0aa233 --- /dev/null +++ b/cInput.cpp @@ -0,0 +1,74 @@ +#include "cInput.h" + +cInput::cInput() +{ + // Save original serial communication configuration for stdin + tcgetattr( STDIN_FILENO, &original); + + // Put stdin in raw mode so keys get through directly without + // requiring pressing enter. + cfmakeraw (&raw); + tcsetattr (STDIN_FILENO, TCSANOW, &raw); + + // Switch to the alternate buffer screen + write (STDOUT_FILENO, "\e[?47h", 6); + + // Enable mouse tracking + write (STDOUT_FILENO, "\e[?9h", 5); +} + +cInput::~cInput() +{ + //revert changes to console + write (STDOUT_FILENO, "\e[?9l", 5); + write (STDOUT_FILENO, "\e[?47l", 6); + tcsetattr (STDIN_FILENO, TCSANOW, &original); +} + +sInputEvent cInput::poll() +{ + sInputEvent ret; + unsigned char buff [6]; + + //setup for select + fd_set rfds; + struct timeval tv; + FD_ZERO(&rfds); + FD_SET(STDIN_FILENO, &rfds); + tv.tv_sec = 0; + tv.tv_usec = 0; + + ret.type = _EVENT_NULL; + + //Check for Input. return of none + if(!select(1, &rfds, NULL, NULL, &tv)) + return ret; + + read (STDIN_FILENO, &buff, 1); + + if (buff[0] == '\x1B') //Escape sequence + { + read (STDIN_FILENO, &buff, 5); + if(buff[0] == '[') + { + if(buff[1] == 'M') //Mouse Event + { + ret.b = buff[2] - 32; + ret.x = buff[3] - 32; + ret.y = buff[4] - 32; + ret.type = _EVENT_MOUSE; + } + else //e.g. Arrow Keys + { + ret.c = buff[1]; + ret.type = _EVENT_KEY; + } + } + } + else + { + ret.type = _EVENT_CHAR; + ret.c = buff[0]; + } + return ret; +} diff --git a/cInput.h b/cInput.h new file mode 100644 index 0000000..8e3481c --- /dev/null +++ b/cInput.h @@ -0,0 +1,48 @@ +/* +* cInput is responsible for Handling everything related to setting up and managing the console window +* as well as decoding keyboard and mouse events. +* Compatible with xterm compatible terminal emulators +*/ + +#ifndef CINPUT_H_ +#define CINPUT_H_ + +#include +#include +#include +#include + +#ifdef __linux__ +#elif _WIN32 + #error "Platforn not supported" +#else + #error "Platforn not supported" +#endif + +#define _EVENT_NULL 0 +#define _EVENT_CHAR 1 +#define _EVENT_KEY 2 +#define _EVENT_MOUSE 3 + +struct sInputEvent +{ + unsigned int type; + unsigned char c; + unsigned int b; + unsigned int x, y; +}; + +class cInput +{ +public: + cInput(); + + ~cInput(); + + sInputEvent poll(); + +private: + struct termios original, raw; +}; + +#endif /* end of include guard: */ diff --git a/cObject.cpp b/cObject.cpp new file mode 100644 index 0000000..8b18e41 --- /dev/null +++ b/cObject.cpp @@ -0,0 +1,59 @@ +#include "cObject.h" + +cObject::cObject(int _sx, int _sy) +{ + bBlockRender = true; //Block inherited render capabilities of parent + + sizeX = _sx; + sizeY = _sy; + + //Initialize 2D array + cScreen = (char**) malloc(sizeof *cScreen * _sx); + for (int i = 0; i < _sx; i++) + cScreen[i] = (char*)malloc(sizeof *cScreen[i] * _sy); + + wColor = (WORD**)malloc(sizeof *wColor * _sx); + for (int i = 0; i < _sx; i++) + wColor[i] = (WORD*)malloc(sizeof *wColor[i] * _sy); + + for (int i = 0; i < sizeY; i++) { + for (int o = 0; o < sizeX; o++) { + cScreen[o][i] = NULL; + wColor[o][i] = _COL_DEFAULT; + } + } +} + +cObject::~cObject() +{ + for (int i = 0; i < sizeX; i++) { + free(cScreen[i]); + free(wColor[i]); + } + + free(cScreen); + free(wColor); +} + +sPos cObject::getPosition() +{ + return pos; +} + +void cObject::setPosition(sPos _pos) +{ + pos = _pos; +} + + +void cObject::setPosition(int _x, int _y) +{ + pos.x = _x; + pos.y = _y; +} + + +sObject cObject::getObject() +{ + return sObject{pos, wColor, cScreen, sizeX, sizeY}; +} diff --git a/cObject.h b/cObject.h new file mode 100644 index 0000000..c3114c3 --- /dev/null +++ b/cObject.h @@ -0,0 +1,36 @@ +#pragma once +#include + +#include "cRender.h" + +struct sObject + +{ + sPos pos; + WORD **wColor; + char **cScreen; + int sizeX; + int sizeY; +}; + +class cObject : public cRender +{ +public: + cObject(int _sx, int _sy); + //_sx : SizeX + //_sy : SizeY + + ~cObject(); + + sPos getPosition(); + + void setPosition(sPos _pos); + + void setPosition(int _x, int _y); + + sObject getObject(); + +private: + //wColor, cScreen, sizeX and sizeY are inherited from cRender + sPos pos; +}; diff --git a/cObjectHandler.cpp b/cObjectHandler.cpp new file mode 100644 index 0000000..d35653a --- /dev/null +++ b/cObjectHandler.cpp @@ -0,0 +1,63 @@ +#include "cObjectHandler.h" + +cObjectHandler::cObjectHandler(cRender *_render) +{ + render = _render; +} + +int cObjectHandler::createObject(cObject *_object) +{ + objects.push_back(_object); + 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_ABSOULUTE) + objects[_object]->setPosition(_pos); + + return 0; +} + +int cObjectHandler::destroyObject(int _object) +{ + delete objects[_object]; + objects[_object] = NULL; + + 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; +} diff --git a/cObjectHandler.h b/cObjectHandler.h new file mode 100644 index 0000000..1749c9b --- /dev/null +++ b/cObjectHandler.h @@ -0,0 +1,37 @@ +#pragma once + +#include + +#include "cObject.h" + +//movemodes +#define _MOVE_RELATIVE 0 +#define _MOVE_ABSOULUTE 1 + +using namespace std; + +class cObject; //Circular dependency break (Bad practice. I Know.) + +class cObjectHandler +{ +public: + cObjectHandler(cRender *_render); + + int createObject(cObject *_object); + //Adds _object to managed objects vector + //returns Identifier for newly created vector + + int moveObject(int _object, sPos _pos, int _mode); + //Alters position of _object by _pos either relative to old position or Absolute + //Depending on selected _mode (_MOVE_RELATIVE / _MOVE_ABSOLUTE). + + int destroyObject(int _object); + //removes _object from vector after deleting it + + int write(); + //writes all objects in objects[] to render buffer + +private: + vector objects; + cRender *render; +}; diff --git a/cRender.cpp b/cRender.cpp new file mode 100644 index 0000000..6d7a70a --- /dev/null +++ b/cRender.cpp @@ -0,0 +1,351 @@ +#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_; + sizeX = 0; + sizeY = 0; + + cBackound = _backound; + wBackColor = _color; + +#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); + + wDefColor = _COL_DEFAULT; + + setBufferSize( getConsoleWindowSize() ); + + if(sizeX < _sx || sizeY < _sy) //Notify Program tha screen is too small for desired Size + iLastError = _ERR_SCREEN_TOO_SMALL_; + + 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 + + setConsoleEcho(false); + clear(true); //Init backround array + //forceReRender(); +}//render() + + +cRender::cRender() {} + +cRender::~cRender() +{ + //Free allocated memory + for (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); + #endif +} + +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; + + 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, 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) +{ + if (bBlockRender) + return _ERR_RENDER_BLOCKED_BY_CHILD_; + + setBufferSize(getConsoleWindowSize()); + + for (int i = 0; i < sizeY; i++) { + for (int o = 0; o < sizeX; o++) { + gotoxy(o,i); //Moving this out of if fixed Render problem... Not optimal, though better for performance + if(bChanged[o][i]) + { + #ifdef _WIN32 + + SetConsoleTextAttribute(hstdout, wColor[o][i] | _COL_INTENSITY); + //cout << cScreen[o][i]; + printf("%c", cScreen[o][i]); + + #elif __linux__ + //cout << "\033["<< wColor[o][i] <<"m"<< cScreen[o][i]; + printf("\033[%im%c", wColor[o][i], cScreen[o][i]); + #endif + } + bChanged[o][i] = false; + } + + } + + gotoxy(sizeX - 1, sizeY - 1); + + return 0; +} + +int cRender::clear(bool _forceReRender) +{ + for (int i = 0; i < sizeY; i++) { + for (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); +} + + +#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 ) ); +} + +sPos cRender::getConsoleWindowSize() +{ + struct winsize w; + ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); + + return {w.ws_col, w.ws_row - 1}; +} +#endif + +void cRender::setBufferSize(sPos _size) +{ + if(_size.x == sizeX && _size.y == sizeY) + return; + + if(sizeX!=0 && sizeY!=0) //resize. delete first + { + for (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 (int i = 0; i < sizeX; i++) + cScreen[i] = (char*)malloc(sizeof *cScreen[i] * sizeY); + + wColor = (WORD**)malloc(sizeof *wColor * sizeX); + for (int i = 0; i < sizeX; i++) + wColor[i] = (WORD*)malloc(sizeof *wColor[i] * sizeY); + + bChanged = (bool**)malloc(sizeof *bChanged * sizeX); + for (int i = 0; i < sizeX; i++) + bChanged[i] = (bool*)malloc(sizeof *bChanged[i] * sizeY); + + clear(true); +} + +sPos cRender::getSize() +{ + return {sizeX, sizeY}; +} + +void cRender::forceReRender() +{ + for (int i = 0; i < sizeY; i++) { + for (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 ); + +#else + struct termios tty; + tcgetattr(STDIN_FILENO, &tty); + if( !_enable ) + tty.c_lflag &= ~ECHO; + else + tty.c_lflag |= ECHO; + + (void) tcsetattr(STDIN_FILENO, TCSANOW, &tty); +#endif +} + +void cRender::setConsoleCursor(bool _enable) +{ + _enable ? write (STDOUT_FILENO, "\e[?25h", 6) : write (STDOUT_FILENO, "\e[?25l", 6); +} diff --git a/cRender.h b/cRender.h new file mode 100644 index 0000000..c30a901 --- /dev/null +++ b/cRender.h @@ -0,0 +1,148 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +#ifdef __linux__ + #include + #include + #include + + typedef int 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 +#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 +{ + int x; + int y; +}; + +class cRender +{ +public: + cRender(char _backound, WORD _color, int _sx, int _sy); + //Constructor + //sets cBackround[][] to _backround & wColor[][] to _color + //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 + + int drawPoint(char _c, sPos _pos, bool _overrideCollision, WORD _color); + //Draws _c @ _pos + //Returns _COLLOSION_ if _pos is already set to !cBackround && _overrideCollision isnt set + + int drawLine(char _c, sPos _pos1, sPos _pos2, bool _overrideCollision, WORD _color); + //x Value of pos1 MUSTNT exceed the x value of pos2! + + int drawText(string _s, sPos _pos, WORD _color); + //Draws Text _s @ _pos + //First char is @ _pos + + int drawRectangle(char _border, char _fill, sPos _pos1, sPos _pos2, WORD _borderColor, WORD _fillColor); + //x Value of pos1 MUSTNT exceed the x value of pos2! + + int render(void); + //Prints cScreen + + int clear(); + int clear(bool _forceReRender); + //clears cScreen + wColor + // for _forceReRender == true, the bChanged[][] is set to true to force Re-Render of whole Screen + // clear(void) calls clear(_forceReRender = false) + + int getLastError(); + //Returns last Error that was not returnable + + sPos getSize(); + //Returns actual Size of screen + + +protected: + cRender(); //Empty Constructor for being inheritable + + void setBufferSize(sPos _size); + + bool bBlockRender; //Used by children to block render function + + char **cScreen; //Pixel Map + WORD **wColor; //Color Map + bool **bChanged; //Pixel Change 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/ +#endif + void gotoxy( int x, int y ); + + void forceReRender(); + + void setConsoleEcho(bool _enable); +#ifdef __linux__ + sPos getConsoleWindowSize(); + + void setConsoleCursor(bool _enable); +#endif +}; diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..f0f6f50 --- /dev/null +++ b/main.cpp @@ -0,0 +1,136 @@ +#include + +#include "cRender.h" +#include "cObject.h" +#include "cObjectHandler.h" +#include "cInput.h" + +int main() +{ + cRender a(' ', _COL_DEFAULT, 20,20); + cInput in; + a.render(); + + sPos size = a.getSize(); + sPos pos = {size.x / 2, size.y / 2}; + + while(1) + { + sInputEvent ie = in.poll(); + if(ie.type != _EVENT_NULL) + { + if(ie.type == _EVENT_KEY) + { + switch (ie.c) + { + case 'A': + pos.y--; + break; + case 'B': + pos.y++; + break; + case 'C': + pos.x++; + break; + case 'D': + pos.x--; + break; + }; + } + else if (ie.type == _EVENT_MOUSE) + { + pos.x = ie.x; + pos.y = ie.y; + } + + a.clear(); + a.drawPoint('X', pos, true, _COL_GREEN); + a.render(); + } + } + + /*unsigned long int framecounter = 0; + cRender a(' ', _COL_DEFAULT, 10,10); + a.render(); + + sPos pos = {0,10}; + int dirX = 1; + int dirY = -1; + + while(1) + { + pos.x += 2 * dirX; + pos.y += 1 * dirY; + + if(pos.x >= a.getSize().x) { + pos.x = a.getSize().x; + dirX *= -1; + } + if(pos.x <= 0) { + pos.x = 0; + dirX *= -1; + } + + if(pos.y >= a.getSize().y) { + pos.y = a.getSize().y; + dirY *= -1; + } + if(pos.y <= 0) { + pos.y = 0; + dirY *= -1; + } + + a.clear(); + a.drawPoint('X', pos, true, _COL_GREEN); + a.drawText(to_string(framecounter), {0,0}, _COL_RED); + a.render(); + + framecounter++; + //cin.get(); + //usleep(100*1000); + if(framecounter >= 1000) + break; + }*/ + + /*cObjectHandler b(&a); + cObject x(1,1); + + int i = b.createObject(&x); + int dir1 = 1; + int dir2 = -1; + int cntr = 0; + + a.clear(true); + b.moveObject(i, {0,30}, _MOVE_ABSOULUTE); + x.drawPoint('X', {0,0}, true,_COL_GREEN); + + while(1) + { + b.moveObject(i, {2 * dir1, 1 * dir2}, _MOVE_RELATIVE); + + b.write(); + a.drawText(to_string(framecounter), {0,0}, _COL_RED); + a.render(); + + if(x.getPosition().x <= 0) + dir1 *= -1; + else if(x.getPosition().x >= a.getSize().x) + { + dir1 *= -1; + x.setPosition({a.getSize().x, x.getPosition().y}); + } + + if(x.getPosition().y <= 0) + dir2 *= -1; + else if(x.getPosition().y >= a.getSize().y) + { + dir2 *= -1; + x.setPosition({x.getPosition().x, a.getSize().y}); + } + + framecounter++; + //cin.get(); + usleep(100*1000); + }*/ + return 0; +} -- cgit v1.2.3