#include "..\e2dbase.h" #include "..\e2dmanager.h" #include "..\e2dtool.h" e2d::Game * e2d::Game::_instance = nullptr; e2d::Game::Game() : _ended(false) , _paused(false) , _config(nullptr) { CoInitialize(nullptr); } e2d::Game::~Game() { GC::safeRelease(_config); CoUninitialize(); } e2d::Game * e2d::Game::getInstance() { if (!_instance) _instance = new (std::nothrow) Game; return _instance; } void e2d::Game::destroyInstance() { if (_instance) { delete _instance; _instance = nullptr; } } void e2d::Game::start(bool cleanup) { auto input = Input::getInstance(); auto window = Window::getInstance(); auto renderer = Renderer::getInstance(); auto timer = Timer::getInstance(); auto sceneManager = SceneManager::getInstance(); auto actionManager = ActionManager::getInstance(); // 显示窗口 ::ShowWindow(window->getHWnd(), SW_SHOWNORMAL); // 刷新窗口内容 ::UpdateWindow(window->getHWnd()); // 处理窗口消息 window->poll(); // 初始化计时 Time::__init(); _ended = false; while (!_ended) { // 处理窗口消息 window->poll(); // 刷新时间 Time::__updateNow(); // 判断是否达到了刷新状态 if (Time::__isReady()) { if (_config->_unconfigured) { _config->_update(); } input->update(); // 获取用户输入 timer->update(); // 更新定时器 actionManager->update(); // 更新动作管理器 sceneManager->update(); // 更新场景内容 renderer->render(); // 渲染游戏画面 GC::flush(); // 刷新内存池 Time::__updateLast(); // 刷新时间信息 } else { Time::__sleep(); // 挂起线程 } } if (cleanup) { Game::cleanup(); } } void e2d::Game::pause() { _paused = true; } void e2d::Game::resume() { if (_paused && !_ended) { Time::__reset(); } _paused = false; } bool e2d::Game::isPaused() { return _paused; } void e2d::Game::setConfig(Config* config) { if (config && _config != config) { GC::release(_config); _config = config; _config->_unconfigured = true; GC::retain(_config); } } e2d::Config* e2d::Game::getConfig() { if (!_config) _config = new (e2d::autorelease) Config(); return _config; } void e2d::Game::quit() { _ended = true; // 这个变量将控制游戏是否结束 } void e2d::Game::cleanup() { // 删除所有场景 SceneManager::getInstance()->clear(); // 清空定时器 Timer::getInstance()->clearAllTasks(); // 清除所有动作 ActionManager::getInstance()->clearAll(); // 清空图片缓存 Image::clearCache(); // 清空音乐缓存 Player::getInstance()->clearCache(); // 删除所有对象 GC::clear(); }