diff options
author | Clyne Sullivan <tullivan99@gmail.com> | 2015-09-10 21:23:08 -0400 |
---|---|---|
committer | Clyne Sullivan <tullivan99@gmail.com> | 2015-09-10 21:23:08 -0400 |
commit | e2c16377c87b2ba70bea8fc1fb428eae525b5cf9 (patch) | |
tree | 53ab50d515e604646034e39db9e79e60cc53b68b /src | |
parent | a103b556146e434d038487fe518b854da8bc10d3 (diff) |
world saving
Diffstat (limited to 'src')
-rw-r--r-- | src/World.cpp | 26 | ||||
-rw-r--r-- | src/main.cpp | 22 |
2 files changed, 45 insertions, 3 deletions
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){ |