Merge pull request #136 from SHADE-DP/SP3-6-c-scripting

Added Application, GameObject.Find(), implicit null checks for scripts and components and fixed bugs with Script retrieval and deletion
This commit is contained in:
XiaoQiDigipen 2022-10-31 15:32:57 +08:00 committed by GitHub
commit 69fbd4167d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 349 additions and 25 deletions

View File

@ -15,7 +15,7 @@
#include "ECS_Base/System/SHSystemRoutine.h"
#include "Resource/SHHandle.h"
#include "EditorWindow/SHEditorWindow.h"
#include "Tools/SHLogger.h"
#include "Tools/SHLog.h"
#include "Gizmos/SHTransformGizmo.h"
@ -76,7 +76,7 @@ namespace SHADE
}
else
{
SHLOG_WARNING("Attempt to create duplicate of Editor window type")
SHLog::Warning("Attempt to create duplicate of Editor window type");
}
}

View File

@ -293,6 +293,10 @@ namespace SHADE
//Handle<SHVkRenderpass> GetRenderPass() const { return renderPass; }
/*-----------------------------------------------------------------------------*/
/* Getters */
/*-----------------------------------------------------------------------------*/
SHWindow* GetWindow() noexcept { return window; }
private:
/*-----------------------------------------------------------------------------*/
@ -320,7 +324,7 @@ namespace SHADE
SHWindow* window = nullptr;
// Middle End Resources
SHResourceHub resourceManager;
SHResourceHub resourceManager;
SHMeshLibrary meshLibrary;
SHTextureLibrary texLibrary;
SHSamplerCache samplerCache;

View File

@ -0,0 +1,77 @@
/************************************************************************************//*!
\file SHGraphicsSystemInterface.cpp
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Oct 31, 2022
\brief Contains the definitions of the functions of the static
SHGraphicsSystemInterface class.
Copyright (C) 2022 DigiPen Institute of Technology.
Reproduction or disclosure of this file or its contents without the prior written consent
of DigiPen Institute of Technology is prohibited.
*//*************************************************************************************/
// Precompiled Headers
#include "SHpch.h"
// Primary Header
#include "SHGraphicsSystemInterface.h"
// Project Includes
#include "ECS_Base/Managers/SHSystemManager.h"
#include "Graphics/MiddleEnd/Interface/SHGraphicsSystem.h"
#include "Graphics/Windowing/SHWindow.h"
namespace SHADE
{
/*-----------------------------------------------------------------------------------*/
/* Static Usage Functions */
/*-----------------------------------------------------------------------------------*/
uint32_t SHGraphicsSystemInterface::GetWindowWidth()
{
auto gfxSystem = SHSystemManager::GetSystem<SHGraphicsSystem>();
if (gfxSystem)
{
const auto WND = gfxSystem->GetWindow();
return WND->GetWindowSize().first;
}
SHLOG_WARNING("[SHGraphicsSystemInterface] Failed to get window width. Value of 0 returned instead.");
return 0;
}
uint32_t SHGraphicsSystemInterface::GetWindowHeight()
{
auto gfxSystem = SHSystemManager::GetSystem<SHGraphicsSystem>();
if (gfxSystem)
{
const auto WND = gfxSystem->GetWindow();
return WND->GetWindowSize().second;
}
SHLOG_WARNING("[SHGraphicsSystemInterface] Failed to get window height. Value of 0 returned instead.");
return 0;
}
bool SHGraphicsSystemInterface::IsFullscreen()
{
auto gfxSystem = SHSystemManager::GetSystem<SHGraphicsSystem>();
if (gfxSystem)
{
const auto WND = gfxSystem->GetWindow();
return WND->GetWindowData().isFullscreen;
}
SHLOG_WARNING("[SHGraphicsSystemInterface] Failed to get window fullscreen status. Value of false returned instead.");
return false;
}
void SHGraphicsSystemInterface::CloseWindow()
{
auto gfxSystem = SHSystemManager::GetSystem<SHGraphicsSystem>();
if (gfxSystem)
{
auto WND = gfxSystem->GetWindow();
return WND->Close();
}
SHLOG_WARNING("[SHGraphicsSystemInterface] Failed to close window.");
}
}

View File

