From 167abfe3746df80dd48dd65751583d97449e55f3 Mon Sep 17 00:00:00 2001 From: jonas Date: Fri, 28 Dec 2018 15:09:41 +0100 Subject: Fixes in cObjectHandler:buildHitmap(), new Sample --- cObjectHandler.cpp | 14 +++++++++---- cObjectHandler.h | 2 +- main.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- testobject.h | 34 +++++++++++++++++++++++++++++++ 4 files changed, 103 insertions(+), 7 deletions(-) create mode 100644 testobject.h 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; +}; -- cgit v1.2.3