]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
world saving
authorClyne Sullivan <tullivan99@gmail.com>
Fri, 11 Sep 2015 01:23:08 +0000 (21:23 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Fri, 11 Sep 2015 01:23:08 +0000 (21:23 -0400)
include/World.h
src/World.cpp
src/main.cpp

index f6a08f90bc0e1f4198917c6590204c12d052717c..08ecabb4095803150ea2412cec5ef48d4923a5c8 100644 (file)
@@ -11,7 +11,7 @@ private:
        struct line_t {\r
                // x = 2.0 (window width) / HLINES\r
                double start; // Where to change to dirt, going down (y)\r
-       } *line;\r
+       } __attribute__ ((packed)) *line;\r
        unsigned int lineCount;\r
 public:\r
        World *toLeft,*toRight;\r
@@ -20,6 +20,8 @@ public:
        void draw(void);\r
        void detect(vec2 *v,const float width);\r
        float getWidth(void);\r
+       void saveToFile(FILE *f,World *parent);\r
+       void loadFromFile(FILE *f,World *parent);\r
 };\r
 \r
 #endif // WORLD_H
index cf14612dcac1637c0aa6ab695b731f2d53e52aa6..9b9a2c3e7fd50d9034cd31fe4be96a7bf9f21fc2 100644 (file)
@@ -1,4 +1,5 @@
 #include <World.h>\r
+#include <cstdio>\r
 \r
 World::World(void){\r
        line=NULL;\r
@@ -62,7 +63,6 @@ void World::draw(void){
                }\r
        glEnd();\r
 }\r
-#include <stdio.h>\r
 void World::detect(vec2 *v,const float width){\r
        unsigned int i;\r
        for(i=0;i<lineCount;i++){\r
@@ -82,3 +82,27 @@ void World::detect(vec2 *v,const float width){
 float World::getWidth(void){\r
        return (lineCount-1)*HLINE;\r
 }\r
+void World::saveToFile(FILE *f,World *parent){\r
+       fwrite(&lineCount,sizeof(unsigned int) ,1        ,f);\r
+       fwrite(&line     ,sizeof(struct line_t),lineCount,f);\r
+       if(toLeft!=NULL&&toLeft!=parent->toLeft){\r
+               toLeft->saveToFile(f,toLeft);\r
+       }\r
+       if(toRight!=NULL&&toRight!=parent->toRight){\r
+               toRight->saveToFile(f,toRight);\r
+       }\r
+}\r
+void World::loadFromFile(FILE *f,World *parent){\r
+       fread(&lineCount,sizeof(unsigned int) ,1        ,f);\r
+       line=(struct line_t *)malloc(lineCount*sizeof(struct line_t *));\r
+       fread(&line     ,sizeof(struct line_t),lineCount,f);\r
+       if(toLeft!=NULL&&toLeft!=parent->toLeft){\r
+               toLeft->loadFromFile(f,toLeft);\r
+       }\r
+       std::cout<<toRight<<" "<<parent->toRight<<std::endl;\r
+       if(toRight!=NULL&&toRight!=parent->toRight){\r
+               puts("A");\r
+               toRight->loadFromFile(f,toRight);\r
+       }\r
+}\r
+\r
index c404f22a707263abab19eb3e993390a2648c57bf..8da775ca753fc537e7a858e61f15229064bc1e25 100644 (file)
@@ -1,4 +1,5 @@
 #include <common.h>
+#include <cstdio>
 #include <ctime>
 
 #define TICKS_PER_SEC 20
@@ -77,11 +78,28 @@ int main(int argc,char **argv){
        entit1 = &player;
        entit1->spawn(0, 0);
 
-       World *w=NULL;
-       World *w2=new World(4,w,NULL);
+       // Generate the world
+       World *w=NULL,*w2=NULL;
+       w2=new World(4,w,NULL);
        w=new World(2,NULL,w2);
+       
        currentWorld=w;
        
+       // Save the world if necessary
+       /*static FILE *f=fopen("world.dat","r");
+       if(f==NULL){
+               f=fopen("world.dat","w");
+               if(f!=NULL){
+                       currentWorld->saveToFile(f,currentWorld);
+                       fclose(f);
+               }else{
+                       std::cout<<"Error! Couldn\'t save the world!"<<std::endl;
+               }
+       }else{
+               currentWorld->loadFromFile(f,currentWorld);
+               fclose(f);
+       }*/
+       
        float gw;
        
        while(gameRunning){