update: debug mode can be turned on during initialization

This commit is contained in:
Haibo 2018-11-12 22:36:50 +08:00 committed by Nomango
parent d2532c09db
commit ab2757375d
11 changed files with 78 additions and 64 deletions

View File

@ -55,9 +55,12 @@ namespace easy2d
void Game::Initialize(const Options& options) void Game::Initialize(const Options& options)
{ {
Window::Instance().Initialize(options.title, options.width, options.height, options.icon, options.debug); debug_mode_ = options.debug;
devices::Graphics::Instance().Initialize(Window::Instance().GetHandle());
devices::Audio::Instance().Initialize(); 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(); HWND console = ::GetConsoleWindow();
@ -250,7 +253,8 @@ namespace easy2d
void Game::DrawScene() void Game::DrawScene()
{ {
devices::Graphics::Instance().BeginDraw(Window::Instance().GetHandle()); auto& graphics = devices::Graphics::Instance();
graphics.BeginDraw(Window::Instance().GetHandle());
if (transition_) if (transition_)
{ {
@ -265,25 +269,20 @@ namespace easy2d
{ {
if (curr_scene_ && curr_scene_->GetRoot()) if (curr_scene_ && curr_scene_->GetRoot())
{ {
devices::Graphics::Instance().SetTransform(math::Matrix()); graphics.SetTransform(math::Matrix());
devices::Graphics::Instance().SetBrushOpacity(1.f); graphics.SetBrushOpacity(1.f);
curr_scene_->GetRoot()->DrawBorder(); curr_scene_->GetRoot()->DrawBorder();
} }
if (next_scene_ && next_scene_->GetRoot()) if (next_scene_ && next_scene_->GetRoot())
{ {
devices::Graphics::Instance().SetTransform(math::Matrix()); graphics.SetTransform(math::Matrix());
devices::Graphics::Instance().SetBrushOpacity(1.f); graphics.SetBrushOpacity(1.f);
next_scene_->GetRoot()->DrawBorder(); next_scene_->GetRoot()->DrawBorder();
} }
devices::Graphics::Instance().DrawDebugInfo(); graphics.DrawDebugInfo();
} }
devices::Graphics::Instance().EndDraw(); graphics.EndDraw();
}
void Game::SetDebugMode(bool enabled)
{
debug_mode_ = enabled;
} }
} }

View File

@ -75,11 +75,6 @@ namespace easy2d
// ½áÊø // ½áÊø
void Quit(); void Quit();
// µ÷ÊÔģʽ
void SetDebugMode(
bool enabled
);
// Çл»³¡¾° // Çл»³¡¾°
void EnterScene( void EnterScene(
Scene * scene, /* ³¡¾° */ Scene * scene, /* ³¡¾° */

View File

@ -25,6 +25,7 @@ namespace easy2d
namespace devices namespace devices
{ {
InputDevice::InputDevice() InputDevice::InputDevice()
: initialized(false)
{ {
ZeroMemory(keys_, sizeof(keys_)); 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) void InputDevice::Update(HWND hwnd, float scale_x, float scale_y)
{ {
::GetKeyboardState(keys_); ::GetKeyboardState(keys_);

View File

@ -33,9 +33,7 @@ namespace easy2d
E2D_DISABLE_COPY(InputDevice); E2D_DISABLE_COPY(InputDevice);
public: public:
InputDevice(); void Initialize(bool debug);
~InputDevice();
// 检测键盘某按键是否正被按下 // 检测键盘某按键是否正被按下
bool IsDown( bool IsDown(
@ -64,6 +62,12 @@ namespace easy2d
); );
protected: protected:
InputDevice();
~InputDevice();
protected:
bool initialized;
BYTE keys_[256]; BYTE keys_[256];
Point mouse_pos_; Point mouse_pos_;
}; };

View File

@ -29,8 +29,6 @@ namespace easy2d
public: public:
static inline T& Instance(); static inline T& Instance();
static inline void Destroy();
private: private:
ISingleton() {} ISingleton() {}
@ -39,27 +37,16 @@ namespace easy2d
ISingleton(const ISingleton&) = delete; ISingleton(const ISingleton&) = delete;
ISingleton & operator= (const ISingleton &) = delete; ISingleton & operator= (const ISingleton &) = delete;
static std::unique_ptr<T> instance_;
}; };
template<typename T> template<typename T>
inline T & easy2d::ISingleton<T>::Instance() inline T & easy2d::ISingleton<T>::Instance()
{ {
static std::unique_ptr<T> instance_;
if (!instance_) if (!instance_)
instance_.reset(new (std::nothrow) T); instance_.reset(new (std::nothrow) T);
return *instance_; return *instance_;
} }
template<typename T>
inline void easy2d::ISingleton<T>::Destroy()
{
if (instance_)
instance_.reset();
}
template<typename T>
std::unique_ptr<T> easy2d::ISingleton<T>::instance_;
} }
// Class that will implement the singleton mode, // Class that will implement the singleton mode,

View File

@ -163,6 +163,7 @@ namespace easy2d
AudioDevice::AudioDevice() AudioDevice::AudioDevice()
: x_audio2_(nullptr) : x_audio2_(nullptr)
, mastering_voice_(nullptr) , mastering_voice_(nullptr)
, initialized(false)
{ {
modules::Initialize(); modules::Initialize();
} }
@ -184,8 +185,11 @@ namespace easy2d
modules::Destroy(); modules::Destroy();
} }
void AudioDevice::Initialize() void AudioDevice::Initialize(bool debug)
{ {
if (initialized)
return;
ThrowIfFailed( ThrowIfFailed(
modules::MediaFoundation.MFStartup(MF_VERSION, MFSTARTUP_FULL) modules::MediaFoundation.MFStartup(MF_VERSION, MFSTARTUP_FULL)
); );
@ -197,6 +201,8 @@ namespace easy2d
ThrowIfFailed( ThrowIfFailed(
x_audio2_->CreateMasteringVoice(&mastering_voice_) x_audio2_->CreateMasteringVoice(&mastering_voice_)
); );
initialized = true;
} }
HRESULT AudioDevice::CreateVoice(Voice* voice, WAVEFORMATEX * wfx) HRESULT AudioDevice::CreateVoice(Voice* voice, WAVEFORMATEX * wfx)

View File

@ -81,7 +81,7 @@ namespace easy2d
E2D_DISABLE_COPY(AudioDevice); E2D_DISABLE_COPY(AudioDevice);
public: public:
void Initialize(); void Initialize(bool debug);
// 开启设备 // 开启设备
void Open(); void Open();
@ -106,6 +106,7 @@ namespace easy2d
~AudioDevice(); ~AudioDevice();
protected: protected:
bool initialized;
IXAudio2 * x_audio2_; IXAudio2 * x_audio2_;
IXAudio2MasteringVoice* mastering_voice_; IXAudio2MasteringVoice* mastering_voice_;
std::list<Voice*> voice_cache_; std::list<Voice*> voice_cache_;

View File

@ -25,7 +25,6 @@
#pragma comment(lib, "d2d1.lib") #pragma comment(lib, "d2d1.lib")
#pragma comment(lib, "dwrite.lib") #pragma comment(lib, "dwrite.lib")
#pragma comment(lib, "windowscodecs.lib")
namespace easy2d namespace easy2d
{ {
@ -35,6 +34,7 @@ namespace easy2d
: fps_text_format_(nullptr) : fps_text_format_(nullptr)
, fps_text_layout_(nullptr) , fps_text_layout_(nullptr)
, clear_color_(D2D1::ColorF(D2D1::ColorF::Black)) , clear_color_(D2D1::ColorF(D2D1::ColorF::Black))
, initialized(false)
{ {
ZeroMemory(&d2d, sizeof(D2DResources)); ZeroMemory(&d2d, sizeof(D2DResources));
@ -63,15 +63,18 @@ namespace easy2d
modules::Destroy(); modules::Destroy();
} }
void GraphicsDevice::Initialize(HWND hwnd) void GraphicsDevice::Initialize(HWND hwnd, bool debug)
{ {
if (d2d.factory) if (initialized)
return; return;
D2D1_FACTORY_OPTIONS options{ debug ? D2D1_DEBUG_LEVEL_INFORMATION : D2D1_DEBUG_LEVEL_NONE };
ThrowIfFailed( ThrowIfFailed(
D2D1CreateFactory( D2D1CreateFactory(
D2D1_FACTORY_TYPE_SINGLE_THREADED, D2D1_FACTORY_TYPE_SINGLE_THREADED,
&d2d.factory __uuidof(ID2D1Factory),
&options,
reinterpret_cast<void**>(&d2d.factory)
) )
); );
@ -80,7 +83,7 @@ namespace easy2d
CLSID_WICImagingFactory, CLSID_WICImagingFactory,
nullptr, nullptr,
CLSCTX_INPROC_SERVER, CLSCTX_INPROC_SERVER,
IID_IWICImagingFactory, __uuidof(IWICImagingFactory),
reinterpret_cast<void**>(&d2d.imaging_factory) reinterpret_cast<void**>(&d2d.imaging_factory)
) )
); );
@ -135,6 +138,8 @@ namespace easy2d
); );
CreateDeviceResources(hwnd); CreateDeviceResources(hwnd);
initialized = true;
} }
void GraphicsDevice::BeginDraw(HWND hwnd) void GraphicsDevice::BeginDraw(HWND hwnd)

