From 6e7bac7b34a86f4e8e1a663631d5f260c064ac0a Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Tue, 6 Feb 2018 17:03:16 -0800 Subject: Reworked Win32 native app implementation --- Common/NativeApp/CMakeLists.txt | 197 +++++++++++++++++++++++++++++++ Common/NativeApp/include/NativeAppBase.h | 50 ++++++++ Common/NativeApp/include/NativeAppData.h | 38 ++++++ 3 files changed, 285 insertions(+) create mode 100644 Common/NativeApp/CMakeLists.txt create mode 100644 Common/NativeApp/include/NativeAppBase.h create mode 100644 Common/NativeApp/include/NativeAppData.h (limited to 'Common/NativeApp') diff --git a/Common/NativeApp/CMakeLists.txt b/Common/NativeApp/CMakeLists.txt new file mode 100644 index 0000000..99bad03 --- /dev/null +++ b/Common/NativeApp/CMakeLists.txt @@ -0,0 +1,197 @@ +cmake_minimum_required (VERSION 3.6) + +project(NativeAppBase) + +if(PLATFORM_WIN32) + set(SOURCE + src/Win32/WinMain.cpp + ) +elseif(PLATFORM_UNIVERSAL_WINDOWS) + + # Windows Runtime types cannot be included into static libraries + # https://social.msdn.microsoft.com/Forums/en-US/269db513-64ef-4817-a025-43954f614eb3/lnk4264-why-are-static-libraries-not-recommended-when-authoring-windows-runtime-types?forum=winappswithnativecode + # So as a workaround, we will include all source files into the target app project + function(get_native_app_uwp_source UWP_SOURCE UWP_INCLUDE UWP_INCLUDE_DIR) + get_target_property(NATIVE_APP_SOURCE_DIR NativeAppBase SOURCE_DIR) + + set(${UWP_SOURCE} + ${NATIVE_APP_SOURCE_DIR}/src/UWP/Common/DeviceResources.cpp + ${NATIVE_APP_SOURCE_DIR}/src/UWP/App.cpp + ${NATIVE_APP_SOURCE_DIR}/src/UWP/SampleAppMain.cpp + ${NATIVE_APP_SOURCE_DIR}/src/UWP/TwEventUWP.cpp + PARENT_SCOPE + ) + + set(${UWP_INCLUDE} + ${NATIVE_APP_SOURCE_DIR}/src/UWP/Common/DeviceResources.h + ${NATIVE_APP_SOURCE_DIR}/src/UWP/Common/DirectXHelper.h + ${NATIVE_APP_SOURCE_DIR}/src/UWP/Common/StepTimer.h + ${NATIVE_APP_SOURCE_DIR}/src/UWP/App.h + ${NATIVE_APP_SOURCE_DIR}/src/UWP/pch2.h + ${NATIVE_APP_SOURCE_DIR}/src/UWP/SampleAppMain.h + ${NATIVE_APP_SOURCE_DIR}/src/UWP/TwEventUWP.h + PARENT_SCOPE + ) + + set(${UWP_INCLUDE_DIR} + ${NATIVE_APP_SOURCE_DIR}/Src/UWP + PARENT_SCOPE + ) + endfunction(get_native_app_uwp_source) + +elseif(PLATFORM_ANDROID) + set(SOURCE + src/Android/AndroidMainImpl.cpp + ) +elseif(PLATFORM_LINUX) + set(SOURCE + src/Linux/LinuxMain.cpp + ) +elseif(PLATFORM_MACOS) + set(SOURCE + src/MacOS/Renderer.cpp + ) + set(INCLUDE + src/MacOS/Renderer.h + ) + + function(get_native_app_macos_source APPLE_SOURCE APPLE_INCLUDE APPLE_RESOURCES APPLE_INFO_PLIST APPLE_INCLUDE_DIRS) + get_target_property(NATIVE_APP_SOURCE_DIR NativeAppBase SOURCE_DIR) + + set(${APPLE_SOURCE} + ${NATIVE_APP_SOURCE_DIR}/Apple/Source/main.m + ${NATIVE_APP_SOURCE_DIR}/Apple/Source/Classes/OSX/WindowController.m + ${NATIVE_APP_SOURCE_DIR}/Apple/Source/Classes/OSX/AppDelegate.m + ${NATIVE_APP_SOURCE_DIR}/Apple/Source/Classes/OSX/FullscreenWindow.m + ${NATIVE_APP_SOURCE_DIR}/Apple/Source/Classes/OSX/GLView.m + ) + set_source_files_properties(${APPLE_SOURCE} PROPERTIES + COMPILE_FLAGS "-x objective-c++" + ) + set( APPLE_SOURCE ${APPLE_SOURCE} PARENT_SCOPE ) + + set(${APPLE_INCLUDE} + ${NATIVE_APP_SOURCE_DIR}/Apple/Source/Classes/OSX/WindowController.h + ${NATIVE_APP_SOURCE_DIR}/Apple/Source/Classes/OSX/AppDelegate.h + ${NATIVE_APP_SOURCE_DIR}/Apple/Source/Classes/OSX/FullscreenWindow.h + ${NATIVE_APP_SOURCE_DIR}/Apple/Source/Classes/OSX/GLView.h + PARENT_SCOPE + ) + + set(${APPLE_RESOURCES} + ${NATIVE_APP_SOURCE_DIR}/Apple/Data/OSX/Base.lproj/Main.storyboard + ${NATIVE_APP_SOURCE_DIR}/Apple/Data/OSX/Images.xcassets/AppIcon.appiconset/dg.icns + PARENT_SCOPE + ) + + set(${APPLE_INFO_PLIST} + ${NATIVE_APP_SOURCE_DIR}/Apple/Data/OSX/Info.plist + PARENT_SCOPE + ) + + set(${APPLE_INCLUDE_DIRS} + ${NATIVE_APP_SOURCE_DIR}/Apple/Source/Classes/OSX + PARENT_SCOPE + ) + endfunction() + +elseif(PLATFORM_IOS) + set(SOURCE + src/MacOS/Renderer.cpp + ) + set(INCLUDE + src/MacOS/Renderer.h + ) + + function(get_native_app_ios_source APPLE_SOURCE APPLE_INCLUDE APPLE_RESOURCES APPLE_INFO_PLIST APPLE_INCLUDE_DIRS) + get_target_property(NATIVE_APP_SOURCE_DIR NativeAppBase SOURCE_DIR) + + set(${APPLE_SOURCE} + ${NATIVE_APP_SOURCE_DIR}/Apple/Source/main.m + ${NATIVE_APP_SOURCE_DIR}/Apple/Source/Classes/iOS/AppDelegate.m + ${NATIVE_APP_SOURCE_DIR}/Apple/Source/Classes/iOS/EAGLView.m + ) + set_source_files_properties(${APPLE_SOURCE} PROPERTIES + COMPILE_FLAGS "-x objective-c++" + ) + set( APPLE_SOURCE ${APPLE_SOURCE} PARENT_SCOPE ) + + set(${APPLE_INCLUDE} + ${NATIVE_APP_SOURCE_DIR}/Apple/Source/Classes/iOS/AppDelegate.h + ${NATIVE_APP_SOURCE_DIR}/Apple/Source/Classes/iOS/EAGLView.h + PARENT_SCOPE + ) + + set(${APPLE_RESOURCES} + ${NATIVE_APP_SOURCE_DIR}/Apple/Data/iOS/Base.lproj/Main.storyboard + ${NATIVE_APP_SOURCE_DIR}/Apple/Data/iOS/Base.lproj/LaunchScreen.xib + ${NATIVE_APP_SOURCE_DIR}/Apple/Data/iOS/Images.xcassets/AppIcon.appiconset/dg-icon.png + PARENT_SCOPE + ) + + set(${APPLE_INFO_PLIST} + ${NATIVE_APP_SOURCE_DIR}/Apple/Data/iOS/info.plist + PARENT_SCOPE + ) + + set(${APPLE_INCLUDE_DIRS} + ${NATIVE_APP_SOURCE_DIR}/Apple/Source/Classes/iOS + PARENT_SCOPE + ) + endfunction() + +endif() + +list(APPEND INCLUDE + include/NativeAppBase.h + include/NativeAppData.h +) + + +add_library(NativeAppBase STATIC ${SOURCE} ${INCLUDE}) +set_common_target_properties(NativeAppBase) + +target_include_directories(NativeAppBase +PUBLIC + include +) + + +if(MSVC) + target_compile_options(NativeAppBase PRIVATE -DUNICODE) + + if(PLATFORM_UNIVERSAL_WINDOWS) + # Disable w4189: local variable is initialized but not referenced + # Disable w4063: case is not a valid value for switch of enum + # Consume the windows runtime extensions (/ZW) + target_compile_options(NativeAppBase INTERFACE /wd4189 /wd4063 /ZW) + endif() +endif() + +target_link_libraries(NativeAppBase +PRIVATE + BuildSettings + Common +) + +if(PLATFORM_UNIVERSAL_WINDOWS) + +elseif(PLATFORM_ANDROID) + target_link_libraries(NativeAppBase PRIVATE NDKHelper android NativeAppBaseGlue) + target_include_directories(NativeAppBase PRIVATE + ${ANDROID_NDK}/sources/android/cpufeatures + ) +elseif(PLATFORM_LINUX) + target_link_libraries(NativeAppBase PRIVATE X11) +elseif(PLATFORM_MACOS OR PLATFORM_IOS) + target_include_directories(NativeAppBase PUBLIC + src/MacOS + ) +endif() + +source_group("src" FILES ${SOURCE}) +source_group("include" FILES ${INCLUDE}) + +set_target_properties(NativeAppBase PROPERTIES + FOLDER Common +) diff --git a/Common/NativeApp/include/NativeAppBase.h b/Common/NativeApp/include/NativeAppBase.h new file mode 100644 index 0000000..590b6c9 --- /dev/null +++ b/Common/NativeApp/include/NativeAppBase.h @@ -0,0 +1,50 @@ +/* Copyright 2015-2018 Egor Yusov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS. + * + * In no event and under no legal theory, whether in tort (including negligence), + * contract, or otherwise, unless required by applicable law (such as deliberate + * and grossly negligent acts) or agreed to in writing, shall any Contributor be + * liable for any damages, including any direct, indirect, special, incidental, + * or consequential damages of any character arising as a result of this License or + * out of the use or inability to use the software (including but not limited to damages + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and + * all other commercial damages or losses), even if such Contributor has been advised + * of the possibility of such damages. + */ + +#pragma once + +struct NativeAppAttributes +{ +#ifdef PLATFORM_WIN32 + // * HWND on Win32 + void* NativeWindowHandle = nullptr; +#endif + int WindowWidth = 0; + int WindowHeight = 0; +}; + +class NativeAppBase +{ +public: + virtual ~NativeAppBase() {} + + virtual void ProcessCommandLine(const char *CmdLine) = 0; + virtual const char* GetAppTitle()const = 0; + virtual void Initialize(const struct NativeAppAttributes &NativeAppAttribs) = 0; + virtual void Update(double CurrTime, double ElapsedTime) {}; + virtual void PlatformRender() = 0; + virtual bool HandleNativeMessage(struct NativeMessage &msg) = 0; + virtual void Resize(int width, int height) = 0; +}; + +extern NativeAppBase* CreateApplication(); diff --git a/Common/NativeApp/include/NativeAppData.h b/Common/NativeApp/include/NativeAppData.h new file mode 100644 index 0000000..ac8c55f --- /dev/null +++ b/Common/NativeApp/include/NativeAppData.h @@ -0,0 +1,38 @@ +/* Copyright 2015-2018 Egor Yusov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS. + * + * In no event and under no legal theory, whether in tort (including negligence), + * contract, or otherwise, unless required by applicable law (such as deliberate + * and grossly negligent acts) or agreed to in writing, shall any Contributor be + * liable for any damages, including any direct, indirect, special, incidental, + * or consequential damages of any character arising as a result of this License or + * out of the use or inability to use the software (including but not limited to damages + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and + * all other commercial damages or losses), even if such Contributor has been advised + * of the possibility of such damages. + */ + +#pragma once + +#ifdef PLATFORM_WIN32 +#include +#endif + +struct NativeMessage +{ +#ifdef PLATFORM_WIN32 + HWND hWnd; + UINT message; + WPARAM wParam; + LPARAM lParam; +#endif +}; -- cgit v1.2.3