From 4a71273b35cdfc1dc19b3ba59e40da9bcdb7eebf Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Mon, 21 Jan 2019 21:24:45 +0800 Subject: [PATCH] min minor refactoring: Resource --- core/base/ActionCombined.cpp | 8 +-- core/base/ActionCombined.h | 16 ++--- core/base/Canvas.cpp | 6 +- core/base/Canvas.h | 4 +- core/base/DebugNode.cpp | 2 +- core/base/DebugNode.h | 8 +-- core/base/EventDispatcher.cpp | 8 +-- core/base/EventDispatcher.h | 8 +-- core/base/EventListener.cpp | 6 +- core/base/EventListener.h | 8 +-- core/base/Factory.cpp | 13 ++-- core/base/Factory.h | 4 +- core/base/Font.hpp | 10 ++-- core/base/Frames.cpp | 8 +-- core/base/Frames.h | 18 +++--- core/base/Game.cpp | 88 ++++++++++++---------------- core/base/Game.h | 19 ++---- core/base/Geometry.cpp | 4 +- core/base/Geometry.h | 2 +- core/base/Image.cpp | 62 ++++++++------------ core/base/Image.h | 14 ----- core/base/Music.cpp | 81 +++++++------------------ core/base/Music.h | 9 --- core/base/Node.cpp | 20 +++---- core/base/Node.h | 43 +++++++------- core/base/Object.cpp | 4 +- core/base/Object.h | 4 +- core/base/Resource.cpp | 43 ++++++++------ core/base/Resource.h | 68 ++++++++++++--------- core/base/Sprite.cpp | 31 ---------- core/base/Sprite.h | 14 ----- core/base/Task.cpp | 6 +- core/base/Task.h | 8 +-- core/base/TaskManager.cpp | 6 +- core/base/TaskManager.h | 6 +- core/base/Text.cpp | 16 ++--- core/base/Text.h | 18 +++--- core/base/audio.h | 8 +-- core/base/helper.hpp | 60 +++++++++++++++++-- core/base/intrusive/SmartPointer.hpp | 22 ------- core/base/render.cpp | 4 +- core/base/render.h | 4 +- core/base/window.cpp | 18 +++--- core/base/window.h | 6 +- core/math/vector.hpp | 3 + core/ui/Menu.cpp | 4 +- core/ui/Menu.h | 6 +- core/utils/Data.cpp | 7 +-- core/utils/Data.h | 18 +++--- core/utils/File.cpp | 31 +++++----- core/utils/File.h | 21 ++++--- core/utils/Path.cpp | 28 ++++----- core/utils/Path.h | 10 ++-- core/utils/Player.cpp | 78 ------------------------ core/utils/Player.h | 35 +---------- core/utils/Transcoder.cpp | 9 +-- 56 files changed, 430 insertions(+), 635 deletions(-) diff --git a/core/base/ActionCombined.cpp b/core/base/ActionCombined.cpp index be3f3765..833dd00b 100644 --- a/core/base/ActionCombined.cpp +++ b/core/base/ActionCombined.cpp @@ -125,7 +125,7 @@ namespace easy2d { } - Sequence::Sequence(const Actions& actions) + Sequence::Sequence(Array const& actions) : action_index_(0) { this->Add(actions); @@ -180,7 +180,7 @@ namespace easy2d } } - void Sequence::Add(const Actions& actions) + void Sequence::Add(Array const& actions) { for (const auto &action : actions) { @@ -227,7 +227,7 @@ namespace easy2d { } - Spawn::Spawn(const Actions& actions) + Spawn::Spawn(Array const& actions) { this->Add(actions); } @@ -289,7 +289,7 @@ namespace easy2d } } - void Spawn::Add(const Actions& actions) + void Spawn::Add(Array const& actions) { for (const auto &action : actions) { diff --git a/core/base/ActionCombined.h b/core/base/ActionCombined.h index d6133c03..b1e29177 100644 --- a/core/base/ActionCombined.h +++ b/core/base/ActionCombined.h @@ -64,13 +64,11 @@ namespace easy2d class Sequence : public Action { - using Actions = std::vector; - public: Sequence(); explicit Sequence( - Actions const& actions /* 动作列表 */ + Array const& actions /* 动作列表 */ ); virtual ~Sequence(); @@ -82,7 +80,7 @@ namespace easy2d // 在结尾添加多个动作 void Add( - const Actions& actions /* 动作列表 */ + Array const& actions /* 动作列表 */ ); // 获取该动作的拷贝对象 @@ -103,7 +101,7 @@ namespace easy2d protected: UINT action_index_; - Actions actions_; + Array actions_; }; @@ -111,13 +109,11 @@ namespace easy2d class Spawn : public Action { - using Actions = std::vector; - public: Spawn(); explicit Spawn( - const Actions& actions /* 动作列表 */ + Array const& actions /* 动作列表 */ ); virtual ~Spawn(); @@ -129,7 +125,7 @@ namespace easy2d // 在结尾添加多个动作 void Add( - const Actions& actions /* 动作列表 */ + Array const& actions /* 动作列表 */ ); // 获取该动作的拷贝对象 @@ -149,6 +145,6 @@ namespace easy2d virtual void Update(Node* target, Duration const& dt) override; protected: - Actions actions_; + Array actions_; }; } diff --git a/core/base/Canvas.cpp b/core/base/Canvas.cpp index 3200f870..157c4738 100644 --- a/core/base/Canvas.cpp +++ b/core/base/Canvas.cpp @@ -262,7 +262,7 @@ namespace easy2d } } - void Canvas::DrawText(std::wstring const & text, Point const & point) + void Canvas::DrawText(String const & text, Point const & point) { if (text.empty()) return; @@ -416,14 +416,14 @@ namespace easy2d current_sink_->AddLine(point); } - void Canvas::AddLines(std::vector const& points) + void Canvas::AddLines(Array const& points) { if (current_sink_) { if (!points.empty()) { size_t size = points.size(); - std::vector d2d_points(size); + Array d2d_points(size); for (size_t i = 0; i < size; ++i) { d2d_points[i] = points[i]; diff --git a/core/base/Canvas.h b/core/base/Canvas.h index 6fec91c7..ed75c80d 100644 --- a/core/base/Canvas.h +++ b/core/base/Canvas.h @@ -91,7 +91,7 @@ namespace easy2d // 画文字 void DrawText( - std::wstring const& text, /* 文字 */ + String const& text, /* 文字 */ Point const& point /* 文字位置 */ ); @@ -147,7 +147,7 @@ namespace easy2d // 添加多条线段 void AddLines( - std::vector const& points + Array const& points ); // 添加一条三次方贝塞尔曲线 diff --git a/core/base/DebugNode.cpp b/core/base/DebugNode.cpp index ea87b6b5..be484c09 100644 --- a/core/base/DebugNode.cpp +++ b/core/base/DebugNode.cpp @@ -50,7 +50,7 @@ namespace easy2d { } - void DebugNodeImpl::AddDebugText(std::wstring const & text) + void DebugNodeImpl::AddDebugText(String const & text) { try { diff --git a/core/base/DebugNode.h b/core/base/DebugNode.h index dbd5c66d..9931e09d 100644 --- a/core/base/DebugNode.h +++ b/core/base/DebugNode.h @@ -35,7 +35,7 @@ namespace easy2d virtual ~DebugNodeImpl(); - void AddDebugText(std::wstring const& text); + void AddDebugText(String const& text); void ClearDebugText(); @@ -44,9 +44,9 @@ namespace easy2d void OnUpdate(Duration const& dt) override; protected: - SpText debug_text_; - std::vector frame_time_; - std::vector texts_; + SpText debug_text_; + Array frame_time_; + Array texts_; }; E2D_DECLARE_SINGLETON_TYPE(DebugNodeImpl, DebugNode); diff --git a/core/base/EventDispatcher.cpp b/core/base/EventDispatcher.cpp index 6e6d8ca4..e2a36acc 100644 --- a/core/base/EventDispatcher.cpp +++ b/core/base/EventDispatcher.cpp @@ -50,7 +50,7 @@ namespace easy2d } } - void EventDispatcher::AddListener(EventType type, EventCallback callback, std::wstring const& name) + void EventDispatcher::AddListener(EventType type, EventCallback callback, String const& name) { SpEventListener listener = new EventListener(type, callback, name); if (listener) @@ -59,7 +59,7 @@ namespace easy2d } } - void EventDispatcher::StartListeners(std::wstring const & listener_name) + void EventDispatcher::StartListeners(String const & listener_name) { for (auto listener = listeners_.First(); listener; listener = listener->NextItem()) { @@ -70,7 +70,7 @@ namespace easy2d } } - void EventDispatcher::StopListeners(std::wstring const & listener_name) + void EventDispatcher::StopListeners(String const & listener_name) { for (auto listener = listeners_.First(); listener; listener = listener->NextItem()) { @@ -81,7 +81,7 @@ namespace easy2d } } - void EventDispatcher::RemoveListeners(std::wstring const & listener_name) + void EventDispatcher::RemoveListeners(String const & listener_name) { SpEventListener next; for (auto listener = listeners_.First(); listener; listener = next) diff --git a/core/base/EventDispatcher.h b/core/base/EventDispatcher.h index a6e9f350..ee8befaa 100644 --- a/core/base/EventDispatcher.h +++ b/core/base/EventDispatcher.h @@ -37,22 +37,22 @@ namespace easy2d void AddListener( EventType type, EventCallback callback, - std::wstring const& name = L"" + String const& name = L"" ); // 启动监听器 void StartListeners( - std::wstring const& listener_name + String const& listener_name ); // 停止监听器 void StopListeners( - std::wstring const& listener_name + String const& listener_name ); // 移除监听器 void RemoveListeners( - std::wstring const& listener_name + String const& listener_name ); // 启动监听器 diff --git a/core/base/EventListener.cpp b/core/base/EventListener.cpp index da6ca97e..47eab905 100644 --- a/core/base/EventListener.cpp +++ b/core/base/EventListener.cpp @@ -23,7 +23,7 @@ namespace easy2d { - EventListener::EventListener(EventType type, EventCallback const & callback, std::wstring const & name) + EventListener::EventListener(EventType type, EventCallback const & callback, String const & name) : type_(type) , callback_(callback) , name_(name) @@ -50,12 +50,12 @@ namespace easy2d return running_; } - std::wstring const & EventListener::GetName() const + String const & EventListener::GetName() const { return name_; } - void EventListener::SetName(std::wstring const & name) + void EventListener::SetName(String const & name) { name_ = name; } diff --git a/core/base/EventListener.h b/core/base/EventListener.h index 6ed23703..94ecf7b9 100644 --- a/core/base/EventListener.h +++ b/core/base/EventListener.h @@ -41,7 +41,7 @@ namespace easy2d EventListener( EventType type, EventCallback const& callback, - std::wstring const& name = L"" + String const& name = L"" ); virtual ~EventListener(); @@ -50,15 +50,15 @@ namespace easy2d void Stop(); - void SetName(std::wstring const& name); + void SetName(String const& name); bool IsRunning() const; - std::wstring const& GetName() const; + String const& GetName() const; protected: bool running_; - std::wstring name_; + String name_; EventType type_; EventCallback callback_; }; diff --git a/core/base/Factory.cpp b/core/base/Factory.cpp index 242f7f6e..e8292eae 100644 --- a/core/base/Factory.cpp +++ b/core/base/Factory.cpp @@ -150,7 +150,7 @@ namespace easy2d return hr; } - HRESULT FactoryImpl::CreateBitmapFromFile(CpBitmap & bitmap, CpRenderTarget const & rt, std::wstring const & file_path) + HRESULT FactoryImpl::CreateBitmapFromFile(CpBitmap & bitmap, CpRenderTarget const & rt, String const & file_path) { if (imaging_factory_ == nullptr) { @@ -227,8 +227,9 @@ namespace easy2d SmartPointer bitmap_tmp; // 加载资源 - ResourceData buffer; - HRESULT hr = res.Load(&buffer) ? S_OK : E_FAIL; + LPVOID buffer; + DWORD buffer_size; + HRESULT hr = res.Load(buffer, buffer_size) ? S_OK : E_FAIL; if (SUCCEEDED(hr)) { @@ -238,8 +239,8 @@ namespace easy2d if (SUCCEEDED(hr)) { hr = stream->InitializeFromMemory( - static_cast(buffer.buffer), - buffer.buffer_size + static_cast(buffer), + buffer_size ); } @@ -418,7 +419,7 @@ namespace easy2d return hr; } - HRESULT FactoryImpl::CreateTextLayout(CpTextLayout & text_layout, Size& layout_size, std::wstring const & text, CpTextFormat const& text_format, TextStyle const & text_style) const + HRESULT FactoryImpl::CreateTextLayout(CpTextLayout & text_layout, Size& layout_size, String const & text, CpTextFormat const& text_format, TextStyle const & text_style) const { if (!write_factory_) return E_UNEXPECTED; diff --git a/core/base/Factory.h b/core/base/Factory.h index 1edf1359..c77de77b 100644 --- a/core/base/Factory.h +++ b/core/base/Factory.h @@ -52,7 +52,7 @@ namespace easy2d HRESULT CreateBitmapFromFile( CpBitmap& bitmap, CpRenderTarget const& rt, - std::wstring const& file_path + String const& file_path ); HRESULT CreateBitmapFromResource( @@ -99,7 +99,7 @@ namespace easy2d HRESULT CreateTextLayout( CpTextLayout& text_layout, Size& layout_size, - std::wstring const& text, + String const& text, CpTextFormat const& text_format, TextStyle const& text_style ) const; diff --git a/core/base/Font.hpp b/core/base/Font.hpp index a6a43cc7..d27b7f4f 100644 --- a/core/base/Font.hpp +++ b/core/base/Font.hpp @@ -41,17 +41,17 @@ namespace easy2d class Font { public: - std::wstring family; // 字体族 + String family; // 字体族 float size; // 字号 unsigned int weight; // 粗细值 bool italic; // 是否斜体 public: Font( - const std::wstring& family = L"", - float size = 22, - unsigned int weight = FontWeight::Normal, - bool italic = false + const String& family = L"", + float size = 22, + unsigned int weight = FontWeight::Normal, + bool italic = false ) : family(family) , size(size) diff --git a/core/base/Frames.cpp b/core/base/Frames.cpp index 7ef10b4e..3d0d2f93 100644 --- a/core/base/Frames.cpp +++ b/core/base/Frames.cpp @@ -29,7 +29,7 @@ namespace easy2d { } - Frames::Frames(Images const& frames) + Frames::Frames(Array const& frames) : interval_(1) { this->Add(frames); @@ -40,7 +40,7 @@ namespace easy2d { } - Frames::Frames(Duration const& interval, Images const& frames) + Frames::Frames(Duration const& interval, Array const& frames) : interval_(interval) { this->Add(frames); @@ -65,7 +65,7 @@ namespace easy2d } } - void Frames::Add(Images const& frames) + void Frames::Add(Array const& frames) { for (const auto &image : frames) { @@ -78,7 +78,7 @@ namespace easy2d return interval_; } - Frames::Images const& Frames::GetFrames() const + Array const& Frames::GetFrames() const { return frames_; } diff --git a/core/base/Frames.h b/core/base/Frames.h index d82059bc..446be900 100644 --- a/core/base/Frames.h +++ b/core/base/Frames.h @@ -28,22 +28,20 @@ namespace easy2d class Frames : public Object { - using Images = std::vector< SpImage >; - public: Frames(); explicit Frames( - Images const& frames /* 关键帧数组 */ + Array const& frames /* 关键帧数组 */ ); explicit Frames( - Duration const& interval /* 帧间隔 */ + Duration const& interval /* 帧间隔 */ ); explicit Frames( - Duration const& interval, /* 帧间隔 */ - Images const& frames /* 关键帧数组 */ + Duration const& interval, /* 帧间隔 */ + Array const& frames /* 关键帧数组 */ ); virtual ~Frames(); @@ -55,14 +53,14 @@ namespace easy2d // 添加多个关键帧 void Add( - Images const& frames + Array const& frames ); // 获取帧间隔 Duration const& GetInterval() const; // 获取关键帧 - Images const& GetFrames() const; + Array const& GetFrames() const; // 设置每一帧的时间间隔 void SetInterval( @@ -76,7 +74,7 @@ namespace easy2d SpFrames Reverse() const; protected: - Duration interval_; - Images frames_; + Duration interval_; + Array frames_; }; } diff --git a/core/base/Game.cpp b/core/base/Game.cpp index df14693c..eff1bad6 100644 --- a/core/base/Game.cpp +++ b/core/base/Game.cpp @@ -27,7 +27,6 @@ #include "Transition.h" #include "KeyEvent.hpp" #include "MouseEvent.hpp" -#include "../math/Matrix.hpp" #include #include @@ -258,10 +257,22 @@ namespace easy2d ::InvalidateRect(hwnd, NULL, FALSE); } - bool Game::HandleMessage(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) + void Game::Dispatch(Event * event) { - bool unhandled = false; + if (transition_) + return; + if (curr_scene_) + curr_scene_->DispatchEvent(event); + } + + LRESULT CALLBACK Game::WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) + { + Game * game = reinterpret_cast(::GetWindowLongW(hwnd, GWLP_USERDATA)); + + if (!game) + return ::DefWindowProcW(hwnd, msg, wparam, lparam); + switch (msg) { case WM_PAINT: @@ -269,10 +280,12 @@ namespace easy2d PAINTSTRUCT ps; ::BeginPaint(hwnd, &ps); - Update(); - Render(hwnd); + game->Update(); + game->Render(hwnd); ::EndPaint(hwnd, &ps); + + return 0; } break; @@ -310,7 +323,7 @@ namespace easy2d if (wparam & MK_LBUTTON || wparam & MK_RBUTTON) event.button_down = true; - Dispatch(&event); + game->Dispatch(&event); } break; @@ -318,13 +331,14 @@ namespace easy2d case WM_KEYUP: { KeyEvent event(msg, KeyCode(wparam)); - Dispatch(&event); + game->Dispatch(&event); } break; case WM_DISPLAYCHANGE: { E2D_LOG("The display resolution has changed"); + ::InvalidateRect(hwnd, nullptr, FALSE); } break; @@ -334,12 +348,13 @@ namespace easy2d E2D_LOG("Received a message to close the window"); SysEvent event(SysEvent::WindowClose); - Dispatch(&event); + game->Dispatch(&event); - if (OnClose()) + if (game->OnClose()) { ::DestroyWindow(hwnd); } + return 0; } break; @@ -347,8 +362,9 @@ namespace easy2d { E2D_LOG("Window was destroyed"); - OnExit(); + game->OnExit(); ::PostQuitMessage(0); + return 0; } break; @@ -356,13 +372,13 @@ namespace easy2d { if (SIZE_MAXHIDE == wparam || SIZE_MINIMIZED == wparam) { - active_ = false; + game->active_ = false; E2D_LOG("Window minimized"); } else if (SIZE_RESTORED == wparam) { - active_ = true; + game->active_ = true; ::InvalidateRect(hwnd, nullptr, FALSE); E2D_LOG("Window restored"); @@ -376,69 +392,41 @@ namespace easy2d // 错误,因为这个错误将在下一次调用 EndDraw 时产生 Graphics::Instance()->Resize(width, height); } - unhandled = true; break; case WM_ACTIVATE: { - if (WA_INACTIVE == wparam) - { - E2D_LOG("Window deactivated"); - - SysEvent event(SysEvent::WindowDeavtivate); - Dispatch(&event); - } - else + bool active = (LOWORD(wparam) != WA_INACTIVE); + if (active) { E2D_LOG("Window activated"); SysEvent event(SysEvent::WindowActivate); - Dispatch(&event); + game->Dispatch(&event); + } + else + { + E2D_LOG("Window deactivated"); + + SysEvent event(SysEvent::WindowDeavtivate); + game->Dispatch(&event); } } - unhandled = true; break; case WM_SETTEXT: { E2D_LOG("Window title changed"); } - unhandled = true; break; case WM_SETICON: { E2D_LOG("Window icon changed"); } - unhandled = true; break; - - default: - unhandled = true; - break; } - return !unhandled; - } - - void Game::Dispatch(Event * event) - { - if (transition_) - return; - - if (curr_scene_) - curr_scene_->DispatchEvent(event); - } - - LRESULT CALLBACK Game::WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) - { - Game * game = reinterpret_cast( - static_cast(::GetWindowLongW(hwnd, GWLP_USERDATA)) - ); - - if (game && game->HandleMessage(hwnd, msg, wparam, lparam)) - return 0; - return ::DefWindowProcW(hwnd, msg, wparam, lparam); } } diff --git a/core/base/Game.h b/core/base/Game.h index bc3a05db..cd72e6cc 100644 --- a/core/base/Game.h +++ b/core/base/Game.h @@ -31,12 +31,12 @@ namespace easy2d { struct Options { - std::wstring title; // 标题 - int width; // 宽度 - int height; // 高度 - LPCWSTR icon; // 图标 - bool vsync; // 垂直同步 - bool debug; // 调试模式 + String title; // 标题 + int width; // 宽度 + int height; // 高度 + LPCWSTR icon; // 图标 + bool vsync; // 垂直同步 + bool debug; // 调试模式 Options() : title(L"Easy2D Game") @@ -100,13 +100,6 @@ namespace easy2d void Update(); - bool HandleMessage( - HWND hwnd, - UINT msg, - WPARAM wparam, - LPARAM lparam - ); - void Dispatch( Event* event ); diff --git a/core/base/Geometry.cpp b/core/base/Geometry.cpp index c8badf22..0e05a878 100644 --- a/core/base/Geometry.cpp +++ b/core/base/Geometry.cpp @@ -324,14 +324,14 @@ namespace easy2d current_sink_->AddLine(point); } - void PathGeometry::AddLines(std::vector const& points) + void PathGeometry::AddLines(Array const& points) { if (current_sink_) { if (!points.empty()) { size_t size = points.size(); - std::vector d2d_points(size); + Array d2d_points(size); for (size_t i = 0; i < size; ++i) { d2d_points[i] = points[i]; diff --git a/core/base/Geometry.h b/core/base/Geometry.h index 8dc33182..589f2ab9 100644 --- a/core/base/Geometry.h +++ b/core/base/Geometry.h @@ -231,7 +231,7 @@ namespace easy2d // 添加多条线段 void AddLines( - std::vector const& points + Array const& points ); // 添加一条三次方贝塞尔曲线 diff --git a/core/base/Image.cpp b/core/base/Image.cpp index bf73fb75..5d197bb3 100644 --- a/core/base/Image.cpp +++ b/core/base/Image.cpp @@ -45,19 +45,6 @@ namespace easy2d this->Crop(crop_rect); } - Image::Image(std::wstring const& file_name) - : Image() - { - this->Load(file_name); - } - - Image::Image(std::wstring const& file_name, const Rect & crop_rect) - : Image() - { - this->Load(file_name); - this->Crop(crop_rect); - } - Image::Image(CpBitmap const & bitmap) : Image() { @@ -70,38 +57,35 @@ namespace easy2d bool Image::Load(Resource const& res) { + HRESULT hr = S_OK; CpBitmap bitmap; - HRESULT hr = Graphics::Instance()->CreateBitmapFromResource(bitmap, res); + + if (res.IsFile()) + { + File image_file; + if (!image_file.Open(res.GetFileName())) + { + logs::Warningln("Image file '%s' not found!", StringWideCharToMultiByte(res.GetFileName()).c_str()); + return false; + } + + // 用户输入的路径不一定是完整路径,因为用户可能通过 File::AddSearchPath 添加 + // 默认搜索路径,所以需要通过 File::GetPath 获取完整路径 + String image_file_path = image_file.GetPath(); + + hr = Graphics::Instance()->CreateBitmapFromFile(bitmap, image_file_path); + } + else + { + hr = Graphics::Instance()->CreateBitmapFromResource(bitmap, res); + } + if (FAILED(hr)) { - logs::Errorln(hr, "Load Image from resource failed!"); - return false; - } - SetBitmap(bitmap); - return true; - } - - bool Image::Load(std::wstring const& file_name) - { - File image_file; - if (!image_file.Open(file_name)) - { - logs::Warningln("Image file '%s' not found!", StringWideCharToMultiByte(file_name).c_str()); + logs::Errorln(hr, "Load image file failed!"); return false; } - // 用户输入的路径不一定是完整路径,因为用户可能通过 File::AddSearchPath 添加 - // 默认搜索路径,所以需要通过 File::GetPath 获取完整路径 - std::wstring image_file_path = image_file.GetPath(); - - CpBitmap bitmap; - HRESULT hr = Graphics::Instance()->CreateBitmapFromFile(bitmap, image_file_path); - if (FAILED(hr)) - { - logs::Errorln(hr, "Load Image from file failed!"); - return false; - } - SetBitmap(bitmap); return true; } diff --git a/core/base/Image.h b/core/base/Image.h index 162ed9aa..38a515bf 100644 --- a/core/base/Image.h +++ b/core/base/Image.h @@ -40,15 +40,6 @@ namespace easy2d Rect const& crop_rect /* 裁剪矩形 */ ); - explicit Image( - std::wstring const& file_name - ); - - explicit Image( - std::wstring const& file_name, - Rect const& crop_rect /* 裁剪矩形 */ - ); - explicit Image( CpBitmap const& bitmap ); @@ -60,11 +51,6 @@ namespace easy2d Resource const& res ); - // 加载图片资源 - bool Load( - std::wstring const& file_name - ); - // 将图片裁剪为矩形 void Crop( Rect const& crop_rect /* 裁剪矩形 */ diff --git a/core/base/Music.cpp b/core/base/Music.cpp index 334eb3d7..b0cb8361 100644 --- a/core/base/Music.cpp +++ b/core/base/Music.cpp @@ -37,22 +37,8 @@ namespace easy2d { } - Music::Music(std::wstring const& file_path) - : opened_(false) - , playing_(false) - , wave_data_(nullptr) - , size_(0) - , voice_(nullptr) - { - Load(file_path); - } - Music::Music(Resource const& res) - : opened_(false) - , playing_(false) - , wave_data_(nullptr) - , size_(0) - , voice_(nullptr) + : Music() { Load(res); } @@ -62,48 +48,6 @@ namespace easy2d Close(); } - bool Music::Load(std::wstring const& file_path) - { - if (opened_) - { - Close(); - } - - File music_file; - if (!music_file.Open(file_path)) - { - logs::Warningln("Media file '%s' not found", StringWideCharToMultiByte(file_path).c_str()); - return false; - } - - // 用户输入的路径不一定是完整路径,因为用户可能通过 File::AddSearchPath 添加 - // 默认搜索路径,所以需要通过 File::GetPath 获取完整路径 - std::wstring music_file_path = music_file.GetPath(); - - Transcoder transcoder; - HRESULT hr = transcoder.LoadMediaFile(music_file_path.c_str(), &wave_data_, &size_); - if (FAILED(hr)) - { - logs::Errorln(hr, "Load media from file failed"); - return false; - } - - hr = Audio::Instance()->CreateVoice(voice_, transcoder.GetWaveFormatEx()); - if (FAILED(hr)) - { - if (wave_data_) - { - delete[] wave_data_; - wave_data_ = nullptr; - } - logs::Errorln(hr, "Create source voice failed"); - return false; - } - - opened_ = true; - return true; - } - bool Music::Load(Resource const& res) { if (opened_) @@ -111,12 +55,31 @@ namespace easy2d Close(); } + HRESULT hr = S_OK; Transcoder transcoder; - HRESULT hr = transcoder.LoadMediaResource(res, &wave_data_, &size_); + + if (res.IsFile()) + { + File music_file; + if (!music_file.Open(res.GetFileName())) + { + logs::Warningln("Media file '%s' not found", StringWideCharToMultiByte(res.GetFileName()).c_str()); + return false; + } + + // 用户输入的路径不一定是完整路径,因为用户可能通过 File::AddSearchPath 添加 + // 默认搜索路径,所以需要通过 File::GetPath 获取完整路径 + String music_file_path = music_file.GetPath(); + HRESULT hr = transcoder.LoadMediaFile(music_file_path.c_str(), &wave_data_, &size_); + } + else + { + hr = transcoder.LoadMediaResource(res, &wave_data_, &size_); + } if (FAILED(hr)) { - logs::Errorln(hr, "Load media from resource failed"); + logs::Errorln(hr, "Load media file failed"); return false; } diff --git a/core/base/Music.h b/core/base/Music.h index 61dbb8cb..73d34b22 100644 --- a/core/base/Music.h +++ b/core/base/Music.h @@ -32,21 +32,12 @@ namespace easy2d public: Music(); - Music( - std::wstring const& file_path /* 音乐文件路径 */ - ); - Music( Resource const& res /* 音乐资源 */ ); virtual ~Music(); - // 打开音乐文件 - bool Load( - std::wstring const& file_path /* 音乐文件路径 */ - ); - // 打开音乐资源 bool Load( Resource const& res /* 音乐资源 */ diff --git a/core/base/Node.cpp b/core/base/Node.cpp index 8777c07c..a2d49279 100644 --- a/core/base/Node.cpp +++ b/core/base/Node.cpp @@ -346,12 +346,12 @@ namespace easy2d visible_ = val; } - void Node::SetName(std::wstring const& name) + void Node::SetName(String const& name) { if (name_ != name) { name_ = name; - hash_name_ = std::hash{}(name); + hash_name_ = std::hash{}(name); } } @@ -470,7 +470,7 @@ namespace easy2d } } - void Node::AddChildren(const Nodes& children) + void Node::AddChildren(Array const& children) { for (const auto& node : children) { @@ -483,10 +483,10 @@ namespace easy2d return Rect(Point{}, size_); } - Node::Nodes Node::GetChildren(std::wstring const& name) const + Array Node::GetChildren(String const& name) const { - Nodes children; - size_t hash_code = std::hash{}(name); + Array children; + size_t hash_code = std::hash{}(name); for (Node* child = children_.First().Get(); child; child = child->NextItem().Get()) { @@ -498,9 +498,9 @@ namespace easy2d return children; } - SpNode Node::GetChild(std::wstring const& name) const + SpNode Node::GetChild(String const& name) const { - size_t hash_code = std::hash{}(name); + size_t hash_code = std::hash{}(name); for (Node* child = children_.First().Get(); child; child = child->NextItem().Get()) { @@ -547,14 +547,14 @@ namespace easy2d return false; } - void Node::RemoveChildren(std::wstring const& child_name) + void Node::RemoveChildren(String const& child_name) { if (children_.IsEmpty()) { return; } - size_t hash_code = std::hash{}(child_name); + size_t hash_code = std::hash{}(child_name); Node* next; for (Node* child = children_.First().Get(); child; child = next) diff --git a/core/base/Node.h b/core/base/Node.h index 621066d4..355c36a1 100644 --- a/core/base/Node.h +++ b/core/base/Node.h @@ -44,7 +44,6 @@ namespace easy2d friend class Transition; friend class intrusive::List; - using Nodes = std::vector; using Children = intrusive::List; public: @@ -60,7 +59,7 @@ namespace easy2d bool IsVisible() const { return visible_; } // 获取名称 - std::wstring const& GetName() const { return name_; } + String const& GetName() const { return name_; } // 获取名称的 Hash 值 size_t GetHashName() const { return hash_name_; } @@ -144,7 +143,7 @@ namespace easy2d // 设置名称 void SetName( - std::wstring const& name + String const& name ); // 设置横坐标 @@ -297,17 +296,17 @@ namespace easy2d // 添加多个子节点 void AddChildren( - const Nodes& children + Array const& children ); // 获取所有名称相同的子节点 - Nodes GetChildren( - std::wstring const& name + Array GetChildren( + String const& name ) const; // 获取名称相同的子节点 SpNode GetChild( - std::wstring const& name + String const& name ) const; // 获取全部子节点 @@ -325,7 +324,7 @@ namespace easy2d // 移除所有名称相同的子节点 void RemoveChildren( - std::wstring const& child_name + String const& child_name ); // 移除所有节点 @@ -354,20 +353,20 @@ namespace easy2d void SetScene(Scene* scene); protected: - bool visible_; - bool hover_; - bool pressed_; - int z_order_; - float opacity_; - float display_opacity_; - std::wstring name_; - size_t hash_name_; - Transform transform_; - Point pivot_; - Size size_; - Node* parent_; - Scene* scene_; - Children children_; + bool visible_; + bool hover_; + bool pressed_; + int z_order_; + float opacity_; + float display_opacity_; + String name_; + size_t hash_name_; + Transform transform_; + Point pivot_; + Size size_; + Node* parent_; + Scene* scene_; + Children children_; mutable bool dirty_transform_; mutable bool dirty_transform_inverse_; diff --git a/core/base/Object.cpp b/core/base/Object.cpp index 04daa4fa..e9a41c6d 100644 --- a/core/base/Object.cpp +++ b/core/base/Object.cpp @@ -25,7 +25,7 @@ namespace easy2d namespace { bool tracing_leaks = true; - std::vector tracing_objects; + Array tracing_objects; } Object::Object() @@ -67,7 +67,7 @@ namespace easy2d tracing_leaks = false; } - std::vector const& easy2d::Object::__GetTracingObjects() + Array const& easy2d::Object::__GetTracingObjects() { return tracing_objects; } diff --git a/core/base/Object.h b/core/base/Object.h index 0544f864..31e00b3d 100644 --- a/core/base/Object.h +++ b/core/base/Object.h @@ -20,7 +20,7 @@ #pragma once #include "RefCounter.hpp" -#include +#include "helper.hpp" namespace easy2d { @@ -40,7 +40,7 @@ namespace easy2d static void StopTracingLeaks(); - static std::vector const& __GetTracingObjects(); + static Array const& __GetTracingObjects(); protected: static void __AddObjectToTracingList(Object*); diff --git a/core/base/Resource.cpp b/core/base/Resource.cpp index e362f50c..a00a9b9a 100644 --- a/core/base/Resource.cpp +++ b/core/base/Resource.cpp @@ -23,36 +23,45 @@ namespace easy2d { + Resource::Resource(String file_name) + : type_(Type::File) + , file_name_(file_name) + { + } + + Resource::Resource(LPCWSTR file_name) + : type_(Type::File) + , file_name_(file_name) + { + } + Resource::Resource(LPCWSTR name, LPCWSTR type) - : name_(name) - , type_(type) + : type_(Type::Binary) + , bin_name_(name) + , bin_type_(type) { } - LPCWSTR Resource::GetName() const + Resource::~Resource() { - return name_; - } - - LPCWSTR Resource::GetType() const - { - return type_; } size_t Resource::GetHashCode() const { - return std::hash{}(name_); + if (type_ == Type::File) + return std::hash{}(file_name_); + return std::hash{}(bin_name_); } - bool Resource::Load(ResourceData* buffer) const + bool Resource::Load(LPVOID& buffer, DWORD& buffer_size) const { - if (!buffer) + if (type_ != Type::Binary) return false; HGLOBAL res_data; HRSRC res_info; - res_info = FindResourceW(nullptr, name_, type_); + res_info = FindResourceW(nullptr, bin_name_, bin_type_); if (res_info == nullptr) { logs::Errorln("FindResource"); @@ -66,15 +75,15 @@ namespace easy2d return false; } - (*buffer).buffer_size = SizeofResource(nullptr, res_info); - if ((*buffer).buffer_size == 0) + buffer_size = SizeofResource(nullptr, res_info); + if (buffer_size == 0) { logs::Errorln("SizeofResource"); return false; } - (*buffer).buffer = LockResource(res_data); - if ((*buffer).buffer == nullptr) + buffer = LockResource(res_data); + if (buffer == nullptr) { logs::Errorln("LockResource"); return false; diff --git a/core/base/Resource.h b/core/base/Resource.h index 862e37b1..61780f7c 100644 --- a/core/base/Resource.h +++ b/core/base/Resource.h @@ -19,32 +19,15 @@ // THE SOFTWARE. #pragma once -#include "macros.h" +#include "helper.hpp" namespace easy2d { - // 资源数据 - // - // Usage: - // 如果需要手动加载资源, 可以通过 Resource::Load 方法获取资源内容 - // ResourceData data; - // if (res.Load(&data)) { - // LPVOID data = data.buffer; - // DWORD size_ = data.buffer_size; - // } - // - struct ResourceData - { - LPVOID buffer; - DWORD buffer_size; - }; - - // 资源 // // Usage: - // Resource 用于获取可执行文件 (exe) 中的资源, 必须在构造函数中指定它的 - // 资源类型和名称标识符。 + // Resource 用于指定一份资源 + // 资源可以是文件类型,也可以是保存在 exe 中的二进制文件 // 例如, 一份音频资源的类型为 L"WAVE", 名称标识符为 IDR_WAVE_1, // 那么可以这样指定该资源: Resource res(MAKEINTRESOURCE(IDR_WAVE_1), L"WAVE"); // @@ -53,23 +36,50 @@ namespace easy2d class Resource { public: + enum class Type { File, Binary }; + Resource( - LPCWSTR name, /* 资源名称 */ - LPCWSTR type /* 资源类型 */ + String file_name /* 文件路径 */ ); + Resource( + LPCWSTR file_name /* 文件路径 */ + ); + + Resource( + LPCWSTR name, /* 资源名称 */ + LPCWSTR type /* 资源类型 */ + ); + + virtual ~Resource(); + + inline bool IsFile() const { return type_ == Type::File; } + + inline Type GetType() const { return type_; } + + inline String const& GetFileName() const { return file_name_; } + bool Load( - ResourceData* buffer + LPVOID& buffer, + DWORD& buffer_size ) const; - LPCWSTR GetName() const; - - LPCWSTR GetType() const; - size_t GetHashCode() const; private: - LPCWSTR name_; - LPCWSTR type_; + Type type_; + union + { + struct + { + String file_name_; + }; + + struct + { + LPCWSTR bin_name_; + LPCWSTR bin_type_; + }; + }; }; } diff --git a/core/base/Sprite.cpp b/core/base/Sprite.cpp index ccbfd27d..425defdf 100644 --- a/core/base/Sprite.cpp +++ b/core/base/Sprite.cpp @@ -47,19 +47,6 @@ namespace easy2d Crop(crop_rect); } - Sprite::Sprite(std::wstring const& file_name) - : image_(nullptr) - { - Load(file_name); - } - - Sprite::Sprite(std::wstring const& file_name, const Rect & crop_rect) - : image_(nullptr) - { - Load(file_name); - Crop(crop_rect); - } - Sprite::~Sprite() { } @@ -94,24 +81,6 @@ namespace easy2d return false; } - bool Sprite::Load(std::wstring const& file_name) - { - if (!image_) - { - image_ = new (std::nothrow) Image; - } - - if (image_) - { - if (image_->Load(file_name)) - { - Node::SetSize(image_->GetWidth(), image_->GetHeight()); - return true; - } - } - return false; - } - void Sprite::Crop(const Rect& crop_rect) { image_->Crop(crop_rect); diff --git a/core/base/Sprite.h b/core/base/Sprite.h index 32719745..45dc2a14 100644 --- a/core/base/Sprite.h +++ b/core/base/Sprite.h @@ -44,15 +44,6 @@ namespace easy2d const Rect& crop_rect /* 裁剪矩形 */ ); - explicit Sprite( - std::wstring const& file_name - ); - - explicit Sprite( - std::wstring const& file_name, - const Rect& crop_rect /* 裁剪矩形 */ - ); - virtual ~Sprite(); // 加载图片文件 @@ -60,11 +51,6 @@ namespace easy2d Resource const& res ); - // 加载图片文件 - bool Load( - std::wstring const& file_name - ); - // 加载图片 bool Load( SpImage const& image diff --git a/core/base/Task.cpp b/core/base/Task.cpp index db58744f..140b5b68 100644 --- a/core/base/Task.cpp +++ b/core/base/Task.cpp @@ -22,12 +22,12 @@ namespace easy2d { - Task::Task(Callback const& func, std::wstring const& name) + Task::Task(Callback const& func, String const& name) : Task(func, Duration{}, -1, name) { } - Task::Task(Callback const& func, Duration const& delay, int times, std::wstring const& name) + Task::Task(Callback const& func, Duration const& delay, int times, String const& name) : running_(true) , run_times_(0) , total_times_(times) @@ -91,7 +91,7 @@ namespace easy2d return running_; } - std::wstring const& Task::GetName() const + String const& Task::GetName() const { return name_; } diff --git a/core/base/Task.h b/core/base/Task.h index 21992bae..f3a55c9d 100644 --- a/core/base/Task.h +++ b/core/base/Task.h @@ -41,14 +41,14 @@ namespace easy2d public: explicit Task( Callback const& func, /* 执行函数 */ - std::wstring const& name = L"" /* 任务名称 */ + String const& name = L"" /* 任务名称 */ ); explicit Task( Callback const& func, /* 执行函数 */ Duration const& delay, /* 时间间隔(秒) */ int times = -1, /* 执行次数(设 -1 为永久执行) */ - std::wstring const& name = L"" /* 任务名称 */ + String const& name = L"" /* 任务名称 */ ); // 启动任务 @@ -61,7 +61,7 @@ namespace easy2d bool IsRunning() const; // 获取任务名称 - std::wstring const& GetName() const; + String const& GetName() const; protected: void Update(Duration const& dt, bool& remove_after_update); @@ -72,7 +72,7 @@ namespace easy2d bool running_; int run_times_; int total_times_; - std::wstring name_; + String name_; Duration delay_; Duration delta_; Callback callback_; diff --git a/core/base/TaskManager.cpp b/core/base/TaskManager.cpp index 58dde7cf..6dbb08c4 100644 --- a/core/base/TaskManager.cpp +++ b/core/base/TaskManager.cpp @@ -52,7 +52,7 @@ namespace easy2d } } - void TaskManager::StopTasks(std::wstring const& name) + void TaskManager::StopTasks(String const& name) { if (tasks_.IsEmpty()) return; @@ -66,7 +66,7 @@ namespace easy2d } } - void TaskManager::StartTasks(std::wstring const& name) + void TaskManager::StartTasks(String const& name) { if (tasks_.IsEmpty()) return; @@ -80,7 +80,7 @@ namespace easy2d } } - void TaskManager::RemoveTasks(std::wstring const& name) + void TaskManager::RemoveTasks(String const& name) { if (tasks_.IsEmpty()) return; diff --git a/core/base/TaskManager.h b/core/base/TaskManager.h index 856c4d46..0080dc9b 100644 --- a/core/base/TaskManager.h +++ b/core/base/TaskManager.h @@ -35,17 +35,17 @@ namespace easy2d // 启动任务 void StartTasks( - std::wstring const& task_name + String const& task_name ); // 停止任务 void StopTasks( - std::wstring const& task_name + String const& task_name ); // 移除任务 void RemoveTasks( - std::wstring const& task_name + String const& task_name ); // 启动所有任务 diff --git a/core/base/Text.cpp b/core/base/Text.cpp index ea6ca42b..bf656660 100644 --- a/core/base/Text.cpp +++ b/core/base/Text.cpp @@ -48,22 +48,22 @@ namespace easy2d { } - Text::Text(std::wstring const& text) + Text::Text(String const& text) : Text(text, text_default_font, text_default_style) { } - Text::Text(std::wstring const& text, const Font & font) + Text::Text(String const& text, const Font & font) : Text(text, font, text_default_style) { } - Text::Text(std::wstring const& text, const TextStyle & style) + Text::Text(String const& text, const TextStyle & style) : Text(text, text_default_font, style) { } - Text::Text(std::wstring const& text, const Font & font, const TextStyle & style) + Text::Text(String const& text, const Font & font, const TextStyle & style) : font_(font) , style_(style) , text_(text) @@ -75,7 +75,7 @@ namespace easy2d { } - std::wstring const& Text::GetText() const + String const& Text::GetText() const { return text_; } @@ -90,7 +90,7 @@ namespace easy2d return style_; } - std::wstring const& Text::GetFontFamily() const + String const& Text::GetFontFamily() const { return font_.family; } @@ -158,7 +158,7 @@ namespace easy2d return style_.outline; } - void Text::SetText(std::wstring const& text) + void Text::SetText(String const& text) { text_ = text; UpdateLayout(); @@ -176,7 +176,7 @@ namespace easy2d UpdateLayout(); } - void Text::SetFontFamily(std::wstring const& family) + void Text::SetFontFamily(String const& family) { if (font_.family != family) { diff --git a/core/base/Text.h b/core/base/Text.h index 4881969c..a8b604d7 100644 --- a/core/base/Text.h +++ b/core/base/Text.h @@ -33,21 +33,21 @@ namespace easy2d Text(); explicit Text( - std::wstring const& text /* 文字内容 */ + String const& text /* 文字内容 */ ); explicit Text( - std::wstring const& text, /* 文字内容 */ + String const& text, /* 文字内容 */ const Font& font /* 字体 */ ); explicit Text( - std::wstring const& text, /* 文字内容 */ + String const& text, /* 文字内容 */ const TextStyle& style /* 文本样式 */ ); explicit Text( - std::wstring const& text, /* 文字内容 */ + String const& text, /* 文字内容 */ const Font& font, /* 字体 */ const TextStyle& style /* 文本样式 */ ); @@ -55,7 +55,7 @@ namespace easy2d virtual ~Text(); // 获取文本 - std::wstring const& GetText() const; + String const& GetText() const; // 获取字体 const Font& GetFont() const; @@ -64,7 +64,7 @@ namespace easy2d const TextStyle& GetStyle() const; // 获取字体族 - std::wstring const& GetFontFamily() const; + String const& GetFontFamily() const; // 获取当前字号 float GetFontSize() const; @@ -101,7 +101,7 @@ namespace easy2d // 设置文本 void SetText( - std::wstring const& text + String const& text ); // 设置文本样式 @@ -116,7 +116,7 @@ namespace easy2d // 设置字体族 void SetFontFamily( - std::wstring const& family + String const& family ); // 设置字号(默认值为 22) @@ -205,7 +205,7 @@ namespace easy2d void UpdateLayout(); protected: - std::wstring text_; + String text_; Font font_; TextStyle style_; CpTextFormat text_format_; diff --git a/core/base/audio.h b/core/base/audio.h index 4a0f5d77..ee1ceb8a 100644 --- a/core/base/audio.h +++ b/core/base/audio.h @@ -20,10 +20,10 @@ #pragma once #include "macros.h" +#include "helper.hpp" #include "Singleton.hpp" #include "noncopyable.hpp" #include -#include namespace easy2d { @@ -79,7 +79,7 @@ namespace easy2d { E2D_DECLARE_SINGLETON(AudioDevice); - using VoiceMap = std::unordered_set; + using VoiceMap = UnorderedSet; public: HRESULT Init(bool debug); @@ -107,9 +107,9 @@ namespace easy2d ~AudioDevice(); protected: - IXAudio2* x_audio2_; + VoiceMap voice_cache_; + IXAudio2* x_audio2_; IXAudio2MasteringVoice* mastering_voice_; - VoiceMap voice_cache_; }; E2D_DECLARE_SINGLETON_TYPE(AudioDevice, Audio); diff --git a/core/base/helper.hpp b/core/base/helper.hpp index 9c33a0a6..ece93ed9 100644 --- a/core/base/helper.hpp +++ b/core/base/helper.hpp @@ -24,6 +24,12 @@ #include "../math/vector.hpp" #include "../math/Rect.hpp" #include "../math/Matrix.hpp" +#include +#include +#include +#include +#include + #ifndef E2D_DECLARE_SMART_PTR #define E2D_DECLARE_SMART_PTR(class_name)\ @@ -38,6 +44,30 @@ } #endif +namespace easy2d +{ + using String = std::wstring; + using StringStream = std::wstringstream; + + template + using Array = std::vector; + + template + using List = std::list; + + template + using Set = std::set; + + template + using UnorderedSet = std::unordered_set; + + template + using Map = std::map; + + template + using UnorderedMap = std::unordered_map; +} + namespace easy2d { // "Sp" is a shorthand for "Smart Pointer" @@ -94,9 +124,29 @@ namespace easy2d E2D_DECLARE_NS_SMART_PTR(ui, Button); E2D_DECLARE_NS_SMART_PTR(ui, Menu); - using Vector2 = math::Vector2; - using Point = math::Vector2; - using Size = math::Vector2; - using Rect = math::Rect; - using Matrix = math::Matrix; + using namespace math; + using namespace ui; } + +namespace easy2d +{ + class __SmartPointerMaker + { + public: + static inline __SmartPointerMaker const& Instance() + { + static __SmartPointerMaker maker; + return maker; + } + + template + inline intrusive::SmartPointer operator- (T* ptr) const + { + return intrusive::SmartPointer(ptr); + } + }; +} + +#ifndef E_NEW +# define E_NEW (::easy2d::__SmartPointerMaker::Instance()) - new (std::nothrow) +#endif diff --git a/core/base/intrusive/SmartPointer.hpp b/core/base/intrusive/SmartPointer.hpp index e680501f..7ee3e0ac 100644 --- a/core/base/intrusive/SmartPointer.hpp +++ b/core/base/intrusive/SmartPointer.hpp @@ -199,26 +199,4 @@ namespace easy2d lhs.Swap(rhs); } - class SmartMaker - { - public: - static inline SmartMaker const& Instance() - { - static SmartMaker maker; - return maker; - } - - template - inline intrusive::SmartPointer operator -(T* ptr) const - { - return intrusive::SmartPointer(ptr); - } - }; - -#ifdef NEW -# undef NEW -#endif - -#define NEW ::easy2d::SmartMaker::Instance() - new (std::nothrow) - } diff --git a/core/base/render.cpp b/core/base/render.cpp index 9cd5fa3c..36f5a95d 100644 --- a/core/base/render.cpp +++ b/core/base/render.cpp @@ -324,14 +324,14 @@ namespace easy2d return S_OK; } - HRESULT GraphicsDevice::CreateBitmapFromFile(CpBitmap& bitmap, std::wstring const& file_path) + HRESULT GraphicsDevice::CreateBitmapFromFile(CpBitmap& bitmap, String const& file_path) { if (render_target_ == nullptr) { return E_UNEXPECTED; } - size_t hash_code = std::hash{}(file_path); + size_t hash_code = std::hash{}(file_path); if (bitmap_cache_.find(hash_code) != bitmap_cache_.end()) { bitmap = bitmap_cache_[hash_code]; diff --git a/core/base/render.h b/core/base/render.h index 6801a250..de7bd336 100644 --- a/core/base/render.h +++ b/core/base/render.h @@ -43,7 +43,7 @@ namespace easy2d int primitives; }; - using BitmapMap = std::unordered_map; + using BitmapMap = UnorderedMap; public: HRESULT Init(HWND hwnd, bool vsync, bool debug); @@ -87,7 +87,7 @@ namespace easy2d HRESULT CreateBitmapFromFile( CpBitmap& bitmap, - std::wstring const& file_path + String const& file_path ); HRESULT CreateBitmapFromResource( diff --git a/core/base/window.cpp b/core/base/window.cpp index ce494fec..6f729f5c 100644 --- a/core/base/window.cpp +++ b/core/base/window.cpp @@ -47,7 +47,7 @@ namespace easy2d E2D_LOG("Destroying window"); } - HRESULT WindowImpl::Init(std::wstring title, int width, int height, LPCWSTR icon, WNDPROC proc, bool debug) + HRESULT WindowImpl::Init(String title, int width, int height, LPCWSTR icon, WNDPROC proc, bool debug) { E2D_LOG("Creating window"); @@ -82,7 +82,7 @@ namespace easy2d GetContentScale(&scale_x, &scale_y); Rect client_rect = LocateWindow(width, height, scale_x, scale_y); - handle = ::CreateWindowEx( + handle = ::CreateWindowExW( NULL, REGISTER_CLASS, title.c_str(), @@ -94,7 +94,7 @@ namespace easy2d nullptr, nullptr, hinstance, - this + nullptr ); if (handle == nullptr) @@ -105,7 +105,7 @@ namespace easy2d return S_OK; } - std::wstring WindowImpl::GetTitle() const + String WindowImpl::GetTitle() const { if (handle) { @@ -113,10 +113,10 @@ namespace easy2d GetWindowTextW(handle, title, 256); return title; } - return std::wstring(); + return String(); } - void WindowImpl::SetTitle(std::wstring const& title) + void WindowImpl::SetTitle(String const& title) { if (handle) ::SetWindowText(handle, title.c_str()); @@ -128,12 +128,12 @@ namespace easy2d { RECT rect; GetClientRect(handle, &rect); - return Size( + return Size{ static_cast(rect.right - rect.left), static_cast(rect.bottom - rect.top) - ); + }; } - return Size(); + return Size{}; } float WindowImpl::GetWidth() const diff --git a/core/base/window.h b/core/base/window.h index 100755a9..3df6a883 100644 --- a/core/base/window.h +++ b/core/base/window.h @@ -31,7 +31,7 @@ namespace easy2d public: HRESULT Init( - std::wstring title, + String title, int width, int height, LPCWSTR icon, @@ -40,10 +40,10 @@ namespace easy2d ); // 获取标题 - std::wstring GetTitle() const; + String GetTitle() const; // 设置标题 - void SetTitle(std::wstring const& title); + void SetTitle(String const& title); // 获取窗口大小 Size GetSize() const; diff --git a/core/math/vector.hpp b/core/math/vector.hpp index e30f89f3..9d320a38 100644 --- a/core/math/vector.hpp +++ b/core/math/vector.hpp @@ -84,5 +84,8 @@ namespace easy2d return reinterpret_cast(*this); } }; + + using Point = Vector2; + using Size = Vector2; } } \ No newline at end of file diff --git a/core/ui/Menu.cpp b/core/ui/Menu.cpp index fd1596c5..4c2ffeee 100644 --- a/core/ui/Menu.cpp +++ b/core/ui/Menu.cpp @@ -29,7 +29,7 @@ namespace easy2d { } - Menu::Menu(const std::vector& buttons) + Menu::Menu(Array const& buttons) : enabled_(true) { for (const auto& button : buttons) @@ -94,7 +94,7 @@ namespace easy2d return false; } - const std::vector& Menu::GetAllButtons() const + Array const& Menu::GetAllButtons() const { return buttons_; } diff --git a/core/ui/Menu.h b/core/ui/Menu.h index 8144ace4..fe423f1d 100644 --- a/core/ui/Menu.h +++ b/core/ui/Menu.h @@ -33,7 +33,7 @@ namespace easy2d Menu(); explicit Menu( - const std::vector& buttons /* 按钮数组 */ + Array const& buttons /* 按钮数组 */ ); // 获取菜单是否禁用 @@ -58,11 +58,11 @@ namespace easy2d ); // 获取所有按钮 - const std::vector& GetAllButtons() const; + Array const& GetAllButtons() const; private: bool enabled_; - std::vector buttons_; + Array buttons_; }; } } \ No newline at end of file diff --git a/core/utils/Data.cpp b/core/utils/Data.cpp index d0b155c1..546b2f15 100644 --- a/core/utils/Data.cpp +++ b/core/utils/Data.cpp @@ -20,11 +20,10 @@ #include "Data.h" #include "Path.h" -#include "../base/macros.h" namespace easy2d { - Data::Data(std::wstring const& key, std::wstring const& field) + Data::Data(String const& key, String const& field) : key_(key) , field_(field) , data_path_(Path::GetDataPath()) @@ -89,7 +88,7 @@ namespace easy2d return ret == TRUE; } - bool Data::SaveString(std::wstring const& val) + bool Data::SaveString(String const& val) { BOOL ret = ::WritePrivateProfileStringW( field_.c_str(), @@ -134,7 +133,7 @@ namespace easy2d return nValue == TRUE; } - std::wstring Data::GetString() + String Data::GetString() { wchar_t temp[256] = { 0 }; ::GetPrivateProfileStringW( diff --git a/core/utils/Data.h b/core/utils/Data.h index b035e885..1f7e48dc 100644 --- a/core/utils/Data.h +++ b/core/utils/Data.h @@ -19,7 +19,7 @@ // THE SOFTWARE. #pragma once -#include +#include "../base/helper.hpp" namespace easy2d { @@ -28,8 +28,8 @@ namespace easy2d { public: Data( - std::wstring const& key, /* 键值 */ - std::wstring const& field = L"Defalut" /* 字段名称 */ + String const& key, /* 键值 */ + String const& field = L"Defalut" /* 字段名称 */ ); // 该数据是否存在 @@ -55,9 +55,9 @@ namespace easy2d bool val ); - // 保存 std::wstring 类型的值 + // 保存 String 类型的值 bool SaveString( - std::wstring const& val + String const& val ); // 获取 int 类型的值 @@ -73,11 +73,11 @@ namespace easy2d bool GetBool() const; // 获取 字符串 类型的值 - std::wstring GetString(); + String GetString(); protected: - std::wstring key_; - std::wstring field_; - std::wstring const& data_path_; + String key_; + String field_; + String const& data_path_; }; } diff --git a/core/utils/File.cpp b/core/utils/File.cpp index 1fbe47fc..7b98d547 100644 --- a/core/utils/File.cpp +++ b/core/utils/File.cpp @@ -24,14 +24,14 @@ namespace easy2d { - std::list File::search_paths_; + std::list File::search_paths_; File::File() : file_path_() { } - File::File(std::wstring const& file_name) + File::File(String const& file_name) : file_path_(file_name) { this->Open(file_name); @@ -41,12 +41,12 @@ namespace easy2d { } - bool File::Open(std::wstring const& file_name) + bool File::Open(String const& file_name) { if (file_name.empty()) return false; - auto FindFile = [](std::wstring const& path) -> bool + auto FindFile = [](String const& path) -> bool { if (modules::Shlwapi().PathFileExistsW(path.c_str())) return true; @@ -77,16 +77,16 @@ namespace easy2d return false; } - std::wstring const& File::GetPath() const + String const& File::GetPath() const { return file_path_; } - std::wstring File::GetExtension() const + String File::GetExtension() const { - std::wstring file_ext; + String file_ext; size_t pos = file_path_.find_last_of(L'.'); - if (pos != std::wstring::npos) + if (pos != String::npos) { file_ext = file_path_.substr(pos); std::transform(file_ext.begin(), file_ext.end(), file_ext.begin(), std::towlower); @@ -101,7 +101,7 @@ namespace easy2d return false; } - File File::Extract(Resource& res, std::wstring const& dest_file_name) + File File::Extract(Resource& res, String const& dest_file_name) { File file; HANDLE file_handle = ::CreateFile( @@ -117,12 +117,13 @@ namespace easy2d if (file_handle == INVALID_HANDLE_VALUE) return file; - ResourceData buffer; - if (res.Load(&buffer)) + LPVOID buffer; + DWORD buffer_size; + if (res.Load(buffer, buffer_size)) { // 写入文件 DWORD written_bytes = 0; - ::WriteFile(file_handle, buffer.buffer, buffer.buffer_size, &written_bytes, NULL); + ::WriteFile(file_handle, buffer, buffer_size, &written_bytes, NULL); ::CloseHandle(file_handle); file.Open(dest_file_name); @@ -136,11 +137,11 @@ namespace easy2d return file; } - void File::AddSearchPath(std::wstring const& path) + void File::AddSearchPath(String const& path) { - std::wstring tmp = path; + String tmp = path; size_t pos = 0; - while ((pos = tmp.find(L"/", pos)) != std::wstring::npos) + while ((pos = tmp.find(L"/", pos)) != String::npos) { tmp.replace(pos, 1, L"\\"); pos++; diff --git a/core/utils/File.h b/core/utils/File.h index 2fbb8d33..394e1220 100644 --- a/core/utils/File.h +++ b/core/utils/File.h @@ -19,9 +19,8 @@ // THE SOFTWARE. #pragma once +#include "../base/helper.hpp" #include "../base/Resource.h" -#include -#include namespace easy2d { @@ -32,14 +31,14 @@ namespace easy2d File(); File( - std::wstring const& file_name + String const& file_name ); virtual ~File(); // 打开文件 bool Open( - std::wstring const& file_name + String const& file_name ); // 文件是否存在 @@ -49,25 +48,25 @@ namespace easy2d bool Delete(); // 获取文件路径 - std::wstring const& GetPath() const; + String const& GetPath() const; // 获取文件扩展名 - std::wstring GetExtension() const; + String GetExtension() const; // 释放资源到临时文件目录 static File Extract( - Resource& res, /* 资源 */ - std::wstring const& dest_file_name /* 目标文件名 */ + Resource& res, /* 资源 */ + String const& dest_file_name /* 目标文件名 */ ); // 添加文件搜索路径 static void AddSearchPath( - std::wstring const& path + String const& path ); protected: - std::wstring file_path_; + String file_path_; - static std::list search_paths_; + static List search_paths_; }; } diff --git a/core/utils/Path.cpp b/core/utils/Path.cpp index b067fc46..8b7b29ce 100644 --- a/core/utils/Path.cpp +++ b/core/utils/Path.cpp @@ -28,7 +28,7 @@ namespace easy2d namespace { // 创建指定文件夹 - bool CreateFolder(std::wstring const& dir_path) + bool CreateFolder(String const& dir_path) { if (dir_path.empty() || dir_path.size() >= MAX_PATH) return false; @@ -55,15 +55,15 @@ namespace easy2d } - std::wstring const& Path::GetDataPath() + String const& Path::GetDataPath() { - static std::wstring data_path; + static String data_path; if (data_path.empty()) { // 设置数据的保存路径 - std::wstring local_app_data_path = Path::GetLocalAppDataPath(); - std::wstring title = Window::Instance()->GetTitle(); - std::wstring folder_name = std::to_wstring(std::hash{}(title)); + String local_app_data_path = Path::GetLocalAppDataPath(); + String title = Window::Instance()->GetTitle(); + String folder_name = std::to_wstring(std::hash{}(title)); if (!local_app_data_path.empty()) { @@ -83,15 +83,15 @@ namespace easy2d return data_path; } - std::wstring const& Path::GetTemporaryPath() + String const& Path::GetTemporaryPath() { - static std::wstring temp_path; + static String temp_path; if (temp_path.empty()) { // 设置临时文件保存路径 wchar_t path[_MAX_PATH]; - std::wstring title = Window::Instance()->GetTitle(); - std::wstring folder_name = std::to_wstring(std::hash{}(title)); + String title = Window::Instance()->GetTitle(); + String folder_name = std::to_wstring(std::hash{}(title)); if (0 != ::GetTempPath(_MAX_PATH, path)) { @@ -110,9 +110,9 @@ namespace easy2d return temp_path; } - std::wstring const& Path::GetLocalAppDataPath() + String const& Path::GetLocalAppDataPath() { - static std::wstring local_app_data_path; + static String local_app_data_path; if (local_app_data_path.empty()) { // 获取 AppData/Local 文件夹的路径 @@ -124,9 +124,9 @@ namespace easy2d return local_app_data_path; } - std::wstring const& Path::GetExeFilePath() + String const& Path::GetExeFilePath() { - static std::wstring exe_file_path; + static String exe_file_path; if (exe_file_path.empty()) { TCHAR path[_MAX_PATH] = { 0 }; diff --git a/core/utils/Path.h b/core/utils/Path.h index e0b3a630..7eef6747 100644 --- a/core/utils/Path.h +++ b/core/utils/Path.h @@ -19,7 +19,7 @@ // THE SOFTWARE. #pragma once -#include +#include "../base/helper.hpp" namespace easy2d { @@ -28,15 +28,15 @@ namespace easy2d { public: // 获取数据的默认保存路径 - static std::wstring const& GetDataPath(); + static String const& GetDataPath(); // 获取临时文件目录 - static std::wstring const& GetTemporaryPath(); + static String const& GetTemporaryPath(); // 获取 LocalAppData 目录 - static std::wstring const& GetLocalAppDataPath(); + static String const& GetLocalAppDataPath(); // 获取当前程序的运行路径 - static std::wstring const& GetExeFilePath(); + static String const& GetExeFilePath(); }; } diff --git a/core/utils/Player.cpp b/core/utils/Player.cpp index 3743a6d5..36f2ba18 100644 --- a/core/utils/Player.cpp +++ b/core/utils/Player.cpp @@ -33,84 +33,6 @@ namespace easy2d ClearCache(); } - bool Player::Load(std::wstring const& file_path) - { - if (file_path.empty()) - return false; - - SpMusic music = new (std::nothrow) Music(); - - if (music) - { - if (music->Load(file_path)) - { - music->SetVolume(volume_); - - size_t hash_code = std::hash{}(file_path); - musics_cache_.insert(std::make_pair(hash_code, music)); - return true; - } - } - return false; - } - - bool Player::Play(std::wstring const& file_path, int loop_count) - { - if (file_path.empty()) - return false; - - if (Load(file_path)) - { - auto music = musics_cache_[std::hash{}(file_path)]; - if (music->Play(loop_count)) - { - return true; - } - } - return false; - } - - void Player::Pause(std::wstring const& file_path) - { - if (file_path.empty()) - return; - - size_t hash_code = std::hash{}(file_path); - if (musics_cache_.end() != musics_cache_.find(hash_code)) - musics_cache_[hash_code]->Pause(); - } - - void Player::Resume(std::wstring const& file_path) - { - if (file_path.empty()) - return; - - size_t hash_code = std::hash{}(file_path); - if (musics_cache_.end() != musics_cache_.find(hash_code)) - musics_cache_[hash_code]->Resume(); - } - - void Player::Stop(std::wstring const& file_path) - { - if (file_path.empty()) - return; - - size_t hash_code = std::hash{}(file_path); - if (musics_cache_.end() != musics_cache_.find(hash_code)) - musics_cache_[hash_code]->Stop(); - } - - bool Player::IsPlaying(std::wstring const& file_path) - { - if (file_path.empty()) - return false; - - size_t hash_code = std::hash{}(file_path); - if (musics_cache_.end() != musics_cache_.find(hash_code)) - return musics_cache_[hash_code]->IsPlaying(); - return false; - } - bool Player::Load(Resource const& res) { size_t hash_code = res.GetHashCode(); diff --git a/core/utils/Player.h b/core/utils/Player.h index f218b255..396aa345 100644 --- a/core/utils/Player.h +++ b/core/utils/Player.h @@ -29,7 +29,7 @@ namespace easy2d class Player : protected Noncopyable { - using MusicMap = std::unordered_map; + using MusicMap = Map; public: Player(); @@ -38,38 +38,7 @@ namespace easy2d // 预加载音乐资源 bool Load( - std::wstring const& file_path /* 音乐文件路径 */ - ); - - // 播放音乐 - bool Play( - std::wstring const& file_path, /* 音乐文件路径 */ - int loop_count = 0 /* 播放循环次数 (-1 为循环播放) */ - ); - - // 暂停音乐 - void Pause( - std::wstring const& file_path /* 音乐文件路径 */ - ); - - // 继续播放音乐 - void Resume( - std::wstring const& file_path /* 音乐文件路径 */ - ); - - // 停止音乐 - void Stop( - std::wstring const& file_path /* 音乐文件路径 */ - ); - - // 获取音乐播放状态 - bool IsPlaying( - std::wstring const& file_path /* 音乐文件路径 */ - ); - - // 预加载音乐资源 - bool Load( - Resource const& res /* 音乐资源 */ + Resource const& res /* 音乐资源 */ ); // 播放音乐 diff --git a/core/utils/Transcoder.cpp b/core/utils/Transcoder.cpp index e3f54b9c..bd097269 100644 --- a/core/utils/Transcoder.cpp +++ b/core/utils/Transcoder.cpp @@ -76,12 +76,13 @@ namespace easy2d SmartPointer byte_stream; SmartPointer reader; - ResourceData buffer; - if (!res.Load(&buffer)) { return false; } + LPVOID buffer; + DWORD buffer_size; + if (!res.Load(buffer, buffer_size)) { return false; } stream = modules::Shlwapi{}.SHCreateMemStream( - static_cast(buffer.buffer), - static_cast(buffer.buffer_size) + static_cast(buffer), + static_cast(buffer_size) ); if (stream == nullptr)