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"
|
2018-07-21 22:57:21 +08:00
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
|
|
|
|
using namespace std::chrono;
|
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-17 18:07:17 +08:00
|
|
|
|
: _ended(true)
|
2018-07-03 01:49:20 +08:00
|
|
|
|
, _paused(false)
|
2018-07-07 11:00:41 +08:00
|
|
|
|
, _config(nullptr)
|
2018-07-03 01:49:20 +08:00
|
|
|
|
{
|
2018-07-04 15:33:09 +08:00
|
|
|
|
CoInitialize(nullptr);
|
2018-07-21 22:57:21 +08:00
|
|
|
|
|
2018-07-22 00:41:24 +08:00
|
|
|
|
_start = _last = _now = Time::now();
|
2018-07-03 01:49:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-07-03 18:16:26 +08:00
|
|
|
|
e2d::Game::~Game()
|
|
|
|
|
|
{
|
2018-07-22 21:22:27 +08:00
|
|
|
|
GC::getInstance()->safeRelease(_config);
|
2018-07-07 11:00:41 +08:00
|
|
|
|
|
2018-07-04 15:33:09 +08:00
|
|
|
|
CoUninitialize();
|
2018-07-03 18:16:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-07-03 18:16:26 +08:00
|
|
|
|
void e2d::Game::destroyInstance()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_instance)
|
|
|
|
|
|
{
|
|
|
|
|
|
delete _instance;
|
|
|
|
|
|
_instance = nullptr;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-07-21 22:57:21 +08:00
|
|
|
|
void e2d::Game::start()
|
2018-01-30 16:45:38 +08:00
|
|
|
|
{
|
2018-07-22 21:22:27 +08:00
|
|
|
|
auto gc = GC::getInstance();
|
2018-07-04 15:33:09 +08:00
|
|
|
|
auto input = Input::getInstance();
|
2018-07-03 18:16:26 +08:00
|
|
|
|
auto window = Window::getInstance();
|
|
|
|
|
|
auto renderer = Renderer::getInstance();
|
2018-07-05 22:05:23 +08:00
|
|
|
|
auto timer = Timer::getInstance();
|
2018-07-05 16:34:53 +08:00
|
|
|
|
auto sceneManager = SceneManager::getInstance();
|
2018-07-05 22:05:23 +08:00
|
|
|
|
auto actionManager = ActionManager::getInstance();
|
2018-07-03 18:16:26 +08:00
|
|
|
|
|
2018-07-21 22:57:21 +08:00
|
|
|
|
if (!input || !window || !renderer || !timer || !sceneManager || !actionManager)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw SystemException(L"<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
HWND hWnd = window->getHWnd();
|
|
|
|
|
|
if (hWnd == nullptr)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw SystemException(L"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-01-30 16:45:38 +08:00
|
|
|
|
// <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>
|
2018-07-21 22:57:21 +08:00
|
|
|
|
::ShowWindow(hWnd, SW_SHOWNORMAL);
|
|
|
|
|
|
::UpdateWindow(hWnd);
|
2018-07-05 22:05:23 +08:00
|
|
|
|
window->poll();
|
2018-01-30 16:45:38 +08:00
|
|
|
|
|
2018-07-21 22:57:21 +08:00
|
|
|
|
// <20><>ʼ<EFBFBD><CABC>Ϸ
|
2018-07-22 00:41:24 +08:00
|
|
|
|
Duration frameInterval(15), interval;
|
|
|
|
|
|
int wait = 0;
|
2018-07-21 22:57:21 +08:00
|
|
|
|
|
2018-07-03 01:49:20 +08:00
|
|
|
|
_ended = false;
|
2018-07-22 00:41:24 +08:00
|
|
|
|
_last = _now = Time::now();
|
2018-05-24 00:58:16 +08:00
|
|
|
|
|
2018-07-03 01:49:20 +08:00
|
|
|
|
while (!_ended)
|
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
|
|
|
|
|
2018-07-21 22:57:21 +08:00
|
|
|
|
if (frameInterval < interval)
|
2018-01-30 16:45:38 +08:00
|
|
|
|
{
|
2018-07-22 21:22:27 +08:00
|
|
|
|
_last += interval;
|
2018-07-05 01:35:50 +08:00
|
|
|
|
|
2018-07-22 20:09:14 +08:00
|
|
|
|
input->update();
|
|
|
|
|
|
timer->update();
|
|
|
|
|
|
actionManager->update();
|
|
|
|
|
|
sceneManager->update();
|
|
|
|
|
|
_config->_update();
|
|
|
|
|
|
renderer->render();
|
|
|
|
|
|
window->poll();
|
2018-07-22 21:22:27 +08:00
|
|
|
|
gc->flush();
|
2018-01-30 16:45:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2018-07-22 00:41:24 +08:00
|
|
|
|
wait = (frameInterval - interval).milliseconds() - 1;
|
|
|
|
|
|
if (wait > 1)
|
2018-07-21 22:57:21 +08:00
|
|
|
|
{
|
2018-07-22 00:41:24 +08:00
|
|
|
|
std::this_thread::sleep_for(milliseconds(wait));
|
2018-07-21 22:57:21 +08:00
|
|
|
|
}
|
2018-01-30 16:45:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-02-07 16:37:12 +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
|
|
|
|
}
|
|
|
|
|
|
|
2018-02-07 16:37:12 +08:00
|
|
|
|
void e2d::Game::resume()
|
2018-01-30 16:45:38 +08:00
|
|
|
|
{
|
2018-07-05 16:44:47 +08:00
|
|
|
|
if (_paused && !_ended)
|
2018-05-24 00:58:16 +08:00
|
|
|
|
{
|
2018-07-22 00:41:24 +08:00
|
|
|
|
_last = _now = Time::now();
|
2018-07-21 22:57:21 +08:00
|
|
|
|
Timer::getInstance()->updateTime();
|
|
|
|
|
|
ActionManager::getInstance()->updateTime();
|
2018-05-24 00:58:16 +08:00
|
|
|
|
}
|
2018-07-05 16:44:47 +08:00
|
|
|
|
_paused = false;
|
2018-05-10 18:18:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-02-07 16:37:12 +08:00
|
|
|
|
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-07 11:00:41 +08:00
|
|
|
|
void e2d::Game::setConfig(Config* config)
|
2018-07-04 17:00:21 +08:00
|
|
|
|
{
|
2018-07-07 11:00:41 +08:00
|
|
|
|
if (config && _config != config)
|
|
|
|
|
|
{
|
2018-07-22 21:22:27 +08:00
|
|
|
|
if (_config) _config->release();
|
2018-07-07 11:00:41 +08:00
|
|
|
|
_config = config;
|
|
|
|
|
|
_config->_unconfigured = true;
|
2018-07-22 21:22:27 +08:00
|
|
|
|
_config->retain();
|
2018-07-07 11:00:41 +08:00
|
|
|
|
}
|
2018-07-04 17:00:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-07-07 11:00:41 +08:00
|
|
|
|
e2d::Config* e2d::Game::getConfig()
|
2018-07-04 17:00:21 +08:00
|
|
|
|
{
|
2018-07-07 11:00:41 +08:00
|
|
|
|
if (!_config)
|
|
|
|
|
|
_config = new (e2d::autorelease) Config();
|
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-21 22:57:21 +08:00
|
|
|
|
{
|
2018-07-22 00:41:24 +08:00
|
|
|
|
return std::move(_now - _start);
|
2018-07-21 22:57:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-02-07 16:37:12 +08:00
|
|
|
|
void e2d::Game::quit()
|
2018-01-30 16:45:38 +08:00
|
|
|
|
{
|
2018-07-03 01:49:20 +08:00
|
|
|
|
_ended = true; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϸ<EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
|
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();
|
2018-04-21 18:22:01 +08:00
|
|
|
|
Image::clearCache();
|
2018-07-05 22:05:23 +08:00
|
|
|
|
Player::getInstance()->clearCache();
|
2018-01-30 16:45:38 +08:00
|
|
|
|
}
|