@ -0,0 +1,52 @@
/************************************************************************************//*!
\file SHGraphicsSystemInterface.h
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Oct 31, 2022
\brief Contains the definition of the SHGraphicsSystemInterface static class.
Copyright (C) 2022 DigiPen Institute of Technology.
Reproduction or disclosure of this file or its contents without the prior written consent
of DigiPen Institute of Technology is prohibited.
*//*************************************************************************************/
#pragma once
namespace SHADE
{
/// <summary>
/// Static class that wraps up certain functions in the SHGraphicsSystem so that
/// accessing it from SHADE_Managed would not cause issues due to C++20 features.
/// </summary>
class SH_API SHGraphicsSystemInterface final
{
public:
/*---------------------------------------------------------------------------------*/
/* Constructor */
/*---------------------------------------------------------------------------------*/
SHGraphicsSystemInterface() = delete;
/*---------------------------------------------------------------------------------*/
/* Static Usage Functions */
/*---------------------------------------------------------------------------------*/
/// <summary>
/// Retrieves the current window width.
/// </summary>
/// <returns>The current window width.</returns>
static uint32_t GetWindowWidth();
/// <summary>
/// Retrieves the current window height.
/// </summary>
/// <returns>The current window height.</returns>
static uint32_t GetWindowHeight();
/// <summary>
/// Retrieves the current window fullscreen status.
/// </summary>
/// <returns>The current window fullscreen status..</returns>
static bool IsFullscreen();
/// <summary>
/// Closes the current window, and depending on the implementation, should also
/// close the application.
/// </summary>
static void CloseWindow();
};
}

View File

@ -260,7 +260,7 @@ namespace SHADE
return wndHWND;
}
const WindowData SHWindow::GetWindowData()
const WindowData SHWindow::GetWindowData() const
{
return wndData;
}

View File

@ -123,7 +123,7 @@ namespace SHADE
HWND GetHWND();
const WindowData GetWindowData();
const WindowData GetWindowData() const;
CALLBACKID RegisterWindowSizeCallback(WindowResizeCallbackFn);
void UnregisterWindowSizeCallback(CALLBACKID const& callbackid);

View File

