From 99249ba62bcdfbe300c6ab7db02088d8d1e118c4 Mon Sep 17 00:00:00 2001 From: Jonas Gunz Date: Thu, 7 Mar 2019 01:06:25 +0100 Subject: Switched to unsigned cariables for all counting loops and buffer accesses --- src/cObject.cpp | 20 +++++++------------- src/cObject.h | 13 ------------- src/cObjectHandler.cpp | 34 +++++++++++++++++----------------- src/cObjectHandler.h | 14 +++++++------- src/cRender.cpp | 32 ++++++++++++++++---------------- src/cRender.h | 2 +- 6 files changed, 48 insertions(+), 67 deletions(-) diff --git a/src/cObject.cpp b/src/cObject.cpp index bba7509..19f485f 100644 --- a/src/cObject.cpp +++ b/src/cObject.cpp @@ -27,22 +27,16 @@ void cObject::setPosition(int _x, int _y) pos.y = _y; } - -sObject cObject::getObject() -{ - return sObject{pos, wColor, cScreen, sizeX, sizeY}; -} - void cObject::write(cRender *_render, sPos _cameraPosition) { if(!bSizeSet) return; - for (int o = 0; o < sizeY; o++) { //y axis - for (int p = 0; p < sizeX; p++) { //x axis + for (unsigned int o = 0; o < sizeY; o++) { //y axis + for (unsigned int p = 0; p < sizeX; p++) { //x axis if (cScreen[p][o]) { //Dont overwrite empty pixels - sPos npos{ pos.x + p - _cameraPosition.x, - pos.y + o - _cameraPosition.y }; + sPos npos{ pos.x + (int)p - _cameraPosition.x, + pos.y + (int)o - _cameraPosition.y }; _render->drawPoint(cScreen[p][o], npos, true, wColor[p][o]); } } @@ -71,8 +65,8 @@ void cObject::setSize(unsigned int _sx, unsigned int _sy) for (unsigned int i = 0; i < _sx; i++) wColor[i] = (WORD*)malloc(sizeof *wColor[i] * _sy); - for (int i = 0; i < sizeY; i++) { - for (int o = 0; o < sizeX; o++) { + for (unsigned int i = 0; i < sizeY; i++) { + for (unsigned int o = 0; o < sizeX; o++) { cScreen[o][i] = 0; wColor[o][i] = _COL_DEFAULT; } @@ -86,7 +80,7 @@ void cObject::destruct() if(!bSizeSet) return; - for (int i = 0; i < sizeX; i++) { + for (unsigned int i = 0; i < sizeX; i++) { free(cScreen[i]); free(wColor[i]); } diff --git a/src/cObject.h b/src/cObject.h index 2504d71..6f24158 100644 --- a/src/cObject.h +++ b/src/cObject.h @@ -3,14 +3,6 @@ #include "cRender.h" -struct sObject -{ - sPos pos; - WORD **wColor; - char **cScreen; - int sizeX; - int sizeY; -}; /** cObject can be used standalone as well as inherited * every cObject has its own framebuffer as well as position viariables to be moveable. @@ -48,11 +40,6 @@ public: */ void setPosition(int _x, int _y); - /** Returns sObject with framebuffer and current position - * DEPRECATED - */ - sObject getObject(); - /** Writes object to framebuffer with offset _cameraPosition */ void write(cRender *_render, sPos _cameraPosition); diff --git a/src/cObjectHandler.cpp b/src/cObjectHandler.cpp index 1168b86..3b69cfb 100644 --- a/src/cObjectHandler.cpp +++ b/src/cObjectHandler.cpp @@ -1,6 +1,6 @@ #include "cObjectHandler.h" -cObjectHandler::cObjectHandler(cRender *_render, bool _enableInputMapping, bool _enableCollision) : cameraPosition ({0,0}), iActiveObject(0) +cObjectHandler::cObjectHandler(cRender *_render, bool _enableInputMapping, bool _enableCollision) : iActiveObject(0), cameraPosition ({0,0}) { render = _render; @@ -12,7 +12,7 @@ cObjectHandler::cObjectHandler(cRender *_render, bool _enableInputMapping, bool buildHitmap(); } -int cObjectHandler::createObject(cObject *_object) +unsigned int cObjectHandler::createObject(cObject *_object) { objects.push_back(_object); @@ -20,7 +20,7 @@ int cObjectHandler::createObject(cObject *_object) return objects.size() - 1; } -int cObjectHandler::moveObject(int _object, sPos _pos, int _mode) +int cObjectHandler::moveObject(unsigned int _object, sPos _pos, int _mode) { if (_object >= objects.size()) //prevent segmentation faults return 1; @@ -48,7 +48,7 @@ int cObjectHandler::moveObject(int _object, sPos _pos, int _mode) if(coll.idc) { - for(int i = 0; i < coll.idc; i++) + for(unsigned int i = 0; i < coll.idc; i++) { if(coll.idv[i] != _object) abort += objects[_object]->onCollisionActive(_pos, objects[coll.idv[i]]->onCollisionPassive(_pos)); @@ -67,7 +67,7 @@ int cObjectHandler::moveObject(int _object, sPos _pos, int _mode) return abort; } -int cObjectHandler::destroyObject(int _object) +int cObjectHandler::destroyObject(unsigned int _object) { if(!objects[_object]) return 1; @@ -106,9 +106,9 @@ int cObjectHandler::write() int cObjectHandler::clickEvent(sPos _pos, unsigned int _button) { - if(_pos.x >= iHitMap.size()) + if(_pos.x >= (int)iHitMap.size()) return 1; - if(_pos.y >= iHitMap[_pos.x].size()) + if(_pos.y >= (int)iHitMap[_pos.x].size()) return 1; @@ -153,17 +153,17 @@ void cObjectHandler::buildHitmap() vector cp; - while(size.y > cp.size()) + while(size.y > (int)cp.size()) { cp.push_back(0); } - while (size.x > iHitMap.size()) + while (size.x > (int)iHitMap.size()) { iHitMap.push_back(cp); } - while (size.x <= iHitMap.size()) + while (size.x <= (int)iHitMap.size()) { iHitMap.pop_back(); } @@ -213,14 +213,14 @@ void cObjectHandler::focus(unsigned int _id) iActiveObject = _id; } -int cObjectHandler::createWiremesh(cWiremesh *_mesh) +unsigned int cObjectHandler::createWiremesh(cWiremesh *_mesh) { meshes.push_back(_mesh); return meshes.size() - 1; } -int cObjectHandler::moveWiremesh(int _mesh, sCoord3d _pos, int _mode) +int cObjectHandler::moveWiremesh(unsigned int _mesh, sCoord3d _pos, int _mode) { if (_mesh >= meshes.size()) //prevent segmentation faults return 1; @@ -238,7 +238,7 @@ int cObjectHandler::moveWiremesh(int _mesh, sCoord3d _pos, int _mode) return 0; } -int cObjectHandler::destroyWiremesh(int _mesh) +int cObjectHandler::destroyWiremesh(unsigned int _mesh) { if(!meshes[_mesh]) return 1; @@ -249,7 +249,7 @@ int cObjectHandler::destroyWiremesh(int _mesh) return 0; } -int cObjectHandler::rotateWiremesh(int _mesh, sCoord3d _angle) +int cObjectHandler::rotateWiremesh(unsigned int _mesh, sCoord3d _angle) { if (_mesh >= meshes.size()) //prevent segmentation faults return 1; @@ -303,9 +303,9 @@ sCollision cObjectHandler::checkCollision(sPos _pos, sPos _size) //The mother of if-statements //No collision for offscreen objects if( (_pos.x < cameraPosition.x && _pos.x + _size.x + cameraPosition.x < 0) || - (_pos.x - cameraPosition.x >= iHitMap.size() && _pos.x + _size.x - cameraPosition.x >= iHitMap.size()) || + (_pos.x - cameraPosition.x >= (int)iHitMap.size() && _pos.x + _size.x - cameraPosition.x >= (int)iHitMap.size()) || (_pos.y < cameraPosition.y && _pos.y + _size.y + cameraPosition.y < 0) || - (_pos.y - cameraPosition.y >= iHitMap[0].size() && _pos.y + _size.y - cameraPosition.y >= iHitMap[0].size()) ) + (_pos.y - cameraPosition.y >= (int)iHitMap[0].size() && _pos.y + _size.y - cameraPosition.y >= (int)iHitMap[0].size()) ) return ret; for(int x = _pos.x - cameraPosition.x; x < _pos.x + _size.x - cameraPosition.x; x++) @@ -327,7 +327,7 @@ sCollision cObjectHandler::checkCollision(sPos _pos, sPos _size) { swaps = 0; - for(long int i = 0; i < (long int)collisions.size() - 1; i++) + for(long int i = 0; i < (long int)collisions.size() - 1; i++) //Do not change to unsigned! can cause underflow!! { if(collisions[i] > collisions[i + 1]) { diff --git a/src/cObjectHandler.h b/src/cObjectHandler.h index 4a0306b..f53ccd7 100644 --- a/src/cObjectHandler.h +++ b/src/cObjectHandler.h @@ -38,30 +38,30 @@ public: * Adds _object to managed objects vector * returns Identifier for newly created vector */ - int createObject(cObject *_object); + unsigned int createObject(cObject *_object); /** * Alters position of _object by _pos either relative to old position or Absolute * Depending on selected _mode (_MOVE_RELATIVE / _MOVE_ABSOLUTE / _MOVE_ABSOLUTE). * _MOVE_ABSOLUTE not recommended: Collision is only checked at destination. To ensure initialisation, use _MOVE_FORCE_ABSOLUTE! */ - int moveObject(int _object, sPos _pos, int _mode); + int moveObject(unsigned int _object, sPos _pos, int _mode); /** * removes _object from vector after deleting it */ - int destroyObject(int _object); + int destroyObject(unsigned int _object); /** * Analog to createObject() */ - int createWiremesh(cWiremesh *_mesh); + unsigned int createWiremesh(cWiremesh *_mesh); - int moveWiremesh(int _mesh, sCoord3d _pos, int _mode); + int moveWiremesh(unsigned int _mesh, sCoord3d _pos, int _mode); - int rotateWiremesh(int _mesh, sCoord3d _angle); + int rotateWiremesh(unsigned int _mesh, sCoord3d _angle); - int destroyWiremesh(int _mesh); + int destroyWiremesh(unsigned int _mesh); void setCameraPosition(sPos _pos, int _mode); diff --git a/src/cRender.cpp b/src/cRender.cpp index df518ee..874b955 100644 --- a/src/cRender.cpp +++ b/src/cRender.cpp @@ -1,7 +1,7 @@ #include "cRender.h" -cRender::cRender(char _backound, WORD _color, int _sx, int _sy) +cRender::cRender(char _backound, WORD _color, unsigned int _sx, unsigned int _sy) { bBlockRender = false; //If this Constructor is used, this instance is not inherited, thus render() doesn't need to be blocked iLastError = _OK_; @@ -48,7 +48,7 @@ cRender::~cRender() if(bBlockRender) //Don't run destructor if inherited return; - for (int i = 0; i < sizeX; i++) { + for (unsigned int i = 0; i < sizeX; i++) { free(cScreen[i]); free(wColor[i]); free(bChanged[i]); @@ -68,10 +68,10 @@ cRender::~cRender() int cRender::drawPoint(char _c, sPos _pos, bool _overrideCollision, WORD _color) { - if (_pos.x >= sizeX || _pos.y >= sizeY || _pos.x < 0 || _pos.y < 0) + if (_pos.x >= (int)sizeX || _pos.y >= (int)sizeY || _pos.x < 0 || _pos.y < 0) return _ERR_COORDINATES_INVALID_; - if (cScreen[_pos.x][_pos.y] != cBackound && _overrideCollision != true) //detect Collsision + if (_overrideCollision != true && cScreen[_pos.x][_pos.y] != cBackound) //detect Collsision return _COLLISION_; cScreen[_pos.x][_pos.y] = _c; @@ -142,9 +142,9 @@ int cRender::drawLine(char _c, sPos _pos1, sPos _pos2, bool _overrideCollision, int cRender::drawText(string _s, sPos _pos, WORD _color) { - for (int i = 0; i < _s.length(); i++) + for (unsigned int i = 0; i < _s.length(); i++) { - drawPoint(_s[i], sPos{ i + _pos.x,_pos.y }, true, _color); + drawPoint(_s[i], sPos{ (int)i + _pos.x,_pos.y }, true, _color); } return 0; } @@ -177,8 +177,8 @@ int cRender::render(void) //Resize screenbuffer if needed setBufferSize( getConsoleWindowSize( ) ); - for (int i = 0; i < sizeY; i++) { - for (int o = 0; o < sizeX; o++) { + for (unsigned int i = 0; i < sizeY; i++) { + for (unsigned int o = 0; o < sizeX; o++) { if(bChanged[o][i]) { #ifdef _WIN32 @@ -191,7 +191,7 @@ int cRender::render(void) #elif __linux__ //gotoxy(x,y) now included!! char buffer[20]; - int cbuf = sprintf(buffer,"\e[%i;%iH\e[%im%c", i + 1, o + 1, wColor[o][i], cScreen[o][i]); + int cbuf = sprintf(buffer,"\e[%u;%uH\e[%im%c", i + 1, o + 1, wColor[o][i], cScreen[o][i]); // Position Color Origin is at 1,1 write (STDOUT_FILENO, buffer, cbuf); @@ -205,8 +205,8 @@ int cRender::render(void) int cRender::clear(bool _forceReRender) { - for (int i = 0; i < sizeY; i++) { - for (int o = 0; o < sizeX; o++) { + for (unsigned int i = 0; i < sizeY; i++) { + for (unsigned int o = 0; o < sizeX; o++) { if(((cScreen[o][i] == cBackound) && (wColor[o][i] == wBackColor)) && !_forceReRender) bChanged[o][i] = false; else @@ -302,7 +302,7 @@ void cRender::setConsoleCursor(bool _enable) void cRender::setBufferSize(sPos _size) { - if(_size.x == sizeX && _size.y == sizeY) + if(_size.x == (int)sizeX && _size.y == (int)sizeY) return; if(_size.x < 0 || _size.y < 0) @@ -310,7 +310,7 @@ void cRender::setBufferSize(sPos _size) if(sizeX!=0 && sizeY!=0) //resize. delete first { - for (int i = 0; i < sizeX; i++) { + for (unsigned int i = 0; i < sizeX; i++) { free(cScreen[i]); free(wColor[i]); free(bChanged[i]); @@ -342,13 +342,13 @@ void cRender::setBufferSize(sPos _size) sPos cRender::getSize() { - return {sizeX, sizeY}; + return {(int)sizeX, (int)sizeY}; } void cRender::forceReRender() { - for (int i = 0; i < sizeY; i++) { - for (int o = 0; o < sizeX; o++) { + for (unsigned int i = 0; i < sizeY; i++) { + for (unsigned int o = 0; o < sizeX; o++) { bChanged[o][i] = true; } } diff --git a/src/cRender.h b/src/cRender.h index 5808c82..d262791 100644 --- a/src/cRender.h +++ b/src/cRender.h @@ -74,7 +74,7 @@ public: * Resizes console window for Windows * Sets Size to Console Window Size for Linux. Writes Error for _sx or _sy smaller than Screen. Get by getLastError() */ - cRender(char _backound, WORD _color, int _sx, int _sy); + cRender(char _backound, WORD _color, unsigned int _sx, unsigned int _sy); virtual ~cRender(); -- cgit v1.2.3