From cb55d6e72ae1f48622ffc2a36d7bdb3719355afe Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Fri, 6 Nov 2015 08:19:46 -0500 Subject: world save/load --- Changelog | 9 +++++++-- include/common.h | 3 --- include/world.h | 28 ++++++++++++++++++++-------- main.cpp | 9 +++++++++ src/gameplay.cpp | 26 ++++++++++++++++++++++---- src/world.cpp | 29 +++++++++++++++++++++++++++++ 6 files changed, 87 insertions(+), 17 deletions(-) diff --git a/Changelog b/Changelog index 1dd169d..62dbbb9 100644 --- a/Changelog +++ b/Changelog @@ -241,11 +241,16 @@ - worked on storyline 11/5/2015: -=========== +========== - wrote more storyline (up to 7 pages) - fixed ortho for when player is inside a building - began work on ray shading (flashlight) - (out of class) began experimenting with writing game soundtracks - ~ About 3400 lines of code + documentation written + ~ About 3400 lines of code + documentation written ( +7 pages of story on gdoc) + +11/6/2015: +========== + + - worlds can now be saved & loaded from a file diff --git a/include/common.h b/include/common.h index 56e6a38..846dfb9 100644 --- a/include/common.h +++ b/include/common.h @@ -30,9 +30,6 @@ typedef unsigned int uint; #undef near #endif -/* - * Include file headers -*/ #include /* diff --git a/include/world.h b/include/world.h index 81ec335..e3c7f15 100644 --- a/include/world.h +++ b/include/world.h @@ -4,9 +4,11 @@ #include // For HLINE, vec2, OpenGL utilities, etc. #include -typedef struct { - vec2 p1,p2; -} __attribute__ ((packed)) Platform; +struct line_t { + bool gs; + float y,gh[2]; + unsigned char color; +} __attribute__ ((packed)); /* * World - creates and handles an area of land @@ -27,11 +29,7 @@ protected: * */ - struct line_t { - bool gs; - float y,gh[2]; - unsigned char color; - } __attribute__ ((packed)) *line; + struct line_t *line; /* * Keeps a dynamically allocated array of platforms in the world. @@ -132,6 +130,20 @@ public: */ int getTheWidth(void); + + /* + * Stores all of this class's contents in a string for saving to a file. This array should + * be freed after being written to a file. + */ + + char *save(unsigned int *ssize); + + /* + * Loads a previous world's contents (from save()) into this one. + */ + + void load(char *buf); + }; /* diff --git a/main.cpp b/main.cpp index 30869bf..90b673d 100644 --- a/main.cpp +++ b/main.cpp @@ -485,6 +485,15 @@ int main(int argc, char *argv[]){ SDL_GL_DeleteContext(mainGLContext); SDL_DestroyWindow(window); + FILE *worldSave = fopen("world.dat","w"); + char *worldBuf; + unsigned int worldSize; + worldBuf=currentWorld->save(&worldSize); + std::cout<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"<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<lineCount<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. -- cgit v1.2.3