From ba7df965e73e121820f20e3a0e57631d078c11db Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Fri, 12 Feb 2016 08:47:15 -0500 Subject: more saving/loading --- Changelog | 18 +++++++++++++ assets/click.wav | Bin 17684 -> 0 bytes assets/sounds/click.wav | Bin 0 -> 17684 bytes include/entities.h | 5 +++- include/inventory.h | 2 +- include/world.h | 3 ++- main.cpp | 13 +++++++--- src/config.cpp | 30 +++++++++++++--------- src/entities.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++++--- src/gameplay.cpp | 6 ++--- src/ui.cpp | 10 ++++++-- src/world.cpp | 41 ++++++++++++++--------------- 12 files changed, 148 insertions(+), 47 deletions(-) delete mode 100644 assets/click.wav create mode 100644 assets/sounds/click.wav diff --git a/Changelog b/Changelog index 582259c..0f4fdcf 100644 --- a/Changelog +++ b/Changelog @@ -647,3 +647,21 @@ - began considering unique_ptr's because they're good - fixed inventory everything - themes work + - pushed template settings.xml + +2/11/2016: +========== + + - fixed text drawing for good + - fixed player boundary checks + - made importantText passive-able + - began work on saving player stats + - fixed dialogClick stuffs + - added readConfig checks + - added background layers and whatnot + +2/12/2016: +========== + + - made saving functionality for player coordinates, inventory, health and current world + - addded structured villages diff --git a/assets/click.wav b/assets/click.wav deleted file mode 100644 index 976fb42..0000000 Binary files a/assets/click.wav and /dev/null differ diff --git a/assets/sounds/click.wav b/assets/sounds/click.wav new file mode 100644 index 0000000..976fb42 Binary files /dev/null and b/assets/sounds/click.wav differ diff --git a/include/entities.h b/include/entities.h index 9cc1b31..d234768 100644 --- a/include/entities.h +++ b/include/entities.h @@ -6,6 +6,8 @@ #include #include +#include + #define DEBUG #define NPCp(n) ((NPC *)n) @@ -175,7 +177,8 @@ public: Player(); ~Player(); - void interact(); + void save(void); + void sspawn(float x,float y); }; class NPC : public Entity{ diff --git a/include/inventory.h b/include/inventory.h index af859a5..8225ab2 100644 --- a/include/inventory.h +++ b/include/inventory.h @@ -31,10 +31,10 @@ struct item_t{ class Inventory { private: - std::vector items; unsigned int size; int os = 0; public: + std::vector items; unsigned int sel; bool invOpen = false; bool invOpening = false; diff --git a/include/world.h b/include/world.h index 47e11f5..0e26ed9 100644 --- a/include/world.h +++ b/include/world.h @@ -342,7 +342,7 @@ public: * playable. */ - void setBGM(const char *path); + void setBGM(std::string path); /** * Plays/stops this world's BGM. If `prev` is not NULL, that world's BGM @@ -439,5 +439,6 @@ extern int worldShade; extern char *currentXML; World *loadWorldFromXML(const char *path); +World *loadWorldFromXMLNoSave(const char *path); #endif // WORLD_H diff --git a/main.cpp b/main.cpp index da1790f..19fe8d4 100644 --- a/main.cpp +++ b/main.cpp @@ -200,8 +200,6 @@ int main(/*int argc, char *argv[]*/){ gameRunning=false; - readConfig(); - /** * (Attempt to) Initialize SDL libraries so that we can use SDL facilities and eventually * make openGL calls. Exit if there was an error. @@ -241,6 +239,12 @@ int main(/*int argc, char *argv[]*/){ // Run Mix_Quit when main returns atexit(Mix_Quit); + /** + * Load saved settings into the game (see config/settings.xml) + */ + + readConfig(); + /* * Create a window for SDL to draw to. Most parameters are the default, except for the * following which are defined in include/common.h: @@ -382,6 +386,7 @@ int main(/*int argc, char *argv[]*/){ */ fadeIntensity = 250; + initEverything(); if(!currentWorld){ @@ -570,7 +575,7 @@ void render(){ glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); - glOrtho((offset.x-SCREEN_WIDTH/2),(offset.x+SCREEN_WIDTH/2),offset.y-SCREEN_HEIGHT/2,offset.y+SCREEN_HEIGHT/2,-1,1); + glOrtho(floor(offset.x-SCREEN_WIDTH/2),floor(offset.x+SCREEN_WIDTH/2),floor(offset.y-SCREEN_HEIGHT/2),floor(offset.y+SCREEN_HEIGHT/2),-1,1); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); @@ -729,7 +734,7 @@ void render(){ SDL_GL_SwapWindow(window); } -static volatile bool objectInteracting = false; +static bool objectInteracting = false; void logic(){ diff --git a/src/config.cpp b/src/config.cpp index ca6db1b..efa2523 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -9,9 +9,9 @@ extern unsigned int SCREEN_WIDTH; extern unsigned int SCREEN_HEIGHT; extern bool FULLSCREEN; -extern float VOLUME_MASTER; -extern float VOLUME_MUSIC; -extern float VOLUME_SFX; +extern float VOLUME_MASTER; +extern float VOLUME_MUSIC; +extern float VOLUME_SFX; XMLDocument xml; XMLElement *scr; @@ -19,6 +19,7 @@ XMLElement *vol; void readConfig(){ unsigned int uval; + float fval; bool bval; xml.LoadFile("config/settings.xml"); @@ -36,18 +37,23 @@ void readConfig(){ if(xml.FirstChildElement("hline")->QueryUnsignedAttribute("size",&uval) == XML_NO_ERROR) HLINE = uval; else HLINE = 3; - - /*SCREEN_WIDTH = scr->UnsignedAttribute("width"); - SCREEN_HEIGHT = scr->UnsignedAttribute("height"); - FULLSCREEN = scr->BoolAttribute("fullscreen"); - HLINE = xml.FirstChildElement("hline")->UnsignedAttribute("size");*/ vol = xml.FirstChildElement("volume"); - VOLUME_MASTER = vol->FirstChildElement("master")->FloatAttribute("volume"); - VOLUME_MUSIC = vol->FirstChildElement("music")->FloatAttribute("volume"); - VOLUME_SFX = vol->FirstChildElement("sfx")->FloatAttribute("volume"); - std::cout<<"Loading font through settings.xml..."<FirstChildElement("master")->QueryFloatAttribute("volume",&fval) == XML_NO_ERROR) + VOLUME_MASTER = fval; + else VOLUME_MASTER = 50; + if(vol->FirstChildElement("music")->QueryFloatAttribute("volume",&fval) == XML_NO_ERROR) + VOLUME_MUSIC = fval; + else VOLUME_MUSIC = 50; + if(vol->FirstChildElement("sfx")->QueryFloatAttribute("volume",&fval) == XML_NO_ERROR) + VOLUME_SFX = fval; + else VOLUME_SFX = 50; + + Mix_Volume(0,VOLUME_MASTER); + Mix_Volume(1,VOLUME_SFX); + Mix_VolumeMusic(VOLUME_MUSIC); + ui::initFonts(); ui::setFontFace(xml.FirstChildElement("font")->Attribute("path")); } diff --git a/src/entities.cpp b/src/entities.cpp index f079b03..aa98809 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -341,9 +341,6 @@ NOPE: if(near)ui::putStringCentered(loc.x+width/2,loc.y-ui::fontSize-HLINE/2,name); } -void Player::interact(){ -} - /* * NPC::wander, this makes the npcs wander around the near area * @@ -576,3 +573,67 @@ void Mob::wander(int timeRun){ break; } } + +void Player::save(void){ + std::string data; + std::ofstream out ("xml/main.dat",std::ios::out | std::ios::binary); + std::cout<<"Saving player data..."<items.size()) + "\n"); + for(auto &i : inv->items) + data.append(std::to_string((int)i.count) + "\n" + std::to_string((int)i.id) + "\n"); + + data.append((std::string)(currentXML+4) + "\n"); + + data.append("dOnE\0"); + out.write(data.c_str(),data.size()); + out.close(); +} + +void Player::sspawn(float x,float y){ + unsigned int i; + uint count; + std::ifstream in ("xml/main.dat",std::ios::in | std::ios::binary); + spawn(x,y); + + if(in.good()){ + std::istringstream data; + std::string ddata; + std::streampos len; + + in.seekg(0,std::ios::end); + len = in.tellg(); + in.seekg(0,std::ios::beg); + + std::vector buf (len,'\0'); + in.read(buf.data(),buf.size()); + + data.rdbuf()->pubsetbuf(buf.data(),buf.size()); + + std::getline(data,ddata); + loc.x = std::stoi(ddata); + std::getline(data,ddata); + loc.y = std::stoi(ddata); + std::getline(data,ddata); + health = std::stoi(ddata); + std::getline(data,ddata); + maxHealth = std::stoi(ddata); + + std::getline(data,ddata); + for(i = std::stoi(ddata);i;i--){ + std::getline(data,ddata); + count = std::stoi(ddata); + std::getline(data,ddata); + inv->items.push_back((item_t){count,(uint)std::stoi(ddata)}); + } + + std::getline(data,ddata); + currentWorld = loadWorldFromXMLNoSave(ddata.c_str()); + + in.close(); + } +} diff --git a/src/gameplay.cpp b/src/gameplay.cpp index e252c26..4bbc672 100644 --- a/src/gameplay.cpp +++ b/src/gameplay.cpp @@ -278,7 +278,7 @@ void initEverything(void){ */ for(unsigned int i=0;ispawn(200,100); + player->sspawn(0,100); currentWorld->bgmPlay(NULL); atexit(destroyEverything); diff --git a/src/ui.cpp b/src/ui.cpp index 7c73192..6dad3f0 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -59,7 +59,7 @@ extern Menu* currentMenu; extern Menu pauseMenu; -Mix_Chunk *dialogClick; +static Mix_Chunk *dialogClick; extern void mainLoop(void); @@ -146,7 +146,12 @@ namespace ui { #ifdef DEBUG DEBUG_printf("Initialized FreeType2.\n",NULL); #endif // DEBUG - dialogClick = Mix_LoadWAV("assets/click.wav"); + + dialogClick = Mix_LoadWAV("assets/sounds/click.wav"); + if(dialogClick == NULL){ + std::cout<save(); return; } switch(SDL_KEY){ diff --git a/src/world.cpp b/src/world.cpp index 6118851..2189007 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -284,10 +284,7 @@ void World::update(Player *p,unsigned int delta){ p->loc.y += p->vel.y * delta; p->loc.x +=(p->vel.x * p->speed) * delta; - /*if(p->inv->usingi){ - p->inv->useItem(); - }*/ - + /* * Update coordinates of all entities except for structures. */ @@ -329,20 +326,20 @@ void World::update(Player *p,unsigned int delta){ } } -void World::setBGM(const char *path){ - if(!bgm) delete[] bgm; - if(path != NULL){ - bgm = new char[strlen(path) + 1]; - strcpy(bgm,path); - bgmObj = Mix_LoadMUS(bgm); - }else{ - bgm = new char[1]; - bgm[0] = '\0'; - } +void World::setBGM(std::string path){ + bgm = new char[path.size()]; + strcpy(bgm,path.c_str()); + bgmObj = Mix_LoadMUS(bgm); } void World::bgmPlay(World *prev){ - if(prev && strcmp(bgm,prev->bgm)){ + if(prev){ + if(bgm != prev->bgm){ + Mix_FadeOutMusic(800); + Mix_VolumeMusic(50); + Mix_PlayMusic(bgmObj,-1); // Loop infinitely + } + }else{ Mix_FadeOutMusic(800); Mix_VolumeMusic(50); Mix_PlayMusic(bgmObj,-1); // Loop infinitely @@ -1431,6 +1428,15 @@ char *currentXML = NULL; extern World *currentWorld; World *loadWorldFromXML(const char *path){ + if(currentXML){ + currentWorld->save(); + delete[] currentXML; + } + + return loadWorldFromXMLNoSave(path); +} + +World *loadWorldFromXMLNoSave(const char *path){ XMLDocument xml; XMLElement *wxml; @@ -1442,11 +1448,6 @@ World *loadWorldFromXML(const char *path){ unsigned int size = 5 + strlen(path); - if(currentXML){ - currentWorld->save(); - delete[] currentXML; - } - memset((currentXML = new char[size]),0,size); strcpy(currentXML,"xml/"); strcat(currentXML,path); -- cgit v1.2.3 From 0d77d11b708e57546d8267e957230c30bc5d048c Mon Sep 17 00:00:00 2001 From: drumsetmonkey Date: Fri, 19 Feb 2016 00:12:50 -0500 Subject: Everything changes based off of master volume, volume updates in menu --- include/ui.h | 2 -- main.cpp | 2 -- src/config.cpp | 19 ++++++++----------- src/ui.cpp | 15 ++------------- src/world.cpp | 4 ++-- 5 files changed, 12 insertions(+), 30 deletions(-) diff --git a/include/ui.h b/include/ui.h index 38a4932..ccc16af 100644 --- a/include/ui.h +++ b/include/ui.h @@ -158,8 +158,6 @@ namespace ui { * Draw various menu items */ void quitGame(); - void quitMenu(); - void optionsMenuF(); void drawMenu(Menu* menu); diff --git a/main.cpp b/main.cpp index 19fe8d4..805fa00 100644 --- a/main.cpp +++ b/main.cpp @@ -377,8 +377,6 @@ int main(/*int argc, char *argv[]*/){ delete[] shaderSource; glEnable(GL_MULTISAMPLE); - - Mix_Volume(0,VOLUME_MASTER); /* * Create all the worlds, entities, mobs, and the player. This function is defined in diff --git a/src/config.cpp b/src/config.cpp index efa2523..72a071d 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -49,25 +49,22 @@ void readConfig(){ if(vol->FirstChildElement("sfx")->QueryFloatAttribute("volume",&fval) == XML_NO_ERROR) VOLUME_SFX = fval; else VOLUME_SFX = 50; - - Mix_Volume(0,VOLUME_MASTER); - Mix_Volume(1,VOLUME_SFX); - Mix_VolumeMusic(VOLUME_MUSIC); - + ui::initFonts(); ui::setFontFace(xml.FirstChildElement("font")->Attribute("path")); + updateConfig(); } void updateConfig(){ - vol->FirstChildElement("master")->SetAttribute("volume",VOLUME_MASTER); - vol->FirstChildElement("music")->SetAttribute("volume",VOLUME_MUSIC); - vol->FirstChildElement("sfx")->SetAttribute("volume", VOLUME_SFX); - Mix_Volume(0,VOLUME_MASTER); - Mix_Volume(1,VOLUME_SFX); - Mix_VolumeMusic(VOLUME_MUSIC); + Mix_Volume(1,VOLUME_SFX * (VOLUME_MASTER/100.0f)); + Mix_VolumeMusic(VOLUME_MUSIC * (VOLUME_MASTER/100.0f)); } void saveConfig(){ + vol->FirstChildElement("master")->SetAttribute("volume",VOLUME_MASTER); + vol->FirstChildElement("music")->SetAttribute("volume",VOLUME_MUSIC); + vol->FirstChildElement("sfx")->SetAttribute("volume", VOLUME_SFX); + xml.SaveFile("config/settings.xml", false); } diff --git a/src/ui.cpp b/src/ui.cpp index 3d635ab..2d00453 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -648,11 +648,7 @@ namespace ui { updateConfig(); saveConfig(); } - - void quitMenu(){ - currentMenu = NULL; - } - + menuItem createButton(vec2 l, dim2 d, Color c, const char* t, menuFunc f){ menuItem temp; temp.member = 0; @@ -717,20 +713,13 @@ namespace ui { return temp; } - char* stradd(const char* a, const char* b){ - size_t len = strlen(a) + strlen(b); - char *ret = (char*)malloc(len * sizeof(char) + 1); - *ret = '\0'; - - return strcat(strcat(ret,a),b); - } - /* * Draws the menu */ void drawMenu(Menu *menu){ setFontSize(24); + updateConfig(); SDL_Event e; mouse.x=premouse.x+offset.x-(SCREEN_WIDTH/2); diff --git a/src/world.cpp b/src/world.cpp index 25b7d35..51efaf2 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -381,12 +381,12 @@ void World::bgmPlay(World *prev){ if(prev){ if(bgm != prev->bgm){ Mix_FadeOutMusic(800); - Mix_VolumeMusic(50); + //Mix_VolumeMusic(50); Mix_PlayMusic(bgmObj,-1); // Loop infinitely } }else{ Mix_FadeOutMusic(800); - Mix_VolumeMusic(50); + //Mix_VolumeMusic(50); Mix_PlayMusic(bgmObj,-1); // Loop infinitely } } -- cgit v1.2.3