From 0357230af8f7bdbf1501d96c5342bde9ca9e2213 Mon Sep 17 00:00:00 2001 From: Reece Mackie <20544390+Rover656@users.noreply.github.com> Date: Thu, 30 Apr 2020 14:10:08 +0100 Subject: [PATCH] Read desc - Moved UWP specific functions to uwp_events.h - Removed BaseApp. - Implemented example UWP lifecycle. --- projects/VS2017.UWP/raylib.App.UWP/App.cpp | 371 ++++++++++++++- projects/VS2017.UWP/raylib.App.UWP/App.h | 48 +- projects/VS2017.UWP/raylib.App.UWP/BaseApp.h | 439 ------------------ .../raylib.App.UWP/raylib.App.UWP.vcxproj | 1 - .../raylib.App.UWP.vcxproj.filters | 1 - .../VS2017.UWP/raylib.UWP/raylib.UWP.vcxproj | 1 + .../raylib.UWP/raylib.UWP.vcxproj.filters | 1 + src/core.c | 174 ++++++- src/utils.c | 214 --------- src/utils.h | 70 --- src/uwp_events.h | 107 +++++ 11 files changed, 673 insertions(+), 754 deletions(-) delete mode 100644 projects/VS2017.UWP/raylib.App.UWP/BaseApp.h create mode 100644 src/uwp_events.h diff --git a/projects/VS2017.UWP/raylib.App.UWP/App.cpp b/projects/VS2017.UWP/raylib.App.UWP/App.cpp index ad3fdb28f..3b7390e09 100644 --- a/projects/VS2017.UWP/raylib.App.UWP/App.cpp +++ b/projects/VS2017.UWP/raylib.App.UWP/App.cpp @@ -1,26 +1,176 @@ #include "pch.h" #include "app.h" -#include "raylib.h" +#include + +#include +#include + +#include +#include + +using namespace Windows::ApplicationModel::Core; +using namespace Windows::ApplicationModel::Activation; +using namespace Windows::UI::Core; +using namespace Windows::UI::Input; +using namespace Windows::Devices::Input; +using namespace Windows::Foundation; +using namespace Windows::Foundation::Collections; +using namespace Windows::Gaming::Input; +using namespace Windows::Graphics::Display; +using namespace Microsoft::WRL; +using namespace Platform; using namespace raylibUWP; +// Stand-ins for "core.c" variables +#define MAX_GAMEPADS 4 // Max number of gamepads supported +#define MAX_GAMEPAD_BUTTONS 32 // Max bumber of buttons supported (per gamepad) +#define MAX_GAMEPAD_AXIS 8 // Max number of axis supported (per gamepad) + // The main function creates an IFrameworkViewSource for our app, and runs the app [Platform::MTAThread] int main(Platform::Array^) { - auto appSource = ref new ApplicationSource(); + auto appSource = ref new AppSource(); CoreApplication::Run(appSource); - return 0; } +// App implementation App::App() { - // This does not work... need to fix this. + // Currently, UWP ignores flags... You must implement flag functionality yourself SetConfigFlags(0); +} - Setup(640, 480); +void App::Initialize(Windows::ApplicationModel::Core::CoreApplicationView^ applicationView) +{ + // Register event handlers for app lifecycle. This example includes Activated, so that we + // can make the CoreWindow active and start rendering on the window. + applicationView->Activated += ref new TypedEventHandler(this, &App::OnActivated); + + // Logic for other event handlers could go 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 + + CoreApplication::Suspending += ref new Windows::Foundation::EventHandler(this, &App::OnSuspending); + CoreApplication::Resuming += ref new EventHandler(this, &App::OnResuming); +} + +void App::SetWindow(Windows::UI::Core::CoreWindow^ window) +{ + // Hook window events + window->SizeChanged += ref new TypedEventHandler(this, &App::OnWindowSizeChanged); + window->VisibilityChanged += ref new TypedEventHandler(this, &App::OnVisibilityChanged); + + // Hook mouse pointer events + window->PointerPressed += ref new TypedEventHandler(this, &App::OnPointerPressed); + window->PointerReleased += ref new Windows::Foundation::TypedEventHandler(this, &App::OnPointerReleased); + window->PointerWheelChanged += ref new TypedEventHandler(this, &App::OnPointerWheelChanged); + window->PointerMoved += ref new Windows::Foundation::TypedEventHandler(this, &App::OnPointerMoved); + + // Hook keyboard events. + window->KeyDown += ref new TypedEventHandler(this, &App::OnKeyDown); + window->KeyUp += ref new TypedEventHandler(this, &App::OnKeyUp); + + // The CoreWindow has been created, we can pass this to raylib for EGL context creation when it's time + UWPSetCoreWindowPtr((void*)window); +} + +void App::Load(Platform::String ^entryPoint) {} // Ignored for this example + +void App::Run() +{ + // Set up our UWP implementation + UWPSetQueryTimeFunc([]() + { + static auto timeStart = std::chrono::high_resolution_clock::now(); + auto delta = std::chrono::high_resolution_clock::now() - timeStart; + return (double)std::chrono::duration_cast(delta).count(); + }); + + UWPSetSleepFunc([](double seconds) { std::this_thread::sleep_for(std::chrono::duration(seconds)); }); + + UWPSetDisplaySizeFunc([](int* width, int* height) + { + // Get display dimensions + DisplayInformation^ dInfo = DisplayInformation::GetForCurrentView(); + *width = dInfo->ScreenWidthInRawPixels; + *height = dInfo->ScreenHeightInRawPixels; + }); + + UWPSetMouseHideFunc([]() + { + CoreWindow::GetForCurrentThread()->PointerCursor = nullptr; + }); + + UWPSetMouseShowFunc([]() + { + CoreWindow::GetForCurrentThread()->PointerCursor = ref new CoreCursor(CoreCursorType::Arrow, 0); + }); + + UWPSetMouseLockFunc([]() + { + CoreWindow::GetForCurrentThread()->PointerCursor = nullptr; + // TODO: + }); + + UWPSetMouseUnlockFunc([]() + { + CoreWindow::GetForCurrentThread()->PointerCursor = ref new CoreCursor(CoreCursorType::Arrow, 0); + // TODO: + }); + + UWPSetMouseSetPosFunc([](int x, int y) + { + CoreWindow^ window = CoreWindow::GetForCurrentThread(); + Point mousePosScreen = Point(x + window->Bounds.X, y + window->Bounds.Y); + window->PointerPosition = mousePosScreen; + }); + + // Set custom output handle + SetTraceLogCallback([](int logType, const char* text, va_list args) + { + std::string format = text; + + switch (logType) + { + case LOG_TRACE: format = std::string("TRACE: ") + format; break; + case LOG_DEBUG: format = std::string("DEBUG: ") + format; break; + case LOG_INFO: format = std::string("INFO: ") + format; break; + case LOG_WARNING: format = std::string("WARNING: ") + format; break; + case LOG_ERROR: format = std::string("ERROR: ") + format; break; + case LOG_FATAL: format = std::string("FATAL: ") + format; break; + default: break; + } + + char buf[1024]; // TODO: Is this large enough? + vsnprintf(buf, sizeof(buf), format.c_str(), args); + std::string output = std::string(buf) + std::string("\n"); + OutputDebugStringA(output.c_str()); + }); + + // Create window + InitWindow(640, 480, "raylib game example"); + + // Run game loop + while (!WindowShouldClose() && !mSuspended) + { + if (mWindowVisible) + { + ProcessGamepads(); + GameLoop(); + CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent); + } else CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneAndAllPending); + } + + CloseWindow(); +} + +void App::Uninitialize() +{ + // Do any UWP cleanup here. } static int posX = 100; @@ -28,7 +178,7 @@ static int posY = 100; static int gTime = 0; // This method is called every frame -void App::Update() +void App::GameLoop() { // Update //---------------------------------------------------------------------------------- @@ -76,4 +226,213 @@ void App::Update() EndDrawing(); //---------------------------------------------------------------------------------- +} + +void App::ProcessGamepads() +{ + // Check if gamepads are ready + for (int i = 0; i < MAX_GAMEPADS; i++) + { + // TODO: ROVER - I want to remove this problem if possible. + // 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 + // 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. + UWPActivateGamepadEvent(i, i < Gamepad::Gamepads->Size); + } + + // Get current gamepad state + for (int i = 0; i < MAX_GAMEPADS; i++) + { + if (IsGamepadAvailable(i)) + { + // Get current gamepad state + auto gamepad = Gamepad::Gamepads->GetAt(i); + 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 + UWPRegisterGamepadButton(i, GAMEPAD_BUTTON_RIGHT_FACE_DOWN, ((reading.Buttons & GamepadButtons::A) == GamepadButtons::A)); + UWPRegisterGamepadButton(i, GAMEPAD_BUTTON_RIGHT_FACE_RIGHT, ((reading.Buttons & GamepadButtons::B) == GamepadButtons::B)); + UWPRegisterGamepadButton(i, GAMEPAD_BUTTON_RIGHT_FACE_LEFT, ((reading.Buttons & GamepadButtons::X) == GamepadButtons::X)); + UWPRegisterGamepadButton(i, GAMEPAD_BUTTON_RIGHT_FACE_UP, ((reading.Buttons & GamepadButtons::Y) == GamepadButtons::Y)); + + UWPRegisterGamepadButton(i, GAMEPAD_BUTTON_LEFT_TRIGGER_1, ((reading.Buttons & GamepadButtons::LeftShoulder) == GamepadButtons::LeftShoulder)); + UWPRegisterGamepadButton(i, GAMEPAD_BUTTON_RIGHT_TRIGGER_1, ((reading.Buttons & GamepadButtons::RightShoulder) == GamepadButtons::RightShoulder)); + + UWPRegisterGamepadButton(i, GAMEPAD_BUTTON_MIDDLE_LEFT, ((reading.Buttons & GamepadButtons::View) == GamepadButtons::View)); // Changed for XB1 Controller + UWPRegisterGamepadButton(i, GAMEPAD_BUTTON_MIDDLE_RIGHT, ((reading.Buttons & GamepadButtons::Menu) == GamepadButtons::Menu)); // Changed for XB1 Controller + + UWPRegisterGamepadButton(i, GAMEPAD_BUTTON_LEFT_FACE_UP, ((reading.Buttons & GamepadButtons::DPadUp) == GamepadButtons::DPadUp)); + UWPRegisterGamepadButton(i, GAMEPAD_BUTTON_LEFT_FACE_RIGHT, ((reading.Buttons & GamepadButtons::DPadRight) == GamepadButtons::DPadRight)); + UWPRegisterGamepadButton(i, GAMEPAD_BUTTON_LEFT_FACE_DOWN, ((reading.Buttons & GamepadButtons::DPadDown) == GamepadButtons::DPadDown)); + UWPRegisterGamepadButton(i, GAMEPAD_BUTTON_LEFT_FACE_LEFT, ((reading.Buttons & GamepadButtons::DPadLeft) == GamepadButtons::DPadLeft)); + UWPRegisterGamepadButton(i, GAMEPAD_BUTTON_MIDDLE, false); // Home button not supported by UWP + + // Get current axis state + UWPRegisterGamepadAxis(i, GAMEPAD_AXIS_LEFT_X, (float)reading.LeftThumbstickX); + UWPRegisterGamepadAxis(i, GAMEPAD_AXIS_LEFT_Y, (float)reading.LeftThumbstickY); + UWPRegisterGamepadAxis(i, GAMEPAD_AXIS_RIGHT_X, (float)reading.RightThumbstickX); + UWPRegisterGamepadAxis(i, GAMEPAD_AXIS_RIGHT_Y, (float)reading.RightThumbstickY); + UWPRegisterGamepadAxis(i, GAMEPAD_AXIS_LEFT_TRIGGER, (float)reading.LeftTrigger); + UWPRegisterGamepadAxis(i, GAMEPAD_AXIS_RIGHT_TRIGGER, (float)reading.RightTrigger); + } + } +} + +// Events +void App::OnActivated(Windows::ApplicationModel::Core::CoreApplicationView^ applicationView, Windows::ApplicationModel::Activation::IActivatedEventArgs^ args) +{ + // Run() won't start until the CoreWindow is activated. + CoreWindow::GetForCurrentThread()->Activate(); +} + +void App::OnResuming(Platform::Object^ sender, Platform::Object^ args) +{ + // TODO: In your game, you will need to load your state here + mSuspended = false; +} + +void App::OnSuspending(Platform::Object^ sender, Windows::ApplicationModel::SuspendingEventArgs^ args) +{ + // TODO: In your game, you will need to save your state here + mSuspended = true; +} + +void App::OnWindowSizeChanged(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::WindowSizeChangedEventArgs^ args) +{ + UWPResizeEvent(args->Size.Width, args->Size.Height); +} + +void App::OnVisibilityChanged(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::VisibilityChangedEventArgs^ args) +{ + mWindowVisible = args->Visible; +} + +// Input event handlers +void App::OnPointerPressed(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::PointerEventArgs^ args) +{ + auto props = args->CurrentPoint->Properties; + + // TODO: UWP Touch input/simulation. + if (args->CurrentPoint->PointerDevice->PointerDeviceType == Windows::Devices::Input::PointerDeviceType::Mouse) + { + if (props->IsLeftButtonPressed) UWPMouseButtonEvent(MOUSE_LEFT_BUTTON, true); + if (props->IsMiddleButtonPressed) UWPMouseButtonEvent(MOUSE_MIDDLE_BUTTON, true); + if (props->IsRightButtonPressed) UWPMouseButtonEvent(MOUSE_RIGHT_BUTTON, true); + } +} + +void App::OnPointerReleased(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::PointerEventArgs^ args) +{ + auto props = args->CurrentPoint->Properties; + + // TODO: UWP Touch input. + if (args->CurrentPoint->PointerDevice->PointerDeviceType == Windows::Devices::Input::PointerDeviceType::Mouse) + { + if (!props->IsLeftButtonPressed) UWPMouseButtonEvent(MOUSE_LEFT_BUTTON, false); + if (!props->IsMiddleButtonPressed) UWPMouseButtonEvent(MOUSE_MIDDLE_BUTTON, false); + if (!props->IsRightButtonPressed) UWPMouseButtonEvent(MOUSE_RIGHT_BUTTON, false); + } +} + +void App::OnPointerWheelChanged(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::PointerEventArgs^ args) +{ + UWPMouseWheelEvent(args->CurrentPoint->Properties->MouseWheelDelta); +} + +void App::OnPointerMoved(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::PointerEventArgs^ args) +{ + auto pos = args->CurrentPoint->Position; + UWPMousePosEvent((double)pos.X, (double)pos.Y); +} + +int App::GetRaylibKey(Windows::System::VirtualKey kVey) +{ + int actualKey = -1; + switch ((int)kVey)// TODO: Use VK enum and finish keys + { + case 0x08: actualKey = KEY_BACKSPACE; break; + case 0x20: actualKey = KEY_SPACE; break; + case 0x1B: actualKey = KEY_ESCAPE; break; + case 0x0D: actualKey = KEY_ENTER; break; + case 0x2E: actualKey = KEY_DELETE; break; + case 0x27: actualKey = KEY_RIGHT; break; + case 0x25: actualKey = KEY_LEFT; break; + case 0x28: actualKey = KEY_DOWN; break; + case 0x26: actualKey = KEY_UP; break; + case 0x70: actualKey = KEY_F1; break; + case 0x71: actualKey = KEY_F2; break; + case 0x72: actualKey = KEY_F3; break; + case 0x73: actualKey = KEY_F4; break; + case 0x74: actualKey = KEY_F5; break; + case 0x75: actualKey = KEY_F6; break; + case 0x76: actualKey = KEY_F7; break; + case 0x77: actualKey = KEY_F8; break; + case 0x78: actualKey = KEY_F9; break; + case 0x79: actualKey = KEY_F10; break; + case 0x7A: actualKey = KEY_F11; break; + case 0x7B: actualKey = KEY_F12; break; + case 0xA0: actualKey = KEY_LEFT_SHIFT; break; + case 0xA2: actualKey = KEY_LEFT_CONTROL; break; + case 0xA4: actualKey = KEY_LEFT_ALT; break; + case 0xA1: actualKey = KEY_RIGHT_SHIFT; break; + case 0xA3: actualKey = KEY_RIGHT_CONTROL; break; + case 0xA5: actualKey = KEY_RIGHT_ALT; break; + case 0x30: actualKey = KEY_ZERO; break; + case 0x31: actualKey = KEY_ONE; break; + case 0x32: actualKey = KEY_TWO; break; + case 0x33: actualKey = KEY_THREE; break; + case 0x34: actualKey = KEY_FOUR; break; + case 0x35: actualKey = KEY_FIVE; break; + case 0x36: actualKey = KEY_SIX; break; + case 0x37: actualKey = KEY_SEVEN; break; + case 0x38: actualKey = KEY_EIGHT; break; + case 0x39: actualKey = KEY_NINE; break; + case 0x41: actualKey = KEY_A; break; + case 0x42: actualKey = KEY_B; break; + case 0x43: actualKey = KEY_C; break; + case 0x44: actualKey = KEY_D; break; + case 0x45: actualKey = KEY_E; break; + case 0x46: actualKey = KEY_F; break; + case 0x47: actualKey = KEY_G; break; + case 0x48: actualKey = KEY_H; break; + case 0x49: actualKey = KEY_I; break; + case 0x4A: actualKey = KEY_J; break; + case 0x4B: actualKey = KEY_K; break; + case 0x4C: actualKey = KEY_L; break; + case 0x4D: actualKey = KEY_M; break; + case 0x4E: actualKey = KEY_N; break; + case 0x4F: actualKey = KEY_O; break; + case 0x50: actualKey = KEY_P; break; + case 0x51: actualKey = KEY_Q; break; + case 0x52: actualKey = KEY_R; break; + case 0x53: actualKey = KEY_S; break; + case 0x54: actualKey = KEY_T; break; + case 0x55: actualKey = KEY_U; break; + case 0x56: actualKey = KEY_V; break; + case 0x57: actualKey = KEY_W; break; + case 0x58: actualKey = KEY_X; break; + case 0x59: actualKey = KEY_Y; break; + case 0x5A: actualKey = KEY_Z; break; + } + return actualKey; +} + +void App::OnKeyDown(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ args) +{ + auto k = GetRaylibKey(args->VirtualKey); + if (k != -1) + UWPKeyDownEvent(k, true); +} + +void App::OnKeyUp(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ args) +{ + auto k = GetRaylibKey(args->VirtualKey); + if (k != -1) + UWPKeyDownEvent(k, false); +} + +// AppSource implementation +Windows::ApplicationModel::Core::IFrameworkView ^AppSource::CreateView() +{ + return ref new App(); } \ No newline at end of file diff --git a/projects/VS2017.UWP/raylib.App.UWP/App.h b/projects/VS2017.UWP/raylib.App.UWP/App.h index 7b96d16e0..b0d800025 100644 --- a/projects/VS2017.UWP/raylib.App.UWP/App.h +++ b/projects/VS2017.UWP/raylib.App.UWP/App.h @@ -4,22 +4,46 @@ #include "pch.h" -// Define what header we use for BaseApp.h -#define PCH "pch.h" - -// Enable hold hack -#define HOLDHACK - -#include "BaseApp.h" - namespace raylibUWP { - ref class App sealed : public BaseApp + ref class App sealed : public Windows::ApplicationModel::Core::IFrameworkView { - public: + public: + App(); - App(); + // IFrameworkView methods. + void Initialize(Windows::ApplicationModel::Core::CoreApplicationView^ applicationView) override; + void SetWindow(Windows::UI::Core::CoreWindow^ window) override; + void Load(Platform::String^ entryPoint) override; + void Run() override; + void Uninitialize() override; + private: + bool mWindowVisible = true; + bool mSuspended = false; - void Update() override; + void GameLoop(); + void ProcessGamepads(); + + // Helpers + int GetRaylibKey(Windows::System::VirtualKey kVey); + + // Events + void OnActivated(Windows::ApplicationModel::Core::CoreApplicationView^ applicationView, Windows::ApplicationModel::Activation::IActivatedEventArgs^ args); + void OnSuspending(Platform::Object^ sender, Windows::ApplicationModel::SuspendingEventArgs^ args); + void OnResuming(Platform::Object^ sender, Platform::Object^ args); + void OnWindowSizeChanged(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::WindowSizeChangedEventArgs^ args); + void OnVisibilityChanged(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::VisibilityChangedEventArgs^ args); + void OnPointerPressed(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::PointerEventArgs^ args); + void OnPointerReleased(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::PointerEventArgs^ args); + void OnPointerWheelChanged(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::PointerEventArgs^ args); + void OnPointerMoved(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::PointerEventArgs^ args); + void OnKeyDown(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ args); + void OnKeyUp(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ args); + }; + + ref class AppSource sealed : Windows::ApplicationModel::Core::IFrameworkViewSource + { + public: + Windows::ApplicationModel::Core::IFrameworkView^ CreateView() override; }; } diff --git a/projects/VS2017.UWP/raylib.App.UWP/BaseApp.h b/projects/VS2017.UWP/raylib.App.UWP/BaseApp.h deleted file mode 100644 index acc3423ae..000000000 --- a/projects/VS2017.UWP/raylib.App.UWP/BaseApp.h +++ /dev/null @@ -1,439 +0,0 @@ -/********************************************************************************************** -* -* raylib.BaseApp - UWP App generic code for managing interface between C and C++ -* -* LICENSE: zlib/libpng -* -* CONFIGURATION: -* -* #define PCH -* This defines what header is the PCH and needs to be included -* -* Copyright (c) 2013-2020 Ramon Santamaria (@raysan5) -* -* This software is provided "as-is", without any express or implied warranty. In no event -* will the authors be held liable for any damages arising from the use of this software. -* -* Permission is granted to anyone to use this software for any purpose, including commercial -* applications, and to alter it and redistribute it freely, subject to the following restrictions: -* -* 1. The origin of this software must not be misrepresented; you must not claim that you -* wrote the original software. If you use this software in a product, an acknowledgment -* in the product documentation would be appreciated but is not required. -* -* 2. Altered source versions must be plainly marked as such, and must not be misrepresented -* as being the original software. -* -* 3. This notice may not be removed or altered from any source distribution. -* -**********************************************************************************************/ - -#pragma once - -#if defined(PCH) -#include PCH -#endif - -#include -#include -#include -#include - -//EGL -#include - -#include "raylib.h" -#include "utils.h" - -using namespace Windows::ApplicationModel::Core; -using namespace Windows::ApplicationModel::Activation; -using namespace Windows::UI::Core; -using namespace Windows::UI::Input; -using namespace Windows::Devices::Input; -using namespace Windows::Foundation; -using namespace Windows::Foundation::Collections; -using namespace Windows::Gaming::Input; -using namespace Windows::Graphics::Display; -using namespace Microsoft::WRL; -using namespace Platform; - -// Stand-ins for "core.c" variables -#define MAX_GAMEPADS 4 // Max number of gamepads supported -#define MAX_GAMEPAD_BUTTONS 32 // Max bumber of buttons supported (per gamepad) -#define MAX_GAMEPAD_AXIS 8 // Max number of axis supported (per gamepad) - -// TODO: I want to remove this "BaseApp" thing and just implement this in App. - -//Base app implementation -ref class BaseApp : public Windows::ApplicationModel::Core::IFrameworkView -{ -public: - - // IFrameworkView Methods. - virtual void Initialize(Windows::ApplicationModel::Core::CoreApplicationView^ applicationView) - { - // Register event handlers for app lifecycle. This example includes Activated, so that we - // can make the CoreWindow active and start rendering on the window. - applicationView->Activated += ref new TypedEventHandler(this, &BaseApp::OnActivated); - - // Logic for other event handlers could go 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 - - CoreApplication::Resuming += ref new EventHandler(this, &BaseApp::OnResuming); - } - - virtual void SetWindow(Windows::UI::Core::CoreWindow^ window) - { - // Hook window events - window->SizeChanged += ref new TypedEventHandler(this, &BaseApp::OnWindowSizeChanged); - window->VisibilityChanged += ref new TypedEventHandler(this, &BaseApp::OnVisibilityChanged); - - // Hook mouse pointer events - window->PointerPressed += ref new TypedEventHandler(this, &BaseApp::PointerPressed); - window->PointerReleased += ref new Windows::Foundation::TypedEventHandler(this, &BaseApp::PointerReleased); - window->PointerWheelChanged += ref new TypedEventHandler(this, &BaseApp::PointerWheelChanged); - window->PointerMoved += ref new Windows::Foundation::TypedEventHandler(this, &BaseApp::PointerMoved); - - // Hook keyboard events. - window->KeyDown += ref new TypedEventHandler(this, &BaseApp::OnKeyDown); - window->KeyUp += ref new TypedEventHandler(this, &BaseApp::OnKeyUp); - - // The CoreWindow has been created, we can pass this to raylib for EGL context creation when it's time - UWPSetCoreWindowPtr((void*) window); - } - - virtual void Load(Platform::String^ entryPoint) {} - - void Setup(int width, int height) - { - // Set dimensions - this->width = width; - this->height = height; - } - - virtual void Run() - { - // Set up our UWP implementation - UWPSetQueryTimeFunc([]() - { - static auto timeStart = std::chrono::high_resolution_clock::now(); - auto delta = std::chrono::high_resolution_clock::now() - timeStart; - return (double)std::chrono::duration_cast(delta).count(); - }); - - UWPSetSleepFunc([](double seconds) { std::this_thread::sleep_for(std::chrono::duration(seconds)); }); - - UWPSetDisplaySizeFunc([](int* width, int* height) - { - // Get display dimensions - DisplayInformation^ dInfo = DisplayInformation::GetForCurrentView(); - *width = dInfo->ScreenWidthInRawPixels; - *height = dInfo->ScreenHeightInRawPixels; - }); - - UWPSetMouseHideFunc([]() - { - CoreWindow::GetForCurrentThread()->PointerCursor = nullptr; - }); - - UWPSetMouseShowFunc([]() - { - CoreWindow::GetForCurrentThread()->PointerCursor = ref new CoreCursor(CoreCursorType::Arrow, 0); - }); - - UWPSetMouseLockFunc([]() - { - CoreWindow::GetForCurrentThread()->PointerCursor = nullptr; - // TODO: - }); - - UWPSetMouseUnlockFunc([]() - { - CoreWindow::GetForCurrentThread()->PointerCursor = ref new CoreCursor(CoreCursorType::Arrow, 0); - // TODO: - }); - - UWPSetMouseSetPosFunc([](int x, int y) - { - CoreWindow^ window = CoreWindow::GetForCurrentThread(); - Point mousePosScreen = Point(x + window->Bounds.X, y + window->Bounds.Y); - window->PointerPosition = mousePosScreen; - }); - - // Set custom output handle - SetTraceLogCallback([](int logType, const char* text, va_list args) - { - std::string format = text; - - switch (logType) - { - case LOG_TRACE: format = std::string("TRACE: ") + format; break; - case LOG_DEBUG: format = std::string("DEBUG: ") + format; break; - case LOG_INFO: format = std::string("INFO: ") + format; break; - case LOG_WARNING: format = std::string("WARNING: ") + format; break; - case LOG_ERROR: format = std::string("ERROR: ") + format; break; - case LOG_FATAL: format = std::string("FATAL: ") + format; break; - default: break; - } - - char buf[1024]; // TODO: Is this large enough? - vsnprintf(buf, sizeof(buf), format.c_str(), args); - std::string output = std::string(buf) + std::string("\n"); - OutputDebugStringA(output.c_str()); - }); - - // Create window - InitWindow(width, height, "raylib game example"); - - // Set fps if 0 - if (GetFPS() <= 0) SetTargetFPS(60); - - while (!WindowShouldClose()) - { - if (mWindowVisible) - { - // Call update function - Update(); - - PollInput(); - - CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent); - } - else - { - CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneAndAllPending); - } - } - - CloseWindow(); - } - - //Called every frame (Maybe add draw) - virtual void Update() {} - - virtual void Uninitialize() {} - -protected: - - // Input polling - void PollInput() - { - // Process Gamepads - { - // Check if gamepads are ready - for (int i = 0; i < MAX_GAMEPADS; i++) - { - // TODO: ROVER - I want to remove this problem if possible. - // 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 - // 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. - UWPActivateGamepadEvent(i, i < Gamepad::Gamepads->Size); - } - - // Get current gamepad state - for (int i = 0; i < MAX_GAMEPADS; i++) - { - if (IsGamepadAvailable(i)) - { - // Get current gamepad state - auto gamepad = Gamepad::Gamepads->GetAt(i); - 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 - UWPRegisterGamepadButton(i, GAMEPAD_BUTTON_RIGHT_FACE_DOWN, ((reading.Buttons & GamepadButtons::A) == GamepadButtons::A)); - UWPRegisterGamepadButton(i, GAMEPAD_BUTTON_RIGHT_FACE_RIGHT, ((reading.Buttons & GamepadButtons::B) == GamepadButtons::B)); - UWPRegisterGamepadButton(i, GAMEPAD_BUTTON_RIGHT_FACE_LEFT, ((reading.Buttons & GamepadButtons::X) == GamepadButtons::X)); - UWPRegisterGamepadButton(i, GAMEPAD_BUTTON_RIGHT_FACE_UP, ((reading.Buttons & GamepadButtons::Y) == GamepadButtons::Y)); - - UWPRegisterGamepadButton(i, GAMEPAD_BUTTON_LEFT_TRIGGER_1, ((reading.Buttons & GamepadButtons::LeftShoulder) == GamepadButtons::LeftShoulder)); - UWPRegisterGamepadButton(i, GAMEPAD_BUTTON_RIGHT_TRIGGER_1, ((reading.Buttons & GamepadButtons::RightShoulder) == GamepadButtons::RightShoulder)); - - UWPRegisterGamepadButton(i, GAMEPAD_BUTTON_MIDDLE_LEFT, ((reading.Buttons & GamepadButtons::View) == GamepadButtons::View)); // Changed for XB1 Controller - UWPRegisterGamepadButton(i, GAMEPAD_BUTTON_MIDDLE_RIGHT, ((reading.Buttons & GamepadButtons::Menu) == GamepadButtons::Menu)); // Changed for XB1 Controller - - UWPRegisterGamepadButton(i, GAMEPAD_BUTTON_LEFT_FACE_UP, ((reading.Buttons & GamepadButtons::DPadUp) == GamepadButtons::DPadUp)); - UWPRegisterGamepadButton(i, GAMEPAD_BUTTON_LEFT_FACE_RIGHT, ((reading.Buttons & GamepadButtons::DPadRight) == GamepadButtons::DPadRight)); - UWPRegisterGamepadButton(i, GAMEPAD_BUTTON_LEFT_FACE_DOWN, ((reading.Buttons & GamepadButtons::DPadDown) == GamepadButtons::DPadDown)); - UWPRegisterGamepadButton(i, GAMEPAD_BUTTON_LEFT_FACE_LEFT, ((reading.Buttons & GamepadButtons::DPadLeft) == GamepadButtons::DPadLeft)); - UWPRegisterGamepadButton(i, GAMEPAD_BUTTON_MIDDLE, false); // Home button not supported by UWP - - // Get current axis state - UWPRegisterGamepadAxis(i, GAMEPAD_AXIS_LEFT_X, (float)reading.LeftThumbstickX); - UWPRegisterGamepadAxis(i, GAMEPAD_AXIS_LEFT_Y, (float)reading.LeftThumbstickY); - UWPRegisterGamepadAxis(i, GAMEPAD_AXIS_RIGHT_X, (float)reading.RightThumbstickX); - UWPRegisterGamepadAxis(i, GAMEPAD_AXIS_RIGHT_Y, (float)reading.RightThumbstickY); - UWPRegisterGamepadAxis(i, GAMEPAD_AXIS_LEFT_TRIGGER, (float)reading.LeftTrigger); - UWPRegisterGamepadAxis(i, GAMEPAD_AXIS_RIGHT_TRIGGER, (float)reading.RightTrigger); - } - } - } - } - - // Application lifecycle event handlers. - void OnActivated(Windows::ApplicationModel::Core::CoreApplicationView^ applicationView, Windows::ApplicationModel::Activation::IActivatedEventArgs^ args) - { - // Run() won't start until the CoreWindow is activated. - CoreWindow::GetForCurrentThread()->Activate(); - } - - void OnResuming(Platform::Object^ sender, Platform::Object^ args) - { - // TODO: Suspend and Resume lifecycle. This should be implemented by the developer, however we need to implement an example of it here. - // Basically, we'd save all resources here and free any temporary stuff as if we were about to close, however *if* resume is called, we need to be ready. - } - - // Window event handlers. - void OnWindowSizeChanged(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::WindowSizeChangedEventArgs^ args) - { - UWPResizeEvent(args->Size.Width, args->Size.Height); - } - - void OnVisibilityChanged(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::VisibilityChangedEventArgs^ args) - { - mWindowVisible = args->Visible; - } - - // Input event handlers - void PointerPressed(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::PointerEventArgs^ args) - { - auto props = args->CurrentPoint->Properties; - - // TODO: UWP Touch input/simulation. - if (args->CurrentPoint->PointerDevice->PointerDeviceType == Windows::Devices::Input::PointerDeviceType::Mouse) - { - if (props->IsLeftButtonPressed) UWPMouseButtonEvent(MOUSE_LEFT_BUTTON, true); - if (props->IsMiddleButtonPressed) UWPMouseButtonEvent(MOUSE_MIDDLE_BUTTON, true); - if (props->IsRightButtonPressed) UWPMouseButtonEvent(MOUSE_RIGHT_BUTTON, true); - } - } - - void PointerReleased(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::PointerEventArgs^ args) - { - auto props = args->CurrentPoint->Properties; - - // TODO: UWP Touch input. - if (args->CurrentPoint->PointerDevice->PointerDeviceType == Windows::Devices::Input::PointerDeviceType::Mouse) - { - if (!props->IsLeftButtonPressed) UWPMouseButtonEvent(MOUSE_LEFT_BUTTON, false); - if (!props->IsMiddleButtonPressed) UWPMouseButtonEvent(MOUSE_MIDDLE_BUTTON, false); - if (!props->IsRightButtonPressed) UWPMouseButtonEvent(MOUSE_RIGHT_BUTTON, false); - } - } - - void PointerWheelChanged(Windows::UI::Core::CoreWindow ^sender, Windows::UI::Core::PointerEventArgs^ args) - { - UWPMouseWheelEvent(args->CurrentPoint->Properties->MouseWheelDelta); - } - - void PointerMoved(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::PointerEventArgs^ args) - { - auto pos = args->CurrentPoint->Position; - UWPMousePosEvent((double)pos.X, (double)pos.Y); - } - - int GetRaylibKey(Windows::System::VirtualKey kVey) - { - int actualKey = -1; - switch ((int) kVey)// TODO: Use VK enum and finish keys - { - case 0x08: actualKey = KEY_BACKSPACE; break; - case 0x20: actualKey = KEY_SPACE; break; - case 0x1B: actualKey = KEY_ESCAPE; break; - case 0x0D: actualKey = KEY_ENTER; break; - case 0x2E: actualKey = KEY_DELETE; break; - case 0x27: actualKey = KEY_RIGHT; break; - case 0x25: actualKey = KEY_LEFT; break; - case 0x28: actualKey = KEY_DOWN; break; - case 0x26: actualKey = KEY_UP; break; - case 0x70: actualKey = KEY_F1; break; - case 0x71: actualKey = KEY_F2; break; - case 0x72: actualKey = KEY_F3; break; - case 0x73: actualKey = KEY_F4; break; - case 0x74: actualKey = KEY_F5; break; - case 0x75: actualKey = KEY_F6; break; - case 0x76: actualKey = KEY_F7; break; - case 0x77: actualKey = KEY_F8; break; - case 0x78: actualKey = KEY_F9; break; - case 0x79: actualKey = KEY_F10; break; - case 0x7A: actualKey = KEY_F11; break; - case 0x7B: actualKey = KEY_F12; break; - case 0xA0: actualKey = KEY_LEFT_SHIFT; break; - case 0xA2: actualKey = KEY_LEFT_CONTROL; break; - case 0xA4: actualKey = KEY_LEFT_ALT; break; - case 0xA1: actualKey = KEY_RIGHT_SHIFT; break; - case 0xA3: actualKey = KEY_RIGHT_CONTROL; break; - case 0xA5: actualKey = KEY_RIGHT_ALT; break; - case 0x30: actualKey = KEY_ZERO; break; - case 0x31: actualKey = KEY_ONE; break; - case 0x32: actualKey = KEY_TWO; break; - case 0x33: actualKey = KEY_THREE; break; - case 0x34: actualKey = KEY_FOUR; break; - case 0x35: actualKey = KEY_FIVE; break; - case 0x36: actualKey = KEY_SIX; break; - case 0x37: actualKey = KEY_SEVEN; break; - case 0x38: actualKey = KEY_EIGHT; break; - case 0x39: actualKey = KEY_NINE; break; - case 0x41: actualKey = KEY_A; break; - case 0x42: actualKey = KEY_B; break; - case 0x43: actualKey = KEY_C; break; - case 0x44: actualKey = KEY_D; break; - case 0x45: actualKey = KEY_E; break; - case 0x46: actualKey = KEY_F; break; - case 0x47: actualKey = KEY_G; break; - case 0x48: actualKey = KEY_H; break; - case 0x49: actualKey = KEY_I; break; - case 0x4A: actualKey = KEY_J; break; - case 0x4B: actualKey = KEY_K; break; - case 0x4C: actualKey = KEY_L; break; - case 0x4D: actualKey = KEY_M; break; - case 0x4E: actualKey = KEY_N; break; - case 0x4F: actualKey = KEY_O; break; - case 0x50: actualKey = KEY_P; break; - case 0x51: actualKey = KEY_Q; break; - case 0x52: actualKey = KEY_R; break; - case 0x53: actualKey = KEY_S; break; - case 0x54: actualKey = KEY_T; break; - case 0x55: actualKey = KEY_U; break; - case 0x56: actualKey = KEY_V; break; - case 0x57: actualKey = KEY_W; break; - case 0x58: actualKey = KEY_X; break; - case 0x59: actualKey = KEY_Y; break; - case 0x5A: actualKey = KEY_Z; break; - } - return actualKey; - } - - void OnKeyDown(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Core::KeyEventArgs ^ args) - { - auto k = GetRaylibKey(args->VirtualKey); - if (k != -1) - UWPKeyDownEvent(k, true); - } - - void OnKeyUp(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Core::KeyEventArgs ^ args) - { - auto k = GetRaylibKey(args->VirtualKey); - if (k != -1) - UWPKeyDownEvent(k, false); - } - -private: - bool mWindowClosed = false; - bool mWindowVisible = true; - - int width = 640; - int height = 480; - -}; - -// Application source for creating the program -template -ref class ApplicationSource sealed : Windows::ApplicationModel::Core::IFrameworkViewSource -{ -public: - virtual Windows::ApplicationModel::Core::IFrameworkView^ CreateView() - { - return ref new AppType(); - } -}; diff --git a/projects/VS2017.UWP/raylib.App.UWP/raylib.App.UWP.vcxproj b/projects/VS2017.UWP/raylib.App.UWP/raylib.App.UWP.vcxproj index 636d0924d..cddbe0dc7 100644 --- a/projects/VS2017.UWP/raylib.App.UWP/raylib.App.UWP.vcxproj +++ b/projects/VS2017.UWP/raylib.App.UWP/raylib.App.UWP.vcxproj @@ -139,7 +139,6 @@ - diff --git a/projects/VS2017.UWP/raylib.App.UWP/raylib.App.UWP.vcxproj.filters b/projects/VS2017.UWP/raylib.App.UWP/raylib.App.UWP.vcxproj.filters index 00f8798a4..ce747ba68 100644 --- a/projects/VS2017.UWP/raylib.App.UWP/raylib.App.UWP.vcxproj.filters +++ b/projects/VS2017.UWP/raylib.App.UWP/raylib.App.UWP.vcxproj.filters @@ -6,7 +6,6 @@ - diff --git a/projects/VS2017.UWP/raylib.UWP/raylib.UWP.vcxproj b/projects/VS2017.UWP/raylib.UWP/raylib.UWP.vcxproj index d19ed88f4..c2909a393 100644 --- a/projects/VS2017.UWP/raylib.UWP/raylib.UWP.vcxproj +++ b/projects/VS2017.UWP/raylib.UWP/raylib.UWP.vcxproj @@ -42,6 +42,7 @@ + diff --git a/projects/VS2017.UWP/raylib.UWP/raylib.UWP.vcxproj.filters b/projects/VS2017.UWP/raylib.UWP/raylib.UWP.vcxproj.filters index 8f939b309..8050a1ef3 100644 --- a/projects/VS2017.UWP/raylib.UWP/raylib.UWP.vcxproj.filters +++ b/projects/VS2017.UWP/raylib.UWP/raylib.UWP.vcxproj.filters @@ -16,6 +16,7 @@ + diff --git a/src/core.c b/src/core.c index 5b1954e55..b0bb6b3aa 100644 --- a/src/core.c +++ b/src/core.c @@ -255,6 +255,7 @@ #include "EGL/egl.h" // Khronos EGL library - Native platform display device control functions #include "EGL/eglext.h" // Khronos EGL library - Extensions #include "GLES2/gl2.h" // Khronos OpenGL ES 2.0 library + #include "uwp_events.h" // UWP bootstrapping functions #endif #if defined(PLATFORM_WEB) @@ -1249,9 +1250,7 @@ void EnableCursor(void) CORE.Input.Mouse.cursorLockRequired = true; #endif #if defined(PLATFORM_UWP) - UWPMessage *msg = CreateUWPMessage(); - msg->type = UWP_MSG_LOCK_MOUSE; - SendMessageToUWP(msg); + UWPGetMouseUnlockFunc()(); #endif CORE.Input.Mouse.cursorHidden = false; } @@ -1266,9 +1265,7 @@ void DisableCursor(void) CORE.Input.Mouse.cursorLockRequired = true; #endif #if defined(PLATFORM_UWP) - UWPMessage *msg = CreateUWPMessage(); - msg->type = UWP_MSG_UNLOCK_MOUSE; - SendMessageToUWP(msg); + UWPGetMouseLockFunc()(); #endif CORE.Input.Mouse.cursorHidden = true; } @@ -2640,11 +2637,7 @@ void SetMousePosition(int x, int y) glfwSetCursorPos(CORE.Window.handle, CORE.Input.Mouse.position.x, CORE.Input.Mouse.position.y); #endif #if defined(PLATFORM_UWP) - UWPMessage *msg = CreateUWPMessage(); - msg->type = UWP_MSG_SET_MOUSE_LOCATION; - msg->paramVector0.x = CORE.Input.Mouse.position.x; - msg->paramVector0.y = CORE.Input.Mouse.position.y; - SendMessageToUWP(msg); + UWPGetMouseSetPosFunc()(x, y); #endif } @@ -5139,6 +5132,165 @@ static void *GamepadThread(void *arg) #if defined(PLATFORM_UWP) +static UWPQueryTimeFunc uwpQueryTimeFunc = NULL; +static UWPSleepFunc uwpSleepFunc = NULL; +static UWPDisplaySizeFunc uwpDisplaySizeFunc = NULL; +static UWPMouseFunc uwpMouseLockFunc = NULL; +static UWPMouseFunc uwpMouseUnlockFunc = NULL; +static UWPMouseFunc uwpMouseShowFunc = NULL; +static UWPMouseFunc uwpMouseHideFunc = NULL; +static UWPMouseSetPosFunc uwpMouseSetPosFunc = NULL; +static void* uwpCoreWindow = NULL; + +bool UWPIsConfigured() +{ + bool pass = true; + if (uwpQueryTimeFunc == NULL) + { + TRACELOG(LOG_ERROR, "You must call UWPSetQueryTimeFunc with a valid function before calling InitWindow()"); + pass = false; + } + + if (uwpSleepFunc == NULL) + { + TRACELOG(LOG_ERROR, "You must call UWPSetSleepFunc with a valid function before calling InitWindow()"); + pass = false; + } + + if (uwpDisplaySizeFunc == NULL) + { + TRACELOG(LOG_ERROR, "You must call UWPSetDisplaySizeFunc with a valid function before calling InitWindow()"); + pass = false; + } + + if (uwpMouseLockFunc == NULL) + { + TRACELOG(LOG_ERROR, "You must call UWPSetMouseLockFunc with a valid function before calling InitWindow()"); + pass = false; + } + + if (uwpMouseUnlockFunc == NULL) + { + TRACELOG(LOG_ERROR, "You must call UWPSetMouseUnlockFunc with a valid function before calling InitWindow()"); + pass = false; + } + + if (uwpMouseShowFunc == NULL) + { + TRACELOG(LOG_ERROR, "You must call UWPSetMouseShowFunc with a valid function before calling InitWindow()"); + pass = false; + } + + if (uwpMouseHideFunc == NULL) + { + TRACELOG(LOG_ERROR, "You must call UWPSetMouseHideFunc with a valid function before calling InitWindow()"); + pass = false; + } + + if (uwpMouseSetPosFunc == NULL) + { + TRACELOG(LOG_ERROR, "You must call UWPSetMouseSetPosFunc with a valid function before calling InitWindow()"); + pass = false; + } + + if (uwpCoreWindow == NULL) + { + TRACELOG(LOG_ERROR, "You must set a pointer to the UWP core window before calling InitWindow()"); + pass = false; + } + return pass; +} + +UWPQueryTimeFunc UWPGetQueryTimeFunc(void) +{ + return uwpQueryTimeFunc; +} + +void UWPSetQueryTimeFunc(UWPQueryTimeFunc func) +{ + uwpQueryTimeFunc = func; +} + +UWPSleepFunc UWPGetSleepFunc(void) +{ + return uwpSleepFunc; +} + +void UWPSetSleepFunc(UWPSleepFunc func) +{ + uwpSleepFunc = func; +} + +UWPDisplaySizeFunc UWPGetDisplaySizeFunc(void) +{ + return uwpDisplaySizeFunc; +} + +void UWPSetDisplaySizeFunc(UWPDisplaySizeFunc func) +{ + uwpDisplaySizeFunc = func; +} + +UWPMouseFunc UWPGetMouseLockFunc() +{ + return uwpMouseLockFunc; +} + +void UWPSetMouseLockFunc(UWPMouseFunc func) +{ + uwpMouseLockFunc = func; +} + +UWPMouseFunc UWPGetMouseUnlockFunc() +{ + return uwpMouseUnlockFunc; +} + +void UWPSetMouseUnlockFunc(UWPMouseFunc func) +{ + uwpMouseUnlockFunc = func; +} + +UWPMouseFunc UWPGetMouseShowFunc() +{ + return uwpMouseShowFunc; +} + +void UWPSetMouseShowFunc(UWPMouseFunc func) +{ + uwpMouseShowFunc = func; +} + +UWPMouseFunc UWPGetMouseHideFunc() +{ + return uwpMouseHideFunc; +} + +void UWPSetMouseHideFunc(UWPMouseFunc func) +{ + uwpMouseHideFunc = func; +} + +UWPMouseSetPosFunc UWPGetMouseSetPosFunc() +{ + return uwpMouseSetPosFunc; +} + +void UWPSetMouseSetPosFunc(UWPMouseSetPosFunc func) +{ + uwpMouseSetPosFunc = func; +} + +void* UWPGetCoreWindowPtr() +{ + return uwpCoreWindow; +} + +void UWPSetCoreWindowPtr(void* ptr) +{ + uwpCoreWindow = ptr; +} + void UWPMouseWheelEvent(int deltaY) { CORE.Input.Mouse.currentWheelMove = (int)deltaY; diff --git a/src/utils.c b/src/utils.c index 719698409..74b33b501 100644 --- a/src/utils.c +++ b/src/utils.c @@ -68,13 +68,6 @@ static AAssetManager *assetManager = NULL; // Android assets manage static const char *internalDataPath = NULL; // Android internal data path #endif -#if defined(PLATFORM_UWP) -static int UWPOutMessageId = -1; // Last index of output message -static UWPMessage *UWPOutMessages[MAX_UWP_MESSAGES]; // Messages out to UWP -static int UWPInMessageId = -1; // Last index of input message -static UWPMessage *UWPInMessages[MAX_UWP_MESSAGES]; // Messages in from UWP -#endif - //---------------------------------------------------------------------------------- // Module specific Functions Declaration //---------------------------------------------------------------------------------- @@ -361,210 +354,3 @@ static int android_close(void *cookie) return 0; } #endif // PLATFORM_ANDROID - -#if defined(PLATFORM_UWP) -UWPMessage *CreateUWPMessage(void) -{ - UWPMessage *msg = (UWPMessage *)RL_MALLOC(sizeof(UWPMessage)); - msg->type = UWP_MSG_NONE; - Vector2 v0 = { 0, 0 }; - msg->paramVector0 = v0; - msg->paramInt0 = 0; - msg->paramInt1 = 0; - msg->paramChar0 = 0; - msg->paramFloat0 = 0; - msg->paramDouble0 = 0; - msg->paramBool0 = false; - return msg; -} - -void DeleteUWPMessage(UWPMessage *msg) -{ - RL_FREE(msg); -} - -bool UWPHasMessages(void) -{ - return (UWPOutMessageId > -1); -} - -UWPMessage *UWPGetMessage(void) -{ - if (UWPHasMessages()) return UWPOutMessages[UWPOutMessageId--]; - - return NULL; -} - -void SendMessageToUWP(UWPMessage *msg) -{ - if ((UWPOutMessageId + 1) < MAX_UWP_MESSAGES) - { - UWPOutMessageId++; - UWPOutMessages[UWPOutMessageId] = msg; - } - else TRACELOG(LOG_WARNING, "UWP: Not enough array space to register new outward message"); -} - - -// NEW UWP API - -static UWPQueryTimeFunc uwpQueryTimeFunc = NULL; -static UWPSleepFunc uwpSleepFunc = NULL; -static UWPDisplaySizeFunc uwpDisplaySizeFunc = NULL; -static UWPMouseFunc uwpMouseLockFunc = NULL; -static UWPMouseFunc uwpMouseUnlockFunc = NULL; -static UWPMouseFunc uwpMouseShowFunc = NULL; -static UWPMouseFunc uwpMouseHideFunc = NULL; -static UWPMouseSetPosFunc uwpMouseSetPosFunc = NULL; -static void* uwpCoreWindow = NULL; - -bool UWPIsConfigured() -{ - bool pass = true; - if (uwpQueryTimeFunc == NULL) - { - TRACELOG(LOG_ERROR, "You must call UWPSetQueryTimeFunc with a valid function before calling InitWindow()"); - pass = false; - } - - if (uwpSleepFunc == NULL) - { - TRACELOG(LOG_ERROR, "You must call UWPSetSleepFunc with a valid function before calling InitWindow()"); - pass = false; - } - - if (uwpDisplaySizeFunc == NULL) - { - TRACELOG(LOG_ERROR, "You must call UWPSetDisplaySizeFunc with a valid function before calling InitWindow()"); - pass = false; - } - - if (uwpMouseLockFunc == NULL) - { - TRACELOG(LOG_ERROR, "You must call UWPSetMouseLockFunc with a valid function before calling InitWindow()"); - pass = false; - } - - if (uwpMouseUnlockFunc == NULL) - { - TRACELOG(LOG_ERROR, "You must call UWPSetMouseUnlockFunc with a valid function before calling InitWindow()"); - pass = false; - } - - if (uwpMouseShowFunc == NULL) - { - TRACELOG(LOG_ERROR, "You must call UWPSetMouseShowFunc with a valid function before calling InitWindow()"); - pass = false; - } - - if (uwpMouseHideFunc == NULL) - { - TRACELOG(LOG_ERROR, "You must call UWPSetMouseHideFunc with a valid function before calling InitWindow()"); - pass = false; - } - - if (uwpMouseSetPosFunc == NULL) - { - TRACELOG(LOG_ERROR, "You must call UWPSetMouseSetPosFunc with a valid function before calling InitWindow()"); - pass = false; - } - - if (uwpCoreWindow == NULL) - { - TRACELOG(LOG_ERROR, "You must set a pointer to the UWP core window before calling InitWindow()"); - pass = false; - } - return pass; -} - -UWPQueryTimeFunc UWPGetQueryTimeFunc(void) -{ - return uwpQueryTimeFunc; -} - -void UWPSetQueryTimeFunc(UWPQueryTimeFunc func) -{ - uwpQueryTimeFunc = func; -} - -UWPSleepFunc UWPGetSleepFunc(void) -{ - return uwpSleepFunc; -} - -void UWPSetSleepFunc(UWPSleepFunc func) -{ - uwpSleepFunc = func; -} - -UWPDisplaySizeFunc UWPGetDisplaySizeFunc(void) -{ - return uwpDisplaySizeFunc; -} - -void UWPSetDisplaySizeFunc(UWPDisplaySizeFunc func) -{ - uwpDisplaySizeFunc = func; -} - -UWPMouseFunc UWPGetMouseLockFunc() -{ - return uwpMouseLockFunc; -} - -void UWPSetMouseLockFunc(UWPMouseFunc func) -{ - uwpMouseLockFunc = func; -} - -UWPMouseFunc UWPGetMouseUnlockFunc() -{ - return uwpMouseUnlockFunc; -} - -void UWPSetMouseUnlockFunc(UWPMouseFunc func) -{ - uwpMouseUnlockFunc = func; -} - -UWPMouseFunc UWPGetMouseShowFunc() -{ - return uwpMouseShowFunc; -} - -void UWPSetMouseShowFunc(UWPMouseFunc func) -{ - uwpMouseShowFunc = func; -} - -UWPMouseFunc UWPGetMouseHideFunc() -{ - return uwpMouseHideFunc; -} - -void UWPSetMouseHideFunc(UWPMouseFunc func) -{ - uwpMouseHideFunc = func; -} - -UWPMouseSetPosFunc UWPGetMouseSetPosFunc() -{ - return uwpMouseSetPosFunc; -} - -void UWPSetMouseSetPosFunc(UWPMouseSetPosFunc func) -{ - uwpMouseSetPosFunc = func; -} - -void* UWPGetCoreWindowPtr() -{ - return uwpCoreWindow; -} - -void UWPSetCoreWindowPtr(void* ptr) -{ - uwpCoreWindow = ptr; -} - -#endif // PLATFORM_UWP diff --git a/src/utils.h b/src/utils.h index 666556d5b..40ee53a48 100644 --- a/src/utils.h +++ b/src/utils.h @@ -72,76 +72,6 @@ void InitAssetManager(AAssetManager *manager, const char *dataPath); // Initia FILE *android_fopen(const char *fileName, const char *mode); // Replacement for fopen() -> Read-only! #endif -#if defined(PLATFORM_UWP) -// UWP Implementation Features - -// Determine if UWP functions are set and ready for raylib's use. -bool UWPIsConfigured(); - -// Function for getting program time. -typedef double(*UWPQueryTimeFunc)(); -UWPQueryTimeFunc UWPGetQueryTimeFunc(void); -void UWPSetQueryTimeFunc(UWPQueryTimeFunc func); - -// Function for sleeping the current thread -typedef void (*UWPSleepFunc)(double sleepUntil); -UWPSleepFunc UWPGetSleepFunc(void); -void UWPSetSleepFunc(UWPSleepFunc func); - -// Function for querying the display size -typedef void(*UWPDisplaySizeFunc)(int *width, int* height); -UWPDisplaySizeFunc UWPGetDisplaySizeFunc(void); -void UWPSetDisplaySizeFunc(UWPDisplaySizeFunc func); - -// Functions for mouse cursor control -typedef void(*UWPMouseFunc)(void); -UWPMouseFunc UWPGetMouseLockFunc(); -void UWPSetMouseLockFunc(UWPMouseFunc func); -UWPMouseFunc UWPGetMouseUnlockFunc(); -void UWPSetMouseUnlockFunc(UWPMouseFunc func); -UWPMouseFunc UWPGetMouseShowFunc(); -void UWPSetMouseShowFunc(UWPMouseFunc func); -UWPMouseFunc UWPGetMouseHideFunc(); -void UWPSetMouseHideFunc(UWPMouseFunc func); - -// Function for setting mouse cursor position. -typedef void (*UWPMouseSetPosFunc)(int x, int y); -UWPMouseSetPosFunc UWPGetMouseSetPosFunc(); -void UWPSetMouseSetPosFunc(UWPMouseSetPosFunc func); - -// The below functions are implemented in core.c but are placed here so they can be called by user code. -// This choice is made as platform-specific code is preferred to be kept away from raylib.h - -// Call this when a Key is pressed or released. -void UWPKeyDownEvent(int key, bool down); - -// Call when a mouse button state changes -void UWPMouseButtonEvent(int button, bool down); - -// Call when the mouse cursor moves -void UWPMousePosEvent(double x, double y); - -// Call when the mouse wheel moves -void UWPMouseWheelEvent(int deltaY); - -// Call when the window resizes -void UWPResizeEvent(int width, int height); - -// Call when a gamepad is made active -void UWPActivateGamepadEvent(int gamepad, bool active); - -// Call when a gamepad button state changes -void UWPRegisterGamepadButton(int gamepad, int button, bool down); - -// Call when a gamepad axis state changes -void UWPRegisterGamepadAxis(int gamepad, int axis, float value); - -// Set the core window pointer so that we can pass it to EGL. -void* UWPGetCoreWindowPtr(); -void UWPSetCoreWindowPtr(void* ptr); - -#endif // defined(PLATFORM_UWP) - #ifdef __cplusplus } #endif diff --git a/src/uwp_events.h b/src/uwp_events.h new file mode 100644 index 000000000..f4494c131 --- /dev/null +++ b/src/uwp_events.h @@ -0,0 +1,107 @@ +/********************************************************************************************** +* +* raylib.uwp_events - Functions for bootstrapping UWP functionality within raylib's core. +* +* +* LICENSE: zlib/libpng +* +* Copyright (c) 2014-2020 Ramon Santamaria (@raysan5) +* +* This software is provided "as-is", without any express or implied warranty. In no event +* will the authors be held liable for any damages arising from the use of this software. +* +* Permission is granted to anyone to use this software for any purpose, including commercial +* applications, and to alter it and redistribute it freely, subject to the following restrictions: +* +* 1. The origin of this software must not be misrepresented; you must not claim that you +* wrote the original software. If you use this software in a product, an acknowledgment +* in the product documentation would be appreciated but is not required. +* +* 2. Altered source versions must be plainly marked as such, and must not be misrepresented +* as being the original software. +* +* 3. This notice may not be removed or altered from any source distribution. +* +**********************************************************************************************/ + +#ifndef UWP_EVENTS_H +#define UWP_EVENTS_H + +#if defined(__cplusplus) +extern "C" { +#endif + +#if defined(PLATFORM_UWP) + +// Determine if UWP functions are set and ready for raylib's use. +bool UWPIsConfigured(); + +// Function for getting program time. +typedef double(*UWPQueryTimeFunc)(); +UWPQueryTimeFunc UWPGetQueryTimeFunc(void); +void UWPSetQueryTimeFunc(UWPQueryTimeFunc func); + +// Function for sleeping the current thread +typedef void (*UWPSleepFunc)(double sleepUntil); +UWPSleepFunc UWPGetSleepFunc(void); +void UWPSetSleepFunc(UWPSleepFunc func); + +// Function for querying the display size +typedef void(*UWPDisplaySizeFunc)(int* width, int* height); +UWPDisplaySizeFunc UWPGetDisplaySizeFunc(void); +void UWPSetDisplaySizeFunc(UWPDisplaySizeFunc func); + +// Functions for mouse cursor control +typedef void(*UWPMouseFunc)(void); +UWPMouseFunc UWPGetMouseLockFunc(); +void UWPSetMouseLockFunc(UWPMouseFunc func); +UWPMouseFunc UWPGetMouseUnlockFunc(); +void UWPSetMouseUnlockFunc(UWPMouseFunc func); +UWPMouseFunc UWPGetMouseShowFunc(); +void UWPSetMouseShowFunc(UWPMouseFunc func); +UWPMouseFunc UWPGetMouseHideFunc(); +void UWPSetMouseHideFunc(UWPMouseFunc func); + +// Function for setting mouse cursor position. +typedef void (*UWPMouseSetPosFunc)(int x, int y); +UWPMouseSetPosFunc UWPGetMouseSetPosFunc(); +void UWPSetMouseSetPosFunc(UWPMouseSetPosFunc func); + +// The below functions are implemented in core.c but are placed here so they can be called by user code. +// This choice is made as platform-specific code is preferred to be kept away from raylib.h + +// Call this when a Key is pressed or released. +void UWPKeyDownEvent(int key, bool down); + +// Call when a mouse button state changes +void UWPMouseButtonEvent(int button, bool down); + +// Call when the mouse cursor moves +void UWPMousePosEvent(double x, double y); + +// Call when the mouse wheel moves +void UWPMouseWheelEvent(int deltaY); + +// Call when the window resizes +void UWPResizeEvent(int width, int height); + +// Call when a gamepad is made active +void UWPActivateGamepadEvent(int gamepad, bool active); + +// Call when a gamepad button state changes +void UWPRegisterGamepadButton(int gamepad, int button, bool down); + +// Call when a gamepad axis state changes +void UWPRegisterGamepadAxis(int gamepad, int axis, float value); + +// Set the core window pointer so that we can pass it to EGL. +void* UWPGetCoreWindowPtr(); +void UWPSetCoreWindowPtr(void* ptr); + +#if defined(__cplusplus) +} +#endif + +#endif // PLATFORM_UWP + +#endif // UWP_EVENTS_H