From bf82dd59140d32f4eb265ae69a44784b5fda116a Mon Sep 17 00:00:00 2001 From: drumsetmonkey Date: Wed, 16 Dec 2015 07:32:28 -0500 Subject: Stuff --- src/entities.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src/entities.cpp') diff --git a/src/entities.cpp b/src/entities.cpp index 24c0d01..3b8b52b 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -155,8 +155,8 @@ Mob::Mob(int sub){ height = 2000; tex = new Texturec(0); case MS_DOOR: - width = HLINE * 10; - height = HLINE * 16; + width = HLINE * 12; + height = HLINE * 20; tex = new Texturec(1,"assets/door.png"); break; } @@ -416,7 +416,10 @@ unsigned int Structures::spawn(_TYPE t, BUILD_SUB sub, float x, float y){ * with type NPC. */ + //((World*)(inWorld))->addNPC(loc.x + i * HLINE ,100); + //inWorld->addNPC(loc.x + i * HLINE, 100); currentWorld->addNPC(loc.x + i * HLINE ,100); + } -- cgit v1.2.3 From 0f379ae55e07d4e1e7904a3eb33b1c29c2177ec1 Mon Sep 17 00:00:00 2001 From: drumsetmonkey Date: Fri, 18 Dec 2015 07:32:05 -0500 Subject: Added lights --- include/common.h | 3 ++- main.cpp | 12 ++++++++++-- src/entities.cpp | 4 ++-- src/world.cpp | 1 - test.frag | 21 +++++++++++++++++---- 5 files changed, 31 insertions(+), 10 deletions(-) (limited to 'src/entities.cpp') diff --git a/include/common.h b/include/common.h index 5c90222..9dcc110 100644 --- a/include/common.h +++ b/include/common.h @@ -14,6 +14,7 @@ #include #include #include +#include #define GLEW_STATIC #include @@ -34,7 +35,7 @@ typedef unsigned int uint; * This flag lets the compiler know that we want to use shaders. */ -#define SHADERSs +#define SHADERS /** * This structure contains a set of coordinates for ease of coding. diff --git a/main.cpp b/main.cpp index 80bbeb9..014d950 100644 --- a/main.cpp +++ b/main.cpp @@ -595,7 +595,6 @@ void render(){ * Call the world's draw function, drawing the player, the world, the background, and entities. Also * draw the player's inventory if it exists. */ - glUseProgramObjectARB(shaderProgram); player->near=true; // Draw the player's name @@ -676,12 +675,20 @@ void render(){ } glUseProgramObjectARB(0); } + + glUseProgramObjectARB(shaderProgram); + glUniform2f(glGetUniformLocation(shaderProgram, "lightLocation"), 250,250); + glUniform3f(glGetUniformLocation(shaderProgram, "lightColor"), 1,0,0); + glColor4f(0.0f,0.0f,0.0f,1.0f); + glRectf(-SCREEN_WIDTH/2,0,SCREEN_WIDTH/2,SCREEN_HEIGHT); + glUseProgramObjectARB(0); + player->inv->draw(); /* * Here we draw a black overlay if it's been requested. */ - glUseProgramObjectARB(0); + //glUseProgramObjectARB(0); if(fadeIntensity){ @@ -772,6 +779,7 @@ void render(){ static volatile bool objectInteracting = false; void logic(){ + /* * NPCSelected is used to insure that only one NPC is made interactable with the mouse * if, for example, multiple entities are occupying one space. diff --git a/src/entities.cpp b/src/entities.cpp index 6a2f4db..cd8d29e 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -83,8 +83,8 @@ Player::Player(){ //sets all of the player specific traits on object creation subtype = 0; health = maxHealth = 100; speed = 1; - //tex = new Texturec(3, "assets/player1.png", "assets/player.png", "assets/player2.png"); - tex = new Texturec(3, "assets/maybeplayer.png", "assets/maybeplayer.png", "assets/maybeplayer.png"); + tex = new Texturec(3, "assets/player1.png", "assets/player.png", "assets/player2.png"); + //tex = new Texturec(3, "assets/maybeplayer.png", "assets/maybeplayer.png", "assets/maybeplayer.png"); inv = new Inventory(PLAYER_INV_SIZE); } Player::~Player(){ diff --git a/src/world.cpp b/src/world.cpp index 7f4d6f6..0ecf7ee 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -555,7 +555,6 @@ LOOP2: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); //for the s direction glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); //for the t direction glBegin(GL_QUADS); - std::cout< Date: Mon, 4 Jan 2016 08:48:27 -0500 Subject: save/load tests --- Changelog | 16 ++++++++++++++++ Goals.txt | 4 ++-- include/ui.h | 2 ++ include/world.h | 10 +++++++--- main.cpp | 1 + src/entities.cpp | 4 ++-- src/gameplay.cpp | 25 ++++++++++++++++++++++--- src/ui.cpp | 19 ++++++++++++++----- src/world.cpp | 37 +++++++++++++++++++++++++++++-------- 9 files changed, 95 insertions(+), 23 deletions(-) (limited to 'src/entities.cpp') diff --git a/Changelog b/Changelog index c0d2b9e..e3ed5e0 100644 --- a/Changelog +++ b/Changelog @@ -460,3 +460,19 @@ - added World::getAvailableNPC() for easy quest assigner assigning - added the Condition class, so that events and actions can be remembered by NPCs - added functionality for multiple lights (GLSL) + +12/22/2015: +=========== + + - 3 month Changelog anniversary! + - fixed dialog quitting bug + - worked on village spawning + - worked on wrapping text for dialog boxes + - did more work on GLSL shaders + +1/3/2015: +========= + + - finished wrapping text for dialog boxes + - began working on world saving/loading again + - got some mad GLSL shaders running diff --git a/Goals.txt b/Goals.txt index 140cdd3..4848bac 100644 --- a/Goals.txt +++ b/Goals.txt @@ -35,7 +35,7 @@ End of December: - create first 'chapters' of story - create very first areas in the game (code it) to get an idea of what's gonna go down - + January - March: =============== @@ -45,7 +45,7 @@ January - March: February - End of March: ======================== - - design sound effects / background music? + - design or focus on sound effects / background music? March-ish: ====== diff --git a/include/ui.h b/include/ui.h index 6a81dab..b769bbf 100644 --- a/include/ui.h +++ b/include/ui.h @@ -33,6 +33,8 @@ namespace ui { extern unsigned char dialogOptChosen; extern bool dialogImportant; + extern unsigned int textWrapLimit; + /* * Initializes the FreeType system. */ diff --git a/include/world.h b/include/world.h index 98ca54a..cdcea3c 100644 --- a/include/world.h +++ b/include/world.h @@ -8,6 +8,9 @@ #ifndef WORLD_H #define WORLD_H +#include +#include + #include #include @@ -128,6 +131,7 @@ protected: */ Texturec *bgTex; + WORLD_BG_TYPE bgType; /** * The Mix_Music object that holds the background soundtrack for the world. @@ -167,7 +171,7 @@ public: std::vector particles; void addStructure(_TYPE t,BUILD_SUB sub,float x,float y,World *inside); - void addVillage(int bCount, int npcMin, int npcMax,_TYPE t,float x,float y,World *outside); + void addVillage(int bCount, int npcMin, int npcMax,_TYPE t,World *inside); void addMob(int t,float x,float y); void addMob(int t,float x,float y,void (*hey)(Mob *)); void addNPC(float x,float y); @@ -262,8 +266,8 @@ public: int getTheWidth(void); - void save(FILE *); - void load(FILE *); + void save(std::ofstream *); + void load(std::ifstream *); }; /* diff --git a/main.cpp b/main.cpp index e6ff2eb..2c088cb 100644 --- a/main.cpp +++ b/main.cpp @@ -480,6 +480,7 @@ void mainLoop(void){ if(prev != currentWorld){ currentWorld->bgmPlay(prev); + ui::dialogBoxExists = false; } if(prevPrevTime + MSEC_PER_TICK <= currentTime){ diff --git a/src/entities.cpp b/src/entities.cpp index 3849040..d424aba 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -101,7 +101,7 @@ NPC::NPC(){ //sets all of the NPC specific traits on object creation tex = new Texturec(1,"assets/NPC.png"); inv = new Inventory(NPC_INV_SIZE); - randDialog = rand() % 12 - 1; + randDialog = 6;//rand() % 12 - 1; } NPC::~NPC(){ while(!aiFunc.empty()){ @@ -341,7 +341,7 @@ const char *randomDialog[] = { "How much wood could a woodchuck chuck if a woodchuck could chuck wood?", "I don\'t think anyone has ever been able to climb up that hill.", "If you ever see a hole in the ground, watch out; it could mean the end for you.", - "Did you know this game has over 4000 lines of code? I didn\'t. I didn't even know I was in a game until now...", + "Did you know this game has over 5000 lines of code? I didn\'t. I didn't even know I was in a game until now...", "HELP MY CAPS LOCK IS STUCK", "You know, if anyone ever asked me who I wanted to be when I grow up, I would say Abby Ross.", "I want to have the wallpaper in our house changed. It doesn\'t really fit the environment.", diff --git a/src/gameplay.cpp b/src/gameplay.cpp index 7dbe98e..ff77728 100644 --- a/src/gameplay.cpp +++ b/src/gameplay.cpp @@ -115,15 +115,21 @@ static World *worldSpawnHill3; static IndoorWorld *worldSpawnHill2_Building1; +static World *worldFirstVillage; /* * initEverything() start */ void destroyEverything(void); void initEverything(void){ - + //static std::ifstream i ("world.dat",std::ifstream::in | std::ifstream::binary); + worldSpawnHill1 = new World(); - worldSpawnHill1->generateFunc(400,gen_worldSpawnHill1); + /*if(!i.fail()){ + worldSpawnHill1->load(&i); + i.close(); + }else*/ + worldSpawnHill1->generateFunc(400,gen_worldSpawnHill1); worldSpawnHill1->setBackground(BG_FOREST); worldSpawnHill1->setBGM("assets/music/embark.wav"); worldSpawnHill1->addMob(MS_TRIGGER,0,0,worldSpawnHill1_hillBlock); @@ -138,12 +144,18 @@ void initEverything(void){ worldSpawnHill3->generateFunc(1000,gen_worldSpawnHill3); worldSpawnHill3->setBackground(BG_FOREST); worldSpawnHill3->setBGM("assets/music/ozone.wav"); - worldSpawnHill3->addHole(800,1000); + + worldFirstVillage = new World(); + worldFirstVillage->generate(1000); + worldFirstVillage->setBackground(BG_FOREST); + worldFirstVillage->setBGM("assets/music/embark.wav"); worldSpawnHill1->toRight = worldSpawnHill2; worldSpawnHill2->toLeft = worldSpawnHill1; worldSpawnHill2->toRight = worldSpawnHill3; worldSpawnHill3->toLeft = worldSpawnHill2; + worldSpawnHill3->toRight = worldFirstVillage; + worldFirstVillage->toLeft = worldSpawnHill3; /* * Spawn some entities. @@ -166,6 +178,8 @@ void initEverything(void){ worldSpawnHill2->addStructure(STRUCTURET,HOUSE,(rand()%120*HLINE),100,worldSpawnHill2_Building1); worldSpawnHill2->getAvailableNPC()->addAIFunc(worldSpawnHill2_Quest1,false); + worldFirstVillage->addVillage(5,0,0,STRUCTURET,worldSpawnHill2_Building1); + player = new Player(); player->spawn(200,100); @@ -178,6 +192,11 @@ extern std::vector AIpreload; extern std::vector AIpreaddr; void destroyEverything(void){ + static std::ofstream o; + o.open("world.dat",std::ifstream::binary); + worldSpawnHill1->save(&o); + o.close(); + while(!AIpreload.empty()) AIpreload.pop_back(); while(!AIpreaddr.empty()) diff --git a/src/ui.cpp b/src/ui.cpp index f73f48a..b827b43 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -95,6 +95,8 @@ namespace ui { bool dialogImportant = false; unsigned char dialogOptChosen = 0; + unsigned int textWrapLimit = 110; + /* * Current font size. Changing this WILL NOT change the font size, see setFontSize() for * actual font size changing. @@ -266,7 +268,13 @@ namespace ui { */ do{ - if(s[i]=='\n'){ // Handle newlines + if(i && ((i / 110.0) == (i / 110))){ + yo-=fontSize*1.05; + xo=x; + if(s[i] == ' ') + i++; + } + if(s[i] == '\n'){ yo-=fontSize*1.05; xo=x; }else if(s[i]==' '){ // Handle spaces @@ -308,12 +316,13 @@ namespace ui { * Draw a string in a typewriter-esque fashion. Each letter is rendered as calls are made * to this function. Passing a different string to the function will reset the counters. */ - + + static char *ret = NULL; char *typeOut(char *str){ static unsigned int sinc, // Acts as a delayer for the space between each character. linc=0, // Contains the number of letters that should be drawn. size=0; // Contains the full size of the current string. - static char *ret = NULL; + //static char *ret = NULL; /* * Create a well-sized buffer if we haven't yet. @@ -453,6 +462,8 @@ namespace ui { dialogBoxExists = true; dialogImportant = false; + if(ret) + ret[0] = '\0'; } void waitForDialog(void){ do{ @@ -667,7 +678,6 @@ DONE: memcpy(&player->loc,&tmppos,sizeof(vec2)); currentWorld = tmp; toggleBlackFast(); - dialogBoxExists = false; } } break; @@ -689,7 +699,6 @@ DONE: memcpy(&player->loc,&tmppos,sizeof(vec2)); currentWorld = tmp; toggleBlackFast(); - dialogBoxExists = false; } } break; diff --git a/src/world.cpp b/src/world.cpp index 5663086..2afbdf5 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -50,6 +50,7 @@ float worldGetYBase(World *w){ } void World::setBackground(WORLD_BG_TYPE bgt){ + bgType = bgt; switch(bgt){ case BG_FOREST: bgTex = new Texturec(7,bgPaths[0]); @@ -60,12 +61,28 @@ void World::setBackground(WORLD_BG_TYPE bgt){ } } -void World::save(FILE *s){ - fclose(s); +void World::save(std::ofstream *o){ + o->write((char *)&lineCount, sizeof(unsigned int)); + o->write((char *)&line ,lineCount * sizeof(struct line_t)); + o->write("GG" ,2 * sizeof(char)); + o->write((char *)&star ,100 * sizeof(vec2)); } -void World::load(FILE *s){ - fclose(s); +void World::load(std::ifstream *i){ + static char end[2]; + + i->read((char *)&lineCount,sizeof(unsigned int)); + line = new struct line_t[lineCount]; + + i->read((char *)&line,lineCount * sizeof(struct line_t)); + i->read(end ,2 * sizeof(char)); + if(strncmp(end,"GG",2)){ + std::cout<<"world.dat corrupt"<read((char *)&star,100 * sizeof(vec2)); + + x_start = 0 - getWidth(this) / 2; } World::World(void){ @@ -913,16 +930,20 @@ void World::addStructure(_TYPE t,BUILD_SUB sub, float x,float y,World *inside){ entity.push_back(build.back()); } -void World::addVillage(int bCount, int npcMin, int npcMax,_TYPE t,float x,float y,World *outside){ +void World::addVillage(int bCount, int npcMin, int npcMax,_TYPE t,World *inside){ std::cout << npcMin << ", " << npcMax << std::endl; - int xwasd; + //int xwasd; for(int i = 0; i < bCount; i++){ - xwasd = (rand()%(int)x+1000*HLINE); + addStructure(t,HOUSE,x_start + (i * 300),100,inside); + /*std::cout<<"1\n"; HERE: + xwasd = (rand()%(int)x+1000*HLINE); for(auto &bu : build){ if(xwasd > bu->loc.x && xwasd < bu->loc.x+bu->width)goto HERE; } - addStructure(t,HOUSE,xwasd,y,outside); + std::cout<<"2\n"; + addStructure(t,HOUSE,xwasd,y,inside); + std::cout<<"3\n";*/ } } void World::addMob(int t,float x,float y){ -- cgit v1.2.3 From 45edad31559852d306d59b50f380cb79c9f27dcc Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Tue, 5 Jan 2016 08:48:29 -0500 Subject: save/load stuffs --- Changelog | 6 +++++ include/entities.h | 35 +++++++++++++++++++++++++++ include/inventory.h | 17 +++++++++++++ src/entities.cpp | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/gameplay.cpp | 11 +++++---- src/world.cpp | 57 +++++++++++++++++++++++++++++++++++++------ 6 files changed, 183 insertions(+), 13 deletions(-) (limited to 'src/entities.cpp') diff --git a/Changelog b/Changelog index e3ed5e0..68edc5e 100644 --- a/Changelog +++ b/Changelog @@ -476,3 +476,9 @@ - finished wrapping text for dialog boxes - began working on world saving/loading again - got some mad GLSL shaders running + +1/4/2015: +========= + + - fixed basic world save/load, working on entity saving + - GLSL shaders worked? diff --git a/include/entities.h b/include/entities.h index 6b8cc32..894dc16 100644 --- a/include/entities.h +++ b/include/entities.h @@ -45,6 +45,31 @@ enum BUILD_SUB{ FOUNTAIN }; +typedef struct { + InventorySavePacket isp; + vec2 loc; + vec2 vel; + float width; + float height; + float speed; + float health; + float maxHealth; + int subtype; + int ticksToUse; + unsigned int randDialog; + unsigned char ground; + bool near; + bool canMove; + bool right,left; + bool alive; + bool hit; + _TYPE type; + GENDER gender; + size_t nameSize; + //char *name; + //Texturec *tex; +} __attribute__ ((packed)) EntitySavePacket; + class World; class Particles{ @@ -139,6 +164,9 @@ public: virtual void interact(){} virtual ~Entity(){} + + char *baseSave(void); + void baseLoad(char *); }; class Player : public Entity { @@ -151,6 +179,10 @@ public: void interact(); }; +typedef struct { + EntitySavePacket esp; +} __attribute__ ((packed)) NPCSavePacket; + class NPC : public Entity{ public: std::vectoraiFunc; @@ -161,6 +193,9 @@ public: void addAIFunc(int (*func)(NPC *),bool preload); void interact(); void wander(int); + + char *save(unsigned int *size); + void load(char *b); }; class Structures : public Entity{ diff --git a/include/inventory.h b/include/inventory.h index 31b7d88..b035f91 100644 --- a/include/inventory.h +++ b/include/inventory.h @@ -60,6 +60,11 @@ struct item_t{ ITEM_ID id; } __attribute__((packed)); +typedef struct { + unsigned int size; + int os; + unsigned int sel; +} __attribute__ ((packed)) InventorySavePacket; class Inventory { private: @@ -87,6 +92,18 @@ public: void draw(void); // Draws a text list of items in this inventory (should only be called for the player for now) + char *save(void){ + static InventorySavePacket *isp = new InventorySavePacket(); + isp->size = size; + isp->os = os; + isp->sel = sel; + return (char *)isp; + } + void load(InventorySavePacket *isp){ + size = isp->size; + os = isp->os; + sel = isp->sel; + } }; void itemUse(void *p); diff --git a/src/entities.cpp b/src/entities.cpp index d424aba..f34abd6 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -490,3 +490,73 @@ void Mob::wander(int timeRun){ break; } } + +char *Entity::baseSave(void){ + static EntitySavePacket *esp = new EntitySavePacket(); + memcpy(&esp->isp,inv->save(),sizeof(InventorySavePacket)); + esp->loc = loc; + esp->vel = vel; + esp->width = width; + esp->height = height; + esp->speed = speed; + esp->health = health; + esp->maxHealth = maxHealth; + esp->subtype = subtype; + esp->ticksToUse = ticksToUse; + esp->randDialog = randDialog; + esp->ground = ground; + esp->near = near; + esp->canMove = canMove; + esp->right = right; + esp->left = left; + esp->alive = alive; + esp->hit = hit; + esp->type = type; + esp->gender = gender; + esp->nameSize = strlen(name) + 1; + return (char *)esp; +} + +void Entity::baseLoad(char *e){ + const char *tmpname = "GG\0"; + EntitySavePacket *esp = (EntitySavePacket *)e; + inv->load(&esp->isp); + loc = esp->loc; + vel = esp->vel; + width = esp->width; + height = esp->height; + speed = esp->speed; + health = esp->health; + maxHealth = esp->maxHealth; + subtype = esp->subtype; + ticksToUse = esp->ticksToUse; + randDialog = esp->randDialog; + ground = esp->ground; + near = esp->near; + canMove = esp->canMove; + right = esp->right; + left = esp->left; + alive = esp->alive; + hit = esp->hit; + type = esp->type; + gender = esp->gender; + name = new char[esp->nameSize+1]; + strcpy(name,tmpname); +} + +char *NPC::save(unsigned int *size){ + static char *buf = new char[(*size = sizeof(EntitySavePacket) + aiFunc.size() * sizeof(int(*)(NPC *)))],*esp; + memcpy(buf,(esp = baseSave()),sizeof(EntitySavePacket)); + delete[] esp; + memcpy(buf+sizeof(EntitySavePacket),aiFunc.data(),aiFunc.size() * sizeof(int(*)(NPC *))); + return buf; +} + +void NPC::load(char *b){ + unsigned int size = *(unsigned int *)b,size2,i; + baseLoad(b + sizeof(unsigned int)); + size2 = (size - sizeof(unsigned int) - sizeof(EntitySavePacket)) / sizeof(int(*)(NPC *)); + for(i=0;isetBackground(BG_FOREST); + if(!i.fail()){ worldSpawnHill1->load(&i); i.close(); - }else*/ + }else{ worldSpawnHill1->generateFunc(400,gen_worldSpawnHill1); - worldSpawnHill1->setBackground(BG_FOREST); - worldSpawnHill1->setBGM("assets/music/embark.wav"); + worldSpawnHill1->setBGM("assets/music/embark.wav"); + } worldSpawnHill1->addMob(MS_TRIGGER,0,0,worldSpawnHill1_hillBlock); worldSpawnHill2 = new World(); diff --git a/src/world.cpp b/src/world.cpp index 2afbdf5..34e1870 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -62,27 +62,68 @@ void World::setBackground(WORLD_BG_TYPE bgt){ } void World::save(std::ofstream *o){ + static unsigned int size2; + unsigned int size,i; + size_t bgms = strlen(bgm) + 1; + char *bufptr; + o->write((char *)&lineCount, sizeof(unsigned int)); - o->write((char *)&line ,lineCount * sizeof(struct line_t)); + o->write((char *)line ,lineCount * sizeof(struct line_t)); o->write("GG" ,2 * sizeof(char)); - o->write((char *)&star ,100 * sizeof(vec2)); + o->write((char *)star ,100 * sizeof(vec2)); + o->write((char *)&bgType , sizeof(WORLD_BG_TYPE)); + o->write((char *)&bgms , sizeof(size_t)); + o->write(bgm ,strlen(bgm)+1); + o->write("NO" ,2 * sizeof(char)); + + /*std::vector npc; + std::vector build; + std::vector mob; + std::vector entity; + std::vector object;*/ + + size = npc.size(); + for(i=0;isave(&size2); + o->write((char *)&size2,sizeof(unsigned int)); + o->write(bufptr,size2); + } } void World::load(std::ifstream *i){ - static char end[2]; + //unsigned int size; + size_t bgms; + char sig[2]; i->read((char *)&lineCount,sizeof(unsigned int)); + line = new struct line_t[lineCount]; + i->read((char *)line,lineCount * sizeof(struct line_t)); - i->read((char *)&line,lineCount * sizeof(struct line_t)); - i->read(end ,2 * sizeof(char)); - if(strncmp(end,"GG",2)){ + i->read(sig,2 * sizeof(char)); + if(strncmp(sig,"GG",2)){ std::cout<<"world.dat corrupt"<read((char *)&star,100 * sizeof(vec2)); - + x_start = 0 - getWidth(this) / 2; + + i->read((char *)star,100 * sizeof(vec2)); + i->read((char *)&bgType,sizeof(WORLD_BG_TYPE)); + + i->read((char *)&bgms,sizeof(size_t)); + bgm = new char[bgms]; + i->read(bgm,bgms); + setBGM(bgm); + + + i->read(sig,2 * sizeof(char)); + if(strncmp(sig,"NO",2)){ + std::cout<<"world.dat corrupt"< Date: Thu, 7 Jan 2016 08:35:51 -0500 Subject: world save/load done? --- Changelog | 6 +++ include/entities.h | 42 +++++++++++++--- src/entities.cpp | 145 +++++++++++++++++++++++++++++++++++++++++++++++------ src/gameplay.cpp | 23 +++++---- src/world.cpp | 83 +++++++++++++++++++++++++----- 5 files changed, 252 insertions(+), 47 deletions(-) (limited to 'src/entities.cpp') diff --git a/Changelog b/Changelog index 68edc5e..125aefc 100644 --- a/Changelog +++ b/Changelog @@ -482,3 +482,9 @@ - fixed basic world save/load, working on entity saving - GLSL shaders worked? + +1/5/2015: +========= + + - can save NPCs and Structures + - more shadering diff --git a/include/entities.h b/include/entities.h index 894dc16..0f7ac9b 100644 --- a/include/entities.h +++ b/include/entities.h @@ -66,7 +66,7 @@ typedef struct { _TYPE type; GENDER gender; size_t nameSize; - //char *name; + char name[32]; //Texturec *tex; } __attribute__ ((packed)) EntitySavePacket; @@ -179,10 +179,6 @@ public: void interact(); }; -typedef struct { - EntitySavePacket esp; -} __attribute__ ((packed)) NPCSavePacket; - class NPC : public Entity{ public: std::vectoraiFunc; @@ -195,9 +191,16 @@ public: void wander(int); char *save(unsigned int *size); - void load(char *b); + void load(unsigned int,char *b); }; +typedef struct { + EntitySavePacket esp; + World *inWorld; + World *inside; + BUILD_SUB bsubtype; +} __attribute__ ((packed)) StructuresSavePacket; + class Structures : public Entity{ public: World *inWorld; @@ -208,6 +211,9 @@ public: ~Structures(); unsigned int spawn(_TYPE, BUILD_SUB, float, float, World *); + + char *save(void); + void load(char *s); }; class Mob : public Entity{ @@ -220,19 +226,41 @@ public: ~Mob(); void wander(int); + + char *save(void); + void load(char *); }; +typedef struct { + EntitySavePacket esp; + double init_y; + //void (*hey)(Mob *callee); +} __attribute__ ((packed)) MobSavePacket; + +typedef struct { + EntitySavePacket esp; + ITEM_ID identifier; + bool questObject; + char pickupDialog[256]; +} __attribute__ ((packed)) ObjectSavePacket; + class Object : public Entity{ private: - int identifier; + ITEM_ID identifier; public: char *pickupDialog; bool questObject = false; + Object(); Object(ITEM_ID id, bool qo, const char *pd); ~Object(); + void reloadTexture(void); + void interact(void); + + char *save(void); + void load(char *); }; #endif // ENTITIES_H diff --git a/src/entities.cpp b/src/entities.cpp index f34abd6..eb653a9 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -123,6 +123,8 @@ Structures::Structures(){ //sets the structure type inWorld = NULL; name = NULL; + + inv = NULL; } Structures::~Structures(){ delete tex; @@ -171,6 +173,19 @@ Mob::~Mob(){ delete[] name; } +Object::Object(){ + type = OBJECTT; + alive = true; + near = false; + width = 0; + height = 0; + + maxHealth = health = 1; + + tex = NULL; + inv = NULL; +} + Object::Object(ITEM_ID id, bool qo, const char *pd){ identifier = id; questObject = qo; @@ -187,6 +202,7 @@ Object::Object(ITEM_ID id, bool qo, const char *pd){ maxHealth = health = 1; tex = new Texturec(1,getItemTexturePath(id)); + inv = NULL; } Object::~Object(){ delete[] pickupDialog; @@ -194,6 +210,15 @@ Object::~Object(){ delete[] name; } +void Object::reloadTexture(void){ + if(tex) + delete tex; + + tex = new Texturec(1,getItemTexturePath(identifier)); + width = getItemWidth(identifier); + height = getItemHeight(identifier); +} + void Entity::draw(void){ //draws the entities glPushMatrix(); glColor3ub(255,255,255); @@ -492,8 +517,12 @@ void Mob::wander(int timeRun){ } char *Entity::baseSave(void){ - static EntitySavePacket *esp = new EntitySavePacket(); - memcpy(&esp->isp,inv->save(),sizeof(InventorySavePacket)); + static EntitySavePacket *esp; + esp = new EntitySavePacket(); + if(inv) + memcpy(&esp->isp,inv->save(),sizeof(InventorySavePacket)); + else + memset(&esp->isp,0,sizeof(InventorySavePacket)); esp->loc = loc; esp->vel = vel; esp->width = width; @@ -513,14 +542,20 @@ char *Entity::baseSave(void){ esp->hit = hit; esp->type = type; esp->gender = gender; - esp->nameSize = strlen(name) + 1; + if(name){ + esp->nameSize = strlen(name) + 1; + strncpy(esp->name,name,32); + }else{ + esp->nameSize = 0; + strcpy(esp->name,"\0"); + } return (char *)esp; } void Entity::baseLoad(char *e){ - const char *tmpname = "GG\0"; EntitySavePacket *esp = (EntitySavePacket *)e; - inv->load(&esp->isp); + if(esp->nameSize > 1) + inv->load(&esp->isp); loc = esp->loc; vel = esp->vel; width = esp->width; @@ -540,23 +575,101 @@ void Entity::baseLoad(char *e){ hit = esp->hit; type = esp->type; gender = esp->gender; - name = new char[esp->nameSize+1]; - strcpy(name,tmpname); + if(esp->nameSize){ + name = new char[esp->nameSize+1]; + strcpy(name,esp->name); + }else{ + name = new char[4]; + strncpy(name,"\0\0\0\0",4); + } } char *NPC::save(unsigned int *size){ - static char *buf = new char[(*size = sizeof(EntitySavePacket) + aiFunc.size() * sizeof(int(*)(NPC *)))],*esp; + static char *buf,*esp; + buf = new char[(*size = sizeof(EntitySavePacket) /*+ aiFunc.size() * sizeof(int(*)(NPC *))*/)]; memcpy(buf,(esp = baseSave()),sizeof(EntitySavePacket)); delete[] esp; - memcpy(buf+sizeof(EntitySavePacket),aiFunc.data(),aiFunc.size() * sizeof(int(*)(NPC *))); + //memcpy(buf+sizeof(EntitySavePacket),aiFunc.data(),aiFunc.size() * sizeof(int(*)(NPC *))); return buf; } -void NPC::load(char *b){ - unsigned int size = *(unsigned int *)b,size2,i; - baseLoad(b + sizeof(unsigned int)); - size2 = (size - sizeof(unsigned int) - sizeof(EntitySavePacket)) / sizeof(int(*)(NPC *)); - for(i=0;i sizeof(EntitySavePacket)){ + size2 = (size - sizeof(EntitySavePacket)) / sizeof(int(*)(NPC *)); + std::cout<esp,esp,sizeof(EntitySavePacket)); + delete[] esp; + ssp->inWorld = inWorld; + ssp->inside = inside; + ssp->bsubtype = bsubtype; + return (char *)ssp; +} + +void Structures::load(char *s){ + StructuresSavePacket *ssp = (StructuresSavePacket *)s; + baseLoad((char *)&ssp->esp); + inWorld = ssp->inWorld; + inside = ssp->inside; + bsubtype = ssp->bsubtype; +} + +char *Object::save(void){ + static ObjectSavePacket *osp; + char *esp; + osp = new ObjectSavePacket(); + memcpy(&osp->esp,(esp = baseSave()),sizeof(EntitySavePacket)); + delete[] esp; + osp->identifier = identifier; + osp->questObject = questObject; + strncpy(osp->pickupDialog,pickupDialog,256); + return (char *)osp; +} + +void Object::load(char *buf){ + ObjectSavePacket *osp = (ObjectSavePacket *)buf; + baseLoad((char *)&osp->esp); + identifier = osp->identifier; + questObject = osp->questObject; + pickupDialog = new char[256]; + strcpy(pickupDialog,osp->pickupDialog); +} + +char *Mob::save(void){ + static MobSavePacket *msp; + char *esp; + msp = new MobSavePacket(); + memcpy(&msp->esp,(esp = baseSave()),sizeof(MobSavePacket)); + delete[] esp; + msp->init_y = init_y; + return (char *)msp; +} + +void Mob::load(char *m){ + MobSavePacket *msp = (MobSavePacket *)m; + baseLoad((char *)&msp->esp); + init_y = msp->init_y; } diff --git a/src/gameplay.cpp b/src/gameplay.cpp index 94fb954..5b9f509 100644 --- a/src/gameplay.cpp +++ b/src/gameplay.cpp @@ -122,34 +122,35 @@ static World *worldFirstVillage; void destroyEverything(void); void initEverything(void){ - static std::ifstream i ("world.dat",std::ifstream::in | std::ifstream::binary); + //static std::ifstream i ("world.dat",std::ifstream::in | std::ifstream::binary); worldSpawnHill1 = new World(); worldSpawnHill1->setBackground(BG_FOREST); - if(!i.fail()){ + /*if(!i.fail()){ worldSpawnHill1->load(&i); i.close(); - }else{ + }else{*/ worldSpawnHill1->generateFunc(400,gen_worldSpawnHill1); worldSpawnHill1->setBGM("assets/music/embark.wav"); - } + //} worldSpawnHill1->addMob(MS_TRIGGER,0,0,worldSpawnHill1_hillBlock); + worldSpawnHill1->addNPC(300,100); worldSpawnHill2 = new World(); - worldSpawnHill2->generate(700); worldSpawnHill2->setBackground(BG_FOREST); worldSpawnHill2->setBGM("assets/music/ozone.wav"); + worldSpawnHill2->generate(700); worldSpawnHill2->addMob(MS_PAGE,-400,0,worldSpawnHill2_infoSprint); - + worldSpawnHill3 = new World(); worldSpawnHill3->generateFunc(1000,gen_worldSpawnHill3); worldSpawnHill3->setBackground(BG_FOREST); worldSpawnHill3->setBGM("assets/music/ozone.wav"); worldFirstVillage = new World(); - worldFirstVillage->generate(1000); worldFirstVillage->setBackground(BG_FOREST); worldFirstVillage->setBGM("assets/music/embark.wav"); + worldFirstVillage->generate(1000); worldSpawnHill1->toRight = worldSpawnHill2; worldSpawnHill2->toLeft = worldSpawnHill1; @@ -184,7 +185,7 @@ void initEverything(void){ player = new Player(); player->spawn(200,100); - currentWorld = worldSpawnHill1; + currentWorld = worldSpawnHill1; currentWorld->bgmPlay(NULL); atexit(destroyEverything); } @@ -193,10 +194,10 @@ extern std::vector AIpreload; extern std::vector AIpreaddr; void destroyEverything(void){ - static std::ofstream o; + /*static std::ofstream o; o.open("world.dat",std::ifstream::binary); - worldSpawnHill1->save(&o); - o.close(); + worldSpawnHill2->save(&o); + o.close();*/ while(!AIpreload.empty()) AIpreload.pop_back(); diff --git a/src/world.cpp b/src/world.cpp index 34e1870..8b09398 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -66,7 +66,7 @@ void World::save(std::ofstream *o){ unsigned int size,i; size_t bgms = strlen(bgm) + 1; char *bufptr; - + o->write((char *)&lineCount, sizeof(unsigned int)); o->write((char *)line ,lineCount * sizeof(struct line_t)); o->write("GG" ,2 * sizeof(char)); @@ -75,25 +75,41 @@ void World::save(std::ofstream *o){ o->write((char *)&bgms , sizeof(size_t)); o->write(bgm ,strlen(bgm)+1); o->write("NO" ,2 * sizeof(char)); - - /*std::vector npc; - std::vector build; - std::vector mob; - std::vector entity; - std::vector object;*/ - size = npc.size(); + o->write((char *)&size,sizeof(unsigned int)); for(i=0;isave(&size2); o->write((char *)&size2,sizeof(unsigned int)); o->write(bufptr,size2); + delete[] bufptr; + } + size = build.size(); + o->write((char *)&size,sizeof(unsigned int)); + for(i=0;isave(); + o->write(bufptr,sizeof(StructuresSavePacket)); + delete[] bufptr; + } + size = object.size(); + o->write((char *)&size,sizeof(unsigned int)); + for(i=0;isave(); + o->write(bufptr,sizeof(ObjectSavePacket)); + delete[] bufptr; + } + size = mob.size(); + o->write((char *)&size,sizeof(unsigned int)); + for(i=0;isave(); + o->write(bufptr,sizeof(MobSavePacket)); + delete[] bufptr; } } void World::load(std::ifstream *i){ - //unsigned int size; + unsigned int size,size2,j; size_t bgms; - char sig[2]; + char sig[2],*buf; i->read((char *)&lineCount,sizeof(unsigned int)); @@ -102,7 +118,7 @@ void World::load(std::ifstream *i){ i->read(sig,2 * sizeof(char)); if(strncmp(sig,"GG",2)){ - std::cout<<"world.dat corrupt"<read(bgm,bgms); setBGM(bgm); - i->read(sig,2 * sizeof(char)); if(strncmp(sig,"NO",2)){ - std::cout<<"world.dat corrupt"<read((char *)&size,sizeof(unsigned int)); + for(j=0;jread((char *)&size2,sizeof(unsigned int)); + buf = new char[size2]; + i->read(buf,size2); + npc.push_back(new NPC()); + npc.back()->load(size2,buf); + entity.push_back(npc.back()); + delete[] buf; + } + static StructuresSavePacket *ssp; + ssp = new StructuresSavePacket(); + i->read((char *)&size,sizeof(unsigned int)); + for(j=0;jread((char *)ssp,sizeof(StructuresSavePacket)); + build.push_back(new Structures()); + build.back()->load((char *)ssp); + entity.push_back(build.back()); + } + delete ssp; + + static ObjectSavePacket *osp; + osp = new ObjectSavePacket(); + i->read((char *)&size,sizeof(unsigned int)); + for(j=0;jread((char *)osp,sizeof(ObjectSavePacket)); + object.push_back(new Object()); + object.back()->load((char *)osp); + object.back()->reloadTexture(); + entity.push_back(object.back()); + } + delete osp; + + static MobSavePacket *msp; + msp = new MobSavePacket(); + i->read((char *)&size,sizeof(unsigned int)); + for(j=0;jread((char *)msp,sizeof(MobSavePacket)); + mob.push_back(new Mob(0)); + mob.back()->load((char *)msp); + entity.push_back(mob.back()); + } } World::World(void){ -- cgit v1.2.3