]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
world save/load
authorClyne Sullivan <tullivan99@gmail.com>
Fri, 6 Nov 2015 13:19:46 +0000 (08:19 -0500)
committerClyne Sullivan <tullivan99@gmail.com>
Fri, 6 Nov 2015 13:19:46 +0000 (08:19 -0500)
Changelog
include/common.h
include/world.h
main.cpp
src/gameplay.cpp
src/world.cpp

index 1dd169dfea249977852aa1ae021585aeb6ded8d6..62dbbb9b98ddfe7a36046b7f2422871a74014ddb 100644 (file)
--- a/Changelog
+++ b/Changelog
        - 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
index 56e6a38dc87bddff9abca6fd83177a4edc50898b..846dfb9e3772c7928edb2a1f9f933a47f33eca1f 100644 (file)
@@ -30,9 +30,6 @@ typedef unsigned int uint;
 #undef near
 #endif
 
-/*
- *     Include file headers
-*/
 #include <Texture.h>
 
 /*
index 81ec33587265cff224ed5c226e8ee5622688ece9..e3c7f1509acd320dbc2293eeffe96accb423d515 100644 (file)
@@ -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);
+       
 };
 
 /*
index 30869bff2d727288e616c4a96b65b71a6b56e5ea..90b673d7b3b4fa8841c680d20b76a3dac2090ba8 100644 (file)
--- 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<<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
 }
 
index baa91e80886a61e4d80a7363250af22c75bd09e5..b32d9ee777db3b50eb786e59310b313f964d8ae5 100644 (file)
@@ -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`.
index 77872c1b52ec11f061aed8ee25d08a0c25f45df2..01c06c47d8bba1ec4edabcad795c3c99bf4790a6 100644 (file)
@@ -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.