This commit is contained in:
Nomango 2020-05-26 00:12:36 +08:00
parent 1af108df90
commit c82fd84a0d
7 changed files with 95 additions and 141 deletions

View File

@ -25,10 +25,14 @@ namespace kiwano
namespace memory namespace memory
{ {
MemoryAllocator* current_allocator_ = GetGlobalAllocator(); MemoryAllocator* current_allocator_ = nullptr;
MemoryAllocator* GetAllocator() MemoryAllocator* GetAllocator()
{ {
if (!current_allocator_)
{
current_allocator_ = GetGlobalAllocator();
}
return current_allocator_; return current_allocator_;
} }

View File

@ -18,6 +18,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE. // THE SOFTWARE.
#include <kiwano/utils/Logger.h>
#include <kiwano/platform/Runner.h> #include <kiwano/platform/Runner.h>
#include <kiwano/platform/Application.h> #include <kiwano/platform/Application.h>
@ -86,6 +87,8 @@ bool Runner::MainLoop(Duration dt)
Application& app = Application::GetInstance(); Application& app = Application::GetInstance();
KGE_LOG(dt.Milliseconds());
// Update modules before poll events // Update modules before poll events
app.Update(dt); app.Update(dt);

View File

@ -77,11 +77,10 @@ public:
* @param height * @param height
* @param icon ID * @param icon ID
* @param resizable * @param resizable
* @param fullscreen
* @throw kiwano::SystemError * @throw kiwano::SystemError
*/ */
static WindowPtr Create(const String& title, uint32_t width, uint32_t height, uint32_t icon = 0, static WindowPtr Create(const String& title, uint32_t width, uint32_t height, uint32_t icon = 0,
bool resizable = false, bool fullscreen = false); bool resizable = false);
/** /**
* \~chinese * \~chinese
@ -117,6 +116,12 @@ public:
*/ */
WindowHandle GetHandle() const; WindowHandle GetHandle() const;
/**
* \~chinese
* @brief
*/
virtual Vector<Resolution> GetResolutions() = 0;
/** /**
* \~chinese * \~chinese
* @brief * @brief
@ -133,11 +138,12 @@ public:
/** /**
* \~chinese * \~chinese
* @brief * @brief
* @param width * @param width
* @param height * @param height
* @param fullscreen
*/ */
virtual void Resize(uint32_t width, uint32_t height) = 0; virtual void SetResolution(uint32_t width, uint32_t height, bool fullscreen) = 0;
/** /**
* \~chinese * \~chinese
@ -162,21 +168,6 @@ public:
*/ */
virtual void SetCursor(CursorType cursor) = 0; virtual void SetCursor(CursorType cursor) = 0;
/**
* \~chinese
* @brief
* @param width
* @param height
* @param fullscreen
*/
virtual void SetResolution(uint32_t width, uint32_t height, bool fullscreen) = 0;
/**
* \~chinese
* @brief
*/
virtual Vector<Resolution> GetResolutions() = 0;
/** /**
* \~chinese * \~chinese
* @brief * @brief

View File

@ -47,14 +47,12 @@ public:
virtual ~WindowWin32Impl(); virtual ~WindowWin32Impl();
void Init(const String& title, uint32_t width, uint32_t height, uint32_t icon, bool resizable, bool fullscreen); void Init(const String& title, uint32_t width, uint32_t height, uint32_t icon, bool resizable);
void SetTitle(const String& title) override; void SetTitle(const String& title) override;
void SetIcon(uint32_t icon_resource) override; void SetIcon(uint32_t icon_resource) override;
void Resize(uint32_t width, uint32_t height) override;
void SetMinimumSize(uint32_t width, uint32_t height) override; void SetMinimumSize(uint32_t width, uint32_t height) override;
void SetMaximumSize(uint32_t width, uint32_t height) override; void SetMaximumSize(uint32_t width, uint32_t height) override;
@ -86,13 +84,12 @@ private:
std::array<KeyCode, 256> key_map_; std::array<KeyCode, 256> key_map_;
}; };
WindowPtr Window::Create(const String& title, uint32_t width, uint32_t height, uint32_t icon, bool resizable, WindowPtr Window::Create(const String& title, uint32_t width, uint32_t height, uint32_t icon, bool resizable)
bool fullscreen)
{ {
WindowWin32ImplPtr ptr = memory::New<WindowWin32Impl>(); WindowWin32ImplPtr ptr = memory::New<WindowWin32Impl>();
if (ptr) if (ptr)
{ {
ptr->Init(title, width, height, icon, resizable, fullscreen); ptr->Init(title, width, height, icon, resizable);
} }
return ptr; return ptr;
} }
@ -199,8 +196,7 @@ WindowWin32Impl::~WindowWin32Impl()
} }
} }
void WindowWin32Impl::Init(const String& title, uint32_t width, uint32_t height, uint32_t icon, bool resizable, void WindowWin32Impl::Init(const String& title, uint32_t width, uint32_t height, uint32_t icon, bool resizable)
bool fullscreen)
{ {
HINSTANCE hinst = GetModuleHandle(nullptr); HINSTANCE hinst = GetModuleHandle(nullptr);
WNDCLASSEXA wcex = { 0 }; WNDCLASSEXA wcex = { 0 };
@ -236,38 +232,22 @@ void WindowWin32Impl::Init(const String& title, uint32_t width, uint32_t height,
// Save the device name // Save the device name
device_name_ = monitor_info_ex.szDevice; device_name_ = monitor_info_ex.szDevice;
int left = -1, top = -1;
if (fullscreen)
{
top = monitor_info_ex.rcMonitor.top;
left = monitor_info_ex.rcMonitor.left;
if (width > static_cast<uint32_t>(monitor_info_ex.rcWork.right - left))
width = static_cast<uint32_t>(monitor_info_ex.rcWork.right - left);
if (height > static_cast<uint32_t>(monitor_info_ex.rcWork.bottom - top))
height = static_cast<uint32_t>(monitor_info_ex.rcWork.bottom - top);
}
else
{
uint32_t screenw = monitor_info_ex.rcWork.right - monitor_info_ex.rcWork.left; uint32_t screenw = monitor_info_ex.rcWork.right - monitor_info_ex.rcWork.left;
uint32_t screenh = monitor_info_ex.rcWork.bottom - monitor_info_ex.rcWork.top; uint32_t screenh = monitor_info_ex.rcWork.bottom - monitor_info_ex.rcWork.top;
uint32_t win_width, win_height; uint32_t win_width, win_height;
AdjustWindow(width, height, GetStyle(), &win_width, &win_height); AdjustWindow(width, height, GetStyle(), &win_width, &win_height);
left = monitor_info_ex.rcWork.left + (screenw - win_width) / 2; int left = monitor_info_ex.rcWork.left + (screenw - win_width) / 2;
top = monitor_info_ex.rcWork.top + (screenh - win_height) / 2; int top = monitor_info_ex.rcWork.top + (screenh - win_height) / 2;
width = win_width; width = win_width;
height = win_height; height = win_height;
}
width_ = width; width_ = width;
height_ = height; height_ = height;
resizable_ = resizable; resizable_ = resizable;
handle_ = ::CreateWindowExA(fullscreen ? WS_EX_TOPMOST : 0, "KiwanoAppWnd", title.c_str(), GetStyle(), left, top, handle_ = ::CreateWindowExA(0, "KiwanoAppWnd", title.c_str(), GetStyle(), left, top, width, height, nullptr,
width, height, nullptr, nullptr, hinst, nullptr); nullptr, hinst, nullptr);
if (handle_ == nullptr) if (handle_ == nullptr)
{ {
@ -283,6 +263,21 @@ void WindowWin32Impl::Init(const String& title, uint32_t width, uint32_t height,
::ShowWindow(handle_, SW_SHOWNORMAL); ::ShowWindow(handle_, SW_SHOWNORMAL);
::UpdateWindow(handle_); ::UpdateWindow(handle_);
// Initialize Direct3D resources
auto d3d_res = graphics::directx::GetD3DDeviceResources();
HRESULT hr = d3d_res->Initialize(handle_);
// Initialize Direct2D resources
if (SUCCEEDED(hr))
{
auto d2d_res = graphics::directx::GetD2DDeviceResources();
hr = d2d_res->Initialize(d3d_res->GetDXGIDevice(), d3d_res->GetDXGISwapChain());
}
KGE_THROW_IF_FAILED(hr, "Create DirectX resources failed");
} }
void WindowWin32Impl::PumpEvents() void WindowWin32Impl::PumpEvents()
@ -313,25 +308,6 @@ void WindowWin32Impl::SetIcon(uint32_t icon_resource)
::SendMessage(handle_, WM_SETICON, ICON_SMALL, (LPARAM)icon); ::SendMessage(handle_, WM_SETICON, ICON_SMALL, (LPARAM)icon);
} }
void WindowWin32Impl::Resize(uint32_t width, uint32_t height)
{
KGE_ASSERT(handle_);
RECT rc = { 0, 0, LONG(width), LONG(height) };
::AdjustWindowRect(&rc, GetStyle(), false);
width = rc.right - rc.left;
height = rc.bottom - rc.top;
MONITORINFOEXA info = GetMoniterInfoEx(handle_);
uint32_t screenw = uint32_t(info.rcWork.right - info.rcWork.left);
uint32_t screenh = uint32_t(info.rcWork.bottom - info.rcWork.top);
int left = screenw > width ? ((screenw - width) / 2) : 0;
int top = screenh > height ? ((screenh - height) / 2) : 0;
::SetWindowPos(handle_, 0, left, top, width, height, SWP_NOZORDER | SWP_NOACTIVATE);
}
void WindowWin32Impl::SetMinimumSize(uint32_t width, uint32_t height) void WindowWin32Impl::SetMinimumSize(uint32_t width, uint32_t height)
{ {
min_width_ = width; min_width_ = width;

View File

@ -58,34 +58,14 @@ void RendererImpl::MakeContextForWindow(WindowPtr window)
HWND target_window = window->GetHandle(); HWND target_window = window->GetHandle();
output_size_ = window->GetSize(); output_size_ = window->GetSize();
d2d_res_ = nullptr; d2d_res_ = graphics::directx::GetD2DDeviceResources();
d3d_res_ = nullptr; d3d_res_ = graphics::directx::GetD3DDeviceResources();
HRESULT hr = target_window ? S_OK : E_FAIL; HRESULT hr = target_window ? S_OK : E_FAIL;
// Direct3D device resources
if (SUCCEEDED(hr))
{
auto d3d_res = graphics::directx::GetD3DDeviceResources();
// Initialize Direct3D resources
hr = d3d_res->Initialize(target_window);
// Direct2D device resources
if (SUCCEEDED(hr))
{
d3d_res_ = d3d_res;
auto d2d_res = graphics::directx::GetD2DDeviceResources();
// Initialize Direct2D resources
hr = d2d_res->Initialize(d3d_res_->GetDXGIDevice(), d3d_res_->GetDXGISwapChain());
if (SUCCEEDED(hr))
{
d2d_res_ = d2d_res;
// Initialize other device resources // Initialize other device resources
if (SUCCEEDED(hr))
{
RenderContextImplPtr ctx = memory::New<RenderContextImpl>(); RenderContextImplPtr ctx = memory::New<RenderContextImpl>();
hr = ctx->CreateDeviceResources(d2d_res_->GetFactory(), d2d_res_->GetDeviceContext()); hr = ctx->CreateDeviceResources(d2d_res_->GetFactory(), d2d_res_->GetDeviceContext());
@ -118,15 +98,11 @@ void RendererImpl::MakeContextForWindow(WindowPtr window)
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
hr = IResourceFontCollectionLoader::Create(&res_font_collection_loader_, hr = IResourceFontCollectionLoader::Create(&res_font_collection_loader_, res_font_file_loader_.Get());
res_font_file_loader_.Get());
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
hr = d2d_res_->GetDWriteFactory()->RegisterFontCollectionLoader( hr = d2d_res_->GetDWriteFactory()->RegisterFontCollectionLoader(res_font_collection_loader_.Get());
res_font_collection_loader_.Get());
}
}
} }
} }
} }

View File

@ -78,6 +78,20 @@ bool Ticker::Tick(Duration dt)
return false; return false;
} }
void Ticker::Pause()
{
is_paused_ = true;
if (timer_)
timer_->Pause();
}
void Ticker::Resume()
{
is_paused_ = false;
if (timer_)
timer_->Resume();
}
Duration Ticker::GetDeltaTime() Duration Ticker::GetDeltaTime()
{ {
return delta_time_; return delta_time_;

View File

@ -110,16 +110,6 @@ private:
TimerPtr timer_; TimerPtr timer_;
}; };
inline void Ticker::Pause()
{
is_paused_ = true;
}
inline void Ticker::Resume()
{
is_paused_ = false;
}
inline bool Ticker::IsPausing() const inline bool Ticker::IsPausing() const
{ {
return is_paused_; return is_paused_;