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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -20,7 +20,10 @@
#include <kiwano/base/logs.h> #include <kiwano/base/logs.h>
#include <kiwano/platform/Application.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 <thread>
#include <codecvt> #include <codecvt>
@ -249,7 +252,7 @@ namespace kiwano
{ {
} }
void HttpClient::SetupComponent(Application * app) void HttpClient::SetupComponent()
{ {
::curl_global_init(CURL_GLOBAL_ALL); ::curl_global_init(CURL_GLOBAL_ALL);

View File

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

View File

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

View File

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

View File

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

View File

@ -25,7 +25,6 @@
namespace kiwano namespace kiwano
{ {
Scene::Scene() Scene::Scene()
: render_border_enabled_(false)
{ {
scene_ = this; scene_ = this;
@ -47,14 +46,4 @@ namespace kiwano
KGE_LOG(L"Scene exited"); 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 : public Node
{ {
friend class Transition; friend class Transition;
friend class Application; friend class Stage;
public: public:
Scene(); Scene();
@ -40,14 +40,5 @@ namespace kiwano
// 退出场景 // 退出场景
virtual void OnExit(); 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 namespace kiwano
{ {
class Scene; class Stage;
// 场景过渡 // 场景过渡
class KGE_API Transition class KGE_API Transition
: public Object : public Object
{ {
friend class Application; friend class Stage;
public: public:
explicit Transition( explicit Transition(

View File

@ -20,23 +20,26 @@
#pragma once #pragma once
#include "../macros.h" #include "../macros.h"
#include "time.h"
#include "Event.hpp"
namespace kiwano namespace kiwano
{ {
class Application;
class KGE_API Component class KGE_API Component
{ {
public: public:
virtual void SetupComponent(Application*) = 0; virtual void SetupComponent() = 0;
virtual void DestroyComponent() = 0; virtual void DestroyComponent() = 0;
virtual void BeforeUpdate(float dt) {} virtual void BeforeUpdate() {}
virtual void OnUpdate(Duration) {}
virtual void AfterUpdate() {} virtual void AfterUpdate() {}
virtual void BeforeRender() {} virtual void BeforeRender() {}
virtual void OnRender() {}
virtual void AfterRender() {} 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(); Point GetMousePos();
public: public:
void SetupComponent(Application*) override {} void SetupComponent() override {}
void DestroyComponent() 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 // base
// //
#include "base/time.h"
#include "base/window.h" #include "base/window.h"
#include "base/input.h" #include "base/input.h"
#include "base/time.h" #include "base/Stage.h"
#include "base/logs.h" #include "base/logs.h"
#include "renderer/render.h" #include "renderer/render.h"
#include "platform/modules.h" #include "platform/modules.h"

View File

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

View File

@ -20,12 +20,11 @@
#include "Application.h" #include "Application.h"
#include "modules.h" #include "modules.h"
#include "../base/window.h"
#include "../base/logs.h" #include "../base/logs.h"
#include "../base/input.h" #include "../base/input.h"
#include "../base/Stage.h"
#include "../renderer/render.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 <windowsx.h> // GET_X_LPARAM, GET_Y_LPARAM
#include <imm.h> // ImmAssociateContext #include <imm.h> // ImmAssociateContext
#include <mutex> // std::mutex #include <mutex> // std::mutex
@ -34,13 +33,24 @@
namespace kiwano namespace kiwano
{ {
using FunctionToPerform = Closure<void()>;
namespace namespace
{ {
using FunctionToPerform = Closure<void()>;
std::mutex perform_mutex_; std::mutex perform_mutex_;
Queue<FunctionToPerform> functions_to_perform_; 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 namespace kiwano
@ -56,6 +66,7 @@ namespace kiwano
Use(Renderer::Instance()); Use(Renderer::Instance());
Use(Input::Instance()); Use(Input::Instance());
Use(Stage::Instance());
} }
Application::~Application() Application::~Application()
@ -84,11 +95,17 @@ namespace kiwano
// Setup all components // Setup all components
for (Component* c : components_) for (Component* c : components_)
{ {
c->SetupComponent(this); c->SetupComponent();
}
if (options.debug)
{
Stage::Instance()->ShowDebugInfo(true);
Renderer::Instance()->SetCollectingStatus(true);
} }
// Everything is ready // Everything is ready
OnStart(); OnReady();
HWND hwnd = Window::Instance()->GetHandle(); HWND hwnd = Window::Instance()->GetHandle();
@ -129,11 +146,6 @@ namespace kiwano
void Application::Destroy() void Application::Destroy()
{ {
transition_.Reset();
next_scene_.Reset();
curr_scene_.Reset();
debug_node_.Reset();
if (inited_) if (inited_)
{ {
inited_ = false; inited_ = false;
@ -146,6 +158,7 @@ namespace kiwano
} }
// Destroy all instances // Destroy all instances
Stage::Destroy();
Input::Destroy(); Input::Destroy();
Renderer::Destroy(); Renderer::Destroy();
Window::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) void Application::SetTimeScale(float scale_factor)
{ {
time_scale_ = 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() void Application::Update()
{ {
static auto last = Time::Now();
const auto now = Time::Now();
const auto dt = (now - last) * time_scale_;
last = now;
// Before update // Before update
for (Component* c : components_) for (Component* c : components_)
{ {
c->BeforeUpdate(dt.Seconds()); c->BeforeUpdate();
}
// 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;
} }
// perform functions // perform functions
@ -300,16 +232,19 @@ namespace kiwano
} }
} }
OnUpdate(dt); // Updating
{
static auto last = Time::Now();
if (curr_scene_) const auto now = Time::Now();
curr_scene_->Update(dt); const auto dt = (now - last) * time_scale_;
last = now;
if (next_scene_) for (Component* c : components_)
next_scene_->Update(dt); {
c->OnUpdate(dt);
if (debug_node_) }
debug_node_->Update(dt); }
// After update // After update
for (auto rit = components_.rbegin(); rit != components_.rend(); ++rit) for (auto rit = components_.rbegin(); rit != components_.rend(); ++rit)
@ -327,19 +262,10 @@ namespace kiwano
} }
// Rendering // 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 // After render
for (auto rit = components_.rbegin(); rit != components_.rend(); ++rit) 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) void Application::PreformInMainThread(Closure<void()> function)
{ {
std::lock_guard<std::mutex> lock(perform_mutex_); std::lock_guard<std::mutex> lock(perform_mutex_);
@ -383,29 +317,23 @@ namespace kiwano
case WM_SYSKEYDOWN: case WM_SYSKEYDOWN:
case WM_KEYUP: case WM_KEYUP:
case WM_SYSKEYUP: case WM_SYSKEYUP:
{
if (!app->transition_ && app->curr_scene_)
{ {
bool down = msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN; bool down = msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN;
Event evt(down ? Event::KeyDown : Event::KeyUp); Event evt(down ? Event::KeyDown : Event::KeyUp);
evt.key.code = static_cast<int>(wparam); evt.key.code = static_cast<int>(wparam);
evt.key.count = static_cast<int>(lparam & 0xFF); evt.key.count = static_cast<int>(lparam & 0xFF);
app->Dispatch(evt); app->DispatchEvent(evt);
}
} }
break; break;
case WM_CHAR: case WM_CHAR:
{
if (!app->transition_ && app->curr_scene_)
{ {
Event evt(Event::Char); Event evt(Event::Char);
evt.key.c = static_cast<char>(wparam); evt.key.c = static_cast<char>(wparam);
evt.key.count = static_cast<int>(lparam & 0xFF); evt.key.count = static_cast<int>(lparam & 0xFF);
app->Dispatch(evt); app->DispatchEvent(evt);
}
} }
break; break;
@ -420,8 +348,6 @@ namespace kiwano
//case WM_RBUTTONDBLCLK: //case WM_RBUTTONDBLCLK:
case WM_MOUSEMOVE: case WM_MOUSEMOVE:
case WM_MOUSEWHEEL: case WM_MOUSEWHEEL:
{
if (!app->transition_ && app->curr_scene_)
{ {
Event evt; Event evt;
@ -439,8 +365,7 @@ namespace kiwano
else if (msg == WM_RBUTTONDOWN || msg == WM_RBUTTONUP) { evt.mouse.button = MouseButton::Right; } 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; } else if (msg == WM_MBUTTONDOWN || msg == WM_MBUTTONUP) { evt.mouse.button = MouseButton::Middle; }
app->Dispatch(evt); app->DispatchEvent(evt);
}
} }
break; break;
@ -454,22 +379,17 @@ namespace kiwano
{ {
KGE_LOG(L"Window resized"); KGE_LOG(L"Window resized");
if (app->curr_scene_) Window::Instance()->UpdateWindowRect();
{
Event evt(Event::WindowResized); Event evt(Event::WindowResized);
evt.win.width = LOWORD(lparam); evt.win.width = LOWORD(lparam);
evt.win.height = HIWORD(lparam); evt.win.height = HIWORD(lparam);
app->Dispatch(evt); app->DispatchEvent(evt);
}
Window::Instance()->UpdateWindowRect();
} }
} }
break; break;
case WM_MOVE: case WM_MOVE:
{
if (app->curr_scene_)
{ {
int x = (int)(short)LOWORD(lparam); int x = (int)(short)LOWORD(lparam);
int y = (int)(short)HIWORD(lparam); int y = (int)(short)HIWORD(lparam);
@ -477,8 +397,7 @@ namespace kiwano
Event evt(Event::WindowMoved); Event evt(Event::WindowMoved);
evt.win.x = x; evt.win.x = x;
evt.win.y = y; evt.win.y = y;
app->Dispatch(evt); app->DispatchEvent(evt);
}
} }
break; break;
@ -488,12 +407,9 @@ namespace kiwano
Window::Instance()->SetActive(active); Window::Instance()->SetActive(active);
if (app->curr_scene_)
{
Event evt(Event::WindowFocusChanged); Event evt(Event::WindowFocusChanged);
evt.win.focus = active; evt.win.focus = active;
app->Dispatch(evt); app->DispatchEvent(evt);
}
} }
break; break;
@ -501,12 +417,9 @@ namespace kiwano
{ {
KGE_LOG(L"Window title changed"); KGE_LOG(L"Window title changed");
if (app->curr_scene_)
{
Event evt(Event::WindowTitleChanged); Event evt(Event::WindowTitleChanged);
evt.win.title = reinterpret_cast<const wchar_t*>(lparam); evt.win.title = reinterpret_cast<const wchar_t*>(lparam);
app->Dispatch(evt); app->DispatchEvent(evt);
}
} }
break; break;
@ -539,12 +452,8 @@ namespace kiwano
{ {
KGE_LOG(L"Window was destroyed"); KGE_LOG(L"Window was destroyed");
if (app->curr_scene_)
{
Event evt(Event::WindowClosed); Event evt(Event::WindowClosed);
app->Dispatch(evt); app->DispatchEvent(evt);
}
app->OnDestroy(); app->OnDestroy();
::PostQuitMessage(0); ::PostQuitMessage(0);

View File

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

View File

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

View File

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