add: virtual functions on application starts & destroys
minor fixes minor fixes minor
This commit is contained in:
parent
63afe9bf39
commit
1e16f9d815
|
|
@ -90,7 +90,7 @@ namespace easy2d
|
||||||
Audio::Instance()->Init(debug_)
|
Audio::Instance()->Init(debug_)
|
||||||
);
|
);
|
||||||
|
|
||||||
Setup();
|
OnStart();
|
||||||
|
|
||||||
// disable imm
|
// disable imm
|
||||||
::ImmAssociateContext(hwnd, nullptr);
|
::ImmAssociateContext(hwnd, nullptr);
|
||||||
|
|
@ -417,7 +417,12 @@ namespace easy2d
|
||||||
|
|
||||||
case WM_CLOSE:
|
case WM_CLOSE:
|
||||||
{
|
{
|
||||||
Window::Instance()->Destroy();
|
E2D_LOG(L"Window is closing");
|
||||||
|
|
||||||
|
if (app->OnClosing())
|
||||||
|
{
|
||||||
|
Window::Instance()->Destroy();
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
@ -432,6 +437,8 @@ namespace easy2d
|
||||||
app->curr_scene_->Dispatch(evt);
|
app->curr_scene_->Dispatch(evt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
app->OnDestroy();
|
||||||
|
|
||||||
::PostQuitMessage(0);
|
::PostQuitMessage(0);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -60,14 +60,20 @@ namespace easy2d
|
||||||
|
|
||||||
virtual ~Application();
|
virtual ~Application();
|
||||||
|
|
||||||
// 启动
|
|
||||||
virtual void Setup() {}
|
|
||||||
|
|
||||||
// 初始化
|
// 初始化
|
||||||
void Init(
|
void Init(
|
||||||
Options const& options
|
Options const& options
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// 启动时
|
||||||
|
virtual void OnStart() {}
|
||||||
|
|
||||||
|
// 关闭时
|
||||||
|
virtual bool OnClosing() { return true; }
|
||||||
|
|
||||||
|
// 销毁时
|
||||||
|
virtual void OnDestroy() {}
|
||||||
|
|
||||||
// 运行
|
// 运行
|
||||||
void Run();
|
void Run();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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 (std::nothrow) Image;
|
||||||
ImagePtr ptr = new Image(path.c_str());
|
if (ptr && ptr->Load(FindRes(image)))
|
||||||
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<Resource> const& images)
|
|
||||||
{
|
|
||||||
FramesPtr frames = new Frames;
|
|
||||||
for (const auto& image : images)
|
|
||||||
{
|
{
|
||||||
String path = Search(image.GetFileName(), search_paths_);
|
res_.insert(std::make_pair(id, ptr));
|
||||||
ImagePtr ptr = new Image(path.c_str());
|
return true;
|
||||||
frames->Add(ptr);
|
|
||||||
}
|
}
|
||||||
res_.insert(std::make_pair(id, frames));
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ResLoader::AddFrames(String const& id, Array<ImagePtr> const& images)
|
bool ResLoader::AddImage(String const & id, ImagePtr const & image)
|
||||||
{
|
{
|
||||||
FramesPtr frames = new Frames;
|
if (image)
|
||||||
for (const auto& image : images)
|
|
||||||
frames->Add(image);
|
|
||||||
res_.insert(std::make_pair(id, frames));
|
|
||||||
}
|
|
||||||
|
|
||||||
void ResLoader::AddFrames(String const& id, Array<std::pair<Resource, Rect>> const& images)
|
|
||||||
{
|
|
||||||
FramesPtr frames = new Frames;
|
|
||||||
for (const auto& pair : images)
|
|
||||||
{
|
{
|
||||||
String path = Search(pair.first.GetFileName(), search_paths_);
|
res_.insert(std::make_pair(id, image));
|
||||||
ImagePtr image = new Image(path.c_str());
|
return true;
|
||||||
if (!pair.second.IsEmpty())
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ResLoader::AddFrames(String const& id, Array<Resource> 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<ImagePtr> const& images)
|
||||||
{
|
{
|
||||||
String path = Search(music.GetFileName(), search_paths_);
|
if (images.empty())
|
||||||
MusicPtr ptr = new Music(path.c_str());
|
return 0;
|
||||||
res_.insert(std::make_pair(id, ptr));
|
|
||||||
|
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<std::pair<Resource, Rect>> 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
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -27,19 +27,21 @@ namespace easy2d
|
||||||
class ResLoader
|
class ResLoader
|
||||||
{
|
{
|
||||||
public:
|
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<Resource> const& images);
|
int AddFrames(String const& id, Array<Resource> const& images);
|
||||||
|
|
||||||
void AddFrames(String const& id, Array<Pair<Resource, Rect>> const& images);
|
int AddFrames(String const& id, Array<Pair<Resource, Rect>> const& images);
|
||||||
|
|
||||||
void AddFrames(String const& id, Array<ImagePtr> const& images);
|
int AddFrames(String const& id, Array<ImagePtr> 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;
|
ImagePtr GetImage(String const& id) const;
|
||||||
|
|
||||||
|
|
@ -58,6 +60,8 @@ 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)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue