summaryrefslogtreecommitdiff
path: root/AmpelJonas
diff options
context:
space:
mode:
authorGravatar jonas <himself@jonasgunz.de> 2018-12-26 21:18:09 +0100
committerGravatar jonas <himself@jonasgunz.de> 2018-12-26 21:18:09 +0100
commit616951a353caf398908e6e6ce7dddf34a65513e5 (patch)
tree36a5ac46861b3f2c21777d383359f8463676e404 /AmpelJonas
parent905f5ad1f16d2ea719eb40c311609f8fad46fd95 (diff)
downloadtermgl-616951a353caf398908e6e6ce7dddf34a65513e5.tar.gz
Added cInput input handler class
cRender: +setConsoleCursor(bool) to disable console cursor main: Example workign with cInput cInput: Input handler class
Diffstat (limited to 'AmpelJonas')
-rw-r--r--AmpelJonas/Makefile2
-rw-r--r--AmpelJonas/cInput.cpp74
-rw-r--r--AmpelJonas/cInput.h48
-rw-r--r--AmpelJonas/cRender.cpp15
-rw-r--r--AmpelJonas/cRender.h2
-rw-r--r--AmpelJonas/main.cpp49
6 files changed, 186 insertions, 4 deletions
diff --git a/AmpelJonas/Makefile b/AmpelJonas/Makefile
index c529a34..ab2707a 100644
--- a/AmpelJonas/Makefile
+++ b/AmpelJonas/Makefile
@@ -3,7 +3,7 @@ CFLAGS = -Wall -g
LDFLAGS = -lm -lcurses
OUTPUT = Engine
-OBJ = main.o cObject.o cObjectHandler.o cRender.o
+OBJ = main.o cObject.o cObjectHandler.o cRender.o cInput.o
prog: $(OBJ)
$(CC) $(CFLAGS) -o $(OUTPUT) $(OBJ) $(LDFLAGS)
diff --git a/AmpelJonas/cInput.cpp b/AmpelJonas/cInput.cpp
new file mode 100644
index 0000000..e0aa233
--- /dev/null
+++ b/AmpelJonas/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/AmpelJonas/cInput.h b/AmpelJonas/cInput.h
new file mode 100644
index 0000000..8e3481c
--- /dev/null
+++ b/AmpelJonas/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 <stdio.h>
+#include <unistd.h>
+#include <termios.h>
+#include <sys/select.h>
+
+#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/cRender.cpp b/AmpelJonas/cRender.cpp
index bc271b8..6d7a70a 100644
--- a/AmpelJonas/cRender.cpp
+++ b/AmpelJonas/cRender.cpp
@@ -23,6 +23,8 @@ cRender::cRender(char _backound, WORD _color, int _sx, int _sy)
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
@@ -54,6 +56,12 @@ cRender::~cRender()
free(cScreen);
free(wColor);
free(bChanged);
+
+ setConsoleEcho(true);
+
+ #ifdef __linux__
+ setConsoleCursor(true);
+ #endif
}
int cRender::drawPoint(char _c, sPos _pos, bool _overrideCollision, WORD _color)
@@ -185,7 +193,7 @@ int cRender::clear(bool _forceReRender)
int cRender::clear()
{
- return clear(false); //false doesn't work!
+ return clear(false);
}
@@ -336,3 +344,8 @@ void cRender::setConsoleEcho(bool _enable)
(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
index ba5b9aa..c30a901 100644
--- a/AmpelJonas/cRender.h
+++ b/AmpelJonas/cRender.h
@@ -142,5 +142,7 @@ private:
void setConsoleEcho(bool _enable);
#ifdef __linux__
sPos getConsoleWindowSize();
+
+ void setConsoleCursor(bool _enable);
#endif
};
diff --git a/AmpelJonas/main.cpp b/AmpelJonas/main.cpp
index 18b483e..f0f6f50 100644
--- a/AmpelJonas/main.cpp
+++ b/AmpelJonas/main.cpp
@@ -3,10 +3,53 @@
#include "cRender.h"
#include "cObject.h"
#include "cObjectHandler.h"
+#include "cInput.h"
int main()
{
- unsigned long int framecounter = 0;
+ 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();
@@ -45,7 +88,9 @@ int main()
framecounter++;
//cin.get();
//usleep(100*1000);
- }
+ if(framecounter >= 1000)
+ break;
+ }*/
/*cObjectHandler b(&a);
cObject x(1,1);