- 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
#undef near
#endif
-/*
- * Include file headers
-*/
#include <Texture.h>
/*
#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
*
*/
- 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.
*/
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);
+
};
/*
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
}
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`.
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.