diff --git a/core/base/Game.cpp b/core/base/Game.cpp index d6ef244b..95d2d44c 100644 --- a/core/base/Game.cpp +++ b/core/base/Game.cpp @@ -55,9 +55,12 @@ namespace easy2d void Game::Initialize(const Options& options) { - Window::Instance().Initialize(options.title, options.width, options.height, options.icon, options.debug); - devices::Graphics::Instance().Initialize(Window::Instance().GetHandle()); - devices::Audio::Instance().Initialize(); + debug_mode_ = options.debug; + + Window::Instance().Initialize(options.title, options.width, options.height, options.icon, debug_mode_); + devices::Graphics::Instance().Initialize(Window::Instance().GetHandle(), debug_mode_); + devices::Input::Instance().Initialize(debug_mode_); + devices::Audio::Instance().Initialize(debug_mode_); // 若开启了调试模式,打开控制台 HWND console = ::GetConsoleWindow(); @@ -250,7 +253,8 @@ namespace easy2d void Game::DrawScene() { - devices::Graphics::Instance().BeginDraw(Window::Instance().GetHandle()); + auto& graphics = devices::Graphics::Instance(); + graphics.BeginDraw(Window::Instance().GetHandle()); if (transition_) { @@ -265,25 +269,20 @@ namespace easy2d { if (curr_scene_ && curr_scene_->GetRoot()) { - devices::Graphics::Instance().SetTransform(math::Matrix()); - devices::Graphics::Instance().SetBrushOpacity(1.f); + graphics.SetTransform(math::Matrix()); + graphics.SetBrushOpacity(1.f); curr_scene_->GetRoot()->DrawBorder(); } if (next_scene_ && next_scene_->GetRoot()) { - devices::Graphics::Instance().SetTransform(math::Matrix()); - devices::Graphics::Instance().SetBrushOpacity(1.f); + graphics.SetTransform(math::Matrix()); + graphics.SetBrushOpacity(1.f); next_scene_->GetRoot()->DrawBorder(); } - devices::Graphics::Instance().DrawDebugInfo(); + graphics.DrawDebugInfo(); } - devices::Graphics::Instance().EndDraw(); - } - - void Game::SetDebugMode(bool enabled) - { - debug_mode_ = enabled; + graphics.EndDraw(); } } \ No newline at end of file diff --git a/core/base/Game.h b/core/base/Game.h index 270654e4..5741968d 100644 --- a/core/base/Game.h +++ b/core/base/Game.h @@ -75,11 +75,6 @@ namespace easy2d // 结束 void Quit(); - // 调试模式 - void SetDebugMode( - bool enabled - ); - // 切换场景 void EnterScene( Scene * scene, /* 场景 */ diff --git a/core/base/Input.cpp b/core/base/Input.cpp index 9c850f24..e6666ce4 100644 --- a/core/base/Input.cpp +++ b/core/base/Input.cpp @@ -25,6 +25,7 @@ namespace easy2d namespace devices { InputDevice::InputDevice() + : initialized(false) { ZeroMemory(keys_, sizeof(keys_)); } @@ -33,6 +34,14 @@ namespace easy2d { } + void InputDevice::Initialize(bool debug) + { + if (initialized) + return; + + initialized = true; + } + void InputDevice::Update(HWND hwnd, float scale_x, float scale_y) { ::GetKeyboardState(keys_); diff --git a/core/base/Input.h b/core/base/Input.h index 08e7e420..b098b4d4 100644 --- a/core/base/Input.h +++ b/core/base/Input.h @@ -33,9 +33,7 @@ namespace easy2d E2D_DISABLE_COPY(InputDevice); public: - InputDevice(); - - ~InputDevice(); + void Initialize(bool debug); // 检测键盘某按键是否正被按下 bool IsDown( @@ -64,6 +62,12 @@ namespace easy2d ); protected: + InputDevice(); + + ~InputDevice(); + + protected: + bool initialized; BYTE keys_[256]; Point mouse_pos_; }; diff --git a/core/base/Singleton.hpp b/core/base/Singleton.hpp index e74c8bca..13317800 100644 --- a/core/base/Singleton.hpp +++ b/core/base/Singleton.hpp @@ -29,8 +29,6 @@ namespace easy2d public: static inline T& Instance(); - static inline void Destroy(); - private: ISingleton() {} @@ -39,27 +37,16 @@ namespace easy2d ISingleton(const ISingleton&) = delete; ISingleton & operator= (const ISingleton &) = delete; - - static std::unique_ptr instance_; }; template inline T & easy2d::ISingleton::Instance() { + static std::unique_ptr instance_; if (!instance_) instance_.reset(new (std::nothrow) T); return *instance_; } - - template - inline void easy2d::ISingleton::Destroy() - { - if (instance_) - instance_.reset(); - } - - template - std::unique_ptr easy2d::ISingleton::instance_; } // Class that will implement the singleton mode, diff --git a/core/base/audio.cpp b/core/base/audio.cpp index eacdecbe..07d026b6 100644 --- a/core/base/audio.cpp +++ b/core/base/audio.cpp @@ -163,6 +163,7 @@ namespace easy2d AudioDevice::AudioDevice() : x_audio2_(nullptr) , mastering_voice_(nullptr) + , initialized(false) { modules::Initialize(); } @@ -184,8 +185,11 @@ namespace easy2d modules::Destroy(); } - void AudioDevice::Initialize() + void AudioDevice::Initialize(bool debug) { + if (initialized) + return; + ThrowIfFailed( modules::MediaFoundation.MFStartup(MF_VERSION, MFSTARTUP_FULL) ); @@ -197,6 +201,8 @@ namespace easy2d ThrowIfFailed( x_audio2_->CreateMasteringVoice(&mastering_voice_) ); + + initialized = true; } HRESULT AudioDevice::CreateVoice(Voice* voice, WAVEFORMATEX * wfx) diff --git a/core/base/audio.h b/core/base/audio.h index 8fcddf41..e761e1c8 100644 --- a/core/base/audio.h +++ b/core/base/audio.h @@ -81,7 +81,7 @@ namespace easy2d E2D_DISABLE_COPY(AudioDevice); public: - void Initialize(); + void Initialize(bool debug); // 开启设备 void Open(); @@ -106,6 +106,7 @@ namespace easy2d ~AudioDevice(); protected: + bool initialized; IXAudio2 * x_audio2_; IXAudio2MasteringVoice* mastering_voice_; std::list voice_cache_; diff --git a/core/base/render.cpp b/core/base/render.cpp index 6cbb10f4..77fe26b9 100644 --- a/core/base/render.cpp +++ b/core/base/render.cpp @@ -25,7 +25,6 @@ #pragma comment(lib, "d2d1.lib") #pragma comment(lib, "dwrite.lib") -#pragma comment(lib, "windowscodecs.lib") namespace easy2d { @@ -35,6 +34,7 @@ namespace easy2d : fps_text_format_(nullptr) , fps_text_layout_(nullptr) , clear_color_(D2D1::ColorF(D2D1::ColorF::Black)) + , initialized(false) { ZeroMemory(&d2d, sizeof(D2DResources)); @@ -63,15 +63,18 @@ namespace easy2d modules::Destroy(); } - void GraphicsDevice::Initialize(HWND hwnd) + void GraphicsDevice::Initialize(HWND hwnd, bool debug) { - if (d2d.factory) + if (initialized) return; + D2D1_FACTORY_OPTIONS options{ debug ? D2D1_DEBUG_LEVEL_INFORMATION : D2D1_DEBUG_LEVEL_NONE }; ThrowIfFailed( D2D1CreateFactory( D2D1_FACTORY_TYPE_SINGLE_THREADED, - &d2d.factory + __uuidof(ID2D1Factory), + &options, + reinterpret_cast(&d2d.factory) ) ); @@ -80,7 +83,7 @@ namespace easy2d CLSID_WICImagingFactory, nullptr, CLSCTX_INPROC_SERVER, - IID_IWICImagingFactory, + __uuidof(IWICImagingFactory), reinterpret_cast(&d2d.imaging_factory) ) ); @@ -135,6 +138,8 @@ namespace easy2d ); CreateDeviceResources(hwnd); + + initialized = true; } void GraphicsDevice::BeginDraw(HWND hwnd) diff --git a/core/base/render.h b/core/base/render.h index 4ff7d778..9dcb8709 100644 --- a/core/base/render.h +++ b/core/base/render.h @@ -52,7 +52,7 @@ namespace easy2d E2D_DISABLE_COPY(GraphicsDevice); public: - void Initialize(HWND hwnd); + void Initialize(HWND hwnd, bool debug); // 开始渲染 void BeginDraw(HWND hwnd); @@ -168,6 +168,7 @@ namespace easy2d ~GraphicsDevice(); protected: + bool initialized; D2DResources d2d; D2D1_COLOR_F clear_color_; IDWriteTextFormat* fps_text_format_; diff --git a/core/base/window.cpp b/core/base/window.cpp index 464a4cd2..468e6d43 100644 --- a/core/base/window.cpp +++ b/core/base/window.cpp @@ -42,21 +42,25 @@ namespace easy2d LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param); } - WindowInfo::WindowInfo() + WindowImpl::WindowImpl() : handle(nullptr) , scale_x(1.f) , scale_y(1.f) + , initialized(false) { } - WindowInfo::~WindowInfo() + WindowImpl::~WindowImpl() { if (handle) ::DestroyWindow(handle); } - void WindowInfo::Initialize(String title, int width, int height, LPCWSTR icon, bool debug) + void WindowImpl::Initialize(String title, int width, int height, LPCWSTR icon, bool debug) { + if (initialized) + return; + HINSTANCE hinstance = GetModuleHandle(nullptr); WNDCLASSEX wcex = { 0 }; wcex.cbSize = sizeof(WNDCLASSEX); @@ -114,9 +118,11 @@ namespace easy2d // 禁用输入法 ::ImmAssociateContext(handle, nullptr); + + initialized = true; } - String WindowInfo::GetTitle() const + String WindowImpl::GetTitle() const { if (handle) { @@ -127,13 +133,13 @@ namespace easy2d return String(); } - void WindowInfo::SetTitle(const String& title) + void WindowImpl::SetTitle(const String& title) { if (handle) ::SetWindowText(handle, title.c_str()); } - Size WindowInfo::GetSize() const + Size WindowImpl::GetSize() const { if (handle) { @@ -147,17 +153,17 @@ namespace easy2d return Size(); } - float WindowInfo::GetWidth() const + float WindowImpl::GetWidth() const { return GetSize().width; } - float WindowInfo::GetHeight() const + float WindowImpl::GetHeight() const { return GetSize().height; } - void WindowInfo::SetSize(int width, int height) + void WindowImpl::SetSize(int width, int height) { if (handle) { @@ -173,7 +179,7 @@ namespace easy2d } } - void WindowInfo::SetIcon(LPCWSTR icon_resource) + void WindowImpl::SetIcon(LPCWSTR icon_resource) { if (handle) { @@ -192,17 +198,17 @@ namespace easy2d } } - HWND WindowInfo::GetHandle() const + HWND WindowImpl::GetHandle() const { return handle; } - float WindowInfo::GetContentScaleX() const + float WindowImpl::GetContentScaleX() const { return scale_x; } - float WindowInfo::GetContentScaleY() const + float WindowImpl::GetContentScaleY() const { return scale_y; } diff --git a/core/base/window.h b/core/base/window.h index 299bef20..5f9663a2 100644 --- a/core/base/window.h +++ b/core/base/window.h @@ -26,11 +26,11 @@ namespace easy2d { - class WindowInfo + class WindowImpl { - E2D_DECLARE_SINGLETON(WindowInfo); + E2D_DECLARE_SINGLETON(WindowImpl); - E2D_DISABLE_COPY(WindowInfo); + E2D_DISABLE_COPY(WindowImpl); public: void Initialize( @@ -69,15 +69,16 @@ namespace easy2d float GetContentScaleY() const; protected: - WindowInfo(); + WindowImpl(); - ~WindowInfo(); + ~WindowImpl(); private: + bool initialized; HWND handle; float scale_x; float scale_y; }; - E2D_DECLARE_SINGLETON_TYPE(WindowInfo, Window); + E2D_DECLARE_SINGLETON_TYPE(WindowImpl, Window); }