diff options
-rw-r--r-- | include/World.h | 4 | ||||
-rw-r--r-- | src/World.cpp | 26 | ||||
-rw-r--r-- | src/main.cpp | 22 |
3 files changed, 48 insertions, 4 deletions
diff --git a/include/World.h b/include/World.h index f6a08f9..08ecabb 100644 --- a/include/World.h +++ b/include/World.h @@ -11,7 +11,7 @@ private: struct line_t {
// x = 2.0 (window width) / HLINES
double start; // Where to change to dirt, going down (y)
- } *line;
+ } __attribute__ ((packed)) *line;
unsigned int lineCount;
public:
World *toLeft,*toRight;
@@ -20,6 +20,8 @@ public: void draw(void);
void detect(vec2 *v,const float width);
float getWidth(void);
+ void saveToFile(FILE *f,World *parent);
+ void loadFromFile(FILE *f,World *parent);
};
#endif // WORLD_H diff --git a/src/World.cpp b/src/World.cpp index cf14612..9b9a2c3 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -1,4 +1,5 @@ #include <World.h>
+#include <cstdio>
World::World(void){
line=NULL;
@@ -62,7 +63,6 @@ void World::draw(void){ }
glEnd();
}
-#include <stdio.h>
void World::detect(vec2 *v,const float width){
unsigned int i;
for(i=0;i<lineCount;i++){
@@ -82,3 +82,27 @@ void World::detect(vec2 *v,const float width){ float World::getWidth(void){
return (lineCount-1)*HLINE;
}
+void World::saveToFile(FILE *f,World *parent){
+ fwrite(&lineCount,sizeof(unsigned int) ,1 ,f);
+ fwrite(&line ,sizeof(struct line_t),lineCount,f);
+ if(toLeft!=NULL&&toLeft!=parent->toLeft){
+ toLeft->saveToFile(f,toLeft);
+ }
+ if(toRight!=NULL&&toRight!=parent->toRight){
+ toRight->saveToFile(f,toRight);
+ }
+}
+void World::loadFromFile(FILE *f,World *parent){
+ fread(&lineCount,sizeof(unsigned int) ,1 ,f);
+ line=(struct line_t *)malloc(lineCount*sizeof(struct line_t *));
+ fread(&line ,sizeof(struct line_t),lineCount,f);
+ if(toLeft!=NULL&&toLeft!=parent->toLeft){
+ toLeft->loadFromFile(f,toLeft);
+ }
+ std::cout<<toRight<<" "<<parent->toRight<<std::endl;
+ if(toRight!=NULL&&toRight!=parent->toRight){
+ puts("A");
+ toRight->loadFromFile(f,toRight);
+ }
+}
+
diff --git a/src/main.cpp b/src/main.cpp index c404f22..8da775c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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){ |