diff --git a/framework/CMakeLists.txt b/framework/CMakeLists.txt index 6aa384a..8307a7f 100644 --- a/framework/CMakeLists.txt +++ b/framework/CMakeLists.txt @@ -39,7 +39,7 @@ add_executable (xna WIN32 "content/manager.cpp" "content/reader.cpp" "csharp/binary.cpp" -"content/decstream.cpp" "content/lzx/decoder.cpp" "content/lzx/decoderstream.cpp" "content/typereadermanager.cpp" "csharp/object.cpp" "csharp/type.cpp" "platform/init-dx.cpp") +"content/decstream.cpp" "content/lzx/decoder.cpp" "content/lzx/decoderstream.cpp" "content/typereadermanager.cpp" "csharp/object.cpp" "csharp/type.cpp" "platform/init-dx.cpp" "game/servicecontainer.cpp") if (CMAKE_VERSION VERSION_GREATER 3.12) set_property(TARGET xna PROPERTY CXX_STANDARD 20) diff --git a/framework/content/manager.hpp b/framework/content/manager.hpp index dfbf2a8..6c4e250 100644 --- a/framework/content/manager.hpp +++ b/framework/content/manager.hpp @@ -7,20 +7,26 @@ #include #include #include +#include "../game/servicecontainer.hpp" namespace xna { class ContentManager { public: friend class ContentReader; - ContentManager(String const& rootDirectory) : + ContentManager(String const& rootDirectory, sptr const& services) : _rootDirectory(rootDirectory), + _services(services), _path(rootDirectory){}; virtual ~ContentManager(){ Unload(); } + sptr Services() { + return _services; + } + constexpr String RootDirectory() const { return _rootDirectory; } @@ -74,6 +80,7 @@ namespace xna { std::map> _loadedAssets; inline const static String contentExtension = ".xnb"; std::vector byteBuffer; + sptr _services = nullptr; }; } diff --git a/framework/csharp/service.hpp b/framework/csharp/service.hpp new file mode 100644 index 0000000..b7a9cb8 --- /dev/null +++ b/framework/csharp/service.hpp @@ -0,0 +1,15 @@ +#ifndef XNA_CSHARP_SERVICE_HPP +#define XNA_CSHARP_SERVICE_HPP + +#include "../default.hpp" +#include "type.hpp" +#include + +namespace xna { + class IServiceProvider { + public: + virtual std::any GetService(Type& serviceType) = 0; + }; +} + +#endif \ No newline at end of file diff --git a/framework/forward.hpp b/framework/forward.hpp index e5d3eaf..dd6f930 100644 --- a/framework/forward.hpp +++ b/framework/forward.hpp @@ -53,6 +53,7 @@ namespace xna { class GraphicsDeviceManager; class IGameTime; class IGameComponent; + class GameServiceContainer; //Graphics class BlendState; diff --git a/framework/game/game.hpp b/framework/game/game.hpp index 936335a..d57154e 100644 --- a/framework/game/game.hpp +++ b/framework/game/game.hpp @@ -1,12 +1,11 @@ #ifndef XNA_GAME_GAME_HPP #define XNA_GAME_GAME_HPP -#include "../enums.hpp" -#include "../forward.hpp" -#include "../types.hpp" +#include "../default.hpp" #include "time.hpp" #include "window.hpp" -#include "../game/component.hpp" +#include "component.hpp" +#include "servicecontainer.hpp" namespace xna { class IGame { @@ -18,6 +17,7 @@ namespace xna { virtual sptr Window() = 0; virtual sptr GetGraphicsDevice() = 0; virtual sptr Components() = 0; + virtual sptr Services() = 0; protected: virtual void Draw(GameTime const& gameTime) = 0; diff --git a/framework/game/servicecontainer.cpp b/framework/game/servicecontainer.cpp new file mode 100644 index 0000000..20c2689 --- /dev/null +++ b/framework/game/servicecontainer.cpp @@ -0,0 +1,18 @@ +#include "servicecontainer.hpp" + +namespace xna { + void GameServiceContainer::AddService(Type& type, std::any& provider) + { + auto hashCode = type.GetHashCode(); + services.insert({ hashCode, provider }); + } + + std::any GameServiceContainer::GetService(Type& serviceType) + { + auto hashCode = serviceType.GetHashCode(); + + return services.contains(hashCode) + ? services[hashCode] + : std::any(); + } +} \ No newline at end of file diff --git a/framework/game/servicecontainer.hpp b/framework/game/servicecontainer.hpp index 2ce68aa..69255d0 100644 --- a/framework/game/servicecontainer.hpp +++ b/framework/game/servicecontainer.hpp @@ -2,12 +2,19 @@ #define XNA_GAME_SERVICECONTAINER_HPP #include "../default.hpp" +#include "../csharp/service.hpp" +#include namespace xna { - class IServiceProvider { - }; + class GameServiceContainer : public IServiceProvider { + public: + void AddService(Type& type, std::any& provider); - class GameServiceContainer { + // Inherited via IServiceProvider + std::any GetService(Type& serviceType) override; + + private: + std::map services; }; } diff --git a/framework/platform/game-dx.cpp b/framework/platform/game-dx.cpp index 365726f..5aff1c0 100644 --- a/framework/platform/game-dx.cpp +++ b/framework/platform/game-dx.cpp @@ -13,6 +13,8 @@ namespace xna { Game::Game() { + _services = New(); + _gameWindow = New(); _gameWindow->Color(255, 155, 55); _gameWindow->Title("XN65"); diff --git a/framework/platform/game-dx.hpp b/framework/platform/game-dx.hpp index b1b42c0..93549be 100644 --- a/framework/platform/game-dx.hpp +++ b/framework/platform/game-dx.hpp @@ -5,6 +5,7 @@ #include "../game/game.hpp" #include "dxheaders.hpp" #include "dx/StepTimer.hpp" +#include "../content/manager.hpp" namespace xna { class Game : public IGame { @@ -30,6 +31,14 @@ namespace xna { return _gameComponents; } + sptr Services() override { + return _services; + } + + sptr Content() { + return contentManager; + } + constexpr void EnableGameComponents(bool value) { _enabledGameComponents = value; } @@ -52,14 +61,16 @@ namespace xna { GameTime _currentGameTime{}; DX::StepTimer _stepTimer; - + sptr contentManager; + sptr _services = nullptr; + private: int startLoop(); void step(); sptr _gameComponents = nullptr; std::vector> _drawableGameComponents; size_t _drawableGameComponentsCount{ 0 }; - bool _enabledGameComponents{ false }; + bool _enabledGameComponents{ false }; }; } diff --git a/framework/xna.cpp b/framework/xna.cpp index 371475a..3db47bb 100644 --- a/framework/xna.cpp +++ b/framework/xna.cpp @@ -9,12 +9,12 @@ using namespace xna; namespace xna { class Game1 : public Game { public: - Game1() { + Game1() : Game() { auto _game = reinterpret_cast(this); graphics = New(_game); graphics->PreferredBackBufferWidth(1280); graphics->PreferredBackBufferHeight(720); - contentManager = New("Content"); + contentManager = New("Content", _services); } void Initialize() override { @@ -65,8 +65,7 @@ namespace xna { MouseState currentState{}; MouseState oldState{}; float vel = 1; - int var = 0; - sptr contentManager; + int var = 0; Texture2D tx; }; }