bgfx/examples/common/entry/entry_winrt.cpp

193 lines
4.1 KiB
C++
Raw Normal View History

2014-11-14 08:23:31 -05:00
/*
2016-01-01 03:11:04 -05:00
* Copyright 2011-2016 Branimir Karadzic. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
2014-11-14 08:23:31 -05:00
*/
#include "entry_p.h"
#if BX_PLATFORM_WINRT
#include <bgfx/bgfxplatform.h>
2014-11-14 08:23:31 -05: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;
using namespace Windows::Graphics::Display;
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-17 23:12:45 -04:00
public:
App()
: m_windowVisible(true)
, m_windowClosed(false)
2014-11-15 18:48:24 -05:00
{
}
2014-11-14 08:23:31 -05:00
2014-11-15 18:48:24 -05:00
// IFrameworkView Methods.
2014-11-14 08:23:31 -05:00
virtual void Initialize(CoreApplicationView^ applicationView)
2014-11-15 18:48:24 -05:00
{
applicationView->Activated += ref new TypedEventHandler<CoreApplicationView^, IActivatedEventArgs^>(this, &App::OnActivated);
}
2014-11-14 08:23:31 -05:00
virtual void SetWindow(CoreWindow^ window)
2014-11-15 18:48:24 -05:00
{
window->VisibilityChanged += ref new TypedEventHandler<CoreWindow^, VisibilityChangedEventArgs^>(this, &App::OnVisibilityChanged);
window->Closed += ref new TypedEventHandler<CoreWindow^, CoreWindowEventArgs^>(this, &App::OnWindowClosed);
2014-11-14 08:23:31 -05:00
2015-07-16 23:28:43 -04:00
bgfx::winrtSetWindow(reinterpret_cast<IUnknown*>(window) );
2014-11-15 18:48:24 -05:00
}
2014-11-14 08:23:31 -05:00
virtual void Load(String^ entryPoint)
2014-11-15 18:48:24 -05:00
{
}
2014-11-14 08:23:31 -05:00
virtual void Run()
2014-11-15 18:48:24 -05:00
{
2014-11-14 08:23:31 -05:00
bx::Thread thread;
thread.init(MainThreadFunc, nullptr);
2014-11-15 18:48:24 -05:00
CoreWindow^ window = CoreWindow::GetForCurrentThread();
2015-04-17 23:12:45 -04:00
auto bounds = window->Bounds;
auto dpi = DisplayInformation::GetForCurrentView()->LogicalDpi;
2014-11-15 18:48:24 -05:00
2015-04-17 23:12:45 -04:00
static const float dipsPerInch = 96.0f;
g_eventQueue.postSizeEvent(g_defaultWindow
2015-07-16 23:28:43 -04:00
, lround(floorf(bounds.Width * dpi / dipsPerInch + 0.5f) )
, lround(floorf(bounds.Height * dpi / dipsPerInch + 0.5f) )
2015-04-17 23:12:45 -04:00
);
2014-11-15 18:48:24 -05: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 08:23:31 -05:00
virtual void Uninitialize()
2014-11-15 18:48:24 -05:00
{
}
2014-11-14 08:23:31 -05:00
private:
2014-11-15 18:48:24 -05:00
bool m_windowVisible;
bool m_windowClosed;
2014-11-14 08:23:31 -05:00
2014-11-15 18:48:24 -05:00
void OnActivated(CoreApplicationView^ applicationView, IActivatedEventArgs^ args)
{
CoreWindow::GetForCurrentThread()->Activate();
}
2014-11-14 08:23:31 -05:00
2014-11-15 18:48:24 -05:00
void OnVisibilityChanged(CoreWindow^ sender, VisibilityChangedEventArgs^ args)
{
m_windowVisible = args->Visible;
}
2014-11-14 08:23:31 -05:00
void OnWindowClosed(CoreWindow^ sender, CoreWindowEventArgs^ args)
2014-11-15 18:48:24 -05:00
{
m_windowClosed = true;
}
static int32_t MainThreadFunc(void*)
{
return entry::main(0, g_emptyArgs);
}
2014-11-14 08:23:31 -05:00
};
ref class AppSource sealed : IFrameworkViewSource
{
public:
virtual IFrameworkView^ CreateView()
2014-11-15 18:48:24 -05:00
{
return ref new App();
}
2014-11-14 08:23:31 -05:00
};
namespace entry
{
2014-11-15 18:48:24 -05:00
const Event* poll()
2014-11-14 08:23:31 -05: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-15 18:48:24 -05:00
WindowHandle createWindow(int32_t _x, int32_t _y, uint32_t _width, uint32_t _height, uint32_t _flags, const char* _title)
2014-11-14 08:23:31 -05: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 08:23:31 -05:00
void setMouseLock(WindowHandle _handle, bool _lock)
{
BX_UNUSED(_handle, _lock);
}
}
[MTAThread]
int main(Array<String^>^)
{
2014-11-15 18:48:24 -05:00
auto appSource = ref new AppSource();
CoreApplication::Run(appSource);
return 0;
2014-11-14 08:23:31 -05:00
}
2014-11-15 18:48:24 -05:00
#endif // BX_PLATFORM_WINRT