From 2e5d37905b1bc246ede3a090ae7160e7fe9b6228 Mon Sep 17 00:00:00 2001 From: jonas Date: Tue, 22 Jan 2019 11:40:05 +0100 Subject: added capabilty to manage cWiremesh to cObjecthandler --- cObjectHandler.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ cObjectHandler.h | 14 ++++++++++++++ cWiremesh.h | 1 - main.cpp | 30 +++++++++++++---------------- 4 files changed, 83 insertions(+), 18 deletions(-) diff --git a/cObjectHandler.cpp b/cObjectHandler.cpp index 140aadd..d88af92 100644 --- a/cObjectHandler.cpp +++ b/cObjectHandler.cpp @@ -39,6 +39,9 @@ int cObjectHandler::moveObject(int _object, sPos _pos, int _mode) int cObjectHandler::destroyObject(int _object) { + if(!objects[_object]) + return 1; + delete objects[_object]; objects[_object] = NULL; @@ -50,6 +53,12 @@ int cObjectHandler::write() { render->clear(); + for (unsigned long int i = 0; i < meshes.size(); i++) + { + if(meshes[i]) + meshes[i]->write(render); + } + for (unsigned long int i = 0; i < objects.size(); i++) { if (objects[i]) // Check if objects[i] is existent @@ -173,3 +182,50 @@ void cObjectHandler::focus(unsigned int _id) else iActiveObject = _id; } + +int cObjectHandler::createWiremesh(cWiremesh *_mesh) +{ + meshes.push_back(_mesh); + + return meshes.size() - 1; +} + +int cObjectHandler::moveWiremesh(int _mesh, sCoord3d _pos, int _mode) +{ + if (_mesh >= meshes.size()) //prevent segmentation faults + return 1; + + if (!meshes[_mesh]) + return 1; + + sCoord3d meshPosition = meshes[_mesh]->getPosition(); + + if (_mode == _MOVE_RELATIVE) + meshes[_mesh]->setPosition(meshPosition + _pos); + else if (_mode == _MOVE_ABSOLUTE) + meshes[_mesh]->setPosition(_pos); + + return 0; +} + +int cObjectHandler::destroyWiremesh(int _mesh) +{ + if(!meshes[_mesh]) + return 1; + + delete meshes[_mesh]; + meshes[_mesh] = NULL; + + return 0; +} + +int cObjectHandler::rotateWiremesh(int _mesh, sCoord3d _angle) +{ + if (_mesh >= meshes.size()) //prevent segmentation faults + return 1; + + if (!meshes[_mesh]) + return 1; + + meshes[_mesh]->rotate(_angle); +} diff --git a/cObjectHandler.h b/cObjectHandler.h index 010783a..47e138c 100644 --- a/cObjectHandler.h +++ b/cObjectHandler.h @@ -3,6 +3,7 @@ #include #include "cObject.h" +#include "cWiremesh.h" //movemodes #define _MOVE_RELATIVE 0 @@ -35,6 +36,18 @@ public: */ int destroyObject(int _object); + /** + * Analog to createObject() + */ + int createWiremesh(cWiremesh *_mesh); + + int moveWiremesh(int _mesh, sCoord3d _pos, int _mode); + + int rotateWiremesh(int _mesh, sCoord3d _angle); + + int destroyWiremesh(int _mesh); + + /** writes all objects in objects[] to render buffer */ int write(); @@ -64,6 +77,7 @@ private: void buildHitmap(); vector objects; + vector meshes; vector< vector > iHitMap; cRender *render; unsigned long int iActiveObject; diff --git a/cWiremesh.h b/cWiremesh.h index 0bdfbdb..320a141 100644 --- a/cWiremesh.h +++ b/cWiremesh.h @@ -6,7 +6,6 @@ #include "cRender.h" #define _DEPTH 99 - #define PI 3.14159265 struct sCoord3d diff --git a/main.cpp b/main.cpp index 991ea63..4f440b6 100644 --- a/main.cpp +++ b/main.cpp @@ -15,7 +15,7 @@ int main() { cRender render(' ', _COL_DEFAULT, 30,30); cObjectHandler handler(&render); - cObject ver(40,1); + cObject ver(45,1); cWiremesh obj; cInput input; @@ -29,7 +29,7 @@ int main() ver.drawPoint(VERSION + 48, {1,0}, true, _COL_WHITE); ver.drawPoint('.', {2,0}, true, _COL_WHITE); ver.drawPoint(PATCHLEVEL + 48, {3,0}, true, _COL_WHITE); - ver.drawText(DATE, {30,0}, _COL_WHITE); + ver.drawText(DATE, {32,0}, _COL_WHITE); ver.drawText(BRANCH, {5,0}, _COL_WHITE); int iver = handler.createObject(&ver); handler.moveObject(iver, {0,0}, _MOVE_ABSOLUTE); @@ -52,9 +52,8 @@ int main() obj.addVector({0,0,0}, {0,y,0}, ',', _COL_RED); obj.addVector({0,y,0}, {x,0,0}, ',', _COL_RED); obj.addVector({x,0,0}, {0,y,0}, ',', _COL_RED); + int imesh = handler.createWiremesh(&obj); - - sCoord3d position = {0,0,0}; while(1) { sInputEvent ie = input.poll(); @@ -66,19 +65,18 @@ int main() switch (ie.c) { case 'A'://up - position.y --; + handler.moveWiremesh(imesh,{0,-1,0}, _MOVE_RELATIVE); break; case 'B'://down - position.y ++; + handler.moveWiremesh(imesh,{0,1,0}, _MOVE_RELATIVE); break; case 'C'://right - position.x ++; + handler.moveWiremesh(imesh,{1,0,0}, _MOVE_RELATIVE); break; case 'D'://left - position.x --; + handler.moveWiremesh(imesh,{-1,0,0}, _MOVE_RELATIVE); break; }; - obj.setPosition(position); } else if (ie.type == _EVENT_MOUSE) { @@ -91,25 +89,24 @@ int main() switch(ie.c) { case 'w': - obj.rotate({-10,0,0}); + handler.rotateWiremesh(imesh,{-10,0,0}); break; case 's': - obj.rotate({10,0,0}); + handler.rotateWiremesh(imesh,{10,0,0}); break; case 'a': - obj.rotate({0,-10,0}); + handler.rotateWiremesh(imesh,{0,-10,0}); break; case 'd': - obj.rotate({0,10,0}); + handler.rotateWiremesh(imesh,{0,10,0}); break; case 'q': - obj.rotate({0,0,-10}); + handler.rotateWiremesh(imesh,{0,0,-10}); break; case 'e': - obj.rotate({0,0,10}); + handler.rotateWiremesh(imesh,{0,0,10}); break; }; - obj.setPosition(position); } else if (ie.type == _EVENT_TERM) { @@ -118,7 +115,6 @@ int main() } handler.write(); - obj.write(&render); render.render(); usleep(10*1000); -- cgit v1.2.3