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_)
|
||||
);
|
||||
|
||||
Setup();
|
||||
OnStart();
|
||||
|
||||
// disable imm
|
||||
::ImmAssociateContext(hwnd, nullptr);
|
||||
|
|
@ -416,8 +416,13 @@ namespace easy2d
|
|||
break;
|
||||
|
||||
case WM_CLOSE:
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
||||
|
|
|
|||
|
|
@ -41,64 +41,131 @@ namespace easy2d
|
|||
}
|
||||
}
|
||||
|
||||
void ResLoader::AddImage(String const& id, Resource const& image)
|
||||
bool ResLoader::AddImage(String const& id, Resource const& image)
|
||||
{
|
||||
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());
|
||||
res_.insert(std::make_pair(id, ptr));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void ResLoader::AddImage(String const & id, ImagePtr const & image)
|
||||
bool ResLoader::AddImage(String const & id, ImagePtr const & image)
|
||||
{
|
||||
if (image)
|
||||
{
|
||||
res_.insert(std::make_pair(id, image));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void ResLoader::AddFrames(String const& id, Array<Resource> const& images)
|
||||
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)
|
||||
{
|
||||
FramesPtr frames = new Frames;
|
||||
for (const auto& image : images)
|
||||
{
|
||||
String path = Search(image.GetFileName(), search_paths_);
|
||||
ImagePtr ptr = new Image(path.c_str());
|
||||
ImagePtr ptr = new (std::nothrow) Image;
|
||||
if (ptr && ptr->Load(FindRes(image)))
|
||||
{
|
||||
frames->Add(ptr);
|
||||
++total;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (total)
|
||||
res_.insert(std::make_pair(id, frames));
|
||||
return total;
|
||||
}
|
||||
|
||||
void ResLoader::AddFrames(String const& id, Array<ImagePtr> const& images)
|
||||
int ResLoader::AddFrames(String const& id, Array<ImagePtr> const& images)
|
||||
{
|
||||
if (images.empty())
|
||||
return 0;
|
||||
|
||||
int total = 0;
|
||||
FramesPtr frames = new (std::nothrow) Frames;
|
||||
if (frames)
|
||||
{
|
||||
FramesPtr frames = new 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::AddFrames(String const& id, Array<std::pair<Resource, Rect>> const& images)
|
||||
int ResLoader::AddFrames(String const& id, Array<std::pair<Resource, Rect>> const& images)
|
||||
{
|
||||
if (images.empty())
|
||||
return 0;
|
||||
|
||||
int total = 0;
|
||||
FramesPtr frames = new (std::nothrow) Frames;
|
||||
if (frames)
|
||||
{
|
||||
FramesPtr frames = new Frames;
|
||||
for (const auto& pair : images)
|
||||
{
|
||||
String path = Search(pair.first.GetFileName(), search_paths_);
|
||||
ImagePtr image = new Image(path.c_str());
|
||||
ImagePtr ptr = new (std::nothrow) Image;
|
||||
if (ptr && ptr->Load(FindRes(pair.first)))
|
||||
{
|
||||
if (!pair.second.IsEmpty())
|
||||
{
|
||||
image->Crop(pair.second);
|
||||
ptr->Crop(pair.second);
|
||||
}
|
||||
frames->Add(image);
|
||||
frames->Add(ptr);
|
||||
++total;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (total)
|
||||
res_.insert(std::make_pair(id, frames));
|
||||
return total;
|
||||
}
|
||||
|
||||
void ResLoader::AddMusic(String const & id, Resource const & music)
|
||||
bool ResLoader::AddMusic(String const & id, Resource const & music)
|
||||
{
|
||||
MusicPtr ptr = new (std::nothrow) Music;
|
||||
if (ptr && ptr->Load(FindRes(music)))
|
||||
{
|
||||
String path = Search(music.GetFileName(), search_paths_);
|
||||
MusicPtr ptr = new Music(path.c_str());
|
||||
res_.insert(std::make_pair(id, ptr));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void ResLoader::AddObj(String const& id, ObjectPtr const& obj)
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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<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;
|
||||
|
||||
|
|
@ -58,6 +60,8 @@ namespace easy2d
|
|||
String const& path
|
||||
);
|
||||
|
||||
Resource FindRes(Resource const& res) const;
|
||||
|
||||
template<typename T>
|
||||
auto Get(String const& id) const -> decltype(auto)
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue