]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
save/load stuffs
authorClyne Sullivan <tullivan99@gmail.com>
Tue, 5 Jan 2016 13:48:29 +0000 (08:48 -0500)
committerClyne Sullivan <tullivan99@gmail.com>
Tue, 5 Jan 2016 13:48:29 +0000 (08:48 -0500)
Changelog
include/entities.h
include/inventory.h
src/entities.cpp
src/gameplay.cpp
src/world.cpp

index e3ed5e0f401825edd9f1cd0047fdfc8fb75b2043..68edc5e456d98d4b54d96bdfc7fa090de647a326 100644 (file)
--- a/Changelog
+++ b/Changelog
        - finished wrapping text for dialog boxes
        - began working on world saving/loading again
        - got some mad GLSL shaders running
+
+1/4/2015:
+=========
+
+       - fixed basic world save/load, working on entity saving
+       - GLSL shaders worked?
index 6b8cc32569790e3ca56579e9ee3c81028c71e1c2..894dc16e5cd8e47b2dea32fb9627e399983baeff 100644 (file)
@@ -45,6 +45,31 @@ enum BUILD_SUB{
        FOUNTAIN
 };
 
+typedef struct {
+       InventorySavePacket isp;
+       vec2 loc;
+       vec2 vel;
+       float width;
+       float height;
+       float speed;
+       float health;
+       float maxHealth;
+       int subtype;
+       int ticksToUse;
+       unsigned int randDialog;
+       unsigned char ground;
+       bool near;
+       bool canMove;
+       bool right,left;
+       bool alive;
+       bool hit;
+       _TYPE type;
+       GENDER  gender;
+       size_t nameSize;
+       //char   *name;
+       //Texturec *tex;
+} __attribute__ ((packed)) EntitySavePacket;
+
 class World;
 
 class Particles{
@@ -139,6 +164,9 @@ public:
        virtual void interact(){}
        
        virtual ~Entity(){}
+       
+       char *baseSave(void);
+       void baseLoad(char *);
 };
 
 class Player : public Entity {
@@ -151,6 +179,10 @@ public:
        void interact();
 };
 
+typedef struct {
+       EntitySavePacket esp;
+} __attribute__ ((packed)) NPCSavePacket;
+
 class NPC : public Entity{
 public:
        std::vector<int (*)(NPC *)>aiFunc;
@@ -161,6 +193,9 @@ public:
        void addAIFunc(int (*func)(NPC *),bool preload);
        void interact();
        void wander(int);
+       
+       char *save(unsigned int *size);
+       void load(char *b);
 };
 
 class Structures : public Entity{
index 31b7d883e18f3f432136cc4e8cd62e6bae8a3930..b035f91d6e693d69bc22a78715de7f332d4e3c20 100644 (file)
@@ -60,6 +60,11 @@ struct item_t{
        ITEM_ID id;
 } __attribute__((packed));
 
+typedef struct {
+       unsigned int size;
+       int os;
+       unsigned int sel;
+} __attribute__ ((packed)) InventorySavePacket;
 
 class Inventory {
 private:
@@ -87,6 +92,18 @@ public:
        
        void draw(void);        // Draws a text list of items in this inventory (should only be called for the player for now)
        
+       char *save(void){
+               static InventorySavePacket *isp = new InventorySavePacket();
+               isp->size = size;
+               isp->os = os;
+               isp->sel = sel;
+               return (char *)isp;
+       }
+       void load(InventorySavePacket *isp){
+               size = isp->size;
+               os = isp->os;
+               sel = isp->sel;
+       }
 };
 
 void itemUse(void *p);
index d424aba40151807321d5d5dcbaff37ae1119e107..f34abd6c690d7a4194480845671a213eb9ef20db 100644 (file)
@@ -490,3 +490,73 @@ void Mob::wander(int timeRun){
                break;
        }
 }
