[build] add Stage component

This commit is contained in:
Nomango 2019-08-12 14:51:54 +08:00
parent 4a5853830e
commit 8b277ed59d
25 changed files with 401 additions and 296 deletions

View File

@ -2,6 +2,7 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ClInclude Include="..\src\kiwano\2d\GifSprite.h" />
<ClInclude Include="..\src\kiwano\base\Stage.h" />
<ClInclude Include="..\src\kiwano\base\types.h" />
<ClInclude Include="..\src\kiwano\kiwano.h" />
<ClInclude Include="..\src\kiwano\config.h" />
@ -108,6 +109,7 @@
<ClCompile Include="..\src\kiwano\base\logs.cpp" />
<ClCompile Include="..\src\kiwano\base\Object.cpp" />
<ClCompile Include="..\src\kiwano\base\Resource.cpp" />
<ClCompile Include="..\src\kiwano\base\Stage.cpp" />
<ClCompile Include="..\src\kiwano\base\Timer.cpp" />
<ClCompile Include="..\src\kiwano\base\TimerManager.cpp" />
<ClCompile Include="..\src\kiwano\base\time.cpp" />

View File

@ -267,6 +267,9 @@
<ClInclude Include="..\src\kiwano\2d\ShapeNode.h">
<Filter>2d</Filter>
</ClInclude>
<ClInclude Include="..\src\kiwano\base\Stage.h">
<Filter>base</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\src\kiwano\ui\Button.cpp">
@ -404,5 +407,8 @@
<ClCompile Include="..\src\kiwano\2d\ShapeNode.cpp">
<Filter>2d</Filter>
</ClCompile>
<ClCompile Include="..\src\kiwano\base\Stage.cpp">
<Filter>base</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -36,7 +36,7 @@ namespace kiwano
{
}
void Audio::SetupComponent(Application*)
void Audio::SetupComponent()
{
KGE_LOG(L"Creating audio resources");

View File

@ -33,7 +33,7 @@ namespace kiwano
KGE_DECLARE_SINGLETON(Audio);
public:
void SetupComponent(Application*) override;
void SetupComponent() override;
void DestroyComponent() override;

View File

@ -32,7 +32,7 @@ namespace kiwano
{
}
void ImGuiModule::SetupComponent(Application* app)
void ImGuiModule::SetupComponent()
{
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
@ -56,12 +56,12 @@ namespace kiwano
ImGui::DestroyContext();
}
void ImGuiModule::BeforeUpdate(float dt)
void ImGuiModule::OnUpdate(Duration dt)
{
ImGuiIO& io = ImGui::GetIO();
// Setup time step
io.DeltaTime = dt;
io.DeltaTime = dt.Seconds();
// Read keyboard modifiers inputs
io.KeyCtrl = Input::Instance()->IsDown(KeyCode::Ctrl);

View File

@ -42,11 +42,11 @@ namespace kiwano
public:
ImGuiModule();
void SetupComponent(Application* app) override;
void SetupComponent() override;
void DestroyComponent() override;
void BeforeUpdate(float dt) override;
void OnUpdate(Duration dt) override;
void BeforeRender() override;

View File

@ -20,7 +20,10 @@
#include <kiwano/base/logs.h>
#include <kiwano/platform/Application.h>
#include "../kiwano-network.h"
#include "helper.h"
#include "HttpRequest.hpp"
#include "HttpResponse.hpp"
#include "HttpClient.h"
#include <thread>
#include <codecvt>
@ -249,7 +252,7 @@ namespace kiwano
{
}
void HttpClient::SetupComponent(Application * app)
void HttpClient::SetupComponent()
{
::curl_global_init(CURL_GLOBAL_ALL);

View File

@ -71,7 +71,7 @@ namespace kiwano
}
public:
virtual void SetupComponent(Application* app) override;
virtual void SetupComponent() override;
virtual void DestroyComponent() override;

View File

@ -19,7 +19,7 @@
// THE SOFTWARE.
#pragma once
#include <kiwano/common/IntrusivePtr.hpp>
#include <kiwano/base/SmartPtr.hpp>
namespace kiwano
{

View File

@ -71,7 +71,7 @@ namespace kiwano
void DebugNode::OnUpdate(Duration dt)
{
KGE_NOT_USED(dt);
KGE_UNUSED(dt);
frame_time_.push_back(Time::Now());
while (frame_time_.back() - frame_time_.front() >= time::Sec)

View File

@ -27,7 +27,7 @@
namespace kiwano
{
class Application;
class Stage;
// 节点
class KGE_API Node
@ -37,7 +37,7 @@ namespace kiwano
, public EventDispatcher
, public IntrusiveListItem<NodePtr>
{
friend class Application;
friend class Stage;
friend class Transition;
friend class IntrusiveList<NodePtr>;
@ -48,7 +48,7 @@ namespace kiwano
Node();
// 更新节点
virtual void OnUpdate(Duration dt) { KGE_NOT_USED(dt); }
virtual void OnUpdate(Duration dt) { KGE_UNUSED(dt); }
// 渲染节点
virtual void OnRender() {}

View File

@ -25,7 +25,6 @@
namespace kiwano
{
Scene::Scene()
: render_border_enabled_(false)
{
scene_ = this;
@ -47,14 +46,4 @@ namespace kiwano
KGE_LOG(L"Scene exited");
}
void Scene::Render()
{
Node::Render();
if (render_border_enabled_)
{
Node::RenderBorder();
}
}
}

View File

@ -28,7 +28,7 @@ namespace kiwano
: public Node
{
friend class Transition;
friend class Application;
friend class Stage;
public:
Scene();
@ -40,14 +40,5 @@ namespace kiwano
// 退出场景
virtual void OnExit();
// 启用或禁用场景内的节点边界渲染功能
inline void SetRenderBorderEnabled(bool enabled) { render_border_enabled_ = enabled; }
protected:
void Render() override;
protected:
bool render_border_enabled_;
};
}

View File

@ -24,13 +24,13 @@
namespace kiwano
{
class Scene;
class Stage;
// 场景过渡
class KGE_API Transition
: public Object
{
friend class Application;
friend class Stage;
public:
explicit Transition(

View File

@ -20,23 +20,26 @@
#pragma once
#include "../macros.h"
#include "time.h"
#include "Event.hpp"
namespace kiwano
{
class Application;
class KGE_API Component
{
public:
virtual void SetupComponent(Application*) = 0;
virtual void SetupComponent() = 0;
virtual void DestroyComponent() = 0;
virtual void BeforeUpdate(float dt) {}
virtual void BeforeUpdate() {}
virtual void OnUpdate(Duration) {}
virtual void AfterUpdate() {}
virtual void BeforeRender() {}
virtual void OnRender() {}
virtual void AfterRender() {}
virtual void HandleMessage(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {}
virtual void HandleEvent(Event&) {}
virtual void HandleMessage(HWND, UINT, WPARAM, LPARAM) {}
};
}

View File

@ -59,7 +59,7 @@ namespace kiwano
Point GetMousePos();
public:
void SetupComponent(Application*) override {}
void SetupComponent() override {}
void DestroyComponent() override {}

152
src/kiwano/base/Stage.cpp Normal file
View File

@ -0,0 +1,152 @@
// Copyright (c) 2016-2018 Kiwano - 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 "Stage.h"
#include "../2d/Node.h"
#include "../2d/Scene.h"
#include "../2d/Transition.h"
#include "../2d/DebugNode.h"
namespace kiwano
{
Stage::Stage()
: render_border_enabled_(false)
{
}
Stage::~Stage()
{
}
void Stage::EnterScene(ScenePtr scene)
{
KGE_ASSERT(scene && "Stage::EnterScene failed, NULL pointer exception");
if (curr_scene_ == scene || next_scene_ == scene)
return;
next_scene_ = scene;
}
void Stage::EnterScene(ScenePtr scene, TransitionPtr transition)
{
EnterScene(scene);
if (transition && next_scene_)
{
if (transition_)
{
transition_->Stop();
}
transition_ = transition;
transition_->Init(curr_scene_, next_scene_);
}
}
ScenePtr Stage::GetCurrentScene()
{
return curr_scene_;
}
void Stage::SetRenderBorderEnabled(bool enabled)
{
render_border_enabled_ = enabled;
}
void Stage::ShowDebugInfo(bool show)
{
if (show)
{
if (!debug_node_)
debug_node_ = new DebugNode;
}
else
{
debug_node_.Reset();
}
}
void Stage::OnUpdate(Duration dt)
{
if (transition_)
{
transition_->Update(dt);
if (transition_->IsDone())
transition_ = nullptr;
}
if (next_scene_ && !transition_)
{
if (curr_scene_)
{
curr_scene_->OnExit();
}
next_scene_->OnEnter();
curr_scene_ = next_scene_;
next_scene_ = nullptr;
}
if (curr_scene_)
curr_scene_->Update(dt);
if (next_scene_)
next_scene_->Update(dt);
if (debug_node_)
debug_node_->Update(dt);
}
void Stage::OnRender()
{
if (transition_)
{
transition_->Render();
}
else if (curr_scene_)
{
curr_scene_->Render();
}
if (debug_node_)
debug_node_->Render();
}
void Stage::AfterRender()
{
if (render_border_enabled_)
{
if (curr_scene_)
curr_scene_->RenderBorder();
}
}
void Stage::HandleEvent(Event& evt)
{
if (debug_node_)
debug_node_->Dispatch(evt);
if (curr_scene_)
curr_scene_->Dispatch(evt);
}
}

81
src/kiwano/base/Stage.h Normal file
View File

@ -0,0 +1,81 @@
// Copyright (c) 2016-2018 Kiwano - 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.
#pragma once
#include "../macros.h"
#include "../common/Singleton.hpp"
#include "../2d/include-forwards.h"
#include "Component.h"
namespace kiwano
{
class KGE_API Stage
: public Singleton<Stage>
, public Component
{
KGE_DECLARE_SINGLETON(Stage);
public:
// 切换场景
void EnterScene(
ScenePtr scene /* 场景 */
);
// 切换场景
void EnterScene(
ScenePtr scene, /* 场景 */
TransitionPtr transition /* 场景动画 */
);
// 获取当前场景
ScenePtr GetCurrentScene();
// 启用或禁用场景内的节点边界渲染功能
void SetRenderBorderEnabled(bool enabled);
// 显示调试信息
void ShowDebugInfo(bool show = true);
public:
void SetupComponent() override {}
void DestroyComponent() override {}
void OnUpdate(Duration dt) override;
void OnRender() override;
void AfterRender() override;
void HandleEvent(Event& evt) override;
protected:
Stage();
virtual ~Stage();
protected:
bool render_border_enabled_;
ScenePtr curr_scene_;
ScenePtr next_scene_;
NodePtr debug_node_;
TransitionPtr transition_;
};
}

View File

@ -60,9 +60,10 @@
// base
//
#include "base/time.h"
#include "base/window.h"
#include "base/input.h"
#include "base/time.h"
#include "base/Stage.h"
#include "base/logs.h"
#include "renderer/render.h"
#include "platform/modules.h"

View File

@ -115,6 +115,6 @@
# pragma warning (disable: 4251)
#endif
#define KGE_NOT_USED(VAR) ((void)VAR)
#define KGE_UNUSED(VAR) ((void)VAR)
#define KGE_DEPRECATED(...) __declspec(deprecated(__VA_ARGS__))

View File

@ -20,12 +20,11 @@
#include "Application.h"
#include "modules.h"
#include "../base/window.h"
#include "../base/logs.h"
#include "../base/input.h"
#include "../base/Stage.h"
#include "../renderer/render.h"
#include "../2d/Scene.h"
#include "../2d/DebugNode.h"
#include "../2d/Transition.h"
#include <windowsx.h> // GET_X_LPARAM, GET_Y_LPARAM
#include <imm.h> // ImmAssociateContext
#include <mutex> // std::mutex
@ -34,13 +33,24 @@
namespace kiwano
{
using FunctionToPerform = Closure<void()>;
namespace
{
using FunctionToPerform = Closure<void()>;
std::mutex perform_mutex_;
Queue<FunctionToPerform> functions_to_perform_;
}
Options::Options(String const& title, int width, int height, LPCWSTR icon, Color clear_color, bool vsync, bool fullscreen, bool debug)
: title(title)
, width(width)
, height(height)
, icon(icon)
, clear_color(clear_color)
, vsync(vsync)
, fullscreen(fullscreen)
, debug(debug)
{}
}
namespace kiwano
@ -56,6 +66,7 @@ namespace kiwano
Use(Renderer::Instance());
Use(Input::Instance());
Use(Stage::Instance());
}
Application::~Application()
@ -84,11 +95,17 @@ namespace kiwano
// Setup all components
for (Component* c : components_)
{
c->SetupComponent(this);
c->SetupComponent();
}
if (options.debug)
{
Stage::Instance()->ShowDebugInfo(true);
Renderer::Instance()->SetCollectingStatus(true);
}
// Everything is ready
OnStart();
OnReady();
HWND hwnd = Window::Instance()->GetHandle();
@ -129,11 +146,6 @@ namespace kiwano
void Application::Destroy()
{
transition_.Reset();
next_scene_.Reset();
curr_scene_.Reset();
debug_node_.Reset();
if (inited_)
{
inited_ = false;
@ -146,6 +158,7 @@ namespace kiwano
}
// Destroy all instances
Stage::Destroy();
Input::Destroy();
Renderer::Destroy();
Window::Destroy();
@ -186,98 +199,17 @@ namespace kiwano
}
}
void Application::EnterScene(ScenePtr scene)
{
KGE_ASSERT(scene && "Application::EnterScene failed, NULL pointer exception");
if (curr_scene_ == scene || next_scene_ == scene)
return;
next_scene_ = scene;
}
void Application::EnterScene(ScenePtr scene, TransitionPtr transition)
{
EnterScene(scene);
if (transition && next_scene_)
{
if (transition_)
{
transition_->Stop();
}
transition_ = transition;
transition_->Init(curr_scene_, next_scene_);
}
}
ScenePtr Application::GetCurrentScene()
{
return curr_scene_;
}
void Application::SetTimeScale(float scale_factor)
{
time_scale_ = scale_factor;
}
void Application::ShowDebugInfo(bool show)
{
if (show)
{
debug_node_ = new DebugNode;
Renderer::Instance()->SetCollectingStatus(true);
}
else
{
debug_node_.Reset();
Renderer::Instance()->SetCollectingStatus(false);
}
}
void Application::Dispatch(Event& evt)
{
if (debug_node_)
debug_node_->Dispatch(evt);
if (curr_scene_)
curr_scene_->Dispatch(evt);
}
void Application::Update()
{
static auto last = Time::Now();
const auto now = Time::Now();
const auto dt = (now - last) * time_scale_;
last = now;
// Before update
for (Component* c : components_)
{
c->BeforeUpdate(dt.Seconds());
}
// Updating
if (transition_)
{
transition_->Update(dt);
if (transition_->IsDone())
transition_ = nullptr;
}
if (next_scene_ && !transition_)
{
if (curr_scene_)
{
curr_scene_->OnExit();
}
next_scene_->OnEnter();
curr_scene_ = next_scene_;
next_scene_ = nullptr;
c->BeforeUpdate();
}
// perform functions
@ -300,16 +232,19 @@ namespace kiwano
}
}
OnUpdate(dt);
// Updating
{
static auto last = Time::Now();
if (curr_scene_)
curr_scene_->Update(dt);
const auto now = Time::Now();
const auto dt = (now - last) * time_scale_;
last = now;
if (next_scene_)
next_scene_->Update(dt);
if (debug_node_)
debug_node_->Update(dt);
for (Component* c : components_)
{
c->OnUpdate(dt);
}
}
// After update
for (auto rit = components_.rbegin(); rit != components_.rend(); ++rit)
@ -327,19 +262,10 @@ namespace kiwano
}
// Rendering
if (transition_)
for (Component* c : components_)
{
transition_->Render();
c->OnRender();
}
else if (curr_scene_)
{
curr_scene_->Render();
}
OnRender();
if (debug_node_)
debug_node_->Render();
// After render
for (auto rit = components_.rbegin(); rit != components_.rend(); ++rit)
@ -348,6 +274,14 @@ namespace kiwano
}
}
void Application::DispatchEvent(Event& evt)
{
for (Component* c : components_)
{
c->HandleEvent(evt);
}
}
void Application::PreformInMainThread(Closure<void()> function)
{
std::lock_guard<std::mutex> lock(perform_mutex_);
@ -384,28 +318,22 @@ namespace kiwano
case WM_KEYUP:
case WM_SYSKEYUP:
{
if (!app->transition_ && app->curr_scene_)
{
bool down = msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN;
Event evt(down ? Event::KeyDown : Event::KeyUp);
evt.key.code = static_cast<int>(wparam);
evt.key.count = static_cast<int>(lparam & 0xFF);
bool down = msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN;
Event evt(down ? Event::KeyDown : Event::KeyUp);
evt.key.code = static_cast<int>(wparam);
evt.key.count = static_cast<int>(lparam & 0xFF);
app->Dispatch(evt);
}
app->DispatchEvent(evt);
}
break;
case WM_CHAR:
{
if (!app->transition_ && app->curr_scene_)
{
Event evt(Event::Char);
evt.key.c = static_cast<char>(wparam);
evt.key.count = static_cast<int>(lparam & 0xFF);
Event evt(Event::Char);
evt.key.c = static_cast<char>(wparam);
evt.key.count = static_cast<int>(lparam & 0xFF);
app->Dispatch(evt);
}
app->DispatchEvent(evt);
}
break;
@ -421,26 +349,23 @@ namespace kiwano
case WM_MOUSEMOVE:
case WM_MOUSEWHEEL:
{
if (!app->transition_ && app->curr_scene_)
{
Event evt;
Event evt;
evt.mouse.x = static_cast<float>(GET_X_LPARAM(lparam));
evt.mouse.y = static_cast<float>(GET_Y_LPARAM(lparam));
evt.mouse.left_btn_down = !!(wparam & MK_LBUTTON);
evt.mouse.left_btn_down = !!(wparam & MK_RBUTTON);
evt.mouse.x = static_cast<float>(GET_X_LPARAM(lparam));
evt.mouse.y = static_cast<float>(GET_Y_LPARAM(lparam));
evt.mouse.left_btn_down = !!(wparam & MK_LBUTTON);
evt.mouse.left_btn_down = !!(wparam & MK_RBUTTON);
if (msg == WM_MOUSEMOVE) { evt.type = Event::MouseMove; }
else if (msg == WM_LBUTTONDOWN || msg == WM_RBUTTONDOWN || msg == WM_MBUTTONDOWN) { evt.type = Event::MouseBtnDown; }
else if (msg == WM_LBUTTONUP || msg == WM_RBUTTONUP || msg == WM_MBUTTONUP) { evt.type = Event::MouseBtnUp; }
else if (msg == WM_MOUSEWHEEL) { evt.type = Event::MouseWheel; evt.mouse.wheel = GET_WHEEL_DELTA_WPARAM(wparam) / (float)WHEEL_DELTA; }
if (msg == WM_MOUSEMOVE) { evt.type = Event::MouseMove; }
else if (msg == WM_LBUTTONDOWN || msg == WM_RBUTTONDOWN || msg == WM_MBUTTONDOWN) { evt.type = Event::MouseBtnDown; }
else if (msg == WM_LBUTTONUP || msg == WM_RBUTTONUP || msg == WM_MBUTTONUP) { evt.type = Event::MouseBtnUp; }
else if (msg == WM_MOUSEWHEEL) { evt.type = Event::MouseWheel; evt.mouse.wheel = GET_WHEEL_DELTA_WPARAM(wparam) / (float)WHEEL_DELTA; }
if (msg == WM_LBUTTONDOWN || msg == WM_LBUTTONUP) { evt.mouse.button = MouseButton::Left; }
else if (msg == WM_RBUTTONDOWN || msg == WM_RBUTTONUP) { evt.mouse.button = MouseButton::Right; }
else if (msg == WM_MBUTTONDOWN || msg == WM_MBUTTONUP) { evt.mouse.button = MouseButton::Middle; }
if (msg == WM_LBUTTONDOWN || msg == WM_LBUTTONUP) { evt.mouse.button = MouseButton::Left; }
else if (msg == WM_RBUTTONDOWN || msg == WM_RBUTTONUP) { evt.mouse.button = MouseButton::Right; }
else if (msg == WM_MBUTTONDOWN || msg == WM_MBUTTONUP) { evt.mouse.button = MouseButton::Middle; }
app->Dispatch(evt);
}
app->DispatchEvent(evt);
}
break;
@ -454,31 +379,25 @@ namespace kiwano
{
KGE_LOG(L"Window resized");
if (app->curr_scene_)
{
Event evt(Event::WindowResized);
evt.win.width = LOWORD(lparam);
evt.win.height = HIWORD(lparam);
app->Dispatch(evt);
}
Window::Instance()->UpdateWindowRect();
Event evt(Event::WindowResized);
evt.win.width = LOWORD(lparam);
evt.win.height = HIWORD(lparam);
app->DispatchEvent(evt);
}
}
break;
case WM_MOVE:
{
if (app->curr_scene_)
{
int x = (int)(short)LOWORD(lparam);
int y = (int)(short)HIWORD(lparam);
int x = (int)(short)LOWORD(lparam);
int y = (int)(short)HIWORD(lparam);
Event evt(Event::WindowMoved);
evt.win.x = x;
evt.win.y = y;
app->Dispatch(evt);
}
Event evt(Event::WindowMoved);
evt.win.x = x;
evt.win.y = y;
app->DispatchEvent(evt);
}
break;
@ -488,12 +407,9 @@ namespace kiwano
Window::Instance()->SetActive(active);
if (app->curr_scene_)
{
Event evt(Event::WindowFocusChanged);
evt.win.focus = active;
app->Dispatch(evt);
}
Event evt(Event::WindowFocusChanged);
evt.win.focus = active;
app->DispatchEvent(evt);
}
break;
@ -501,12 +417,9 @@ namespace kiwano
{
KGE_LOG(L"Window title changed");
if (app->curr_scene_)
{
Event evt(Event::WindowTitleChanged);
evt.win.title = reinterpret_cast<const wchar_t*>(lparam);
app->Dispatch(evt);
}
Event evt(Event::WindowTitleChanged);
evt.win.title = reinterpret_cast<const wchar_t*>(lparam);
app->DispatchEvent(evt);
}
break;
@ -539,12 +452,8 @@ namespace kiwano
{
KGE_LOG(L"Window was destroyed");
if (app->curr_scene_)
{
Event evt(Event::WindowClosed);
app->Dispatch(evt);
}
Event evt(Event::WindowClosed);
app->DispatchEvent(evt);
app->OnDestroy();
::PostQuitMessage(0);

View File

@ -19,11 +19,14 @@
// THE SOFTWARE.
#pragma once
#include "../2d/include-forwards.h"
#include "../common/String.hpp"
#include "../common/Array.hpp"
#include "../common/Closure.hpp"
#include "../common/Noncopyable.hpp"
#include "../base/time.h"
#include "../base/window.h"
#include "../base/Component.h"
#include "../base/Event.hpp"
#include "../2d/Color.h"
namespace kiwano
{
@ -36,6 +39,7 @@ namespace kiwano
Color clear_color; // 清屏颜色
bool vsync; // 垂直同步
bool fullscreen; // 全屏模式
bool debug; // 调试模式
Options(
String const& title = L"Kiwano Game",
@ -44,19 +48,13 @@ namespace kiwano
LPCWSTR icon = nullptr,
Color clear_color = Color::Black,
bool vsync = true,
bool fullscreen = false
)
: title(title)
, width(width)
, height(height)
, icon(icon)
, clear_color(clear_color)
, vsync(vsync)
, fullscreen(fullscreen)
{}
bool fullscreen = false,
bool debug = false
);
};
// 应用
class KGE_API Application
: protected Noncopyable
{
@ -67,24 +65,18 @@ namespace kiwano
// 初始化
void Init(
Options const& options
Options const& options = Options{}
);
// 启动
virtual void OnStart() {}
// 初始化成功
virtual void OnReady() {}
// 关闭时
// 窗口关闭时
virtual bool OnClosing() { return true; }
// 销毁时
virtual void OnDestroy() {}
// 渲染时
virtual void OnRender() {}
// 更新时
virtual void OnUpdate(Duration dt) { KGE_NOT_USED(dt); }
// 运行
void Run();
@ -104,32 +96,13 @@ namespace kiwano
Component* component
);
// 切换场景
void EnterScene(
ScenePtr scene /* 场景 */
);
// 切换场景
void EnterScene(
ScenePtr scene, /* 场景 */
TransitionPtr transition /* 场景动画 */
);
// 获取当前场景
ScenePtr GetCurrentScene();
// 设置时间缩放因子
void SetTimeScale(
float scale_factor
);
// 显示调试信息
void ShowDebugInfo(
bool show = true
);
// 分发事件
void Dispatch(Event& evt);
void DispatchEvent(Event& evt);
// 在 Kiwano 主线程中执行函数
// 当在其他线程调用 Kiwano 函数时使用
@ -145,14 +118,9 @@ namespace kiwano
static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
protected:
bool end_;
bool inited_;
float time_scale_;
ScenePtr curr_scene_;
ScenePtr next_scene_;
NodePtr debug_node_;
TransitionPtr transition_;
bool end_;
bool inited_;
float time_scale_;
Array<Component*> components_;
};

View File

@ -213,10 +213,10 @@ namespace kiwano
__in DWRITE_GLYPH_RUN_DESCRIPTION const* glyphRunDescription,
IUnknown* clientDrawingEffect)
{
KGE_NOT_USED(clientDrawingContext);
KGE_NOT_USED(measuringMode);
KGE_NOT_USED(glyphRunDescription);
KGE_NOT_USED(clientDrawingEffect);
KGE_UNUSED(clientDrawingContext);
KGE_UNUSED(measuringMode);
KGE_UNUSED(glyphRunDescription);
KGE_UNUSED(clientDrawingEffect);
HRESULT hr = S_OK;
@ -304,8 +304,8 @@ namespace kiwano
__in DWRITE_UNDERLINE const* underline,
IUnknown* clientDrawingEffect)
{
KGE_NOT_USED(clientDrawingContext);
KGE_NOT_USED(clientDrawingEffect);
KGE_UNUSED(clientDrawingContext);
KGE_UNUSED(clientDrawingEffect);
HRESULT hr;
@ -373,8 +373,8 @@ namespace kiwano
__in DWRITE_STRIKETHROUGH const* strikethrough,
IUnknown* clientDrawingEffect)
{
KGE_NOT_USED(clientDrawingContext);
KGE_NOT_USED(clientDrawingEffect);
KGE_UNUSED(clientDrawingContext);
KGE_UNUSED(clientDrawingEffect);
HRESULT hr;
@ -444,13 +444,13 @@ namespace kiwano
BOOL IsRightToLeft,
IUnknown* clientDrawingEffect)
{
KGE_NOT_USED(clientDrawingContext);
KGE_NOT_USED(originX);
KGE_NOT_USED(originY);
KGE_NOT_USED(inlineObject);
KGE_NOT_USED(IsSideways);
KGE_NOT_USED(IsRightToLeft);
KGE_NOT_USED(clientDrawingEffect);
KGE_UNUSED(clientDrawingContext);
KGE_UNUSED(originX);
KGE_UNUSED(originY);
KGE_UNUSED(inlineObject);
KGE_UNUSED(IsSideways);
KGE_UNUSED(IsRightToLeft);
KGE_UNUSED(clientDrawingEffect);
return E_NOTIMPL;
}
@ -476,7 +476,7 @@ namespace kiwano
__maybenull void* clientDrawingContext,
__out BOOL* isDisabled)
{
KGE_NOT_USED(clientDrawingContext);
KGE_UNUSED(clientDrawingContext);
*isDisabled = FALSE;
return S_OK;
@ -486,7 +486,7 @@ namespace kiwano
__maybenull void* clientDrawingContext,
__out DWRITE_MATRIX* transform)
{
KGE_NOT_USED(clientDrawingContext);
KGE_UNUSED(clientDrawingContext);
pRT_->GetTransform(reinterpret_cast<D2D1_MATRIX_3X2_F*>(transform));
return S_OK;
@ -496,7 +496,7 @@ namespace kiwano
__maybenull void* clientDrawingContext,
__out FLOAT* pixelsPerDip)
{
KGE_NOT_USED(clientDrawingContext);
KGE_UNUSED(clientDrawingContext);
float x, yUnused;

View File

@ -41,7 +41,7 @@ namespace kiwano
{
}
void Renderer::SetupComponent(Application* app)
void Renderer::SetupComponent()
{
KGE_LOG(L"Creating device resources");

View File

@ -152,7 +152,7 @@ namespace kiwano
);
public:
void SetupComponent(Application*) override;
void SetupComponent() override;
void DestroyComponent() override;