Add RenderComponent & UpdateComponent & EventComponent
This commit is contained in:
parent
2345e22792
commit
ccbaf0573f
|
|
@ -115,6 +115,7 @@
|
|||
<ClCompile Include="..\src\kiwano\2d\Transform.cpp" />
|
||||
<ClCompile Include="..\src\kiwano\2d\Transition.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\EventListener.cpp" />
|
||||
<ClCompile Include="..\src\kiwano\base\Input.cpp" />
|
||||
|
|
|
|||
|
|
@ -491,5 +491,8 @@
|
|||
<ClCompile Include="..\src\kiwano\renderer\Brush.cpp">
|
||||
<Filter>renderer</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\src\kiwano\base\Component.cpp">
|
||||
<Filter>base</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
|
@ -30,15 +30,11 @@ namespace kiwano
|
|||
{
|
||||
class KGE_API Audio
|
||||
: public Singleton<Audio>
|
||||
, public Component
|
||||
, public ComponentBase
|
||||
{
|
||||
KGE_DECLARE_SINGLETON(Audio);
|
||||
|
||||
public:
|
||||
void SetupComponent() override;
|
||||
|
||||
void DestroyComponent() override;
|
||||
|
||||
// 开启设备
|
||||
void Open();
|
||||
|
||||
|
|
@ -50,6 +46,11 @@ namespace kiwano
|
|||
const Transcoder::Buffer& buffer
|
||||
);
|
||||
|
||||
public:
|
||||
void SetupComponent() override;
|
||||
|
||||
void DestroyComponent() override;
|
||||
|
||||
protected:
|
||||
Audio();
|
||||
|
||||
|
|
|
|||
|
|
@ -28,7 +28,9 @@ namespace kiwano
|
|||
{
|
||||
class ImGuiModule
|
||||
: public Singleton<ImGuiModule>
|
||||
, public Component
|
||||
, public RenderComponent
|
||||
, public UpdateComponent
|
||||
, public EventComponent
|
||||
{
|
||||
KGE_DECLARE_SINGLETON(ImGuiModule);
|
||||
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ namespace kiwano
|
|||
{
|
||||
class KGE_API HttpClient
|
||||
: public Singleton<HttpClient>
|
||||
, public Component
|
||||
, public ComponentBase
|
||||
{
|
||||
KGE_DECLARE_SINGLETON(HttpClient);
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -25,23 +25,73 @@
|
|||
|
||||
namespace kiwano
|
||||
{
|
||||
class RenderTarget;
|
||||
|
||||
class KGE_API Component
|
||||
#define KGE_DEFINE_COMPONENT_FLAG ( 0x01 << (__COUNTER__ % 32) )
|
||||
|
||||
class KGE_API ComponentBase
|
||||
{
|
||||
public:
|
||||
virtual void SetupComponent() = 0;
|
||||
virtual void DestroyComponent() = 0;
|
||||
|
||||
virtual void BeforeUpdate() {}
|
||||
virtual void OnUpdate(Duration) {}
|
||||
virtual void AfterUpdate() {}
|
||||
bool Check(const Int32 flag);
|
||||
|
||||
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 OnRender(RenderTarget*) {}
|
||||
|
||||
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 HandleMessage(HWND, UInt32, WPARAM, LPARAM) {}
|
||||
};
|
||||
|
||||
|
||||
#undef KGE_DEFINE_COMPONENT_FLAG
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,7 +28,9 @@ namespace kiwano
|
|||
// ľźŃÝ
|
||||
class KGE_API Director
|
||||
: public Singleton<Director>
|
||||
, public Component
|
||||
, public UpdateComponent
|
||||
, public RenderComponent
|
||||
, public EventComponent
|
||||
{
|
||||
KGE_DECLARE_SINGLETON(Director);
|
||||
|
||||
|
|
|
|||
|
|
@ -29,7 +29,8 @@ namespace kiwano
|
|||
{
|
||||
class KGE_API Input
|
||||
: public Singleton<Input>
|
||||
, public Component
|
||||
, public UpdateComponent
|
||||
, public EventComponent
|
||||
{
|
||||
KGE_DECLARE_SINGLETON(Input);
|
||||
|
||||
|
|
|
|||
|
|
@ -87,7 +87,7 @@ namespace kiwano
|
|||
Renderer::GetInstance()->Init(config.render);
|
||||
|
||||
// Setup all components
|
||||
for (Component* c : components_)
|
||||
for (auto c : comps_)
|
||||
{
|
||||
c->SetupComponent();
|
||||
}
|
||||
|
|
@ -148,11 +148,11 @@ namespace kiwano
|
|||
{
|
||||
inited_ = false;
|
||||
|
||||
for (auto iter = components_.rbegin(); iter != components_.rend(); ++iter)
|
||||
for (auto iter = comps_.rbegin(); iter != comps_.rend(); ++iter)
|
||||
{
|
||||
(*iter)->DestroyComponent();
|
||||
}
|
||||
components_.clear();
|
||||
comps_.clear();
|
||||
}
|
||||
|
||||
// Destroy all instances
|
||||
|
|
@ -167,35 +167,28 @@ namespace kiwano
|
|||
// Logger::DestroyInstance();
|
||||
}
|
||||
|
||||
void Application::Use(Component* component)
|
||||
void Application::Use(ComponentBase* component)
|
||||
{
|
||||
if (component)
|
||||
{
|
||||
|
||||
#if defined(KGE_DEBUG)
|
||||
if (components_.contains(component))
|
||||
if (comps_.contains(component))
|
||||
{
|
||||
KGE_ASSERT(false && "Component already exists!");
|
||||
}
|
||||
#endif
|
||||
|
||||
components_.push_back(component);
|
||||
}
|
||||
}
|
||||
comps_.push_back(component);
|
||||
|
||||
void Application::Remove(Component* component)
|
||||
{
|
||||
if (component)
|
||||
{
|
||||
for (auto iter = components_.begin(); iter != components_.end(); ++iter)
|
||||
{
|
||||
if ((*iter) == component)
|
||||
{
|
||||
(*iter)->DestroyComponent();
|
||||
components_.erase(iter);
|
||||
break;
|
||||
}
|
||||
}
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -207,7 +200,7 @@ namespace kiwano
|
|||
void Application::Update()
|
||||
{
|
||||
// Before update
|
||||
for (Component* c : components_)
|
||||
for (auto c : update_comps_)
|
||||
{
|
||||
c->BeforeUpdate();
|
||||
}
|
||||
|
|
@ -240,14 +233,14 @@ namespace kiwano
|
|||
const auto dt = (now - last) * time_scale_;
|
||||
last = now;
|
||||
|
||||
for (Component* c : components_)
|
||||
for (auto c : update_comps_)
|
||||
{
|
||||
c->OnUpdate(dt);
|
||||
}
|
||||
}
|
||||
|
||||
// After update
|
||||
for (auto rit = components_.rbegin(); rit != components_.rend(); ++rit)
|
||||
for (auto rit = update_comps_.rbegin(); rit != update_comps_.rend(); ++rit)
|
||||
{
|
||||
(*rit)->AfterUpdate();
|
||||
}
|
||||
|
|
@ -256,20 +249,20 @@ namespace kiwano
|
|||
void Application::Render()
|
||||
{
|
||||
// Before render
|
||||
for (Component* c : components_)
|
||||
for (auto c : render_comps_)
|
||||
{
|
||||
c->BeforeRender();
|
||||
}
|
||||
|
||||
// Rendering
|
||||
Renderer* renderer = Renderer::GetInstance();
|
||||
for (Component* c : components_)
|
||||
for (auto c : render_comps_)
|
||||
{
|
||||
c->OnRender(renderer);
|
||||
}
|
||||
|
||||
// After render
|
||||
for (auto rit = components_.rbegin(); rit != components_.rend(); ++rit)
|
||||
for (auto rit = render_comps_.rbegin(); rit != render_comps_.rend(); ++rit)
|
||||
{
|
||||
(*rit)->AfterRender();
|
||||
}
|
||||
|
|
@ -277,7 +270,7 @@ namespace kiwano
|
|||
|
||||
void Application::DispatchEvent(Event& evt)
|
||||
{
|
||||
for (Component* c : components_)
|
||||
for (auto c : event_comps_)
|
||||
{
|
||||
c->HandleEvent(evt);
|
||||
}
|
||||
|
|
@ -298,7 +291,7 @@ namespace kiwano
|
|||
}
|
||||
|
||||
// Handle Message
|
||||
for (Component* c : app->components_)
|
||||
for (auto c : app->event_comps_)
|
||||
{
|
||||
c->HandleMessage(hwnd, msg, wparam, lparam);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -82,12 +82,7 @@ namespace kiwano
|
|||
|
||||
// 添加组件
|
||||
void Use(
|
||||
Component* component
|
||||
);
|
||||
|
||||
// 卸载组件
|
||||
void Remove(
|
||||
Component* component
|
||||
ComponentBase* component
|
||||
);
|
||||
|
||||
// 设置时间缩放因子
|
||||
|
|
@ -116,6 +111,9 @@ namespace kiwano
|
|||
bool inited_;
|
||||
Float32 time_scale_;
|
||||
|
||||
Vector<Component*> components_;
|
||||
Vector<ComponentBase*> comps_;
|
||||
Vector<RenderComponent*> render_comps_;
|
||||
Vector<UpdateComponent*> update_comps_;
|
||||
Vector<EventComponent*> event_comps_;
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -55,7 +55,8 @@ namespace kiwano
|
|||
// 渲染器
|
||||
class KGE_API Renderer
|
||||
: public Singleton<Renderer>
|
||||
, public Component
|
||||
, public RenderComponent
|
||||
, public EventComponent
|
||||
, public RenderTarget
|
||||
{
|
||||
KGE_DECLARE_SINGLETON(Renderer);
|
||||
|
|
|
|||
Loading…
Reference in New Issue