]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
segfault fixes
authorClyne Sullivan <tullivan99@gmail.com>
Wed, 2 Dec 2015 11:11:35 +0000 (06:11 -0500)
committerClyne Sullivan <tullivan99@gmail.com>
Wed, 2 Dec 2015 11:11:35 +0000 (06:11 -0500)
include/inventory.h
main.cpp
src/gameplay.cpp
src/inventory.cpp
src/ui.cpp
src/world.cpp

index cde8d2a3c869e33670cc6a219718a690c8fb3308..e95195b62cebfcbd33822b784b6ffa13f38fb472 100644 (file)
@@ -58,10 +58,6 @@ public:
        }
 };
 
-static Item item[5]= {
-       #include "../config/items.h"
-};
-
 struct item_t{
        int count;
        ITEM_ID id;
index 422a32976ab5692fd31d960bc74f059de3bb6e74..ffd9ead3b4899bca35491ed8ac12afb25c3d429e 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -483,14 +483,13 @@ void mainLoop(void){
        
        static unsigned int prevTime    = 0,    // Used for timing operations
                                                currentTime = 0,        //
-                                               prevPrevTime= 0;        // shit
-                                               
-       unsigned int i;                                                 // Used for `for` loops
+                                               prevPrevTime= 0;        //
        
        if(!currentTime){                                               // Initialize currentTime if it hasn't been
                currentTime=millis();
                prevPrevTime=currentTime;
        }       
+       
        /*
         *      Update timing values. This is crucial to calling logic and updating the window (basically
         *      the entire game).
@@ -503,7 +502,9 @@ void mainLoop(void){
        /*
         *      Run the logic handler if MSEC_PER_TICK milliseconds have passed.
        */
+       
        ui::handleEvents();
+       
        if(prevPrevTime + MSEC_PER_TICK <= currentTime){
                logic();
                prevPrevTime = currentTime;
@@ -518,6 +519,7 @@ void mainLoop(void){
        /*
         *      Update debug variables if necessary
        */
+       
        if(++debugDiv==20){
                debugDiv=0;
                
@@ -770,9 +772,9 @@ void render(){
        glColor3ub(255,255,255);
 
        glBegin(GL_TRIANGLES);
-               glVertex2i(ui::mouse.x                   ,ui::mouse.y             );
+               glVertex2i(ui::mouse.x                  ,ui::mouse.y              );
                glVertex2i(ui::mouse.x+HLINE*3.5,ui::mouse.y              );
-               glVertex2i(ui::mouse.x                   ,ui::mouse.y-HLINE*3.5);
+               glVertex2i(ui::mouse.x                  ,ui::mouse.y-HLINE*3.5);
        glEnd();
 
        /*
@@ -818,6 +820,7 @@ void logic(){
        /*
         *      Handle user input (keyboard & mouse).
        */
+       
        //ui::handleEvents();
 
        /*
@@ -927,7 +930,6 @@ void logic(){
                        }
                }
        }
-       unsigned int i = 0;
        for(auto &o : currentWorld->object){
                if(o->alive){
                        if(ui::mouse.x >= o->loc.x                              &&
@@ -948,10 +950,6 @@ void logic(){
                                }
                        }
                }
-               if(!(o->alive)){
-                       currentWorld->object.erase(currentWorld->object.begin()+i);
-               }
-               i++;
        }
 
        /*
index b7eab727076e6aa22566c3c562a5fa0205351d35..cc9ae6a178b877ec7b3d2aa947e08dd1d7fa7dca 100644 (file)
@@ -16,11 +16,12 @@ int giveTestQuest(NPC *speaker){
        unsigned char i;
        
        ui::dialogBox(speaker->name,":Yes:No","Here, have a quest!");
+       
        ui::waitForDialog();
        
        if(ui::dialogOptChosen == 1){
-       
-               ui::dialogBox(speaker->name,NULL,"Have a good day! :)");
+               
+               ui::dialogBox(speaker->name,"","Have a good day! :)");
                ui::waitForDialog();
 
                player->qh.assign("Test");
index 28612ae531032a7cd5f41f7aa2c9e5ebb0e1d291..2af4de37446ec0d41cc8b5d106467fc68708c8da 100644 (file)
@@ -7,6 +7,10 @@
 extern Player *player;
 extern GLuint invUI;
 
+static Item item[5]= {
+       #include "../config/items.h"
+};
+
 void itemDraw(Player *p,ITEM_ID id);
 
 char *getItemTexturePath(ITEM_ID id){
@@ -27,7 +31,7 @@ Item::Item(ITEM_ID i, const char *n, ITEM_TYPE t, float w, float h, int m, const
        strcpy(name,n);
        strcpy(textureLoc,tl);
 
-       tex= new Texturec(1,textureLoc);
+       //tex= new Texturec(1,textureLoc);
 }
 
 Inventory::Inventory(unsigned int s){
@@ -52,13 +56,14 @@ int Inventory::addItem(ITEM_ID id,unsigned char count){
        os++;
 
 
-       #ifdef DEBUG
-       DEBUG_printf("Gave player %u more %s(s)(%d).\n",count,item[id].name,item[id].id);
-       #endif // DEBUG
+#ifdef DEBUG
+       DEBUG_printf("Gave player %u more %s(s)(ID: %d).\n",count,item[id].name,item[id].id);
+#endif // DEBUG
 
-       /*#ifdef DEBUG
+/*#ifdef DEBUG
        DEBUG_printf("Failed to add non-existant item with id %u.\n",id);
-       #endif // DEBUG*/
+#endif // DEBUG*/
+
        return 0;
 }
 
index 8d57c36e6b325ce9f47f10f99e3f08f232304be7..a37a97c9fe346d670ea10361e03859061c45a368 100644 (file)
@@ -398,20 +398,30 @@ namespace ui {
                */
                
                while(dialogOptCount){
-                       if(dialogOptText[dialogOptCount])
+                       if(dialogOptText[dialogOptCount]){
                                delete[] dialogOptText[dialogOptCount]; //free(dialogOptText[dialogOptCount]);
+                               dialogOptText[dialogOptCount] = NULL;
+                       }
                        dialogOptCount--;
                };
+
                dialogOptChosen=0;
                dialogOptCount=0;
                
-               soptbuf = new char[strlen(opt)+1];
-               
-               sopt=strtok(soptbuf,":");
-               while(sopt != NULL){
-                       dialogOptText[dialogOptCount] = new char[strlen(sopt)+1];       //(char *)malloc(strlen(sopt));
-                       strcpy(dialogOptText[dialogOptCount++],sopt);
-                       sopt=strtok(NULL,":");
+               if(opt){
+                       
+                       soptbuf = new char[strlen(opt)+1];
+                       strcpy(soptbuf,opt);
+                       
+                       sopt=strtok(soptbuf,":");
+                       while(sopt != NULL){
+                               dialogOptText[dialogOptCount] = new char[strlen(sopt)+1];       //(char *)malloc(strlen(sopt));
+                               strcpy(dialogOptText[dialogOptCount++],sopt);
+                               sopt=strtok(NULL,":");
+                       }
+                       
+                       delete[] soptbuf;
+
                }
                
                /*
index 8067fe7fa106606ac26debcace83bdb68cb445c3..c078726cc62eec064ee71f8781af3801766b9224 100644 (file)
@@ -586,7 +586,8 @@ LOOP2:
 }
 
 void World::singleDetect(Entity *e){
-       unsigned int i,j;
+       int i;
+       unsigned int j;
        
        /*
         *      Kill any dead entities.
@@ -596,33 +597,46 @@ void World::singleDetect(Entity *e){
                
                for(i=0;i<entity.size();i++){
                        if(entity[i]==e){
-                               entity.erase(entity.begin()+i);
                                switch(e->type){
                                case STRUCTURET:
                                        for(j=0;j<build.size();j++){
                                                if(build[j]==e){
+                                                       delete build[j];
                                                        build.erase(build.begin()+j);
-                                                       return;
+                                                       break;
                                                }
                                        }
                                        break;
                                case NPCT:
                                        for(j=0;j<npc.size();j++){
                                                if(npc[j]==e){
+                                                       delete npc[j];
                                                        npc.erase(npc.begin()+j);
-                                                       return;
+                                                       break;
                                                }
                                        }
                                        break;
                                case MOBT:
                                        for(j=0;j<mob.size();j++){
                                                if(mob[j]==e){
+                                                       delete mob[j];
                                                        mob.erase(mob.begin()+j);
-                                                       return;
+                                                       break;
+                                               }
+                                       }
+                                       break;
+                               case OBJECTT:
+                                       for(j=0;j<object.size();j++){
+                                               if(object[j]==e){
+                                                       delete object[j];
+                                                       object.erase(object.begin()+j);
+                                                       break;
                                                }
                                        }
                                        break;
                                }
+                               std::cout<<"Killed an entity..."<<std::endl;
+                               entity.erase(entity.begin()+i);
                                return;
                        }
                }
@@ -646,6 +660,8 @@ void World::singleDetect(Entity *e){
                */
                
                i=(e->loc.x + e->width / 2 - x_start) / HLINE;
+               if(i < 0) i=0;
+               if(i > lineCount-1) i=lineCount-1;
                
                /*
                 *      If the entity is under the world/line, pop it back to the surface.
@@ -672,7 +688,10 @@ void World::singleDetect(Entity *e){
                                
                                do{
                                        e->loc.x+=.001 * e->vel.x>0?-1:1;
+                                       
                                        i=(e->loc.x - e->width / 2 - x_start) / HLINE;
+                                       if(i < 0){ e->alive = false; return; }
+                                       if(i > lineCount-1){ e->alive = false; return; }
                                }while(line[i].y>e->loc.y+ e->height);
                                
                        }