]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
merge
authorClyne Sullivan <tullivan99@gmail.com>
Tue, 26 Jan 2016 12:33:28 +0000 (07:33 -0500)
committerClyne Sullivan <tullivan99@gmail.com>
Tue, 26 Jan 2016 12:33:28 +0000 (07:33 -0500)
Changelog
include/entities.h
include/world.h
main.cpp
src/Texture.cpp
src/entities.cpp
src/ui.cpp
src/world.cpp

index 27a3b44f1553b9e2d7b13b3961b09af264feb7cd..c74cbc22af9c8f11e1d87f1048e1b60b89dee56e 100644 (file)
--- a/Changelog
+++ b/Changelog
 
        ~ A little over 5200 lines of code
 
+1/22/2016:
+==========
+
+       - better player sprites???
+       - fixed major bugs
+       - free'd more resources
+       - documented almost all of world.h
+       - improved settings.xml
+
 1/25/2016:
 ==========
 
        - Game has a menu API
        - Pause Menu
+
index ab339883aa5fd0be77ebc480f343c6e276271c51..28fd7e955e932a8c75be8cbbdb332d1e9d4a4dca 100644 (file)
@@ -226,7 +226,7 @@ public:
        bool questObject = false;
        
        Object();
-       Object(ITEM_ID id, bool qo, const char *pd);
+       Object(ITEM_ID id,const char *pd);
        ~Object();
        
        void reloadTexture(void);
index baa45757f59c9d0da82b08e051a04c5d988db9ac..d7527283542a84a5880c8b4cc8f574ea1775bacf 100644 (file)
@@ -8,9 +8,6 @@
 #ifndef WORLD_H
 #define WORLD_H
 
-#include <ostream>
-#include <istream>
-
 #include <common.h>
 #include <entities.h>
 
