Add RenderComponent & UpdateComponent & EventComponent

This commit is contained in:
Nomango 2019-09-10 13:17:47 +08:00
parent 2345e22792
commit ccbaf0573f
12 changed files with 154 additions and 51 deletions

View File

@ -115,6 +115,7 @@
<ClCompile Include="..\src\kiwano\2d\Transform.cpp" /> <ClCompile Include="..\src\kiwano\2d\Transform.cpp" />
<ClCompile Include="..\src\kiwano\2d\Transition.cpp" /> <ClCompile Include="..\src\kiwano\2d\Transition.cpp" />
<ClCompile Include="..\src\kiwano\base\AsyncTask.cpp" /> <ClCompile Include="..\src\kiwano\base\AsyncTask.cpp" />
<ClCompile Include="..\src\kiwano\base\Component.cpp" />
<ClCompile Include="..\src\kiwano\base\EventDispatcher.cpp" /> <ClCompile Include="..\src\kiwano\base\EventDispatcher.cpp" />
<ClCompile Include="..\src\kiwano\base\EventListener.cpp" /> <ClCompile Include="..\src\kiwano\base\EventListener.cpp" />
<ClCompile Include="..\src\kiwano\base\Input.cpp" /> <ClCompile Include="..\src\kiwano\base\Input.cpp" />

View File

