This commit is contained in:
Reece Mackie 2019-04-27 18:17:19 +01:00
parent 2d24a9c7a1
commit 037cc8252c

View File

@ -6,11 +6,11 @@
* *
* CONFIGURATION: * CONFIGURATION:
* *
* #define PCH * #define PCH
* This defines what header is the PCH and needs to be included * This defines what header is the PCH and needs to be included
* *
* #define HOLDHACK * #define HOLDHACK
* This enables a hack to fix flickering key presses (Temporary) * This enables a hack to fix flickering key presses (Temporary)
* *
* Copyright (c) 2013-2019 Ramon Santamaria (@raysan5) * Copyright (c) 2013-2019 Ramon Santamaria (@raysan5)
* *
@ -63,8 +63,8 @@ extern "C" { EGLNativeWindowType uwpWindow; };
/* /*
TODO list: TODO list:
- Cache reference to our CoreWindow? - Cache reference to our CoreWindow?
- Implement gestures support - Implement gestures support
*/ */
// Stand-ins for "core.c" variables // Stand-ins for "core.c" variables
@ -84,474 +84,474 @@ ref class BaseApp : public Windows::ApplicationModel::Core::IFrameworkView
{ {
public: public:
// IFrameworkView Methods. // IFrameworkView Methods.
virtual void Initialize(Windows::ApplicationModel::Core::CoreApplicationView^ applicationView) virtual void Initialize(Windows::ApplicationModel::Core::CoreApplicationView^ applicationView)
{ {
// Register event handlers for app lifecycle. This example includes Activated, so that we // Register event handlers for app lifecycle. This example includes Activated, so that we
// can make the CoreWindow active and start rendering on the window. // can make the CoreWindow active and start rendering on the window.
applicationView->Activated += ref new TypedEventHandler<CoreApplicationView^, IActivatedEventArgs^>(this, &BaseApp::OnActivated); applicationView->Activated += ref new TypedEventHandler<CoreApplicationView^, IActivatedEventArgs^>(this, &BaseApp::OnActivated);
// Logic for other event handlers could go here. // Logic for other event handlers could go here.
// Information about the Suspending and Resuming event handlers can be found here: // Information about the Suspending and Resuming event handlers can be found here:
// http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh994930.aspx // http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh994930.aspx
CoreApplication::Resuming += ref new EventHandler<Platform::Object^>(this, &BaseApp::OnResuming); CoreApplication::Resuming += ref new EventHandler<Platform::Object^>(this, &BaseApp::OnResuming);
} }
virtual void SetWindow(Windows::UI::Core::CoreWindow^ window) virtual void SetWindow(Windows::UI::Core::CoreWindow^ window)
{ {
window->SizeChanged += ref new TypedEventHandler<CoreWindow^, WindowSizeChangedEventArgs^>(this, &BaseApp::OnWindowSizeChanged); window->SizeChanged += ref new TypedEventHandler<CoreWindow^, WindowSizeChangedEventArgs^>(this, &BaseApp::OnWindowSizeChanged);
window->VisibilityChanged += ref new TypedEventHandler<CoreWindow^, VisibilityChangedEventArgs^>(this, &BaseApp::OnVisibilityChanged); window->VisibilityChanged += ref new TypedEventHandler<CoreWindow^, VisibilityChangedEventArgs^>(this, &BaseApp::OnVisibilityChanged);
window->Closed += ref new TypedEventHandler<CoreWindow^, CoreWindowEventArgs^>(this, &BaseApp::OnWindowClosed); window->Closed += ref new TypedEventHandler<CoreWindow^, CoreWindowEventArgs^>(this, &BaseApp::OnWindowClosed);
window->PointerPressed += ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &BaseApp::PointerPressed); window->PointerPressed += ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &BaseApp::PointerPressed);
window->PointerWheelChanged += ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &BaseApp::PointerWheelChanged); window->PointerWheelChanged += ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &BaseApp::PointerWheelChanged);
window->KeyDown += ref new TypedEventHandler<CoreWindow ^, KeyEventArgs ^>(this, &BaseApp::OnKeyDown); window->KeyDown += ref new TypedEventHandler<CoreWindow ^, KeyEventArgs ^>(this, &BaseApp::OnKeyDown);
window->KeyUp += ref new TypedEventHandler<CoreWindow ^, KeyEventArgs ^>(this, &BaseApp::OnKeyUp); window->KeyUp += ref new TypedEventHandler<CoreWindow ^, KeyEventArgs ^>(this, &BaseApp::OnKeyUp);
Windows::Devices::Input::MouseDevice::GetForCurrentView()->MouseMoved += ref new TypedEventHandler<MouseDevice^, MouseEventArgs^>(this, &BaseApp::MouseMoved); Windows::Devices::Input::MouseDevice::GetForCurrentView()->MouseMoved += ref new TypedEventHandler<MouseDevice^, MouseEventArgs^>(this, &BaseApp::MouseMoved);
DisplayInformation^ currentDisplayInformation = DisplayInformation::GetForCurrentView(); DisplayInformation^ currentDisplayInformation = DisplayInformation::GetForCurrentView();
currentDisplayInformation->DpiChanged += ref new TypedEventHandler<DisplayInformation^, Object^>(this, &BaseApp::OnDpiChanged); currentDisplayInformation->DpiChanged += ref new TypedEventHandler<DisplayInformation^, Object^>(this, &BaseApp::OnDpiChanged);
currentDisplayInformation->OrientationChanged += ref new TypedEventHandler<DisplayInformation^, Object^>(this, &BaseApp::OnOrientationChanged); currentDisplayInformation->OrientationChanged += ref new TypedEventHandler<DisplayInformation^, Object^>(this, &BaseApp::OnOrientationChanged);
// The CoreWindow has been created, so EGL can be initialized. // The CoreWindow has been created, so EGL can be initialized.
uwpWindow = (EGLNativeWindowType)window; uwpWindow = (EGLNativeWindowType)window;
InitWindow(width, height, NULL); InitWindow(width, height, NULL);
} }
virtual void Load(Platform::String^ entryPoint) {} virtual void Load(Platform::String^ entryPoint) {}
void Setup(int width, int height) void Setup(int width, int height)
{ {
//Set dimensions //Set dimensions
this->width = width; this->width = width;
this->height = height; this->height = height;
} }
virtual void Run() virtual void Run()
{ {
//Get display dimensions //Get display dimensions
DisplayInformation^ dInfo = DisplayInformation::GetForCurrentView(); DisplayInformation^ dInfo = DisplayInformation::GetForCurrentView();
Vector2 screenSize = { dInfo->ScreenWidthInRawPixels, dInfo->ScreenHeightInRawPixels }; Vector2 screenSize = { dInfo->ScreenWidthInRawPixels, dInfo->ScreenHeightInRawPixels };
//Send display dimensions //Send display dimensions
UWPMessage* msg = CreateUWPMessage(); UWPMessage* msg = CreateUWPMessage();
msg->Type = SetDisplayDims; msg->Type = SetDisplayDims;
msg->Vector0 = screenSize; msg->Vector0 = screenSize;
UWPSendMessage(msg); UWPSendMessage(msg);
//Send the time to the core //Send the time to the core
using clock = std::chrono::high_resolution_clock; using clock = std::chrono::high_resolution_clock;
auto timeStart = clock::now(); auto timeStart = clock::now();
//Set fps if 0 //Set fps if 0
if (GetFPS() <= 0) if (GetFPS() <= 0)
SetTargetFPS(60); SetTargetFPS(60);
while (!mWindowClosed) while (!mWindowClosed)
{ {
if (mWindowVisible) if (mWindowVisible)
{ {
//Send time //Send time
auto delta = clock::now() - timeStart; auto delta = clock::now() - timeStart;
UWPMessage* timeMsg = CreateUWPMessage(); UWPMessage* timeMsg = CreateUWPMessage();
timeMsg->Type = SetGameTime; timeMsg->Type = SetGameTime;
timeMsg->Double0 = std::chrono::duration_cast<std::chrono::seconds>(delta).count(); timeMsg->Double0 = std::chrono::duration_cast<std::chrono::seconds>(delta).count();
UWPSendMessage(timeMsg); UWPSendMessage(timeMsg);
//Call update function //Call update function
Update(); Update();
PollInput(); PollInput();
CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent); CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
} }
else else
{ {
CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneAndAllPending); CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneAndAllPending);
} }
} }
CloseWindow(); CloseWindow();
} }
//Called every frame (Maybe add draw) //Called every frame (Maybe add draw)
virtual void Update() {} virtual void Update() {}
virtual void Uninitialize() {} virtual void Uninitialize() {}
protected: protected:
// Input polling // Input polling
void PollInput() void PollInput()
{ {
// Process Messages // Process Messages
{ {
//Loop over pending messages //Loop over pending messages
while (UWPHasMessages()) while (UWPHasMessages())
{ {
//Get the message //Get the message
auto msg = UWPGetMessage(); auto msg = UWPGetMessage();
//Carry out the command //Carry out the command
switch(msg->Type) switch(msg->Type)
{ {
case ShowMouse: //Do the same thing because of how UWP works... case ShowMouse: //Do the same thing because of how UWP works...
case UnlockMouse: case UnlockMouse:
{ {
CoreWindow::GetForCurrentThread()->PointerCursor = regularCursor; CoreWindow::GetForCurrentThread()->PointerCursor = regularCursor;
cursorLocked = false; cursorLocked = false;
MoveMouse(GetMousePosition()); MoveMouse(GetMousePosition());
break; break;
} }
case HideMouse: //Do the same thing because of how UWP works... case HideMouse: //Do the same thing because of how UWP works...
case LockMouse: case LockMouse:
{ {
CoreWindow::GetForCurrentThread()->PointerCursor = nullptr; CoreWindow::GetForCurrentThread()->PointerCursor = nullptr;
cursorLocked = true; cursorLocked = true;
break; break;
} }
case SetMouseLocation: case SetMouseLocation:
{ {
MoveMouse(msg->Vector0); MoveMouse(msg->Vector0);
break; break;
} }
} }
//Delete the message //Delete the message
DeleteUWPMessage(msg); DeleteUWPMessage(msg);
} }
} }
// Process Keyboard // Process Keyboard
{ {
for (int k = 0x08; k < 0xA6; k++) { for (int k = 0x08; k < 0xA6; k++) {
auto state = CoreWindow::GetForCurrentThread()->GetKeyState((Windows::System::VirtualKey) k); auto state = CoreWindow::GetForCurrentThread()->GetKeyState((Windows::System::VirtualKey) k);
#ifdef HOLDHACK #ifdef HOLDHACK
//Super hacky way of waiting three frames to see if we are ready to register the key as deregistered //Super hacky way of waiting three frames to see if we are ready to register the key as deregistered
//This will wait an entire 4 frames before deregistering the key, this makes sure that the key is not flickering //This will wait an entire 4 frames before deregistering the key, this makes sure that the key is not flickering
if (KeyboardStateHack[k] == 2) if (KeyboardStateHack[k] == 2)
{ {
if ((state & CoreVirtualKeyStates::None) == CoreVirtualKeyStates::None) if ((state & CoreVirtualKeyStates::None) == CoreVirtualKeyStates::None)
{ {
KeyboardStateHack[k] = 3; KeyboardStateHack[k] = 3;
} }
} }
else if (KeyboardStateHack[k] == 3) else if (KeyboardStateHack[k] == 3)
{ {
if ((state & CoreVirtualKeyStates::None) == CoreVirtualKeyStates::None) if ((state & CoreVirtualKeyStates::None) == CoreVirtualKeyStates::None)
{ {
KeyboardStateHack[k] = 4; KeyboardStateHack[k] = 4;
} }
} }
else if (KeyboardStateHack[k] == 4) else if (KeyboardStateHack[k] == 4)
{ {
if ((state & CoreVirtualKeyStates::None) == CoreVirtualKeyStates::None) if ((state & CoreVirtualKeyStates::None) == CoreVirtualKeyStates::None)
{ {
//Reset key... //Reset key...
KeyboardStateHack[k] = 0; KeyboardStateHack[k] = 0;
//Tell core //Tell core
RegisterKey(k, 0); RegisterKey(k, 0);
} }
} }
#endif #endif
//Left and right alt, KeyUp and KeyDown are not called for it //Left and right alt, KeyUp and KeyDown are not called for it
//No need to hack because this is not a character //No need to hack because this is not a character
//TODO: Maybe do all other key registrations like this, no more key events? //TODO: Maybe do all other key registrations like this, no more key events?
if (k == 0xA4 || k == 0xA5) if (k == 0xA4 || k == 0xA5)
{ {
if ((state & CoreVirtualKeyStates::Down) == CoreVirtualKeyStates::Down) if ((state & CoreVirtualKeyStates::Down) == CoreVirtualKeyStates::Down)
{ {
RegisterKey(k, 1); RegisterKey(k, 1);
} }
else else
{ {
RegisterKey(k, 0); RegisterKey(k, 0);
} }
} }
} }
} }
// Process Mouse // Process Mouse
{ {
if (CurrentPointerID > -1) { if (CurrentPointerID > -1) {
auto point = PointerPoint::GetCurrentPoint(CurrentPointerID); auto point = PointerPoint::GetCurrentPoint(CurrentPointerID);
auto props = point->Properties; auto props = point->Properties;
if (props->IsLeftButtonPressed) if (props->IsLeftButtonPressed)
{ {
RegisterClick(MOUSE_LEFT_BUTTON, 1); RegisterClick(MOUSE_LEFT_BUTTON, 1);
} }
else else
{ {
RegisterClick(MOUSE_LEFT_BUTTON, 0); RegisterClick(MOUSE_LEFT_BUTTON, 0);
} }
if (props->IsRightButtonPressed) if (props->IsRightButtonPressed)
{ {
RegisterClick(MOUSE_RIGHT_BUTTON, 1); RegisterClick(MOUSE_RIGHT_BUTTON, 1);
} }
else else
{ {
RegisterClick(MOUSE_RIGHT_BUTTON, 0); RegisterClick(MOUSE_RIGHT_BUTTON, 0);
} }
if (props->IsMiddleButtonPressed) if (props->IsMiddleButtonPressed)
{ {
RegisterClick(MOUSE_MIDDLE_BUTTON, 1); RegisterClick(MOUSE_MIDDLE_BUTTON, 1);
} }
else else
{ {
RegisterClick(MOUSE_MIDDLE_BUTTON, 0); RegisterClick(MOUSE_MIDDLE_BUTTON, 0);
} }
} }
CoreWindow ^window = CoreWindow::GetForCurrentThread(); CoreWindow ^window = CoreWindow::GetForCurrentThread();
if (cursorLocked) if (cursorLocked)
{ {
// Track cursor movement delta, recenter it on the client // Track cursor movement delta, recenter it on the client
auto curMousePos = GetMousePosition(); auto curMousePos = GetMousePosition();
auto x = curMousePos.x + mouseDelta.x; auto x = curMousePos.x + mouseDelta.x;
auto y = curMousePos.y + mouseDelta.y; auto y = curMousePos.y + mouseDelta.y;
UpdateMousePosition({ x, y }); UpdateMousePosition({ x, y });
// Why we're not using UWPSetMousePosition here... // Why we're not using UWPSetMousePosition here...
// UWPSetMousePosition changes the "mousePosition" variable to match where the cursor actually is. // UWPSetMousePosition changes the "mousePosition" variable to match where the cursor actually is.
// Our cursor is locked to the middle of screen, and we don't want that reflected in "mousePosition" // Our cursor is locked to the middle of screen, and we don't want that reflected in "mousePosition"
Vector2 centerClient = { (float)(GetScreenWidth() / 2), (float)(GetScreenHeight() / 2) }; Vector2 centerClient = { (float)(GetScreenWidth() / 2), (float)(GetScreenHeight() / 2) };
window->PointerPosition = Point(centerClient.x + window->Bounds.X, centerClient.y + window->Bounds.Y); window->PointerPosition = Point(centerClient.x + window->Bounds.X, centerClient.y + window->Bounds.Y);
} }
else else
{ {
// Record the cursor's position relative to the client // Record the cursor's position relative to the client
auto x = window->PointerPosition.X - window->Bounds.X; auto x = window->PointerPosition.X - window->Bounds.X;
auto y = window->PointerPosition.Y - window->Bounds.Y; auto y = window->PointerPosition.Y - window->Bounds.Y;
UpdateMousePosition({ x, y }); UpdateMousePosition({ x, y });
} }
mouseDelta = { 0 ,0 }; mouseDelta = { 0 ,0 };
} }
// Process Gamepads // Process Gamepads
{ {
// Check if gamepads are ready // Check if gamepads are ready
for (int i = 0; i < MAX_GAMEPADS; i++) for (int i = 0; i < MAX_GAMEPADS; i++)
{ {
// HACK: UWP keeps a contiguous list of gamepads. For the interest of time I'm just doing a 1:1 mapping of // HACK: UWP keeps a contiguous list of gamepads. For the interest of time I'm just doing a 1:1 mapping of
// connected gamepads with their spot in the list, but this has serious robustness problems // connected gamepads with their spot in the list, but this has serious robustness problems
// e.g. player 1, 2, and 3 are playing a game - if player2 disconnects, p3's controller would now be mapped to p2's character since p3 is now second in the list. // e.g. player 1, 2, and 3 are playing a game - if player2 disconnects, p3's controller would now be mapped to p2's character since p3 is now second in the list.
UWPMessage* msg = CreateUWPMessage(); UWPMessage* msg = CreateUWPMessage();
msg->Type = MarkGamepadActive; msg->Type = MarkGamepadActive;
msg->Int0 = i; msg->Int0 = i;
msg->Bool0 = i < Gamepad::Gamepads->Size; msg->Bool0 = i < Gamepad::Gamepads->Size;
UWPSendMessage(msg); UWPSendMessage(msg);
} }
// Get current gamepad state // Get current gamepad state
for (int i = 0; i < MAX_GAMEPADS; i++) for (int i = 0; i < MAX_GAMEPADS; i++)
{ {
if (IsGamepadAvailable(i)) if (IsGamepadAvailable(i))
{ {
// Get current gamepad state // Get current gamepad state
auto gamepad = Gamepad::Gamepads->GetAt(i); auto gamepad = Gamepad::Gamepads->GetAt(i);
GamepadReading reading = gamepad->GetCurrentReading(); GamepadReading reading = gamepad->GetCurrentReading();
// NOTE: Maybe it would be wiser to redefine the gamepad button mappings in "raylib.h" for the UWP platform instead of remapping them manually // NOTE: Maybe it would be wiser to redefine the gamepad button mappings in "raylib.h" for the UWP platform instead of remapping them manually
RegisterGamepadButton(i, GAMEPAD_XBOX_BUTTON_A, ((reading.Buttons & GamepadButtons::A) == GamepadButtons::A)); RegisterGamepadButton(i, GAMEPAD_XBOX_BUTTON_A, ((reading.Buttons & GamepadButtons::A) == GamepadButtons::A));
RegisterGamepadButton(i, GAMEPAD_XBOX_BUTTON_B, ((reading.Buttons & GamepadButtons::B) == GamepadButtons::B)); RegisterGamepadButton(i, GAMEPAD_XBOX_BUTTON_B, ((reading.Buttons & GamepadButtons::B) == GamepadButtons::B));
RegisterGamepadButton(i, GAMEPAD_XBOX_BUTTON_X, ((reading.Buttons & GamepadButtons::X) == GamepadButtons::X)); RegisterGamepadButton(i, GAMEPAD_XBOX_BUTTON_X, ((reading.Buttons & GamepadButtons::X) == GamepadButtons::X));
RegisterGamepadButton(i, GAMEPAD_XBOX_BUTTON_Y, ((reading.Buttons & GamepadButtons::Y) == GamepadButtons::Y)); RegisterGamepadButton(i, GAMEPAD_XBOX_BUTTON_Y, ((reading.Buttons & GamepadButtons::Y) == GamepadButtons::Y));
RegisterGamepadButton(i, GAMEPAD_XBOX_BUTTON_LB, ((reading.Buttons & GamepadButtons::LeftShoulder) == GamepadButtons::LeftShoulder)); RegisterGamepadButton(i, GAMEPAD_XBOX_BUTTON_LB, ((reading.Buttons & GamepadButtons::LeftShoulder) == GamepadButtons::LeftShoulder));
RegisterGamepadButton(i, GAMEPAD_XBOX_BUTTON_RB, ((reading.Buttons & GamepadButtons::RightShoulder) == GamepadButtons::RightShoulder)); RegisterGamepadButton(i, GAMEPAD_XBOX_BUTTON_RB, ((reading.Buttons & GamepadButtons::RightShoulder) == GamepadButtons::RightShoulder));
RegisterGamepadButton(i, GAMEPAD_XBOX_BUTTON_SELECT, ((reading.Buttons & GamepadButtons::View) == GamepadButtons::View)); // Changed for XB1 Controller RegisterGamepadButton(i, GAMEPAD_XBOX_BUTTON_SELECT, ((reading.Buttons & GamepadButtons::View) == GamepadButtons::View)); // Changed for XB1 Controller
RegisterGamepadButton(i, GAMEPAD_XBOX_BUTTON_START, ((reading.Buttons & GamepadButtons::Menu) == GamepadButtons::Menu)); // Changed for XB1 Controller RegisterGamepadButton(i, GAMEPAD_XBOX_BUTTON_START, ((reading.Buttons & GamepadButtons::Menu) == GamepadButtons::Menu)); // Changed for XB1 Controller
RegisterGamepadButton(i, GAMEPAD_XBOX_BUTTON_UP, ((reading.Buttons & GamepadButtons::DPadUp) == GamepadButtons::DPadUp)); RegisterGamepadButton(i, GAMEPAD_XBOX_BUTTON_UP, ((reading.Buttons & GamepadButtons::DPadUp) == GamepadButtons::DPadUp));
RegisterGamepadButton(i, GAMEPAD_XBOX_BUTTON_RIGHT, ((reading.Buttons & GamepadButtons::DPadRight) == GamepadButtons::DPadRight)); RegisterGamepadButton(i, GAMEPAD_XBOX_BUTTON_RIGHT, ((reading.Buttons & GamepadButtons::DPadRight) == GamepadButtons::DPadRight));
RegisterGamepadButton(i, GAMEPAD_XBOX_BUTTON_DOWN, ((reading.Buttons & GamepadButtons::DPadDown) == GamepadButtons::DPadDown)); RegisterGamepadButton(i, GAMEPAD_XBOX_BUTTON_DOWN, ((reading.Buttons & GamepadButtons::DPadDown) == GamepadButtons::DPadDown));
RegisterGamepadButton(i, GAMEPAD_XBOX_BUTTON_LEFT, ((reading.Buttons & GamepadButtons::DPadLeft) == GamepadButtons::DPadLeft)); RegisterGamepadButton(i, GAMEPAD_XBOX_BUTTON_LEFT, ((reading.Buttons & GamepadButtons::DPadLeft) == GamepadButtons::DPadLeft));
RegisterGamepadButton(i, GAMEPAD_XBOX_BUTTON_HOME, false); // Home button not supported by UWP RegisterGamepadButton(i, GAMEPAD_XBOX_BUTTON_HOME, false); // Home button not supported by UWP
// Get current axis state // Get current axis state
RegisterGamepadAxis(i, GAMEPAD_XBOX_AXIS_LEFT_X, (float)reading.LeftThumbstickX); RegisterGamepadAxis(i, GAMEPAD_XBOX_AXIS_LEFT_X, (float)reading.LeftThumbstickX);
RegisterGamepadAxis(i, GAMEPAD_XBOX_AXIS_LEFT_Y, (float)reading.LeftThumbstickY); RegisterGamepadAxis(i, GAMEPAD_XBOX_AXIS_LEFT_Y, (float)reading.LeftThumbstickY);
RegisterGamepadAxis(i, GAMEPAD_XBOX_AXIS_RIGHT_X, (float)reading.RightThumbstickX); RegisterGamepadAxis(i, GAMEPAD_XBOX_AXIS_RIGHT_X, (float)reading.RightThumbstickX);
RegisterGamepadAxis(i, GAMEPAD_XBOX_AXIS_RIGHT_Y, (float)reading.RightThumbstickY); RegisterGamepadAxis(i, GAMEPAD_XBOX_AXIS_RIGHT_Y, (float)reading.RightThumbstickY);
RegisterGamepadAxis(i, GAMEPAD_XBOX_AXIS_LT, (float)reading.LeftTrigger); RegisterGamepadAxis(i, GAMEPAD_XBOX_AXIS_LT, (float)reading.LeftTrigger);
RegisterGamepadAxis(i, GAMEPAD_XBOX_AXIS_RT, (float)reading.RightTrigger); RegisterGamepadAxis(i, GAMEPAD_XBOX_AXIS_RT, (float)reading.RightTrigger);
} }
} }
} }
} }
// Application lifecycle event handlers. // Application lifecycle event handlers.
void OnActivated(Windows::ApplicationModel::Core::CoreApplicationView^ applicationView, Windows::ApplicationModel::Activation::IActivatedEventArgs^ args) void OnActivated(Windows::ApplicationModel::Core::CoreApplicationView^ applicationView, Windows::ApplicationModel::Activation::IActivatedEventArgs^ args)
{ {
// Run() won't start until the CoreWindow is activated. // Run() won't start until the CoreWindow is activated.
CoreWindow::GetForCurrentThread()->Activate(); CoreWindow::GetForCurrentThread()->Activate();
} }
void OnResuming(Platform::Object^ sender, Platform::Object^ args) {} void OnResuming(Platform::Object^ sender, Platform::Object^ args) {}
// Window event handlers. // Window event handlers.
void OnWindowSizeChanged(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::WindowSizeChangedEventArgs^ args) void OnWindowSizeChanged(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::WindowSizeChangedEventArgs^ args)
{ {
UWPMessage* msg = CreateUWPMessage(); UWPMessage* msg = CreateUWPMessage();
msg->Type = HandleResize; msg->Type = HandleResize;
UWPSendMessage(msg); UWPSendMessage(msg);
} }
void OnVisibilityChanged(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::VisibilityChangedEventArgs^ args) void OnVisibilityChanged(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::VisibilityChangedEventArgs^ args)
{ {
mWindowVisible = args->Visible; mWindowVisible = args->Visible;
} }
void OnWindowClosed(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::CoreWindowEventArgs^ args) void OnWindowClosed(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::CoreWindowEventArgs^ args)
{ {
mWindowClosed = true; mWindowClosed = true;
} }
// DisplayInformation event handlers. // DisplayInformation event handlers.
void OnDpiChanged(Windows::Graphics::Display::DisplayInformation^ sender, Platform::Object^ args) {} void OnDpiChanged(Windows::Graphics::Display::DisplayInformation^ sender, Platform::Object^ args) {}
void OnOrientationChanged(Windows::Graphics::Display::DisplayInformation^ sender, Platform::Object^ args) {} void OnOrientationChanged(Windows::Graphics::Display::DisplayInformation^ sender, Platform::Object^ args) {}
// Input event handlers // Input event handlers
void PointerPressed(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::PointerEventArgs^ args) void PointerPressed(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::PointerEventArgs^ args)
{ {
//Get the current active pointer ID for our loop //Get the current active pointer ID for our loop
CurrentPointerID = args->CurrentPoint->PointerId; CurrentPointerID = args->CurrentPoint->PointerId;
args->Handled = true; args->Handled = true;
} }
void PointerWheelChanged(Windows::UI::Core::CoreWindow ^sender, Windows::UI::Core::PointerEventArgs^ args) void PointerWheelChanged(Windows::UI::Core::CoreWindow ^sender, Windows::UI::Core::PointerEventArgs^ args)
{ {
UWPMessage* msg = CreateUWPMessage(); UWPMessage* msg = CreateUWPMessage();
msg->Type = ScrollWheelUpdate; msg->Type = ScrollWheelUpdate;
msg->Float0 = args->CurrentPoint->Properties->MouseWheelDelta; msg->Float0 = args->CurrentPoint->Properties->MouseWheelDelta;
UWPSendMessage(msg); UWPSendMessage(msg);
} }
void MouseMoved(Windows::Devices::Input::MouseDevice^ mouseDevice, Windows::Devices::Input::MouseEventArgs^ args) void MouseMoved(Windows::Devices::Input::MouseDevice^ mouseDevice, Windows::Devices::Input::MouseEventArgs^ args)
{ {
mouseDelta.x += args->MouseDelta.X; mouseDelta.x += args->MouseDelta.X;
mouseDelta.y += args->MouseDelta.Y; mouseDelta.y += args->MouseDelta.Y;
} }
void OnKeyDown(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Core::KeyEventArgs ^ args) void OnKeyDown(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Core::KeyEventArgs ^ args)
{ {
#ifdef HOLDHACK #ifdef HOLDHACK
//Start the hack //Start the hack
KeyboardStateHack[(int)args->VirtualKey] = 1; KeyboardStateHack[(int)args->VirtualKey] = 1;
#endif #endif
RegisterKey((int)args->VirtualKey, 1); RegisterKey((int)args->VirtualKey, 1);
} }
void OnKeyUp(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Core::KeyEventArgs ^ args) void OnKeyUp(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Core::KeyEventArgs ^ args)
{ {
#ifdef HOLDHACK #ifdef HOLDHACK
//The same hack //The same hack
if (KeyboardStateHack[(int)args->VirtualKey] == 1) if (KeyboardStateHack[(int)args->VirtualKey] == 1)
{ {
KeyboardStateHack[(int)args->VirtualKey] = 2; KeyboardStateHack[(int)args->VirtualKey] = 2;
} }
else if (KeyboardStateHack[(int)args->VirtualKey] == 2) else if (KeyboardStateHack[(int)args->VirtualKey] == 2)
{ {
KeyboardStateHack[(int)args->VirtualKey] = 3; KeyboardStateHack[(int)args->VirtualKey] = 3;
} }
else if (KeyboardStateHack[(int)args->VirtualKey] == 3) else if (KeyboardStateHack[(int)args->VirtualKey] == 3)
{ {
KeyboardStateHack[(int)args->VirtualKey] = 4; KeyboardStateHack[(int)args->VirtualKey] = 4;
} }
else if (KeyboardStateHack[(int)args->VirtualKey] == 4) else if (KeyboardStateHack[(int)args->VirtualKey] == 4)
{ {
RegisterKey((int)args->VirtualKey, 0); RegisterKey((int)args->VirtualKey, 0);
KeyboardStateHack[(int)args->VirtualKey] = 0; KeyboardStateHack[(int)args->VirtualKey] = 0;
} }
#else #else
//No hack, allow flickers //No hack, allow flickers
RegisterKey((int)args->VirtualKey, 0); RegisterKey((int)args->VirtualKey, 0);
#endif #endif
} }
private: private:
void RegisterKey(int key, char status) void RegisterKey(int key, char status)
{ {
UWPMessage* msg = CreateUWPMessage(); UWPMessage* msg = CreateUWPMessage();
msg->Type = UWPMessageType::RegisterKey; msg->Type = UWPMessageType::RegisterKey;
msg->Int0 = key; msg->Int0 = key;
msg->Char0 = status; msg->Char0 = status;
UWPSendMessage(msg); UWPSendMessage(msg);
} }
void MoveMouse(Vector2 pos) void MoveMouse(Vector2 pos)
{ {
CoreWindow ^window = CoreWindow::GetForCurrentThread(); CoreWindow ^window = CoreWindow::GetForCurrentThread();
Point mousePosScreen = Point(pos.x + window->Bounds.X, pos.y + window->Bounds.Y); Point mousePosScreen = Point(pos.x + window->Bounds.X, pos.y + window->Bounds.Y);
window->PointerPosition = mousePosScreen; window->PointerPosition = mousePosScreen;
} }
void RegisterGamepadButton(int gamepad, int button, char status) void RegisterGamepadButton(int gamepad, int button, char status)
{ {
UWPMessage* msg = CreateUWPMessage(); UWPMessage* msg = CreateUWPMessage();
msg->Type = MarkGamepadButton; msg->Type = MarkGamepadButton;
msg->Int0 = gamepad; msg->Int0 = gamepad;
msg->Int1 = button; msg->Int1 = button;
msg->Char0 = status; msg->Char0 = status;
UWPSendMessage(msg); UWPSendMessage(msg);
} }
void RegisterGamepadAxis(int gamepad, int axis, float value) void RegisterGamepadAxis(int gamepad, int axis, float value)
{ {
UWPMessage* msg = CreateUWPMessage(); UWPMessage* msg = CreateUWPMessage();
msg->Type = MarkGamepadAxis; msg->Type = MarkGamepadAxis;
msg->Int0 = gamepad; msg->Int0 = gamepad;
msg->Int1 = axis; msg->Int1 = axis;
msg->Float0 = value; msg->Float0 = value;
UWPSendMessage(msg); UWPSendMessage(msg);
} }
void UpdateMousePosition(Vector2 pos) void UpdateMousePosition(Vector2 pos)
{ {
UWPMessage* msg = CreateUWPMessage(); UWPMessage* msg = CreateUWPMessage();
msg->Type = UpdateMouseLocation; msg->Type = UpdateMouseLocation;
msg->Vector0 = pos; msg->Vector0 = pos;
UWPSendMessage(msg); UWPSendMessage(msg);
} }
void RegisterClick(int button, char status) void RegisterClick(int button, char status)
{ {
UWPMessage* msg = CreateUWPMessage(); UWPMessage* msg = CreateUWPMessage();
msg->Type = UWPMessageType::RegisterClick; msg->Type = UWPMessageType::RegisterClick;
msg->Int0 = button; msg->Int0 = button;
msg->Char0 = status; msg->Char0 = status;
UWPSendMessage(msg); UWPSendMessage(msg);
} }
bool mWindowClosed = false; bool mWindowClosed = false;
bool mWindowVisible = true; bool mWindowVisible = true;
int width = 640; int width = 640;
int height = 480; int height = 480;
int CurrentPointerID = -1; int CurrentPointerID = -1;
#ifdef HOLDHACK #ifdef HOLDHACK
char KeyboardStateHack[0xA6]; //0xA6 because the highest key we compare against is 0xA5 char KeyboardStateHack[0xA6]; //0xA6 because the highest key we compare against is 0xA5
#endif #endif
}; };
@ -560,8 +560,8 @@ template<typename AppType>
ref class ApplicationSource sealed : Windows::ApplicationModel::Core::IFrameworkViewSource ref class ApplicationSource sealed : Windows::ApplicationModel::Core::IFrameworkViewSource
{ {
public: public:
virtual Windows::ApplicationModel::Core::IFrameworkView^ CreateView() virtual Windows::ApplicationModel::Core::IFrameworkView^ CreateView()
{ {
return ref new AppType(); return ref new AppType();
} }
}; };