]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
efficiency stuffs
authorClyne Sullivan <tullivan99@gmail.com>
Thu, 3 Mar 2016 14:26:06 +0000 (09:26 -0500)
committerClyne Sullivan <tullivan99@gmail.com>
Thu, 3 Mar 2016 14:26:06 +0000 (09:26 -0500)
Changelog
include/Texture.h
include/entities.h
include/tinyxml2.h
include/world.h
main.cpp
src/Texture.cpp
src/entities.cpp
src/tinyxml2.cpp
src/world.cpp

index 0384181d8b0aac5e9949e6a3e85a25e216898c87..f6309bf4a2bc215aeb0a866fc54b8b0ff57b964a 100644 (file)
--- a/Changelog
+++ b/Changelog
 
        - 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
index 659c32d185068bcfc155df66198411c32bd50bd9..7201a4c250da6749a93294baf33931a51f2770a0 100644 (file)
@@ -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.
index 545f0a4dfc8cb5e52dc2d33a9b677bacd51fe5f1..3b8a19bb0a93c50bbf640902db88d16e73ca07b6 100644 (file)
@@ -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();
index fb7464a7790dc1d78f232f11a0207e086b9635b4..4282642308dda37c50052e801bb3379065b14dc6 100755 (executable)
@@ -38,6 +38,8 @@ distribution.
 #   include <cstring>\r
 #endif\r
 \r
+#include <string>\r
+\r
 /*\r
    TODO: intern strings instead of allocation.\r
 */\r
@@ -1187,6 +1189,11 @@ public:
     */\r
     const char* Attribute( const char* name, const char* value=0 ) const;\r
 \r
+       /** Functions the same as Attribute(), but returns the result\r
+           as a std::string.\r
+       */\r
+       std::string StrAttribute( const char* name, const char* value=0 ) const;\r
+\r
     /** Given an attribute name, IntAttribute() returns the value\r
        of the attribute interpreted as an integer. 0 will be\r
        returned if there is an error. For a method with error\r
index b3d1071760c6510b090ba3e2877ca3f522267f6d..7b9383a2c90cc025a1f035b971eefc155a783618 100644 (file)
@@ -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);
index abc4f3cc23ba3745f9842d64cb690dd80b8256c9..3769379e15151be9f22007713b4257a6197a7cf3 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -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.
index 1aaadf6944bf373333d2117bfb5cac6f2b9a57e2..731a1d66ae7a1f1dc5e740b85fa4cd0866ef6bfa 100644 (file)
@@ -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){
index 1c364a46e6e40e0edccf2f48fd96b046d9d300a0..4291b0a127c6fdbd0e6ccd8ab4406af61371ee46 100644 (file)
@@ -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;
index c4ea7cd5e32e8bc445801dd5ea297fd32eb1e6d4..6198418f0734a35f6f8213af5d24b7c24ec9dc11 100755 (executable)
@@ -1400,6 +1400,18 @@ const XMLAttribute* XMLElement::FindAttribute( const char* name ) const
     return 0;\r
 }\r
 \r
+std::string XMLElement::StrAttribute( const char* name, const char* value ) const\r
+{\r
+       std::string str;\r
+    const XMLAttribute* a = FindAttribute( name );\r
+    if ( a ) {\r
+               if ( !value || XMLUtil::StringEqual( a->Value(), value )) {\r
+                       str = a->Value();\r
+                       return str;\r
+               }\r
+       }\r
+    return str;\r
+}\r
 \r
 const char* XMLElement::Attribute( const char* name, const char* value ) const\r
 {\r
@@ -1413,7 +1425,6 @@ const char* XMLElement::Attribute( const char* name, const char* value ) const
     return 0;\r
 }\r
 \r
-\r
 const char* XMLElement::GetText() const\r
 {\r
     if ( FirstChild() && FirstChild()->ToText() ) {\r
index 80ddc5a27a0ae8484173ab8aa2036cc45b685cc7..a9e0fd98e78cdf50820f8ef5afc75d44a0f3c726 100644 (file)
@@ -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());