Magic_Game/core/Base/Game.cpp

146 lines
2.4 KiB
C++
Raw Normal View History

2018-04-21 21:24:46 +08:00
#include "..\e2dbase.h"
#include "..\e2dmanager.h"
2018-04-24 09:02:06 +08:00
#include "..\e2dtool.h"
#include <thread>
2018-01-30 16:45:38 +08:00
2018-07-03 01:49:20 +08:00
e2d::Game * e2d::Game::_instance = nullptr;
2018-01-30 16:45:38 +08:00
2018-07-03 01:49:20 +08:00
e2d::Game::Game()
2018-07-24 12:49:32 +08:00
: _quit(true)
2018-07-03 01:49:20 +08:00
, _paused(false)
2018-07-24 12:49:32 +08:00
, _config()
, _frameInterval(_config.getFrameInterval())
2018-07-03 01:49:20 +08:00
{
2018-07-04 15:33:09 +08:00
CoInitialize(nullptr);
2018-07-22 00:41:24 +08:00
_start = _last = _now = Time::now();
2018-07-03 01:49:20 +08:00
}
e2d::Game::~Game()
{
2018-07-04 15:33:09 +08:00
CoUninitialize();
}
2018-07-03 01:49:20 +08:00
e2d::Game * e2d::Game::getInstance()
{
if (!_instance)
_instance = new (std::nothrow) Game;
return _instance;
}
2018-01-30 16:45:38 +08:00
void e2d::Game::destroyInstance()
{
if (_instance)
{
delete _instance;
_instance = nullptr;
}
}
void e2d::Game::start()
2018-01-30 16:45:38 +08:00
{
// <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>
2018-07-29 01:43:15 +08:00
HWND hWnd = Window::getInstance()->getHWnd();
::ShowWindow(hWnd, SW_SHOWNORMAL);
::UpdateWindow(hWnd);
2018-07-24 23:27:59 +08:00
SceneManager::getInstance()->update();
2018-07-24 20:21:25 +08:00
Window::getInstance()->poll();
2018-01-30 16:45:38 +08:00
// <20><>ʼ<EFBFBD><CABC>Ϸ
2018-07-22 00:41:24 +08:00
int wait = 0;
Duration interval;
2018-07-24 12:49:32 +08:00
_quit = false;
2018-07-22 00:41:24 +08:00
_last = _now = Time::now();
2018-07-29 01:43:15 +08:00
2018-07-24 12:49:32 +08:00
while (!_quit)
2018-01-30 16:45:38 +08:00
{
2018-07-22 00:41:24 +08:00
_now = Time::now();
interval = _now - _last;
2018-01-30 16:45:38 +08:00
if (_config.isVSyncEnabled() || _frameInterval < interval)
2018-01-30 16:45:38 +08:00
{
2018-07-26 21:43:47 +08:00
_last = _now;
2018-07-29 01:43:15 +08:00
Input::getInstance()->update();
Timer::getInstance()->update();
ActionManager::getInstance()->update();
SceneManager::getInstance()->update();
Renderer::getInstance()->render();
Window::getInstance()->poll();
GC::getInstance()->flush();
2018-01-30 16:45:38 +08:00
}
else
{
2018-07-29 01:43:15 +08:00
std::this_thread::yield();
2018-01-30 16:45:38 +08:00
}
}
}
void e2d::Game::pause()
2018-01-30 16:45:38 +08:00
{
2018-07-03 01:49:20 +08:00
_paused = true;
2018-01-30 16:45:38 +08:00
}
void e2d::Game::resume()
2018-01-30 16:45:38 +08:00
{
2018-07-24 12:49:32 +08:00
if (_paused && !_quit)
2018-05-24 00:58:16 +08:00
{
2018-07-22 00:41:24 +08:00
_last = _now = Time::now();
Timer::getInstance()->updateTime();
ActionManager::getInstance()->updateTime();
2018-05-24 00:58:16 +08:00
}
2018-07-05 16:44:47 +08:00
_paused = false;
}
bool e2d::Game::isPaused()
2018-01-30 16:45:38 +08:00
{
2018-07-03 01:49:20 +08:00
return _paused;
2018-01-30 16:45:38 +08:00
}
2018-07-24 12:49:32 +08:00
void e2d::Game::setConfig(const Config& config)
2018-07-04 17:00:21 +08:00
{
2018-07-24 20:21:25 +08:00
if (_config.isSoundEnabled() != config.isSoundEnabled())
{
if (config.isSoundEnabled())
Player::getInstance()->getXAudio2()->StartEngine();
else
Player::getInstance()->getXAudio2()->StopEngine();
}
if (_config.getFrameInterval() != config.getFrameInterval())
{
2018-07-24 20:21:25 +08:00
_frameInterval = Duration(config.getFrameInterval());
}
2018-07-24 20:21:25 +08:00
if (_config.isVSyncEnabled() != config.isVSyncEnabled())
2018-07-24 12:49:32 +08:00
{
2018-07-24 20:21:25 +08:00
Renderer::getInstance()->discardDeviceResources();
2018-07-24 12:49:32 +08:00
}
2018-07-24 20:21:25 +08:00
_config = config;
2018-07-04 17:00:21 +08:00
}
2018-07-24 12:49:32 +08:00
const e2d::Config& e2d::Game::getConfig()
2018-07-04 17:00:21 +08:00
{
return _config;
}
2018-07-22 00:41:24 +08:00
e2d::Duration e2d::Game::getTotalDuration() const
{
2018-07-22 00:41:24 +08:00
return std::move(_now - _start);
}
void e2d::Game::quit()
2018-01-30 16:45:38 +08:00
{
2018-07-24 12:49:32 +08:00
_quit = true;
2018-01-30 16:45:38 +08:00
}
2018-07-03 01:49:20 +08:00
void e2d::Game::cleanup()
2018-01-30 16:45:38 +08:00
{
2018-07-22 21:22:27 +08:00
GC::getInstance()->clear();
Image::clearCache();
Player::getInstance()->clearCache();
2018-01-30 16:45:38 +08:00
}