aboutsummaryrefslogtreecommitdiff
path: root/AmpelJonas
diff options
context:
space:
mode:
authorGravatar Jonas Gunz <himself@jonasgunz.de> 2018-12-10 20:53:50 +0100
committerGravatar Jonas Gunz <himself@jonasgunz.de> 2018-12-10 20:53:50 +0100
commit9fae9dfbc12525cdba4451d92f93c66bb6077b35 (patch)
treedb5d05f0146d836bb7d6e793d12b01defbba1b7c /AmpelJonas
parent4e44cec4a2a29cfee132bbe1ee722fbc5168db87 (diff)
downloadtermgl-9fae9dfbc12525cdba4451d92f93c66bb6077b35.tar.gz
Fixes, automatic resize(linux)
Diffstat (limited to 'AmpelJonas')
-rw-r--r--AmpelJonas/cObject.h7
-rw-r--r--AmpelJonas/cRender.cpp69
-rw-r--r--AmpelJonas/cRender.h8
-rw-r--r--AmpelJonas/main.cpp28
4 files changed, 94 insertions, 18 deletions
diff --git a/AmpelJonas/cObject.h b/AmpelJonas/cObject.h
index d268a0a..c3114c3 100644
--- a/AmpelJonas/cObject.h
+++ b/AmpelJonas/cObject.h
@@ -1,4 +1,6 @@
#pragma once
+#include <stdlib.h>
+
#include "cRender.h"
struct sObject
@@ -17,15 +19,18 @@ 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/cRender.cpp b/AmpelJonas/cRender.cpp
index 61d4843..296d7a2 100644
--- a/AmpelJonas/cRender.cpp
+++ b/AmpelJonas/cRender.cpp
@@ -5,7 +5,7 @@ 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 = sizeY = 0;
#ifdef __linux__ //In Linux, setting Console size is not supported, so it gets Size of Console (Window) instead.
@@ -13,12 +13,15 @@ cRender::cRender(char _backound, WORD _color, int _sx, int _sy)
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
- sizeX = w.ws_col;
- sizeY = w.ws_row - 1;
+
if(sizeX < _sx || sizeY < _sy) //Notify Program tha screen is too small for desired Size
iLastError = _ERR_SCREEN_TOO_SMALL_;
+ wDefColor = _color;
+
+ setBufferSize( getConsoleWindowSize() );
+
#elif _WIN32 //Windows Specific Code
hstdout = GetStdHandle(STD_OUTPUT_HANDLE); //get handle
@@ -27,26 +30,14 @@ cRender::cRender(char _backound, WORD _color, int _sx, int _sy)
SetConsoleWindowSize(_sx + 1, _sy + 1); //set the windows size to _sx * _sy (+1 so no scrolling accurs)
- sizeX = _sx;
- sizeY = _sy;
+ setBufferSize({_sx,_sy});
#endif
-
-
cBackound = _backound;
wBackColor = _color;
- //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);
-
clear(); //Init backround array
-}//render() WINDOWS
+}//render()
cRender::cRender() {}
@@ -139,6 +130,8 @@ int cRender::drawRectangle(char _border, char _fill, sPos _pos1, sPos _pos2, WOR
int cRender::render(void)
{
+ setBufferSize(getConsoleWindowSize());
+
gotoxy(0,0);
if (bBlockRender)
@@ -230,4 +223,46 @@ void cRender::gotoxy( int x, int y )
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(cScreen);
+ free(wColor);
+ }
+
+ 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);
+}
+
+sPos cRender::getSize()
+{
+ return {sizeX, sizeY};
+}
diff --git a/AmpelJonas/cRender.h b/AmpelJonas/cRender.h
index 848619d..30077e9 100644
--- a/AmpelJonas/cRender.h
+++ b/AmpelJonas/cRender.h
@@ -1,5 +1,6 @@
#pragma once
+#include <stdlib.h>
#include <string>
#include <math.h>
#include <iostream>
@@ -96,6 +97,9 @@ public:
int getLastError();
//Returns last Error that was not returnable
+ sPos getSize();
+ //Returns actual Size of screen
+
protected:
cRender(); //Empty Constructor for being inheritable
@@ -124,4 +128,8 @@ private:
//Slightly adapted from: http://www.cplusplus.com/forum/windows/121444/
#endif
void gotoxy( int x, int y );
+ void setBufferSize(sPos _size);
+#ifdef __linux__
+ sPos getConsoleWindowSize();
+#endif
};
diff --git a/AmpelJonas/main.cpp b/AmpelJonas/main.cpp
index adf9113..343a688 100644
--- a/AmpelJonas/main.cpp
+++ b/AmpelJonas/main.cpp
@@ -1,8 +1,36 @@
+#include <unistd.h>
+
#include "cRender.h"
#include "cObject.h"
#include "cObjectHandler.h"
int main()
{
+ cRender a(' ', _COL_DEFAULT, 10,10);
+ cObjectHandler b(&a);
+ cObject x(1,1);
+
+ int i = b.createObject(&x);
+ int dir1 = 1;
+ int dir2 = -1;
+ int cntr = 0;
+
+ 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.render();
+ //usleep(10*1000);
+
+ if(x.getPosition().x <= 0 || x.getPosition().x >= a.getSize().x - 1)
+ dir1 *= -1;
+ if(x.getPosition().y <= 0 || x.getPosition().y >= a.getSize().y)
+ dir2 *= -1;
+ }
+
+ a.render();
return 0;
}