From 5957e75924f03a315837926f82f467020a4b5e99 Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Wed, 30 Jan 2019 21:20:23 +0800 Subject: [PATCH] update ResLoader --- src/core/Image.cpp | 5 ++ src/core/Image.h | 3 + src/utils/ResLoader.cpp | 121 +++++++++++++++++++++++++++++++--------- src/utils/ResLoader.h | 24 ++++++-- 4 files changed, 123 insertions(+), 30 deletions(-) diff --git a/src/core/Image.cpp b/src/core/Image.cpp index 5e51c0f0..059acde3 100644 --- a/src/core/Image.cpp +++ b/src/core/Image.cpp @@ -84,6 +84,11 @@ namespace easy2d return true; } + bool Image::IsValid() const + { + return !!bitmap_; + } + void Image::Crop(Rect const& crop_rect) { if (bitmap_) diff --git a/src/core/Image.h b/src/core/Image.h index eea23ae6..b2ba8440 100644 --- a/src/core/Image.h +++ b/src/core/Image.h @@ -51,6 +51,9 @@ namespace easy2d Resource const& res ); + // 资源是否有效 + bool IsValid() const; + // 将图片裁剪为矩形 void Crop( Rect const& crop_rect /* 裁剪矩形 */ diff --git a/src/utils/ResLoader.cpp b/src/utils/ResLoader.cpp index 9408c157..014f6fc5 100644 --- a/src/utils/ResLoader.cpp +++ b/src/utils/ResLoader.cpp @@ -44,10 +44,21 @@ namespace easy2d bool ResLoader::AddImage(String const& id, Resource const& image) { ImagePtr ptr = new (std::nothrow) Image; - if (ptr && ptr->Load(FindRes(image))) + if (ptr) { - res_.insert(std::make_pair(id, ptr)); - return true; + if (image.IsFile()) + { + String path = Search(image.GetFileName(), search_paths_); + ptr->Load(path.c_str()); + } + else + ptr->Load(image); + + if (ptr->IsValid()) + { + res_.insert(std::make_pair(id, ptr)); + return true; + } } return false; } @@ -74,10 +85,21 @@ namespace easy2d for (const auto& image : images) { ImagePtr ptr = new (std::nothrow) Image; - if (ptr && ptr->Load(FindRes(image))) + if (ptr) { - frames->Add(ptr); - ++total; + if (image.IsFile()) + { + String path = Search(image.GetFileName(), search_paths_); + ptr->Load(path.c_str()); + } + else + ptr->Load(image); + + if (ptr->IsValid()) + { + frames->Add(ptr); + ++total; + } } } } @@ -109,24 +131,59 @@ namespace easy2d return total; } - int ResLoader::AddFrames(String const& id, Array> const& images) + int ResLoader::AddFrames(String const & id, Resource const & image, int cols, int rows) { - if (images.empty()) + if (cols <= 0 || rows <= 0) return 0; int total = 0; FramesPtr frames = new (std::nothrow) Frames; if (frames) { - for (const auto& pair : images) + ImagePtr raw = new (std::nothrow) Image; + if (!raw || !raw->Load(image)) + return 0; + + float raw_width = raw->GetSourceWidth(); + float raw_height = raw->GetSourceHeight(); + float width = raw_width / cols; + float height = raw_height / rows; + + for (int i = 0; i < rows; i++) { - ImagePtr ptr = new (std::nothrow) Image; - if (ptr && ptr->Load(FindRes(pair.first))) + for (int j = 0; j < cols; j++) { - if (!pair.second.IsEmpty()) + ImagePtr ptr = new (std::nothrow) Image(raw->GetBitmap()); + if (ptr) { - ptr->Crop(pair.second); + ptr->Crop(Rect{ i * width, j * height, width, height }); + frames->Add(ptr); + ++total; } + } + } + } + if (total) + res_.insert(std::make_pair(id, frames)); + return total; + } + + int ResLoader::AddFrames(String const & id, Resource const & image, Array const & crop_rects) + { + int total = 0; + FramesPtr frames = new (std::nothrow) Frames; + if (frames) + { + ImagePtr raw = new (std::nothrow) Image; + if (!raw || !raw->Load(image)) + return 0; + + for (const auto& rect : crop_rects) + { + ImagePtr ptr = new (std::nothrow) Image(raw->GetBitmap()); + if (ptr) + { + ptr->Crop(rect); frames->Add(ptr); ++total; } @@ -137,13 +194,35 @@ namespace easy2d return total; } + bool ResLoader::AddFrames(String const & id, FramesPtr const & frames) + { + if (frames) + { + res_.insert(std::make_pair(id, frames)); + return true; + } + return false; + } + bool ResLoader::AddMusic(String const & id, Resource const & music) { MusicPtr ptr = new (std::nothrow) Music; - if (ptr && ptr->Load(FindRes(music))) + if (ptr) { - res_.insert(std::make_pair(id, ptr)); - return true; + bool valid = false; + if (music.IsFile()) + { + String path = Search(music.GetFileName(), search_paths_); + valid = ptr->Load(path.c_str()); + } + else + valid = ptr->Load(music); + + if (valid) + { + res_.insert(std::make_pair(id, ptr)); + return true; + } } return false; } @@ -220,14 +299,4 @@ 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 28277c0c..b3ffa98a 100644 --- a/src/utils/ResLoader.h +++ b/src/utils/ResLoader.h @@ -27,20 +27,36 @@ namespace easy2d class ResLoader { public: + // 添加图片 bool AddImage(String const& id, Resource const& image); + // 添加图片 bool AddImage(String const& id, ImagePtr const& image); + // 添加帧集合 int AddFrames(String const& id, Array const& images); - int AddFrames(String const& id, Array> const& images); - + // 添加帧集合 int AddFrames(String const& id, Array const& images); + // 添加帧集合 + // 按行列数裁剪图片 + int AddFrames(String const& id, Resource const& image, int cols, int rows = 1); + + // 添加帧集合 + // 按指定裁剪矩形裁剪图片 + int AddFrames(String const& id, Resource const& image, Array const& crop_rects); + + // 添加帧集合 + bool AddFrames(String const& id, FramesPtr const& frames); + + // 添加音乐 bool AddMusic(String const& id, Resource const& music); + // 添加音乐 bool AddMusic(String const& id, MusicPtr const& music); + // 添加对象 bool AddObj(String const& id, ObjectPtr const& obj); ImagePtr GetImage(String const& id) const; @@ -51,8 +67,10 @@ namespace easy2d ObjectPtr GetObj(String const& id) const; + // 删除指定资源 void Delete(String const& id); + // 销毁所有资源 void Destroy(); // 添加资源搜索路径 @@ -60,8 +78,6 @@ namespace easy2d String const& path ); - Resource FindRes(Resource const& res) const; - template auto Get(String const& id) const -> decltype(auto) {