]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
bug fixes
authorClyne Sullivan <tullivan99@gmail.com>
Wed, 28 Oct 2015 12:48:56 +0000 (08:48 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Wed, 28 Oct 2015 12:48:56 +0000 (08:48 -0400)
Changelog
assets/mountain.png
include/entities.h
main.cpp
src/entities.cpp
src/gameplay.cpp
src/world.cpp
xcf/mountain.xcf

index 4c4d82300a675adbf4594fe924eaf72fa6acafdb..4af9d0cc99e11f5183df475dc6ba9c3cf0738900 100644 (file)
--- a/Changelog
+++ b/Changelog
        - gained knowledge on sprite creation
        - created tree and mountain sprites
        - created a decent bird AI
+
+10/28/2015:
+===========
+
+       - fixed world drawing bug
+       - fixed segfault with entering buildings
+       - found bug with npc quest preloading
+       - documented more of world.cpp
+       - improved background textures/parallax stuff
+       - 
index 12c0f64000c912aa6456ac2ead339796ddc93371..7039174f7350359fc4ff72070e3915c1855eb738 100644 (file)
Binary files a/assets/mountain.png and b/assets/mountain.png differ
index 94febcf0e6c6c3794a29875a702dbe0f91f0adcb..a9375c31d92bcf5014aaa06585dcf12e032e2fde 100644 (file)
@@ -69,7 +69,7 @@ public:
 
        void spawn(float, float);
        void draw(void);
-       virtual void wander(int, vec2*){}
+       virtual void wander(int){}
        void getName();
        virtual void interact(){}
        int ticksToUse; //The variable for deciding how long an entity should do a certain task
@@ -89,14 +89,16 @@ public:
        NPC();
        void addAIFunc(int (*func)(NPC *),bool preload);
        void interact();
-       void wander(int, vec2*);
+       void wander(int);
 };
+
 class Structures : public Entity{
 public:
        void *inside;
        Structures();
        unsigned int spawn(_TYPE, float, float);
 };
