1
0
mirror of https://github.com/borgesdan/xn65 synced 2024-12-29 21:54:47 +01:00

Implementa GameServices

This commit is contained in:
Danilo 2024-05-06 10:32:17 -03:00
parent b194a27dad
commit 319b545a03
10 changed files with 75 additions and 15 deletions

View File

@ -39,7 +39,7 @@ add_executable (xna WIN32
"content/manager.cpp" "content/manager.cpp"
"content/reader.cpp" "content/reader.cpp"
"csharp/binary.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) if (CMAKE_VERSION VERSION_GREATER 3.12)
set_property(TARGET xna PROPERTY CXX_STANDARD 20) set_property(TARGET xna PROPERTY CXX_STANDARD 20)

View File

@ -7,20 +7,26 @@
#include <algorithm> #include <algorithm>
#include <filesystem> #include <filesystem>
#include <map> #include <map>
#include "../game/servicecontainer.hpp"
namespace xna { namespace xna {
class ContentManager { class ContentManager {
public: public:
friend class ContentReader; friend class ContentReader;
ContentManager(String const& rootDirectory) : ContentManager(String const& rootDirectory, sptr<GameServiceContainer> const& services) :
_rootDirectory(rootDirectory), _rootDirectory(rootDirectory),
_services(services),
_path(rootDirectory){}; _path(rootDirectory){};
virtual ~ContentManager(){ virtual ~ContentManager(){
Unload(); Unload();
} }
sptr<GameServiceContainer> Services() {
return _services;
}
constexpr String RootDirectory() const { constexpr String RootDirectory() const {
return _rootDirectory; return _rootDirectory;
} }
@ -74,6 +80,7 @@ namespace xna {
std::map<String, sptr<void>> _loadedAssets; std::map<String, sptr<void>> _loadedAssets;
inline const static String contentExtension = ".xnb"; inline const static String contentExtension = ".xnb";
std::vector<Byte> byteBuffer; std::vector<Byte> byteBuffer;
sptr<GameServiceContainer> _services = nullptr;
}; };
} }

View File

@ -0,0 +1,15 @@
#ifndef XNA_CSHARP_SERVICE_HPP
#define XNA_CSHARP_SERVICE_HPP
#include "../default.hpp"
#include "type.hpp"
#include <any>
namespace xna {
class IServiceProvider {
public:
virtual std::any GetService(Type& serviceType) = 0;
};
}
#endif

View File

@ -53,6 +53,7 @@ namespace xna {
class GraphicsDeviceManager; class GraphicsDeviceManager;
class IGameTime; class IGameTime;
class IGameComponent; class IGameComponent;
class GameServiceContainer;
//Graphics //Graphics
class BlendState; class BlendState;

View File

@ -1,12 +1,11 @@
#ifndef XNA_GAME_GAME_HPP #ifndef XNA_GAME_GAME_HPP
#define XNA_GAME_GAME_HPP #define XNA_GAME_GAME_HPP
#include "../enums.hpp" #include "../default.hpp"
#include "../forward.hpp"
#include "../types.hpp"
#include "time.hpp" #include "time.hpp"
#include "window.hpp" #include "window.hpp"
#include "../game/component.hpp" #include "component.hpp"
#include "servicecontainer.hpp"
namespace xna { namespace xna {
class IGame { class IGame {
@ -18,6 +17,7 @@ namespace xna {
virtual sptr<GameWindow> Window() = 0; virtual sptr<GameWindow> Window() = 0;
virtual sptr<GraphicsDevice> GetGraphicsDevice() = 0; virtual sptr<GraphicsDevice> GetGraphicsDevice() = 0;
virtual sptr<GameComponentCollection> Components() = 0; virtual sptr<GameComponentCollection> Components() = 0;
virtual sptr<GameServiceContainer> Services() = 0;
protected: protected:
virtual void Draw(GameTime const& gameTime) = 0; virtual void Draw(GameTime const& gameTime) = 0;

View File

@ -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();
}
}

View File

@ -2,12 +2,19 @@
#define XNA_GAME_SERVICECONTAINER_HPP #define XNA_GAME_SERVICECONTAINER_HPP
#include "../default.hpp" #include "../default.hpp"
#include "../csharp/service.hpp"
#include <map>
namespace xna { 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<size_t, std::any> services;
}; };
} }

View File

@ -13,6 +13,8 @@
namespace xna { namespace xna {
Game::Game() { Game::Game() {
_services = New<GameServiceContainer>();
_gameWindow = New<GameWindow>(); _gameWindow = New<GameWindow>();
_gameWindow->Color(255, 155, 55); _gameWindow->Color(255, 155, 55);
_gameWindow->Title("XN65"); _gameWindow->Title("XN65");

View File

@ -5,6 +5,7 @@
#include "../game/game.hpp" #include "../game/game.hpp"
#include "dxheaders.hpp" #include "dxheaders.hpp"
#include "dx/StepTimer.hpp" #include "dx/StepTimer.hpp"
#include "../content/manager.hpp"
namespace xna { namespace xna {
class Game : public IGame { class Game : public IGame {
@ -30,6 +31,14 @@ namespace xna {
return _gameComponents; return _gameComponents;
} }
sptr<GameServiceContainer> Services() override {
return _services;
}
sptr<ContentManager> Content() {
return contentManager;
}
constexpr void EnableGameComponents(bool value) { constexpr void EnableGameComponents(bool value) {
_enabledGameComponents = value; _enabledGameComponents = value;
} }
@ -52,14 +61,16 @@ namespace xna {
GameTime _currentGameTime{}; GameTime _currentGameTime{};
DX::StepTimer _stepTimer; DX::StepTimer _stepTimer;
sptr<ContentManager> contentManager;
sptr<GameServiceContainer> _services = nullptr;
private: private:
int startLoop(); int startLoop();
void step(); void step();
sptr<GameComponentCollection> _gameComponents = nullptr; sptr<GameComponentCollection> _gameComponents = nullptr;
std::vector<sptr<IGameComponent>> _drawableGameComponents; std::vector<sptr<IGameComponent>> _drawableGameComponents;
size_t _drawableGameComponentsCount{ 0 }; size_t _drawableGameComponentsCount{ 0 };
bool _enabledGameComponents{ false }; bool _enabledGameComponents{ false };
}; };
} }

View File

@ -9,12 +9,12 @@ using namespace xna;
namespace xna { namespace xna {
class Game1 : public Game { class Game1 : public Game {
public: public:
Game1() { Game1() : Game() {
auto _game = reinterpret_cast<Game*>(this); auto _game = reinterpret_cast<Game*>(this);
graphics = New<GraphicsDeviceManager>(_game); graphics = New<GraphicsDeviceManager>(_game);
graphics->PreferredBackBufferWidth(1280); graphics->PreferredBackBufferWidth(1280);
graphics->PreferredBackBufferHeight(720); graphics->PreferredBackBufferHeight(720);
contentManager = New<ContentManager>("Content"); contentManager = New<ContentManager>("Content", _services);
} }
void Initialize() override { void Initialize() override {
@ -65,8 +65,7 @@ namespace xna {
MouseState currentState{}; MouseState currentState{};
MouseState oldState{}; MouseState oldState{};
float vel = 1; float vel = 1;
int var = 0; int var = 0;
sptr<ContentManager> contentManager;
Texture2D tx; Texture2D tx;
}; };
} }