diff --git a/Editor/main.cpp b/Editor/main.cpp index cf2190176..4e9759001 100644 --- a/Editor/main.cpp +++ b/Editor/main.cpp @@ -56,8 +56,6 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance, HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WICKEDENGINEGAME)); - editor.Initialize(); - MSG msg = { 0 }; while (msg.message != WM_QUIT) @@ -131,8 +129,6 @@ BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) if (enabled != 0) { file >> voidStr >> x >> voidStr >> y >> voidStr >> w >> voidStr >> h >> voidStr >> editor.fullscreen >> voidStr >> borderless; - editor.screenW = w; - editor.screenH = h; } } file.close(); diff --git a/Template_Windows/ReadMe.txt b/Template_Windows/ReadMe.txt index 6adda5b04..cb69f7daa 100644 --- a/Template_Windows/ReadMe.txt +++ b/Template_Windows/ReadMe.txt @@ -6,5 +6,5 @@ Contents of interest: This is the precompiled header file, WickedEngine.h has been included here - main.cpp This is the windows application entry point. The MainComponent has been created here, this is the engine runtime component. - main.Initialize(), main.SetWindow() and main.Run() functions are called here which are necessary to setting up an application. + main.SetWindow() and main.Run() functions are called here which are necessary to setting up an application. diff --git a/Template_Windows/main.cpp b/Template_Windows/main.cpp index 07abbd8c4..41f1a3d1e 100644 --- a/Template_Windows/main.cpp +++ b/Template_Windows/main.cpp @@ -45,7 +45,6 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance, wiRenderer::GetShaderPath() = "../WickedEngine/shaders/"; // search for shaders elsewhere wiFont::GetFontPath() = "../WickedEngine/fonts/"; // search for fonts elsewhere - main.Initialize(); // initialize engine systems (mandatory) main.infoDisplay.active = true; // just show some basic info... diff --git a/Tests/main.cpp b/Tests/main.cpp index 47585c75a..4436a0825 100644 --- a/Tests/main.cpp +++ b/Tests/main.cpp @@ -47,8 +47,6 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance, HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WICKEDENGINETESTS)); - tests.Initialize(); - MSG msg = { 0 }; while (msg.message != WM_QUIT) diff --git a/WickedEngine/MainComponent.cpp b/WickedEngine/MainComponent.cpp index b0adff690..cfa1f5a8a 100644 --- a/WickedEngine/MainComponent.cpp +++ b/WickedEngine/MainComponent.cpp @@ -26,26 +26,24 @@ using namespace std; using namespace wiGraphicsTypes; -MainComponent::MainComponent() -{ - // This call also saves the current working dir as the original one on this first call - wiHelper::GetOriginalWorkingDirectory(); - - activeComponent = new RenderableComponent(); -} - - MainComponent::~MainComponent() { + // This usually means appllication is terminating. Wait for GPU to finish rendering. wiRenderer::GetDevice()->WaitForGPU(); } void MainComponent::Initialize() { + if (initialized) + { + return; + } + initialized = true; // User can also create a graphics device if custom logic is desired, but he must do before this function! if (wiRenderer::GetDevice() == nullptr) { + auto window = wiWindowRegistration::GetRegisteredWindow(); bool debugdevice = wiStartupArguments::HasArgument("debugdevice"); @@ -91,9 +89,17 @@ void MainComponent::activateComponent(RenderableComponent* component, float fade // Fade manager will activate on fadeout fadeManager.Clear(); fadeManager.Start(fadeSeconds, fadeColor, [this,component]() { + if (component == nullptr) + { return; - activeComponent->Stop(); + } + + if (activeComponent != nullptr) + { + activeComponent->Stop(); + } + component->Start(); activeComponent = component; }); @@ -101,6 +107,12 @@ void MainComponent::activateComponent(RenderableComponent* component, float fade void MainComponent::Run() { + if (!initialized) + { + // Initialize in a lazy way, so the user application doesn't have to call this explicitly + Initialize(); + initialized = true; + } if (!wiInitializer::IsInitializeFinished()) { // Until engine is not loaded, present initialization screen... @@ -192,7 +204,11 @@ void MainComponent::Run() void MainComponent::Update(float dt) { wiCpuInfo::UpdateFrame(); - getActiveComponent()->Update(dt); + + if (getActiveComponent() != nullptr) + { + getActiveComponent()->Update(dt); + } wiLua::GetGlobal()->Update(); } @@ -202,7 +218,10 @@ void MainComponent::FixedUpdate() wiBackLog::Update(); wiLua::GetGlobal()->FixedUpdate(); - getActiveComponent()->FixedUpdate(); + if (getActiveComponent() != nullptr) + { + getActiveComponent()->FixedUpdate(); + } } void MainComponent::Render() @@ -213,13 +232,19 @@ void MainComponent::Render() wiRenderer::BindPersistentState(GRAPHICSTHREAD_IMMEDIATE); wiImage::BindPersistentState(GRAPHICSTHREAD_IMMEDIATE); wiFont::BindPersistentState(GRAPHICSTHREAD_IMMEDIATE); - getActiveComponent()->Render(); + if (getActiveComponent() != nullptr) + { + getActiveComponent()->Render(); + } wiProfiler::EndRange(GRAPHICSTHREAD_IMMEDIATE); // GPU Frame } void MainComponent::Compose() { - getActiveComponent()->Compose(); + if (getActiveComponent() != nullptr) + { + getActiveComponent()->Compose(); + } if (fadeManager.IsActive()) { @@ -298,16 +323,7 @@ void MainComponent::Compose() #ifndef WINSTORE_SUPPORT bool MainComponent::SetWindow(wiWindowRegistration::window_type window, HINSTANCE hInst) { - this->window = window; - this->instance = hInst; - - if (screenW == 0 || screenH == 0) - { - RECT rect = RECT(); - GetClientRect(window, &rect); - screenW = rect.right - rect.left; - screenH = rect.bottom - rect.top; - } + this->hInst = hInst; wiWindowRegistration::RegisterWindow(window); @@ -316,11 +332,6 @@ bool MainComponent::SetWindow(wiWindowRegistration::window_type window, HINSTANC #else bool MainComponent::SetWindow(wiWindowRegistration::window_type window) { - screenW = (int)window->Bounds.Width; - screenH = (int)window->Bounds.Height; - - this->window = window; - wiWindowRegistration::RegisterWindow(window); return true; diff --git a/WickedEngine/MainComponent.h b/WickedEngine/MainComponent.h index 00bf76e67..7e7dd89a5 100644 --- a/WickedEngine/MainComponent.h +++ b/WickedEngine/MainComponent.h @@ -9,10 +9,11 @@ class RenderableComponent; class MainComponent { -private: +protected: RenderableComponent* activeComponent = nullptr; float targetFrameRate = 60; bool frameskip = true; + bool initialized = false; wiFadeManager fadeManager; @@ -20,19 +21,19 @@ private: float deltaTimeAccumulator = 0; wiTimer timer; public: - MainComponent(); virtual ~MainComponent(); - int screenW = 0; - int screenH = 0; bool fullscreen = false; // Runs the main engine loop void Run(); + // This will activate a RenderableComponent as the active one, so it will run its Update, FixedUpdate, Render and Compose functions + // You can set a fade time and fade screen color so that switching components will happen when the screen is faded out. Then it will fade back to the new component void activateComponent(RenderableComponent* component, float fadeSeconds = 0, const wiColor& fadeColor = wiColor(0,0,0,255)); - RenderableComponent* getActiveComponent(){ return activeComponent; } + inline RenderableComponent* getActiveComponent(){ return activeComponent; } + // You can use this as a self-contained resource manager if you want to avoid using the wiResourceManager::GetGlobal() wiResourceManager Content; // Set the desired target framerate for the FixedUpdate() loop (default = 60) @@ -44,19 +45,26 @@ public: // disabled : the FixedUpdate() loop will run every frame only once. void setFrameSkip(bool enabled) { frameskip = enabled; } - // Initializes all engine components + // This is where the critical initializations happen (before any rendering or anything else) virtual void Initialize(); + // This is where application-wide updates get executed once per frame. + // RenderableComponent::Update is also called from here for the active component virtual void Update(float dt); + // This is where application-wide updates get executed in a fixed timestep based manner. + // RenderableComponent::FixedUpdate is also called from here for the active component virtual void FixedUpdate(); + // This is where application-wide rendering happens to offscreen buffers. + // RenderableComponent::Render is also called from here for the active component virtual void Render(); + // This is where the application will render to the screen (backbuffer) virtual void Compose(); - wiWindowRegistration::window_type window; - #ifndef WINSTORE_SUPPORT - HINSTANCE instance; - bool SetWindow(wiWindowRegistration::window_type window, HINSTANCE hInst); + HINSTANCE hInst = NULL; + // You need to call this before calling Run() or Initialize() if you want to render to a Win32 window handle + bool SetWindow(wiWindowRegistration::window_type window, HINSTANCE hInst = NULL); #else + // You need to call this before calling Run() or Initialize() if you want to render to a UWP window bool SetWindow(wiWindowRegistration::window_type window); #endif @@ -64,23 +72,19 @@ public: struct InfoDisplayer { // activate the whole display - bool active; + bool active = false; // display engine version number - bool watermark; + bool watermark = true; // display framerate - bool fpsinfo; + bool fpsinfo = false; // display cpu utilization - bool cpuinfo; + bool cpuinfo = false; // display resolution info - bool resolution; + bool resolution = false; // display engine initialization time - bool initstats; + bool initstats = false; // text size - int size; - - InfoDisplayer() :active(false), watermark(true), fpsinfo(false), cpuinfo(false), - resolution(false), size(-1) - {} + int size = -1; }; // display all-time engine information text InfoDisplayer infoDisplay; diff --git a/WickedEngine/wiGraphicsDevice_DX11.h b/WickedEngine/wiGraphicsDevice_DX11.h index a5921f30a..f1e3453e0 100644 --- a/WickedEngine/wiGraphicsDevice_DX11.h +++ b/WickedEngine/wiGraphicsDevice_DX11.h @@ -50,7 +50,7 @@ namespace wiGraphicsTypes public: GraphicsDevice_DX11(wiWindowRegistration::window_type window, bool fullscreen = false, bool debuglayer = false); - ~GraphicsDevice_DX11(); + virtual ~GraphicsDevice_DX11(); HRESULT CreateBuffer(const GPUBufferDesc *pDesc, const SubresourceData* pInitialData, GPUBuffer *ppBuffer) override; HRESULT CreateTexture1D(const TextureDesc* pDesc, const SubresourceData *pInitialData, Texture1D **ppTexture1D) override; diff --git a/WickedEngine/wiGraphicsDevice_DX12.h b/WickedEngine/wiGraphicsDevice_DX12.h index bbafefcaf..5592f3fc2 100644 --- a/WickedEngine/wiGraphicsDevice_DX12.h +++ b/WickedEngine/wiGraphicsDevice_DX12.h @@ -135,7 +135,7 @@ namespace wiGraphicsTypes public: GraphicsDevice_DX12(wiWindowRegistration::window_type window, bool fullscreen = false, bool debuglayer = false); - ~GraphicsDevice_DX12(); + virtual ~GraphicsDevice_DX12(); HRESULT CreateBuffer(const GPUBufferDesc *pDesc, const SubresourceData* pInitialData, GPUBuffer *ppBuffer) override; HRESULT CreateTexture1D(const TextureDesc* pDesc, const SubresourceData *pInitialData, Texture1D **ppTexture1D) override; diff --git a/WickedEngine/wiGraphicsDevice_Vulkan.h b/WickedEngine/wiGraphicsDevice_Vulkan.h index 75b3e8edb..99999b14b 100644 --- a/WickedEngine/wiGraphicsDevice_Vulkan.h +++ b/WickedEngine/wiGraphicsDevice_Vulkan.h @@ -198,7 +198,7 @@ namespace wiGraphicsTypes public: GraphicsDevice_Vulkan(wiWindowRegistration::window_type window, bool fullscreen = false, bool debuglayer = false); - ~GraphicsDevice_Vulkan(); + virtual ~GraphicsDevice_Vulkan(); HRESULT CreateBuffer(const GPUBufferDesc *pDesc, const SubresourceData* pInitialData, GPUBuffer *ppBuffer) override; HRESULT CreateTexture1D(const TextureDesc* pDesc, const SubresourceData *pInitialData, Texture1D **ppTexture1D) override; diff --git a/WickedEngine/wiHelper.cpp b/WickedEngine/wiHelper.cpp index 3b75ba2d3..5c080dc47 100644 --- a/WickedEngine/wiHelper.cpp +++ b/WickedEngine/wiHelper.cpp @@ -176,9 +176,9 @@ namespace wiHelper return __appDir; } + static const string __originalWorkingDir = GetWorkingDirectory(); string GetOriginalWorkingDirectory() { - static const string __originalWorkingDir = GetWorkingDirectory(); return __originalWorkingDir; } diff --git a/WickedEngine/wiVersion.cpp b/WickedEngine/wiVersion.cpp index 45eb30c1f..e915e02bc 100644 --- a/WickedEngine/wiVersion.cpp +++ b/WickedEngine/wiVersion.cpp @@ -9,7 +9,7 @@ namespace wiVersion // minor features, major updates const int minor = 21; // minor bug fixes, alterations, refactors, updates - const int revision = 36; + const int revision = 37; long GetVersion()