]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
more saving/loading
authorClyne Sullivan <tullivan99@gmail.com>
Fri, 12 Feb 2016 13:47:15 +0000 (08:47 -0500)
committerClyne Sullivan <tullivan99@gmail.com>
Fri, 12 Feb 2016 13:47:15 +0000 (08:47 -0500)
12 files changed:
Changelog
assets/click.wav [deleted file]
assets/sounds/click.wav [new file with mode: 0644]
include/entities.h
include/inventory.h
include/world.h
main.cpp
src/config.cpp
src/entities.cpp
src/gameplay.cpp
src/ui.cpp
src/world.cpp

index 582259c4d9d190ac27e27f2d0292481f1d0dcbfb..0f4fdcfbf40bbb1a1369aefb5c821e4aee805cd4 100644 (file)
--- a/Changelog
+++ b/Changelog
        - 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 (file)
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 (file)
index 0000000..976fb42
Binary files /dev/null and b/assets/sounds/click.wav differ
index 9cc1b310594e5acc6502125b2488df32da2eee5c..d234768a3b3ba639bb5d90f758978d5e449d3775 100644 (file)
@@ -6,6 +6,8 @@
 #include <inventory.h>
 #include <Texture.h>
 
+#include <sstream>
+
 #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{
index af859a5b2134c2862827c38623ea94bed7598f73..8225ab27ce941e7da702c15a525b1dc8c8db6e8b 100644 (file)
@@ -31,10 +31,10 @@ struct item_t{
 
 class Inventory {
 private:
-       std::vector<item_t> items;
        unsigned int size;
        int os = 0;
 public:
+       std::vector<item_t> items;
        unsigned int sel;
        bool invOpen = false;
        bool invOpening = false;
index 47e11f52224b7623fb0cd99d78bc83bb61ae7252..0e26ed9eb23fd461baa7caa6b8b53f7d015f9cfa 100644 (file)
@@ -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
index da1790fa5473a71c062a79b4af3db48173ad9f38..19fe8d4382c0fa208d82e39d56acfde9feed04ea 100644 (file)
--- 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(){
 
index ca6db1b295dde6da4d6f5ef9c66b2fbdbd93a3e4..efa2523bfdf72c1b05a3a5e0e4ef86964c498702 100644 (file)
@@ -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..."<<std::endl;
+       if(vol->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"));
 }
index f079b034ec159e78b113ca036d8b2662f368d708..aa98809160a1e2ea449b185b3a5807dde180a47c 100644 (file)
@@ -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..."<<std::endl;
+       data.append(std::to_string((int)loc.x) + "\n");
+       data.append(std::to_string((int)loc.y) + "\n");
+       data.append(std::to_string((int)health) + "\n");
+       data.append(std::to_string((int)maxHealth) + "\n");
+       
+       data.append(std::to_string((int)inv->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<char> 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();
+       }
+}
index e252c26f3092352d217e1995c34828e2b0dfbdd8..4bbc672c36d1fd4b729d2f18162d2431cac9a6ec 100644 (file)
@@ -278,7 +278,7 @@ void initEverything(void){
         */
        
        for(unsigned int i=0;i<xmlFiles.size();i++){
-               if(strncmp(xmlFiles[i].c_str(),".",1) && strncmp(xmlFiles[i].c_str(),"..",2)){
+               if(xmlFiles[i] != "." && xmlFiles[i] != ".." && strcmp(xmlFiles[i].c_str()+xmlFiles[i].size()-3,"dat")){
                        
                        /*
                         * Read in the XML file.
@@ -307,9 +307,9 @@ void initEverything(void){
        /*
         * Spawn the player and begin the game.
         */
-       
+
        player = new Player();
-       player->spawn(200,100);
+       player->sspawn(0,100);
 
        currentWorld->bgmPlay(NULL);
        atexit(destroyEverything);
index 7c7319294f4d98ca7036388d14b76ed58badb6c8..6dad3f0985874ca1283e7cd984921ac937b8221f 100644 (file)
@@ -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<<Mix_GetError()<<std::endl;
+                       abort();
+               }
                battleStart = Mix_LoadWAV("assets/sounds/frig.wav");
                sanic = Mix_LoadWAV("assets/sounds/sanic.wav");
                Mix_Volume(1,50);
@@ -1178,6 +1183,7 @@ DONE:
                                if(SDL_KEY == SDLK_ESCAPE){
                                        //gameRunning = false;
                                        currentMenu = &pauseMenu;
+                                       player->save();
                                        return;
                                }
                                switch(SDL_KEY){
index 6118851ff0b0f803077904aa2e636a071238667c..21890072e7d8644c6ce4ffa766cc6922ca7cd94e 100644 (file)
@@ -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);