+
 class Mob : public Entity{
 public:
        float init_y;
index 002fc8390124524bee835f9de2e4c5a9f8f4d3c5..4fb5a008877035044c35f873ed28620a2537382f 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -62,7 +62,7 @@ static GLuint  bgImage, bgTreesFirst;
  * 
 */
 
-bool gameRunning = true;
+bool gameRunning;
 
 /*
  *     currentWorld    -       This is a pointer to the current world that the player
@@ -173,6 +173,7 @@ unsigned int millis(void){
  * MAIN ************************************************************************
  *******************************************************************************/
 int main(int argc, char *argv[]){
+       gameRunning=false;
        
        /*
         *      (Attempt to) Initialize SDL libraries so that we can use SDL facilities and eventually
@@ -383,6 +384,7 @@ int main(int argc, char *argv[]){
        ****     GAMELOOP      ****
        **************************/
        
+       gameRunning=true;
        while(gameRunning){
                mainLoop();
        }
@@ -558,7 +560,7 @@ void render(){
                glTexCoord2i(0,0);glVertex2i(-SCREEN_WIDTH*2,SCREEN_HEIGHT);
        glEnd();
 
-       int base = 50 - (int)worldGetYBase(currentWorld);
+       int base = 40 - (int)worldGetYBase(currentWorld);
 
        glBindTexture(GL_TEXTURE_2D, bgTreesFirst);
 
@@ -729,9 +731,9 @@ void logic(){
                                 *      that the NPC doesn't move when it talks to the player.
                                 * 
                                */
-                       
+                               
                                if(entity[i]->canMove)
-                                       NPCp(entity[i])->wander((rand() % 120 + 30), &entity[i]->vel);
+                                       NPCp(entity[i])->wander((rand() % 120 + 30));
                                
                                /*
                                 *      Don't bother handling the NPC if another has already been handled.
index 0479d5e6cd855224709a62c46546bf71a04fd355..acc45bdcd1cdb5ee983b7bd68905fc726d1aba11 100644 (file)
@@ -227,28 +227,32 @@ void Player::interact(){ //the function that will cause the player to search for
  *                                                     the memory address passed to it is directly modified.
 */
 
-void NPC::wander(int timeRun, vec2 *v){ //this makes the entites wander about
+void NPC::wander(int timeRun){
+       
        /*
         *      Direction is the variable that decides what direction the entity will travel in
         *      the value is either -1, 0, or 1. -1 being left, 0 means that the npc will stay still
         *      and a value of 1 makes the entity move to the right
        */
-       static int direction;   //variable to decide what direction the entity moves
+       
+       static int direction;
+       
        /*
         *      Ticks to use is a variable in the entity class that counts the total ticks that need to be used
         *
         *      This loop only runs when ticksToUse is 0, this means that the speed, direction, etc... Will be
         *      calculated only after the npc has finished his current walking state
        */
+       
        if(ticksToUse == 0){
                ticksToUse = timeRun;
-               v->x = .008*HLINE;      //sets the inital velocity of the entity
+               vel.x = .008*HLINE;                                     //sets the inital velocity of the entity
                direction = (getRand() % 3 - 1);        //sets the direction to either -1, 0, 1
                                                                                        //this lets the entity move left, right, or stay still
                if(direction==0)ticksToUse*=2;
-               v->x *= direction;      //changes the velocity based off of the direction
+               vel.x *= direction;                                     //changes the velocity based off of the direction
        }
-       ticksToUse--; //removes one off of the entities timer
+       ticksToUse--;                                                    //removes one off of the entities timer
 }
 
 std::vector<int (*)(NPC *)> AIpreload; // A dynamic array of AI functions that are being preloaded
index d3cc4f463b2bffeef08f31da170d0a139b0c6d6b..9e0cc99d3360ea4d6133ed07544f014f34b2c890 100644 (file)
@@ -20,9 +20,10 @@ int compTestQuest(NPC *speaker){
 }
 
 int giveTestQuest(NPC *speaker){
+       unsigned int i;
        ui::dialogBox(speaker->name,"Here, have a quest!");
        player->qh.assign("Test");
-       NPCp(entity[2])->addAIFunc(compTestQuest,true);
+       npc[1]->addAIFunc(compTestQuest,true);
        return 0;
 }
 
@@ -41,7 +42,6 @@ void initEverything(void){
        */
        
        test->addLayer(400);
-       //test->addLayer(100);
        
        test->addPlatform(150,100,100,10);
 
@@ -91,7 +91,7 @@ void initEverything(void){
        /*
         *      Link all the entities that were just created to the initial world, and setup a test AI function. 
        */
-       NPCp(entity[1])->addAIFunc(giveTestQuest,false);
+       npc[0]->addAIFunc(giveTestQuest,false);
        
        for(i=0;i<entity.size();i++){
                entity[i]->inWorld=currentWorld;
index e1d05ab63b2025c2db822307b5f9b59229d9976d..168ac4c4d1e9a469bb448d6beeb611ffe0647169 100644 (file)
@@ -15,7 +15,8 @@
 
 #define INDOOR_FLOOR_HEIGHT 100 // Defines how high the base floor of an IndoorWorld should be
 
-extern std::vector<Entity *>entity;
+extern std::vector<Entity        *> entity;
+extern std::vector<Structures *> build;
 
 void safeSetColor(int r,int g,int b){  // safeSetColor() is an alternative to directly using glColor3ub() to set
        if(r>255)r=255;                                         // the color for OpenGL drawing. safeSetColor() checks for values that are
@@ -79,8 +80,8 @@ void World::generate(unsigned int width){     // Generates the world and sets all va
                */
                
                line[i].y=rand() % 8 - 4 + line[i-GEN_INC].y;   // Add +/- 4 to the previous line
-                        if(line[i].y < 60)line[i].y =  60;                     // Minimum bound
-               else if(line[i].y > 90)line[i].y =  90;                 // Maximum bound
+                        if(line[i].y < 40)line[i].y =  40;                     // Minimum bound
+               else if(line[i].y > 70)line[i].y =  70;                 // Maximum bound
                
        }
        
@@ -198,14 +199,16 @@ LOOP2:
         *      the 'for' loop below that draws the layer.
        */
 
-       v_offset=(p->loc.x - current->x_start) / HLINE;
+       v_offset=(p->loc.x + p->width / 2 - current->x_start) / HLINE;
        
        // is -> i start
        
-       is=v_offset - SCREEN_WIDTH / (yoff / (DRAW_Y_OFFSET / 2));
+       is=v_offset - (SCREEN_WIDTH / 2 / HLINE) - GEN_INC;
        if(is<0)is=0;                                                                                           // Minimum bound
-
-       ie=v_offset + SCREEN_WIDTH / (yoff / (DRAW_Y_OFFSET / 2));
+       
+       // ie -> i end
+       
+       ie=v_offset + (SCREEN_WIDTH / 2 / HLINE) + GEN_INC; 
        if(ie>current->lineCount)ie=current->lineCount;                         // Maximum bound
        
        /*
@@ -222,12 +225,13 @@ LOOP2:
        //shade*=-1;
        
        /*
-        *      Draw entities in the current layer if we're on the one we started at.
+        *      Draw structures in the current layer if we're on the one we started at. We draw
+        *      structures behind the dirt/grass so that the buildings corners don't stick out.
        */
        
        if(current==this){
                for(i=0;i<entity.size();i++){
-                       if(entity[i]->inWorld==this)
+                       if(entity[i]->inWorld==this && entity[i]->type == STRUCTURET)
                                entity[i]->draw();
                }
        }
@@ -248,48 +252,133 @@ LOOP2:
                }
        glEnd();
        
+       /*
+        *      If we're drawing the closest/last world, handle and draw the player and entities in
+        *      the world.
+       */
+       
        if(current==this){
-               int ph;
-               ph=(p->loc.x+p->width/2-x_start)/HLINE; // Calculate what line the player is currently on
-               for(i=0;i<lineCount-GEN_INC;i++){
-                       if(p->ground==1&&i<ph+6&&i>ph-6)cline[i].gs=false;
-                       else cline[i].gs=true;
+               
+               /*
+                *      Calculate the line that the player is on
+               */
+               
+               int ph = (p->loc.x + p->width / 2 - x_start) / HLINE;
+               
+               /*
+                *      If the player is on the ground, flatten the grass where the player is standing
+                *      by setting line.gs to false.
+               */
+               
+               if(p->ground){
+                       for(i=0;i<lineCount-GEN_INC;i++){
+                               if(i < ph + 6 && 
+                                  i > ph - 6 )
+                                       cline[i].gs=false;
+                               else cline[i].gs=true;
+                       }
                }
+               
+               /*
+                *      Draw the player.
+               */
+               
                p->draw();
+               
+               /*
+                *      Draw non-structure entities.
+               */
+               
+               if(current==this){
+                       for(i=0;i<entity.size();i++){
+                               if(entity[i]->inWorld==this && entity[i]->type != STRUCTURET)
+                                       entity[i]->draw();
+                       }
+               }
        }
+       
+       /*
+        *      Draw grass on every line.
+       */
+       
        float cgh[2];
        glBegin(GL_QUADS);
                for(i=is;i<ie-GEN_INC;i++){
-                       memcpy(cgh,cline[i].gh,2*sizeof(float));
+                       
+                       /*
+                        *      Load the current line's grass values
+                       */
+                       
+                       if(cline[i].y)memcpy(cgh,cline[i].gh,2*sizeof(float));
+                       else              memset(cgh,0                  ,2*sizeof(float));
+                       
+                       
+                       
+                       /*
+                        *      Flatten the grass if the player is standing on it.
+                       */
+                       
                        if(!cline[i].gs){
                                cgh[0]/=4;
                                cgh[1]/=4;
                        }
+                       
+                       /*
+                        *      Actually draw the grass.
+                       */
+                       
                        cline[i].y+=(yoff-DRAW_Y_OFFSET);
+                       
                        safeSetColor(shade,150+shade,shade);
+                       
                        glVertex2i(cx_start+i*HLINE        ,cline[i].y+cgh[0]);
                        glVertex2i(cx_start+i*HLINE+HLINE/2,cline[i].y+cgh[0]);
                        glVertex2i(cx_start+i*HLINE+HLINE/2,cline[i].y-GRASS_HEIGHT);
                        glVertex2i(cx_start+i*HLINE                ,cline[i].y-GRASS_HEIGHT);
+                       
                        glVertex2i(cx_start+i*HLINE+HLINE/2,cline[i].y+cgh[1]);
                        glVertex2i(cx_start+i*HLINE+HLINE  ,cline[i].y+cgh[1]);
                        glVertex2i(cx_start+i*HLINE+HLINE  ,cline[i].y-GRASS_HEIGHT);
                        glVertex2i(cx_start+i*HLINE+HLINE/2,cline[i].y-GRASS_HEIGHT);
+                       
                        cline[i].y-=(yoff-DRAW_Y_OFFSET);
                }
        glEnd();
+       
+       /*
+        *      Restore the inverted shading if it was inverted above.
+       */
+       
        //shade*=-1;
+       
+       /*
+        *      Draw platforms...
+       */
+       
        safeSetColor(255+shade*2,0+shade,0+shade);
+       
        for(i=0;i<current->platform.size();i++){
-               glRectf(current->platform[i].p1.x,current->platform[i].p1.y+yoff-DRAW_Y_OFFSET,
-                               current->platform[i].p2.x,current->platform[i].p2.y+yoff-DRAW_Y_OFFSET);
+               glRectf(current->platform[i].p1.x, current->platform[i].p1.y + yoff - DRAW_Y_OFFSET,
+                               current->platform[i].p2.x, current->platform[i].p2.y + yoff - DRAW_Y_OFFSET);
        }
-       if(current->infront){                   // If there's a world in front of the one that was just drawn
-               yoff-=DRAW_Y_OFFSET;            // draw it as well.
-               shade-=DRAW_SHADE;
+       
+       /*
+        *      Draw the next closest world if it exists.
+       */
+       
+       if(current->infront){
+               yoff  -= DRAW_Y_OFFSET;
+               shade -= DRAW_SHADE;
+               
                current=current->infront;
                goto LOOP2;
-       }else{                                                  // Otherwise reset static values and return
+               
+       }else{
+               
+               /*
+                *      If finished, reset the yoff and shade variables for the next call.
+               */
+               
                yoff=DRAW_Y_OFFSET;
                shade=0;
        }
@@ -470,17 +559,16 @@ void World::addPlatform(float x,float y,float w,float h){
 
 World *World::goInsideStructure(Player *p){
        unsigned int i;
-       for(i=0;i<entity.size();i++){
-               if(entity[i]->type==-1){
-                       if(entity[i]->inWorld==this){
-                               if(p->loc.x>entity[i]->loc.x&&p->loc.x+p->width<entity[i]->loc.x+entity[i]->width){
-                                       return (World *)((Structures *)entity[i])->inside;
-                               }
-                       }else if(((Structures *)entity[i])->inside==this){
-                               p->loc.x=entity[i]->loc.x+entity[i]->width/2-p->width/2;
-                               p->loc.y=entity[i]->loc.y+HLINE;
-                               return (World *)entity[i]->inWorld;
+       for(i=0;i<build.size();i++){
+               if(build[i]->inWorld==this){
+                       if(p->loc.x                        > build[i]->loc.x &&
+                          p->loc.x + p->width < build[i]->loc.x + build[i]->width){
+                               return (World *)build[i]->inside;
                        }
+               }else if(build[i]->inside==this){
+                       p->loc.x=build[i]->loc.x + build[i]->width / 2 - p->width / 2;
+                       p->loc.y=build[i]->loc.y + HLINE;
+                       return (World *)build[i]->inWorld;
                }
        }
        return this;
@@ -493,8 +581,6 @@ void World::addHole(unsigned int start,unsigned int end){
        }
 }
 
-
-
 IndoorWorld::IndoorWorld(void){
 }
 
@@ -519,6 +605,7 @@ void IndoorWorld::generate(unsigned int width){             // Generates a flat area of wid
 
 void IndoorWorld::draw(Player *p){
        int i,ie,v_offset;
+       
        v_offset=(p->loc.x-x_start)/HLINE;                                      // Calculate the player's offset in the array 'line' using the player's location 'vec'
        i=v_offset-SCREEN_WIDTH/2;                                                      // um
        if(i<0)i=0;                                                                                     // If the player is past the start of that world 'i' should start at the beginning
@@ -535,7 +622,7 @@ void IndoorWorld::draw(Player *p){
                        glVertex2i(x_start+i*HLINE              ,line[i].y-50);
                }
        glEnd();
-       for(i=0;i<entity.size()+1;i++){
+       for(i=0;i<entity.size();i++){
                if(entity[i]->inWorld==this)
                        entity[i]->draw();
        }
index 5ef311ee1e0e1c69fc513d8ee33a762cdb7a694a..32fd5b1f43c1f855291870a1985fa15f4b638ab2 100644 (file)
Binary files a/xcf/mountain.xcf and b/xcf/mountain.xcf differ