From e2c16377c87b2ba70bea8fc1fb428eae525b5cf9 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Thu, 10 Sep 2015 21:23:08 -0400 Subject: world saving --- include/World.h | 4 +++- src/World.cpp | 26 +++++++++++++++++++++++++- 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 +#include World::World(void){ line=NULL; @@ -62,7 +63,6 @@ void World::draw(void){ } glEnd(); } -#include void World::detect(vec2 *v,const float width){ unsigned int i; for(i=0;itoLeft){ + 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<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 +#include #include #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!"<loadFromFile(f,currentWorld); + fclose(f); + }*/ + float gw; while(gameRunning){ -- cgit v1.2.3