@@ -59,7 +56,7 @@ typedef enum {
 typedef enum {
        SUNNY = 0,      /**< Sunny/daytime */
        DARK,           /**< Nighttime */
-       RAIN            /**< Rain (not implemented :) )*/
+       RAIN            /**< Rain (to be implemented)*/
 } WEATHER;
 
 /**
@@ -77,12 +74,12 @@ typedef struct {
  * lines. Dirt color and grass properties are also kept track of here.
  */
 
-struct line_t {
+typedef struct line_t {
        float y;                                /**< Height of this vertical line */
        bool gs;                                /**< Show grass */
        float gh[2];                    /**< Height of glass (2 blades per line) */
        unsigned char color;    /**< Lightness of dirt (brown) */
-} __attribute__ ((packed));
+} line_t;
 
 /**
  * The world class. This class does everything a world should do.
@@ -148,6 +145,11 @@ protected:
         */
        
        Texturec *bgTex;
+       
+       /**
+        * Defines the set of background images that should be used for this world.
+        */
+       
        WORLD_BG_TYPE bgType;
        
        /**
@@ -165,111 +167,237 @@ protected:
 public:
 
        /**
-        * These pointers keep track of worlds that are adjacent to this one. Used in
-        * ui.cpp for world jumping.
+        * The filename of the XML file for the world to the left; NULL if no world
+        * is present.
+        */
+       
+       char *toLeft;
+       
+       /**
+        * The filename of the XML file for the world to the right; NULL if no world
+        * is present.
         */
        
-       char *toLeft,*toRight;
+       char *toRight;
+       
+       /**
+        * Sets what XML file to use for loading the world to the left.
+        */
        
        char *setToLeft(const char *file);
-       char *setToRight(const char *file);
        
        /**
-        * These vectors contain the NPCs, Mobs, Structures and Objects that are
-        * loaded inside the world, with the Entity vector containing pointers to
-        * the contents of all the others.
+        * Sets what XML file to use for loading the world to the right.
+        */
+       
+       char *setToRight(const char *file);
+
+
+       /**
+        * A vector of pointers to every NPC, Structure, Mob, and Object in this
+        * world.
+        */
+
+       std::vector<Entity              *>      entity;
+
+       /**
+        * A vector of all NPCs in this world.
         */
 
        std::vector<NPC                 *>      npc;
+       
+       /**
+        * A vector of all Structures in this world.
+        */
+        
        std::vector<Structures  *>      build;
+       
+       /**
+        * A vector of all Mobs in this world.
+        */
+        
        std::vector<Mob                 *>      mob;
-       std::vector<Entity              *>      entity;
+       
+       /**
+        * A vector of all Objects in this world.
+        */
+       
        std::vector<Object              *>      object;
+       
+       /**
+        * A vector of all particles in this world.
+        */
+       
        std::vector<Particles   *>      particles;
+       
+       /**
+        * A vector of all light elements in this world.
+        */
+       
        std::vector<Light        >  light;
        
-       void addStructure(BUILD_SUB sub,float x,float y,const char *inside);
-       void addVillage(int bCount, int npcMin, int npcMax,const char *inside);
-       void addMob(int t,float x,float y);
+       /**
+        * NULLifies pointers and allocates necessary memory. This should be
+        * followed by some combination of setBackground(), setBGM(), or
+        * generate().
+        */
+       
+       World(void);
+       
+       /**
+        * Frees resources taken by the world.
+        */
+       
+       virtual ~World(void);
+       
+       /**
+        * Adds a structure to the world, with the specified subtype and
+        * coordinates. `inside` is a file name for the IndoorWorld XML file that
+        * this structure will lead to; if NULL the player won't be able to enter
+        * the structure.
+        */
+       
+       void addStructure(BUILD_SUB subtype,float x,float y,const char *inside);
+       //void addVillage(int buildingCount, int npcMin, int npcMax,const char *inside);
+       
+       /**
+        * Adds a Mob to the world with the specified type and coordinates.
+        */
+       
+       void addMob(int type,float x,float y);
+       
+       /**
+        * Adds a Mob to the world with a handler function that can be called by
+        * certain mobs to trigger events.
+        */
+       
        void addMob(int t,float x,float y,void (*hey)(Mob *));
+       
+       /**
+        * Adds an NPC to the world with the specified coordinates.
+        */
+       
        void addNPC(float x,float y);
-       void addObject(ITEM_ID, bool, const char *, float, float);
-       void addParticle(float, float, float, float, float, float, Color color, int);
-       void addLight(vec2, Color);
+       
+       /**
+        * Adds an object to the world with the specified item id and coordinates.
+        * If `pickupDialog` is not NULL, that string will display in a dialog box
+        * upon object interaction.
+        */
+       
+       void addObject(ITEM_ID id,const char *pickupDialog, float x, float y);
+       
+       /**
+        * Adds a particle to the world with the specified coordinates, dimensions,
+        * velocity, color and duration (time to live).
+        */
+       
+       void addParticle(float x, float y, float w, float h, float vx, float vy, Color color, int duration);
+       
+       /**
+        * Adds a light to the world with the specified coordinates and color.
+        */
+       
+       void addLight(vec2 xy, Color color);
+
+       /**
+        * Get the next NPC in the NPC vector that currently lacks a written dialog.
+        * Could be used to assign random NPCs non-random dialogs.
+        */
 
        NPC *getAvailableNPC(void);
        
-       /*
-        *      Update coordinates of all entities.
+       /**
+        * Updates the coordinates of everything in the world that has coordinates
+        * and a velocity. The provided delta time is used for smoother updating.
         */
        
        void update(Player *p,unsigned int delta);
        
-       /*
-        *      Constructor and deconstructor, these do what you would expect.
-       */
-                                               
-       World(void);
-       virtual ~World(void);                           // Frees the 'line' array.
-       
-       /*
-        *      Generate a world of width `width`. This function is virtual so that other world
-        *      classes that are based on this one can generate themselves their own way.
-       */
+       /**
+        * Generate a world of the provided width. Worlds are drawn centered on the
+        * y-axis, so the reachable coordinates on the world would be from negative
+        * half-width to positive half-width.
+        */
        
        virtual void generate(unsigned int width);
+       
+       /**
+        * Generates a world of the provided width using the given function to
+        * determine ground coordinates. The given y coordinates from the function
+        * are limited to a certain range, most likely from GEN_MIN to 2000.
+        */
+       
        void generateFunc(unsigned int width,float(*func)(float));
        
-       /*
-        *      Adds images to using for the background.
-       */
+       /**
+        * Sets the background theme, collecting the required textures into a
+        * Texturec object.
+        */
        
        void setBackground(WORLD_BG_TYPE bgt);
        
-       /*
-        *      Start/stop background music. 
-       */
+       /**
+        * Sets the background music for the world, required for the world to be
+        * playable.
+        */
        
        void setBGM(const char *path);
+       
+       /**
+        * Plays/stops this world's BGM. If `prev` is not NULL, that world's BGM
+        * will be faded out followed by the fading in of this world's BGM.
+        */
+       
        void bgmPlay(World *prev);
        
-       /*
-        *      Draw the world and entities based on the player's coordinates. Virtual for the same
-        *      reason generate() is.
-       */
-                                                                                                               
+       /**
+        * Draw the world and entities based on the player's coordinates.
+        */
+
        virtual void draw(Player *p);
        
-       /*
-        *      Detect the player and any entities in the current world.
-       */
+       /**
+        * Handles collision between the entities and the world, as well as entity
+        * death.
+        */
        
        void detect(Player *p);
        
-       /*
-        *      These functions return the pointer to the world in the direction that is requested if it
-        *      exists and the player is in a condition that it can make the switch, otherwise they
-        *      return the current world.
-       */
+       /**
+        * Attempts to let the player enter the left-linked world specified by
+        * `toLeft`. Returns the world to the left if the movement is possible,
+        * otherwise returns this world.
+        */
        
        World *goWorldLeft(Player *p);
+       
+       /**
+        * Attempts to let the player enter the right-linked world specified by
+        * `toRight`. Returns the world to the right if the movement is possible,
+        * otherwise returns this world.
+        */
+       
        World *goWorldRight(Player *p);
        
-       /*
-        *      Called to enter/exit a structure.
-       */
+       /**
+        * This function looks for any structure the player is standing in front of
+        * that also have an inside world. Returns the inside world if those
+        * conditions are met, otherwise returns this world.
+        */
        
        World *goInsideStructure(Player *p);
        
-       /*
-        *      These functions add features to the world.
-       */
+       /**
+        * Adds a hole between the specified y coordinates. If the player falls in
+        * this hole the game will exit.
+        */
        
        void addHole(unsigned int start,unsigned int end);
        
        /*
         *      Get's the world's width.
-       */
+        */
        
        int getTheWidth(void);
        
index 14c67c5ff0eeda83e8d0b987790b5e8ec16eddf7..320b87baa42d829432e05006bc227240fbcf0340 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -252,16 +252,15 @@ int main(/*int argc, char *argv[]*/){
         *
        */
 
+       uint32_t SDL_CreateWindowFlags = SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL | (FULLSCREEN ? SDL_WINDOW_FULLSCREEN : 0);
+
        window = SDL_CreateWindow(GAME_NAME,
                                                          SDL_WINDOWPOS_UNDEFINED,      // Spawn the window at random (undefined) x and y coordinates
                                                          SDL_WINDOWPOS_UNDEFINED,      //
                                                          SCREEN_WIDTH,
                                                          SCREEN_HEIGHT,
-                                                         SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL
-#ifdef FULLSCREEN
-                                                         | SDL_WINDOW_FULLSCREEN
-#endif // FULLSCREEN
-                                 );
+                                                         SDL_CreateWindowFlags
+                                                         );
 
     /*
      * Exit if the window cannot be created
@@ -439,7 +438,6 @@ int main(/*int argc, char *argv[]*/){
  *     fps contains the game's current FPS, debugY contains the player's
  *     y coordinates, updated at a certain interval. These are used in
  *     the debug menu (see below).
- * 
 */
 
 static unsigned int fps=0;
index 4418a3bbf78b625f5e93a629b064f1fff0dd2cb0..e715b1ea2c2d0a0c52bcbc0c50615bbf88f28e86 100644 (file)
@@ -79,30 +79,38 @@ namespace Texture{
                }
        }
 
