From 1e16f9d81536f4d59a527cef1fcc11cbc07964b7 Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Sun, 27 Jan 2019 09:07:51 +0800 Subject: [PATCH] add: virtual functions on application starts & destroys minor fixes minor fixes minor --- src/core/Application.cpp | 11 ++- src/core/Application.h | 12 ++- src/utils/ResLoader.cpp | 161 +++++++++++++++++++++++++++++---------- src/utils/ResLoader.h | 18 +++-- 4 files changed, 148 insertions(+), 54 deletions(-) diff --git a/src/core/Application.cpp b/src/core/Application.cpp index 2e46feae..a26529df 100644 --- a/src/core/Application.cpp +++ b/src/core/Application.cpp @@ -90,7 +90,7 @@ namespace easy2d Audio::Instance()->Init(debug_) ); - Setup(); + OnStart(); // disable imm ::ImmAssociateContext(hwnd, nullptr); @@ -417,7 +417,12 @@ namespace easy2d case WM_CLOSE: { - Window::Instance()->Destroy(); + E2D_LOG(L"Window is closing"); + + if (app->OnClosing()) + { + Window::Instance()->Destroy(); + } return 0; } break; @@ -432,6 +437,8 @@ namespace easy2d app->curr_scene_->Dispatch(evt); } + app->OnDestroy(); + ::PostQuitMessage(0); return 0; } diff --git a/src/core/Application.h b/src/core/Application.h index 995120f3..8e388a48 100644 --- a/src/core/Application.h +++ b/src/core/Application.h @@ -60,14 +60,20 @@ namespace easy2d virtual ~Application(); - // 启动 - virtual void Setup() {} - // 初始化 void Init( Options const& options ); + // 启动时 + virtual void OnStart() {} + + // 关闭时 + virtual bool OnClosing() { return true; } + + // 销毁时 + virtual void OnDestroy() {} + // 运行 void Run(); diff --git a/src/utils/ResLoader.cpp b/src/utils/ResLoader.cpp index d8d15c40..9408c157 100644 --- a/src/utils/ResLoader.cpp +++ b/src/utils/ResLoader.cpp @@ -41,64 +41,131 @@ namespace easy2d } } - void ResLoader::AddImage(String const& id, Resource const& image) + bool ResLoader::AddImage(String const& id, Resource const& image) { - String path = Search(image.GetFileName(), search_paths_); - ImagePtr ptr = new Image(path.c_str()); - res_.insert(std::make_pair(id, ptr)); - } - - void ResLoader::AddImage(String const & id, ImagePtr const & image) - { - res_.insert(std::make_pair(id, image)); - } - - void ResLoader::AddFrames(String const& id, Array const& images) - { - FramesPtr frames = new Frames; - for (const auto& image : images) + ImagePtr ptr = new (std::nothrow) Image; + if (ptr && ptr->Load(FindRes(image))) { - String path = Search(image.GetFileName(), search_paths_); - ImagePtr ptr = new Image(path.c_str()); - frames->Add(ptr); + res_.insert(std::make_pair(id, ptr)); + return true; } - res_.insert(std::make_pair(id, frames)); + return false; } - void ResLoader::AddFrames(String const& id, Array const& images) + bool ResLoader::AddImage(String const & id, ImagePtr const & image) { - FramesPtr frames = new Frames; - for (const auto& image : images) - frames->Add(image); - res_.insert(std::make_pair(id, frames)); - } - - void ResLoader::AddFrames(String const& id, Array> const& images) - { - FramesPtr frames = new Frames; - for (const auto& pair : images) + if (image) { - String path = Search(pair.first.GetFileName(), search_paths_); - ImagePtr image = new Image(path.c_str()); - if (!pair.second.IsEmpty()) + res_.insert(std::make_pair(id, image)); + return true; + } + return false; + } + + int ResLoader::AddFrames(String const& id, Array const& images) + { + if (images.empty()) + return 0; + + int total = 0; + FramesPtr frames = new (std::nothrow) Frames; + if (frames) + { + for (const auto& image : images) { - image->Crop(pair.second); + ImagePtr ptr = new (std::nothrow) Image; + if (ptr && ptr->Load(FindRes(image))) + { + frames->Add(ptr); + ++total; + } } - frames->Add(image); } - res_.insert(std::make_pair(id, frames)); + if (total) + res_.insert(std::make_pair(id, frames)); + return total; } - void ResLoader::AddMusic(String const & id, Resource const & music) + int ResLoader::AddFrames(String const& id, Array const& images) { - String path = Search(music.GetFileName(), search_paths_); - MusicPtr ptr = new Music(path.c_str()); - res_.insert(std::make_pair(id, ptr)); + if (images.empty()) + return 0; + + int total = 0; + FramesPtr frames = new (std::nothrow) Frames; + if (frames) + { + for (const auto& image : images) + { + if (image) + { + frames->Add(image); + total++; + } + } + } + if (total) + res_.insert(std::make_pair(id, frames)); + return total; } - void ResLoader::AddObj(String const& id, ObjectPtr const& obj) + int ResLoader::AddFrames(String const& id, Array> const& images) { - res_.insert(std::make_pair(id, obj)); + if (images.empty()) + return 0; + + int total = 0; + FramesPtr frames = new (std::nothrow) Frames; + if (frames) + { + for (const auto& pair : images) + { + ImagePtr ptr = new (std::nothrow) Image; + if (ptr && ptr->Load(FindRes(pair.first))) + { + if (!pair.second.IsEmpty()) + { + ptr->Crop(pair.second); + } + frames->Add(ptr); + ++total; + } + } + } + if (total) + res_.insert(std::make_pair(id, frames)); + return total; + } + + bool ResLoader::AddMusic(String const & id, Resource const & music) + { + MusicPtr ptr = new (std::nothrow) Music; + if (ptr && ptr->Load(FindRes(music))) + { + res_.insert(std::make_pair(id, ptr)); + return true; + } + return false; + } + + bool ResLoader::AddMusic(String const & id, MusicPtr const & music) + { + if (music) + { + res_.insert(std::make_pair(id, music)); + return true; + } + return false; + } + + bool ResLoader::AddObj(String const& id, ObjectPtr const& obj) + { + if (obj) + { + res_.insert(std::make_pair(id, obj)); + return true; + } + return false; } ImagePtr ResLoader::GetImage(String const & id) const @@ -153,4 +220,14 @@ namespace easy2d } } + Resource ResLoader::FindRes(Resource const & res) const + { + if (res.IsFile()) + { + String path = Search(res.GetFileName(), search_paths_); + return Resource(path.c_str()); + } + return res; + } + } \ No newline at end of file diff --git a/src/utils/ResLoader.h b/src/utils/ResLoader.h index f15685e5..28277c0c 100644 --- a/src/utils/ResLoader.h +++ b/src/utils/ResLoader.h @@ -27,19 +27,21 @@ namespace easy2d class ResLoader { public: - void AddImage(String const& id, Resource const& image); + bool AddImage(String const& id, Resource const& image); - void AddImage(String const& id, ImagePtr const& image); + bool AddImage(String const& id, ImagePtr const& image); - void AddFrames(String const& id, Array const& images); + int AddFrames(String const& id, Array const& images); - void AddFrames(String const& id, Array> const& images); + int AddFrames(String const& id, Array> const& images); - void AddFrames(String const& id, Array const& images); + int AddFrames(String const& id, Array const& images); - void AddMusic(String const& id, Resource const& music); + bool AddMusic(String const& id, Resource const& music); - void AddObj(String const& id, ObjectPtr const& obj); + bool AddMusic(String const& id, MusicPtr const& music); + + bool AddObj(String const& id, ObjectPtr const& obj); ImagePtr GetImage(String const& id) const; @@ -58,6 +60,8 @@ namespace easy2d String const& path ); + Resource FindRes(Resource const& res) const; + template auto Get(String const& id) const -> decltype(auto) {