@ -54,7 +54,7 @@ namespace SHADE
/* System Routine Functions - FrameCleanUpRoutine */
/*-----------------------------------------------------------------------------------*/
SHScriptEngine::FrameCleanUpRoutine::FrameCleanUpRoutine()
: SHSystemRoutine("Script Engine Frame Clean Up", false)
: SHSystemRoutine("Script Engine Frame Clean Up", true)
{}
void SHScriptEngine::FrameCleanUpRoutine::Execute(double) noexcept
{

View File

@ -9,7 +9,7 @@ Copyright (C) 2022 DigiPen Institute of Technology.
Reproduction or disclosure of this file or its contents without the prior written consent
of DigiPen Institute of Technology is prohibited.
*//*************************************************************************************/
#pragma once
// Standard Library
#include <string>
// Project Headers

View File

@ -25,11 +25,16 @@ project "SHADE_Managed"
includedirs
{
"%{prj.location}/src",
}
externalincludedirs
{
"%{IncludeDir.spdlog}/include",
"%{IncludeDir.imgui}",
"%{IncludeDir.imguizmo}",
"%{IncludeDir.imnodes}",
"%{IncludeDir.yamlcpp}",
"%{IncludeDir.SDL}\\include",
"%{IncludeDir.RTTR}/include",
"%{IncludeDir.dotnet}\\include",
"%{IncludeDir.reactphysics3d}\\include",
@ -38,13 +43,16 @@ project "SHADE_Managed"
libdirs
{
"%{IncludeDir.RTTR}/lib"
"%{IncludeDir.RTTR}/lib",
"%{IncludeDir.SDL}/lib"
}
links
{
"yaml-cpp",
"imgui",
"SDL2.lib",
"SDL2main.lib",
"SHADE_Engine",
"SHADE_CSharp"
}

View File

@ -67,6 +67,11 @@ namespace SHADE
ScriptStore::RemoveScript<T>(owner.GetEntity());
}
BaseComponent::operator bool(BaseComponent^ c)
{
return c != nullptr;
}
/*---------------------------------------------------------------------------------*/
/* Constructors */
/*---------------------------------------------------------------------------------*/

View File

@ -101,6 +101,15 @@ namespace SHADE
generic<typename T> where T : ref class, Script
void RemoveScript();
/*-----------------------------------------------------------------------------*/
/* Operator Overloads */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Implicit conversion operator to enable checking if a component is null.
/// </summary>
/// <param name="c">Component to check.</param>
static operator bool(BaseComponent^ c);
protected:
/*-----------------------------------------------------------------------------*/
/* Constructors */

View File

@ -0,0 +1,71 @@
/************************************************************************************//*!
\file Application.cxx
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Oct 31, 2022
\brief Contains the definitions of the functions in the static managed
Application class.
Note: This file is written in C++17/CLI.
Copyright (C) 2022 DigiPen Institute of Technology.
Reproduction or disclosure of this file or its contents without the prior written consent
of DigiPen Institute of Technology is prohibited.
*//*************************************************************************************/
// Precompiled Headers
#include "SHpch.h"
// Primary Header
#include "Application.hxx"
// External Dependencies
#include "ECS_Base/Managers/SHSystemManager.h"
#include "Editor/SHEditor.h"
#include "Graphics/MiddleEnd/Interface/SHGraphicsSystemInterface.h"
namespace SHADE
{
/*---------------------------------------------------------------------------------*/
/* Properties */
/*---------------------------------------------------------------------------------*/
bool Application::IsPlaying::get()
{
auto editor = SHSystemManager::GetSystem<SHEditor>();
if (editor)
return editor->editorState == SHEditor::State::PLAY
||
editor->editorState == SHEditor::State::PAUSE;
return true;
}
bool Application::IsPaused::get()
{
auto editor = SHSystemManager::GetSystem<SHEditor>();
if (editor)
return editor->editorState == SHEditor::State::PAUSE;
return false;
}
int Application::WindowWidth::get()
{
return SHGraphicsSystemInterface::GetWindowWidth();
}
int Application::WindowHeight::get()
{
return SHGraphicsSystemInterface::GetWindowWidth();
}
bool Application::IsFullscreen::get()
{
return SHGraphicsSystemInterface::IsFullscreen();
}
/*void Application::IsFullscreen::set(bool value)
{
return SHGraphicsSystemInterface::SetFullscreen(value);
}*/
/*---------------------------------------------------------------------------------*/
/* Usage Functions */
/*---------------------------------------------------------------------------------*/
void Application::Quit()
{
SHGraphicsSystemInterface::CloseWindow();
}
}

View File

@ -0,0 +1,77 @@
/************************************************************************************//*!
\file Application.hxx
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Oct 31, 2022
\brief Contains the definitions of a managed static Application class.
Note: This file is written in C++17/CLI.
Copyright (C) 2022 DigiPen Institute of Technology.
Reproduction or disclosure of this file or its contents without the prior written consent
of DigiPen Institute of Technology is prohibited.
*//*************************************************************************************/
#pragma once
namespace SHADE
{
/// <summary>
/// Static class that contains useful properties for querying the state of the
/// engine.
/// </summary>
public ref class Application abstract sealed
{
public:
/*-----------------------------------------------------------------------------*/
/* Properties */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Whether or not the engine is playing. This will always be true on Publish.
/// On Debug/Release builds, this is true when the editor is in Play Mode. It
/// will also be true even if the editor is in Play Mode but is paused.
/// </summary>
static property bool IsPlaying
{
bool get();
}
/// <summary>
/// Whether or not the engine is in a paused state where script updates and
/// physics are not in play.
/// </summary>
static property bool IsPaused
{
bool get();
}
/// <summary>
/// Retrieves the designated width of the current window.
/// </summary>
static property int WindowWidth
{
int get();
}
/// <summary>
/// Retrieves the designated height of the current window.
/// </summary>
static property int WindowHeight
{
int get();
}
/// <summary>
/// Whether or not the application is currently in fullscreen mode or not.
/// </summary>
static property bool IsFullscreen
{
bool get();
// TODO: once implemented on SHADE_Engine
//void set(bool value);
}
/*-----------------------------------------------------------------------------*/
/* Usage Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Marks the application to stop at the end of the current frame.
/// </summary>
static void Quit();
};
}

View File

@ -19,6 +19,7 @@ of DigiPen Institute of Technology is prohibited.
#include "ECS_Base/Managers/SHEntityManager.h"
// Project Headers
#include "ECS.hxx"
#include "Utility/Convert.hxx"
#include "Scripts/ScriptStore.hxx"
namespace SHADE
@ -39,7 +40,13 @@ namespace SHADE
System::Nullable<GameObject> GameObject::Find(System::String ^ name)
{
// Search the GameObjectLibrary for an Entity with the specified name
throw gcnew System::NotImplementedException();
const auto ENTITY_ID = SHEntityManager::GetEntityByName(Convert::ToNative(name));
if (ENTITY_ID == MAX_EID)
{
return {};
}
return GameObject(ENTITY_ID);
}
/*---------------------------------------------------------------------------------*/

View File

@ -18,6 +18,8 @@ of DigiPen Institute of Technology is prohibited.
#include "Script.hxx"
// Project Headers
#include "Utility/Debug.hxx"
#include "ScriptStore.hxx"
#include "Engine/ECS.hxx"
namespace SHADE
{
@ -49,8 +51,7 @@ namespace SHADE
generic <typename T>
void Script::RemoveComponent()
{
throw gcnew System::NotImplementedException;
//ECS::RemoveComponent<T>(owner.GetNativeEntity());
owner.RemoveComponent<T>();
}
/*---------------------------------------------------------------------------------*/
@ -59,37 +60,37 @@ namespace SHADE
generic <typename T>
T Script::AddScript()
{
throw gcnew System::NotImplementedException;
//return ScriptStore::AddScript<T>(owner.GetEntity());
return ScriptStore::AddScript<T>(owner.GetEntity());
}
generic <typename T>
T Script::GetScript()
{
throw gcnew System::NotImplementedException;
//return ScriptStore::GetScript<T>(owner.GetEntity());
return ScriptStore::GetScript<T>(owner.GetEntity());
}
generic <typename T>
T Script::GetScriptInChildren()
{
throw gcnew System::NotImplementedException;
//return ScriptStore::GetScriptInChildren<T>(owner.GetEntity());
return ScriptStore::GetScriptInChildren<T>(owner.GetEntity());
}
generic <typename T>
System::Collections::Generic::IEnumerable<T>^ Script::GetScripts()
{
throw gcnew System::NotImplementedException;
//return ScriptStore::GetScripts<T>(owner.GetEntity());
return ScriptStore::GetScripts<T>(owner.GetEntity());
}
generic <typename T>
void Script::RemoveScript()
{
throw gcnew System::NotImplementedException;
//ScriptStore::RemoveScript<T>(owner.GetEntity());
ScriptStore::RemoveScript<T>(owner.GetEntity());
}
Script::operator bool(Script^ s)
{
return s != nullptr;
}
/*---------------------------------------------------------------------------------*/
/* "All-time" Lifecycle Functions */
/*---------------------------------------------------------------------------------*/

View File

@ -153,6 +153,15 @@ namespace SHADE
generic<typename T> where T : ref class, Script
void RemoveScript();
/*-----------------------------------------------------------------------------*/
/* Operator Overloads */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Implicit conversion operator to enable checking if a component is null.
/// </summary>
/// <param name="c">Component to check.</param>
static operator bool(Script^ s);
internal:
/*-----------------------------------------------------------------------------*/
/* "All-Time" Lifecycle Functions */

View File

@ -27,6 +27,7 @@ of DigiPen Institute of Technology is prohibited.
#include "Script.hxx"
#include "Engine/Entity.hxx"
#include "Serialisation/ReflectionUtilities.hxx"
#include "Engine/Application.hxx"
namespace SHADE
{
@ -372,8 +373,11 @@ namespace SHADE
// Clear the queue
while (disposalQueue.Count > 0)
{
Script^ script = disposalQueue.Dequeue();
script->OnDestroy();
Script^ script = disposalQueue.Dequeue();
if (Application::IsPlaying)
{
script->OnDestroy();
}
auto entity = script->Owner.GetEntity();
auto scriptList = scripts[script->Owner.GetEntity()];
scriptList->Remove(script);
@ -388,7 +392,7 @@ namespace SHADE
{
SAFE_NATIVE_CALL_BEGIN
// Run the deinit all scripts if needed
//if (Application::IsPlaying)
if (Application::IsPlaying)
{
Debug::Log("Running OnDestroy() for scripts.");
for each (System::Collections::Generic::KeyValuePair<Entity, ScriptList^> entity in scripts)