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

147 lines
3.6 KiB
C++
Raw Normal View History

2024-04-23 16:11:17 -03:00
#define NOMINMAX
2024-03-23 17:23:07 -03:00
#include "../game/time.hpp"
2024-04-23 16:11:17 -03:00
#include "audioengine-dx.hpp"
#include "device-dx.hpp"
#include "game-dx.hpp"
#include "gamepad-dx.hpp"
#include "gdevicemanager-dx.hpp"
2024-04-20 13:39:19 -03:00
#include "keyboard-dx.hpp"
#include "mouse-dx.hpp"
2024-04-23 16:11:17 -03:00
#include "window-dx.hpp"
2024-04-24 10:11:53 -03:00
#include <Windows.h>
2024-03-21 16:01:47 -03:00
namespace xna {
Game::Game() {
_gameWindow = New<GameWindow>();
_gameWindow->Color(255, 155, 55);
2024-04-20 13:39:19 -03:00
_gameWindow->Title("XN65");
_gameWindow->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-04-22 16:14:55 -03:00
void Game::Exit()
{
_gameWindow->Close();
}
int Game::Run() {
Initialize();
2024-04-07 14:06:12 -03:00
if (_graphicsDevice == nullptr) {
2024-04-20 13:39:19 -03:00
MessageBox(nullptr, "O dispositivo gr<67>fico n<>o foi inicializado corretamente", "XN65", MB_OK);
2024-03-21 16:01:47 -03:00
return EXIT_FAILURE;
}
return startLoop();
2024-04-27 15:21:47 -03:00
}
2024-04-23 16:11:17 -03:00
2024-04-20 13:39:19 -03:00
void Game::Initialize() {
2024-04-24 10:11:53 -03:00
Keyboard::Initialize();
Mouse::Initialize();
//initialize <20> requisito para GamePad
Microsoft::WRL::Wrappers::RoInitializeWrapper initialize(RO_INIT_MULTITHREADED);
if (FAILED(initialize))
MessageBox(nullptr, "Ocorreu um erro ao executar Microsoft::WRL::Wrappers::RoInitializeWrapper. O GamePad n<>o foi inicializado corretamente.", "XN65", MB_OK);
GamePad.Initialize();
//CoInitializeEx <20> requisito para AudioEngine
const auto hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
if (FAILED(hr))
MessageBox(nullptr, "Ocorreu um erro ao executar CoInitializeEx. O AudioEngine n<>o foi inicializado corretamente.", "XN65", MB_OK);
_audioEngine = New<AudioEngine>();
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();
}
_graphicsDevice->Present();
}
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-03-21 16:01:47 -03:00
int Game::startLoop() {
2024-04-23 16:11:17 -03:00
MSG msg{};
_stepTimer = DX::StepTimer();
2024-03-21 16:01:47 -03:00
do {
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
2024-04-23 16:11:17 -03:00
{
2024-03-21 16:01:47 -03:00
TranslateMessage(&msg);
2024-04-23 16:11:17 -03:00
DispatchMessage(&msg);
2024-03-21 16:01:47 -03:00
}
else {
2024-04-23 16:11:17 -03:00
step();
}
2024-03-23 17:23:07 -03:00
} while (msg.message != WM_QUIT);
2024-03-23 17:23:07 -03:00
return static_cast<int>(msg.wParam);
2024-04-24 10:11:53 -03:00
}
2024-03-23 17:23:07 -03:00
2024-04-23 16:11:17 -03:00
void Game::step()
{
2024-04-24 10:11:53 -03:00
_stepTimer.Tick([&]()
2024-04-23 16:11:17 -03:00
{
const auto elapsed = _stepTimer.GetElapsedSeconds();
2024-04-24 10:11:53 -03:00
const auto total = _stepTimer.GetTotalSeconds();
2024-04-23 16:11:17 -03:00
const auto elapsedTimeSpan = TimeSpan::FromSeconds(elapsed);
const auto totalTimeSpan = TimeSpan::FromSeconds(total);
_currentGameTime.ElapsedGameTime = elapsedTimeSpan;
_currentGameTime.TotalGameTime = totalTimeSpan;
2024-04-24 10:11:53 -03:00
Update(_currentGameTime);
2024-04-23 16:11:17 -03:00
});
Draw(_currentGameTime);
2024-03-21 16:01:47 -03:00
}
}