+       #define CINDEX_WIDTH (8*4*3)
        void initColorIndex(){
+               unsigned int i;
+               GLubyte *buffer;
+               GLfloat *bufferf;
+               
+               buffer  = new GLubyte[CINDEX_WIDTH];
+               bufferf = new GLfloat[CINDEX_WIDTH];
+               
                colorIndex = loadTexture("assets/colorIndex.png");
                glActiveTexture(GL_TEXTURE0);
                glBindTexture(GL_TEXTURE_2D, colorIndex);
-               GLubyte* buffer = new GLubyte[8*4*3];
                glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, buffer);
-               GLfloat* bufferf = new GLfloat[8*4*3];
-               for(uint iu = 0; iu < 8*4*3; iu++){
-                       bufferf[iu] = float(buffer[iu]) / 255.0f;
-               }
-               uint i = 0;
-               for(uint y = 0; y < 8; y++){
-                       for(uint x = 0; x < 4; x++){
-                                       if(i >= 8*4*3){
+               
+               for(i = 0; i < CINDEX_WIDTH; i++)
+                       bufferf[i] = (float)buffer[i] / 255.0f;
+               
+               i = 0;
+               for(unsigned int y = 0; y < 8; y++){
+                       for(unsigned int x = 0; x < 4; x++){
+                                       if(i >= CINDEX_WIDTH){
+                                               delete[] buffer;
+                                               delete[] bufferf;
                                                return;
                                        }
                                        pixels[y][x].red = buffer[i++];
                                        pixels[y][x].green = buffer[i++];
                                        pixels[y][x].blue = buffer[i++];
-                                       //std::cout << pixels[y][x].red << "," << pixels[y][x].green << "," << pixels[y][x].blue << std::endl;
-                               //std::cout << std::endl;
                        }
                }
-
+               delete[] buffer;
+               delete[] bufferf;
        }
 
        //sqrt((255-145)^2+(90-145)^2+(0-0)^2);
index 8608aeb1cb5202abbcf6e0b22bc8d01b4e6cca8f..4a8e6b76850674dea585aee2766e2d537c0f7af9 100644 (file)
@@ -38,13 +38,9 @@ void getRandomName(Entity *e){
        
        bufs = new char[32];
        
-       std::cout<<"1\n";
-       
        for(;!names.eof();max++)
                names.getline(bufs,32);
        
-       std::cout<<"2\n";
-       
        tempNum = rand() % max;
        names.seekg(0,names.beg);
        
@@ -218,13 +214,18 @@ Object::Object(){
        inv = NULL;
 }
 
-Object::Object(ITEM_ID id, bool qo, const char *pd){
+Object::Object(ITEM_ID id, const char *pd){
        identifier = id;
-       questObject = qo;
-       
-       pickupDialog = new char[strlen(pd)+1];
-       strcpy(pickupDialog,pd);
-       
+
+       if(pd){
+               pickupDialog = new char[strlen(pd)+1];
+               strcpy(pickupDialog,pd);
+               questObject = true;
+       }else{
+               pickupDialog = new char[1];
+               *pickupDialog = '\0';
+               questObject = false;
+       }
 
        type = OBJECTT;
        alive = true;
index f86536079c18873a51c7eb5a6afb2604173f9316..1520e9f02feee07e10afa786e4fc2709020ccd9c 100644 (file)
@@ -585,13 +585,19 @@ namespace ui {
                        };
                        
                        putText(hub.x,hub.y,"Health: %u/%u",player->health>0?(unsigned)player->health:0,
-                                                                                               (unsigned)player->maxHealth);
+                                                                                               (unsigned)player->maxHealth
+                                                                                               );
                        if(player->alive){
-                               glColor3ub(255,0,0);
+                               glColor3ub(150,0,0);
                                hub.y-=fontSize*1.15;
                                glRectf(hub.x,
                                                hub.y,
-                                               hub.x+(player->health/player->maxHealth?player->maxHealth:1)*130,
+                                               hub.x+150,
+                                               hub.y+12);
+                               glColor3ub(255,0,0);
+                               glRectf(hub.x,
+                                               hub.y,
+                                               hub.x+(player->health/player->maxHealth * 150),
                                                hub.y+12);
                        }
                        
@@ -916,6 +922,7 @@ DONE:
                                                        player->loc.y+=HLINE*5;
                                                        player->ground=false;
                                                }*/
+                                               player->health -= 5;
                                                break;
                                        case SDLK_k:
                                                /*currentWorld=currentWorld->goWorldFront(player);      // Go forward a layer if possible
index 71bd0f4682fe303ae71b64b0ca6d65a751815e35..c8b4bde069c6dc388f69087190677760fd9fd234 100644 (file)
@@ -962,12 +962,12 @@ void World::addStructure(BUILD_SUB sub, float x,float y,const char *inside){
        entity.push_back(build.back());
 }
        
-void World::addVillage(int bCount, int npcMin, int npcMax,const char *inside){
+/*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";
+               std::cout<<"1\n";
                HERE:
                xwasd = (rand()%(int)x+1000*HLINE);
                for(auto &bu : build){
@@ -975,9 +975,10 @@ void World::addVillage(int bCount, int npcMin, int npcMax,const char *inside){
                }
                std::cout<<"2\n";
                addStructure(t,HOUSE,xwasd,y,inside);
-               std::cout<<"3\n";*/
+               std::cout<<"3\n";
        }
-}
+}*/
+
 void World::addMob(int t,float x,float y){
        mob.push_back(new Mob(t));
        mob.back()->spawn(x,y);
@@ -1000,8 +1001,8 @@ void World::addNPC(float x,float y){
        entity.push_back(npc.back());
 }
 
-void World::addObject(ITEM_ID i, bool q, const char *p, float x, float y){
-       object.push_back(new Object(i,q, p));
+void World::addObject(ITEM_ID i,const char *p, float x, float y){
+       object.push_back(new Object(i,p));
        object.back()->spawn(x,y);
 
        entity.push_back(object.back());
@@ -1203,9 +1204,11 @@ extern int commonAIFunc(NPC *);
 
 void World::load(void){
        std::string save,data,line;
+       const char *filedata;
        
        save = (std::string)currentXML + ".dat";
-       data = readFile(save.c_str());
+       filedata = readFile(save.c_str());
+       data = filedata;
        std::istringstream iss (data);
        
        for(auto &n : npc){
@@ -1244,11 +1247,9 @@ void World::load(void){
        while(std::getline(iss,line)){
                if(line == "dOnE")
                        break;
-               
-               //std::cout<<line<<std::endl;
        }
        
-       //abort();
+       delete[] filedata;
 }
 
 IndoorWorld::IndoorWorld(void){