From 1f8b0bb058eb1091ee7508762be14013d7a782fd Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Tue, 12 Feb 2019 16:12:15 +0800 Subject: [PATCH] update box2d-sample minor minor minor refactoring Events minor --- project/Box2DSample/main.cpp | 36 +++++---- project/Samples/Demo1.h | 2 +- project/Samples/Demo2.h | 19 ++--- project/Samples/Demo3.h | 18 ++--- project/Samples/main.cpp | 6 +- src/core/ActionHelper.h | 6 ++ src/core/Application.cpp | 86 ++++++++++------------ src/core/Application.h | 16 +++- src/core/Canvas.cpp | 16 ++-- src/core/DebugNode.cpp | 12 +-- src/core/DebugNode.h | 2 +- src/core/Event.hpp | 137 +++++++++++++++++++---------------- src/core/EventDispatcher.cpp | 8 +- src/core/EventDispatcher.h | 8 +- src/core/EventListener.cpp | 17 +---- src/core/EventListener.h | 10 +-- src/core/Factory.h | 2 +- src/core/Geometry.cpp | 12 +-- src/core/GeometryNode.cpp | 6 +- src/core/Image.cpp | 4 +- src/core/Input.h | 2 +- src/core/Music.cpp | 2 +- src/core/Node.cpp | 22 +++--- src/core/RefCounter.hpp | 2 +- src/core/Singleton.hpp | 26 +++---- src/core/Sprite.cpp | 2 +- src/core/Text.cpp | 10 +-- src/core/Transition.cpp | 24 +++--- src/core/audio.cpp | 2 +- src/core/audio.h | 2 +- src/core/closure.hpp | 105 ++++++++++++--------------- src/core/noncopyable.hpp | 2 - src/core/render.cpp | 12 +-- src/core/render.h | 2 +- src/core/window.h | 2 +- src/ui/Button.cpp | 16 ++-- src/utils/Path.cpp | 4 +- 37 files changed, 324 insertions(+), 336 deletions(-) diff --git a/project/Box2DSample/main.cpp b/project/Box2DSample/main.cpp index 496ef23c..6f8f51cc 100644 --- a/project/Box2DSample/main.cpp +++ b/project/Box2DSample/main.cpp @@ -15,18 +15,19 @@ namespace { const float GLOBAL_SCALE = 100.0f; - b2Vec2 convert(const Point& pos) + b2Vec2 Vec2Convert(const Point& pos) { return b2Vec2(pos.x / GLOBAL_SCALE, pos.y / GLOBAL_SCALE); } - Point convert(const b2Vec2& pos) + Point Vec2Convert(const b2Vec2& pos) { return Point(pos.x * GLOBAL_SCALE, pos.y * GLOBAL_SCALE); } } // 小圆形 +E2D_DECLARE_SMART_PTR(Circle); class Circle : public Sprite { @@ -39,7 +40,7 @@ public: b2BodyDef bodyDef; bodyDef.type = b2_dynamicBody; - bodyDef.position = convert(pos); + bodyDef.position = Vec2Convert(pos); b2Body* body = world->CreateBody(&bodyDef); @@ -59,6 +60,7 @@ public: }; // 小方块 +E2D_DECLARE_SMART_PTR(Square); class Square : public Sprite { @@ -71,14 +73,14 @@ public: b2BodyDef bodyDef; bodyDef.type = b2_dynamicBody; - bodyDef.position = convert(pos); + bodyDef.position = Vec2Convert(pos); b2Body* body = world->CreateBody(&bodyDef); SetUserData(body); b2PolygonShape shape; - b2Vec2 sz = convert(GetSize() / 2 * 0.7f); + b2Vec2 sz = Vec2Convert(GetSize() / 2 * 0.7f); shape.SetAsBox(sz.x, sz.y); b2FixtureDef fixtureDef; @@ -92,13 +94,15 @@ public: }; // 固定的木板 +E2D_DECLARE_SMART_PTR(Board); class Board : public GeometryNode { public: Board(b2World* world, const Size& size, const Point& pos) { - SetGeometry(new RectangleGeometry(Point(), size)); + GeometryPtr geo = new RectangleGeometry(Point(), size); + SetGeometry(geo); SetStrokeColor(Color::White); SetFillColor(Color(0, 0, 0, 0)); @@ -108,13 +112,13 @@ public: SetPosition(pos); b2BodyDef groundBodyDef; - groundBodyDef.position = convert(GetPosition()); + groundBodyDef.position = Vec2Convert(GetPosition()); groundBodyDef.angle = 10 * math::constants::PI_F / 180.f; b2Body* groundBody = world->CreateBody(&groundBodyDef); b2PolygonShape groundBox; - b2Vec2 sz = convert(Point{ size.x / 2, size.y / 2 }); + b2Vec2 sz = Vec2Convert(Point{ size.x / 2, size.y / 2 }); groundBox.SetAsBox(sz.x, sz.y); groundBody->CreateFixture(&groundBox, 0.0f); } @@ -130,20 +134,20 @@ public: { // 修改场景大小, 并设置可响应状态, 使场景可以 // 接收到鼠标 Click 消息 - auto window = Window::Instance(); - SetSize(window->GetSize()); + auto size = Window::Instance().GetSize(); + SetSize(size); SetResponsible(true); // 添加消息监听 - AddListener(MouseEvent::Click, Closure(this, &MainScene::Click)); + AddListener(Event::Click, Closure(this, &MainScene::Click)); // 创建物理世界 world_ = new b2World(b2Vec2(0, 10)); - auto board = new Board(world_, Size(GetWidth() - 100, 20), Point(window->GetSize().x / 2, window->GetSize().y - 50)); + BoardPtr board = new Board(world_, Size(GetWidth() - 100, 20), Point(size.x / 2, size.y - 50)); AddChild(board); - auto circle = new Circle(world_, Point(320, 240)); + CirclePtr circle = new Circle(world_, Point(320, 240)); AddChild(circle); } @@ -167,7 +171,7 @@ public: if (node) { const b2Vec2& pos = body->GetPosition(); - node->SetPosition(convert(pos)); + node->SetPosition(Vec2Convert(pos)); node->SetRotation(body->GetAngle() * 180.f / math::constants::PI_F); // 移除掉落到场景外的物体 @@ -188,12 +192,12 @@ public: { if (evt.mouse.button == MouseButton::Left) { - auto circle = E_NEW Circle(world_, Point{ evt.mouse.x, evt.mouse.y }); + CirclePtr circle = new Circle(world_, Point{ evt.mouse.x, evt.mouse.y }); AddChild(circle); } else if (evt.mouse.button == MouseButton::Right) { - auto rect = E_NEW Square(world_, Point{ evt.mouse.x, evt.mouse.y }); + SquarePtr rect = new Square(world_, Point{ evt.mouse.x, evt.mouse.y }); AddChild(rect); } } diff --git a/project/Samples/Demo1.h b/project/Samples/Demo1.h index b25d36e2..1d49fc76 100644 --- a/project/Samples/Demo1.h +++ b/project/Samples/Demo1.h @@ -15,7 +15,7 @@ public: Demo1() { // 创建文本 - auto text = E_NEW Text(L"Hello Easy2D!"); + TextPtr text = new Text(L"Hello Easy2D!"); // 让文本显示在屏幕中央 text->SetPosition(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2); text->SetAnchor(0.5, 0.5); diff --git a/project/Samples/Demo2.h b/project/Samples/Demo2.h index 508b86a3..97b49211 100644 --- a/project/Samples/Demo2.h +++ b/project/Samples/Demo2.h @@ -3,6 +3,7 @@ #pragma once #include "common.h" +E2D_DECLARE_SMART_PTR(Man); class Man : public Sprite { @@ -19,29 +20,29 @@ public: void OnUpdate(Duration dt) override { // 获取输入设备 - auto input = Input::Instance(); + auto& input = Input::Instance(); // 按下左右键 - if (input->IsDown(KeyCode::Left)) + if (input.IsDown(KeyCode::Left)) { this->Move(-2, 0); } - else if (input->IsDown(KeyCode::Right)) + else if (input.IsDown(KeyCode::Right)) { this->Move(2, 0); } // 按下上下键 - if (input->IsDown(KeyCode::Up)) + if (input.IsDown(KeyCode::Up)) { this->Move(0, -2); } - else if (input->IsDown(KeyCode::Down)) + else if (input.IsDown(KeyCode::Down)) { this->Move(0, 2); } // 按下鼠标左键,顺时针旋转小人 - if (input->IsDown(MouseButton::Left)) + if (input.IsDown(MouseButton::Left)) { // 获取当前旋转角度 float rotation = this->GetRotation(); @@ -50,7 +51,7 @@ public: } // 点击鼠标右键,隐藏或显示小人 - if (input->WasPressed(MouseButton::Right)) + if (input.WasPressed(MouseButton::Right)) { // 获取当前显示状态 bool visible = this->IsVisible(); @@ -72,13 +73,13 @@ public: Demo2() { // 创建人物 - auto man = E_NEW Man; + ManPtr man = new Man; // 在屏幕上居中显示 man->SetAnchor(0.5f, 0.5f); man->SetPosition(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2); // 创建说明文字 - auto text = E_NEW Text(L"按上下左右键移动\n按鼠标左键旋转\n点击鼠标右键隐藏"); + TextPtr text = new Text(L"按上下左右键移动\n按鼠标左键旋转\n点击鼠标右键隐藏"); // 设置文字位置 text->SetAnchor(0.5f, 0.5f); text->SetPosition(WINDOW_WIDTH / 2, WINDOW_HEIGHT - 50); diff --git a/project/Samples/Demo3.h b/project/Samples/Demo3.h index fa74602a..d5f3b6ea 100644 --- a/project/Samples/Demo3.h +++ b/project/Samples/Demo3.h @@ -19,12 +19,12 @@ public: Demo3() { // 加载音乐 - music = E_NEW Music; + music = new Music; if (!music->Load(L"res/music.wav")) { music = nullptr; - auto err = E_NEW Text(L"音频文件加载失败"); + TextPtr err = new Text(L"音频文件加载失败"); this->AddChild(err); } @@ -32,17 +32,17 @@ public: music->Play(-1); // 创建说明文字 - auto intro_text = E_NEW Text(L"按上下键调整音量\n按空格键暂停或继续"); + TextPtr intro_text = new Text(L"按上下键调整音量\n按空格键暂停或继续"); intro_text->SetAnchor(0.5f, 0.5f); intro_text->SetPosition(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2 - 50); // 创建音量文字 - volume_text = E_NEW Text(L"当前音量:"); + volume_text = new Text(L"当前音量:"); volume_text->SetAnchor(0.5f, 0.5f); volume_text->SetPosition(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2 + 30); // 创建状态文字 - state_text = E_NEW Text(L"当前状态:"); + state_text = new Text(L"当前状态:"); state_text->SetAnchor(0.5f, 0.5f); state_text->SetPosition(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2 + 60); @@ -66,20 +66,20 @@ public: state_text->SetText(playing ? L"当前状态:正在播放" : L"当前状态:停止播放"); // 获取输入设备 - auto input = Input::Instance(); + auto& input = Input::Instance(); // 按空格键暂停或继续 - if (input->WasPressed(KeyCode::Space)) + if (input.WasPressed(KeyCode::Space)) { music->IsPlaying() ? music->Pause() : music->Resume(); } // 按上下键调整音量 - if (input->WasPressed(KeyCode::Up)) + if (input.WasPressed(KeyCode::Up)) { music->SetVolume(volume + 0.1f); } - else if (input->WasPressed(KeyCode::Down)) + else if (input.WasPressed(KeyCode::Down)) { music->SetVolume(volume - 0.1f); } diff --git a/project/Samples/main.cpp b/project/Samples/main.cpp index d0f1e9ee..39a7e608 100644 --- a/project/Samples/main.cpp +++ b/project/Samples/main.cpp @@ -40,13 +40,13 @@ public: void ChangeDemoScene() { - Window::Instance()->SetTitle(s_Demos[s_DemoIndex].title); + Window::Instance().SetTitle(s_Demos[s_DemoIndex].title); ScenePtr scene = s_Demos[s_DemoIndex].Create(); EnterScene(scene); // 添加按键监听 - scene->AddListener(KeyboardEvent::Up, Closure(this, &DemoApp::KeyPressed)); + scene->AddListener(Event::KeyUp, Closure(this, &DemoApp::KeyPressed)); // 显示提示文字 TextPtr intro = new Text(L"Key 1~3 to select demo"); @@ -56,7 +56,7 @@ public: void KeyPressed(Event const& evt) { - E2D_ASSERT(evt.type == KeyboardEvent::Up); + E2D_ASSERT(evt.type == Event::KeyUp); if (evt.key.code > KeyCode::Num0 && evt.key.code <= (KeyCode::Num0 + s_DemoNum)) diff --git a/src/core/ActionHelper.h b/src/core/ActionHelper.h index 91285da9..5c7eacc1 100644 --- a/src/core/ActionHelper.h +++ b/src/core/ActionHelper.h @@ -42,6 +42,9 @@ namespace easy2d // 动作结束时移除目标节点 inline ActionHelper& RemoveTargetWhenDone() { base->RemoveTargetWhenDone(); return (*this); } + // 设置名称 + inline ActionHelper& SetName(String const& name) { base->SetName(name); return (*this); } + // 获取指针 inline ActionPtr const& Get() const { return base; } @@ -76,6 +79,9 @@ namespace easy2d // 动作结束时移除目标节点 inline TweenHelper& RemoveTargetWhenDone() { base->RemoveTargetWhenDone(); return (*this); } + // 设置名称 + inline TweenHelper& SetName(String const& name) { base->SetName(name); return (*this); } + // 获取指针 inline ActionTweenPtr const& Get() const { return base; } diff --git a/src/core/Application.cpp b/src/core/Application.cpp index 38093906..b22d2742 100644 --- a/src/core/Application.cpp +++ b/src/core/Application.cpp @@ -34,16 +34,6 @@ namespace easy2d { - Options::Options() - : title(L"Easy2D Game") - , width(640) - , height(480) - , icon(nullptr) - , vsync(true) - , fullscreen(false) - , debug(false) - {} - Application::Application(String const& app_name) : end_(true) , inited_(false) @@ -101,11 +91,11 @@ namespace easy2d } ThrowIfFailed( - Factory::Instance()->Init(debug_) + Factory::Instance().Init(debug_) ); ThrowIfFailed( - Window::Instance()->Init( + Window::Instance().Init( options.title, options.width, options.height, @@ -116,10 +106,10 @@ namespace easy2d ) ); - HWND hwnd = Window::Instance()->GetHandle(); + HWND hwnd = Window::Instance().GetHandle(); ThrowIfFailed( - RenderSystem::Instance()->Init( + RenderSystem::Instance().Init( hwnd, options.vsync, debug_ @@ -127,14 +117,14 @@ namespace easy2d ); ThrowIfFailed( - Input::Instance()->Init( + Input::Instance().Init( hwnd, debug_ ) ); ThrowIfFailed( - Audio::Instance()->Init(debug_) + Audio::Instance().Init(debug_) ); OnStart(); @@ -150,13 +140,13 @@ namespace easy2d void Application::Run() { - HWND hwnd = Window::Instance()->GetHandle(); + HWND hwnd = Window::Instance().GetHandle(); if (hwnd) { end_ = false; - Window::Instance()->Prepare(); + Window::Instance().Prepare(); MSG msg = {}; while (::GetMessageW(&msg, nullptr, 0, 0) && !end_) @@ -182,10 +172,10 @@ namespace easy2d next_scene_.Reset(); curr_scene_.Reset(); - Audio::Instance()->Destroy(); - RenderSystem::Instance()->Destroy(); - Window::Instance()->Destroy(); - Factory::Instance()->Destroy(); + Audio::Instance().Destroy(); + RenderSystem::Instance().Destroy(); + Window::Instance().Destroy(); + Factory::Instance().Destroy(); } } @@ -219,9 +209,9 @@ namespace easy2d return curr_scene_; } - void Application::SetTimeScale(float scale) + void Application::SetTimeScale(float scale_factor) { - time_scale_ = scale; + time_scale_ = scale_factor; } void Application::Update() @@ -262,15 +252,15 @@ namespace easy2d next_scene_->Update(dt); if (debug_) - DebugNode::Instance()->Update(dt); + DebugNode::Instance().Update(dt); } void Application::Render(HWND hwnd) { - auto rt = RenderSystem::Instance(); + auto& rt = RenderSystem::Instance(); ThrowIfFailed( - rt->BeginDraw(hwnd) + rt.BeginDraw(hwnd) ); if (transition_) @@ -283,10 +273,10 @@ namespace easy2d } if (debug_) - DebugNode::Instance()->Render(); + DebugNode::Instance().Render(); ThrowIfFailed( - rt->EndDraw() + rt.EndDraw() ); ::InvalidateRect(hwnd, NULL, FALSE); @@ -308,7 +298,7 @@ namespace easy2d app->Update(); app->Render(hwnd); - Input::Instance()->Update(); + Input::Instance().Update(); return 0; } break; @@ -316,11 +306,11 @@ namespace easy2d case WM_KEYDOWN: case WM_KEYUP: { - Input::Instance()->UpdateKey((int)wparam, (msg == WM_KEYDOWN) ? true : false); + Input::Instance().UpdateKey((int)wparam, (msg == WM_KEYDOWN) ? true : false); if (!app->transition_ && app->curr_scene_) { - Event evt((msg == WM_KEYDOWN) ? KeyboardEvent::Down : KeyboardEvent::Up); + Event evt((msg == WM_KEYDOWN) ? Event::KeyDown : Event::KeyUp); evt.key.code = static_cast(wparam); evt.key.count = static_cast(lparam & 0xFF); @@ -341,9 +331,9 @@ namespace easy2d case WM_MOUSEMOVE: case WM_MOUSEWHEEL: { - if (msg == WM_LBUTTONDOWN || msg == WM_LBUTTONUP) { Input::Instance()->UpdateKey(VK_LBUTTON, (msg == WM_LBUTTONDOWN) ? true : false); } - else if (msg == WM_RBUTTONDOWN || msg == WM_RBUTTONUP) { Input::Instance()->UpdateKey(VK_RBUTTON, (msg == WM_RBUTTONDOWN) ? true : false); } - else if (msg == WM_MBUTTONDOWN || msg == WM_MBUTTONUP) { Input::Instance()->UpdateKey(VK_MBUTTON, (msg == WM_MBUTTONDOWN) ? true : false); } + if (msg == WM_LBUTTONDOWN || msg == WM_LBUTTONUP) { Input::Instance().UpdateKey(VK_LBUTTON, (msg == WM_LBUTTONDOWN) ? true : false); } + else if (msg == WM_RBUTTONDOWN || msg == WM_RBUTTONUP) { Input::Instance().UpdateKey(VK_RBUTTON, (msg == WM_RBUTTONDOWN) ? true : false); } + else if (msg == WM_MBUTTONDOWN || msg == WM_MBUTTONUP) { Input::Instance().UpdateKey(VK_MBUTTON, (msg == WM_MBUTTONDOWN) ? true : false); } if (!app->transition_ && app->curr_scene_) { @@ -354,10 +344,10 @@ namespace easy2d evt.mouse.left_btn_down = !!(wparam & MK_LBUTTON); evt.mouse.left_btn_down = !!(wparam & MK_RBUTTON); - if (msg == WM_MOUSEMOVE) { evt.type = MouseEvent::Move; } - else if (msg == WM_LBUTTONDOWN || msg == WM_RBUTTONDOWN || msg == WM_MBUTTONDOWN) { evt.type = MouseEvent::Down; } - else if (msg == WM_LBUTTONUP || msg == WM_RBUTTONUP || msg == WM_MBUTTONUP) { evt.type = MouseEvent::Up; } - else if (msg == WM_MOUSEWHEEL) { evt.type = MouseEvent::Wheel; 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; } @@ -376,7 +366,7 @@ namespace easy2d // 如果程序接收到一个 WM_SIZE 消息,这个方法将调整渲染 // 目标的大小。它可能会调用失败,但是这里可以忽略有可能的 // 错误,因为这个错误将在下一次调用 EndDraw 时产生 - RenderSystem::Instance()->Resize(width, height); + RenderSystem::Instance().Resize(width, height); if (SIZE_MAXHIDE == wparam || SIZE_MINIMIZED == wparam) { @@ -388,13 +378,13 @@ namespace easy2d if (app->curr_scene_) { - Event evt(WindowEvent::Resized); + Event evt(Event::WindowResized); evt.win.width = static_cast(width); evt.win.height = static_cast(height); app->curr_scene_->Dispatch(evt); } - Window::Instance()->UpdateWindowRect(); + Window::Instance().UpdateWindowRect(); } } break; @@ -406,7 +396,7 @@ namespace easy2d int x = (int)(short)LOWORD(lparam); int y = (int)(short)HIWORD(lparam); - Event evt(WindowEvent::Moved); + Event evt(Event::WindowMoved); evt.win.x = x; evt.win.y = y; app->curr_scene_->Dispatch(evt); @@ -420,11 +410,11 @@ namespace easy2d E2D_LOG(active ? L"Window activated" : L"Window deactivated"); - Window::Instance()->SetActive(active); + Window::Instance().SetActive(active); if (app->curr_scene_) { - Event evt(WindowEvent::FocusChanged); + Event evt(Event::WindowFocusChanged); evt.win.focus = active; app->curr_scene_->Dispatch(evt); } @@ -437,7 +427,7 @@ namespace easy2d if (app->curr_scene_) { - Event evt(WindowEvent::TitleChanged); + Event evt(Event::WindowTitleChanged); evt.win.title = reinterpret_cast(lparam); app->curr_scene_->Dispatch(evt); } @@ -464,7 +454,7 @@ namespace easy2d if (app->OnClosing()) { - Window::Instance()->Destroy(); + Window::Instance().Destroy(); } return 0; } @@ -476,7 +466,7 @@ namespace easy2d if (app->curr_scene_) { - Event evt(WindowEvent::Closed); + Event evt(Event::WindowClosed); app->curr_scene_->Dispatch(evt); } diff --git a/src/core/Application.h b/src/core/Application.h index 20ad57c7..9ee1e04a 100644 --- a/src/core/Application.h +++ b/src/core/Application.h @@ -38,7 +38,15 @@ namespace easy2d bool fullscreen; // 全屏模式 bool debug; // 调试模式 - E2D_API Options(); + Options() + : title(L"Easy2D Game") + , width(640) + , height(480) + , icon(nullptr) + , vsync(true) + , fullscreen(false) + , debug(false) + {} }; @@ -92,8 +100,10 @@ namespace easy2d // 获取当前场景 ScenePtr const& GetCurrentScene(); - // 设置变速 - void SetTimeScale(float scale); + // 设置时间缩放因子 + void SetTimeScale( + float scale_factor + ); private: void Render(HWND); diff --git a/src/core/Canvas.cpp b/src/core/Canvas.cpp index 68e25302..1bfc4573 100644 --- a/src/core/Canvas.cpp +++ b/src/core/Canvas.cpp @@ -32,7 +32,7 @@ namespace easy2d , stroke_width_(1.0f) { ThrowIfFailed( - RenderSystem::Instance()->CreateBitmapRenderTarget(render_target_) + RenderSystem::Instance().CreateBitmapRenderTarget(render_target_) ); auto properties = D2D1::BrushProperties(); @@ -58,7 +58,7 @@ namespace easy2d ); ThrowIfFailed( - Factory::Instance()->CreateTextRenderer( + Factory::Instance().CreateTextRenderer( text_renderer_, render_target_, text_brush_ @@ -105,7 +105,7 @@ namespace easy2d if (bitmap_cached_) { - RenderSystem::Instance()->DrawBitmap(bitmap_cached_); + RenderSystem::Instance().DrawBitmap(bitmap_cached_); } } @@ -126,7 +126,7 @@ namespace easy2d void Canvas::SetOutlineJoinStyle(StrokeStyle outline_join) { - outline_join_style_ = Factory::Instance()->GetStrokeStyle(outline_join); + outline_join_style_ = Factory::Instance().GetStrokeStyle(outline_join); } void Canvas::SetTextStyle(Font const& font, TextStyle const & text_style) @@ -139,7 +139,7 @@ namespace easy2d text_style_.outline, ToD2dColorF(text_style_.outline_color), text_style_.outline_width, - Factory::Instance()->GetStrokeStyle(text_style_.outline_stroke).Get() + Factory::Instance().GetStrokeStyle(text_style_.outline_stroke).Get() ); } @@ -271,7 +271,7 @@ namespace easy2d D2DTextFormatPtr text_format; ThrowIfFailed( - Factory::Instance()->CreateTextFormat( + Factory::Instance().CreateTextFormat( text_format, text_font_, text_style_ @@ -281,7 +281,7 @@ namespace easy2d D2DTextLayoutPtr text_layout; Size layout_size; ThrowIfFailed( - Factory::Instance()->CreateTextLayout( + Factory::Instance().CreateTextLayout( text_layout, layout_size, text, @@ -390,7 +390,7 @@ namespace easy2d current_geometry_ = nullptr; ThrowIfFailed( - Factory::Instance()->CreatePathGeometry(current_geometry_) + Factory::Instance().CreatePathGeometry(current_geometry_) ); ThrowIfFailed( diff --git a/src/core/DebugNode.cpp b/src/core/DebugNode.cpp index c4e0110d..31a54911 100644 --- a/src/core/DebugNode.cpp +++ b/src/core/DebugNode.cpp @@ -67,16 +67,16 @@ namespace easy2d void DebugNode::OnRender() { - auto rt = RenderSystem::Instance(); + auto& rt = RenderSystem::Instance(); - rt->GetSolidBrush()->SetColor(D2D1::ColorF(0.0f, 0.0f, 0.0f, 0.5f)); + rt.GetSolidBrush()->SetColor(D2D1::ColorF(0.0f, 0.0f, 0.0f, 0.5f)); - rt->GetRenderTarget()->FillRoundedRectangle( + rt.GetRenderTarget()->FillRoundedRectangle( D2D1::RoundedRect( D2D1_RECT_F{ 10, 10, 200, 120 }, 6.f, 6.f), - rt->GetSolidBrush().Get() + rt.GetSolidBrush().Get() ); } @@ -105,9 +105,9 @@ namespace easy2d ss << "Objects: " << Object::__GetTracingObjects().size() << std::endl; #endif - ss << "Render: " << RenderSystem::Instance()->GetStatus().duration.Milliseconds() << "ms" << std::endl; + ss << "Render: " << RenderSystem::Instance().GetStatus().duration.Milliseconds() << "ms" << std::endl; - ss << "Primitives / sec: " << RenderSystem::Instance()->GetStatus().primitives * frame_time_.size() << std::endl; + ss << "Primitives / sec: " << RenderSystem::Instance().GetStatus().primitives * frame_time_.size() << std::endl; PROCESS_MEMORY_COUNTERS_EX pmc; GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS*)&pmc, sizeof(pmc)); diff --git a/src/core/DebugNode.h b/src/core/DebugNode.h index fcf2652a..0cdd04e4 100644 --- a/src/core/DebugNode.h +++ b/src/core/DebugNode.h @@ -27,7 +27,7 @@ namespace easy2d { class E2D_API DebugNode : public Node - , public ISingleton + , public Singleton { E2D_DECLARE_SINGLETON(DebugNode); diff --git a/src/core/Event.hpp b/src/core/Event.hpp index 1fd33fa8..75ed5910 100644 --- a/src/core/Event.hpp +++ b/src/core/Event.hpp @@ -24,122 +24,111 @@ namespace easy2d { - typedef UINT EventType; // 鼠标事件 struct MouseEvent { - enum Type : EventType - { - First = WM_MOUSEFIRST, - - Move, // 移动 - Down, // 按下 - Up, // 抬起 - Wheel, // 滚轮滚动 - - Hover, // 鼠标移入 - Out, // 鼠标移出 - Click, // 鼠标点击 - - Last // 结束标志 - }; - float x; float y; bool left_btn_down; // 左键是否按下 bool right_btn_down; // 右键是否按下 - struct + struct // Events::MouseDown | Events::MouseUp | Events::MouseClick { - int button; // 仅当消息类型为 Down | Up | Click 时有效 + int button; }; - struct + struct // Events::MouseWheel { - float wheel; // 仅当消息类型为 Wheel 时有效 + float wheel; }; - static inline bool Check(EventType type) - { - return type > Type::First && type < Type::Last; - } + static bool Check(UINT type); }; // 键盘事件 struct KeyboardEvent { - enum Type : UINT - { - First = WM_KEYFIRST, - - Down, // 键按下 - Up, // 键抬起 - - Last - }; - - int code; // enum KeyCode + int code; // enum KeyCode int count; - static inline bool Check(UINT type) - { - return type > Type::First && type < Type::Last; - } + static bool Check(UINT type); }; // 窗口事件 struct WindowEvent { - public: - enum Type : EventType - { - First = WM_NULL, - - Moved, // 窗口移动 - Resized, // 窗口大小变化 - FocusChanged, // 获得或失去焦点 - TitleChanged, // 标题变化 - Closed, // 窗口被关闭 - - Last - }; - union { - struct // WindowEvent::Moved + struct // Events::WindowMoved { int x; int y; }; - struct // WindowEvent::Resized + struct // Events::WindowResized { int width; int height; }; - struct // WindowEvent::FocusChanged + struct // Events::WindowFocusChanged { bool focus; }; - struct // WindowEvent::TitleChanged + struct // Events::WindowTitleChanged { const wchar_t* title; }; }; - static inline bool Check(EventType type) - { - return type > Type::First && type < Type::Last; - } + static bool Check(UINT type); + }; + + // 自定义事件 + struct CustomEvent + { + void* data; }; // 事件 struct E2D_API Event { - EventType type; + enum Type : UINT + { + First, + + // 鼠标事件 + MouseFirst, + MouseMove, // 移动 + MouseBtnDown, // 鼠标按下 + MouseBtnUp, // 鼠标抬起 + MouseWheel, // 滚轮滚动 + MouseHover, // 鼠标移入 + MouseOut, // 鼠标移出 + Click, // 鼠标点击 + MouseLast, + + // 按键事件 + KeyFirst, + KeyDown, // 按键按下 + KeyUp, // 按键抬起 + KeyLast, + + // 窗口消息 + WindowFirst, + WindowMoved, // 窗口移动 + WindowResized, // 窗口大小变化 + WindowFocusChanged, // 获得或失去焦点 + WindowTitleChanged, // 标题变化 + WindowClosed, // 窗口被关闭 + WindowLast, + + Last + }; + + UINT type; Node* target; union @@ -147,8 +136,28 @@ namespace easy2d MouseEvent mouse; KeyboardEvent key; WindowEvent win; + CustomEvent custom; }; - Event(EventType type = 0) : type(type), target(nullptr) {} + Event(UINT type = Type::First) : type(type), target(nullptr) {} }; + + + // Check-functions + + inline bool MouseEvent::Check(UINT type) + { + return type > Event::MouseFirst && type < Event::MouseLast; + } + + inline bool KeyboardEvent::Check(UINT type) + { + return type > Event::KeyFirst && type < Event::KeyLast; + } + + inline bool WindowEvent::Check(UINT type) + { + return type > Event::WindowFirst && type < Event::WindowLast; + } + } diff --git a/src/core/EventDispatcher.cpp b/src/core/EventDispatcher.cpp index 1d2edb86..bf53dfde 100644 --- a/src/core/EventDispatcher.cpp +++ b/src/core/EventDispatcher.cpp @@ -50,7 +50,7 @@ namespace easy2d } } - void EventDispatcher::AddListener(EventType type, EventCallback callback, String const& name) + void EventDispatcher::AddListener(UINT type, EventCallback callback, String const& name) { EventListenerPtr listener = new EventListener(type, callback, name); if (listener) @@ -95,7 +95,7 @@ namespace easy2d } } - void EventDispatcher::StartListeners(EventType type) + void EventDispatcher::StartListeners(UINT type) { for (auto listener = listeners_.First(); listener; listener = listener->NextItem()) { @@ -106,7 +106,7 @@ namespace easy2d } } - void EventDispatcher::StopListeners(EventType type) + void EventDispatcher::StopListeners(UINT type) { for (auto listener = listeners_.First(); listener; listener = listener->NextItem()) { @@ -117,7 +117,7 @@ namespace easy2d } } - void EventDispatcher::RemoveListeners(EventType type) + void EventDispatcher::RemoveListeners(UINT type) { EventListenerPtr next; for (auto listener = listeners_.First(); listener; listener = next) diff --git a/src/core/EventDispatcher.h b/src/core/EventDispatcher.h index 332a03c9..6f882fcc 100644 --- a/src/core/EventDispatcher.h +++ b/src/core/EventDispatcher.h @@ -35,7 +35,7 @@ namespace easy2d // 添加监听器 void AddListener( - EventType type, + UINT type, EventCallback callback, String const& name = L"" ); @@ -57,17 +57,17 @@ namespace easy2d // 启动监听器 void StartListeners( - EventType type + UINT type ); // 停止监听器 void StopListeners( - EventType type + UINT type ); // 移除监听器 void RemoveListeners( - EventType type + UINT type ); virtual void Dispatch(Event& evt); diff --git a/src/core/EventListener.cpp b/src/core/EventListener.cpp index b1f74013..e26073d5 100644 --- a/src/core/EventListener.cpp +++ b/src/core/EventListener.cpp @@ -23,7 +23,7 @@ namespace easy2d { - EventListener::EventListener(EventType type, EventCallback const & callback, String const & name) + EventListener::EventListener(UINT type, EventCallback const & callback, String const & name) : type_(type) , callback_(callback) , running_(true) @@ -35,19 +35,4 @@ namespace easy2d { } - void EventListener::Start() - { - running_ = true; - } - - void EventListener::Stop() - { - running_ = false; - } - - bool EventListener::IsRunning() const - { - return running_; - } - } diff --git a/src/core/EventListener.h b/src/core/EventListener.h index 820cc310..62ddb0bb 100644 --- a/src/core/EventListener.h +++ b/src/core/EventListener.h @@ -38,22 +38,22 @@ namespace easy2d public: EventListener( - EventType type, + UINT type, EventCallback const& callback, String const& name = L"" ); virtual ~EventListener(); - void Start(); + inline void Start() { running_ = true; } - void Stop(); + inline void Stop() { running_ = true; } - bool IsRunning() const; + inline bool IsRunning() const { return running_; } protected: bool running_; - EventType type_; + UINT type_; EventCallback callback_; }; } diff --git a/src/core/Factory.h b/src/core/Factory.h index dce7e937..9c47f483 100644 --- a/src/core/Factory.h +++ b/src/core/Factory.h @@ -29,7 +29,7 @@ namespace easy2d { class E2D_API Factory - : public ISingleton + : public Singleton { E2D_DECLARE_SINGLETON(Factory); diff --git a/src/core/Geometry.cpp b/src/core/Geometry.cpp index 4a8d839c..5e6dd997 100644 --- a/src/core/Geometry.cpp +++ b/src/core/Geometry.cpp @@ -129,7 +129,7 @@ namespace easy2d D2DPathGeometryPtr path_geo; D2DGeometrySinkPtr path_sink; - HRESULT hr = Factory::Instance()->CreatePathGeometry(path_geo); + HRESULT hr = Factory::Instance().CreatePathGeometry(path_geo); if (SUCCEEDED(hr)) { @@ -186,7 +186,7 @@ namespace easy2d void RectangleGeometry::SetRect(Rect const & rect) { D2DRectangleGeometryPtr geo; - if (SUCCEEDED(Factory::Instance()->CreateRectangleGeometry(geo, rect))) + if (SUCCEEDED(Factory::Instance().CreateRectangleGeometry(geo, rect))) { geo_ = geo; rect_ = rect; @@ -225,7 +225,7 @@ namespace easy2d void CircleGeometry::SetCircle(Point const & center, float radius) { D2DEllipseGeometryPtr geo; - if (SUCCEEDED(Factory::Instance()->CreateEllipseGeometry(geo, center, radius, radius))) + if (SUCCEEDED(Factory::Instance().CreateEllipseGeometry(geo, center, radius, radius))) { geo_ = geo; center_ = center; @@ -266,7 +266,7 @@ namespace easy2d void EllipseGeometry::SetEllipse(Point const & center, float radius_x, float radius_y) { D2DEllipseGeometryPtr geo; - if (SUCCEEDED(Factory::Instance()->CreateEllipseGeometry(geo, center, radius_x, radius_y))) + if (SUCCEEDED(Factory::Instance().CreateEllipseGeometry(geo, center, radius_x, radius_y))) { geo_ = geo; radius_x_ = radius_x; @@ -292,7 +292,7 @@ namespace easy2d current_geometry_ = nullptr; ThrowIfFailed( - Factory::Instance()->CreatePathGeometry(current_geometry_) + Factory::Instance().CreatePathGeometry(current_geometry_) ); ThrowIfFailed( @@ -405,7 +405,7 @@ namespace easy2d void RoundedRectGeometry::SetRoundedRect(Rect const & rect, float radius_x, float radius_y) { D2DRoundedRectangleGeometryPtr geo; - if (SUCCEEDED(Factory::Instance()->CreateRoundedRectangleGeometry(geo, rect, radius_x, radius_y))) + if (SUCCEEDED(Factory::Instance().CreateRoundedRectangleGeometry(geo, rect, radius_x, radius_y))) { geo_ = geo; rect_ = rect; diff --git a/src/core/GeometryNode.cpp b/src/core/GeometryNode.cpp index 5ba5cbc0..54cbcc69 100644 --- a/src/core/GeometryNode.cpp +++ b/src/core/GeometryNode.cpp @@ -70,14 +70,14 @@ namespace easy2d { if (geometry_ && geometry_->geo_) { - auto rt = RenderSystem::Instance(); + auto& rt = RenderSystem::Instance(); - rt->FillGeometry( + rt.FillGeometry( geometry_->geo_, fill_color_ ); - rt->DrawGeometry( + rt.DrawGeometry( geometry_->geo_, stroke_color_, stroke_width_, diff --git a/src/core/Image.cpp b/src/core/Image.cpp index f15d7afa..e4041442 100644 --- a/src/core/Image.cpp +++ b/src/core/Image.cpp @@ -67,11 +67,11 @@ namespace easy2d E2D_WARNING_LOG(L"Image file '%s' not found!", res.GetFileName().c_str()); return false; } - hr = RenderSystem::Instance()->CreateBitmapFromFile(bitmap, res.GetFileName()); + hr = RenderSystem::Instance().CreateBitmapFromFile(bitmap, res.GetFileName()); } else { - hr = RenderSystem::Instance()->CreateBitmapFromResource(bitmap, res); + hr = RenderSystem::Instance().CreateBitmapFromResource(bitmap, res); } if (FAILED(hr)) diff --git a/src/core/Input.h b/src/core/Input.h index 371e82b2..97ff6ace 100644 --- a/src/core/Input.h +++ b/src/core/Input.h @@ -26,7 +26,7 @@ namespace easy2d { class E2D_API Input - : public ISingleton + : public Singleton { E2D_DECLARE_SINGLETON(Input); diff --git a/src/core/Music.cpp b/src/core/Music.cpp index c54b24e9..8b8f47d3 100644 --- a/src/core/Music.cpp +++ b/src/core/Music.cpp @@ -78,7 +78,7 @@ namespace easy2d return false; } - hr = Audio::Instance()->CreateVoice(voice_, transcoder.GetWaveFormatEx()); + hr = Audio::Instance().CreateVoice(voice_, transcoder.GetWaveFormatEx()); if (FAILED(hr)) { if (wave_data_) diff --git a/src/core/Node.cpp b/src/core/Node.cpp index 0991cd6a..32e0ac25 100644 --- a/src/core/Node.cpp +++ b/src/core/Node.cpp @@ -88,12 +88,12 @@ namespace easy2d UpdateTransform(); - auto rt = RenderSystem::Instance(); + auto& rt = RenderSystem::Instance(); if (children_.IsEmpty()) { - rt->SetTransform(transform_matrix_); - rt->SetOpacity(display_opacity_); + rt.SetTransform(transform_matrix_); + rt.SetOpacity(display_opacity_); OnRender(); } @@ -110,8 +110,8 @@ namespace easy2d child = child->NextItem().Get(); } - rt->SetTransform(transform_matrix_); - rt->SetOpacity(display_opacity_); + rt.SetTransform(transform_matrix_); + rt.SetOpacity(display_opacity_); OnRender(); @@ -137,7 +137,7 @@ namespace easy2d if (responsible_ && MouseEvent::Check(evt.type)) { - if (evt.type == MouseEvent::Move) + if (evt.type == Event::MouseMove) { if (!evt.target && ContainsPoint(Point{ evt.mouse.x, evt.mouse.y })) { @@ -148,7 +148,7 @@ namespace easy2d hover_ = true; Event hover = evt; - hover.type = MouseEvent::Hover; + hover.type = Event::MouseHover; EventDispatcher::Dispatch(hover); } } @@ -159,24 +159,24 @@ namespace easy2d Event out = evt; out.target = this; - out.type = MouseEvent::Out; + out.type = Event::MouseOut; EventDispatcher::Dispatch(out); } } - if (evt.type == MouseEvent::Down && hover_) + if (evt.type == Event::MouseBtnDown && hover_) { pressed_ = true; evt.target = this; } - if (evt.type == MouseEvent::Up && pressed_) + if (evt.type == Event::MouseBtnUp && pressed_) { pressed_ = false; evt.target = this; Event click = evt; - click.type = MouseEvent::Click; + click.type = Event::Click; EventDispatcher::Dispatch(click); } } diff --git a/src/core/RefCounter.hpp b/src/core/RefCounter.hpp index 7c08c578..df6992de 100644 --- a/src/core/RefCounter.hpp +++ b/src/core/RefCounter.hpp @@ -44,7 +44,7 @@ namespace easy2d protected: RefCounter() : ref_count_(0) {} - ~RefCounter() {} + virtual ~RefCounter() {} protected: long ref_count_; diff --git a/src/core/Singleton.hpp b/src/core/Singleton.hpp index ba34f3c1..c7f4b099 100644 --- a/src/core/Singleton.hpp +++ b/src/core/Singleton.hpp @@ -19,28 +19,26 @@ // THE SOFTWARE. #pragma once -#include "noncopyable.hpp" -#include namespace easy2d { - template - class ISingleton - : protected Noncopyable + template + class Singleton { public: - static inline T* Instance() + static inline _Ty& Instance() { - static std::unique_ptr instance_; - if (!instance_) - instance_.reset(new (std::nothrow) T); - return instance_.get(); + static _Ty instance; // Thread safe + return instance; } protected: - ISingleton() = default; + Singleton() = default; - ~ISingleton() {} + private: + Singleton(const Singleton&) = delete; + + Singleton& operator=(const Singleton&) = delete; }; } @@ -49,7 +47,5 @@ namespace easy2d #ifndef E2D_DECLARE_SINGLETON #define E2D_DECLARE_SINGLETON( type ) \ - friend class ::std::unique_ptr< type >; \ - friend struct ::std::default_delete< type >;\ - friend class ::easy2d::ISingleton< type > + friend class ::easy2d::Singleton< type > #endif diff --git a/src/core/Sprite.cpp b/src/core/Sprite.cpp index 529289be..5ac2e9d3 100644 --- a/src/core/Sprite.cpp +++ b/src/core/Sprite.cpp @@ -99,7 +99,7 @@ namespace easy2d { if (image_) { - RenderSystem::Instance()->DrawImage(image_, GetBounds()); + RenderSystem::Instance().DrawImage(image_, GetBounds()); } } } \ No newline at end of file diff --git a/src/core/Text.cpp b/src/core/Text.cpp index 43671a01..9039ade5 100644 --- a/src/core/Text.cpp +++ b/src/core/Text.cpp @@ -295,15 +295,15 @@ namespace easy2d { if (text_layout_) { - auto rt = RenderSystem::Instance(); - rt->SetTextStyle( + auto& rt = RenderSystem::Instance(); + rt.SetTextStyle( style_.color, style_.outline, style_.outline_color, style_.outline_width, style_.outline_stroke ); - rt->DrawTextLayout(text_layout_); + rt.DrawTextLayout(text_layout_); } } @@ -316,7 +316,7 @@ namespace easy2d return; ThrowIfFailed( - Factory::Instance()->CreateTextFormat( + Factory::Instance().CreateTextFormat( text_format_, font_, style_ @@ -325,7 +325,7 @@ namespace easy2d Size layout_size; ThrowIfFailed( - Factory::Instance()->CreateTextLayout( + Factory::Instance().CreateTextLayout( text_layout_, layout_size, text_, diff --git a/src/core/Transition.cpp b/src/core/Transition.cpp index b0be77fc..447c8917 100644 --- a/src/core/Transition.cpp +++ b/src/core/Transition.cpp @@ -66,18 +66,18 @@ namespace easy2d if (in_scene_) { ThrowIfFailed( - RenderSystem::Instance()->CreateLayer(in_layer_) + RenderSystem::Instance().CreateLayer(in_layer_) ); } if (out_scene_) { ThrowIfFailed( - RenderSystem::Instance()->CreateLayer(out_layer_) + RenderSystem::Instance().CreateLayer(out_layer_) ); } - window_size_ = Window::Instance()->GetSize(); + window_size_ = Window::Instance().GetSize(); out_layer_prop_ = in_layer_prop_ = LayerProperties{ Rect(Point(), window_size_),1.f }; } @@ -101,34 +101,34 @@ namespace easy2d void Transition::Render() { - auto rt = RenderSystem::Instance(); + auto& rt = RenderSystem::Instance(); if (out_scene_) { - rt->PushClip( + rt.PushClip( out_scene_->GetTransformMatrix(), window_size_ ); - rt->PushLayer(out_layer_, out_layer_prop_); + rt.PushLayer(out_layer_, out_layer_prop_); out_scene_->Render(); - rt->PopLayer(); - rt->PopClip(); + rt.PopLayer(); + rt.PopClip(); } if (in_scene_) { - rt->PushClip( + rt.PushClip( in_scene_->GetTransformMatrix(), window_size_ ); - rt->PushLayer(in_layer_, in_layer_prop_); + rt.PushLayer(in_layer_, in_layer_prop_); in_scene_->Render(); - rt->PopLayer(); - rt->PopClip(); + rt.PopLayer(); + rt.PopClip(); } } diff --git a/src/core/audio.cpp b/src/core/audio.cpp index 6c2519fd..cdbd92da 100644 --- a/src/core/audio.cpp +++ b/src/core/audio.cpp @@ -46,7 +46,7 @@ namespace easy2d { Destroy(); - Audio::Instance()->DeleteVoice(this); + Audio::Instance().DeleteVoice(this); } HRESULT Voice::Play(const BYTE * wave_data, UINT32 data_size, UINT32 loop_count) diff --git a/src/core/audio.h b/src/core/audio.h index 584ff465..bd7660e9 100644 --- a/src/core/audio.h +++ b/src/core/audio.h @@ -75,7 +75,7 @@ namespace easy2d class E2D_API Audio - : public ISingleton