Magic_Game/core/base/Game.cpp

300 lines
6.1 KiB
C++
Raw Normal View History

// Copyright (c) 2016-2018 Easy2D - Nomango
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include "Game.h"
#include "Scene.h"
#include "Transition.h"
#include "Image.h"
#include "../utils/Player.h"
#include "../math/Matrix.hpp"
2018-11-15 17:59:18 +08:00
#include "logs.h"
#include "render.h"
#include "input.h"
#include "audio.h"
#include "modules.h"
#include <thread>
namespace easy2d
{
Game::Game()
: quit_(true)
, curr_scene_(nullptr)
, next_scene_(nullptr)
, transition_(nullptr)
, debug_enabled_(false)
, initialized_(false)
{
::CoInitialize(nullptr);
}
Game::Game(Options const & options)
: Game()
{
Init(options);
}
Game::~Game()
{
::CoUninitialize();
}
void Game::Init(const Options& options)
{
if (initialized_)
return;
debug_enabled_ = options.debug;
2018-11-15 14:35:19 +08:00
Window::Instance()->Init(options.title, options.width, options.height, options.icon, debug_enabled_);
devices::Graphics::Instance()->Init(Window::Instance()->GetHandle(), debug_enabled_);
devices::Input::Instance()->Init(debug_enabled_);
devices::Audio::Instance()->Init(debug_enabled_);
HWND console = ::GetConsoleWindow();
if (debug_enabled_)
{
if (console == nullptr)
{
// <20><>ʾһ<CABE><D2BB><EFBFBD>¿<EFBFBD><C2BF><EFBFBD>̨
if (::AllocConsole())
{
console = ::GetConsoleWindow();
// <20>ض<EFBFBD><D8B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
FILE * stdoutStream, *stdinStream, *stderrStream;
freopen_s(&stdoutStream, "conout$", "w+t", stdout);
freopen_s(&stdinStream, "conin$", "r+t", stdin);
freopen_s(&stderrStream, "conout$", "w+t", stderr);
// <20><><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD>̨<EFBFBD>رհ<D8B1>ť
HMENU hmenu = ::GetSystemMenu(console, FALSE);
::RemoveMenu(hmenu, SC_CLOSE, MF_BYCOMMAND);
}
}
}
else
{
if (console)
{
::ShowWindow(console, SW_HIDE);
}
}
::SetWindowLongPtrW(
2018-11-15 14:35:19 +08:00
Window::Instance()->GetHandle(),
GWLP_USERDATA,
PtrToUlong(this)
);
initialized_ = true;
}
void Game::Run()
{
quit_ = false;
if (next_scene_)
{
next_scene_->OnEnter();
curr_scene_ = next_scene_;
next_scene_ = nullptr;
}
2018-11-15 14:35:19 +08:00
const auto window = Window::Instance();
::ShowWindow(window->GetHandle(), SW_SHOWNORMAL);
::UpdateWindow(window->GetHandle());
const int64_t min_interval = 5;
auto last = time::Now();
MSG msg = {};
while (!quit_)
{
auto now = time::Now();
auto dur = now - last;
if (dur.Milliseconds() > min_interval)
{
const auto dt = now - last;
last = now;
2018-11-15 14:35:19 +08:00
devices::Input::Instance()->Update(
window->GetHandle(),
window->GetContentScaleX(),
window->GetContentScaleY()
);
UpdateScene(dt);
DrawScene();
while (::PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
}
else
{
// ID2D1HwndRenderTarget <20><><EFBFBD><EFBFBD><EFBFBD>˴<EFBFBD>ֱͬ<D6B1><CDAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱦʱ<C8BE><CAB1><EFBFBD>ȴ<EFBFBD><C8B4><EFBFBD>ʾ<EFBFBD><CABE>ˢ<EFBFBD>£<EFBFBD>
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˷dz<CBB7><C7B3>ȶ<EFBFBD><C8B6><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>ã<EFBFBD><C3A3><EFBFBD><EFBFBD>Դ󲿷<D4B4>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>ֶ<EFBFBD><D6B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳̽<DFB3><CCBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
// <20><><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һЩ<D2BB><D0A9><EFBFBD><EFBFBD><EFBFBD>£<EFBFBD><C2A3><EFBFBD><EFBFBD><EFBFBD><E7B4B0><EFBFBD><EFBFBD>С<EFBFBD><D0A1>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̣߳<DFB3><CCA3><EFBFBD>ֹռ<D6B9>ù<EFBFBD><C3B9><EFBFBD> CPU <20><>
int64_t wait = min_interval - dur.Milliseconds();
if (wait > 1LL)
{
std::this_thread::sleep_for(std::chrono::milliseconds(wait));
}
}
}
}
void Game::Quit()
{
quit_ = true;
}
bool Game::EnterScene(spScene const & scene)
{
if (!scene)
{
2018-11-15 17:59:18 +08:00
logs::Warningln("Game::EnterScene failed, scene is nullptr");
return false;
}
if (curr_scene_ == scene ||
next_scene_ == scene)
return false;
next_scene_ = scene;
return true;
}
bool Game::EnterScene(spScene const& scene, spTransition const& transition)
{
if (!EnterScene(scene))
return false;
if (transition)
{
if (transition_)
{
transition_->Stop();
}
transition_ = transition;
transition_->Init(curr_scene_, next_scene_);
}
return true;
}
spScene const& Game::GetCurrentScene()
{
return curr_scene_;
}
void Game::UpdateScene(Duration const& dt)
{
if (curr_scene_)
{
curr_scene_->Update(dt);
}
if (next_scene_)
{
next_scene_->Update(dt);
}
if (transition_)
{
transition_->Update(dt);
if (transition_->IsDone())
{
transition_ = nullptr;
}
else
{
return;
}
}
if (next_scene_)
{
if (curr_scene_)
{
curr_scene_->OnExit();
}
next_scene_->OnEnter();
curr_scene_ = next_scene_;
next_scene_ = nullptr;
}
}
void Game::Dispatch(MouseEvent const & e)
{
if (transition_)
return;
if (curr_scene_)
curr_scene_->Dispatch(e, false);
}
void Game::Dispatch(KeyEvent const & e)
{
if (transition_)
return;
if (curr_scene_)
curr_scene_->Dispatch(e, false);
}
void Game::DrawScene()
{
2018-11-15 14:35:19 +08:00
auto graphics = devices::Graphics::Instance();
graphics->BeginDraw(Window::Instance()->GetHandle());
if (transition_)
{
transition_->Draw();
}
else if (curr_scene_)
{
curr_scene_->Visit();
}
if (debug_enabled_)
{
if (curr_scene_)
{
2018-11-15 14:35:19 +08:00
graphics->SetTransform(math::Matrix());
graphics->SetBrushOpacity(1.f);
curr_scene_->DrawBorder();
}
if (next_scene_)
{
2018-11-15 14:35:19 +08:00
graphics->SetTransform(math::Matrix());
graphics->SetBrushOpacity(1.f);
next_scene_->DrawBorder();
}
2018-11-15 14:35:19 +08:00
graphics->DrawDebugInfo();
}
2018-11-15 14:35:19 +08:00
graphics->EndDraw();
}
}