diff options
-rw-r--r-- | Changelog | 9 | ||||
-rw-r--r-- | include/common.h | 3 | ||||
-rw-r--r-- | include/world.h | 28 | ||||
-rw-r--r-- | main.cpp | 9 | ||||
-rw-r--r-- | src/gameplay.cpp | 26 | ||||
-rw-r--r-- | src/world.cpp | 29 |
6 files changed, 87 insertions, 17 deletions
@@ -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 <Texture.h> /* 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 <common.h> // For HLINE, vec2, OpenGL utilities, etc. #include <entities.h> -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); + }; /* @@ -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<<worldSize<<" "<<(int)worldBuf<<std::endl; + std::cout<<fwrite(worldBuf,1,worldSize,worldSave)<<std::endl; + if(ferror(worldSave))perror("HEY: "); + fclose(worldSave); + return 0; // Calls everything passed to atexit } 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. |