View File

@ -52,7 +52,7 @@ namespace easy2d
E2D_DISABLE_COPY(GraphicsDevice); E2D_DISABLE_COPY(GraphicsDevice);
public: public:
void Initialize(HWND hwnd); void Initialize(HWND hwnd, bool debug);
// ¿ªÊ¼äÖȾ // ¿ªÊ¼äÖȾ
void BeginDraw(HWND hwnd); void BeginDraw(HWND hwnd);
@ -168,6 +168,7 @@ namespace easy2d
~GraphicsDevice(); ~GraphicsDevice();
protected: protected:
bool initialized;
D2DResources d2d; D2DResources d2d;
D2D1_COLOR_F clear_color_; D2D1_COLOR_F clear_color_;
IDWriteTextFormat* fps_text_format_; IDWriteTextFormat* fps_text_format_;

View File

@ -42,21 +42,25 @@ namespace easy2d
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param); LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param);
} }
WindowInfo::WindowInfo() WindowImpl::WindowImpl()
: handle(nullptr) : handle(nullptr)
, scale_x(1.f) , scale_x(1.f)
, scale_y(1.f) , scale_y(1.f)
, initialized(false)
{ {
} }
WindowInfo::~WindowInfo() WindowImpl::~WindowImpl()
{ {
if (handle) if (handle)
::DestroyWindow(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); HINSTANCE hinstance = GetModuleHandle(nullptr);
WNDCLASSEX wcex = { 0 }; WNDCLASSEX wcex = { 0 };
wcex.cbSize = sizeof(WNDCLASSEX); wcex.cbSize = sizeof(WNDCLASSEX);
@ -114,9 +118,11 @@ namespace easy2d
// ½ûÓÃÊäÈë·¨ // ½ûÓÃÊäÈë·¨
::ImmAssociateContext(handle, nullptr); ::ImmAssociateContext(handle, nullptr);
initialized = true;
} }
String WindowInfo::GetTitle() const String WindowImpl::GetTitle() const
{ {
if (handle) if (handle)
{ {
@ -127,13 +133,13 @@ namespace easy2d
return String(); return String();
} }
void WindowInfo::SetTitle(const String& title) void WindowImpl::SetTitle(const String& title)
{ {
if (handle) if (handle)
::SetWindowText(handle, title.c_str()); ::SetWindowText(handle, title.c_str());
} }
Size WindowInfo::GetSize() const Size WindowImpl::GetSize() const
{ {
if (handle) if (handle)
{ {
@ -147,17 +153,17 @@ namespace easy2d
return Size(); return Size();
} }
float WindowInfo::GetWidth() const float WindowImpl::GetWidth() const
{ {
return GetSize().width; return GetSize().width;
} }
float WindowInfo::GetHeight() const float WindowImpl::GetHeight() const
{ {
return GetSize().height; return GetSize().height;
} }
void WindowInfo::SetSize(int width, int height) void WindowImpl::SetSize(int width, int height)
{ {
if (handle) if (handle)
{ {
@ -173,7 +179,7 @@ namespace easy2d
} }
} }
void WindowInfo::SetIcon(LPCWSTR icon_resource) void WindowImpl::SetIcon(LPCWSTR icon_resource)
{ {
if (handle) if (handle)
{ {
@ -192,17 +198,17 @@ namespace easy2d
} }
} }
HWND WindowInfo::GetHandle() const HWND WindowImpl::GetHandle() const
{ {
return handle; return handle;
} }
float WindowInfo::GetContentScaleX() const float WindowImpl::GetContentScaleX() const
{ {
return scale_x; return scale_x;
} }
float WindowInfo::GetContentScaleY() const float WindowImpl::GetContentScaleY() const
{ {
return scale_y; return scale_y;
} }

View File

@ -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: public:
void Initialize( void Initialize(
@ -69,15 +69,16 @@ namespace easy2d
float GetContentScaleY() const; float GetContentScaleY() const;
protected: protected:
WindowInfo(); WindowImpl();
~WindowInfo(); ~WindowImpl();
private: private:
bool initialized;
HWND handle; HWND handle;
float scale_x; float scale_x;
float scale_y; float scale_y;
}; };
E2D_DECLARE_SINGLETON_TYPE(WindowInfo, Window); E2D_DECLARE_SINGLETON_TYPE(WindowImpl, Window);
} }