From 8d44d9387b057200d2ab39359895bf3a5a81b492 Mon Sep 17 00:00:00 2001 From: Misa Date: Sun, 1 Mar 2020 12:24:43 -0800 Subject: [PATCH] Refactor edentities to not use separate length-trackers This turns the array 'edentity' into a proper vector, and removes the need to use a separate length-tracking variable and manually keep track of the actual amount of edentities in the level by using the long-winded 'EditorData::GetInstance().numedentities'. This manual tracking was more error-prone and much less maintainable. editorclass::naddedentity() has been removed due to now functionally being the same as editorclass::addedentity() (there's no more 'EditorData::GetInstance().numedentities' to not increment) and for also being unused in the first place. editorclass::copyedentity() has been removed because it was only used to shift the rest of the edentities up manually, but now that we let C++ do all the hard work it's no longer necessary. --- desktop_version/src/Map.cpp | 2 +- desktop_version/src/editor.cpp | 130 ++++++++++++--------------------- desktop_version/src/editor.h | 12 +-- desktop_version/src/main.cpp | 4 +- 4 files changed, 52 insertions(+), 96 deletions(-) diff --git a/desktop_version/src/Map.cpp b/desktop_version/src/Map.cpp index 016a934a..56b6072f 100644 --- a/desktop_version/src/Map.cpp +++ b/desktop_version/src/Map.cpp @@ -1646,7 +1646,7 @@ void mapclass::loadlevel(int rx, int ry, Graphics& dwgfx, Game& game, entityclas //Entities have to be created HERE, akwardly int tempcheckpoints=0; int tempscriptbox=0; - for(int edi=0; ediFirstChildElement(); edEntityEl; edEntityEl=edEntityEl->NextSiblingElement()) { + edentities entity; + std::string pKey(edEntityEl->Value()); - //const char* pText = edEntityEl->GetText() ; - if(edEntityEl->GetText() != NULL) + if (edEntityEl->GetText() != NULL) { - edentity[i].scriptname = std::string(edEntityEl->GetText()) ; + entity.scriptname = std::string(edEntityEl->GetText()); } - edEntityEl->QueryIntAttribute("x", &edentity[i].x); - edEntityEl->QueryIntAttribute("y", &edentity[i].y); - edEntityEl->QueryIntAttribute("t", &edentity[i].t); + edEntityEl->QueryIntAttribute("x", &entity.x); + edEntityEl->QueryIntAttribute("y", &entity.y); + edEntityEl->QueryIntAttribute("t", &entity.t); - edEntityEl->QueryIntAttribute("p1", &edentity[i].p1); - edEntityEl->QueryIntAttribute("p2", &edentity[i].p2); - edEntityEl->QueryIntAttribute("p3", &edentity[i].p3); - edEntityEl->QueryIntAttribute("p4", &edentity[i].p4); - edEntityEl->QueryIntAttribute("p5", &edentity[i].p5); - edEntityEl->QueryIntAttribute("p6", &edentity[i].p6); - - i++; + edEntityEl->QueryIntAttribute("p1", &entity.p1); + edEntityEl->QueryIntAttribute("p2", &entity.p2); + edEntityEl->QueryIntAttribute("p3", &entity.p3); + edEntityEl->QueryIntAttribute("p4", &entity.p4); + edEntityEl->QueryIntAttribute("p5", &entity.p5); + edEntityEl->QueryIntAttribute("p6", &entity.p6); + edentity.push_back(entity); } - - EditorData::GetInstance().numedentities = i; } if (pKey == "levelMetaData") @@ -2056,7 +2053,7 @@ void editorclass::save(std::string& _path) */ msg = new TiXmlElement( "edEntities" ); - for(int i = 0; i < EditorData::GetInstance().numedentities; i++) + for(size_t i = 0; i < edentity.size(); i++) { TiXmlElement *edentityElement = new TiXmlElement( "edentity" ); edentityElement->SetAttribute( "x", edentity[i].x); @@ -2113,64 +2110,30 @@ void editorclass::save(std::string& _path) void addedentity( int xp, int yp, int tp, int p1/*=0*/, int p2/*=0*/, int p3/*=0*/, int p4/*=0*/, int p5/*=320*/, int p6/*=240*/) { - edentity[EditorData::GetInstance().numedentities].x=xp; - edentity[EditorData::GetInstance().numedentities].y=yp; - edentity[EditorData::GetInstance().numedentities].t=tp; - edentity[EditorData::GetInstance().numedentities].p1=p1; - edentity[EditorData::GetInstance().numedentities].p2=p2; - edentity[EditorData::GetInstance().numedentities].p3=p3; - edentity[EditorData::GetInstance().numedentities].p4=p4; - edentity[EditorData::GetInstance().numedentities].p5=p5; - edentity[EditorData::GetInstance().numedentities].p6=p6; - edentity[EditorData::GetInstance().numedentities].scriptname=""; + edentities entity; - EditorData::GetInstance().numedentities++; -} + entity.x=xp; + entity.y=yp; + entity.t=tp; + entity.p1=p1; + entity.p2=p2; + entity.p3=p3; + entity.p4=p4; + entity.p5=p5; + entity.p6=p6; + entity.scriptname=""; -void naddedentity( int xp, int yp, int tp, int p1/*=0*/, int p2/*=0*/, int p3/*=0*/, int p4/*=0*/, int p5/*=320*/, int p6/*=240*/) -{ - edentity[EditorData::GetInstance().numedentities].x=xp; - edentity[EditorData::GetInstance().numedentities].y=yp; - edentity[EditorData::GetInstance().numedentities].t=tp; - edentity[EditorData::GetInstance().numedentities].p1=p1; - edentity[EditorData::GetInstance().numedentities].p2=p2; - edentity[EditorData::GetInstance().numedentities].p3=p3; - edentity[EditorData::GetInstance().numedentities].p4=p4; - edentity[EditorData::GetInstance().numedentities].p5=p5; - edentity[EditorData::GetInstance().numedentities].p6=p6; - edentity[EditorData::GetInstance().numedentities].scriptname=""; -} - -void copyedentity( int a, int b ) -{ - edentity[a].x=edentity[b].x; - edentity[a].y=edentity[b].y; - edentity[a].t=edentity[b].t; - edentity[a].p1=edentity[b].p1; - edentity[a].p2=edentity[b].p2; - edentity[a].p3=edentity[b].p3; - edentity[a].p4=edentity[b].p4; - edentity[a].p5=edentity[b].p5; - edentity[a].p6=edentity[b].p6; - edentity[a].scriptname=edentity[b].scriptname; + edentity.push_back(entity); } void removeedentity( int t ) { - if(t==EditorData::GetInstance().numedentities-1) - { - EditorData::GetInstance().numedentities--; - } - else - { - for(int m=t; m= 0; i--) + for (int i = edentity.size() - 1; i >= 0; i--) { //if() on screen int tx=(edentity[i].x-(edentity[i].x%40))/40; @@ -4413,7 +4375,7 @@ void editorinput( KeyPoll& key, Graphics& dwgfx, Game& game, mapclass& map, enti if(key.keymap[SDLK_w] && ed.keydelay==0) { int j=0, tx=0, ty=0; - for(int i=0; i edentity; extern scriptclass script; class EditorData @@ -63,7 +63,6 @@ class EditorData } - int numedentities; std::string title; std::string creator; @@ -74,8 +73,7 @@ class EditorData private: - EditorData(): - numedentities(0) + EditorData() { } @@ -242,10 +240,6 @@ class editorclass{ void addedentity(int xp, int yp, int tp, int p1=0, int p2=0, int p3=0, int p4=0, int p5=320, int p6=240); -void naddedentity(int xp, int yp, int tp, int p1=0, int p2=0, int p3=0, int p4=0, int p5=320, int p6=240); - -void copyedentity(int a, int b); - void removeedentity(int t); int edentat(int xp, int yp); @@ -266,4 +260,4 @@ void editorinput(KeyPoll& key, Graphics& dwgfx, Game& game, mapclass& map, #endif /* EDITOR_H */ -#endif /* NO_CUSTOM_LEVELS */ \ No newline at end of file +#endif /* NO_CUSTOM_LEVELS */ diff --git a/desktop_version/src/main.cpp b/desktop_version/src/main.cpp index 6f14e5d9..7459bb48 100644 --- a/desktop_version/src/main.cpp +++ b/desktop_version/src/main.cpp @@ -32,8 +32,8 @@ scriptclass script; #if !defined(NO_CUSTOM_LEVELS) - edentities edentity[3000]; - editorclass ed; + std::vector edentity; + editorclass ed; #endif UtilityClass help;