]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
saving inventory
authorClyne Sullivan <tullivan99@gmail.com>
Tue, 5 Sep 2017 16:54:48 +0000 (12:54 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Tue, 5 Sep 2017 16:54:48 +0000 (12:54 -0400)
include/common.hpp
include/inventory.hpp
main.cpp
src/inventory.cpp
src/systems/dialog.cpp
src/tinyxml2.cpp
src/ui.cpp

index 9ecd912e6d18783af6e6cb0813996b373e784d10..a03a8880cd4d11b4385a8180bcfa1b4318bb2ea3 100644 (file)
@@ -23,11 +23,11 @@ constexpr float PI = 3.1415926535f;
  */
 unsigned int millis(void);
 
-namespace std {
+/*namespace std {
        template<class T>
        constexpr const T& clamp(const T& v, const T& lo, const T& hi) {
                return (v > hi) ? hi : ((v > lo) ? v : lo);
        }
-}
+}*/
 
 #endif // COMMON_HPP_
index 448e27f254071a186dc7785a98d01b4d35a9364a..f340a244132ecd44febe914203339f31c085318b 100644 (file)
@@ -124,6 +124,9 @@ public:
 
        static inline Item* getItem(const std::string& s)
        { return &itemList[s]; }
+
+       static bool save(void);
+       static void load(void);
 };
 
 #endif // INVENTORY_HPP_
index bcec5ab276922330be575c88b614c330df6d936d..51a577664455942bbe5a32c4d70556476438d70f 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -106,6 +106,8 @@ int main(int argc, char *argv[])
 
        WorldSystem::loader();
 
+       InventorySystem::load();
+
        /////////////////////////////
        //                         //
        // actually start the game //
@@ -172,6 +174,7 @@ int main(int argc, char *argv[])
 
        // save
        game::briceSave();
+       InventorySystem::save();
        WorldSystem::save();
 
        // exit
index f200705f21be8f2ecb39d46c4ad07213c5c93c81..b96d5cfe857fc567c4db12a8a1e25dc9027cc637 100644 (file)
@@ -4,12 +4,15 @@
 #include <components.hpp>
 #include <engine.hpp>
 #include <error.hpp>
+#include <fileio.hpp>
 #include <font.hpp>
 #include <player.hpp>
 #include <render.hpp>
 #include <ui.hpp>
 
 #include <forward_list>
+#include <iostream>
+#include <fstream>
 #include <unordered_map>
 
 #include <tinyxml2.h>
@@ -248,7 +251,7 @@ bool InventorySystem::receive(const MouseClickEvent &mce)
                                game::events.emit<UseItemEvent>(mce.position, items[0].item, &attack->second);
                }
        }
-       return true;
+       return false;
 }
 
 bool InventorySystem::receive(const MouseReleaseEvent &mre)
@@ -384,3 +387,42 @@ bool InventorySystem::take(const std::string& name, int count)
 
        return true;
 }
+
+bool InventorySystem::save(void)
+{      
+       std::ofstream s (game::config::xmlFolder + "inventory.dat");
+
+       // signature?
+       s << "831998\n";
+
+       for (const auto& i : items) {
+               if (i.item != nullptr && i.count > 0)
+                       s << std::string(i.item->name) << '\n' << i.count << '\n';
+       }
+       
+       // end of list?
+       s.close();
+       return true;
+}
+
+void InventorySystem::load(void)
+{
+       // attempt to load data
+       std::ifstream sf (game::config::xmlFolder + "inventory.dat");
+       if (sf.good()) {
+               sf.close();
+               auto lines = readFileA(game::config::xmlFolder + "inventory.dat");
+
+               // check signature
+               if (std::stoi(lines[0]) != 831998)
+                       UserError("Save file signature is invalid... (delete it)");
+
+               for (unsigned int i = 1; i < lines.size(); i += 2) {
+                       if (lines[i].size() > 0) {
+                               int count = std::stoi(lines[i + 1]);
+                               if (count > 0)
+                                       add(lines[i], count);
+                       }
+               }
+       }
+}
index dd07ab88ebcbf05cbbc0d8e9446e2fe5a5d71c77..ee7c8341b3ce27486a1d1e6f87982cc59e348965 100644 (file)
@@ -144,7 +144,7 @@ bool DialogSystem::receive(const MouseClickEvent &mce)
                        }
                }
        });
-       return true;
+       return false;
 }
 
 void DialogSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt)
index 7473881482196c7701253f0d58428b7b1e760623..d76535740d7909bc66f5567c1e4a0ee049317b38 100755 (executable)
@@ -409,17 +409,17 @@ void XMLUtil::ConvertUTF32ToUTF8(unsigned long input, char* output, int* length)
             --output;\r
             *output = (char)((input | BYTE_MARK) & BYTE_MASK);\r
             input >>= 6;\r
-           //[[fallthrough]];\r
+           [[fallthrough]];\r
         case 3:\r
             --output;\r
             *output = (char)((input | BYTE_MARK) & BYTE_MASK);\r
             input >>= 6;\r
-           //[[fallthrough]];\r
+           [[fallthrough]];\r
         case 2:\r
             --output;\r
             *output = (char)((input | BYTE_MARK) & BYTE_MASK);\r
             input >>= 6;\r
-           //[[fallthrough]];\r
+           [[fallthrough]];\r
         case 1:\r
             --output;\r
             *output = (char)(input | FIRST_BYTE_MARK[*length]);\r
index 7a10b20a6325de3c196cdde635980975f74bf74a..1043ae967d96c220309280be23abdb7456bae5cc 100644 (file)
@@ -691,10 +691,9 @@ bool InputSystem::receive(const MainSDLEvent& event)
        case SDL_KEYUP:
                ev.emit<KeyUpEvent>(SDL_KEY);
 
-               if (SDL_KEY == SDLK_ESCAPE)
+               if (SDL_KEY == SDLK_ESCAPE) {
                        ui::menu::toggle();
-
-               if (SDL_KEY == SDLK_h) {
+               } else if (SDL_KEY == SDLK_h) {
                        quest::toggle();
                } else switch (SDL_KEY) {
                case SDLK_F3: