2019-04-11 14:40:54 +08:00
|
|
|
// Copyright (c) 2016-2018 Kiwano - Nomango
|
2020-01-21 10:09:55 +08:00
|
|
|
//
|
2019-03-31 01:37:06 +08:00
|
|
|
// 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:
|
2020-01-21 10:09:55 +08:00
|
|
|
//
|
2019-03-31 01:37:06 +08:00
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
// all copies or substantial portions of the Software.
|
2020-01-21 10:09:55 +08:00
|
|
|
//
|
2019-03-31 01:37:06 +08:00
|
|
|
// 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.
|
|
|
|
|
|
2020-01-16 18:33:42 +08:00
|
|
|
#include <kiwano/core/Director.h>
|
|
|
|
|
#include <kiwano/core/Logger.h>
|
2020-01-21 10:09:55 +08:00
|
|
|
#include <kiwano/platform/Application.h>
|
|
|
|
|
#include <kiwano/platform/Input.h>
|
2020-01-17 16:55:47 +08:00
|
|
|
#include <kiwano/render/TextureCache.h>
|
2019-10-11 21:55:29 +08:00
|
|
|
#include <kiwano/utils/ResourceCache.h>
|
2020-01-21 10:09:55 +08:00
|
|
|
#include <mutex>
|
2019-10-10 15:09:38 +08:00
|
|
|
|
2019-04-24 13:33:19 +08:00
|
|
|
namespace kiwano
|
2019-03-31 13:08:59 +08:00
|
|
|
{
|
2020-01-21 10:09:55 +08:00
|
|
|
namespace
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
using FunctionToPerform = Function<void()>;
|
|
|
|
|
|
|
|
|
|
std::mutex perform_mutex_;
|
|
|
|
|
Queue<FunctionToPerform> functions_to_perform_;
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
Application::Application()
|
|
|
|
|
: time_scale_(1.f)
|
|
|
|
|
{
|
|
|
|
|
Use(&Renderer::Instance());
|
|
|
|
|
Use(&Input::Instance());
|
|
|
|
|
Use(&Director::Instance());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Application::~Application()
|
|
|
|
|
{
|
|
|
|
|
Destroy();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Application::Run(bool debug)
|
|
|
|
|
{
|
|
|
|
|
// Setup all components
|
|
|
|
|
for (auto c : comps_)
|
|
|
|
|
{
|
|
|
|
|
c->SetupComponent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (debug)
|
|
|
|
|
{
|
|
|
|
|
Director::Instance().ShowDebugInfo(true);
|
2020-02-03 14:15:30 +08:00
|
|
|
Renderer::Instance().GetContext().SetCollectingStatus(true);
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Everything is ready
|
|
|
|
|
OnReady();
|
|
|
|
|
|
|
|
|
|
last_update_time_ = Time::Now();
|
|
|
|
|
|
|
|
|
|
Window& window = Window::Instance();
|
|
|
|
|
while (!window.ShouldClose())
|
|
|
|
|
{
|
|
|
|
|
while (EventPtr evt = window.PollEvent())
|
|
|
|
|
{
|
|
|
|
|
DispatchEvent(evt.get());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Update();
|
|
|
|
|
Render();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Application::Quit()
|
|
|
|
|
{
|
|
|
|
|
Window::Instance().Destroy();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Application::Destroy()
|
|
|
|
|
{
|
|
|
|
|
// Clear all resources
|
|
|
|
|
Director::Instance().ClearStages();
|
|
|
|
|
ResourceCache::Instance().Clear();
|
|
|
|
|
TextureCache::Instance().Clear();
|
|
|
|
|
|
|
|
|
|
for (auto iter = comps_.rbegin(); iter != comps_.rend(); ++iter)
|
|
|
|
|
{
|
|
|
|
|
(*iter)->DestroyComponent();
|
|
|
|
|
}
|
|
|
|
|
render_comps_.clear();
|
|
|
|
|
update_comps_.clear();
|
|
|
|
|
event_comps_.clear();
|
|
|
|
|
comps_.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Application::Use(ComponentBase* component)
|
|
|
|
|
{
|
|
|
|
|
if (component)
|
|
|
|
|
{
|
2019-03-31 01:37:06 +08:00
|
|
|
|
2019-04-11 14:40:54 +08:00
|
|
|
#if defined(KGE_DEBUG)
|
2020-01-21 10:09:55 +08:00
|
|
|
if (comps_.contains(component))
|
|
|
|
|
{
|
|
|
|
|
KGE_ASSERT(false && "Component already exists!");
|
|
|
|
|
}
|
2019-03-31 01:37:06 +08:00
|
|
|
#endif
|
|
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
comps_.push_back(component);
|
|
|
|
|
|
|
|
|
|
if (component->Check(RenderComponent::flag))
|
|
|
|
|
render_comps_.push_back(dynamic_cast<RenderComponent*>(component));
|
|
|
|
|
|
|
|
|
|
if (component->Check(UpdateComponent::flag))
|
|
|
|
|
update_comps_.push_back(dynamic_cast<UpdateComponent*>(component));
|
|
|
|
|
|
|
|
|
|
if (component->Check(EventComponent::flag))
|
|
|
|
|
event_comps_.push_back(dynamic_cast<EventComponent*>(component));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Application::SetTimeScale(float scale_factor)
|
|
|
|
|
{
|
|
|
|
|
time_scale_ = scale_factor;
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
void Application::Update()
|
|
|
|
|
{
|
|
|
|
|
// Before update
|
|
|
|
|
for (auto c : update_comps_)
|
|
|
|
|
{
|
|
|
|
|
c->BeforeUpdate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// perform functions
|
|
|
|
|
{
|
|
|
|
|
if (!functions_to_perform_.empty())
|
|
|
|
|
{
|
|
|
|
|
perform_mutex_.lock();
|
|
|
|
|
auto functions = std::move(functions_to_perform_);
|
|
|
|
|
perform_mutex_.unlock();
|
|
|
|
|
|
|
|
|
|
while (!functions.empty())
|
|
|
|
|
{
|
|
|
|
|
auto& func = functions.front();
|
|
|
|
|
if (func)
|
|
|
|
|
{
|
|
|
|
|
func();
|
|
|
|
|
}
|
|
|
|
|
functions.pop();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Updating
|
|
|
|
|
{
|
|
|
|
|
const Time now = Time::Now();
|
|
|
|
|
const Duration dt = (now - last_update_time_) * time_scale_;
|
|
|
|
|
|
|
|
|
|
last_update_time_ = now;
|
|
|
|
|
|
|
|
|
|
for (auto c : update_comps_)
|
|
|
|
|
{
|
|
|
|
|
c->OnUpdate(dt);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// After update
|
|
|
|
|
for (auto rit = update_comps_.rbegin(); rit != update_comps_.rend(); ++rit)
|
|
|
|
|
{
|
|
|
|
|
(*rit)->AfterUpdate();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Application::Render()
|
|
|
|
|
{
|
2020-02-03 14:15:30 +08:00
|
|
|
Renderer& renderer = Renderer::Instance();
|
|
|
|
|
renderer.Clear();
|
|
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
// Before render
|
|
|
|
|
for (auto c : render_comps_)
|
|
|
|
|
{
|
|
|
|
|
c->BeforeRender();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Rendering
|
2020-02-03 14:15:30 +08:00
|
|
|
renderer.BeginDraw();
|
2020-01-21 10:09:55 +08:00
|
|
|
for (auto c : render_comps_)
|
|
|
|
|
{
|
2020-02-03 14:15:30 +08:00
|
|
|
c->OnRender(renderer.GetContext());
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
2020-02-03 14:15:30 +08:00
|
|
|
renderer.EndDraw();
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
// After render
|
|
|
|
|
for (auto rit = render_comps_.rbegin(); rit != render_comps_.rend(); ++rit)
|
|
|
|
|
{
|
|
|
|
|
(*rit)->AfterRender();
|
|
|
|
|
}
|
2020-02-03 14:15:30 +08:00
|
|
|
|
|
|
|
|
renderer.Present();
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Application::DispatchEvent(Event* evt)
|
|
|
|
|
{
|
|
|
|
|
for (auto c : event_comps_)
|
|
|
|
|
{
|
|
|
|
|
c->HandleEvent(evt);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Application::PreformInMainThread(Function<void()> func)
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> lock(perform_mutex_);
|
|
|
|
|
functions_to_perform_.push(func);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace kiwano
|