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

Implementações em Game

This commit is contained in:
Danilo 2024-08-01 16:10:05 -03:00
parent d2dcf4fe88
commit 34d9533261
4 changed files with 101 additions and 20 deletions

View File

@ -5,8 +5,8 @@ namespace xna {
impl = unew<PlatformImplementation>(); impl = unew<PlatformImplementation>();
services = snew<GameServiceContainer>(); services = snew<GameServiceContainer>();
auto iservice = reinterpret_pointer_cast<IServiceProvider>(services); auto iservice = reinterpret_pointer_cast<IServiceProvider>(services);
_contentManager = snew<ContentManager>(services, ""); contentManager = snew<ContentManager>(services, "");
_contentManager->mainGameService = iservice; contentManager->mainGameService = iservice;
_gameWindow = snew<GameWindow>(); _gameWindow = snew<GameWindow>();
_gameWindow->impl->Color(146, 150, 154); _gameWindow->impl->Color(146, 150, 154);
@ -16,6 +16,9 @@ namespace xna {
GraphicsDeviceManager::DefaultBackBufferHeight, false); GraphicsDeviceManager::DefaultBackBufferHeight, false);
_gameComponents = snew<GameComponentCollection>(); _gameComponents = snew<GameComponentCollection>();
IsFixedTimeStep(isFixedTimeStep);
TargetElapsedTime(targetElapsedTime);
} }
void Game::Exit() { void Game::Exit() {
@ -34,7 +37,7 @@ namespace xna {
DispatchMessage(&msg); DispatchMessage(&msg);
} }
else { else {
Step(); Tick();
} }
} while (msg.message != WM_QUIT); } while (msg.message != WM_QUIT);
@ -42,7 +45,7 @@ namespace xna {
return static_cast<int>(msg.wParam); return static_cast<int>(msg.wParam);
} }
void Game::Step() void Game::Tick()
{ {
impl->_stepTimer.Tick([&]() impl->_stepTimer.Tick([&]()
{ {
@ -59,6 +62,9 @@ namespace xna {
} }
int Game::Run() { int Game::Run() {
if (isRunning)
return EXIT_FAILURE;
try { try {
if (!_gameWindow->impl->Create()) { if (!_gameWindow->impl->Create()) {
Exception::Throw(Exception::FAILED_TO_CREATE); Exception::Throw(Exception::FAILED_TO_CREATE);
@ -72,6 +78,7 @@ namespace xna {
return EXIT_FAILURE; return EXIT_FAILURE;
} }
isRunning = true;
return StartGameLoop(); return StartGameLoop();
} }
catch (std::exception& e) { catch (std::exception& e) {
@ -138,10 +145,10 @@ namespace xna {
} }
sptr<GameWindow> Game::Window() { return _gameWindow; } sptr<GameWindow> Game::Window() { return _gameWindow; }
sptr<GraphicsDevice> Game::GetGraphicsDevice() { return graphicsDevice; } sptr<GraphicsDevice> Game::Device() const { return graphicsDevice; }
sptr<GameComponentCollection> Game::Components() { return _gameComponents; } sptr<GameComponentCollection> Game::Components() const { return _gameComponents; }
sptr<GameServiceContainer> Game::Services() { return services; } sptr<GameServiceContainer> Game::Services() { return services; }
sptr<ContentManager> Game::Content() { return _contentManager; } sptr<ContentManager> Game::Content() const { return contentManager; }
void Game::EnableGameComponents(bool value) { _enabledGameComponents = value; } void Game::EnableGameComponents(bool value) { _enabledGameComponents = value; }
void Game::AttachGraphicsDevice(sptr<GraphicsDevice> const& device) { void Game::AttachGraphicsDevice(sptr<GraphicsDevice> const& device) {
@ -158,4 +165,44 @@ namespace xna {
_gameWindow->impl->Update(); _gameWindow->impl->Update();
} }
} }
void Game::Content(sptr<ContentManager> const& value) {
contentManager = value;
auto iservice = reinterpret_pointer_cast<IServiceProvider>(services);
contentManager->mainGameService = iservice;
}
void Game::IsFixedTimeStep(bool value) {
isFixedTimeStep = value;
impl->_stepTimer.SetFixedTimeStep(value);
}
bool Game::IsMouseVisible() const {
if (!Mouse::impl)
return false;
return Mouse::impl->_dxMouse->IsVisible();
}
void Game::IsMouseVisible(bool value) {
if (!Mouse::impl)
return;
Mouse::impl->_dxMouse->SetVisible(value);
}
void Game::TargetElapsedTime(TimeSpan const& value) {
if (!isFixedTimeStep)
return;
const auto ticks = targetElapsedTime.Ticks();
impl->_stepTimer.SetTargetElapsedTicks(ticks);
}
void Game::ResetElapsedTime() const {
impl->_stepTimer.ResetElapsedTime();
}
void Game::RunOneFrame() {
Tick();
}
} }

View File

@ -5,18 +5,50 @@
#include "time.hpp" #include "time.hpp"
namespace xna { namespace xna {
//Provides basic graphics device initialization, game logic, and rendering code.
class Game : public std::enable_shared_from_this<Game> { class Game : public std::enable_shared_from_this<Game> {
public: public:
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);
//Gets the collection of GameComponents owned by the game.
sptr<GameComponentCollection> Components() const;
//Gets or sets the current ContentManager.
sptr<ContentManager> Content() const;
//Gets or sets the current ContentManager.
void Content(sptr<ContentManager> const& value);
//Gets the current GraphicsDevice.
sptr<GraphicsDevice> Device() const;
//Gets or sets a value indicating whether to use fixed time steps.
//The default value for IsFixedTimeStep is true.
constexpr bool IsFixedTimeStep() const { return isFixedTimeStep; }
//Gets or sets a value indicating whether to use fixed time steps.
//The default value for IsFixedTimeStep is true.
void IsFixedTimeStep(bool value);
//Gets or sets a value indicating whether the mouse cursor should be visible.
bool IsMouseVisible() const;
//Gets or sets a value indicating whether the mouse cursor should be visible.
void IsMouseVisible(bool value);
//Gets the GameServiceContainer holding all the service providers attached to the Game.
sptr<GameServiceContainer> Services();
//Gets or sets the target time between calls to Update when IsFixedTimeStep is true.
constexpr TimeSpan TargetElapsedTime() const { return targetElapsedTime; }
//Gets or sets the target time between calls to Update when IsFixedTimeStep is true.
void TargetElapsedTime(TimeSpan const& value);
//Gets the underlying operating system window.
sptr<GameWindow> Window();
//Exits the game.
void Exit();
//Resets the elapsed time counter.
void ResetElapsedTime() const;
//Call this method to initialize the game, begin running the game loop, and start processing events for the game.
int Run();
//Run the game through what would happen in a single tick of the game clock; this method is designed for debugging only.
void RunOneFrame();
//Updates the game's clock and calls Update and Draw.
void Tick();
void EnableGameComponents(bool value);
void AttachGraphicsDevice(sptr<GraphicsDevice> const& graphicsDevice); void AttachGraphicsDevice(sptr<GraphicsDevice> const& graphicsDevice);
void ResizeWindow(int width, int heigth); void ResizeWindow(int width, int heigth);
@ -25,8 +57,7 @@ namespace xna {
virtual void Initialize(); virtual void Initialize();
virtual void LoadContent(){} virtual void LoadContent(){}
virtual void Update(GameTime const& gameTime); virtual void Update(GameTime const& gameTime);
int StartGameLoop(); int StartGameLoop();
void Step();
public: public:
sptr<GraphicsDevice> graphicsDevice = nullptr; sptr<GraphicsDevice> graphicsDevice = nullptr;
@ -38,11 +69,14 @@ namespace xna {
sptr<GameComponentCollection> _gameComponents = nullptr; sptr<GameComponentCollection> _gameComponents = nullptr;
sptr<GameWindow> _gameWindow{ nullptr }; sptr<GameWindow> _gameWindow{ nullptr };
sptr<AudioEngine> _audioEngine = nullptr; sptr<AudioEngine> _audioEngine = nullptr;
sptr<ContentManager> _contentManager; sptr<ContentManager> contentManager;
std::vector<sptr<IGameComponent>> _drawableGameComponents; std::vector<sptr<IGameComponent>> _drawableGameComponents;
size_t _drawableGameComponentsCount{ 0 }; size_t _drawableGameComponentsCount{ 0 };
bool _enabledGameComponents{ false }; bool _enabledGameComponents{ false };
GameTime _currentGameTime{}; GameTime _currentGameTime{};
bool isFixedTimeStep{ true };
TimeSpan targetElapsedTime{ TimeSpan::FromTicks(166667L) };
bool isRunning{ false };
public: public:
struct PlatformImplementation; struct PlatformImplementation;

View File

@ -73,7 +73,7 @@ namespace PlatformerStarterKit {
auto keyboardState = Keyboard::GetState(); auto keyboardState = Keyboard::GetState();
auto gamepadState = GamePad::GetState(PlayerIndex::One); auto gamepadState = GamePad::GetState(PlayerIndex::One);
if (gamepadState.Buttons.Back == ButtonState::Pressed) if (gamepadState.Buttons().Back() == ButtonState::Pressed)
Exit(); Exit();
bool continuePressed = bool continuePressed =

View File

@ -92,7 +92,7 @@ namespace PlatformerStarterKit {
auto gamePadState = xna::GamePad::GetState(xna::PlayerIndex::One); auto gamePadState = xna::GamePad::GetState(xna::PlayerIndex::One);
auto keyboardState = xna::Keyboard::GetState(); auto keyboardState = xna::Keyboard::GetState();
movement = gamePadState.ThumbSticks.Left().X * MoveStickScale; movement = gamePadState.ThumbSticks().Left().X * MoveStickScale;
if (std::abs(movement) < 0.5f) if (std::abs(movement) < 0.5f)
movement = 0.0f; movement = 0.0f;