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)
{
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();
}
}

View File

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

View File

@ -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_);

View File

@ -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_;
};

View File

@ -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<T> instance_;
};
template<typename T>
inline T & easy2d::ISingleton<T>::Instance()
{
static std::unique_ptr<T> instance_;
if (!instance_)
instance_.reset(new (std::nothrow) T);
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,

View File

@ -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)

View File

@ -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*> voice_cache_;

View File

@ -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<void**>(&d2d.factory)
)
);
@ -80,7 +83,7 @@ namespace easy2d
CLSID_WICImagingFactory,
nullptr,
CLSCTX_INPROC_SERVER,
IID_IWICImagingFactory,
__uuidof(IWICImagingFactory),
reinterpret_cast<void**>(&d2d.imaging_factory)
)
);
@ -135,6 +138,8 @@ namespace easy2d
);
CreateDeviceResources(hwnd);
initialized = true;
}
void GraphicsDevice::BeginDraw(HWND hwnd)

View File

@ -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_;

View File

@ -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;
}

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:
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);
}