update ResLoader

This commit is contained in:
Nomango 2019-01-30 21:20:23 +08:00 committed by Nomango
parent 084fc4aff7
commit 5957e75924
4 changed files with 123 additions and 30 deletions

View File

@ -84,6 +84,11 @@ namespace easy2d
return true; return true;
} }
bool Image::IsValid() const
{
return !!bitmap_;
}
void Image::Crop(Rect const& crop_rect) void Image::Crop(Rect const& crop_rect)
{ {
if (bitmap_) if (bitmap_)

View File

@ -51,6 +51,9 @@ namespace easy2d
Resource const& res Resource const& res
); );
// 资源是否有效
bool IsValid() const;
// 将图片裁剪为矩形 // 将图片裁剪为矩形
void Crop( void Crop(
Rect const& crop_rect /* 裁剪矩形 */ Rect const& crop_rect /* 裁剪矩形 */

View File

@ -44,11 +44,22 @@ namespace easy2d
bool ResLoader::AddImage(String const& id, Resource const& image) bool ResLoader::AddImage(String const& id, Resource const& image)
{ {
ImagePtr ptr = new (std::nothrow) Image; ImagePtr ptr = new (std::nothrow) Image;
if (ptr && ptr->Load(FindRes(image))) if (ptr)
{
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)); res_.insert(std::make_pair(id, ptr));
return true; return true;
} }
}
return false; return false;
} }
@ -74,13 +85,24 @@ namespace easy2d
for (const auto& image : images) for (const auto& image : images)
{ {
ImagePtr ptr = new (std::nothrow) Image; ImagePtr ptr = new (std::nothrow) Image;
if (ptr && ptr->Load(FindRes(image))) if (ptr)
{
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); frames->Add(ptr);
++total; ++total;
} }
} }
} }
}
if (total) if (total)
res_.insert(std::make_pair(id, frames)); res_.insert(std::make_pair(id, frames));
return total; return total;
@ -109,24 +131,59 @@ namespace easy2d
return total; return total;
} }
int ResLoader::AddFrames(String const& id, Array<std::pair<Resource, Rect>> 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; return 0;
int total = 0; int total = 0;
FramesPtr frames = new (std::nothrow) Frames; FramesPtr frames = new (std::nothrow) Frames;
if (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; for (int j = 0; j < cols; j++)
if (ptr && ptr->Load(FindRes(pair.first)))
{ {
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<Rect> 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); frames->Add(ptr);
++total; ++total;
} }
@ -137,14 +194,36 @@ namespace easy2d
return total; 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) bool ResLoader::AddMusic(String const & id, Resource const & music)
{ {
MusicPtr ptr = new (std::nothrow) Music; MusicPtr ptr = new (std::nothrow) Music;
if (ptr && ptr->Load(FindRes(music))) if (ptr)
{
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)); res_.insert(std::make_pair(id, ptr));
return true; return true;
} }
}
return false; 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;
}
} }

View File

@ -27,20 +27,36 @@ namespace easy2d
class ResLoader class ResLoader
{ {
public: public:
// 添加图片
bool AddImage(String const& id, Resource const& image); bool AddImage(String const& id, Resource const& image);
// 添加图片
bool AddImage(String const& id, ImagePtr const& image); bool AddImage(String const& id, ImagePtr const& image);
// 添加帧集合
int AddFrames(String const& id, Array<Resource> const& images); int AddFrames(String const& id, Array<Resource> const& images);
int AddFrames(String const& id, Array<Pair<Resource, Rect>> const& images); // 添加帧集合
int AddFrames(String const& id, Array<ImagePtr> const& images); int AddFrames(String const& id, Array<ImagePtr> const& images);
// 添加帧集合
// 按行列数裁剪图片
int AddFrames(String const& id, Resource const& image, int cols, int rows = 1);
// 添加帧集合
// 按指定裁剪矩形裁剪图片
int AddFrames(String const& id, Resource const& image, Array<Rect> const& crop_rects);
// 添加帧集合
bool AddFrames(String const& id, FramesPtr const& frames);
// 添加音乐
bool AddMusic(String const& id, Resource const& music); bool AddMusic(String const& id, Resource const& music);
// 添加音乐
bool AddMusic(String const& id, MusicPtr const& music); bool AddMusic(String const& id, MusicPtr const& music);
// 添加对象
bool AddObj(String const& id, ObjectPtr const& obj); bool AddObj(String const& id, ObjectPtr const& obj);
ImagePtr GetImage(String const& id) const; ImagePtr GetImage(String const& id) const;
@ -51,8 +67,10 @@ namespace easy2d
ObjectPtr GetObj(String const& id) const; ObjectPtr GetObj(String const& id) const;
// 删除指定资源
void Delete(String const& id); void Delete(String const& id);
// 销毁所有资源
void Destroy(); void Destroy();
// Ìí¼Ó×ÊÔ´ËÑË÷·¾¶ // Ìí¼Ó×ÊÔ´ËÑË÷·¾¶
@ -60,8 +78,6 @@ namespace easy2d
String const& path String const& path
); );
Resource FindRes(Resource const& res) const;
template<typename T> template<typename T>
auto Get(String const& id) const -> decltype(auto) auto Get(String const& id) const -> decltype(auto)
{ {