+
+char *Entity::baseSave(void){
+       static EntitySavePacket *esp = new EntitySavePacket();
+       memcpy(&esp->isp,inv->save(),sizeof(InventorySavePacket));
+       esp->loc = loc;
+       esp->vel = vel;
+       esp->width = width;
+       esp->height = height;
+       esp->speed = speed;
+       esp->health = health;
+       esp->maxHealth = maxHealth;
+       esp->subtype = subtype;
+       esp->ticksToUse = ticksToUse;
+       esp->randDialog = randDialog;
+       esp->ground = ground;
+       esp->near = near;
+       esp->canMove = canMove;
+       esp->right = right;
+       esp->left = left;
+       esp->alive = alive;
+       esp->hit = hit;
+       esp->type = type;
+       esp->gender = gender;
+       esp->nameSize = strlen(name) + 1;
+       return (char *)esp;
+}
+
+void Entity::baseLoad(char *e){
+       const char *tmpname = "GG\0";
+       EntitySavePacket *esp = (EntitySavePacket *)e;
+       inv->load(&esp->isp);
+       loc = esp->loc;
+       vel = esp->vel;
+       width = esp->width;
+       height = esp->height;
+       speed = esp->speed;
+       health = esp->health;
+       maxHealth = esp->maxHealth;
+       subtype = esp->subtype;
+       ticksToUse = esp->ticksToUse;
+       randDialog = esp->randDialog;
+       ground = esp->ground;
+       near = esp->near;
+       canMove = esp->canMove;
+       right = esp->right;
+       left = esp->left;
+       alive = esp->alive;
+       hit = esp->hit;
+       type = esp->type;
+       gender = esp->gender;
+       name = new char[esp->nameSize+1];
+       strcpy(name,tmpname);
+}
+
+char *NPC::save(unsigned int *size){
+       static char *buf = new char[(*size = sizeof(EntitySavePacket) + aiFunc.size() * sizeof(int(*)(NPC *)))],*esp;
+       memcpy(buf,(esp = baseSave()),sizeof(EntitySavePacket));
+       delete[] esp;
+       memcpy(buf+sizeof(EntitySavePacket),aiFunc.data(),aiFunc.size() * sizeof(int(*)(NPC *)));
+       return buf;
+}
+
+void NPC::load(char *b){
+       unsigned int size = *(unsigned int *)b,size2,i;
+       baseLoad(b + sizeof(unsigned int));
+       size2 = (size - sizeof(unsigned int) - sizeof(EntitySavePacket)) / sizeof(int(*)(NPC *));
+       for(i=0;i<size2;i++){
+               //aiFunc.push_back
+       }
+}
index ff77728283e1027f91acd5b9d71a9ee8b8fae7e4..94fb954fbf750505ba3b18716240ab5806bd6580 100644 (file)
@@ -122,16 +122,17 @@ static World *worldFirstVillage;
 
 void destroyEverything(void);
 void initEverything(void){
-       //static std::ifstream i ("world.dat",std::ifstream::in | std::ifstream::binary);
+       static std::ifstream i ("world.dat",std::ifstream::in | std::ifstream::binary);
        
        worldSpawnHill1 = new World();
-       /*if(!i.fail()){
+       worldSpawnHill1->setBackground(BG_FOREST);
+       if(!i.fail()){
                worldSpawnHill1->load(&i);
                i.close();
-       }else*/
+       }else{
                worldSpawnHill1->generateFunc(400,gen_worldSpawnHill1);
-       worldSpawnHill1->setBackground(BG_FOREST);
-       worldSpawnHill1->setBGM("assets/music/embark.wav");
+               worldSpawnHill1->setBGM("assets/music/embark.wav");
+       }
        worldSpawnHill1->addMob(MS_TRIGGER,0,0,worldSpawnHill1_hillBlock);
 
        worldSpawnHill2 = new World();
index 2afbdf5dbc8e5c7888e3b545727dcc3061b5330a..34e187048f408fd28a12543a2e8de73bbc5aac19 100644 (file)
@@ -62,27 +62,68 @@ void World::setBackground(WORLD_BG_TYPE bgt){
 }
 
 void World::save(std::ofstream *o){
+       static unsigned int size2;
+       unsigned int size,i;
+       size_t bgms = strlen(bgm) + 1;
+       char *bufptr;
+       
        o->write((char *)&lineCount,            sizeof(unsigned int));
-       o->write((char *)&line     ,lineCount * sizeof(struct line_t));
+       o->write((char *)line      ,lineCount * sizeof(struct line_t));
        o->write("GG"              ,2         * sizeof(char));
-       o->write((char *)&star     ,100       * sizeof(vec2));
+       o->write((char *)star      ,100       * sizeof(vec2));
+       o->write((char *)&bgType   ,            sizeof(WORLD_BG_TYPE));
+       o->write((char *)&bgms     ,            sizeof(size_t));
+       o->write(bgm               ,strlen(bgm)+1);
+       o->write("NO"              ,2         * sizeof(char));
+       
+       /*std::vector<NPC                       *>      npc;
+       std::vector<Structures  *>      build;
+       std::vector<Mob                 *>      mob;
+       std::vector<Entity              *>      entity;
+       std::vector<Object              *>      object;*/
+       
+       size = npc.size();
+       for(i=0;i<size;i++){
+               bufptr = npc[i]->save(&size2);
+               o->write((char *)&size2,sizeof(unsigned int));
+               o->write(bufptr,size2);
+       }
 }
 
 void World::load(std::ifstream *i){
-       static char end[2];
+       //unsigned int size;
+       size_t bgms;
+       char sig[2];
        
        i->read((char *)&lineCount,sizeof(unsigned int));
+       
        line = new struct line_t[lineCount];
+       i->read((char *)line,lineCount * sizeof(struct line_t));
        
-       i->read((char *)&line,lineCount * sizeof(struct line_t));
-       i->read(end          ,2         * sizeof(char));
-       if(strncmp(end,"GG",2)){
+       i->read(sig,2 * sizeof(char));
+       if(strncmp(sig,"GG",2)){
                std::cout<<"world.dat corrupt"<<std::endl;
                exit(EXIT_FAILURE);
        }
-       i->read((char *)&star,100 * sizeof(vec2));
-       
+
        x_start = 0 - getWidth(this) / 2;
+       
+       i->read((char *)star,100 * sizeof(vec2));
+       i->read((char *)&bgType,sizeof(WORLD_BG_TYPE));
+       
+       i->read((char *)&bgms,sizeof(size_t));
+       bgm = new char[bgms];
+       i->read(bgm,bgms);
+       setBGM(bgm);
+
+       
+       i->read(sig,2 * sizeof(char));
+       if(strncmp(sig,"NO",2)){
+               std::cout<<"world.dat corrupt"<<std::endl;
+               exit(EXIT_FAILURE);
+       }
+       
+       
 }
 
 World::World(void){