diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/gameplay.cpp | 26 | ||||
-rw-r--r-- | src/world.cpp | 29 |
2 files changed, 51 insertions, 4 deletions
diff --git a/src/gameplay.cpp b/src/gameplay.cpp index baa91e8..b32d9ee 100644 --- a/src/gameplay.cpp +++ b/src/gameplay.cpp @@ -31,14 +31,32 @@ void initEverything(void){ unsigned int i; /* - * Generate a new world. + * World creation: */ World *test=new World(); - test->generate(SCREEN_WIDTH * 2); + + /* + * Load the saved world if it exists, otherwise generate a new one. + */ + + FILE *worldLoad; + if((worldLoad=fopen("world.dat","r"))){ + std::cout<<"Yes"<<std::endl; + char *buf; + unsigned int size; + fseek(worldLoad,0,SEEK_END); + size=ftell(worldLoad); + rewind(worldLoad); + buf=(char *)malloc(size); + fread(buf,1,size,worldLoad); + test->load(buf); + }else{ + test->generate(SCREEN_WIDTH * 2); + test->addHole(100,150); + } + test->addLayer(400); - - test->addHole(100,150); /* * Setup the current world, making the player initially spawn in `test`. diff --git a/src/world.cpp b/src/world.cpp index 77872c1..01c06c4 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -34,6 +34,35 @@ float worldGetYBase(World *w){ return base; } +struct wSavePack { + int x_start; + unsigned int lineCount; +} __attribute__ ((packed)); + +char *World::save(unsigned int *ssize){ + struct wSavePack *sp; + unsigned int size; + char *buf; + size=sizeof(struct wSavePack) + lineCount * sizeof(struct line_t); + buf=(char *)malloc(size); + sp=(struct wSavePack *)buf; + sp->x_start=x_start; + sp->lineCount=lineCount; + memcpy(buf+sizeof(struct wSavePack),line,lineCount * sizeof(struct line_t)); + *ssize=size; + return buf; +} + +void World::load(char *buf){ + struct wSavePack *sp; + sp=(struct wSavePack *)buf; + std::cout<<sp->lineCount<<std::endl; + x_start=sp->x_start; + lineCount=sp->lineCount; + line=(struct line_t *)calloc(lineCount,sizeof(struct line_t)); + memcpy(line,buf+sizeof(struct wSavePack),lineCount * sizeof(struct line_t)); +} + World::World(void){ /* * Nullify pointers to other worlds. |