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

Corrige Game

This commit is contained in:
Danilo 2024-05-24 22:57:41 -03:00
parent 5940d093a4
commit d17d36cc15
7 changed files with 107 additions and 114 deletions

View File

@ -1,11 +1,14 @@
#include "csharp/type.hpp"
#include "game/gdevicemanager.hpp"
#include "game/time.hpp"
#include "platform-dx/game-dx.hpp"
#include "game/component.hpp"
#include "game/servicecontainer.hpp"
#include "platform-dx/implementations.hpp"
#include "game/gdevicemanager.hpp"
#include "content/manager.hpp"
namespace xna {
Game::Game() {
impl = unew<PlatformImplementation>();
services = New<GameServiceContainer>();
_contentManager = New<ContentManager>("", services);
@ -19,10 +22,50 @@ namespace xna {
_gameComponents = New<GameComponentCollection>();
}
Game::~Game() {
impl = nullptr;
}
void Game::Exit() {
_gameWindow->impl->Close();
}
int Game::StartGameLoop() {
MSG msg{};
impl->_stepTimer = DX::StepTimer();
do {
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else {
Step();
}
} while (msg.message != WM_QUIT);
return static_cast<int>(msg.wParam);
}
void Game::Step()
{
impl->_stepTimer.Tick([&]()
{
const auto elapsed = impl->_stepTimer.GetElapsedSeconds();
const auto total = impl->_stepTimer.GetTotalSeconds();
const auto elapsedTimeSpan = TimeSpan::FromSeconds(elapsed);
const auto totalTimeSpan = TimeSpan::FromSeconds(total);
_currentGameTime.ElapsedGameTime = elapsedTimeSpan;
_currentGameTime.TotalGameTime = totalTimeSpan;
Update(_currentGameTime);
});
Draw(_currentGameTime);
}
int Game::Run() {
Initialize();
@ -31,7 +74,7 @@ namespace xna {
return EXIT_FAILURE;
}
return startLoop();
return StartGameLoop();
}
void Game::Initialize() {
@ -105,38 +148,10 @@ namespace xna {
}
}
int Game::startLoop() {
MSG msg{};
_stepTimer = DX::StepTimer();
do {
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else {
step();
}
} while (msg.message != WM_QUIT);
return static_cast<int>(msg.wParam);
}
void Game::step()
{
_stepTimer.Tick([&]()
{
const auto elapsed = _stepTimer.GetElapsedSeconds();
const auto total = _stepTimer.GetTotalSeconds();
const auto elapsedTimeSpan = TimeSpan::FromSeconds(elapsed);
const auto totalTimeSpan = TimeSpan::FromSeconds(total);
_currentGameTime.ElapsedGameTime = elapsedTimeSpan;
_currentGameTime.TotalGameTime = totalTimeSpan;
Update(_currentGameTime);
});
Draw(_currentGameTime);
}
sptr<GameWindow> Game::Window() { return _gameWindow; }
sptr<GraphicsDevice> Game::GetGraphicsDevice() { return graphicsDevice; }
sptr<GameComponentCollection> Game::Components() { return _gameComponents; }
sptr<GameServiceContainer> Game::Services() { return services; }
sptr<ContentManager> Game::Content() { return _contentManager; }
void Game::EnableGameComponents(bool value) { _enabledGameComponents = value; }
}

View File

@ -1,7 +1,6 @@
#include "game/gdevicemanager.hpp"
#include "graphics/presentparams.hpp"
#include "graphics/swapchain.hpp"
#include "platform-dx/game-dx.hpp"
#include "platform-dx/implementations.hpp"
namespace xna {

View File

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

View File

@ -2,26 +2,49 @@
#define XNA_GAME_GAME_HPP
#include "../default.hpp"
#include "time.hpp"
#include "component.hpp"
#include "servicecontainer.hpp"
#include "game/time.hpp"
namespace xna {
class IGame {
class Game : public std::enable_shared_from_this<Game> {
public:
virtual void Exit() = 0;
virtual int Run() = 0;
virtual sptr<GameWindow> Window() = 0;
virtual sptr<GraphicsDevice> GetGraphicsDevice() = 0;
virtual sptr<GameComponentCollection> Components() = 0;
virtual sptr<GameServiceContainer> Services() = 0;
virtual sptr<ContentManager> Content() = 0;
Game();
~Game();
void Exit();
int Run();
sptr<GameWindow> Window();
sptr<GraphicsDevice> GetGraphicsDevice();
sptr<GameComponentCollection> Components();
sptr<GameServiceContainer> Services();
sptr<ContentManager> Content();
void EnableGameComponents(bool value);
protected:
virtual void Draw(GameTime const& gameTime) = 0;
virtual void Initialize() = 0;
virtual void LoadContent() = 0;
virtual void Update(GameTime const& gameTime) = 0;
virtual void Draw(GameTime const& gameTime);
virtual void Initialize();
virtual void LoadContent(){}
virtual void Update(GameTime const& gameTime);
int StartGameLoop();
void Step();
public:
sptr<GraphicsDevice> graphicsDevice = nullptr;
protected:
sptr<GameServiceContainer> services = nullptr;
private:
sptr<GameComponentCollection> _gameComponents = nullptr;
sptr<GameWindow> _gameWindow{ nullptr };
sptr<AudioEngine> _audioEngine = nullptr;
sptr<ContentManager> _contentManager;
std::vector<sptr<IGameComponent>> _drawableGameComponents;
size_t _drawableGameComponentsCount{ 0 };
bool _enabledGameComponents{ false };
GameTime _currentGameTime{};
public:
struct PlatformImplementation;
uptr<PlatformImplementation> impl = nullptr;
};
}

View File

@ -1,53 +0,0 @@
#ifndef XNA_PLATFORM_GAME_DX_HPP
#define XNA_PLATFORM_GAME_DX_HPP
#include "../content/manager.hpp"
#include "../default.hpp"
#include "../game/game.hpp"
#include "dxheaders.hpp"
#include "platform-dx/dx/StepTimer.hpp"
namespace xna {
class Game : public IGame, public std::enable_shared_from_this<Game> {
public:
Game();
void Exit() override;
int Run() override;
inline sptr<GameWindow> Window() override { return _gameWindow; }
inline sptr<GraphicsDevice> GetGraphicsDevice() override { return graphicsDevice; }
inline sptr<GameComponentCollection> Components() override { return _gameComponents; }
inline sptr<GameServiceContainer> Services() override { return services; }
inline sptr<ContentManager> Content() override { return _contentManager; }
constexpr void EnableGameComponents(bool value) { _enabledGameComponents = value; }
protected:
virtual void Draw(GameTime const& gameTime) override;
virtual void Initialize() override;
virtual void LoadContent() override{}
virtual void Update(GameTime const& gameTime) override;
public:
sptr<GraphicsDevice> graphicsDevice = nullptr;
protected:
sptr<GameServiceContainer> services = nullptr;
private:
sptr<GameComponentCollection> _gameComponents = nullptr;
sptr<GameWindow> _gameWindow{ nullptr };
sptr<AudioEngine> _audioEngine = nullptr;
sptr<ContentManager> _contentManager;
std::vector<sptr<IGameComponent>> _drawableGameComponents;
size_t _drawableGameComponentsCount{ 0 };
bool _enabledGameComponents{ false };
GameTime _currentGameTime{};
DX::StepTimer _stepTimer{};
int startLoop();
void step();
};
}
#endif

View File

@ -2,6 +2,7 @@
#define XNA_PLATFORM_DX_IMPLEMENTATIONS_HPP
#include "dxheaders.hpp"
#include "dx/StepTimer.hpp"
#include "graphics/device.hpp"
#include "graphics/adapter.hpp"
#include "graphics/blendstate.hpp"
@ -23,6 +24,7 @@
#include "audio/audioengine.hpp"
#include "graphics/viewport.hpp"
#include "common/color.hpp"
#include "game/game.hpp"
namespace xna {
struct SpriteFont::PlatformImplementation {
@ -176,7 +178,7 @@ namespace xna {
}
ID3D11Buffer* dxBuffer = nullptr;
};
};
struct Keyboard::PlatformImplementation {
inline static uptr<DirectX::Keyboard> _dxKeyboard = nullptr;
@ -299,7 +301,7 @@ namespace xna {
ID3D11Buffer* dxBuffer = nullptr;
UINT size{ 0 };
};
};
struct VertexInputLayout::PlatformImplementation {
~PlatformImplementation() {
@ -413,7 +415,7 @@ namespace xna {
int _windowPosY{ 0 };
float _windowCenterX{ 0 };
float _windowCenterY{ 0 };
inline void setPosition() {
_windowPosX = GetSystemMetrics(SM_CXSCREEN) / 2 - _windowWidth / 2;
_windowPosY = GetSystemMetrics(SM_CYSCREEN) / 2 - _windowHeight / 2;
@ -423,7 +425,7 @@ namespace xna {
_windowCenterX = _windowWidth / 2.0f;
_windowCenterY = _windowHeight / 2.0f;
}
};
};
struct AudioEngine::PlatformImplementation {
PlatformImplementation() {
@ -431,10 +433,10 @@ namespace xna {
#ifdef _DEBUG
DirectX::AudioEngine_Debug
#endif
);
);
}
~PlatformImplementation(){
~PlatformImplementation() {
if (_dxAudioEngine) {
_dxAudioEngine->Suspend();
}
@ -458,7 +460,14 @@ namespace xna {
private:
friend class GraphicsDevice;
float _backgroundColor[4] = { 0, 0, 0, 0 };
bool _usevsync{ true };
bool _usevsync{ true };
};
struct Game::PlatformImplementation {
private:
friend class Game;
DX::StepTimer _stepTimer{};
};
template <typename T>

View File

@ -1,7 +1,6 @@
#include "content-readers/texture2Dreader-dx.hpp"
#include "dx/StepTimer.hpp"
#include "dxheaders.hpp"
#include "game-dx.hpp"
#include "init-dx.hpp"
#include "soundeffect-dx.hpp"
#include "implementations.hpp"