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
{
MemoryAllocator* current_allocator_ = GetGlobalAllocator();
MemoryAllocator* current_allocator_ = nullptr;
MemoryAllocator* GetAllocator()
{
if (!current_allocator_)
{
current_allocator_ = GetGlobalAllocator();
}
return current_allocator_;
}

View File

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

View File

@ -77,11 +77,10 @@ public:
* @param height
* @param icon ID
* @param resizable
* @param fullscreen
* @throw kiwano::SystemError
*/
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
@ -117,6 +116,12 @@ public:
*/
WindowHandle GetHandle() const;
/**
* \~chinese
* @brief
*/
virtual Vector<Resolution> GetResolutions() = 0;
/**
* \~chinese
* @brief
@ -133,11 +138,12 @@ public:
/**
* \~chinese
* @brief
* @param width
* @param height
* @brief
* @param width
* @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
@ -162,21 +168,6 @@ public:
*/
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
* @brief

View File

@ -47,14 +47,12 @@ public:
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 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 SetMaximumSize(uint32_t width, uint32_t height) override;
@ -86,13 +84,12 @@ private:
std::array<KeyCode, 256> key_map_;
};
WindowPtr Window::Create(const String& title, uint32_t width, uint32_t height, uint32_t icon, bool resizable,
bool fullscreen)
WindowPtr Window::Create(const String& title, uint32_t width, uint32_t height, uint32_t icon, bool resizable)
{
WindowWin32ImplPtr ptr = memory::New<WindowWin32Impl>();
if (ptr)
{
ptr->Init(title, width, height, icon, resizable, fullscreen);
ptr->Init(title, width, height, icon, resizable);
}
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,
bool fullscreen)
void WindowWin32Impl::Init(const String& title, uint32_t width, uint32_t height, uint32_t icon, bool resizable)
{
HINSTANCE hinst = GetModuleHandle(nullptr);
WNDCLASSEXA wcex = { 0 };
@ -236,38 +232,22 @@ void WindowWin32Impl::Init(const String& title, uint32_t width, uint32_t height,
// Save the device name
device_name_ = monitor_info_ex.szDevice;
int left = -1, top = -1;
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;
if (fullscreen)
{
top = monitor_info_ex.rcMonitor.top;
left = monitor_info_ex.rcMonitor.left;
uint32_t win_width, win_height;
AdjustWindow(width, height, GetStyle(), &win_width, &win_height);
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 screenh = monitor_info_ex.rcWork.bottom - monitor_info_ex.rcWork.top;
uint32_t win_width, win_height;
AdjustWindow(width, height, GetStyle(), &win_width, &win_height);
left = monitor_info_ex.rcWork.left + (screenw - win_width) / 2;
top = monitor_info_ex.rcWork.top + (screenh - win_height) / 2;
width = win_width;
height = win_height;
}
int left = monitor_info_ex.rcWork.left + (screenw - win_width) / 2;
int top = monitor_info_ex.rcWork.top + (screenh - win_height) / 2;
width = win_width;
height = win_height;
width_ = width;
height_ = height;
resizable_ = resizable;
handle_ = ::CreateWindowExA(fullscreen ? WS_EX_TOPMOST : 0, "KiwanoAppWnd", title.c_str(), GetStyle(), left, top,
width, height, nullptr, nullptr, hinst, nullptr);
handle_ = ::CreateWindowExA(0, "KiwanoAppWnd", title.c_str(), GetStyle(), left, top, width, height, nullptr,
nullptr, hinst, nullptr);
if (handle_ == nullptr)
{
@ -283,6 +263,21 @@ void WindowWin32Impl::Init(const String& title, uint32_t width, uint32_t height,
::ShowWindow(handle_, SW_SHOWNORMAL);
::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()
@ -313,25 +308,6 @@ void WindowWin32Impl::SetIcon(uint32_t icon_resource)
::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)
{
min_width_ = width;

View File

@ -58,75 +58,51 @@ void RendererImpl::MakeContextForWindow(WindowPtr window)
HWND target_window = window->GetHandle();
output_size_ = window->GetSize();
d2d_res_ = nullptr;
d3d_res_ = nullptr;
d2d_res_ = graphics::directx::GetD2DDeviceResources();
d3d_res_ = graphics::directx::GetD3DDeviceResources();
HRESULT hr = target_window ? S_OK : E_FAIL;
// Direct3D device resources
// Initialize other device resources
if (SUCCEEDED(hr))
{
auto d3d_res = graphics::directx::GetD3DDeviceResources();
RenderContextImplPtr ctx = memory::New<RenderContextImpl>();
// Initialize Direct3D resources
hr = d3d_res->Initialize(target_window);
// Direct2D device resources
hr = ctx->CreateDeviceResources(d2d_res_->GetFactory(), d2d_res_->GetDeviceContext());
if (SUCCEEDED(hr))
{
d3d_res_ = d3d_res;
render_ctx_ = ctx;
}
}
auto d2d_res = graphics::directx::GetD2DDeviceResources();
// FontFileLoader and FontCollectionLoader
if (SUCCEEDED(hr))
{
hr = IFontCollectionLoader::Create(&font_collection_loader_);
// Initialize Direct2D resources
hr = d2d_res->Initialize(d3d_res_->GetDXGIDevice(), d3d_res_->GetDXGISwapChain());
if (SUCCEEDED(hr))
{
hr = d2d_res_->GetDWriteFactory()->RegisterFontCollectionLoader(font_collection_loader_.Get());
}
}
// ResourceFontFileLoader and ResourceFontCollectionLoader
if (SUCCEEDED(hr))
{
hr = IResourceFontFileLoader::Create(&res_font_file_loader_);
if (SUCCEEDED(hr))
{
hr = d2d_res_->GetDWriteFactory()->RegisterFontFileLoader(res_font_file_loader_.Get());
}
if (SUCCEEDED(hr))
{
hr = IResourceFontCollectionLoader::Create(&res_font_collection_loader_, res_font_file_loader_.Get());
if (SUCCEEDED(hr))
{
d2d_res_ = d2d_res;
// Initialize other device resources
RenderContextImplPtr ctx = memory::New<RenderContextImpl>();
hr = ctx->CreateDeviceResources(d2d_res_->GetFactory(), d2d_res_->GetDeviceContext());
if (SUCCEEDED(hr))
{
render_ctx_ = ctx;
}
}
// FontFileLoader and FontCollectionLoader
if (SUCCEEDED(hr))
{
hr = IFontCollectionLoader::Create(&font_collection_loader_);
if (SUCCEEDED(hr))
{
hr = d2d_res_->GetDWriteFactory()->RegisterFontCollectionLoader(font_collection_loader_.Get());
}
}
// ResourceFontFileLoader and ResourceFontCollectionLoader
if (SUCCEEDED(hr))
{
hr = IResourceFontFileLoader::Create(&res_font_file_loader_);
if (SUCCEEDED(hr))
{
hr = d2d_res_->GetDWriteFactory()->RegisterFontFileLoader(res_font_file_loader_.Get());
}
if (SUCCEEDED(hr))
{
hr = IResourceFontCollectionLoader::Create(&res_font_collection_loader_,
res_font_file_loader_.Get());
if (SUCCEEDED(hr))
{
hr = d2d_res_->GetDWriteFactory()->RegisterFontCollectionLoader(
res_font_collection_loader_.Get());
}
}
hr = d2d_res_->GetDWriteFactory()->RegisterFontCollectionLoader(res_font_collection_loader_.Get());
}
}
}

View File

@ -78,6 +78,20 @@ bool Ticker::Tick(Duration dt)
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()
{
return delta_time_;

View File

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