]> git.tdb.fi Git - libs/game.git/commitdiff
Add resource container references to Director and Stage
authorMikko Rasa <tdb@tdb.fi>
Sat, 29 Oct 2022 18:30:25 +0000 (21:30 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 29 Oct 2022 22:25:28 +0000 (01:25 +0300)
Build
source/game/director.cpp
source/game/director.h
source/game/resources.cpp [new file with mode: 0644]
source/game/resources.h [new file with mode: 0644]
source/game/stage.cpp
source/game/stage.h

diff --git a/Build b/Build
index beae8cded01900b84295026c7bf46ffc93120e77..837d2a2b9ded68f9796ee0b524d163308d9cbf86 100644 (file)
--- a/Build
+++ b/Build
@@ -3,6 +3,7 @@ package "mspgame"
        version "0.1";
 
        require "mspcore";
+       require "mspdatafile";
        require "mspmath";
 
        build_info
index 28d86e7d142e23142839e186b8b4613c6299d8f6..96bd143dc542b525a66cf61cc16952915b233d8a 100644 (file)
@@ -7,13 +7,14 @@ using namespace std;
 
 namespace Msp::Game {
 
-Director::Director():
+Director::Director(DataFile::Collection &r):
+       resources(r),
        event_source(event_bus)
 { }
 
 Stage &Director::create_stage()
 {
-       stages.emplace_back(std::make_unique<Stage>());
+       stages.emplace_back(std::make_unique<Stage>(std::ref(resources)));
        event_source.emit<Events::StageCreated>(std::ref(*stages.back()));
        return *stages.back();
 }
index 5f33aef0a0dcbe48fe744832a17fe54556a3ad84..05be9f7172c653bd04a9a01f72920a42e123b665 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <memory>
 #include <vector>
+#include <msp/datafile/collection.h>
 #include <msp/time/timedelta.h>
 #include <msp/time/timestamp.h>
 #include "eventbus.h"
@@ -19,6 +20,7 @@ public:
        using EventSource = Game::EventSource<Events::StageCreated, Events::StageActivated, Events::StageDeactivated>;
 
 private:
+       DataFile::Collection &resources;
        EventBus event_bus;
        EventSource event_source;
        std::vector<std::unique_ptr<Stage>> stages;
@@ -30,8 +32,9 @@ private:
        unsigned max_backlog_steps = 600;
 
 public:
-       Director();
+       Director(DataFile::Collection &);
 
+       DataFile::Collection &get_resources() const { return resources; }
        EventBus &get_event_bus() { return event_bus; }
        EventSource &get_event_source() { return event_source; }
        const std::vector<std::unique_ptr<Stage>> &get_stages() const { return stages; }
diff --git a/source/game/resources.cpp b/source/game/resources.cpp
new file mode 100644 (file)
index 0000000..0893f97
--- /dev/null
@@ -0,0 +1,30 @@
+#include <msp/core/application.h>
+#include <msp/fs/dir.h>
+#include <msp/fs/stat.h>
+#include "resources.h"
+
+using namespace std;
+
+namespace Msp::Game {
+
+ApplicationResources::ApplicationResources()
+{
+       FS::Path data_dir = FS::get_sys_data_dir()/"data";
+#ifdef DEBUG
+       if(FS::exists(data_dir))
+               dir_src.add_directory(data_dir);
+       add_source(dir_src);
+#endif
+
+       FS::Path main_pack = FS::get_sys_data_dir()/(Application::get_name()+".mdp");
+       if(FS::exists(main_pack))
+               pack_src.add_pack_file(main_pack.str());
+       if(FS::exists(data_dir))
+       {
+               for(const string &f: FS::list_filtered(data_dir, "\\.mdp$"))
+                       pack_src.add_pack_file((data_dir/f).str());
+       }
+       add_source(pack_src);
+}
+
+} // namespace Msp::Game
diff --git a/source/game/resources.h b/source/game/resources.h
new file mode 100644 (file)
index 0000000..b8f10b0
--- /dev/null
@@ -0,0 +1,27 @@
+#ifndef MSP_GAME_RESOURCES_H_
+#define MSP_GAME_RESOURCES_H_
+
+#include <msp/datafile/collection.h>
+#include <msp/datafile/directorysource.h>
+#include <msp/datafile/packsource.h>
+
+namespace Msp::Game {
+
+class Resources: virtual public DataFile::Collection
+{
+public:
+};
+
+class ApplicationResources: public Resources
+{
+private:
+       DataFile::DirectorySource dir_src;
+       DataFile::PackSource pack_src;
+
+public:
+       ApplicationResources();
+};
+
+} // namespace Msp::Game
+
+#endif
index 8d781f28961fc77da688b640c9c579a44c9b4a28..779be6e02eeefa39c379d200b7dd5056406b29ea 100644 (file)
@@ -4,7 +4,8 @@
 
 namespace Msp::Game {
 
-Stage::Stage():
+Stage::Stage(DataFile::Collection &r):
+       resources(r),
        event_source(event_bus),
        root(std::make_unique<Root>(*this))
 { }
index d7793e22f60ec21fd52acab1b8af9aafbe2f4d94..6af3b60a62596b97d61f833baa0c937c395b063a 100644 (file)
@@ -2,6 +2,7 @@
 #define MSP_GAME_STAGE_H_
 
 #include <memory>
+#include <msp/datafile/collection.h>
 #include <msp/time/timedelta.h>
 #include "eventbus.h"
 #include "events.h"
@@ -20,6 +21,7 @@ public:
                Events::ComponentCreated, Events::ComponentDestroyed>;
 
 private:
+       DataFile::Collection &resources;
        PoolPool pools;
        EventBus event_bus;
        EventSource event_source;
@@ -29,9 +31,10 @@ private:
        std::vector<std::unique_ptr<System>> systems;
 
 public:
-       Stage();
+       Stage(DataFile::Collection &);
        ~Stage();
 
+       DataFile::Collection &get_resources() const { return resources; }
        PoolPool &get_pools() { return pools; }
        EventBus &get_event_bus() { return event_bus; }
        EventSource &get_event_source() { return event_source; }