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

148 lines
3.7 KiB
C++
Raw Normal View History

#include "csharp/type.hpp"
#include "game/time.hpp"
#include "platform-dx/audioengine-dx.hpp"
#include "platform-dx/device-dx.hpp"
#include "platform-dx/game-dx.hpp"
2024-05-21 09:40:59 -03:00
#include "input/gamepad.hpp"
#include "platform-dx/gdevicemanager-dx.hpp"
#include "platform-dx/mouse-dx.hpp"
2024-05-21 09:40:59 -03:00
#include "platform-dx/implementations.hpp"
#include "platform-dx/window-dx.hpp"
2024-03-21 16:01:47 -03:00
namespace xna {
Game::Game() {
2024-05-06 14:54:13 -03:00
services = New<GameServiceContainer>();
_contentManager = New<ContentManager>("", services);
2024-05-06 10:32:17 -03:00
2024-03-21 16:01:47 -03:00
_gameWindow = New<GameWindow>();
2024-05-06 14:54:13 -03:00
_gameWindow->Color(146, 150, 154);
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-05-06 14:54:13 -03:00
void Game::Exit() {
2024-04-22 16:14:55 -03:00
_gameWindow->Close();
}
int Game::Run() {
Initialize();
2024-05-06 14:54:13 -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-05-21 16:21:01 -03:00
//#if (_WIN32_WINNT >= 0x0A00 /*_WIN32_WINNT_WIN10*/)
2024-05-04 21:07:39 -03:00
Microsoft::WRL::Wrappers::RoInitializeWrapper initialize(RO_INIT_MULTITHREADED);
if (FAILED(initialize))
{
2024-05-06 14:54:13 -03:00
MessageBox(nullptr, "Ocorreu um erro ao chamar Microsoft::WRL::Wrappers::RoInitializeWrapper.", "XN65", MB_OK);
2024-05-04 21:07:39 -03:00
}
2024-05-21 16:21:01 -03:00
//#else
2024-05-04 21:07:39 -03:00
HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
2024-04-24 10:11:53 -03:00
if (FAILED(hr))
2024-05-04 21:07:39 -03:00
{
2024-05-06 14:54:13 -03:00
MessageBox(nullptr, "Ocorreu um erro ao chamar CoInitializeEx.", "XN65", MB_OK);
2024-05-04 21:07:39 -03:00
}
2024-05-21 16:21:01 -03:00
//#endif
Keyboard::Initialize();
Mouse::Initialize();
GamePad::Initialize();
2024-04-24 10:11:53 -03:00
_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();
}
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-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
}