aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar jonas <himself@jonasgunz.de> 2018-12-28 15:09:41 +0100
committerGravatar jonas <himself@jonasgunz.de> 2018-12-28 15:09:41 +0100
commit167abfe3746df80dd48dd65751583d97449e55f3 (patch)
treec308f70936e378eca8b68208a379e0458de934b4
parentc5af4c3057a904f2a037a624273c1c7a4b0295a5 (diff)
downloadtermgl-167abfe3746df80dd48dd65751583d97449e55f3.tar.gz
Fixes in cObjectHandler:buildHitmap(), new Sample
-rw-r--r--cObjectHandler.cpp14
-rw-r--r--cObjectHandler.h2
-rw-r--r--main.cpp60
-rw-r--r--testobject.h34
4 files changed, 103 insertions, 7 deletions
diff --git a/cObjectHandler.cpp b/cObjectHandler.cpp
index f1252d3..edd021c 100644
--- a/cObjectHandler.cpp
+++ b/cObjectHandler.cpp
@@ -30,7 +30,7 @@ int cObjectHandler::moveObject(int _object, sPos _pos, int _mode)
if (_mode == _MOVE_RELATIVE)
objects[_object]->setPosition(sPos{ objPosition.x + _pos.x, objPosition.y + _pos.y });
- else if (_mode == _MOVE_ABSOULUTE)
+ else if (_mode == _MOVE_ABSOLUTE)
objects[_object]->setPosition(_pos);
buildHitmap();
@@ -117,7 +117,13 @@ void cObjectHandler::buildHitmap()
{
iHitMap.pop_back();
}
-
+ for(unsigned int x = 0; x < iHitMap.size(); x++)
+ {
+ for(unsigned int y = 0; y < iHitMap[x].size(); y++)
+ {
+ iHitMap[x][y] = 0;
+ }
+ }
//Write object IDs to iHitMap
for(unsigned int i = 0; i < objects.size(); i++)
{
@@ -126,9 +132,9 @@ void cObjectHandler::buildHitmap()
sPos oPos = objects[i]->getPosition();
sPos oSize = objects[i]->getSize();
- for(unsigned int x = oPos.x; x < oPos.x + oSize.x; x++)
+ for(unsigned int x = oPos.x; x <= oPos.x + oSize.x; x++)
{
- for(unsigned int y = oPos.y; y < oPos.y + oSize.y; y++)
+ for(unsigned int y = oPos.y; y <= oPos.y + oSize.y; y++)
{
if(x < size.x && y < size.y) //Objects can be outside the screen.
iHitMap[x][y] = i;
diff --git a/cObjectHandler.h b/cObjectHandler.h
index ee83877..86034b8 100644
--- a/cObjectHandler.h
+++ b/cObjectHandler.h
@@ -6,7 +6,7 @@
//movemodes
#define _MOVE_RELATIVE 0
-#define _MOVE_ABSOULUTE 1
+#define _MOVE_ABSOLUTE 1
using namespace std;
diff --git a/main.cpp b/main.cpp
index 786194a..94f834c 100644
--- a/main.cpp
+++ b/main.cpp
@@ -5,9 +5,65 @@
#include "cObjectHandler.h"
#include "cInput.h"
+#include "testobject.h"
+
int main()
{
- cRender a(' ', _COL_DEFAULT, 20,20);
+ cRender render(' ', _COL_DEFAULT, 30,30);
+ cObjectHandler handler(&render);
+ testobject obj;
+ cInput input;
+
+ render.render();
+
+ int iobj = handler.createObject((cObject*)&obj);
+ handler.moveObject(iobj, {10,10}, _MOVE_ABSOLUTE);
+
+ while(1)
+ {
+ sInputEvent ie = input.poll();
+ if(ie.type != _EVENT_NULL)
+ {
+ if(ie.type == _EVENT_KEY)
+ {
+ switch (ie.c)
+ {
+ case 'A'://up
+ handler.moveObject(iobj, {0,-1}, _MOVE_RELATIVE);
+ break;
+ case 'B'://down
+ handler.moveObject(iobj, {0,1}, _MOVE_RELATIVE);
+ break;
+ case 'C'://right
+ handler.moveObject(iobj, {1,0}, _MOVE_RELATIVE);
+ break;
+ case 'D'://left
+ handler.moveObject(iobj, {-1,0}, _MOVE_RELATIVE);
+ break;
+ };
+ }
+ else if (ie.type == _EVENT_MOUSE)
+ {
+ if(ie.b == 0)
+ handler.clickEvent({ie.x, ie.y}, 0);
+ }
+ else if (ie.type == _EVENT_CHAR)
+ {
+ handler.charEvent(ie.c);
+ }
+ else if (ie.type == _EVENT_TERM)
+ {
+ return 0;
+ }
+
+ handler.write();
+ render.render();
+
+ usleep(10*1000);
+ }
+ }
+
+ /*cRender a(' ', _COL_DEFAULT, 20,20);
cInput in;
a.render();
@@ -53,7 +109,7 @@ int main()
usleep(10*1000);
}
- }
+ }*/
/*unsigned long int framecounter = 0;
cRender a(' ', _COL_DEFAULT, 10,10);
diff --git a/testobject.h b/testobject.h
new file mode 100644
index 0000000..5e5475f
--- /dev/null
+++ b/testobject.h
@@ -0,0 +1,34 @@
+#pragma once
+
+#include "cObject.h"
+
+class testobject : cObject
+{
+
+public:
+ testobject()
+ {
+ setSize(10,5);
+ cc = 0;
+
+ drawRectangle('#', NULL, {0,0}, {9,4}, _COL_GREEN, _COL_DEFAULT);
+ }
+
+ ~testobject()
+ {
+ destruct();
+ }
+
+ virtual void onClick(sPos _pos, unsigned int _button)
+ {
+ cc++;
+ drawText(to_string(cc), {2,2}, _COL_RED);
+ }
+
+ virtual void onChar(unsigned char _c)
+ {
+ drawPoint(_c, {1,1},true, _COL_BLUE);
+ }
+private:
+ int cc;
+};