From 6dce8d1945731314ee4def31431cc99a6d620192 Mon Sep 17 00:00:00 2001 From: Jonas Gunz Date: Sun, 10 Feb 2019 01:57:40 +0100 Subject: cObjectHandler: Implemented Camera movement cObjectHandler can now move the origin of the camera around the screen --- cObjectHandler.cpp | 35 ++++++++++++++++++++++++++++++++--- cObjectHandler.h | 6 ++++++ test.cpp | 38 ++++++++++++++++++++++++++++++++++---- 3 files changed, 72 insertions(+), 7 deletions(-) diff --git a/cObjectHandler.cpp b/cObjectHandler.cpp index dba0b77..8a62a52 100644 --- a/cObjectHandler.cpp +++ b/cObjectHandler.cpp @@ -3,6 +3,9 @@ cObjectHandler::cObjectHandler(cRender *_render) { render = _render; + + cameraPosition = {0,0}; + iActiveObject = 0; objects.push_back(NULL); //Create first Object as Catcher for Events @@ -56,7 +59,11 @@ int cObjectHandler::write() for (unsigned long int i = 0; i < meshes.size(); i++) { if(meshes[i]) + { + moveWiremesh(i,{-cameraPosition.x, -cameraPosition.y, 0} ,_MOVE_RELATIVE); meshes[i]->write(render); + moveWiremesh(i,{cameraPosition.x, cameraPosition.y, 0},_MOVE_RELATIVE); + } } for (unsigned long int i = 0; i < objects.size(); i++) @@ -69,7 +76,8 @@ int cObjectHandler::write() 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 }; + sPos pos{ obj.pos.x + p - cameraPosition.x, + obj.pos.y + o - cameraPosition.y }; render->drawPoint(obj.cScreen[p][o], pos, true, obj.wColor[p][o]); } } @@ -92,8 +100,8 @@ int cObjectHandler::clickEvent(sPos _pos, unsigned int _button) { sPos rel_pos; sPos obj_pos = objects[ iHitMap[_pos.x][_pos.y] ]->getPosition(); - rel_pos.x = _pos.x - obj_pos.x; - rel_pos.y = _pos.y - obj_pos.y; + rel_pos.x = _pos.x - obj_pos.x + cameraPosition.x; + rel_pos.y = _pos.y - obj_pos.y + cameraPosition.y; iActiveObject = iHitMap[_pos.x][_pos.y]; //Set active object objects[ iHitMap[_pos.x][_pos.y] ]->onClick(rel_pos, _button); @@ -155,6 +163,9 @@ void cObjectHandler::buildHitmap() sPos oPos = objects[i]->getPosition(); sPos oSize = objects[i]->getSize(); + oPos.x -= cameraPosition.x; + oPos.y -= cameraPosition.y; + for(int x = oPos.x; x < oPos.x + oSize.x; x++) { for(int y = oPos.y; y < oPos.y + oSize.y; y++) @@ -231,3 +242,21 @@ int cObjectHandler::rotateWiremesh(int _mesh, sCoord3d _angle) return 0; } + +void cObjectHandler::setCameraPosition(sPos _pos, int _mode) +{ + if(_mode == _MOVE_ABSOLUTE) + cameraPosition = _pos; + else if(_mode == _MOVE_RELATIVE) + { + cameraPosition.x += _pos.x; + cameraPosition.y += _pos.y; + } + + buildHitmap(); +} + +sPos cObjectHandler::getCameraPosition() +{ + return cameraPosition; +} diff --git a/cObjectHandler.h b/cObjectHandler.h index bc6a48d..0e7750c 100644 --- a/cObjectHandler.h +++ b/cObjectHandler.h @@ -53,6 +53,11 @@ public: int destroyWiremesh(int _mesh); + void setCameraPosition(sPos _pos, int _mode); + + sPos getCameraPosition(); + + /** * writes all objects in objects[] to render buffer */ @@ -91,4 +96,5 @@ private: vector< vector > iHitMap; cRender *render; unsigned long int iActiveObject; + sPos cameraPosition; }; diff --git a/test.cpp b/test.cpp index d9970b4..4278f2a 100644 --- a/test.cpp +++ b/test.cpp @@ -11,12 +11,39 @@ //#include "testobject.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(std::to_string(cc), {2,2}, _COL_RED); + + drawPoint('Q', _pos, true, _COL_YELLOW); + } + + virtual void onChar(unsigned char _c) { drawPoint(_c, {1,1},true, _COL_BLUE); } +private: + int cc; +}; + int main(int argc, char* argv[]) { cRender render(' ', _COL_DEFAULT, 30,30); cObjectHandler handler(&render); cObject ver(45,1); cWiremesh obj; + testobject obj2; cInput input; @@ -59,6 +86,9 @@ int main(int argc, char* argv[]) obj.addVector({x,-x,0}, {0,2*y,0}, ',', _COL_RED); int imesh = handler.createWiremesh(&obj); + int iobj2 = handler.createObject((cObject*)&obj2); + handler.moveObject(iobj2, {3,3}, _MOVE_ABSOLUTE); + sPos middle = render.getSize(); middle.x /= 2; middle.y /= 2; @@ -76,16 +106,16 @@ int main(int argc, char* argv[]) switch (ie.c) { case 'A'://up - handler.moveWiremesh(imesh,{0,-1,0}, _MOVE_RELATIVE); + handler.setCameraPosition({0,-1}, _MOVE_RELATIVE); break; case 'B'://down - handler.moveWiremesh(imesh,{0,1,0}, _MOVE_RELATIVE); + handler.setCameraPosition({0,1}, _MOVE_RELATIVE); break; case 'C'://right - handler.moveWiremesh(imesh,{1,0,0}, _MOVE_RELATIVE); + handler.setCameraPosition({1,0}, _MOVE_RELATIVE); break; case 'D'://left - handler.moveWiremesh(imesh,{-1,0,0}, _MOVE_RELATIVE); + handler.setCameraPosition({-1,0}, _MOVE_RELATIVE); break; }; } -- cgit v1.2.3