diff --git a/NativeApp/include/Android/AndroidAppBase.h b/NativeApp/include/Android/AndroidAppBase.h index 607c8b6..0b291c6 100644 --- a/NativeApp/include/Android/AndroidAppBase.h +++ b/NativeApp/include/Android/AndroidAppBase.h @@ -21,7 +21,7 @@ * of the possibility of such damages. */ -#pragma once +#pragma once #include @@ -38,23 +38,24 @@ class AndroidAppBase : public AppBase { public: - int InitDisplay(); - void SetState(android_app* state, const char* native_activity_class_name); - void InitSensors(); - void ProcessSensors( int32_t id ); - void DrawFrame(); - bool IsReady(); - virtual void TrimMemory() = 0; - virtual void TermDisplay() = 0; + int InitDisplay(); + void SetState(android_app* state, const char* native_activity_class_name); + void InitSensors(); + void ProcessSensors(int32_t id); + void DrawFrame(); + bool IsReady(); + virtual void TrimMemory() = 0; + virtual void TermDisplay() = 0; static int32_t HandleInput(android_app* app, AInputEvent* event); - static void HandleCmd(android_app* app, int32_t cmd); + static void HandleCmd(android_app* app, int32_t cmd); + bool CheckWindowSizeChanged() { - auto new_window_width_ = ANativeWindow_getWidth(app_->window); + auto new_window_width_ = ANativeWindow_getWidth(app_->window); auto new_window_height_ = ANativeWindow_getHeight(app_->window); - if(new_window_width_ != window_width_ || new_window_height_ != window_height_) + if (new_window_width_ != window_width_ || new_window_height_ != window_height_) { - window_width_ = new_window_width_; + window_width_ = new_window_width_; window_height_ = new_window_height_; return true; } @@ -70,7 +71,7 @@ virtual int Resume(ANativeWindow* window) = 0; - virtual int32_t HandleInput(AInputEvent* event ){return 0;} + virtual int32_t HandleInput(AInputEvent* event) { return 0; } virtual void LoadResources() { @@ -84,29 +85,29 @@ } ndk_helper::DoubletapDetector doubletap_detector_; - ndk_helper::PinchDetector pinch_detector_; - ndk_helper::DragDetector drag_detector_; - ndk_helper::PerfMonitor monitor_; + ndk_helper::PinchDetector pinch_detector_; + ndk_helper::DragDetector drag_detector_; + ndk_helper::PerfMonitor monitor_; //ndk_helper::TapCamera tap_camera_; android_app* app_ = nullptr; std::string native_activity_class_name_; private: - void UpdatePosition( AInputEvent* event, int32_t iIndex, float& fX, float& fY ); + void UpdatePosition(AInputEvent* event, int32_t iIndex, float& fX, float& fY); void SuspendSensors(); void ResumeSensors(); void ShowUI(); - void UpdateFPS( float fFPS ); + void UpdateFPS(float fFPS); - bool initialized_resources_ = false; - bool has_focus_ = false; - int32_t window_width_ = 0; - int32_t window_height_ = 0; + bool initialized_resources_ = false; + bool has_focus_ = false; + int32_t window_width_ = 0; + int32_t window_height_ = 0; - ASensorManager* sensor_manager_ = nullptr; - const ASensor* accelerometer_sensor_ = nullptr; - ASensorEventQueue* sensor_event_queue_ = nullptr; + ASensorManager* sensor_manager_ = nullptr; + const ASensor* accelerometer_sensor_ = nullptr; + ASensorEventQueue* sensor_event_queue_ = nullptr; }; -} +} // namespace Diligent diff --git a/NativeApp/include/AppBase.h b/NativeApp/include/AppBase.h index d3de11c..634de48 100644 --- a/NativeApp/include/AppBase.h +++ b/NativeApp/include/AppBase.h @@ -21,7 +21,7 @@ * of the possibility of such damages. */ -#pragma once +#pragma once namespace Diligent { @@ -31,17 +31,17 @@ public: virtual ~AppBase() {} - virtual void ProcessCommandLine(const char* CmdLine) = 0; - virtual const char* GetAppTitle()const = 0; - virtual void Update(double CurrTime, double ElapsedTime) {}; - virtual void Render() = 0; - virtual void Present() = 0; - virtual void WindowResize(int width, int height) = 0; - virtual void GetDesiredInitialWindowSize(int& width, int& height) + virtual void ProcessCommandLine(const char* CmdLine) = 0; + virtual const char* GetAppTitle() const = 0; + virtual void Update(double CurrTime, double ElapsedTime){}; + virtual void Render() = 0; + virtual void Present() = 0; + virtual void WindowResize(int width, int height) = 0; + virtual void GetDesiredInitialWindowSize(int& width, int& height) { width = 0; height = 0; } }; -} +} // namespace Diligent diff --git a/NativeApp/include/IOS/IOSAppBase.h b/NativeApp/include/IOS/IOSAppBase.h index 847052a..a49aadb 100644 --- a/NativeApp/include/IOS/IOSAppBase.h +++ b/NativeApp/include/IOS/IOSAppBase.h @@ -21,7 +21,7 @@ * of the possibility of such damages. */ -#pragma once +#pragma once #include "AppBase.h" #include "Timer.h" @@ -33,15 +33,15 @@ { public: using AppBase::Update; - void Update(); + void Update(); virtual void Initialize(int deviceType, void* layer) = 0; - virtual void OnTouchBegan(float x, float y){} - virtual void OnTouchMoved(float x, float y){} - virtual void OnTouchEnded(float x, float y){} + virtual void OnTouchBegan(float x, float y) {} + virtual void OnTouchMoved(float x, float y) {} + virtual void OnTouchEnded(float x, float y) {} protected: - Timer timer; + Timer timer; double PrevTime = 0.0; }; -} +} // namespace Diligent diff --git a/NativeApp/include/Linux/LinuxAppBase.h b/NativeApp/include/Linux/LinuxAppBase.h index e4c6a3e..eb900f7 100644 --- a/NativeApp/include/Linux/LinuxAppBase.h +++ b/NativeApp/include/Linux/LinuxAppBase.h @@ -21,7 +21,7 @@ * of the possibility of such damages. */ -#pragma once +#pragma once #include @@ -29,17 +29,17 @@ // Undef symbols defined by XLib #ifdef Bool -# undef Bool +# undef Bool #endif #ifdef True -# undef True +# undef True #endif #ifdef False -# undef False +# undef False #endif #if VULKAN_SUPPORTED -#include +# include #endif #include "AppBase.h" @@ -51,12 +51,12 @@ { public: virtual void OnGLContextCreated(Display* display, Window window) = 0; - virtual int HandleXEvent(XEvent* xev){} + virtual int HandleXEvent(XEvent* xev) {} #if VULKAN_SUPPORTED virtual bool InitVulkan(xcb_connection_t* connection, uint32_t window) = 0; - virtual void HandleXCBEvent(xcb_generic_event_t* event){} + virtual void HandleXCBEvent(xcb_generic_event_t* event) {} #endif }; -} +} // namespace Diligent diff --git a/NativeApp/include/MacOS/MacOSAppBase.h b/NativeApp/include/MacOS/MacOSAppBase.h index 923fcec..3a0e5b0 100644 --- a/NativeApp/include/MacOS/MacOSAppBase.h +++ b/NativeApp/include/MacOS/MacOSAppBase.h @@ -21,7 +21,7 @@ * of the possibility of such damages. */ -#pragma once +#pragma once #include "AppBase.h" #include "Timer.h" @@ -33,13 +33,13 @@ { public: using AppBase::Update; - void Update(); + void Update(); virtual void Initialize(void* view) = 0; virtual void HandleOSXEvent(void* event, void* view){}; protected: - Timer timer; + Timer timer; double PrevTime = 0.0; }; -} +} // namespace Diligent diff --git a/NativeApp/include/NativeAppBase.h b/NativeApp/include/NativeAppBase.h index c475e0c..e81e9a1 100644 --- a/NativeApp/include/NativeAppBase.h +++ b/NativeApp/include/NativeAppBase.h @@ -20,7 +20,9 @@ * all other commercial damages or losses), even if such Contributor has been advised * of the possibility of such damages. */ -#pragma once +#pragma once + +// clang-format off #if PLATFORM_WIN32 @@ -78,4 +80,4 @@ namespace Diligent { extern NativeAppBase* CreateApplication(); -} \ No newline at end of file +} diff --git a/NativeApp/include/Win32/Win32AppBase.h b/NativeApp/include/Win32/Win32AppBase.h index 8242ac7..02cfd85 100644 --- a/NativeApp/include/Win32/Win32AppBase.h +++ b/NativeApp/include/Win32/Win32AppBase.h @@ -21,10 +21,10 @@ * of the possibility of such damages. */ -#pragma once +#pragma once #ifndef NOMINMAX -# define NOMINMAX +# define NOMINMAX #endif #include @@ -40,13 +40,13 @@ LONG WindowWidth, LONG WindowHeight) = 0; - virtual LRESULT HandleWin32Message(HWND hWnd, - UINT message, - WPARAM wParam, - LPARAM lParam) + virtual LRESULT HandleWin32Message(HWND hWnd, + UINT message, + WPARAM wParam, + LPARAM lParam) { return 0; } }; -} +} // namespace Diligent diff --git a/NativeApp/src/Android/AndroidAppBase.cpp b/NativeApp/src/Android/AndroidAppBase.cpp index f3e341c..05386fd 100644 --- a/NativeApp/src/Android/AndroidAppBase.cpp +++ b/NativeApp/src/Android/AndroidAppBase.cpp @@ -35,7 +35,7 @@ int AndroidAppBase::InitDisplay() { - if( !initialized_resources_ ) + if (!initialized_resources_) { Initialize(); @@ -45,7 +45,7 @@ else { // initialize OpenGL ES and EGL - if( EGL_SUCCESS != Resume( app_->window ) ) + if (EGL_SUCCESS != Resume(app_->window)) { UnloadResources(); LoadResources(); @@ -57,14 +57,14 @@ //tap_camera_.SetFlip( 1.f, -1.f, -1.f ); //tap_camera_.SetPinchTransformFactor( 2.f, 2.f, 8.f ); - return 0; + return 0; } void AndroidAppBase::InitSensors() { - sensor_manager_ = ASensorManager_getInstance(); - accelerometer_sensor_ = ASensorManager_getDefaultSensor( sensor_manager_, ASENSOR_TYPE_ACCELEROMETER ); - sensor_event_queue_ = ASensorManager_createEventQueue( sensor_manager_, app_->looper, LOOPER_ID_USER, NULL, NULL ); + sensor_manager_ = ASensorManager_getInstance(); + accelerometer_sensor_ = ASensorManager_getDefaultSensor(sensor_manager_, ASENSOR_TYPE_ACCELEROMETER); + sensor_event_queue_ = ASensorManager_createEventQueue(sensor_manager_, app_->looper, LOOPER_ID_USER, NULL, NULL); } // @@ -75,19 +75,21 @@ // APP_CMD_CONFIG_CHANGED event is generated seveal frames // before the screen is actually resized. The only robust way // to detect window resize is to check it very frame - if(CheckWindowSizeChanged()) - WindowResize(0,0); + if (CheckWindowSizeChanged()) + WindowResize(0, 0); float fFPS; - if( monitor_.Update( fFPS ) ) - { - UpdateFPS( fFPS ); + if (monitor_.Update(fFPS)) + { + UpdateFPS(fFPS); } static Diligent::Timer Timer; - static double PrevTime = Timer.GetElapsedTime(); - auto CurrTime = Timer.GetElapsedTime(); - auto ElapsedTime = CurrTime - PrevTime; + + static double PrevTime = Timer.GetElapsedTime(); + auto CurrTime = Timer.GetElapsedTime(); + auto ElapsedTime = CurrTime - PrevTime; + PrevTime = CurrTime; Update(CurrTime, ElapsedTime); @@ -106,80 +108,80 @@ // // Process the next input event. // -int32_t AndroidAppBase::HandleInput( android_app* app, AInputEvent* event ) +int32_t AndroidAppBase::HandleInput(android_app* app, AInputEvent* event) { AndroidAppBase* eng = (AndroidAppBase*)app->userData; - return eng->HandleInput( event ); + return eng->HandleInput(event); } // // Process the next main command. // -void AndroidAppBase::HandleCmd( struct android_app* app, int32_t cmd ) +void AndroidAppBase::HandleCmd(struct android_app* app, int32_t cmd) { AndroidAppBase* eng = (AndroidAppBase*)app->userData; - switch( cmd ) - { - case APP_CMD_SAVE_STATE: - break; - - case APP_CMD_INIT_WINDOW: - // The window is being shown, get it ready. - if( app->window != NULL ) - { - eng->InitDisplay(); + switch (cmd) + { + case APP_CMD_SAVE_STATE: + break; + + case APP_CMD_INIT_WINDOW: + // The window is being shown, get it ready. + if (app->window != NULL) + { + eng->InitDisplay(); + eng->DrawFrame(); + } + break; + + case APP_CMD_CONFIG_CHANGED: + case APP_CMD_WINDOW_RESIZED: + // This does not work as the screen resizes few frames + // after the event has been received + // eng->WindowResize(0,0); + break; + + case APP_CMD_TERM_WINDOW: + // The window is being hidden or closed, clean it up. + eng->TermDisplay(); + eng->has_focus_ = false; + break; + + case APP_CMD_STOP: + break; + + case APP_CMD_GAINED_FOCUS: + eng->ResumeSensors(); + //Start animation + eng->has_focus_ = true; + break; + + case APP_CMD_LOST_FOCUS: + eng->SuspendSensors(); + // Also stop animating. + eng->has_focus_ = false; eng->DrawFrame(); - } - break; - - case APP_CMD_CONFIG_CHANGED: - case APP_CMD_WINDOW_RESIZED: - // This does not work as the screen resizes few frames - // after the event has been received - // eng->WindowResize(0,0); - break; - - case APP_CMD_TERM_WINDOW: - // The window is being hidden or closed, clean it up. - eng->TermDisplay(); - eng->has_focus_ = false; - break; - - case APP_CMD_STOP: - break; - - case APP_CMD_GAINED_FOCUS: - eng->ResumeSensors(); - //Start animation - eng->has_focus_ = true; - break; - - case APP_CMD_LOST_FOCUS: - eng->SuspendSensors(); - // Also stop animating. - eng->has_focus_ = false; - eng->DrawFrame(); - break; - - case APP_CMD_LOW_MEMORY: - //Free up GL resources - eng->TrimMemory(); - break; + break; + + case APP_CMD_LOW_MEMORY: + //Free up GL resources + eng->TrimMemory(); + break; } } //------------------------------------------------------------------------- //Sensor handlers //------------------------------------------------------------------------- -void AndroidAppBase::ProcessSensors( int32_t id ) +void AndroidAppBase::ProcessSensors(int32_t id) { // If a sensor has data, process it now. - if( id == LOOPER_ID_USER ) - { - if( accelerometer_sensor_ != NULL ) + if (id == LOOPER_ID_USER) + { + if (accelerometer_sensor_ != NULL) { ASensorEvent event; - while( ASensorEventQueue_getEvents( sensor_event_queue_, &event, 1 ) > 0 ) + while (ASensorEventQueue_getEvents(sensor_event_queue_, &event, 1) > 0) { } } @@ -189,12 +191,12 @@ void AndroidAppBase::ResumeSensors() { // When our app gains focus, we start monitoring the accelerometer. - if( accelerometer_sensor_ != NULL ) - { - ASensorEventQueue_enableSensor( sensor_event_queue_, accelerometer_sensor_ ); + if (accelerometer_sensor_ != NULL) + { + ASensorEventQueue_enableSensor(sensor_event_queue_, accelerometer_sensor_); // We'd like to get 60 events per second (in us). - ASensorEventQueue_setEventRate( sensor_event_queue_, accelerometer_sensor_, - (1000L / 60) * 1000 ); + ASensorEventQueue_setEventRate(sensor_event_queue_, accelerometer_sensor_, + (1000L / 60) * 1000); } } @@ -202,27 +204,28 @@ { // When our app loses focus, we stop monitoring the accelerometer. // This is to avoid consuming battery while not being used. - if( accelerometer_sensor_ != NULL ) - { - ASensorEventQueue_disableSensor( sensor_event_queue_, accelerometer_sensor_ ); + if (accelerometer_sensor_ != NULL) + { + ASensorEventQueue_disableSensor(sensor_event_queue_, accelerometer_sensor_); } } //------------------------------------------------------------------------- //Misc //------------------------------------------------------------------------- -void AndroidAppBase::SetState( android_app* state, const char* native_activity_class_name ) +void AndroidAppBase::SetState(android_app* state, const char* native_activity_class_name) { app_ = state; + native_activity_class_name_ = native_activity_class_name; - doubletap_detector_.SetConfiguration( app_->config ); - drag_detector_.SetConfiguration( app_->config ); - pinch_detector_.SetConfiguration( app_->config ); + doubletap_detector_.SetConfiguration(app_->config); + drag_detector_.SetConfiguration(app_->config); + pinch_detector_.SetConfiguration(app_->config); } bool AndroidAppBase::IsReady() { - if( has_focus_ ) + if (has_focus_) return true; return false; @@ -237,30 +240,30 @@ void AndroidAppBase::ShowUI() { - JNIEnv *jni; - app_->activity->vm->AttachCurrentThread( &jni, NULL ); + JNIEnv* jni; + app_->activity->vm->AttachCurrentThread(&jni, NULL); //Default class retrieval - jclass clazz = jni->GetObjectClass( app_->activity->clazz ); - jmethodID methodID = jni->GetMethodID( clazz, "showUI", "()V" ); - jni->CallVoidMethod( app_->activity->clazz, methodID ); + jclass clazz = jni->GetObjectClass(app_->activity->clazz); + jmethodID methodID = jni->GetMethodID(clazz, "showUI", "()V"); + jni->CallVoidMethod(app_->activity->clazz, methodID); app_->activity->vm->DetachCurrentThread(); return; } -void AndroidAppBase::UpdateFPS( float fFPS ) -{ - JNIEnv *jni; - app_->activity->vm->AttachCurrentThread( &jni, NULL ); +void AndroidAppBase::UpdateFPS(float fFPS) +{ + JNIEnv* jni; + app_->activity->vm->AttachCurrentThread(&jni, NULL); //Default class retrieval - jclass clazz = jni->GetObjectClass( app_->activity->clazz ); - jmethodID methodID = jni->GetMethodID( clazz, "updateFPS", "(F)V" ); - jni->CallVoidMethod( app_->activity->clazz, methodID, fFPS ); + jclass clazz = jni->GetObjectClass(app_->activity->clazz); + jmethodID methodID = jni->GetMethodID(clazz, "updateFPS", "(F)V"); + jni->CallVoidMethod(app_->activity->clazz, methodID, fFPS); app_->activity->vm->DetachCurrentThread(); return; } -} +} // namespace Diligent diff --git a/NativeApp/src/Android/AndroidMain.cpp b/NativeApp/src/Android/AndroidMain.cpp index 6e65b8a..b37bf88 100644 --- a/NativeApp/src/Android/AndroidMain.cpp +++ b/NativeApp/src/Android/AndroidMain.cpp @@ -1,4 +1,4 @@ - /* Copyright 2015-2018 Egor Yusov +/* 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. @@ -29,7 +29,7 @@ #include "NativeAppBase.h" -#define HELPER_CLASS_NAME "com/android/helper/NDKHelper" //Class name of helper function +#define HELPER_CLASS_NAME "com/android/helper/NDKHelper" //Class name of helper function #define NATIVEACTIVITY_CLASS_NAME "android/app/NativeActivity" using namespace Diligent; @@ -37,53 +37,53 @@ // This is the main entry point of a native application that is using // android_native_app_glue. It runs in its own thread, with its own // event loop for receiving input events and doing other things. -void android_main( android_app* state ) +void android_main(android_app* state) { std::unique_ptr theApp(CreateApplication()); - theApp->SetState( state, NATIVEACTIVITY_CLASS_NAME ); + theApp->SetState(state, NATIVEACTIVITY_CLASS_NAME); //Init helper functions - ndk_helper::JNIHelper::Init( state->activity, HELPER_CLASS_NAME ); + ndk_helper::JNIHelper::Init(state->activity, HELPER_CLASS_NAME); - state->userData = theApp.get(); - state->onAppCmd = AndroidAppBase::HandleCmd; + state->userData = theApp.get(); + state->onAppCmd = AndroidAppBase::HandleCmd; state->onInputEvent = AndroidAppBase::HandleInput; #ifdef USE_NDK_PROFILER - monstartup( "libEngineSandbox.so" ); + monstartup("libEngineSandbox.so"); #endif // Prepare to monitor accelerometer theApp->InitSensors(); // loop waiting for stuff to do. - while( 1 ) + while (true) { // Read all pending events. - int id; - int events; + int id; + int events; android_poll_source* source; // If not animating, we will block forever waiting for events. // If animating, we loop until all events are read, then continue // to draw the next frame of animation. - while( (id = ALooper_pollAll( theApp->IsReady() ? 0 : -1, NULL, &events, (void**)&source )) >= 0 ) + while ((id = ALooper_pollAll(theApp->IsReady() ? 0 : -1, NULL, &events, (void**)&source)) >= 0) { // Process this event. - if( source != NULL ) - source->process( state, source ); + if (source != NULL) + source->process(state, source); - theApp->ProcessSensors( id ); + theApp->ProcessSensors(id); // Check if we are exiting. - if( state->destroyRequested != 0 ) + if (state->destroyRequested != 0) { theApp->TermDisplay(); return; } } - if( theApp->IsReady() ) + if (theApp->IsReady()) { // Drawing is throttled to the screen update rate, so there // is no need to do timing here. diff --git a/NativeApp/src/IOS/IOSAppBase.cpp b/NativeApp/src/IOS/IOSAppBase.cpp index d4defb8..f091443 100644 --- a/NativeApp/src/IOS/IOSAppBase.cpp +++ b/NativeApp/src/IOS/IOSAppBase.cpp @@ -29,10 +29,10 @@ void IOSAppBase::Update() { // Render the scene - auto CurrTime = timer.GetElapsedTime(); + auto CurrTime = timer.GetElapsedTime(); auto ElapsedTime = CurrTime - PrevTime; - PrevTime = CurrTime; + PrevTime = CurrTime; Update(CurrTime, ElapsedTime); } -} +} // namespace Diligent diff --git a/NativeApp/src/Linux/LinuxMain.cpp b/NativeApp/src/Linux/LinuxMain.cpp index c8cf197..b06c14e 100644 --- a/NativeApp/src/Linux/LinuxMain.cpp +++ b/NativeApp/src/Linux/LinuxMain.cpp @@ -32,19 +32,19 @@ #ifndef GLX_CONTEXT_MAJOR_VERSION_ARB -# define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091 +# define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091 #endif #ifndef GLX_CONTEXT_MINOR_VERSION_ARB -# define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092 +# define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092 #endif #ifndef GLX_CONTEXT_FLAGS_ARB -# define GLX_CONTEXT_FLAGS_ARB 0x2094 +# define GLX_CONTEXT_FLAGS_ARB 0x2094 #endif #ifndef GLX_CONTEXT_DEBUG_BIT_ARB -# define GLX_CONTEXT_DEBUG_BIT_ARB 0x0001 +# define GLX_CONTEXT_DEBUG_BIT_ARB 0x0001 #endif typedef GLXContext (*glXCreateContextAttribsARBProc)(Display*, GLXFBConfig, GLXContext, int, const int*); @@ -58,18 +58,18 @@ namespace { - + class WindowTitleHelper { public: - WindowTitleHelper(std::string _Title) : - Title(std::move(_Title)) + WindowTitleHelper(std::string _Title) : + Title{std::move(_Title)} {} - + std::string GetTitleWithFPS(double ElapsedTime) { double filterScale = 0.2; - FilteredFrameTime = FilteredFrameTime * (1.0 - filterScale) + filterScale * ElapsedTime; + FilteredFrameTime = FilteredFrameTime * (1.0 - filterScale) + filterScale * ElapsedTime; std::stringstream TitleWithFpsSS; TitleWithFpsSS << Title; TitleWithFpsSS << " - " << std::fixed << std::setprecision(1) << FilteredFrameTime * 1000; @@ -79,48 +79,48 @@ private: const std::string Title; - double FilteredFrameTime = 0.0; + double FilteredFrameTime = 0.0; }; -} +} // namespace #if VULKAN_SUPPORTED // https://code.woboq.org/qt5/include/xcb/xcb_icccm.h.html enum XCB_SIZE_HINT { - XCB_SIZE_HINT_US_POSITION = 1 << 0, - XCB_SIZE_HINT_US_SIZE = 1 << 1, - XCB_SIZE_HINT_P_POSITION = 1 << 2, - XCB_SIZE_HINT_P_SIZE = 1 << 3, - XCB_SIZE_HINT_P_MIN_SIZE = 1 << 4, - XCB_SIZE_HINT_P_MAX_SIZE = 1 << 5, - XCB_SIZE_HINT_P_RESIZE_INC = 1 << 6, - XCB_SIZE_HINT_P_ASPECT = 1 << 7, - XCB_SIZE_HINT_BASE_SIZE = 1 << 8, - XCB_SIZE_HINT_P_WIN_GRAVITY = 1 << 9 + XCB_SIZE_HINT_US_POSITION = 1 << 0, + XCB_SIZE_HINT_US_SIZE = 1 << 1, + XCB_SIZE_HINT_P_POSITION = 1 << 2, + XCB_SIZE_HINT_P_SIZE = 1 << 3, + XCB_SIZE_HINT_P_MIN_SIZE = 1 << 4, + XCB_SIZE_HINT_P_MAX_SIZE = 1 << 5, + XCB_SIZE_HINT_P_RESIZE_INC = 1 << 6, + XCB_SIZE_HINT_P_ASPECT = 1 << 7, + XCB_SIZE_HINT_BASE_SIZE = 1 << 8, + XCB_SIZE_HINT_P_WIN_GRAVITY = 1 << 9 }; struct xcb_size_hints_t { - uint32_t flags; /** User specified flags */ - int32_t x, y; /** User-specified position */ - int32_t width, height; /** User-specified size */ - int32_t min_width, min_height; /** Program-specified minimum size */ - int32_t max_width, max_height; /** Program-specified maximum size */ - int32_t width_inc, height_inc; /** Program-specified resize increments */ - int32_t min_aspect_num, min_aspect_den; /** Program-specified minimum aspect ratios */ - int32_t max_aspect_num, max_aspect_den; /** Program-specified maximum aspect ratios */ - int32_t base_width, base_height; /** Program-specified base size */ - uint32_t win_gravity; /** Program-specified window gravity */ + uint32_t flags; /** User specified flags */ + int32_t x, y; /** User-specified position */ + int32_t width, height; /** User-specified size */ + int32_t min_width, min_height; /** Program-specified minimum size */ + int32_t max_width, max_height; /** Program-specified maximum size */ + int32_t width_inc, height_inc; /** Program-specified resize increments */ + int32_t min_aspect_num, min_aspect_den; /** Program-specified minimum aspect ratios */ + int32_t max_aspect_num, max_aspect_den; /** Program-specified maximum aspect ratios */ + int32_t base_width, base_height; /** Program-specified base size */ + uint32_t win_gravity; /** Program-specified window gravity */ }; struct XCBInfo { - xcb_connection_t* connection = nullptr; - uint32_t window = 0; - uint16_t width = 0; - uint16_t height = 0; + xcb_connection_t* connection = nullptr; + uint32_t window = 0; + uint16_t width = 0; + uint16_t height = 0; xcb_intern_atom_reply_t* atom_wm_delete_window = nullptr; }; @@ -128,7 +128,7 @@ { XCBInfo info; - int scr = 0; + int scr = 0; info.connection = xcb_connect(nullptr, &scr); if (info.connection == nullptr || xcb_connection_has_error(info.connection)) { @@ -136,53 +136,53 @@ exit(-1); } - const xcb_setup_t* setup = xcb_get_setup(info.connection); - xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup); + const xcb_setup_t* setup = xcb_get_setup(info.connection); + xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup); while (scr-- > 0) xcb_screen_next(&iter); auto screen = iter.data; - info.width = WindowWidth; + info.width = WindowWidth; info.height = WindowHeight; uint32_t value_mask, value_list[32]; info.window = xcb_generate_id(info.connection); - value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK; + value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK; value_list[0] = screen->black_pixel; value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | - XCB_EVENT_MASK_KEY_PRESS | - XCB_EVENT_MASK_EXPOSURE | - XCB_EVENT_MASK_STRUCTURE_NOTIFY | - XCB_EVENT_MASK_POINTER_MOTION | - XCB_EVENT_MASK_BUTTON_PRESS | - XCB_EVENT_MASK_BUTTON_RELEASE; + XCB_EVENT_MASK_KEY_PRESS | + XCB_EVENT_MASK_EXPOSURE | + XCB_EVENT_MASK_STRUCTURE_NOTIFY | + XCB_EVENT_MASK_POINTER_MOTION | + XCB_EVENT_MASK_BUTTON_PRESS | + XCB_EVENT_MASK_BUTTON_RELEASE; xcb_create_window(info.connection, XCB_COPY_FROM_PARENT, info.window, screen->root, 0, 0, info.width, info.height, 0, - XCB_WINDOW_CLASS_INPUT_OUTPUT, screen->root_visual, value_mask, value_list); + XCB_WINDOW_CLASS_INPUT_OUTPUT, screen->root_visual, value_mask, value_list); // Magic code that will send notification when window is destroyed xcb_intern_atom_cookie_t cookie = xcb_intern_atom(info.connection, 1, 12, "WM_PROTOCOLS"); - xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(info.connection, cookie, 0); + xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(info.connection, cookie, 0); xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(info.connection, 0, 16, "WM_DELETE_WINDOW"); - info.atom_wm_delete_window = xcb_intern_atom_reply(info.connection, cookie2, 0); + info.atom_wm_delete_window = xcb_intern_atom_reply(info.connection, cookie2, 0); xcb_change_property(info.connection, XCB_PROP_MODE_REPLACE, info.window, (*reply).atom, 4, 32, 1, &(*info.atom_wm_delete_window).atom); free(reply); xcb_change_property(info.connection, XCB_PROP_MODE_REPLACE, info.window, XCB_ATOM_WM_NAME, XCB_ATOM_STRING, - 8, Title.length(), Title.c_str() ); + 8, Title.length(), Title.c_str()); // https://stackoverflow.com/a/27771295 xcb_size_hints_t hints = {}; - hints.flags = XCB_SIZE_HINT_P_MIN_SIZE; - hints.min_width = MinWindowWidth; - hints.min_height = MinWindowHeight; + hints.flags = XCB_SIZE_HINT_P_MIN_SIZE; + hints.min_width = MinWindowWidth; + hints.min_height = MinWindowHeight; xcb_change_property(info.connection, XCB_PROP_MODE_REPLACE, info.window, XCB_ATOM_WM_NORMAL_HINTS, XCB_ATOM_WM_SIZE_HINTS, 32, sizeof(xcb_size_hints_t), &hints); @@ -194,7 +194,7 @@ xcb_configure_window(info.connection, info.window, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords); xcb_flush(info.connection); - xcb_generic_event_t *e; + xcb_generic_event_t* e; while ((e = xcb_wait_for_event(info.connection))) { if ((e->response_type & ~0x80) == XCB_EXPOSE) break; @@ -202,7 +202,7 @@ return info; } -void DestroyXCBConnectionAndWindow(XCBInfo &info) +void DestroyXCBConnectionAndWindow(XCBInfo& info) { xcb_destroy_window(info.connection, info.window); xcb_disconnect(info.connection); @@ -212,21 +212,22 @@ { std::unique_ptr TheApp(CreateApplication()); - std::string Title = TheApp->GetAppTitle(); - auto xcbInfo = InitXCBConnectionAndWindow(Title); - if(!TheApp->InitVulkan(xcbInfo.connection, xcbInfo.window)) + std::string Title = TheApp->GetAppTitle(); + auto xcbInfo = InitXCBConnectionAndWindow(Title); + if (!TheApp->InitVulkan(xcbInfo.connection, xcbInfo.window)) return -1; xcb_flush(xcbInfo.connection); Timer timer; - auto PrevTime = timer.GetElapsedTime(); - Title = TheApp->GetAppTitle(); + auto PrevTime = timer.GetElapsedTime(); + Title = TheApp->GetAppTitle(); WindowTitleHelper TitleHelper(Title); while (true) { xcb_generic_event_t* event = nullptr; + bool Quit = false; while ((event = xcb_poll_for_event(xcbInfo.connection)) != nullptr) { @@ -239,14 +240,14 @@ { Quit = true; } - break; + break; case XCB_KEY_RELEASE: { const auto* keyEvent = reinterpret_cast(event); switch (keyEvent->detail) { - #define KEY_ESCAPE 0x9 +# define KEY_ESCAPE 0x9 case KEY_ESCAPE: Quit = true; break; @@ -256,7 +257,7 @@ case XCB_DESTROY_NOTIFY: Quit = true; - break; + break; case XCB_CONFIGURE_NOTIFY: { @@ -272,7 +273,7 @@ } } break; - + default: break; } @@ -283,19 +284,19 @@ break; // Render the scene - auto CurrTime = timer.GetElapsedTime(); + auto CurrTime = timer.GetElapsedTime(); auto ElapsedTime = CurrTime - PrevTime; - PrevTime = CurrTime; + PrevTime = CurrTime; TheApp->Update(CurrTime, ElapsedTime); TheApp->Render(); - + TheApp->Present(); auto TitleWithFPS = TitleHelper.GetTitleWithFPS(ElapsedTime); xcb_change_property(xcbInfo.connection, XCB_PROP_MODE_REPLACE, xcbInfo.window, XCB_ATOM_WM_NAME, XCB_ATOM_STRING, - 8, TitleWithFPS.length(), TitleWithFPS.c_str() ); + 8, TitleWithFPS.length(), TitleWithFPS.c_str()); xcb_flush(xcbInfo.connection); } @@ -311,8 +312,9 @@ int x_main() { std::unique_ptr TheApp(CreateApplication()); - Display *display = XOpenDisplay(0); - + Display* display = XOpenDisplay(0); + + // clang-format off static int visual_attribs[] = { GLX_RENDER_TYPE, GLX_RGBA_BIT, @@ -334,135 +336,137 @@ GLX_SAMPLES, 1, None }; - - int fbcount = 0; - GLXFBConfig *fbc = glXChooseFBConfig(display, DefaultScreen(display), visual_attribs, &fbcount); + // clang-format on + + int fbcount = 0; + GLXFBConfig* fbc = glXChooseFBConfig(display, DefaultScreen(display), visual_attribs, &fbcount); if (!fbc) { LOG_ERROR_MESSAGE("Failed to retrieve a framebuffer config"); return -1; } - - XVisualInfo *vi = glXGetVisualFromFBConfig(display, fbc[0]); - + + XVisualInfo* vi = glXGetVisualFromFBConfig(display, fbc[0]); + XSetWindowAttributes swa; - swa.colormap = XCreateColormap(display, RootWindow(display, vi->screen), vi->visual, AllocNone); + swa.colormap = XCreateColormap(display, RootWindow(display, vi->screen), vi->visual, AllocNone); swa.border_pixel = 0; - swa.event_mask = - StructureNotifyMask | - ExposureMask | - KeyPressMask | + swa.event_mask = + StructureNotifyMask | + ExposureMask | + KeyPressMask | KeyReleaseMask | - ButtonPressMask | - ButtonReleaseMask | + ButtonPressMask | + ButtonReleaseMask | PointerMotionMask; - - Window win = XCreateWindow(display, RootWindow(display, vi->screen), 0, 0, WindowWidth, WindowHeight, 0, vi->depth, InputOutput, vi->visual, CWBorderPixel|CWColormap|CWEventMask, &swa); + + Window win = XCreateWindow(display, RootWindow(display, vi->screen), 0, 0, WindowWidth, WindowHeight, 0, vi->depth, InputOutput, vi->visual, CWBorderPixel | CWColormap | CWEventMask, &swa); if (!win) { LOG_ERROR_MESSAGE("Failed to create window."); return -1; } - - { - auto SizeHints = XAllocSizeHints(); - SizeHints->flags = PMinSize; - SizeHints->min_width = MinWindowWidth; + + { + auto SizeHints = XAllocSizeHints(); + SizeHints->flags = PMinSize; + SizeHints->min_width = MinWindowWidth; SizeHints->min_height = MinWindowHeight; XSetWMNormalHints(display, win, SizeHints); XFree(SizeHints); } XMapWindow(display, win); - + glXCreateContextAttribsARBProc glXCreateContextAttribsARB = nullptr; { // Create an oldstyle context first, to get the correct function pointer for glXCreateContextAttribsARB - GLXContext ctx_old = glXCreateContext(display, vi, 0, GL_TRUE); - glXCreateContextAttribsARB = (glXCreateContextAttribsARBProc)glXGetProcAddress((const GLubyte*)"glXCreateContextAttribsARB"); + GLXContext ctx_old = glXCreateContext(display, vi, 0, GL_TRUE); + glXCreateContextAttribsARB = (glXCreateContextAttribsARBProc)glXGetProcAddress((const GLubyte*)"glXCreateContextAttribsARB"); glXMakeCurrent(display, None, NULL); glXDestroyContext(display, ctx_old); } - + if (glXCreateContextAttribsARB == nullptr) { LOG_ERROR("glXCreateContextAttribsARB entry point not found. Aborting."); return -1; } - + int Flags = GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB; #ifdef _DEBUG - Flags |= GLX_CONTEXT_DEBUG_BIT_ARB; -#endif - + Flags |= GLX_CONTEXT_DEBUG_BIT_ARB; +#endif + int major_version = 4; int minor_version = 3; + static int context_attribs[] = - { - GLX_CONTEXT_MAJOR_VERSION_ARB, major_version, - GLX_CONTEXT_MINOR_VERSION_ARB, minor_version, - GLX_CONTEXT_FLAGS_ARB, Flags, - None - }; - + { + GLX_CONTEXT_MAJOR_VERSION_ARB, major_version, + GLX_CONTEXT_MINOR_VERSION_ARB, minor_version, + GLX_CONTEXT_FLAGS_ARB, Flags, + None // + }; + constexpr int True = 1; - GLXContext ctx = glXCreateContextAttribsARB(display, fbc[0], NULL, True, context_attribs); + GLXContext ctx = glXCreateContextAttribsARB(display, fbc[0], NULL, True, context_attribs); if (!ctx) { LOG_ERROR("Failed to create GL context."); return -1; } XFree(fbc); - + glXMakeCurrent(display, win, ctx); TheApp->OnGLContextCreated(display, win); std::string Title = TheApp->GetAppTitle(); - - Timer timer; - auto PrevTime = timer.GetElapsedTime(); + + Timer timer; + auto PrevTime = timer.GetElapsedTime(); WindowTitleHelper TitleHelper(Title); - - while (true) - { - bool EscPressed = false; + + while (true) + { + bool EscPressed = false; XEvent xev; // Handle all events in the queue - while(XCheckMaskEvent(display, 0xFFFFFFFF, &xev)) + while (XCheckMaskEvent(display, 0xFFFFFFFF, &xev)) { TheApp->HandleXEvent(&xev); - switch(xev.type) + switch (xev.type) { case KeyPress: { KeySym keysym; - char buffer[80]; - int num_char = XLookupString((XKeyEvent *)&xev, buffer, _countof(buffer), &keysym, 0); - EscPressed = (keysym==XK_Escape); + char buffer[80]; + int num_char = XLookupString((XKeyEvent*)&xev, buffer, _countof(buffer), &keysym, 0); + EscPressed = (keysym == XK_Escape); } - + case ConfigureNotify: { - XConfigureEvent &xce = reinterpret_cast(xev); - if(xce.width != 0 && xce.height != 0) + XConfigureEvent& xce = reinterpret_cast(xev); + if (xce.width != 0 && xce.height != 0) TheApp->WindowResize(xce.width, xce.height); break; } } } - if(EscPressed) + if (EscPressed) break; // Render the scene - auto CurrTime = timer.GetElapsedTime(); + auto CurrTime = timer.GetElapsedTime(); auto ElapsedTime = CurrTime - PrevTime; - PrevTime = CurrTime; + PrevTime = CurrTime; TheApp->Update(CurrTime, ElapsedTime); TheApp->Render(); - + TheApp->Present(); auto TitleWithFPS = TitleHelper.GetTitleWithFPS(ElapsedTime); @@ -470,7 +474,7 @@ } TheApp.reset(); - + ctx = glXGetCurrentContext(); glXMakeCurrent(display, None, NULL); glXDestroyContext(display, ctx); @@ -478,7 +482,7 @@ XCloseDisplay(display); } -int main (int argc, char ** argv) +int main(int argc, char** argv) { bool UseVulkan = false; @@ -491,7 +495,7 @@ if (pos != nullptr) { pos += strlen(Key); - while(*pos != 0 && *pos == ' ')++pos; + while (*pos != 0 && *pos == ' ') ++pos; if (strcasecmp(pos, "GL") == 0) { UseVulkan = false; diff --git a/NativeApp/src/MacOS/MacOSAppBase.cpp b/NativeApp/src/MacOS/MacOSAppBase.cpp index f3d0313..e126c85 100644 --- a/NativeApp/src/MacOS/MacOSAppBase.cpp +++ b/NativeApp/src/MacOS/MacOSAppBase.cpp @@ -29,10 +29,10 @@ void MacOSAppBase::Update() { // Render the scene - auto CurrTime = timer.GetElapsedTime(); + auto CurrTime = timer.GetElapsedTime(); auto ElapsedTime = CurrTime - PrevTime; - PrevTime = CurrTime; + PrevTime = CurrTime; Update(CurrTime, ElapsedTime); } -} +} // namespace Diligent diff --git a/NativeApp/src/Win32/WinMain.cpp b/NativeApp/src/Win32/WinMain.cpp index 8f63460..63dd01c 100644 --- a/NativeApp/src/Win32/WinMain.cpp +++ b/NativeApp/src/Win32/WinMain.cpp @@ -38,10 +38,10 @@ int WINAPI WinMain(HINSTANCE instance, HINSTANCE, LPSTR, int cmdShow) { #if defined(_DEBUG) || defined(DEBUG) - _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); + _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); #endif - g_pTheApp.reset( CreateApplication() ); + g_pTheApp.reset(CreateApplication()); const auto* cmdLine = GetCommandLineA(); g_pTheApp->ProcessCommandLine(cmdLine); @@ -49,40 +49,41 @@ const auto* AppTitle = g_pTheApp->GetAppTitle(); #ifdef UNICODE - const auto* const WindowClassName = L"SampleApp"; + const auto* const WindowClassName = L"SampleApp"; #else - const auto* const WindowClassName = "SampleApp"; + const auto* const WindowClassName = "SampleApp"; #endif // Register our window class - WNDCLASSEX wcex = { sizeof(WNDCLASSEX), CS_HREDRAW|CS_VREDRAW, MessageProc, - 0L, 0L, instance, NULL, NULL, NULL, NULL, WindowClassName, NULL }; + WNDCLASSEX wcex = {sizeof(WNDCLASSEX), CS_HREDRAW | CS_VREDRAW, MessageProc, + 0L, 0L, instance, NULL, NULL, NULL, NULL, WindowClassName, NULL}; RegisterClassEx(&wcex); - int DesiredWidth = 0; + int DesiredWidth = 0; int DesiredHeight = 0; g_pTheApp->GetDesiredInitialWindowSize(DesiredWidth, DesiredHeight); // Create a window - LONG WindowWidth = DesiredWidth > 0 ? DesiredWidth : 1280; + LONG WindowWidth = DesiredWidth > 0 ? DesiredWidth : 1280; LONG WindowHeight = DesiredHeight > 0 ? DesiredHeight : 1024; - RECT rc = { 0, 0, WindowWidth, WindowHeight }; + RECT rc = {0, 0, WindowWidth, WindowHeight}; AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE); HWND wnd = CreateWindowA("SampleApp", AppTitle, - WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, - rc.right-rc.left, rc.bottom-rc.top, NULL, NULL, instance, NULL); + WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, + rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, instance, NULL); if (!wnd) { - MessageBoxA(NULL, "Cannot create window", "Error", MB_OK|MB_ICONERROR); + MessageBoxA(NULL, "Cannot create window", "Error", MB_OK | MB_ICONERROR); return 0; } ShowWindow(wnd, cmdShow); UpdateWindow(wnd); - + g_pTheApp->OnWindowCreated(wnd, WindowWidth, WindowHeight); AppTitle = g_pTheApp->GetAppTitle(); Diligent::Timer Timer; - auto PrevTime = Timer.GetElapsedTime(); + + auto PrevTime = Timer.GetElapsedTime(); double filteredFrameTime = 0.0; // Main message loop @@ -96,9 +97,10 @@ } else { - auto CurrTime = Timer.GetElapsedTime(); + auto CurrTime = Timer.GetElapsedTime(); auto ElapsedTime = CurrTime - PrevTime; - PrevTime = CurrTime; + PrevTime = CurrTime; + g_pTheApp->Update(CurrTime, ElapsedTime); g_pTheApp->Render(); @@ -106,14 +108,14 @@ g_pTheApp->Present(); double filterScale = 0.2; - filteredFrameTime = filteredFrameTime * (1.0 - filterScale) + filterScale * ElapsedTime; + filteredFrameTime = filteredFrameTime * (1.0 - filterScale) + filterScale * ElapsedTime; std::stringstream fpsCounterSS; fpsCounterSS << AppTitle << " - " << std::fixed << std::setprecision(1) << filteredFrameTime * 1000; fpsCounterSS << " ms (" << 1.0 / filteredFrameTime << " fps)"; SetWindowTextA(wnd, fpsCounterSS.str().c_str()); } } - + g_pTheApp.reset(); return (int)msg.wParam; @@ -122,24 +124,24 @@ // Called every time the NativeNativeAppBase receives a message LRESULT CALLBACK MessageProc(HWND wnd, UINT message, WPARAM wParam, LPARAM lParam) { - if(g_pTheApp) + if (g_pTheApp) { auto res = g_pTheApp->HandleWin32Message(wnd, message, wParam, lParam); if (res != 0) return res; } - switch (message) + switch (message) { case WM_PAINT: - { - PAINTSTRUCT ps; - BeginPaint(wnd, &ps); - EndPaint(wnd, &ps); - return 0; - } + { + PAINTSTRUCT ps; + BeginPaint(wnd, &ps); + EndPaint(wnd, &ps); + return 0; + } case WM_SIZE: // Window size has been changed - if( g_pTheApp ) + if (g_pTheApp) { g_pTheApp->WindowResize(LOWORD(lParam), HIWORD(lParam)); } @@ -156,7 +158,7 @@ case WM_GETMINMAXINFO: { - LPMINMAXINFO lpMMI = (LPMINMAXINFO)lParam; + LPMINMAXINFO lpMMI = (LPMINMAXINFO)lParam; lpMMI->ptMinTrackSize.x = 320; lpMMI->ptMinTrackSize.y = 240; return 0;