@ -491,5 +491,8 @@
<ClCompile Include="..\src\kiwano\renderer\Brush.cpp"> <ClCompile Include="..\src\kiwano\renderer\Brush.cpp">
<Filter>renderer</Filter> <Filter>renderer</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\src\kiwano\base\Component.cpp">
<Filter>base</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -30,15 +30,11 @@ namespace kiwano
{ {
class KGE_API Audio class KGE_API Audio
: public Singleton<Audio> : public Singleton<Audio>
, public Component , public ComponentBase
{ {
KGE_DECLARE_SINGLETON(Audio); KGE_DECLARE_SINGLETON(Audio);
public: public:
void SetupComponent() override;
void DestroyComponent() override;
// 开启设备 // 开启设备
void Open(); void Open();
@ -50,6 +46,11 @@ namespace kiwano
const Transcoder::Buffer& buffer const Transcoder::Buffer& buffer
); );
public:
void SetupComponent() override;
void DestroyComponent() override;
protected: protected:
Audio(); Audio();

View File

@ -28,7 +28,9 @@ namespace kiwano
{ {
class ImGuiModule class ImGuiModule
: public Singleton<ImGuiModule> : public Singleton<ImGuiModule>
, public Component , public RenderComponent
, public UpdateComponent
, public EventComponent
{ {
KGE_DECLARE_SINGLETON(ImGuiModule); KGE_DECLARE_SINGLETON(ImGuiModule);

View File

@ -31,7 +31,7 @@ namespace kiwano
{ {
class KGE_API HttpClient class KGE_API HttpClient
: public Singleton<HttpClient> : public Singleton<HttpClient>
, public Component , public ComponentBase
{ {
KGE_DECLARE_SINGLETON(HttpClient); KGE_DECLARE_SINGLETON(HttpClient);

View File

@ -0,0 +1,51 @@
// 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 "Component.h"
namespace kiwano
{
ComponentBase::ComponentBase()
: flag_(0)
{
}
bool ComponentBase::Check(const Int32 flag)
{
return !!(flag_ & flag);
}
RenderComponent::RenderComponent()
{
flag_ |= flag;
}
UpdateComponent::UpdateComponent()
{
flag_ |= flag;
}
EventComponent::EventComponent()
{
flag_ |= flag;
}
}

View File

@ -25,23 +25,73 @@
namespace kiwano namespace kiwano
{ {
class RenderTarget;
class KGE_API Component #define KGE_DEFINE_COMPONENT_FLAG ( 0x01 << (__COUNTER__ % 32) )
class KGE_API ComponentBase
{ {
public: public:
virtual void SetupComponent() = 0; virtual void SetupComponent() = 0;
virtual void DestroyComponent() = 0; virtual void DestroyComponent() = 0;
virtual void BeforeUpdate() {} bool Check(const Int32 flag);
virtual void OnUpdate(Duration) {}
virtual void AfterUpdate() {} protected:
ComponentBase();
protected:
Int32 flag_;
};
class RenderTarget;
class KGE_API RenderComponent
: public virtual ComponentBase
{
public:
static const Int32 flag = KGE_DEFINE_COMPONENT_FLAG;
RenderComponent();
virtual void BeforeRender() {} virtual void BeforeRender() {}
virtual void OnRender(RenderTarget*) {} virtual void OnRender(RenderTarget*) {}
virtual void AfterRender() {} virtual void AfterRender() {}
};
class KGE_API UpdateComponent
: public virtual ComponentBase
{
public:
static const Int32 flag = KGE_DEFINE_COMPONENT_FLAG;
UpdateComponent();
virtual void BeforeUpdate() {}
virtual void OnUpdate(Duration) {}
virtual void AfterUpdate() {}
};
class KGE_API EventComponent
: public virtual ComponentBase
{
public:
static const Int32 flag = KGE_DEFINE_COMPONENT_FLAG;
EventComponent();
virtual void HandleEvent(Event&) {} virtual void HandleEvent(Event&) {}
virtual void HandleMessage(HWND, UInt32, WPARAM, LPARAM) {} virtual void HandleMessage(HWND, UInt32, WPARAM, LPARAM) {}
}; };
#undef KGE_DEFINE_COMPONENT_FLAG
} }

View File

@ -28,7 +28,9 @@ namespace kiwano
// ľźŃÝ // ľźŃÝ
class KGE_API Director class KGE_API Director
: public Singleton<Director> : public Singleton<Director>
, public Component , public UpdateComponent
, public RenderComponent
, public EventComponent
{ {
KGE_DECLARE_SINGLETON(Director); KGE_DECLARE_SINGLETON(Director);

View File

@ -29,7 +29,8 @@ namespace kiwano
{ {
class KGE_API Input class KGE_API Input
: public Singleton<Input> : public Singleton<Input>
, public Component , public UpdateComponent
, public EventComponent
{ {
KGE_DECLARE_SINGLETON(Input); KGE_DECLARE_SINGLETON(Input);

View File

@ -87,7 +87,7 @@ namespace kiwano
Renderer::GetInstance()->Init(config.render); Renderer::GetInstance()->Init(config.render);
// Setup all components // Setup all components
for (Component* c : components_) for (auto c : comps_)
{ {
c->SetupComponent(); c->SetupComponent();
} }
@ -148,11 +148,11 @@ namespace kiwano
{ {
inited_ = false; inited_ = false;
for (auto iter = components_.rbegin(); iter != components_.rend(); ++iter) for (auto iter = comps_.rbegin(); iter != comps_.rend(); ++iter)
{ {
(*iter)->DestroyComponent(); (*iter)->DestroyComponent();
} }
components_.clear(); comps_.clear();
} }
// Destroy all instances // Destroy all instances
@ -167,35 +167,28 @@ namespace kiwano
// Logger::DestroyInstance(); // Logger::DestroyInstance();
} }
void Application::Use(Component* component) void Application::Use(ComponentBase* component)
{ {
if (component) if (component)
{ {
#if defined(KGE_DEBUG) #if defined(KGE_DEBUG)
if (components_.contains(component)) if (comps_.contains(component))
{ {
KGE_ASSERT(false && "Component already exists!"); KGE_ASSERT(false && "Component already exists!");
} }
#endif #endif
components_.push_back(component); comps_.push_back(component);
}
}
void Application::Remove(Component* component) if (component->Check(RenderComponent::flag))
{ render_comps_.push_back(dynamic_cast<RenderComponent*>(component));
if (component)
{ if (component->Check(UpdateComponent::flag))
for (auto iter = components_.begin(); iter != components_.end(); ++iter) update_comps_.push_back(dynamic_cast<UpdateComponent*>(component));
{
if ((*iter) == component) if (component->Check(EventComponent::flag))
{ event_comps_.push_back(dynamic_cast<EventComponent*>(component));
(*iter)->DestroyComponent();
components_.erase(iter);
break;
}
}
} }
} }
@ -207,7 +200,7 @@ namespace kiwano
void Application::Update() void Application::Update()
{ {
// Before update // Before update
for (Component* c : components_) for (auto c : update_comps_)
{ {
c->BeforeUpdate(); c->BeforeUpdate();
} }
@ -240,14 +233,14 @@ namespace kiwano
const auto dt = (now - last) * time_scale_; const auto dt = (now - last) * time_scale_;
last = now; last = now;
for (Component* c : components_) for (auto c : update_comps_)
{ {
c->OnUpdate(dt); c->OnUpdate(dt);
} }
} }
// After update // After update
for (auto rit = components_.rbegin(); rit != components_.rend(); ++rit) for (auto rit = update_comps_.rbegin(); rit != update_comps_.rend(); ++rit)
{ {
(*rit)->AfterUpdate(); (*rit)->AfterUpdate();
} }
@ -256,20 +249,20 @@ namespace kiwano
void Application::Render() void Application::Render()
{ {
// Before render // Before render
for (Component* c : components_) for (auto c : render_comps_)
{ {
c->BeforeRender(); c->BeforeRender();
} }
// Rendering // Rendering
Renderer* renderer = Renderer::GetInstance(); Renderer* renderer = Renderer::GetInstance();
for (Component* c : components_) for (auto c : render_comps_)
{ {
c->OnRender(renderer); c->OnRender(renderer);
} }
// After render // After render
for (auto rit = components_.rbegin(); rit != components_.rend(); ++rit) for (auto rit = render_comps_.rbegin(); rit != render_comps_.rend(); ++rit)
{ {
(*rit)->AfterRender(); (*rit)->AfterRender();
} }
@ -277,7 +270,7 @@ namespace kiwano
void Application::DispatchEvent(Event& evt) void Application::DispatchEvent(Event& evt)
{ {
for (Component* c : components_) for (auto c : event_comps_)
{ {
c->HandleEvent(evt); c->HandleEvent(evt);
} }
@ -298,7 +291,7 @@ namespace kiwano
} }
// Handle Message // Handle Message
for (Component* c : app->components_) for (auto c : app->event_comps_)
{ {
c->HandleMessage(hwnd, msg, wparam, lparam); c->HandleMessage(hwnd, msg, wparam, lparam);
} }

View File

@ -82,12 +82,7 @@ namespace kiwano
// 添加组件 // 添加组件
void Use( void Use(
Component* component ComponentBase* component
);
// 卸载组件
void Remove(
Component* component
); );
// 设置时间缩放因子 // 设置时间缩放因子
@ -116,6 +111,9 @@ namespace kiwano
bool inited_; bool inited_;
Float32 time_scale_; Float32 time_scale_;
Vector<Component*> components_; Vector<ComponentBase*> comps_;
Vector<RenderComponent*> render_comps_;
Vector<UpdateComponent*> update_comps_;
Vector<EventComponent*> event_comps_;
}; };
} }

View File

@ -55,7 +55,8 @@ namespace kiwano
// 渲染器 // 渲染器
class KGE_API Renderer class KGE_API Renderer
: public Singleton<Renderer> : public Singleton<Renderer>
, public Component , public RenderComponent
, public EventComponent
, public RenderTarget , public RenderTarget
{ {
KGE_DECLARE_SINGLETON(Renderer); KGE_DECLARE_SINGLETON(Renderer);