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

152 lines
3.9 KiB
C++
Raw Normal View History

2024-06-05 21:28:53 -03:00
#include "xna/content/manager.hpp"
2024-06-03 21:55:09 -03:00
#include "xna/csharp/type.hpp"
#include "xna/game/component.hpp"
#include "xna/game/gdevicemanager.hpp"
2024-06-05 21:28:53 -03:00
#include "xna/game/servicecontainer.hpp"
#include "xna/game/time.hpp"
#include "xna/platform-dx/dx.hpp"
2024-03-21 16:01:47 -03:00
namespace xna {
Game::Game() {
2024-05-24 22:57:41 -03:00
impl = unew<PlatformImplementation>();
2024-05-06 14:54:13 -03:00
services = New<GameServiceContainer>();
2024-05-27 16:44:01 -03:00
auto iservice = reinterpret_pointer_cast<IServiceProvider>(services);
2024-06-02 20:36:41 -03:00
_contentManager = New<ContentManager>(services, "");
_contentManager->_gameServices = iservice;
2024-05-06 10:32:17 -03:00
2024-03-21 16:01:47 -03:00
_gameWindow = New<GameWindow>();
2024-05-24 12:31:24 -03:00
_gameWindow->impl->Color(146, 150, 154);
2024-04-20 13:39:19 -03:00
_gameWindow->Title("XN65");
2024-05-24 12:31:24 -03:00
_gameWindow->impl->Size(
GraphicsDeviceManager::DefaultBackBufferWidth,
GraphicsDeviceManager::DefaultBackBufferHeight, false);
2024-04-27 00:10:07 -03:00
_gameComponents = New<GameComponentCollection>();
2024-04-24 10:11:53 -03:00
}
2024-04-20 13:39:19 -03:00
2024-05-24 22:57:41 -03:00
Game::~Game() {
impl = nullptr;
}
2024-05-06 14:54:13 -03:00
void Game::Exit() {
2024-05-24 12:31:24 -03:00
_gameWindow->impl->Close();
2024-04-22 16:14:55 -03:00
}
2024-05-24 22:57:41 -03:00
int Game::StartGameLoop() {
MSG msg{};
2024-05-27 17:34:28 -03:00
impl->_stepTimer = xna::StepTimer();
2024-05-24 22:57:41 -03:00
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);
}
2024-04-22 16:14:55 -03:00
int Game::Run() {
2024-05-28 14:43:56 -03:00
try {
Initialize();
2024-05-28 14:43:56 -03:00
if (graphicsDevice == nullptr) {
MessageBox(nullptr, "O dispositivo gr<67>fico n<>o foi inicializado corretamente", "XN65", MB_OK);
return EXIT_FAILURE;
}
2024-03-21 16:01:47 -03:00
2024-05-28 14:43:56 -03:00
return StartGameLoop();
}
catch (std::exception& e) {
MessageBox(nullptr, e.what(), "XN65", MB_OK);
2024-05-28 16:59:03 -03:00
return EXIT_FAILURE;
2024-05-28 14:43:56 -03:00
}
2024-04-27 15:21:47 -03:00
}
2024-04-23 16:11:17 -03:00
2024-05-27 21:12:46 -03:00
void Game::Initialize() {
2024-05-21 16:21:01 -03:00
Keyboard::Initialize();
Mouse::Initialize();
GamePad::Initialize();
2024-05-27 21:12:46 -03:00
AudioEngine::Initialize();
2024-04-24 10:11:53 -03:00
2024-04-20 13:39:19 -03:00
LoadContent();
}
2024-04-27 15:21:47 -03:00
void Game::Draw(GameTime const& gameTime) {
if (_enabledGameComponents && !_drawableGameComponents.empty()) {
const auto count = _drawableGameComponents.size();
if (count != _drawableGameComponentsCount && _gameComponents->AutoSort) {
GameComponentCollection::DrawSort(_drawableGameComponents);
_drawableGameComponentsCount = count;
}
for (size_t i = 0; i < count; ++i) {
auto& component = _drawableGameComponents[i];
if (!component) continue;
auto drawable = reinterpret_pointer_cast<IDrawable>(component);
if (drawable && drawable->Visible())
drawable->Draw(gameTime);
}
_drawableGameComponents.clear();
}
2024-05-06 14:54:13 -03:00
graphicsDevice->Present();
2024-04-27 15:21:47 -03:00
}
2024-04-20 13:39:19 -03:00
void Game::Update(GameTime const& gameTime) {
_audioEngine->Update();
2024-04-27 00:10:07 -03:00
2024-04-27 15:21:47 -03:00
if (_enabledGameComponents && _gameComponents->Count() > 0) {
const auto count = _gameComponents->Count();
for (size_t i = 0; i < count; ++i) {
2024-04-27 00:10:07 -03:00
auto component = _gameComponents->At(i);
if (!component) continue;
2024-04-27 15:21:47 -03:00
if (component->Type() == GameComponentType::Drawable) {
_drawableGameComponents.push_back(component);
}
2024-04-27 00:10:07 -03:00
auto updatable = reinterpret_pointer_cast<IUpdateable>(component);
if(updatable && updatable->Enabled())
updatable->Update(gameTime);
2024-04-27 15:21:47 -03:00
}
2024-04-27 00:10:07 -03:00
}
2024-04-20 13:39:19 -03:00
}
2024-04-23 16:11:17 -03:00
2024-05-24 22:57:41 -03:00
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; }
2024-03-21 16:01:47 -03:00
}