diff options
-rw-r--r-- | Changelog | 10 | ||||
-rw-r--r-- | include/Texture.h | 3 | ||||
-rw-r--r-- | include/entities.h | 4 | ||||
-rwxr-xr-x | include/tinyxml2.h | 7 | ||||
-rw-r--r-- | include/world.h | 11 | ||||
-rw-r--r-- | main.cpp | 1 | ||||
-rw-r--r-- | src/Texture.cpp | 28 | ||||
-rw-r--r-- | src/entities.cpp | 15 | ||||
-rwxr-xr-x | src/tinyxml2.cpp | 13 | ||||
-rw-r--r-- | src/world.cpp | 154 |
10 files changed, 125 insertions, 121 deletions
@@ -712,3 +712,13 @@ - merged 'remake' with 'master', fixed font issues and world stuffs - continued work on merchant dialog + +3/2/2016, +3/3/2016: +========= + + - made particle vector not pointers + - improved efficiency of code using newly learned C++11 tricks + - added StrAttribute() to TinyXML2 + - more merchant stuff, trading???? + - improved village XML loading diff --git a/include/Texture.h b/include/Texture.h index 659c32d..7201a4c 100644 --- a/include/Texture.h +++ b/include/Texture.h @@ -58,7 +58,7 @@ public: * Contains an array of the GLuints returned from Texture::loadTexture(). */ - GLuint *image = NULL; + std::vector<GLuint> image; /** * Populates the image array from a list of strings, with each string as a @@ -73,6 +73,7 @@ public: Texturec(uint amt,const char **paths); Texturec(std::vector<std::string>vec); + Texturec( std::initializer_list<std::string> l ); /** * Frees memory taken by the image array. diff --git a/include/entities.h b/include/entities.h index 545f0a4..3b8a19b 100644 --- a/include/entities.h +++ b/include/entities.h @@ -228,8 +228,8 @@ class Structures : public Entity{ public: BUILD_SUB bsubtype; World *inWorld; - char *inside; - char *textureLoc; + std::string inside; + std::string textureLoc; Structures(); ~Structures(); diff --git a/include/tinyxml2.h b/include/tinyxml2.h index fb7464a..4282642 100755 --- a/include/tinyxml2.h +++ b/include/tinyxml2.h @@ -38,6 +38,8 @@ distribution. # include <cstring>
#endif
+#include <string>
+
/*
TODO: intern strings instead of allocation.
*/
@@ -1187,6 +1189,11 @@ public: */
const char* Attribute( const char* name, const char* value=0 ) const;
+ /** Functions the same as Attribute(), but returns the result
+ as a std::string.
+ */
+ std::string StrAttribute( const char* name, const char* value=0 ) const;
+
/** Given an attribute name, IntAttribute() returns the value
of the attribute interpreted as an integer. 0 will be
returned if there is an error. For a method with error
diff --git a/include/world.h b/include/world.h index b3d1071..7b9383a 100644 --- a/include/world.h +++ b/include/world.h @@ -279,7 +279,7 @@ public: * A vector of all particles in this world. */ - std::vector<Particles *> particles; + std::vector<Particles> particles; std::vector<Village *> village; @@ -294,7 +294,7 @@ public: * Vector of all building textures for the current world style */ - std::vector<std::string > sTexLoc; + std::vector<std::string> sTexLoc; /** * NULLifies pointers and allocates necessary memory. This should be @@ -317,8 +317,7 @@ public: * the structure. */ - void addStructure(BUILD_SUB subtype,float x,float y, char* tex, const char *inside); - //void addVillage(int buildingCount, int npcMin, int npcMax,const char *inside); + void addStructure(BUILD_SUB subtype,float x,float y, std::string tex, std::string inside); /** * Adds a Mob to the world with the specified type and coordinates. @@ -346,7 +345,7 @@ public: * upon object interaction. */ - void addObject(/*ITEM_ID id*/std::string in,const char *pickupDialog, float x, float y); + void addObject(std::string in, const char *pickupDialog, float x, float y); /** * Adds a particle to the world with the specified coordinates, dimensions, @@ -479,8 +478,6 @@ public: class Arena : public World { private: - //vec2 pxy; - //World *exit; Mob *mmob; public: Arena(World *leave,Player *p,Mob *m); @@ -117,7 +117,6 @@ std::mutex mtx; std::condition_variable cv; ThreadPool pool(10); - /* * loops is used for texture animation. It is believed to be passed to entity * draw functions, although it may be externally referenced instead. diff --git a/src/Texture.cpp b/src/Texture.cpp index 1aaadf6..731a1d6 100644 --- a/src/Texture.cpp +++ b/src/Texture.cpp @@ -1,5 +1,7 @@ +#include <algorithm> +#include <string> + #include <Texture.h> -#include <string.h> /** * A structure for keeping track of loaded textures. @@ -173,32 +175,30 @@ namespace Texture{ Texturec::Texturec(uint amt, ...){ va_list fNames; texState = 0; - image = new GLuint[amt]; va_start(fNames, amt); - for(unsigned int i = 0; i < amt; i++){ - image[i] = Texture::loadTexture(va_arg(fNames, char *)); - } + for(unsigned int i = 0; i < amt; i++) + image.push_back( Texture::loadTexture(va_arg(fNames, char *)) ); va_end(fNames); } +Texturec::Texturec( std::initializer_list<std::string> l ) +{ + texState = 0; + std::for_each( l.begin(), l.end(), [&](std::string s){ image.push_back( Texture::loadTexture( s ) ); }); +} + Texturec::Texturec(std::vector<std::string>v){ texState = 0; - image = new GLuint[v.size()]; - for(unsigned int i = 0; i < v.size(); i++){ - image[i] = Texture::loadTexture(v[i].c_str()); - } + std::for_each( v.begin(), v.end(), [&](std::string s){ image.push_back( Texture::loadTexture( s ) ); }); } Texturec::Texturec(uint amt,const char **paths){ texState = 0; - image = new GLuint[amt]; - for(unsigned int i = 0; i < amt; i++){ - image[i] = Texture::loadTexture(paths[i]); - } + for(unsigned int i = 0; i < amt; i++) + image.push_back( Texture::loadTexture(paths[i]) ); } Texturec::~Texturec(){ - delete[] image; } void Texturec::bind(unsigned int bn){ diff --git a/src/entities.cpp b/src/entities.cpp index 1c364a4..4291b0a 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -179,10 +179,9 @@ Structures::Structures(){ //sets the structure type } Structures::~Structures(){ delete tex; + if(name) delete[] name; - if(inside) - delete[] inside; } Mob::Mob(int sub){ @@ -505,16 +504,20 @@ unsigned int Structures::spawn(BUILD_SUB sub, float x, float y){ */ //unsigned int tempN = (getRand() % 5 + 2); + + if ( textureLoc.empty() ) + textureLoc = inWorld->sTexLoc[sub]; + switch(sub){ case STALL_MARKET: - tex = new Texturec(1, textureLoc ? textureLoc : inWorld->sTexLoc[sub].c_str()); - dim = Texture::imageDim(textureLoc ? textureLoc : inWorld->sTexLoc[sub].c_str()); + tex = new Texturec({ textureLoc }); + dim = Texture::imageDim( textureLoc ); width = dim.x; height = dim.y; break; default: - tex = new Texturec(1, textureLoc ? textureLoc : inWorld->sTexLoc[sub].c_str()); - dim = Texture::imageDim(textureLoc ? textureLoc : inWorld->sTexLoc[sub].c_str()); + tex = new Texturec({ textureLoc }); + dim = Texture::imageDim( textureLoc ); width = dim.x; height = dim.y; inv = NULL; diff --git a/src/tinyxml2.cpp b/src/tinyxml2.cpp index c4ea7cd..6198418 100755 --- a/src/tinyxml2.cpp +++ b/src/tinyxml2.cpp @@ -1400,6 +1400,18 @@ const XMLAttribute* XMLElement::FindAttribute( const char* name ) const return 0;
}
+std::string XMLElement::StrAttribute( const char* name, const char* value ) const
+{
+ std::string str;
+ const XMLAttribute* a = FindAttribute( name );
+ if ( a ) {
+ if ( !value || XMLUtil::StringEqual( a->Value(), value )) {
+ str = a->Value();
+ return str;
+ }
+ }
+ return str;
+}
const char* XMLElement::Attribute( const char* name, const char* value ) const
{
@@ -1413,7 +1425,6 @@ const char* XMLElement::Attribute( const char* name, const char* value ) const return 0;
}
-
const char* XMLElement::GetText() const
{
if ( FirstChild() && FirstChild()->ToText() ) {
diff --git a/src/world.cpp b/src/world.cpp index 80ddc5a..a9e0fd9 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -1,3 +1,5 @@ +#include <algorithm> + #include <world.h> #include <ui.h> @@ -149,9 +151,7 @@ deleteEntities( void ) mob.pop_back(); } - while(!merchant.empty()){ - merchant.pop_back(); - } + merchant.clear(); while(!npc.empty()){ delete npc.back(); npc.pop_back(); @@ -170,19 +170,13 @@ deleteEntities( void ) } // clear entity array - while ( !entity.empty() ) { - entity.pop_back(); - } + entity.clear(); // free particles - while ( !particles.empty() ) { - delete particles.back(); - particles.pop_back(); - } + particles.clear(); // clear light array - while ( !light.empty() ) - light.pop_back(); + light.clear(); // free villages while ( !village.empty() ) { @@ -311,21 +305,17 @@ update( Player *p, unsigned int delta ) } // iterate through particles - for(unsigned int i=0;i<particles.size();i++){ - if(particles[i]->kill(deltaTime)){ - delete particles[i]; - particles.erase(particles.begin()+i); - }else if(particles[i]->canMove){ - particles[i]->loc.y += particles[i]->vely * deltaTime; - particles[i]->loc.x += particles[i]->velx * deltaTime; - - for(auto &b : build){ - if(b->bsubtype==FOUNTAIN){ - if(particles[i]->loc.x >= b->loc.x && particles[i]->loc.x <= b->loc.x + b->width){ - if(particles[i]->loc.y <= b->loc.y + b->height * .25){ - delete particles[i]; - particles.erase(particles.begin()+i); - } + particles.erase( std::remove_if( particles.begin(), particles.end(), [&delta](Particles part){ return part.kill( delta ); }), particles.end()); + for ( auto part = particles.begin(); part != particles.end(); part++ ) { + if ( (*part).canMove ) { + (*part).loc.y += (*part).vely * delta; + (*part).loc.x += (*part).velx * delta; + + for ( auto &b : build ) { + if ( b->bsubtype == FOUNTAIN ) { + if ( (*part).loc.x >= b->loc.x && (*part).loc.x <= b->loc.x + b->width ) { + if ( (*part).loc.y <= b->loc.y + b->height * .25) + particles.erase( part ); } } } @@ -475,7 +465,7 @@ draw( Player *p ) } glEnd(); - for ( i = 0; i < 4; i++ ) { + for ( i = 4; i--; ) { bgTex->bindNext(); safeSetColorA( bgDraw[i][0] - shadeBackground, bgDraw[i][0] - shadeBackground, bgDraw[i][0] - shadeBackground, bgDraw[i][1] ); @@ -513,11 +503,8 @@ draw( Player *p ) iEnd = GROUND_HILLINESS; // draw particles and buildings - - for ( auto &part : particles ) { - if ( part->behind ) - part->draw(); - } + + std::for_each( particles.begin(), particles.end(), [](Particles part) { if ( part.behind ) part.draw(); }); for ( auto &b : build ) b->draw(); @@ -640,10 +627,7 @@ draw( Player *p ) * Draw remaining entities. */ - for ( auto &part : particles ) { - if( !part->behind ) - part->draw(); - } + std::for_each( particles.begin(), particles.end(), [](Particles part) { if ( !part.behind ) part.draw(); }); for ( auto &n : npc ) n->draw(); @@ -757,7 +741,8 @@ singleDetect( Entity *e ) if(e->alive){ - if(e->type == MOBT && Mobp(e)->subtype == MS_TRIGGER)return; + if ( e->type == MOBT && Mobp(e)->subtype == MS_TRIGGER ) + return; /* * Calculate the line that this entity is currently standing on. @@ -825,17 +810,18 @@ detect( Player *p ) for ( auto &part : particles ) { int l; unsigned int i; - l=(part->loc.x + part->width / 2 - worldStart) / HLINE; + l=(part.loc.x + part.width / 2 - worldStart) / HLINE; if(l < 0) l=0; i = l; if(i > lineCount-1) i=lineCount-1; - if(part->loc.y < worldData[i].groundHeight){ - part->loc.y = worldData[i].groundHeight; - part->vely = 0; - part->velx = 0; - part->canMove = false; + if(part.loc.y < worldData[i].groundHeight){ + part.loc.y = worldData[i].groundHeight; + part.vely = 0; + part.velx = 0; + part.canMove = false; }else{ - if(part->gravity && part->vely > -2)part->vely-=.003 * deltaTime; + if(part.gravity && part.vely > -2) + part.vely-=.003 * deltaTime; } } for(auto &b : build){ @@ -851,14 +837,14 @@ detect( Player *p ) {0,0,255}, 2500); - particles.back()->fountain = true; + particles.back().fountain = true; } break; case FIRE_PIT: for(int r = 0; r < (rand()%20)+10;r++){ addParticle(rand()%(int)(b->width/2) + b->loc.x+b->width/4, b->loc.y+3*HLINE, HLINE, HLINE, rand()%2 == 0?-(rand()%3)*.01:(rand()%3)*.01,((4+rand()%6)*.005), {255,0,0}, 400); - particles.back()->gravity = false; - particles.back()->behind = true; + particles.back().gravity = false; + particles.back().behind = true; } break; default: break; @@ -882,36 +868,16 @@ detect( Player *p ) }*/ } -void World::addStructure(BUILD_SUB sub, float x,float y, char *tex, const char *inside){ +void World::addStructure(BUILD_SUB sub, float x,float y, std::string tex, std::string inside){ build.push_back(new Structures()); build.back()->inWorld = this; build.back()->textureLoc = tex; build.back()->spawn(sub,x,y); - - if(inside) - strcpy((build.back()->inside = new char[1 + strlen(inside)]),inside); - else - strcpy((build.back()->inside = new char[1]),"\0"); + + build.back()->inside = inside; entity.push_back(build.back()); } - -/*void World::addVillage(int bCount, int npcMin, int npcMax,const char *inside){ - std::cout << npcMin << ", " << npcMax << std::endl; - //int xwasd; - for(int i = 0; i < bCount; i++){ - addStructure(HOUSE,x_start + (i * 300),100,inside); - std::cout<<"1\n"; - HERE: - xwasd = (rand()%(int)x+1000*HLINE); - for(auto &bu : build){ - if(xwasd > bu->loc.x && xwasd < bu->loc.x+bu->width)goto HERE; - } - std::cout<<"2\n"; - addStructure(t,HOUSE,xwasd,y,inside); - std::cout<<"3\n"; - } -}*/ void World::addMob(int t,float x,float y){ mob.push_back(new Mob(t)); @@ -950,9 +916,11 @@ void World::addObject(/*ITEM_ID i*/std::string in,const char *p, float x, float entity.push_back(object.back()); } -void World::addParticle(float x, float y, float w, float h, float vx, float vy, Color color, int d){ - particles.push_back(new Particles(x,y,w,h,vx,vy,color,d)); - particles.back()->canMove = true; +void World:: +addParticle( float x, float y, float w, float h, float vx, float vy, Color color, int d ) +{ + particles.emplace_back( x, y, w, h, vx, vy, color, d ); + particles.back().canMove = true; } void World::addLight(vec2 loc, Color color){ @@ -1037,7 +1005,7 @@ goInsideStructure( Player *p ) p->loc.x + p->width < b->loc.x + b->width ){ inside.push_back((std::string)(currentXML.c_str() + 4)); - tmp = loadWorldFromXML(b->inside); + tmp = loadWorldFromXML(b->inside.c_str()); ui::toggleBlackFast(); ui::waitForCover(); @@ -1050,7 +1018,7 @@ goInsideStructure( Player *p ) strcpy((current = new char[strlen((const char *)(currentXML.c_str() + 4)) + 1]),(const char *)(currentXML.c_str() + 4)); tmp = loadWorldFromXML(inside.back().c_str()); for(auto &b : tmp->build){ - if(!strcmp(current,b->inside)){ + if(!strcmp(current,b->inside.c_str())){ inside.pop_back(); ui::toggleBlackFast(); @@ -1269,8 +1237,12 @@ void IndoorWorld::draw(Player *p){ * Draw all entities. */ - for(auto &part : particles) part->draw(); - for(auto &e : entity) e->draw(); + for ( auto &part : particles ) + part.draw(); + + for ( auto &e : entity ) + e->draw(); + p->draw(); } @@ -1324,18 +1296,23 @@ World *Arena::exitArena(Player *p){ #include <tinyxml2.h> using namespace tinyxml2; -std::string currentXML = "\0"; +std::string currentXML; extern World *currentWorld; World *loadWorldFromXML(const char *path){ - if ( currentXML != "\0" ) + if ( !currentXML.empty() ) currentWorld->save(); return loadWorldFromXMLNoSave(path); } -World *loadWorldFromXMLNoSave(const char *path){ +/** + * Loads a world from the given XML file. + */ + +World * +loadWorldFromXMLNoSave( const char *path ) { XMLDocument xml; XMLElement *wxml; XMLElement *vil; @@ -1451,13 +1428,12 @@ World *loadWorldFromXMLNoSave(const char *path){ * READS DATA ABOUT STRUCTURE CONTAINED IN VILLAGE */ - if(!strcmp(name,"structure")){ - ptr = vil->Attribute("inside"); + if(!strcmp(name,"structure")){ tmp->addStructure((BUILD_SUB)vil->UnsignedAttribute("type"), vil->QueryFloatAttribute("x", &spawnx) != XML_NO_ERROR ? randx : spawnx, 100, - (char*)vil->Attribute("texture"), - ptr); + vil->StrAttribute("texture"), + vil->StrAttribute("inside")); }else if(!strcmp(name, "stall")){ if(!strcmp(vil->Attribute("type"),"market")){ @@ -1466,8 +1442,8 @@ World *loadWorldFromXMLNoSave(const char *path){ vil->QueryFloatAttribute("x", &spawnx) != XML_NO_ERROR ? randx : spawnx, 100, - (char*)vil->Attribute("texture"), - ptr); + vil->StrAttribute("texture"), + vil->StrAttribute("inside")); tmp->addMerchant(0,100); if(!strcmp(name,"buy")){ std::cout << "Buying"; @@ -1482,8 +1458,8 @@ World *loadWorldFromXMLNoSave(const char *path){ vil->QueryFloatAttribute("x", &spawnx) != XML_NO_ERROR ? randx : spawnx, 100, - (char*)vil->Attribute("texture"), - ptr); + vil->StrAttribute("texture"), + vil->StrAttribute("inside")); } } vptr->build.push_back(tmp->build.back()); |