bgfx/examples/common/entry/entry_winrt.cpp

217 lines
4.7 KiB
C++
Raw Normal View History

2014-11-14 16:23:31 +03:00
/*
2016-01-01 11:11:04 +03:00
* Copyright 2011-2016 Branimir Karadzic. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
2014-11-14 16:23:31 +03:00
*/
#include "entry_p.h"
2016-04-09 03:30:35 +03:00
#if BX_PLATFORM_WINRT || BX_PLATFORM_XBOXONE
2014-11-14 16:23:31 +03:00
#include <bgfx/bgfxplatform.h>
2014-11-14 16:23:31 +03:00
#include <bx/thread.h>
using namespace Windows::ApplicationModel;
using namespace Windows::ApplicationModel::Core;
using namespace Windows::ApplicationModel::Activation;
using namespace Windows::UI::Core;
using namespace Windows::UI::Input;
using namespace Windows::System;
using namespace Windows::Foundation;
2016-04-09 03:30:35 +03:00
#if BX_PLATFORM_WINRT
2014-11-14 16:23:31 +03:00
using namespace Windows::Graphics::Display;
2016-04-09 03:30:35 +03:00
#endif // BX_PLATFORM_WINRT
2014-11-14 16:23:31 +03:00
using namespace Platform;
static char* g_emptyArgs[] = { "" };
static entry::WindowHandle g_defaultWindow = { 0 };
static entry::EventQueue g_eventQueue;
ref class App sealed : public IFrameworkView
{
2015-04-18 06:12:45 +03:00
public:
App()
: m_windowVisible(true)
, m_windowClosed(false)
2014-11-16 02:48:24 +03:00
{
}
2014-11-14 16:23:31 +03:00
2014-11-16 02:48:24 +03:00
// IFrameworkView Methods.
2014-11-14 16:23:31 +03:00
virtual void Initialize(CoreApplicationView^ applicationView)
2014-11-16 02:48:24 +03:00
{
2016-04-09 03:30:35 +03:00
applicationView->Activated += ref new
TypedEventHandler<CoreApplicationView^, IActivatedEventArgs^>(this, &App::OnActivated);
CoreApplication::Suspending += ref new
EventHandler<SuspendingEventArgs^>(this, &App::OnSuspending);
CoreApplication::Resuming += ref new
EventHandler<Platform::Object^>(this, &App::OnResuming);
2014-11-16 02:48:24 +03:00
}
2014-11-14 16:23:31 +03:00
virtual void SetWindow(CoreWindow^ window)
2014-11-16 02:48:24 +03:00
{
2016-04-09 03:30:35 +03:00
window->VisibilityChanged += ref new
TypedEventHandler<CoreWindow^, VisibilityChangedEventArgs^>(this, &App::OnVisibilityChanged);
window->Closed += ref new
TypedEventHandler<CoreWindow^, CoreWindowEventArgs^>(this, &App::OnWindowClosed);
2014-11-14 16:23:31 +03:00
2015-07-17 06:28:43 +03:00
bgfx::winrtSetWindow(reinterpret_cast<IUnknown*>(window) );
2014-11-16 02:48:24 +03:00
}
2014-11-14 16:23:31 +03:00
virtual void Load(String^ entryPoint)
2014-11-16 02:48:24 +03:00
{
}
2014-11-14 16:23:31 +03:00
virtual void Run()
2014-11-16 02:48:24 +03:00
{
2014-11-14 16:23:31 +03:00
bx::Thread thread;
thread.init(MainThreadFunc, nullptr);
2014-11-16 02:48:24 +03:00
CoreWindow^ window = CoreWindow::GetForCurrentThread();
2015-04-18 06:12:45 +03:00
auto bounds = window->Bounds;
2014-11-16 02:48:24 +03:00
2016-04-09 03:30:35 +03:00
#if BX_PLATFORM_WINRT
auto dpi = DisplayInformation::GetForCurrentView()->LogicalDpi;
2015-04-18 06:12:45 +03:00
static const float dipsPerInch = 96.0f;
g_eventQueue.postSizeEvent(g_defaultWindow
2016-04-09 03:30:35 +03:00
, lround(bx::ffloor(bounds.Width * dpi / dipsPerInch + 0.5f) )
, lround(bx::ffloor(bounds.Height * dpi / dipsPerInch + 0.5f) )
2015-04-18 06:12:45 +03:00
);
2016-04-09 03:30:35 +03:00
#endif // BX_PLATFORM_WINRT
2014-11-16 02:48:24 +03:00
while (!m_windowClosed)
{
if (m_windowVisible)
{
window->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
}
else
{
window->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneAndAllPending);
}
}
g_eventQueue.postExitEvent();
thread.shutdown();
}
2014-11-14 16:23:31 +03:00
virtual void Uninitialize()
2014-11-16 02:48:24 +03:00
{
}
2014-11-14 16:23:31 +03:00
private:
2014-11-16 02:48:24 +03:00
bool m_windowVisible;
bool m_windowClosed;
2014-11-14 16:23:31 +03:00
2014-11-16 02:48:24 +03:00
void OnActivated(CoreApplicationView^ applicationView, IActivatedEventArgs^ args)
{
CoreWindow::GetForCurrentThread()->Activate();
}
2014-11-14 16:23:31 +03:00
2014-11-16 02:48:24 +03:00
void OnVisibilityChanged(CoreWindow^ sender, VisibilityChangedEventArgs^ args)
{
m_windowVisible = args->Visible;
}
2014-11-14 16:23:31 +03:00
2016-04-18 19:35:53 +03:00
void OnSuspending(Platform::Object^ sender, SuspendingEventArgs^ args)
{
SuspendingDeferral^ deferral = args->SuspendingOperation->GetDeferral();
2016-04-09 03:30:35 +03:00
BX_UNUSED(deferral);
2016-04-18 19:35:53 +03:00
}
2016-04-09 03:30:35 +03:00
2016-04-18 19:35:53 +03:00
void OnResuming(Platform::Object^ sender, Platform::Object^ args)
{
}
2016-04-09 03:30:35 +03:00
2014-11-14 16:23:31 +03:00
void OnWindowClosed(CoreWindow^ sender, CoreWindowEventArgs^ args)
2014-11-16 02:48:24 +03:00
{
m_windowClosed = true;
}
static int32_t MainThreadFunc(void*)
{
return entry::main(0, g_emptyArgs);
}
2014-11-14 16:23:31 +03:00
};
ref class AppSource sealed : IFrameworkViewSource
{
public:
virtual IFrameworkView^ CreateView()
2014-11-16 02:48:24 +03:00
{
return ref new App();
}
2014-11-14 16:23:31 +03:00
};
namespace entry
{
2014-11-16 02:48:24 +03:00
const Event* poll()
2014-11-14 16:23:31 +03:00
{
return g_eventQueue.poll();
}
const Event* poll(WindowHandle _handle)
{
return g_eventQueue.poll(_handle);
}
void release(const Event* _event)
{
g_eventQueue.release(_event);
}
2014-11-16 02:48:24 +03:00
WindowHandle createWindow(int32_t _x, int32_t _y, uint32_t _width, uint32_t _height, uint32_t _flags, const char* _title)
2014-11-14 16:23:31 +03:00
{
BX_UNUSED(_x, _y, _width, _height, _flags, _title);
WindowHandle handle = { UINT16_MAX };
return handle;
}
void destroyWindow(WindowHandle _handle)
{
BX_UNUSED(_handle);
}
void setWindowPos(WindowHandle _handle, int32_t _x, int32_t _y)
{
BX_UNUSED(_handle, _x, _y);
}
void setWindowSize(WindowHandle _handle, uint32_t _width, uint32_t _height)
{
BX_UNUSED(_handle, _width, _height);
}
void setWindowTitle(WindowHandle _handle, const char* _title)
{
BX_UNUSED(_handle, _title);
}
void toggleWindowFrame(WindowHandle _handle)
{
BX_UNUSED(_handle);
}
void toggleFullscreen(WindowHandle _handle)
{
BX_UNUSED(_handle);
}
2014-11-14 16:23:31 +03:00
void setMouseLock(WindowHandle _handle, bool _lock)
{
BX_UNUSED(_handle, _lock);
}
}
[MTAThread]
int main(Array<String^>^)
{
2014-11-16 02:48:24 +03:00
auto appSource = ref new AppSource();
CoreApplication::Run(appSource);
return 0;
2014-11-14 16:23:31 +03:00
}
2016-04-09 03:30:35 +03:00
#endif // BX_PLATFORM_WINRT || BX_PLATFORM_XBOXONE