diff --git a/Deer/Include/DeerCore/Application.h b/Deer/Include/DeerCore/Application.h deleted file mode 100755 index b7bff53..0000000 --- a/Deer/Include/DeerCore/Application.h +++ /dev/null @@ -1,32 +0,0 @@ -#pragma once -#include "DeerCore/Tools/Memory.h" - -namespace Deer { - class ImGuiLayer; - namespace Core { - extern int argc; - extern char** argv; - } // namespace Core - - class Timestep { - public: - Timestep(float time = 0.0f) : m_time(time) {} - - float getSeconds() const { return m_time; } - float getMilliseconds() const { return m_time * 1000; } - - private: - float m_time; - }; - - namespace Application { - using Function = void (*)(); - - extern bool running; - - void run(); - void setTickCallback(Function); - - void shutdown(); - } // namespace Application -} // namespace Deer diff --git a/Deer/Include/DeerCore/Engine.h b/Deer/Include/DeerCore/Engine.h new file mode 100644 index 0000000..79ffa8d --- /dev/null +++ b/Deer/Include/DeerCore/Engine.h @@ -0,0 +1,12 @@ +#pragma once + +namespace Deer { + using Function = void (*)(); + + namespace Engine { + void init(); + void shutdown(); + + void setUpdateCallback(Function); + } // namespace Engine +} // namespace Deer \ No newline at end of file diff --git a/Deer/Include/DeerCore/EntityEnviroment.h b/Deer/Include/DeerCore/EntityEnviroment.h index 88ec23c..cff33f5 100755 --- a/Deer/Include/DeerCore/EntityEnviroment.h +++ b/Deer/Include/DeerCore/EntityEnviroment.h @@ -21,15 +21,6 @@ namespace Deer { class Entity; class EntityEnvironment { - // Note: Outdated note - ///////// NOTES /////////// - // - The entity id means the position in a array defined in EntityEnvironment - // - The entity id is relative to a EntityEnvironment so it can be a complete - // diferent entity in other environments - // - The entity number 0 is allways the root - // - There is a limit defined by ENVIRONMENT_MAX_ENTITIES of how many - // entities can be in an EntityEnvironment - ///////// NOTES /////////// public: EntityEnvironment(); ~EntityEnvironment(); @@ -40,17 +31,17 @@ namespace Deer { // Clears all entities void clear(); // Obtains the entity - Entity& getEntity(uint16_t id); - bool entityExists(uint16_t id) const; - uint16_t getEntityCount() const; + Entity& getEntity(uint32_t id); + bool entityExists(uint32_t id) const; + uint32_t getEntityCount() const; // Creates a entity child at root // WARNING: This method can change internal pointers and invalidate entitiy references Entity& createEntity(const std::string& name = ""); // Can be slow! This has to empty the stack of empty entities in case its necessary so use it in ascendent order! // WARNING: This method can change internal pointers and invalidate entitiy references - Entity& createEntityWithId(uint16_t id, const std::string& name = ""); - void destroyEntity(uint16_t id); + Entity& createEntityWithId(uint32_t id, const std::string& name = ""); + void destroyEntity(uint32_t id); // Special behaviour // WARNING: This method can change internal pointers and invalidate entitiy references @@ -76,22 +67,6 @@ namespace Deer { friend class Entity; }; - struct EntityData { - int toDo; - }; - - struct EntityEnvironmentData { - std::vector entities; - }; - - template <> - class ResourceBuilder { - public: - using BaseDataType = EntityEnvironmentData; - static Scope buildResource(const BaseDataType& baseData); - }; - - // Warning: This calss does not initialize for performance class Entity { public: Entity() {} @@ -103,9 +78,9 @@ namespace Deer { Entity& duplicate(); void destroy(); - uint16_t getId() const { return entId; } + uint32_t getId() const { return entId; } Entity& getParent() const; - inline uint16_t getParentId() const { return parentId; } + inline uint32_t getParentId() const { return parentId; } // TODO, enable transfer entitys from difrent environments void setParent(Entity& parent); @@ -125,10 +100,10 @@ namespace Deer { private: EntityEnvironment* environment = nullptr; entt::entity entHandle = entt::null; - uint16_t entId = 0; - uint16_t parentId = 0; + uint32_t entId = 0; + uint32_t parentId = 0; - Entity(entt::entity handle, EntityEnvironment* scene, uint16_t entityID); + Entity(entt::entity handle, EntityEnvironment* scene, uint32_t entityID); friend class EntityEnvironment; @@ -139,8 +114,7 @@ namespace Deer { !environment->m_registry->all_of(entHandle), "Entity already have component {0}", typeid(T).name()); - return environment->m_registry->emplace( - entHandle, std::forward(args)...); + return environment->m_registry->emplace(entHandle, std::forward(args)...); } template diff --git a/Deer/Include/DeerCore/ScriptSystem.h b/Deer/Include/DeerCore/ScriptSystem.h new file mode 100644 index 0000000..4dad41f --- /dev/null +++ b/Deer/Include/DeerCore/ScriptSystem.h @@ -0,0 +1,41 @@ +#pragma once +#include "DeerCore/Tools/Memory.h" +#include "DeerCore/Tools/TypeDefs.h" + +#include "angelscript.h" +#include +#include + +class asIScriptEngine; +namespace Deer { + class ScriptEnvironment; + + namespace ScriptSystem { + void init(); + void shutdown(); + + asIScriptEngine* getScriptEngine(); + Scope createScriptEnvironment(const char* baseDataType); + } // namespace ScriptSystem + + class EnvironmentSystems { + }; + + class ScriptInstance { + + } + + class EnvironmentInstances { + }; + + class ScriptEnvironment { + public: + private: + asIScriptContext* context; + std::string baseTypeName; + asITypeInfo* baseType; + + ScriptEnvironment(asITypeInfo*); + friend Scope Deer::ScriptSystem::createScriptEnvironment(const char* baseDataType); + }; +} // namespace Deer \ No newline at end of file diff --git a/Deer/Include/DeerCore/Universe.h b/Deer/Include/DeerCore/Universe.h new file mode 100644 index 0000000..d0d62e0 --- /dev/null +++ b/Deer/Include/DeerCore/Universe.h @@ -0,0 +1,30 @@ +#pragma once +#include "DeerCore/Tools/Memory.h" + +namespace Deer { + class World; + class WorldSettings; + + namespace Universe { + struct WorldHandle { + uint32_t worldId = 0; + uint32_t generation = 0; + + WorldHandle() {} + WorldHandle(uint32_t _worldId, uint32_t _generation) : worldId(_worldId), generation(_generation) {} + }; + + struct EntityHandle { + uint32_t entityId = 0; + WorldHandle worldId; + }; + + WorldHandle createWorld(const WorldSettings&); + World& getWorld(WorldHandle); + + void destroyWorld(WorldHandle); + void destroyAllWorlds(); + + void flushDestroyedWorlds(); + } // namespace Universe +} // namespace Deer \ No newline at end of file diff --git a/Deer/Include/DeerCore/World.h b/Deer/Include/DeerCore/World.h index 387b38c..0a57493 100755 --- a/Deer/Include/DeerCore/World.h +++ b/Deer/Include/DeerCore/World.h @@ -1,30 +1,51 @@ #pragma once +#include "DeerCore/Tools/Memory.h" #include "DeerCore/Tools/TypeDefs.h" +#include +#include + namespace Deer { class EntityEnvironment; - class WorldCamera; class GizmoRenderer; + class World; - namespace World { - // Clears all the assets and memory the World had conained - void clear(); - - // This is the cycle to execution of scripts and physics - void initExecution(); - void tickExecution(); - void endExecution(); - - bool getExecutingState(); - uint32_t getCurrentExTick(); + struct WorldCamera; + using WorldCallback = std::function; + struct WorldSettings { + WorldCallback updateCallback; + u_int32_t updateFrequency; #ifdef DEER_RENDER - // This function renders with the default camera in the environment - void render(); - void render(const WorldCamera&); - - extern GizmoRenderer gizmoRenderer; + WorldCallback renderCallback; + u_int32_t renderFrequency; #endif - extern EntityEnvironment environment; - } // namespace World + }; + + enum class WorldState { + Created, + Executing, + StopRequested, + Stopped, + DestroyQueued, + ReadyToDestroy + }; + + class World { + public: + World(const WorldSettings&); + ~World(); + + void execute(); + void stopExecution(); + void destroy(); + + WorldState getExecutionState(); + Scope entityEnvironment; + + private: + std::atomic executingState; + WorldSettings worldSettings; + + }; // namespace World } // namespace Deer diff --git a/Deer/Include/DeerRender/Application.h b/Deer/Include/DeerRender/Application.h deleted file mode 100644 index 56d5107..0000000 --- a/Deer/Include/DeerRender/Application.h +++ /dev/null @@ -1,21 +0,0 @@ -#include "DeerCore/Application.h" - -#ifdef DEER_RENDER -#include "DeerRender/Events/ApplicationEvent.h" -#include "DeerRender/Events/Event.h" -#include "DeerRender/Window.h" -#endif - -namespace Deer { - namespace Application { - using EventFunction = void (*)(Event&); - - void initWindow(); - void shutdownWindow(); - - void setRenderCallback(Function); - void setEventCallback(EventFunction); - - Window& getWindow(); - } // namespace Application -} // namespace Deer \ No newline at end of file diff --git a/Deer/Include/DeerRender/DataStore.h b/Deer/Include/DeerRender/DataStore.h deleted file mode 100644 index 97b9aca..0000000 --- a/Deer/Include/DeerRender/DataStore.h +++ /dev/null @@ -1,2 +0,0 @@ -#pragma once -#include "DeerCore/DataStore.h" \ No newline at end of file diff --git a/Deer/Include/DeerRender/Engine.h b/Deer/Include/DeerRender/Engine.h new file mode 100644 index 0000000..1c34d81 --- /dev/null +++ b/Deer/Include/DeerRender/Engine.h @@ -0,0 +1,20 @@ +#pragma once +#include "DeerCore/Engine.h" +#include "DeerRender/Events/Event.h" + +namespace Deer { + using EventFunction = void (*)(Event&); + class World; + class Window; + + namespace Engine { + void setRenderCallback(Function); + void setEventCallback(EventFunction); + + void beginRender(); + void endRender(); + + World& getMainWorld(); + Window& getWindow(); + } // namespace Engine +} // namespace Deer \ No newline at end of file diff --git a/Deer/Include/DeerRender/Input.h b/Deer/Include/DeerRender/Input.h index 45e4760..7603a6e 100755 --- a/Deer/Include/DeerRender/Input.h +++ b/Deer/Include/DeerRender/Input.h @@ -1,6 +1,4 @@ #pragma once -#include "DeerCore/Application.h" - namespace Deer { class Input { public: diff --git a/Deer/Include/DeerRender/ScriptSystem.h b/Deer/Include/DeerRender/ScriptSystem.h new file mode 100644 index 0000000..00d9984 --- /dev/null +++ b/Deer/Include/DeerRender/ScriptSystem.h @@ -0,0 +1,7 @@ +#pragma once +#include "DeerCore/ScriptSystem.h" + +namespace Deer { + namespace ScriptSystem { + } +} // namespace Deer \ No newline at end of file diff --git a/Deer/Include/DeerRender/Universe.h b/Deer/Include/DeerRender/Universe.h new file mode 100644 index 0000000..5feb406 --- /dev/null +++ b/Deer/Include/DeerRender/Universe.h @@ -0,0 +1,2 @@ +#pragma once +#include "DeerCore/Universe.h" \ No newline at end of file diff --git a/Deer/src/DeerCore/Core/Application.cpp b/Deer/src/DeerCore/Core/Application.cpp deleted file mode 100644 index 70ef871..0000000 --- a/Deer/src/DeerCore/Core/Application.cpp +++ /dev/null @@ -1,59 +0,0 @@ -#include "DeerCore/Application.h" -#include -#include - -namespace Deer { - namespace Application { - // Implemented in DeerRender/Application - void runRender(float deltaTime); - void resolveEvents(); - - Function tickCallback; - bool running; - - const double targetUpdateTime = 1.0 / 60.0; // Fixed 60 FPS update - double targetRenderTime = 1.0 / 160.0; // User-defined render FPS - - void setTickCallback(Function _tick) { - tickCallback = _tick; - } - void run() { - running = true; - - auto previousTime = std::chrono::high_resolution_clock::now(); - double accumulatedUpdateTime = 0.0; - double accumulatedRenderTime = 0.0; - - while (running) { - // Time handling - auto currentTime = std::chrono::high_resolution_clock::now(); - std::chrono::duration deltaTime = currentTime - previousTime; - previousTime = currentTime; - - accumulatedUpdateTime += deltaTime.count(); - accumulatedRenderTime += deltaTime.count(); - - // Fixed Update loop (60 FPS) - while (accumulatedUpdateTime >= targetUpdateTime) { - Timestep timestep = (float)targetUpdateTime; - accumulatedUpdateTime -= targetUpdateTime; - - if (tickCallback) - tickCallback(); - } -#ifdef DEER_RENDER - if (accumulatedRenderTime >= targetRenderTime) { - runRender((float)targetRenderTime); - accumulatedRenderTime -= targetRenderTime; - } - resolveEvents(); -#endif - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } - } - - void shutdown() { - running = false; - } - } // namespace Application -} // namespace Deer \ No newline at end of file diff --git a/Deer/src/DeerCore/Core/Engine.cpp b/Deer/src/DeerCore/Core/Engine.cpp new file mode 100644 index 0000000..ddc1eb2 --- /dev/null +++ b/Deer/src/DeerCore/Core/Engine.cpp @@ -0,0 +1,52 @@ +#ifndef DEER_RENDER +#include "DeerCore/Engine.h" +#include "DeerCore/Log.h" + +namespace Deer { + namespace Engine { + Function renderCallback = nullptr; + Function updateCallback = nullptr; + } // namespace Engine + + void Engine::setUpdateCallback(Function _update) { + updateCallback = _update; + } + + void Engine::init() { + Log::init(); + } + + void Engine::shutdown() { + Log::shutdown(); + } + + void Engine::execute() { + bool running = true; + + auto previousTime = std::chrono::high_resolution_clock::now(); + double accumulatedUpdateTime = 0.0; + double accumulatedRenderTime = 0.0; + + double targetUpdateTime = 1.0f / 50.0f; + double targetRenderTime = 1.0f / 144.0f; + + while (running) { + auto currentTime = std::chrono::high_resolution_clock::now(); + std::chrono::duration deltaTime = currentTime - previousTime; + previousTime = currentTime; + + accumulatedUpdateTime += deltaTime.count(); + accumulatedRenderTime += deltaTime.count(); + + while (accumulatedUpdateTime >= targetUpdateTime) { + float timestep = targetUpdateTime; + accumulatedUpdateTime -= targetUpdateTime; + + updateCallback(); + } + + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + } +} // namespace Deer +#endif \ No newline at end of file diff --git a/Deer/src/DeerCore/DataStore/DataStore.cpp b/Deer/src/DeerCore/DataStore/DataStore.cpp deleted file mode 100755 index 51f24fa..0000000 --- a/Deer/src/DeerCore/DataStore/DataStore.cpp +++ /dev/null @@ -1,210 +0,0 @@ -#include "DeerCore/DataStore.h" -#include "DeerCore/Log.h" -#include "DeerCore/Tools/Path.h" - -#include "cereal/archives/portable_binary.hpp" -#include "cereal/cereal.hpp" -#include "cereal/types/unordered_map.hpp" - -#include "DeerCore/DataStore/DataStructure.h" -#include "DeerCore/DataStore/DataStructureSerialization.h" - -#include -#include -#include -#include -#include - -namespace Deer { - namespace DataStore { - Path rootPath = "./"; - std::unordered_map dirData_cache; - } // namespace DataStore - - const DirectoryData& DataStore::getDirData(const Path& id, - const Path& subDir, - const char* extension) { - std::string dirId = std::string(id) + "&" + std::string(subDir) + "&" + - std::string(extension); - if (dirData_cache.contains(dirId)) { - return dirData_cache[dirId]; - } - - Path idPath = rootPath / id; - - Path searchPath = idPath / subDir; - - DirectoryData& dirData = dirData_cache[dirId]; - for (const auto& entry : - std::filesystem::directory_iterator(searchPath)) { - if (entry.is_directory()) - dirData.dirs.push_back(entry.path().lexically_relative(idPath)); - else if (entry.path().extension() == extension) { - Path ent = entry.path().lexically_relative(idPath); - dirData.elements.push_back(ent.parent_path() / ent.stem()); - } - } - - return dirData; - } - - bool DataStore::loadFileData(const Path& id, const Path& name, - uint8_t** data, uint32_t* size) { - Path filePath = rootPath / id / name; - std::ifstream file(filePath, std::ios::in | std::ios::binary); - - if (!file) { - file.close(); - return false; - } - - file.seekg(0, std::ios::end); - *size = (size_t)file.tellg(); - file.seekg(0, std::ios::beg); - - *data = new uint8_t[*size]; - - if (!file.read(reinterpret_cast(*data), *size)) { - DEER_CORE_ERROR("Failed to read file: {0}", - filePath.generic_string().c_str()); - delete[] *data; - return false; - } - - file.close(); - return true; - } - - bool DataStore::loadGlobalFileData(const Path& id, const Path& name, - uint8_t** data, uint32_t* size) { - Path filePath = rootPath / id; - for (auto& f : - std::filesystem::recursive_directory_iterator(filePath)) { - if (f.path().stem() == name) { - std::ifstream file(f.path(), std::ios::in | std::ios::binary); - - if (!file) { - file.close(); - return false; - } - - file.seekg(0, std::ios::end); - *size = (size_t)file.tellg(); - file.seekg(0, std::ios::beg); - - *data = new uint8_t[*size]; - - if (!file.read(reinterpret_cast(*data), *size)) { - DEER_CORE_ERROR("Failed to read file: {0}", - filePath.generic_string().c_str()); - delete[] *data; - return false; - } - - file.close(); - return true; - } - } - - DEER_CORE_ERROR("File {0} not found", filePath.string().c_str()); - return false; - } - - void DataStore::freeFileData(uint8_t* data) { delete[] data; } - - void DataStore::deleteFile(const Path& path) { - Path filePath = rootPath / toLowerCasePath(path); - std::filesystem::remove(filePath); - } - - uint8_t* DataStore::readFile(const Path& path, uint32_t* size) { - Path filePath = rootPath / path; - std::ifstream file(filePath, std::ios::in | std::ios::binary); - - if (!file) { - file.close(); - return nullptr; - } - - file.seekg(0, std::ios::end); - *size = (size_t)file.tellg(); - file.seekg(0, std::ios::beg); - - uint8_t* buffer = new uint8_t[*size]; - - if (!file.read(reinterpret_cast(buffer), *size)) { - DEER_CORE_ERROR("Failed to read file: {0}", - filePath.generic_string().c_str()); - delete[] buffer; - return nullptr; - } - - file.close(); - return buffer; - } - - void DataStore::saveFile(const Path& path, uint8_t* data, uint32_t size) { - Path filePath = rootPath / toLowerCasePath(path); - std::filesystem::create_directories(filePath.parent_path()); - - std::ofstream file(filePath, std::ios::out | std::ios::binary); - - DEER_CORE_ASSERT(file, "Error when writing file {0}", - filePath.generic_string().c_str()); - - file.write(reinterpret_cast(data), size); - } - - void DataStore::compressFiles(std::vector files, const Path& path) { - std::unordered_map dataStructure; - std::vector combinedData; - - for (const Path& inputPath : files) { - uint32_t fileSize = 0; - uint8_t* fileData = readFile(inputPath, &fileSize); - - uint32_t start = combinedData.size(); - - combinedData.insert(combinedData.end(), fileData, - fileData + fileSize); - dataStructure[inputPath] = DataStructure{.dataPath = inputPath, - .dataStart = start, - .dataSize = fileSize}; - - delete[] fileData; - } - - Path compressedPath = path; - compressedPath += ".deer"; - Path metaPath = path; - metaPath += ".deer.meta"; - - std::stringstream buffer; - { - cereal::PortableBinaryOutputArchive archive(buffer); - archive(dataStructure); - } - - saveFile(compressedPath, combinedData.data(), combinedData.size()); - saveFile(metaPath, (uint8_t*)buffer.str().c_str(), buffer.str().size()); - } - - std::vector DataStore::getFiles(const Path& path, const std::string& extension) { - std::vector files; - Path lookPath = rootPath / path; - - for (const auto& entry : - std::filesystem::recursive_directory_iterator(lookPath)) { - if (std::filesystem::is_regular_file(entry) && - entry.path().extension() == extension) { - files.push_back(entry.path().lexically_relative(rootPath)); - } - } - - return files; - } - - void DataStore::createFolder(const Path& path) { - std::filesystem::create_directories(path); - } -} // namespace Deer diff --git a/Deer/src/DeerCore/ScriptSystem/EngineRegistry.cpp b/Deer/src/DeerCore/ScriptSystem/EngineRegistry.cpp new file mode 100644 index 0000000..4084ccc --- /dev/null +++ b/Deer/src/DeerCore/ScriptSystem/EngineRegistry.cpp @@ -0,0 +1,39 @@ +#include "DeerCore/ScriptSystem.h" +#include "DeerCore/ScriptSystem/Helpers.h" +#include "DeerCore/ScriptSystem/InternalAPI/Engine.h" +#include "angelscript.h" + +namespace Deer { + namespace ScriptSystem { + extern asIScriptEngine* scriptEngine; + + void registerEngine(); + + void registerEngineFunctions(); + void registerEngineStructs(); + } // namespace ScriptSystem + + void ScriptSystem::registerEngineFunctions() { + REGISTER_GLOBAL_FUNC(scriptEngine, "string getParent(const string&in path)", getParentPath); + REGISTER_GLOBAL_FUNC(scriptEngine, "string getParentName(const string&in path)", getParentPathName); + REGISTER_GLOBAL_FUNC(scriptEngine, "string getName(const string&in path)", getPathName); + REGISTER_GLOBAL_FUNC(scriptEngine, "array@ divide(const string&in path)", dividePath_angelscript); + } + + void ScriptSystem::registerEngineStructs() { + AS_RET_CHECK(scriptEngine->RegisterInterface("Panel")); + AS_RET_CHECK(scriptEngine->RegisterInterfaceMethod("Panel", "void render()")); + AS_RET_CHECK(scriptEngine->RegisterInterface("Service")); + + AS_CHECK(scriptEngine->RegisterFuncdef("void SimpleFunction()")); + AS_CHECK(scriptEngine->RegisterFuncdef("void ReciverFunction(any@)")); + AS_CHECK(scriptEngine->RegisterFuncdef("void TransferFunction(any@, any@)")); + } + + void ScriptSystem::registerEngine() { + registerEngineStructs(); + scriptEngine->SetDefaultNamespace("Path"); + registerEngineFunctions(); + scriptEngine->SetDefaultNamespace(""); + } +} // namespace Deer \ No newline at end of file diff --git a/Deer/src/DeerCore/ScriptSystem/Helpers.cpp b/Deer/src/DeerCore/ScriptSystem/Helpers.cpp new file mode 100644 index 0000000..18569ce --- /dev/null +++ b/Deer/src/DeerCore/ScriptSystem/Helpers.cpp @@ -0,0 +1,64 @@ +#include "DeerCore/ScriptSystem/Helpers.h" +#include "angelscript.h" + +namespace Deer { + namespace ScriptSystem { + const char* getAngelScriptReturnCodeString(int code) { + switch (code) { + case asSUCCESS: + return "SUCCESS (The operation was successful)"; + case asERROR: + return "ERROR (A generic error occurred)"; + case asCONTEXT_ACTIVE: + return "CONTEXT_ACTIVE (A context was active during an invalid operation)"; + case asCONTEXT_NOT_FINISHED: + return "CONTEXT_NOT_FINISHED (Context has not finished execution)"; + case asCONTEXT_NOT_PREPARED: + return "CONTEXT_NOT_PREPARED (Context is not prepared)"; + case asINVALID_ARG: + return "INVALID_ARG (An invalid argument was provided)"; + case asNO_FUNCTION: + return "NO_FUNCTION (Function not found)"; + case asNOT_SUPPORTED: + return "NOT_SUPPORTED (Feature not supported by build configuration)"; + case asINVALID_NAME: + return "INVALID_NAME (Name is not allowed or invalid)"; + case asNAME_TAKEN: + return "NAME_TAKEN (Name is already taken by another entity)"; + case asINVALID_DECLARATION: + return "INVALID_DECLARATION (The declaration is invalid)"; + case asINVALID_OBJECT: + return "INVALID_OBJECT (Invalid object handle or object type)"; + case asINVALID_TYPE: + return "INVALID_TYPE (Invalid type provided)"; + case asALREADY_REGISTERED: + return "ALREADY_REGISTERED (Item is already registered)"; + case asMULTIPLE_FUNCTIONS: + return "MULTIPLE_FUNCTIONS (More than one matching function found)"; + case asNO_MODULE: + return "NO_MODULE (Module was not found)"; + case asNO_GLOBAL_VAR: + return "NO_GLOBAL_VAR (Global variable was not found)"; + case asINVALID_CONFIGURATION: + return "INVALID_CONFIGURATION (Invalid configuration, likely during registration)"; + case asINVALID_INTERFACE: + return "INVALID_INTERFACE (Invalid interface registration)"; + case asCANT_BIND_ALL_FUNCTIONS: + return "CANT_BIND_ALL_FUNCTIONS (Couldn't bind all functions for a virtual interface)"; + case asLOWER_ARRAY_DIMENSION_NOT_REGISTERED: + return "LOWER_ARRAY_DIMENSION_NOT_REGISTERED (Lower dimension type for array not registered)"; + default: + return "Unknown AngelScript error code"; + } + } + + bool ImplementsInterface(asITypeInfo* type, asITypeInfo* iface) { + for (uint32_t i = 0; i < type->GetInterfaceCount(); i++) { + if (type->GetInterface(i) == iface) + return true; + } + return false; + } + + } // namespace ScriptSystem +} // namespace Deer \ No newline at end of file diff --git a/Deer/src/DeerCore/ScriptSystem/Helpers.h b/Deer/src/DeerCore/ScriptSystem/Helpers.h new file mode 100644 index 0000000..c629f68 --- /dev/null +++ b/Deer/src/DeerCore/ScriptSystem/Helpers.h @@ -0,0 +1,57 @@ +#pragma once +#include "DeerRender/Log.h" +#include "angelscript.h" + +namespace Deer { + namespace ScriptSystem { + const char* getAngelScriptReturnCodeString(int code); + bool ImplementsInterface(asITypeInfo* type, asITypeInfo* iface); + } // namespace ScriptSystem +} // namespace Deer + +#define REGISTER_GLOBAL_FUNC(scriptEngine, funcdef, func) \ + AS_CHECK(scriptEngine->RegisterGlobalFunction( \ + funcdef, \ + asFUNCTION(func), asCALL_CDECL)) + +#define REGISTER_OBJECT_METHOD(scriptEngine, clasdef, funcdef, clas, func) \ + AS_CHECK(scriptEngine->RegisterObjectMethod( \ + clasdef, funcdef, \ + asMETHOD(clas, func), asCALL_THISCALL)) + +#define REGISTER_EXT_OBJECT_METHOD(scriptEngine, clasdef, funcdef, func) \ + AS_CHECK(scriptEngine->RegisterObjectMethod( \ + clasdef, funcdef, \ + asFUNCTION(func), asCALL_CDECL_OBJLAST)) + +#define REGISTER_EXT_OBJECT_CONSTRUCTOR(scriptEngine, clasdef, funcdef, func) \ + AS_CHECK(scriptEngine->RegisterObjectBehaviour( \ + clasdef, asBEHAVE_CONSTRUCT, funcdef, \ + asFUNCTION(func), asCALL_CDECL_OBJLAST)) +#define REGISTER_EXT_OBJECT_DESTRUCTOR(scriptEngine, clasdef, funcdef, func) \ + AS_CHECK(scriptEngine->RegisterObjectBehaviour( \ + clasdef, asBEHAVE_DESTRUCT, funcdef, \ + asFUNCTION(func), asCALL_CDECL_OBJLAST)) + +#define AS_CHECK(f) \ + { \ + int __r = f; \ + if (__r < 0) { \ + DEER_EDITOR_ENGINE_ERROR("Error at line: {0}:{1} -> {2}", __FILE__, __LINE__, Deer::ScriptSystem::getAngelScriptReturnCodeString(__r)); \ + } \ + } +#define AS_CHECK_ADDITIONAL_INFO(f, i) \ + { \ + int __r = f; \ + if (__r < 0) { \ + DEER_EDITOR_ENGINE_ERROR("Error at line: {0}:{1} -> {2} \n {3}", __FILE__, __LINE__, Deer::ScriptSystem::getAngelScriptReturnCodeString(__r), i); \ + } \ + } +#define AS_RET_CHECK(f) \ + { \ + int __r = f; \ + if (__r < 0) { \ + DEER_EDITOR_ENGINE_ERROR("Error at line: {0}:{1} -> {2}", __FILE__, __LINE__, Deer::ScriptSystem::getAngelScriptReturnCodeString(__r)); \ + return; \ + } \ + } diff --git a/Deer/src/DeerCore/ScriptSystem/InternalAPI/Engine.cpp b/Deer/src/DeerCore/ScriptSystem/InternalAPI/Engine.cpp new file mode 100644 index 0000000..c8721a2 --- /dev/null +++ b/Deer/src/DeerCore/ScriptSystem/InternalAPI/Engine.cpp @@ -0,0 +1,41 @@ +#include "DeerCore/ScriptSystem/InternalAPI/Engine.h" +#include "DeerRender/Tools/Path.h" + +#include "angelscript.h" +#include "scriptarray.h" +#include "scriptstdstring.h" + +#include + +namespace Deer { + namespace ScriptSystem { + extern asIScriptEngine* scriptEngine; + + std::string getParentPath(std::string& path) { + return Path(path).parent_path().string(); + } + + std::string getParentPathName(std::string& path) { + return Path(path).parent_path().stem().string(); + } + + std::string getPathName(std::string& path) { + return Path(path).stem().string(); + } + + CScriptArray* dividePath_angelscript(std::string& path_s) { + asITypeInfo* arrayStringType = scriptEngine->GetTypeInfoByDecl("array"); + CScriptArray* array = CScriptArray::Create(arrayStringType); + + Path path_p(path_s); + for (const auto& part : path_p) { + std::string s = part.string(); + + array->Resize(array->GetSize() + 1); + array->SetValue(array->GetSize() - 1, &s); + } + + return array; + } + } // namespace ScriptSystem +} // namespace Deer \ No newline at end of file diff --git a/DeerStudio/headers/DeerStudio/StudioAPI/Engine.h b/Deer/src/DeerCore/ScriptSystem/InternalAPI/Engine.h similarity index 78% rename from DeerStudio/headers/DeerStudio/StudioAPI/Engine.h rename to Deer/src/DeerCore/ScriptSystem/InternalAPI/Engine.h index fc8de7d..81cd083 100644 --- a/DeerStudio/headers/DeerStudio/StudioAPI/Engine.h +++ b/Deer/src/DeerCore/ScriptSystem/InternalAPI/Engine.h @@ -4,11 +4,10 @@ class CScriptArray; namespace Deer { - namespace StudioAPI { - // ANGELSCRIPT SPECIFIC + namespace ScriptSystem { CScriptArray* dividePath_angelscript(std::string&); std::string getParentPath(std::string&); std::string getParentPathName(std::string&); std::string getPathName(std::string&); - } // namespace StudioAPI + } // namespace ScriptSystem } // namespace Deer \ No newline at end of file diff --git a/Deer/src/DeerCore/ScriptSystem/InternalAPI/Entity.h b/Deer/src/DeerCore/ScriptSystem/InternalAPI/Entity.h new file mode 100644 index 0000000..672b0c4 --- /dev/null +++ b/Deer/src/DeerCore/ScriptSystem/InternalAPI/Entity.h @@ -0,0 +1,91 @@ +#pragma once +#include "DeerCore/Universe.h" + +#include "DeerRender/Mesh.h" +#include "DeerRender/Shader.h" +#include "DeerRender/Texture.h" + +#include "glm/glm.hpp" +#include +#include + +class asIScriptGeneric; + +namespace Deer { + namespace StudioAPI { + Universe::EntityHandle entity_getSelf(Universe::EntityHandle); + std::string entity_getName(Universe::EntityHandle); + void entity_getName(std::string, Universe::EntityHandle); + bool entity_exists(Universe::EntityHandle); + bool entity_isRoot(Universe::EntityHandle); + void entity_destroy(Universe::EntityHandle); + + Universe::EntityHandle entity_createChild(Universe::EntityHandle); + void entity_setParent(Universe::EntityHandle); + Universe::EntityHandle entity_getParent(Universe::EntityHandle); + + bool entity_isDescendantOf(Universe::EntityHandle); + bool entity_opEquals(Universe::EntityHandle& other, Universe::EntityHandle); + bool entity_isDescendantOf(Universe::EntityHandle parent); + bool entity_opEquals(const Universe::EntityHandle& other); + + void entity_addGenericComponent(asIScriptGeneric* generic); + void entity_removeGenericComponent(asIScriptGeneric* generic); + void entity_getGenericComponent(asIScriptGeneric* generic); + void entity_hasGenericComponent(asIScriptGeneric* generic); + + struct TransformComponentStruct : Universe::EntityHandle { + glm::vec3 getPosition(); + glm::vec3 getScale(); + glm::vec3 getRotation(); + + void setPosition(glm::vec3); + void setScale(glm::vec3); + void setRotation(glm::vec3); + }; + + struct MeshComponentStruct : Universe::EntityHandle { + bool isActive(); + void setActive(bool); + + bool hasMesh(); + void clear(); + + Resource getMesh(); + void setMesh(Resource); + + bool assertMeshComponent(const char* funcName); + }; + + struct ShaderComponentStruct : Universe::EntityHandle { + bool hasShader(); + void clear(); + + Resource getShader(); + void setShader(Resource); + + Resource getTexture(); + void setTexture(Resource); + + bool assertShaderComponent(const char* funcName); + }; + + struct CameraComponentStruct : Universe::EntityHandle { + float getFov(); + float getAspectRation(); + float getNearZ(); + float getFarZ(); + + void setFov(float); + void setAspectRation(float); + void setNearZ(float); + void setFarZ(float); + + bool assertCameraComponent(const char* funcName); + }; + + Universe::EntityHandle getRoot(); + void constructEntityStruct(int id, void* memory); + void copyEntityStruct(int id, void* memory); + } // namespace StudioAPI +} // namespace Deer \ No newline at end of file diff --git a/Deer/src/DeerCore/ScriptSystem/InternalAPI/InternalFunctions.cpp b/Deer/src/DeerCore/ScriptSystem/InternalAPI/InternalFunctions.cpp new file mode 100644 index 0000000..5df1f5c --- /dev/null +++ b/Deer/src/DeerCore/ScriptSystem/InternalAPI/InternalFunctions.cpp @@ -0,0 +1,21 @@ +#include "DeerCore/ScriptSystem/InternalAPI/InternalFunctions.h" +#include "DeerRender/Log.h" +#include "angelscript.h" + +namespace Deer { + void ScriptSystem::errorCallback_angelscript(const asSMessageInfo* msg, void* param) { + if (msg->type == asMSGTYPE_WARNING) { + DEER_EDITOR_ENGINE_WARN("{0}:{1}:{2}) : {3}", msg->section, msg->row, msg->col, msg->message); + } else if (msg->type == asMSGTYPE_INFORMATION) { + DEER_EDITOR_ENGINE_INFO("{0}:{1}:{2}) : {3}", msg->section, msg->row, msg->col, msg->message); + } else if (msg->type == asMSGTYPE_ERROR) { + DEER_EDITOR_ENGINE_ERROR("{0}:{1}:{2}) : {3}", msg->section, msg->row, msg->col, msg->message); + } else { + DEER_EDITOR_ENGINE_INFO("{0}:{1}:{2}) : {3}", msg->section, msg->row, msg->col, msg->message); + } + } + + void ScriptSystem::print(std::string& msg) { + DEER_EDITOR_ENGINE_INFO("{0}", msg.c_str()); + } +} // namespace Deer \ No newline at end of file diff --git a/DeerStudio/headers/DeerStudio/StudioAPI/Debug.h b/Deer/src/DeerCore/ScriptSystem/InternalAPI/InternalFunctions.h similarity index 63% rename from DeerStudio/headers/DeerStudio/StudioAPI/Debug.h rename to Deer/src/DeerCore/ScriptSystem/InternalAPI/InternalFunctions.h index 0b12b73..43b2bf6 100644 --- a/DeerStudio/headers/DeerStudio/StudioAPI/Debug.h +++ b/Deer/src/DeerCore/ScriptSystem/InternalAPI/InternalFunctions.h @@ -1,11 +1,9 @@ #pragma once -#include "string" - -class asSMessageInfo; +struct asSMessageInfo; namespace Deer { - namespace StudioAPI { + namespace ScriptSystem { void errorCallback_angelscript(const asSMessageInfo* msg, void* param); void print(std::string& msg); - } // namespace StudioAPI + } // namespace ScriptSystem } // namespace Deer \ No newline at end of file diff --git a/Deer/src/DeerCore/ScriptSystem/InternalAPI/Math.cpp b/Deer/src/DeerCore/ScriptSystem/InternalAPI/Math.cpp new file mode 100644 index 0000000..ab6bb3a --- /dev/null +++ b/Deer/src/DeerCore/ScriptSystem/InternalAPI/Math.cpp @@ -0,0 +1,66 @@ +#include "DeerCore/ScriptSystem/InternalAPI/Math.h" +#include "glm/glm.hpp" + +namespace Deer { + namespace ScriptSystem { + void vec3_constructor_params(float x, float y, float z, void* mem) { + new (mem) glm::vec3(x, y, z); + } + + glm::vec3 vec3_add(glm::vec3& value, glm::vec3& self) { + return self + value; + } + + glm::vec3 vec3_sub(const glm::vec3& value, glm::vec3& self) { + return self - value; + } + + glm::vec3 vec3_neg(glm::vec3& self) { + return -self; + } + + glm::vec3 vec3_mult(float value, glm::vec3& self) { + return self * value; + } + + void quat_construct(glm::quat* mem) { + new (mem) glm::quat(); + } + + void quat_copyConstruct(glm::quat* data, glm::quat* mem) { + new (mem) glm::quat(*data); + } + + void quat_constructFromValue(float x, float y, float z, float w, glm::quat* mem) { + new (mem) glm::quat(x, y, z, w); + } + + glm::vec3 quat_getEuler(glm::quat* mem) { + return glm::degrees(glm::eulerAngles(*mem)); + } + + void quat_setEuler(glm::vec3 euler, glm::quat* mem) { + new (mem) glm::quat(glm::radians(euler)); + } + + glm::quat quat_multiply(glm::quat* data, glm::quat* mem) { + return *mem * *data; + } + + glm::vec3 transform_relative(glm::vec3 pos, TransformComponent* transform) { + return transform->getMatrix() * glm::vec4(pos, 1.0f); + } + + void transform_construct(TransformComponent* mem) { + new (mem) TransformComponent(); + } + + void camera_construct(CameraComponent* mem) { + new (mem) CameraComponent(); + } + + void sceneCamera_Construct(WorldCamera* mem) { + new (mem) WorldCamera(); + } + } // namespace ScriptSystem +} // namespace Deer \ No newline at end of file diff --git a/DeerStudio/headers/DeerStudio/StudioAPI/Math.h b/Deer/src/DeerCore/ScriptSystem/InternalAPI/Math.h similarity index 92% rename from DeerStudio/headers/DeerStudio/StudioAPI/Math.h rename to Deer/src/DeerCore/ScriptSystem/InternalAPI/Math.h index 3efedf0..3e4bcf4 100644 --- a/DeerStudio/headers/DeerStudio/StudioAPI/Math.h +++ b/Deer/src/DeerCore/ScriptSystem/InternalAPI/Math.h @@ -6,7 +6,7 @@ #include "DeerRender/World.h" namespace Deer { - namespace StudioAPI { + namespace ScriptSystem { void vec3_constructor(void*); void vec3_constructor_params(float, float, float, void*); @@ -17,7 +17,6 @@ namespace Deer { void quat_construct(glm::quat*); void quat_copyConstruct(glm::quat*, glm::quat*); - void quat_destruct(glm::quat*); void quat_constructFromValue(float, float, float, float, glm::quat*); glm::vec3 quat_getEuler(glm::quat*); @@ -31,5 +30,5 @@ namespace Deer { glm::vec3 transform_relative(glm::vec3, TransformComponent*); void emptyDestructor(); - } // namespace StudioAPI + } // namespace ScriptSystem } // namespace Deer \ No newline at end of file diff --git a/Deer/src/DeerCore/ScriptSystem/InternalAPI/World.h b/Deer/src/DeerCore/ScriptSystem/InternalAPI/World.h new file mode 100644 index 0000000..ccb8bd9 --- /dev/null +++ b/Deer/src/DeerCore/ScriptSystem/InternalAPI/World.h @@ -0,0 +1,8 @@ +#pragma once +#include "DeerCore/Universe.h" + +namespace Deer { + namespace ScriptSystem { + Universe::WorldHandle getExecutingWorld(); + } +} // namespace Deer \ No newline at end of file diff --git a/Deer/src/DeerCore/ScriptSystem/MathRegistry.cpp b/Deer/src/DeerCore/ScriptSystem/MathRegistry.cpp new file mode 100644 index 0000000..1db2cd2 --- /dev/null +++ b/Deer/src/DeerCore/ScriptSystem/MathRegistry.cpp @@ -0,0 +1,68 @@ +#include "DeerCore/ScriptSystem.h" +#include "DeerCore/ScriptSystem/Helpers.h" +#include "DeerCore/ScriptSystem/InternalAPI/Math.h" +#include "angelscript.h" + +namespace Deer { + namespace ScriptSystem { + extern asIScriptEngine* scriptEngine; + + void registerMath(); + void registerMathStructs(); + void registerMathFunctions(); + } // namespace ScriptSystem + + void ScriptSystem::registerMathStructs() { + scriptEngine->RegisterObjectType("quat", sizeof(glm::vec3), asOBJ_VALUE | asOBJ_POD | asGetTypeTraits() | asOBJ_APP_CLASS_ALLFLOATS); + scriptEngine->RegisterObjectProperty("vec3", "float x", asOFFSET(glm::vec3, x)); + scriptEngine->RegisterObjectProperty("vec3", "float y", asOFFSET(glm::vec3, y)); + scriptEngine->RegisterObjectProperty("vec3", "float z", asOFFSET(glm::vec3, z)); + REGISTER_EXT_OBJECT_CONSTRUCTOR(scriptEngine, "vec3", "void f()", vec3_constructor); + REGISTER_EXT_OBJECT_CONSTRUCTOR(scriptEngine, "vec3", "void f(float, float = 0, float = 0)", vec3_constructor_params); + + scriptEngine->RegisterObjectType("quat", sizeof(glm::quat), asOBJ_VALUE | asOBJ_POD | asGetTypeTraits() | asOBJ_APP_CLASS_ALLFLOATS); + scriptEngine->RegisterObjectProperty("quat", "float x", asOFFSET(glm::quat, x)); + scriptEngine->RegisterObjectProperty("quat", "float y", asOFFSET(glm::quat, y)); + scriptEngine->RegisterObjectProperty("quat", "float z", asOFFSET(glm::quat, z)); + scriptEngine->RegisterObjectProperty("quat", "float w", asOFFSET(glm::quat, w)); + REGISTER_EXT_OBJECT_CONSTRUCTOR(scriptEngine, "quat", "void f()", quat_construct); + REGISTER_EXT_OBJECT_CONSTRUCTOR(scriptEngine, "quat", "void f(float, float, float, float)", quat_constructFromValue); + + scriptEngine->RegisterObjectType("Transform", sizeof(TransformComponent), asOBJ_VALUE | asOBJ_POD | asGetTypeTraits()); + scriptEngine->RegisterObjectProperty("Transform", "vec3 position", asOFFSET(TransformComponent, position)); + scriptEngine->RegisterObjectProperty("Transform", "vec3 scale", asOFFSET(TransformComponent, scale)); + scriptEngine->RegisterObjectProperty("Transform", "quat rotation", asOFFSET(TransformComponent, rotation)); + REGISTER_EXT_OBJECT_CONSTRUCTOR(scriptEngine, "Transform", "void f()", transform_construct); + + scriptEngine->RegisterObjectType("Camera", sizeof(CameraComponent), asOBJ_VALUE | asOBJ_POD | asGetTypeTraits()); + scriptEngine->RegisterObjectProperty("Camera", "float fov", asOFFSET(CameraComponent, fov)); + scriptEngine->RegisterObjectProperty("Camera", "float aspect", asOFFSET(CameraComponent, aspect)); + scriptEngine->RegisterObjectProperty("Camera", "float nearZ", asOFFSET(CameraComponent, nearZ)); + scriptEngine->RegisterObjectProperty("Camera", "float farZ", asOFFSET(CameraComponent, farZ)); + REGISTER_EXT_OBJECT_CONSTRUCTOR(scriptEngine, "Camera", "void f()", camera_construct); + + scriptEngine->RegisterObjectType("WorldCamera", sizeof(WorldCamera), asOBJ_VALUE | asOBJ_POD | asGetTypeTraits()); + scriptEngine->RegisterObjectProperty("WorldCamera", "Camera camera", asOFFSET(WorldCamera, camera)); + scriptEngine->RegisterObjectProperty("WorldCamera", "Transform transform", asOFFSET(WorldCamera, transform)); + REGISTER_EXT_OBJECT_CONSTRUCTOR(scriptEngine, "WorldCamera", "void f()", sceneCamera_Construct); + } + + void ScriptSystem::registerMathFunctions() { + REGISTER_EXT_OBJECT_METHOD(scriptEngine, "vec3", "vec3 opAdd(const vec3 &in)", vec3_add); + REGISTER_EXT_OBJECT_METHOD(scriptEngine, "vec3", "vec3 opSub(const vec3 &in) const", vec3_sub); + REGISTER_EXT_OBJECT_METHOD(scriptEngine, "vec3", "vec3 opNeg() const", vec3_neg); + REGISTER_EXT_OBJECT_METHOD(scriptEngine, "vec3", "vec3 opMul(float) const", vec3_mult); + REGISTER_EXT_OBJECT_METHOD(scriptEngine, "vec3", "vec3 opMul_r(float) const", vec3_mult); + + REGISTER_EXT_OBJECT_METHOD(scriptEngine, "quat", "quat opMul(const quat &in) const", quat_multiply); + REGISTER_EXT_OBJECT_METHOD(scriptEngine, "quat", "void setEuler(vec3)", quat_setEuler); + REGISTER_EXT_OBJECT_METHOD(scriptEngine, "quat", "vec3 getEuler() const", quat_getEuler); + + REGISTER_EXT_OBJECT_METHOD(scriptEngine, "Transform", "vec3 relative(vec3)", transform_relative); + } + + void ScriptSystem::registerMath() { + registerMathStructs(); + registerMathFunctions(); + } +} // namespace Deer \ No newline at end of file diff --git a/Deer/src/DeerCore/ScriptSystem/Registering.cpp b/Deer/src/DeerCore/ScriptSystem/Registering.cpp new file mode 100644 index 0000000..514add2 --- /dev/null +++ b/Deer/src/DeerCore/ScriptSystem/Registering.cpp @@ -0,0 +1,19 @@ +#include "DeerCore/Log.h" +#include "DeerCore/ScriptSystem.h" +#include "DeerCore/Tools/Memory.h" +#include "angelscript.h" + +#include +#include + +namespace Deer { + namespace ScriptSystem { + extern asIScriptEngine* scriptEngine; + + } // namespace ScriptSystem + + asIScriptEngine* ScriptSystem::getScriptEngine() { + return scriptEngine; + } + +} // namespace Deer \ No newline at end of file diff --git a/Deer/src/DeerCore/ScriptSystem/ScriptEnvironment.cpp b/Deer/src/DeerCore/ScriptSystem/ScriptEnvironment.cpp new file mode 100644 index 0000000..3b66be2 --- /dev/null +++ b/Deer/src/DeerCore/ScriptSystem/ScriptEnvironment.cpp @@ -0,0 +1,33 @@ +#pragma once +#include "DeerCore/Log.h" +#include "DeerCore/ScriptSystem.h" +#include "angelscript.h" + +namespace Deer { + namespace ScriptSystem { + extern asIScriptEngine* scriptEngine; + } + + Scope ScriptSystem::createScriptEnvironment(const char* baseTypeName) { + asITypeInfo* baseType = scriptEngine->GetTypeInfoByDecl(baseTypeName); + if (!baseType) { + DEER_CORE_ERROR("Base type {} not found", baseTypeName); + return nullptr; + } + + if (baseType->GetModule()) { + DEER_CORE_ERROR("Base type {} must be a engine type", baseTypeName); + return nullptr; + } + + Scope environment = MakeScope(baseType); + + return environment; + } + + ScriptEnvironment::ScriptEnvironment(asITypeInfo* _baseType) + : baseType(_baseType), baseTypeName(_baseType->GetName()) { + + ScriptSystem::scriptEngine->GetTypedefCount(); + } +} // namespace Deer \ No newline at end of file diff --git a/Deer/src/DeerCore/ScriptSystem/ScriptSystem.cpp b/Deer/src/DeerCore/ScriptSystem/ScriptSystem.cpp new file mode 100644 index 0000000..002fbef --- /dev/null +++ b/Deer/src/DeerCore/ScriptSystem/ScriptSystem.cpp @@ -0,0 +1,50 @@ +#include "DeerCore/ScriptSystem.h" +#include "DeerCore/ScriptSystem/Helpers.h" +#include "DeerCore/ScriptSystem/InternalAPI/InternalFunctions.h" + +#include "angelscript.h" +#include "scriptany.h" +#include "scriptarray.h" +#include "scriptbuilder.h" +#include "scriptdictionary.h" +#include "scripthandle.h" +#include "scriptstdstring.h" + +#include +#include + +namespace Deer { + namespace ScriptSystem { + asIScriptEngine* scriptEngine; + + void registerMath(); + void registerEngine(); +#ifdef DEER_RENDER + void registerResources(); +#endif + }; // namespace ScriptSystem + + void ScriptSystem::init() { + asPrepareMultithread(); + + scriptEngine = asCreateScriptEngine(); + AS_CHECK(scriptEngine->SetMessageCallback(asFUNCTION(Deer::ScriptSystem::errorCallback_angelscript), 0, asCALL_CDECL)); + REGISTER_GLOBAL_FUNC(scriptEngine, "void print(const string&in text)", print); + + RegisterScriptHandle(scriptEngine); + RegisterScriptAny(scriptEngine); + RegisterStdString(scriptEngine); + RegisterScriptArray(scriptEngine, true); + RegisterScriptDictionary(scriptEngine); + + registerMath(); + registerEngine(); +#ifdef DEER_RENDER + registerResources(); +#endif + } + + void ScriptSystem::shutdown() { + asUnprepareMultithread(); + } +} // namespace Deer \ No newline at end of file diff --git a/Deer/src/DeerCore/Universe/Universe.cpp b/Deer/src/DeerCore/Universe/Universe.cpp new file mode 100644 index 0000000..9006994 --- /dev/null +++ b/Deer/src/DeerCore/Universe/Universe.cpp @@ -0,0 +1,90 @@ +#include "DeerCore/Universe.h" +#include "DeerCore/Log.h" +#include "DeerCore/Tools/Memory.h" +#include "DeerCore/World.h" + +#include + +#include + +namespace Deer { + namespace Universe { + struct WorldSlot { + Scope world = nullptr; + uint32_t generation = 1; + }; + + std::vector universeWorldSlots; + std::mutex universeMutex; + + int getFreeWorldSlot(); + void unlockedFlushDestroyedWorlds(); + } // namespace Universe + + int Universe::getFreeWorldSlot() { + for (int i = 0; i < universeWorldSlots.size(); i++) { + if (universeWorldSlots[i].world == nullptr) + return i; + } + return -1; + } + + Universe::WorldHandle Universe::createWorld(const WorldSettings& worldSettings) { + std::lock_guard lock(universeMutex); + unlockedFlushDestroyedWorlds(); + + int universeSlot = getFreeWorldSlot(); + if (universeSlot == -1) { + universeSlot = universeWorldSlots.size(); + universeWorldSlots.push_back({}); + } + + WorldSlot& worldSlot = universeWorldSlots[universeSlot]; + worldSlot.world = MakeScope(worldSettings); + + return WorldHandle(universeSlot, worldSlot.generation); + } + + World& Universe::getWorld(WorldHandle handle) { + std::lock_guard lock(universeMutex); + + DEER_CORE_ASSERT(handle.worldId < universeWorldSlots.size(), "Invalid world handle"); + DEER_CORE_ASSERT(handle.generation == universeWorldSlots[handle.worldId].generation, "Invalid world generation"); + DEER_CORE_ASSERT(universeWorldSlots[handle.worldId].world, "World no longer exists"); + + return *universeWorldSlots[handle.worldId].world; + } + + void Universe::destroyWorld(WorldHandle handle) { + std::lock_guard lock(universeMutex); + + DEER_CORE_ASSERT(handle.worldId < universeWorldSlots.size(), "Invalid world handle"); + DEER_CORE_ASSERT(handle.generation == universeWorldSlots[handle.worldId].generation, "Invalid world generation"); + DEER_CORE_ASSERT(universeWorldSlots[handle.worldId].world, "World no longer exists"); + + universeWorldSlots[handle.worldId].world->destroy(); + unlockedFlushDestroyedWorlds(); + } + + void Universe::destroyAllWorlds() { + std::lock_guard lock(universeMutex); + for (WorldSlot& slot : universeWorldSlots) + slot.world->destroy(); + unlockedFlushDestroyedWorlds(); + } + + void Universe::unlockedFlushDestroyedWorlds() { + for (WorldSlot& slot : universeWorldSlots) { + if (slot.world != nullptr && slot.world->getExecutionState() == WorldState::ReadyToDestroy) { + slot.world.release(); + slot.world = nullptr; + slot.generation++; + } + } + } + + void Universe::flushDestroyedWorlds() { + std::lock_guard lock(universeMutex); + unlockedFlushDestroyedWorlds(); + } +} // namespace Deer \ No newline at end of file diff --git a/Deer/src/DeerCore/World/Enviroment.cpp b/Deer/src/DeerCore/World/Enviroment.cpp index 6bf6d08..486f4d1 100755 --- a/Deer/src/DeerCore/World/Enviroment.cpp +++ b/Deer/src/DeerCore/World/Enviroment.cpp @@ -1,6 +1,5 @@ #include "DeerCore/EntityEnviroment.h" -#include "DeerCore/Application.h" #include "DeerCore/Components.h" #include "DeerCore/Log.h" #include "DeerRender/Render/Render.h" @@ -26,16 +25,16 @@ namespace Deer { createEntityWithId(0, "root"); } - uint16_t EntityEnvironment::getEntityCount() const { + uint32_t EntityEnvironment::getEntityCount() const { return entities.size() - unused_entities_spaces.size(); } - Entity& EntityEnvironment::getEntity(uint16_t id) { + Entity& EntityEnvironment::getEntity(uint32_t id) { DEER_CORE_ASSERT(entityExists(id), "Entity id {0} does not exist", id); return entities[id]; } - bool EntityEnvironment::entityExists(uint16_t id) const { + bool EntityEnvironment::entityExists(uint32_t id) const { if (id >= entities.size()) return false; const Entity& refEntity = entities[id]; @@ -67,7 +66,7 @@ namespace Deer { return entity; } - Entity& EntityEnvironment::createEntityWithId(uint16_t id, const std::string& name) { + Entity& EntityEnvironment::createEntityWithId(uint32_t id, const std::string& name) { entt::entity entityID = m_registry->create(); // We allocate all the memory until that id @@ -118,7 +117,7 @@ namespace Deer { return entity; } - void EntityEnvironment::destroyEntity(uint16_t entityID) { + void EntityEnvironment::destroyEntity(uint32_t entityID) { DEER_CORE_ASSERT(entityExists(entityID), "Entity id {0} does not exist", entityID); DEER_CORE_ASSERT(entityID != 0, "Cannot destroy root"); @@ -128,7 +127,7 @@ namespace Deer { RelationshipComponent& relationship = entity.getComponent(); // We want to delete all childrens for (int i = 0; i < relationship.getChildCount(); i++) { - uint16_t childID = relationship.getChildId(i); + uint32_t childID = relationship.getChildId(i); destroyEntity(childID); } diff --git a/Deer/src/DeerCore/World/Serialization/EnvironmentSerialization.h b/Deer/src/DeerCore/World/Serialization/EnvironmentSerialization.h index a0415f2..4cac0b5 100755 --- a/Deer/src/DeerCore/World/Serialization/EnvironmentSerialization.h +++ b/Deer/src/DeerCore/World/Serialization/EnvironmentSerialization.h @@ -26,10 +26,10 @@ namespace Deer { template void save(Archive& archive, EntityEnvironmentEntity const& m_entities) { archive(cereal::make_size_tag(static_cast( - m_entities.environment.getEntityCount()))); + m_entities.entityEnvironment->getEntityCount()))); - for (uint16_t i = 0; i < m_entities.environment.getEntityCount(); i++) { - while (!m_entities.environment.entityExists(i)) { + for (uint16_t i = 0; i < m_entities.entityEnvironment->getEntityCount(); i++) { + while (!m_entities.entityEnvironment->entityExists(i)) { i++; } diff --git a/Deer/src/DeerCore/World/World.cpp b/Deer/src/DeerCore/World/World.cpp index f8d617f..f95a268 100755 --- a/Deer/src/DeerCore/World/World.cpp +++ b/Deer/src/DeerCore/World/World.cpp @@ -4,7 +4,6 @@ #include "DeerCore/EntityEnviroment.h" #include "DeerCore/Log.h" #include "DeerCore/Tools/Memory.h" -#include "DeerCore/World/WorldData.h" #ifdef DEER_RENDER #include "DeerRender/FrameBuffer.h" @@ -13,32 +12,31 @@ #endif namespace Deer { - void World::clear() { - environment.clear(); + World::World(const WorldSettings& settings) : worldSettings(settings) { + entityEnvironment = MakeScope(); + DEER_CORE_ASSERT(worldSettings.updateFrequency <= 120, "Invalid world update frequency"); -#ifdef DEER_RENDER - ResourceManager::unloadResources(); - ResourceManager::unloadResources(); - ResourceManager::unloadResources(); - ResourceManager::unloadResources(); -#endif + executingState = WorldState::Stopped; } - bool World::getExecutingState() { - return isExecuting; + World::~World() { + DEER_CORE_ASSERT(executingState == WorldState::Stopped, "Invalid executing state while destroying world"); } - void World::initExecution() { - DEER_CORE_ASSERT(!isExecuting, "Deer scene is already executing"); - isExecuting = true; + void World::stopExecution() { + if (executingState == WorldState::Executing) + executingState = WorldState::StopRequested; } - void World::tickExecution() { + void World::destroy() { + if (executingState == WorldState::Executing || executingState == WorldState::StopRequested) + executingState = WorldState::DestroyQueued; + else + executingState = WorldState::ReadyToDestroy; } - void World::endExecution() { - DEER_CORE_ASSERT(isExecuting, "Deer scene is not executing"); - isExecuting = false; + WorldState World::getExecutionState() { + return executingState; } } // namespace Deer diff --git a/Deer/src/DeerCore/World/WorldData.cpp b/Deer/src/DeerCore/World/WorldData.cpp deleted file mode 100644 index 2582e49..0000000 --- a/Deer/src/DeerCore/World/WorldData.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include "DeerCore/EntityEnviroment.h" -#include "DeerCore/Tools/Memory.h" - -namespace Deer { - namespace World { - EntityEnvironment environment; - bool isExecuting = false; - } // namespace World - -} // namespace Deer diff --git a/Deer/src/DeerCore/World/WorldData.h b/Deer/src/DeerCore/World/WorldData.h deleted file mode 100644 index 523b940..0000000 --- a/Deer/src/DeerCore/World/WorldData.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once -#include "DeerCore/Tools/Memory.h" - -namespace Deer { - class EntityEnvironment; - - namespace World { - extern EntityEnvironment environment; - extern bool isExecuting; - } // namespace World - -} // namespace Deer diff --git a/Deer/src/DeerRender/Core/Application.cpp b/Deer/src/DeerRender/Core/Application.cpp deleted file mode 100644 index 6c1685d..0000000 --- a/Deer/src/DeerRender/Core/Application.cpp +++ /dev/null @@ -1,80 +0,0 @@ -#include "DeerRender/Application.h" -#include -#include - -#include "DeerRender/Render/Render.h" -#include "DeerRender/Render/RenderCommand.h" - -#include "DeerRender/Log.h" - -#include "DeerRender/ImGui/ImGuiLayer.h" -#include "imgui.h" - -namespace Deer { - namespace Application { - Function renderCallback; - EventFunction eventCallback; - Scope window; - Scope imGuiLayer; - - void setRenderCallback(Function _render) { - renderCallback = _render; - } - - void setEventCallback(EventFunction _event) { - eventCallback = _event; - } - - Window& getWindow() { - return *window; - } - - void internalEventCallback(Event& e) { - if (eventCallback) - eventCallback(e); - - imGuiLayer->onEvent(e); - } - - void initWindow() { - window = Scope(Window::create()); - - window->setEventCallback(Deer::Application::internalEventCallback); - window->initWindow(); - - imGuiLayer = MakeScope(); - imGuiLayer->onAttach(); - - RenderCommand::init(); - } - - void resolveEvents() { - window->resolveEvents(); - } - - void shutdownWindow() { - imGuiLayer->onDetach(); - - imGuiLayer.reset(); - window.reset(); - } - - void runRender(float deltaTime) { - if (!renderCallback) - return; - - RenderCommand::setClearColor({0.2f, 0.2f, 0.3f, 1.0f}); - RenderCommand::clear(); - - ImGuiIO& io = ImGui::GetIO(); - io.DeltaTime = (float)deltaTime; - imGuiLayer->begin(); - - renderCallback(); - - imGuiLayer->end(); - window->onRender(); - } - - } // namespace Application -} // namespace Deer \ No newline at end of file diff --git a/Deer/src/DeerRender/Core/Engine.cpp b/Deer/src/DeerRender/Core/Engine.cpp new file mode 100644 index 0000000..aa23fc4 --- /dev/null +++ b/Deer/src/DeerRender/Core/Engine.cpp @@ -0,0 +1,107 @@ +#ifdef DEER_RENDER +#include "DeerRender/Engine.h" +#include "DeerRender/Log.h" + +#include "DeerRender/ImGui/ImGuiLayer.h" +#include "DeerRender/ScriptSystem.h" +#include "DeerRender/Universe.h" +#include "DeerRender/Window.h" +#include "DeerRender/World.h" +#include "imgui.h" + +#include "DeerRender/Render/RenderCommand.h" +#include "DeerRender/Render/RenderUtils.h" + +#include "DeerRender/FrameBuffer.h" +#include "DeerRender/Mesh.h" +#include "DeerRender/Resource.h" +#include "DeerRender/Shader.h" +#include "DeerRender/Texture.h" + +namespace Deer { + namespace Engine { + Function renderCallback = nullptr; + Function updateCallback = nullptr; + EventFunction eventCallback; + + Scope window = nullptr; + Scope imGuiLayer = nullptr; + bool running = true; + + void internalEventCallback(Event& e); + void runRender(float); + } // namespace Engine + + Window& Engine::getWindow() { + return *window; + } + + void Engine::setEventCallback(EventFunction _event) { + eventCallback = _event; + } + + void Engine::setUpdateCallback(Function _update) { + updateCallback = _update; + } + + void Engine::setRenderCallback(Function _render) { + renderCallback = _render; + } + + void Engine::internalEventCallback(Event& e) { + if (eventCallback) + eventCallback(e); + + imGuiLayer->onEvent(e); + } + + void Engine::init() { + Log::init(); + ScriptSystem::init(); + + imGuiLayer = MakeScope(); + + window = Scope(Window::create()); + window->setEventCallback(Engine::internalEventCallback); + window->initWindow(); + + RenderCommand::init(); + RenderUtils::initializeRenderUtils(); + + imGuiLayer = MakeScope(); + imGuiLayer->onAttach(*window); + } + + void Engine::shutdown() { + Universe::destroyAllWorlds(); + + ResourceManager::unloadResources(); + ResourceManager::unloadResources(); + ResourceManager::unloadResources(); + ResourceManager::unloadResources(); + + imGuiLayer->onDetach(); + imGuiLayer.reset(); + window.reset(); + + ScriptSystem::shutdown(); + RenderUtils::deinitializeRenderUtils(); + Log::shutdown(); + } + + void Engine::beginRender() { + RenderCommand::setClearColor({0.2f, 0.2f, 0.3f, 1.0f}); + RenderCommand::clear(); + + ImGuiIO& io = ImGui::GetIO(); + io.DeltaTime = 1.0f / 60; + imGuiLayer->begin(); + } + + void Engine::endRender() { + imGuiLayer->end(); + window->onRender(); + window->resolveEvents(); + } +} // namespace Deer +#endif \ No newline at end of file diff --git a/Deer/src/DeerRender/Core/Input/Input.cpp b/Deer/src/DeerRender/Core/Input.cpp similarity index 66% rename from Deer/src/DeerRender/Core/Input/Input.cpp rename to Deer/src/DeerRender/Core/Input.cpp index 6cfd962..79a3a29 100755 --- a/Deer/src/DeerRender/Core/Input/Input.cpp +++ b/Deer/src/DeerRender/Core/Input.cpp @@ -1,18 +1,19 @@ #include "DeerRender/Input.h" -#include "DeerRender/Application.h" +#include "DeerRender/Engine.h" +#include "DeerRender/Window.h" namespace Deer { #ifdef DEER_RENDER bool Input::isKeyPressed(unsigned int key) { - return Application::getWindow().getKeyPressed(key); + return Engine::getWindow().getKeyPressed(key); } bool Input::isMouseButtonPressed(int button) { - return Application::getWindow().getMouseButton(button); + return Engine::getWindow().getMouseButton(button); } void Input::getMousePos(float& x, float& y) { - return Application::getWindow().getMousePos(x, y); + return Engine::getWindow().getMousePos(x, y); } #else bool Input::isKeyPressed(unsigned int key) { diff --git a/Deer/src/DeerRender/ImGui/ImGuiLayer.h b/Deer/src/DeerRender/ImGui/ImGuiLayer.h index d22f215..f1abbef 100755 --- a/Deer/src/DeerRender/ImGui/ImGuiLayer.h +++ b/Deer/src/DeerRender/ImGui/ImGuiLayer.h @@ -4,18 +4,21 @@ #include "DeerRender/Events/MouseEvent.h" namespace Deer { + class Window; + class ImGuiLayer { - public: + public: ~ImGuiLayer() = default; - void onAttach(); + void onAttach(Window& window); void onDetach(); void begin(); void end(); void onEvent(Event& event); - private: + + private: bool onMouseButtonPressedEvent(MouseButtonPressedEvent& e); bool onMouseButtonReleasedEvent(MouseButtonReleasedEvent& e); bool onMouseMovedEvent(MouseMovedEvent& e); @@ -25,5 +28,4 @@ namespace Deer { bool onKeyTypedEvent(KeyTypedEvent& e); bool onWindowResizeEvent(WindowResizeEvent& e); }; -} - +} // namespace Deer diff --git a/Deer/src/DeerRender/Mesh/MeshResource.cpp b/Deer/src/DeerRender/Mesh/MeshResource.cpp index d5fc640..c606855 100644 --- a/Deer/src/DeerRender/Mesh/MeshResource.cpp +++ b/Deer/src/DeerRender/Mesh/MeshResource.cpp @@ -1,4 +1,3 @@ -#include "DeerRender/DataStore.h" #include "DeerRender/Log.h" #include "DeerRender/Mesh.h" #include "DeerRender/Render/VertexArray.h" diff --git a/Deer/src/DeerRender/ScriptSystem/InternalAPI/Resources.cpp b/Deer/src/DeerRender/ScriptSystem/InternalAPI/Resources.cpp new file mode 100644 index 0000000..e876956 --- /dev/null +++ b/Deer/src/DeerRender/ScriptSystem/InternalAPI/Resources.cpp @@ -0,0 +1,79 @@ +#include "DeerRender/ScriptSystem/InternalAPI/Resources.h" +#include "DeerRender/ScriptSystem.h" + +#include "DeerRender/Log.h" +#include "DeerRender/Tools/Path.h" + +#include "angelscript.h" +#include "scriptarray.h" +#include "scriptstdstring.h" + +#include + +namespace Deer { + namespace ScriptSystem { + extern asIScriptEngine* scriptEngine; + + ResourceType getResourceType(std::string& dir) { + Path path(dir); + std::string extension = path.extension().string(); + + if (extension == ".obj" || extension == ".fbx" || extension == ".dae" || + extension == ".3ds" || extension == ".ply" || extension == ".stl" || + extension == ".glb" || extension == ".gltf" || extension == ".mtl") { + return ResourceType::MESH; + } + + if (extension == ".glsl") + return ResourceType::SHADER; + + if (extension == ".png") + return ResourceType::TEXTURE; + + return ResourceType::NONE; + }; + + CScriptArray* getResourceFolders(std::string& path_s) { + asITypeInfo* arrayString = scriptEngine->GetTypeInfoByName("array"); + CScriptArray* array = CScriptArray::Create(arrayString); + + Path path_p = Path("Resources") / Path(path_s).lexically_normal(); + try { + for (const auto& entry : std::filesystem::directory_iterator(path_p)) { + if (entry.is_directory()) { + std::string s = entry.path().lexically_relative("Resources").string(); + + array->Resize(array->GetSize() + 1); + array->SetValue(array->GetSize() - 1, &s); + } + } + } catch (const std::exception& e) { + DEER_EDITOR_ENGINE_ERROR("Error in getResourceFolders(), {}", e.what()); + } + + return array; + } + + CScriptArray* getResourceFiles(std::string& path_s) { + asITypeInfo* arrayString = scriptEngine->GetTypeInfoByName("array"); + CScriptArray* array = CScriptArray::Create(arrayString); + + Path path_p = Path("Resources") / Path(path_s).lexically_normal(); + try { + for (const auto& entry : std::filesystem::directory_iterator(path_p)) { + if (entry.is_regular_file()) { + std::string s = entry.path().lexically_relative("Resources").string(); + + array->Resize(array->GetSize() + 1); + array->SetValue(array->GetSize() - 1, &s); + } + } + } catch (const std::exception& e) { + DEER_EDITOR_ENGINE_ERROR("Error in getResourceFiles(), {}", e.what()); + } + + return array; + } + + } // namespace ScriptSystem +} // namespace Deer \ No newline at end of file diff --git a/Deer/src/DeerRender/ScriptSystem/InternalAPI/Resources.h b/Deer/src/DeerRender/ScriptSystem/InternalAPI/Resources.h new file mode 100644 index 0000000..77fe5e7 --- /dev/null +++ b/Deer/src/DeerRender/ScriptSystem/InternalAPI/Resources.h @@ -0,0 +1,32 @@ +#pragma once +#include "scriptarray.h" +#include "scriptdictionary.h" + +#include +#include + +namespace Deer { + namespace ScriptSystem { + template + std::string resource_getPath(Resource resource) { + return ResourceManager::getStorageId(resource); + } + + template + std::string resource_getName(Resource resource) { + return ResourceManager::getStorageId(resource).stem().string(); + } + + enum ResourceType : uint32_t { + NONE = 0, + MESH = 1, + SHADER = 2, + TEXTURE = 3, + }; + + CScriptArray* getResourceFolders(std::string& path); + CScriptArray* getResourceFiles(std::string& path); + + ResourceType getResourceType(std::string&); + } // namespace ScriptSystem +} // namespace Deer \ No newline at end of file diff --git a/Deer/src/DeerRender/ScriptSystem/ResourcesRegistry.cpp b/Deer/src/DeerRender/ScriptSystem/ResourcesRegistry.cpp new file mode 100644 index 0000000..3099fc0 --- /dev/null +++ b/Deer/src/DeerRender/ScriptSystem/ResourcesRegistry.cpp @@ -0,0 +1,60 @@ +#include "DeerCore/ScriptSystem/Helpers.h" +#include "DeerRender/ScriptSystem.h" +#include "DeerRender/ScriptSystem/InternalAPI/Resources.h" + +#include "DeerRender/Mesh.h" +#include "DeerRender/Resource.h" +#include "DeerRender/Shader.h" +#include "DeerRender/Texture.h" +#include "angelscript.h" + +namespace Deer { + namespace ScriptSystem { + extern asIScriptEngine* scriptEngine; + + void registerResources(); + + void registerResourceStructs(); + void registerResourceFunctions(); + + template + void registerResourceBasics(const char* objName) { + REGISTER_OBJECT_METHOD(scriptEngine, objName, "bool isValid() const", Resource, isValid); + REGISTER_OBJECT_METHOD(scriptEngine, objName, "int get_resourceId() const property", Resource, getResourceId); + REGISTER_EXT_OBJECT_METHOD(scriptEngine, objName, "string get_name() const property", resource_getName); + REGISTER_EXT_OBJECT_METHOD(scriptEngine, objName, "string get_path() const property", resource_getPath); + } + } // namespace ScriptSystem + + void ScriptSystem::registerResourceStructs() { + AS_CHECK(scriptEngine->RegisterObjectType("GPUMesh", sizeof(Resource), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_ALLINTS | asGetTypeTraits>())); + AS_CHECK(scriptEngine->RegisterObjectType("Shader", sizeof(Resource), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_ALLINTS | asGetTypeTraits>())); + AS_CHECK(scriptEngine->RegisterObjectType("Texture", sizeof(Resource), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_ALLINTS | asGetTypeTraits>())); + + scriptEngine->RegisterEnum("ResourceType"); + scriptEngine->RegisterEnumValue("ResourceType", "None", (int)ResourceType::NONE); + scriptEngine->RegisterEnumValue("ResourceType", "Mesh", (int)ResourceType::MESH); + scriptEngine->RegisterEnumValue("ResourceType", "Shader", (int)ResourceType::SHADER); + scriptEngine->RegisterEnumValue("ResourceType", "Texture", (int)ResourceType::TEXTURE); + } + /* + void ScriptSystem::registerResourceFunctions() { + scriptEngine->SetDefaultNamespace("Resource"); + REGISTER_GLOBAL_FUNC("GPUMesh loadGPUMesh(string&in path)", ResourceManager::loadResource); + REGISTER_GLOBAL_FUNC("Shader loadShader(string&in path)", ResourceManager::loadResource); + REGISTER_GLOBAL_FUNC("Texture loadTexture(string&in path)", ResourceManager::loadResource); + + REGISTER_GLOBAL_FUNC("array@ getResourceFolders(string&in path)", StudioAPI::getResourceFolders); + REGISTER_GLOBAL_FUNC("array@ getResourceFiles(string&in path)", StudioAPI::getResourceFiles); + REGISTER_GLOBAL_FUNC("ResourceType getResourceType(string&in path)", StudioAPI::getResourceType); + scriptEngine->SetDefaultNamespace(""); + } + */ + + void ScriptSystem::registerResources() { + registerResourceStructs(); + registerResourceBasics("GPUMesh"); + registerResourceBasics("Shader"); + registerResourceBasics("Texture"); + } +} // namespace Deer \ No newline at end of file diff --git a/Deer/src/DeerRender/Shader/ShaderBuilder.cpp b/Deer/src/DeerRender/Shader/ShaderBuilder.cpp index e86fd18..0ab339b 100644 --- a/Deer/src/DeerRender/Shader/ShaderBuilder.cpp +++ b/Deer/src/DeerRender/Shader/ShaderBuilder.cpp @@ -1,4 +1,3 @@ -#include "DeerRender/DataStore.h" #include "DeerRender/Shader.h" #include "DeerRender/Tools/Memory.h" diff --git a/Deer/src/DeerRender/World/Environment.cpp b/Deer/src/DeerRender/World/Environment.cpp index b0de5f8..2de74a3 100755 --- a/Deer/src/DeerRender/World/Environment.cpp +++ b/Deer/src/DeerRender/World/Environment.cpp @@ -1,8 +1,5 @@ -#include "DeerRender/EntityEnviroment.h" - -#include "DeerRender/Application.h" - #include "DeerRender/Components.h" +#include "DeerRender/EntityEnviroment.h" #include "DeerRender/Mesh.h" #include "DeerRender/Render/Render.h" diff --git a/Deer/src/DeerRender/World/World.cpp b/Deer/src/DeerRender/World/World.cpp index 4185b6c..0e10be1 100755 --- a/Deer/src/DeerRender/World/World.cpp +++ b/Deer/src/DeerRender/World/World.cpp @@ -6,24 +6,50 @@ #include "DeerRender/World.h" namespace Deer { - void World::render() { - uint32_t mainCamera = environment.tryGetMainCamera(); - if (mainCamera == 0) + void World::execute() { + if (executingState != WorldState::Created) return; - Entity& m_cameraEntity = environment.getEntity(mainCamera); - WorldCamera sceneCamera; - sceneCamera.camera = m_cameraEntity.getComponent(); - sceneCamera.transform = m_cameraEntity.getComponent(); + if (worldSettings.updateFrequency == 0) + return; - World::render(sceneCamera); - } + auto previousTime = std::chrono::high_resolution_clock::now(); + double accumulatedUpdateTime = 0.0; + double accumulatedRenderTime = 0.0; - void World::render(const WorldCamera& sceneCamera) { - RenderCommand::setDepthBuffer(true); - environment.render(sceneCamera); + double targetUpdateTime = 1.0f / worldSettings.updateFrequency; + double targetRenderTime = 0; + if (worldSettings.renderFrequency > 0) + targetRenderTime = 1.0f / worldSettings.renderFrequency; - RenderCommand::setDepthBuffer(false); + executingState = WorldState::Executing; + while (executingState == WorldState::Executing) { + auto currentTime = std::chrono::high_resolution_clock::now(); + std::chrono::duration deltaTime = currentTime - previousTime; + previousTime = currentTime; + + accumulatedUpdateTime += deltaTime.count(); + accumulatedRenderTime += deltaTime.count(); + + while (accumulatedUpdateTime >= targetUpdateTime) { + accumulatedUpdateTime -= targetUpdateTime; + + worldSettings.updateCallback(*this); + } + + if (targetRenderTime > 0 && accumulatedRenderTime >= targetRenderTime) { + worldSettings.renderCallback(*this); + while (accumulatedRenderTime >= targetRenderTime) + accumulatedRenderTime -= targetRenderTime; + } + + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + + if (executingState == WorldState::DestroyQueued) + executingState = WorldState::ReadyToDestroy; + else + executingState = WorldState::Stopped; } } // namespace Deer diff --git a/Deer/src/Plattform/ImGUI/ImGuiLayer.cpp b/Deer/src/Plattform/ImGUI/ImGuiLayer.cpp index 3bfb985..483203b 100755 --- a/Deer/src/Plattform/ImGUI/ImGuiLayer.cpp +++ b/Deer/src/Plattform/ImGUI/ImGuiLayer.cpp @@ -1,10 +1,10 @@ #include "DeerRender/ImGui/ImGuiLayer.h" -#include "DeerRender/Application.h" #include "DeerRender/Events/Event.h" #include "DeerRender/Input.h" #include "DeerRender/Log.h" #include "DeerRender/Render/RenderCommand.h" +#include "DeerRender/Window.h" // THIS ORDER #include "Plattform/OpenGL/imgui_impl_opengl3.h" @@ -20,7 +20,7 @@ #include "ImGuizmo.h" namespace Deer { - void ImGuiLayer::onAttach() { + void ImGuiLayer::onAttach(Window& window) { ImGui::CreateContext(); ImGui::StyleColorsDark(); @@ -30,11 +30,9 @@ namespace Deer { io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; ImGui_ImplOpenGL3_Init("#version 410"); - Application::getWindow().initImGUI(); + window.initImGUI(); - io.DisplaySize = - ImVec2(Application::getWindow().getWitdth(), - Application::getWindow().getHeight()); + io.DisplaySize = ImVec2(window.getWitdth(), window.getHeight()); } void ImGuiLayer::onDetach() {} @@ -99,27 +97,25 @@ namespace Deer { } bool ImGuiLayer::onKeyPressedEvent(KeyPressedEvent& e) { - ImGuiIO& io = ImGui::GetIO(); - ImGuiKey key = static_cast(e.getKeyCode()); - - // Modern way: register key press - io.AddKeyEvent(key, true); - - // Modifier keys can also be updated automatically or manually: - io.AddKeyEvent(ImGuiKey_LeftCtrl, ImGui::IsKeyPressed(ImGuiKey_LeftCtrl) || ImGui::IsKeyPressed(ImGuiKey_RightCtrl)); - io.AddKeyEvent(ImGuiKey_LeftShift, ImGui::IsKeyPressed(ImGuiKey_LeftShift) || ImGui::IsKeyPressed(ImGuiKey_RightShift)); - io.AddKeyEvent(ImGuiKey_LeftAlt, ImGui::IsKeyPressed(ImGuiKey_LeftAlt) || ImGui::IsKeyPressed(ImGuiKey_RightAlt)); - io.AddKeyEvent(ImGuiKey_LeftSuper, ImGui::IsKeyPressed(ImGuiKey_LeftSuper) || ImGui::IsKeyPressed(ImGuiKey_RightSuper)); - - return false; - } + ImGuiIO& io = ImGui::GetIO(); + ImGuiKey key = static_cast(e.getKeyCode()); + // Modern way: register key press + io.AddKeyEvent(key, true); + + // Modifier keys can also be updated automatically or manually: + io.AddKeyEvent(ImGuiKey_LeftCtrl, ImGui::IsKeyPressed(ImGuiKey_LeftCtrl) || ImGui::IsKeyPressed(ImGuiKey_RightCtrl)); + io.AddKeyEvent(ImGuiKey_LeftShift, ImGui::IsKeyPressed(ImGuiKey_LeftShift) || ImGui::IsKeyPressed(ImGuiKey_RightShift)); + io.AddKeyEvent(ImGuiKey_LeftAlt, ImGui::IsKeyPressed(ImGuiKey_LeftAlt) || ImGui::IsKeyPressed(ImGuiKey_RightAlt)); + io.AddKeyEvent(ImGuiKey_LeftSuper, ImGui::IsKeyPressed(ImGuiKey_LeftSuper) || ImGui::IsKeyPressed(ImGuiKey_RightSuper)); + + return false; + } bool ImGuiLayer::onKeyReleasedEvent(KeyReleasedEvent& e) { ImGuiIO& io = ImGui::GetIO(); io.AddKeyEvent((ImGuiKey)e.getKeyCode(), - ImGui::IsKeyPressed(ImGuiKey_LeftSuper) || ImGui::IsKeyPressed(ImGuiKey_RightSuper)); - + ImGui::IsKeyPressed(ImGuiKey_LeftSuper) || ImGui::IsKeyPressed(ImGuiKey_RightSuper)); return false; } diff --git a/Deer/src/Plattform/Linux/LinuxWindow.cpp b/Deer/src/Plattform/Linux/LinuxWindow.cpp index b82a928..798515b 100755 --- a/Deer/src/Plattform/Linux/LinuxWindow.cpp +++ b/Deer/src/Plattform/Linux/LinuxWindow.cpp @@ -1,5 +1,4 @@ #include "LinuxWindow.h" -#include "DeerCore/Application.h" #include "DeerCore/Log.h" #include "glad/glad.h" diff --git a/Deer/src/Plattform/OpenGL/OpenGLFrameBuffer.cpp b/Deer/src/Plattform/OpenGL/OpenGLFrameBuffer.cpp index 4620d5b..be0c447 100755 --- a/Deer/src/Plattform/OpenGL/OpenGLFrameBuffer.cpp +++ b/Deer/src/Plattform/OpenGL/OpenGLFrameBuffer.cpp @@ -1,6 +1,7 @@ #include "OpenGLFrameBuffer.h" -#include "DeerRender/Application.h" +#include "DeerRender/Engine.h" #include "DeerRender/Log.h" +#include "DeerRender/Window.h" #include "glad/glad.h" @@ -30,8 +31,8 @@ namespace Deer { } void OpenGLFrameBuffer::unbind() { - unsigned int width = Application::getWindow().getWitdth(); - unsigned int height = Application::getWindow().getHeight(); + unsigned int width = Engine::getWindow().getWitdth(); + unsigned int height = Engine::getWindow().getHeight(); glViewport(0, 0, width, height); glBindFramebuffer(GL_FRAMEBUFFER, 0); } diff --git a/Deer/src/Plattform/OpenGL/OpenGLRenderAPI.cpp b/Deer/src/Plattform/OpenGL/OpenGLRenderAPI.cpp index 65ff11e..874134b 100755 --- a/Deer/src/Plattform/OpenGL/OpenGLRenderAPI.cpp +++ b/Deer/src/Plattform/OpenGL/OpenGLRenderAPI.cpp @@ -1,6 +1,7 @@ #include "OpenGLRenderAPI.h" -#include "DeerRender/Application.h" +#include "DeerRender/Engine.h" #include "DeerRender/Render/VertexArray.h" +#include "DeerRender/Window.h" #include "Plattform/OpenGL/OpenGLBuffer.h" #include "glad/glad.h" @@ -62,8 +63,8 @@ namespace Deer { } void OpenGLRenderAPI::unbindFrameBuffer() { - unsigned int width = Application::getWindow().getWitdth(); - unsigned int height = Application::getWindow().getHeight(); + unsigned int width = Engine::getWindow().getWitdth(); + unsigned int height = Engine::getWindow().getHeight(); glViewport(0, 0, width, height); glBindFramebuffer(GL_FRAMEBUFFER, 0); } diff --git a/DeerStudio/headers/DeerStudio/AngelScriptEngine.h b/DeerStudio/headers/DeerStudio/AngelScriptEngine.h index 3c6c85f..0cf9cb6 100644 --- a/DeerStudio/headers/DeerStudio/AngelScriptEngine.h +++ b/DeerStudio/headers/DeerStudio/AngelScriptEngine.h @@ -16,7 +16,7 @@ class CScriptBuilder; class asIScriptGeneric; namespace Deer { - namespace AngelScriptEngine { + namespace StudioAPI { void initialize(); void update(); void shutdown(); @@ -149,5 +149,5 @@ namespace Deer { void registerStructs(); void registerFunctions(); void extractBaseTypes(); - } // namespace AngelScriptEngine + } // namespace StudioAPI } // namespace Deer \ No newline at end of file diff --git a/DeerStudio/headers/DeerStudio/StudioAPI.h b/DeerStudio/headers/DeerStudio/StudioAPI.h new file mode 100644 index 0000000..594b9bc --- /dev/null +++ b/DeerStudio/headers/DeerStudio/StudioAPI.h @@ -0,0 +1,15 @@ +#pragma once +#include "DeerRender/ScriptSystem.h" + +namespace Deer { + namespace StudioAPI { + extern ScriptSystem::Registry* engineRegistry; + extern ScriptSystem::Registry* uiRegistry; + extern ScriptSystem::Registry* entityRegistry; + + extern ScriptSystem::Registry* mathRegistry; + extern ScriptSystem::Registry* resourceRegistry; + + void registerStudioAPI(); + } // namespace StudioAPI +} // namespace Deer \ No newline at end of file diff --git a/DeerStudio/headers/DeerStudio/StudioAPI/FrameBuffer.h b/DeerStudio/headers/DeerStudio/StudioAPI/FrameBuffer.h index 0e484c4..b434f2b 100644 --- a/DeerStudio/headers/DeerStudio/StudioAPI/FrameBuffer.h +++ b/DeerStudio/headers/DeerStudio/StudioAPI/FrameBuffer.h @@ -22,6 +22,5 @@ namespace Deer { FrameBufferStruct getFrameBuffer(std::string& name); void frameBuffer_constructor(FrameBufferHandleStruct* mem); - } // namespace StudioAPI } // namespace Deer \ No newline at end of file diff --git a/DeerStudio/headers/DeerStudio/StudioAPI/GenericRefStructs.h b/DeerStudio/headers/DeerStudio/StudioAPI/GenericRefStructs.h index 39f5b2b..0f627f8 100644 --- a/DeerStudio/headers/DeerStudio/StudioAPI/GenericRefStructs.h +++ b/DeerStudio/headers/DeerStudio/StudioAPI/GenericRefStructs.h @@ -18,7 +18,7 @@ namespace Deer { int32_t environmentId; bool assertEntity(const char* funcName); - EntityEnvironment* getEntityEntityEnvironment(); + EntityEnvironment* getEntityEnvironment(); }; struct FrameBufferHandleStruct { diff --git a/DeerStudio/src/DeerStudio/AngelScriptEngine/AngelScriptEngine.cpp b/DeerStudio/src/DeerStudio/AngelScriptEngine/AngelScriptEngine.cpp index f03acd7..a73a4b1 100644 --- a/DeerStudio/src/DeerStudio/AngelScriptEngine/AngelScriptEngine.cpp +++ b/DeerStudio/src/DeerStudio/AngelScriptEngine/AngelScriptEngine.cpp @@ -1,6 +1,7 @@ #include "DeerStudio/AngelScriptEngine.h" #include "DeerStudio/AngelScriptEngine/AngelScriptRegisters.h" #include "DeerStudio/AngelScriptEngine/ErrorHandle.h" +#include "DeerStudio/StudioAPI/Resources.h" #include "DeerStudio/StudioAPI/Debug.h" @@ -14,7 +15,7 @@ namespace Deer { // GLOBAL DECLARATIONS - namespace AngelScriptEngine { + namespace StudioAPI { asIScriptEngine* scriptEngine; asIScriptContext* scriptContext; @@ -27,14 +28,14 @@ namespace Deer { asITypeInfo* panelBaseType; asITypeInfo* serviceBaseType; asITypeInfo* arrayStringBaseType; - } // namespace AngelScriptEngine + } // namespace StudioAPI - void AngelScriptEngine::raiseError() { - if (AngelScriptEngine::executingModule) + void StudioAPI::raiseError() { + if (StudioAPI::executingModule) executingModule->invalidate(); } - void AngelScriptEngine::initialize() { + void StudioAPI::initialize() { int err = 0; // Deinitialize if its initialized @@ -61,23 +62,23 @@ namespace Deer { module.init(); } } - void AngelScriptEngine::update() { + void StudioAPI::update() { for (Module& module : modules) { module.update(); } } - void AngelScriptEngine::shutdown() { + void StudioAPI::shutdown() { for (Module& module : modules) { module.shutdown(); } } - void AngelScriptEngine::render() { + void StudioAPI::render() { for (Module& module : modules) { module.render(); } } - void AngelScriptEngine::deinitialize() { + void StudioAPI::deinitialize() { for (Module& module : modules) { module.shutdown(); } @@ -95,7 +96,7 @@ namespace Deer { scriptEngine = nullptr; } - void AngelScriptEngine::registerStructs() { + void StudioAPI::registerStructs() { registerMathStructs(); registerResourceStructs(); registerEntityEnvironmentStructs(); @@ -105,7 +106,7 @@ namespace Deer { registerUIStructs(); } - void AngelScriptEngine::registerFunctions() { + void StudioAPI::registerFunctions() { registerUIFunctions(); registerMathFunctions(); registerResourceFunctions(); diff --git a/DeerStudio/src/DeerStudio/AngelScriptEngine/ErrorHandle.cpp b/DeerStudio/src/DeerStudio/AngelScriptEngine/ErrorHandle.cpp index dea0dad..2f3669f 100644 --- a/DeerStudio/src/DeerStudio/AngelScriptEngine/ErrorHandle.cpp +++ b/DeerStudio/src/DeerStudio/AngelScriptEngine/ErrorHandle.cpp @@ -3,7 +3,7 @@ #include "angelscript.h" namespace Deer { - namespace AngelScriptEngine { + namespace StudioAPI { const char* getAngelScriptReturnCodeString(int code) { switch (code) { case asSUCCESS: @@ -61,5 +61,5 @@ namespace Deer { return false; } - } // namespace AngelScriptEngine + } // namespace StudioAPI } // namespace Deer \ No newline at end of file diff --git a/DeerStudio/src/DeerStudio/AngelScriptEngine/ErrorHandle.h b/DeerStudio/src/DeerStudio/AngelScriptEngine/ErrorHandle.h index 3cf80ed..f6c9ec5 100644 --- a/DeerStudio/src/DeerStudio/AngelScriptEngine/ErrorHandle.h +++ b/DeerStudio/src/DeerStudio/AngelScriptEngine/ErrorHandle.h @@ -3,31 +3,31 @@ #include "angelscript.h" namespace Deer { - namespace AngelScriptEngine { + namespace StudioAPI { const char* getAngelScriptReturnCodeString(int code); bool ImplementsInterface(asITypeInfo* type, asITypeInfo* iface); - } + } // namespace StudioAPI } // namespace Deer -#define AS_CHECK(f) \ - { \ - int __r = f; \ - if (__r < 0) { \ - DEER_EDITOR_ENGINE_ERROR("Error at line: {0}:{1} -> {2}", __FILE__, __LINE__, Deer::AngelScriptEngine::getAngelScriptReturnCodeString(__r)); \ - } \ +#define AS_CHECK(f) \ + { \ + int __r = f; \ + if (__r < 0) { \ + DEER_EDITOR_ENGINE_ERROR("Error at line: {0}:{1} -> {2}", __FILE__, __LINE__, Deer::StudioAPI::getAngelScriptReturnCodeString(__r)); \ + } \ } -#define AS_CHECK_ADDITIONAL_INFO(f, i) \ - { \ - int __r = f; \ - if (__r < 0) { \ - DEER_EDITOR_ENGINE_ERROR("Error at line: {0}:{1} -> {2} \n {3}", __FILE__, __LINE__, Deer::AngelScriptEngine::getAngelScriptReturnCodeString(__r), i); \ - } \ +#define AS_CHECK_ADDITIONAL_INFO(f, i) \ + { \ + int __r = f; \ + if (__r < 0) { \ + DEER_EDITOR_ENGINE_ERROR("Error at line: {0}:{1} -> {2} \n {3}", __FILE__, __LINE__, Deer::StudioAPI::getAngelScriptReturnCodeString(__r), i); \ + } \ } -#define AS_RET_CHECK(f) \ - { \ - int __r = f; \ - if (__r < 0) { \ - DEER_EDITOR_ENGINE_ERROR("Error at line: {0}:{1} -> {2}", __FILE__, __LINE__, Deer::AngelScriptEngine::getAngelScriptReturnCodeString(__r)); \ - return; \ - } \ +#define AS_RET_CHECK(f) \ + { \ + int __r = f; \ + if (__r < 0) { \ + DEER_EDITOR_ENGINE_ERROR("Error at line: {0}:{1} -> {2}", __FILE__, __LINE__, Deer::StudioAPI::getAngelScriptReturnCodeString(__r)); \ + return; \ + } \ } diff --git a/DeerStudio/src/DeerStudio/AngelScriptEngine/Module.cpp b/DeerStudio/src/DeerStudio/AngelScriptEngine/Module.cpp index 9b31c3a..cfc71d1 100644 --- a/DeerStudio/src/DeerStudio/AngelScriptEngine/Module.cpp +++ b/DeerStudio/src/DeerStudio/AngelScriptEngine/Module.cpp @@ -4,7 +4,7 @@ #include "angelscript.h" namespace Deer { - namespace AngelScriptEngine { + namespace StudioAPI { Module::Module(const ModuleDescription& _mi) : moduleInfo(_mi) {} void Module::extract(asIScriptModule* _module) { @@ -33,7 +33,7 @@ namespace Deer { if (state != ModuleState::Built) return; - angelscriptModule = AngelScriptEngine::scriptEngine->GetModule(moduleInfo.moduleName.c_str()); + angelscriptModule = StudioAPI::scriptEngine->GetModule(moduleInfo.moduleName.c_str()); for (Service& service : services) service.updateTypes(angelscriptModule); @@ -89,5 +89,5 @@ namespace Deer { services.clear(); panels.clear(); } - }; // namespace AngelScriptEngine + }; // namespace StudioAPI }; // namespace Deer \ No newline at end of file diff --git a/DeerStudio/src/DeerStudio/AngelScriptEngine/ModuleDescriptorSerializer.h b/DeerStudio/src/DeerStudio/AngelScriptEngine/ModuleDescriptorSerializer.h index 995a4ca..1fe2ce2 100644 --- a/DeerStudio/src/DeerStudio/AngelScriptEngine/ModuleDescriptorSerializer.h +++ b/DeerStudio/src/DeerStudio/AngelScriptEngine/ModuleDescriptorSerializer.h @@ -4,7 +4,7 @@ #include namespace Deer { - namespace AngelScriptEngine { + namespace StudioAPI { template void serialize(Archive& archive, ModuleDescription& m) { @@ -13,5 +13,5 @@ namespace Deer { archive(cereal::make_nvp("requires", m.module_requires)); } - } // namespace AngelScriptEngine + } // namespace StudioAPI } // namespace Deer \ No newline at end of file diff --git a/DeerStudio/src/DeerStudio/AngelScriptEngine/ModuleLoading.cpp b/DeerStudio/src/DeerStudio/AngelScriptEngine/ModuleLoading.cpp index 17e2182..b3910eb 100644 --- a/DeerStudio/src/DeerStudio/AngelScriptEngine/ModuleLoading.cpp +++ b/DeerStudio/src/DeerStudio/AngelScriptEngine/ModuleLoading.cpp @@ -20,12 +20,12 @@ namespace fs = std::filesystem; namespace Deer { - namespace AngelScriptEngine { + namespace StudioAPI { bool loadModule(Module& module); void loadModuleInfo(const Path& path); - } // namespace AngelScriptEngine + } // namespace StudioAPI - asIScriptFunction* AngelScriptEngine::getFactory(asITypeInfo* type) { + asIScriptFunction* StudioAPI::getFactory(asITypeInfo* type) { std::string callString = ""; if (strlen(type->GetNamespace())) { callString += type->GetNamespace(); @@ -43,7 +43,7 @@ namespace Deer { return type->GetFactoryByDecl(callString.c_str()); } - bool AngelScriptEngine::loadModule(Module& module) { + bool StudioAPI::loadModule(Module& module) { if (module.state != ModuleState::NotBuilt) return false; @@ -106,7 +106,7 @@ namespace Deer { asIScriptModule* as_module = scriptBuilder.GetModule(); if (err < 0) { - DEER_EDITOR_ENGINE_ERROR("Failed compiling module {}\nerror: {}", module.moduleInfo.moduleName.c_str(), Deer::AngelScriptEngine::getAngelScriptReturnCodeString(err)); + DEER_EDITOR_ENGINE_ERROR("Failed compiling module {}\nerror: {}", module.moduleInfo.moduleName.c_str(), Deer::StudioAPI::getAngelScriptReturnCodeString(err)); module.state = ModuleState::CompilationFailed; return false; } @@ -117,7 +117,7 @@ namespace Deer { return true; } - void AngelScriptEngine::loadModuleInfo(const Path& path) { + void StudioAPI::loadModuleInfo(const Path& path) { ModuleDescription desc; std::ifstream is((path / "module.json").string()); @@ -165,7 +165,7 @@ namespace Deer { modules.push_back({desc}); } - void AngelScriptEngine::loadModules() { + void StudioAPI::loadModules() { const Path path = Path("Editor") / Path("Modules"); if (!fs::exists(path) || !fs::is_directory(path)) { diff --git a/DeerStudio/src/DeerStudio/AngelScriptEngine/ModulePredefinedGenerator.cpp b/DeerStudio/src/DeerStudio/AngelScriptEngine/ModulePredefinedGenerator.cpp index 48a30c8..dd7fb7f 100644 --- a/DeerStudio/src/DeerStudio/AngelScriptEngine/ModulePredefinedGenerator.cpp +++ b/DeerStudio/src/DeerStudio/AngelScriptEngine/ModulePredefinedGenerator.cpp @@ -14,7 +14,6 @@ #include #include -#include "DeerRender/DataStore.h" #include "DeerRender/Tools/Path.h" static std::stringstream stream; @@ -268,7 +267,7 @@ void printGlobalTypedef(const asIScriptEngine& engine) { } namespace Deer { - namespace AngelScriptEngine { + namespace StudioAPI { void Module::generatePredefined() { stream.clear(); stream.str(""); @@ -288,5 +287,5 @@ namespace Deer { predefinedFile.write(info.c_str(), info.size()); predefinedFile.close(); } - } // namespace AngelScriptEngine + } // namespace StudioAPI } // namespace Deer \ No newline at end of file diff --git a/DeerStudio/src/DeerStudio/AngelScriptEngine/Panel.cpp b/DeerStudio/src/DeerStudio/AngelScriptEngine/Panel.cpp index 690ddd4..43fad87 100644 --- a/DeerStudio/src/DeerStudio/AngelScriptEngine/Panel.cpp +++ b/DeerStudio/src/DeerStudio/AngelScriptEngine/Panel.cpp @@ -4,7 +4,7 @@ #include "imgui.h" namespace Deer { - namespace AngelScriptEngine { + namespace StudioAPI { Panel::Panel(asITypeInfo* _info) : Service(_info) { @@ -54,5 +54,5 @@ namespace Deer { } Panel::~Panel() {} - } // namespace AngelScriptEngine + } // namespace StudioAPI } // namespace Deer \ No newline at end of file diff --git a/DeerStudio/src/DeerStudio/AngelScriptEngine/Registers/Engine.cpp b/DeerStudio/src/DeerStudio/AngelScriptEngine/Registers/Engine.cpp deleted file mode 100644 index 3b25728..0000000 --- a/DeerStudio/src/DeerStudio/AngelScriptEngine/Registers/Engine.cpp +++ /dev/null @@ -1,35 +0,0 @@ -#include "DeerStudio/StudioAPI/Engine.h" -#include "DeerStudio/AngelScriptEngine/AngelScriptRegisters.h" -#include "DeerStudio/StudioAPI/Debug.h" - -namespace Deer { - void AngelScriptEngine::registerEngineFunctions() { - scriptEngine->SetDefaultNamespace("Engine"); - REGISTER_GLOBAL_FUNC("void print(const string&in text)", StudioAPI::print); - scriptEngine->SetDefaultNamespace(""); - - scriptEngine->SetDefaultNamespace("Path"); - REGISTER_GLOBAL_FUNC("string getParent(const string&in path)", StudioAPI::getParentPath); - REGISTER_GLOBAL_FUNC("string getParentName(const string&in path)", StudioAPI::getParentPathName); - REGISTER_GLOBAL_FUNC("string getName(const string&in path)", StudioAPI::getPathName); - REGISTER_GLOBAL_FUNC("array@ divide(const string&in path)", StudioAPI::dividePath_angelscript); - scriptEngine->SetDefaultNamespace(""); - } - - void AngelScriptEngine::registerEngineStructs() { - AS_RET_CHECK(scriptEngine->RegisterInterface("Panel")); - AS_RET_CHECK(scriptEngine->RegisterInterfaceMethod("Panel", "void render()")); - - AS_RET_CHECK(scriptEngine->RegisterInterface("Service")); - - AS_CHECK(scriptEngine->RegisterFuncdef("void SimpleFunction()")); - AS_CHECK(scriptEngine->RegisterFuncdef("void ReciverFunction(any@)")); - AS_CHECK(scriptEngine->RegisterFuncdef("void TransferFunction(any@, any@)")); - } - - void AngelScriptEngine::extractBaseTypes() { - panelBaseType = scriptEngine->GetTypeInfoByDecl("Panel"); - serviceBaseType = scriptEngine->GetTypeInfoByDecl("Service"); - arrayStringBaseType = scriptEngine->GetTypeInfoByDecl("array"); - } -} // namespace Deer \ No newline at end of file diff --git a/DeerStudio/src/DeerStudio/AngelScriptEngine/Registers/Entity.cpp b/DeerStudio/src/DeerStudio/AngelScriptEngine/Registers/Entity.cpp deleted file mode 100644 index fe7d157..0000000 --- a/DeerStudio/src/DeerStudio/AngelScriptEngine/Registers/Entity.cpp +++ /dev/null @@ -1,102 +0,0 @@ -#include "DeerStudio/StudioAPI/Entity.h" -#include "DeerStudio/AngelScriptEngine/AngelScriptRegisters.h" - -namespace Deer { - namespace AngelScriptEngine { - void registerTransformComponentFunctions(); - void registerMeshComponentFunction(); - void registerShaderComponentFunctions(); - void registerComponentComponentFunctions(); - } // namespace AngelScriptEngine - - void AngelScriptEngine::registerEntityStructs() { - AS_CHECK(scriptEngine->RegisterObjectType("Entity", sizeof(StudioAPI::EntityHandleStruct), asOBJ_VALUE | asOBJ_POD | asGetTypeTraits() | asOBJ_APP_CLASS_ALLINTS)); - AS_CHECK(scriptEngine->RegisterObjectType("EntityChilds", sizeof(StudioAPI::EntityHandleStruct), asOBJ_VALUE | asOBJ_POD | asGetTypeTraits() | asOBJ_APP_CLASS_ALLINTS)); - AS_CHECK(scriptEngine->RegisterObjectType("TransformComponent", sizeof(StudioAPI::EntityHandleStruct), asOBJ_VALUE | asOBJ_POD | asGetTypeTraits() | asOBJ_APP_CLASS_ALLINTS)); - AS_CHECK(scriptEngine->RegisterObjectType("MeshComponent", sizeof(StudioAPI::EntityHandleStruct), asOBJ_VALUE | asOBJ_POD | asGetTypeTraits() | asOBJ_APP_CLASS_ALLINTS)); - AS_CHECK(scriptEngine->RegisterObjectType("ShaderComponent", sizeof(StudioAPI::EntityHandleStruct), asOBJ_VALUE | asOBJ_POD | asGetTypeTraits() | asOBJ_APP_CLASS_ALLINTS)); - AS_CHECK(scriptEngine->RegisterObjectType("CameraComponent", sizeof(StudioAPI::EntityHandleStruct), asOBJ_VALUE | asOBJ_POD | asGetTypeTraits() | asOBJ_APP_CLASS_ALLINTS)); - } - - void AngelScriptEngine::registerEntityFunctions() { - registerTransformComponentFunctions(); - registerMeshComponentFunction(); - registerShaderComponentFunctions(); - registerComponentComponentFunctions(); - - REGISTER_OBJECT_METHOD("Entity", "string get_name() const property", StudioAPI::EntityStruct, getName); - REGISTER_OBJECT_METHOD("Entity", "void set_name(string& in) property", StudioAPI::EntityStruct, setName); - REGISTER_OBJECT_METHOD("Entity", "int get_id() const property", StudioAPI::EntityStruct, getId); - REGISTER_OBJECT_METHOD("Entity", "Entity createChild(const string& in)", StudioAPI::EntityStruct, createChild); - REGISTER_OBJECT_METHOD("Entity", "bool get_isRoot() const property", StudioAPI::EntityStruct, isRoot); - REGISTER_OBJECT_METHOD("Entity", "void destroy()", StudioAPI::EntityStruct, destroy); - REGISTER_OBJECT_METHOD("Entity", "bool get_exists() const property", StudioAPI::EntityStruct, exists); - REGISTER_OBJECT_METHOD("Entity", "Entity get_parent() property", StudioAPI::EntityStruct, getParent); - REGISTER_OBJECT_METHOD("Entity", "void set_parent(Entity) property", StudioAPI::EntityStruct, setParent); - REGISTER_OBJECT_METHOD("Entity", "bool isDescendantOf(Entity)", StudioAPI::EntityStruct, isDescendantOf); - REGISTER_OBJECT_METHOD("Entity", "bool opEquals(const Entity &in) const", StudioAPI::EntityStruct, opEquals); - REGISTER_OBJECT_METHOD("Entity", "EntityChilds get_childs() const property", StudioAPI::EntityStruct, getSelf); - REGISTER_OBJECT_METHOD("Entity", "TransformComponent get_transform() const property", StudioAPI::EntityStruct, getSelf); - REGISTER_OBJECT_METHOD("Entity", "MeshComponent getMeshComponent()", StudioAPI::EntityStruct, getMeshComponent); - REGISTER_OBJECT_METHOD("Entity", "MeshComponent createMeshComponent()", StudioAPI::EntityStruct, createMeshComponent); - REGISTER_OBJECT_METHOD("Entity", "bool hasMeshComponent()", StudioAPI::EntityStruct, hasMeshComponent); - REGISTER_OBJECT_METHOD("Entity", "void removeMeshComponent()", StudioAPI::EntityStruct, removeMeshComponent); - REGISTER_OBJECT_METHOD("Entity", "ShaderComponent getShaderComponent()", StudioAPI::EntityStruct, getShaderComponent); - REGISTER_OBJECT_METHOD("Entity", "ShaderComponent createShaderComponent()", StudioAPI::EntityStruct, createShaderComponent); - REGISTER_OBJECT_METHOD("Entity", "bool hasShaderComponent()", StudioAPI::EntityStruct, hasShaderComponent); - REGISTER_OBJECT_METHOD("Entity", "void removeShaderComponent()", StudioAPI::EntityStruct, removeShaderComponent); - REGISTER_OBJECT_METHOD("Entity", "CameraComponent getCameraComponent()", StudioAPI::EntityStruct, getCameraComponent); - REGISTER_OBJECT_METHOD("Entity", "CameraComponent createCameraComponent()", StudioAPI::EntityStruct, createCameraComponent); - REGISTER_OBJECT_METHOD("Entity", "bool hasCameraComponent()", StudioAPI::EntityStruct, hasCameraComponent); - REGISTER_OBJECT_METHOD("Entity", "void removeCameraComponent()", StudioAPI::EntityStruct, removeCameraComponent); - - scriptEngine->SetDefaultNamespace("Engine"); - REGISTER_GLOBAL_FUNC("Entity getRoot()", StudioAPI::getRoot); - scriptEngine->SetDefaultNamespace(""); - - REGISTER_OBJECT_METHOD("EntityChilds", "int get_count() const property", StudioAPI::EntityChildArrayStruct, getChildCount); - REGISTER_OBJECT_METHOD("EntityChilds", "Entity opIndex(int) const", StudioAPI::EntityChildArrayStruct, getChild); - } - - void AngelScriptEngine::registerMeshComponentFunction() { - REGISTER_OBJECT_METHOD("MeshComponent", "bool get_isActive() const property", StudioAPI::MeshComponentStruct, isActive); - REGISTER_OBJECT_METHOD("MeshComponent", "bool get_hasMesh() const property", StudioAPI::MeshComponentStruct, hasMesh); - REGISTER_OBJECT_METHOD("MeshComponent", "void clear()", StudioAPI::MeshComponentStruct, clear); - REGISTER_OBJECT_METHOD("MeshComponent", "GPUMesh get_meshResource() property", StudioAPI::MeshComponentStruct, getMesh); - REGISTER_OBJECT_METHOD("MeshComponent", "void set_meshResource(GPUMesh) property", StudioAPI::MeshComponentStruct, setMesh); - REGISTER_OBJECT_METHOD("MeshComponent", "void set_isActive(const bool) const property", StudioAPI::MeshComponentStruct, setActive); - } - - void AngelScriptEngine::registerTransformComponentFunctions() { - REGISTER_OBJECT_METHOD("TransformComponent", "vec3 get_position() const property", StudioAPI::TransformComponentStruct, getPosition); - REGISTER_OBJECT_METHOD("TransformComponent", "vec3 get_scale() const property", StudioAPI::TransformComponentStruct, getScale); - REGISTER_OBJECT_METHOD("TransformComponent", "vec3 get_rotation() const property", StudioAPI::TransformComponentStruct, getRotation); - - REGISTER_OBJECT_METHOD("TransformComponent", "void set_position(const vec3) property", StudioAPI::TransformComponentStruct, setPosition); - REGISTER_OBJECT_METHOD("TransformComponent", "void set_scale(const vec3) property", StudioAPI::TransformComponentStruct, setScale); - REGISTER_OBJECT_METHOD("TransformComponent", "void set_rotation(const vec3) property", StudioAPI::TransformComponentStruct, setRotation); - } - - void AngelScriptEngine::registerShaderComponentFunctions() { - REGISTER_OBJECT_METHOD("ShaderComponent", "bool get_hasShader() const property", StudioAPI::ShaderComponentStruct, hasShader); - REGISTER_OBJECT_METHOD("ShaderComponent", "void clear()", StudioAPI::ShaderComponentStruct, clear); - - REGISTER_OBJECT_METHOD("ShaderComponent", "Shader get_shader() const property", StudioAPI::ShaderComponentStruct, getShader); - REGISTER_OBJECT_METHOD("ShaderComponent", "void set_shader(Shader) property", StudioAPI::ShaderComponentStruct, setShader); - - REGISTER_OBJECT_METHOD("ShaderComponent", "Texture get_texture() const property", StudioAPI::ShaderComponentStruct, getTexture); - REGISTER_OBJECT_METHOD("ShaderComponent", "void set_texture(Texture) property", StudioAPI::ShaderComponentStruct, setTexture); - } - - void AngelScriptEngine::registerComponentComponentFunctions() { - REGISTER_OBJECT_METHOD("CameraComponent", "float get_fov() const property", StudioAPI::CameraComponentStruct, getFov); - REGISTER_OBJECT_METHOD("CameraComponent", "float get_aspectRatio() const property", StudioAPI::CameraComponentStruct, getAspectRation); - REGISTER_OBJECT_METHOD("CameraComponent", "float get_nearZ() const property", StudioAPI::CameraComponentStruct, getNearZ); - REGISTER_OBJECT_METHOD("CameraComponent", "float get_farZ() const property", StudioAPI::CameraComponentStruct, getFarZ); - - REGISTER_OBJECT_METHOD("CameraComponent", "void set_fov(float) property", StudioAPI::CameraComponentStruct, setFov); - REGISTER_OBJECT_METHOD("CameraComponent", "void set_aspectRatio(float) property", StudioAPI::CameraComponentStruct, setAspectRation); - REGISTER_OBJECT_METHOD("CameraComponent", "void set_nearZ(float) property", StudioAPI::CameraComponentStruct, setNearZ); - REGISTER_OBJECT_METHOD("CameraComponent", "void set_farZ(float) property", StudioAPI::CameraComponentStruct, setFarZ); - } -} // namespace Deer diff --git a/DeerStudio/src/DeerStudio/AngelScriptEngine/Registers/Math.cpp b/DeerStudio/src/DeerStudio/AngelScriptEngine/Registers/Math.cpp deleted file mode 100644 index f7085b5..0000000 --- a/DeerStudio/src/DeerStudio/AngelScriptEngine/Registers/Math.cpp +++ /dev/null @@ -1,56 +0,0 @@ -#include "DeerStudio/StudioAPI/Math.h" -#include "DeerStudio/AngelScriptEngine/AngelScriptRegisters.h" - -namespace Deer { - void AngelScriptEngine::registerMathStructs() { - AS_CHECK(scriptEngine->RegisterObjectType("vec3", sizeof(glm::vec3), asOBJ_VALUE | asOBJ_POD | asGetTypeTraits() | asOBJ_APP_CLASS_ALLFLOATS)); - AS_CHECK(scriptEngine->RegisterObjectBehaviour("vec3", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(StudioAPI::vec3_constructor), asCALL_CDECL_OBJLAST)); - AS_CHECK(scriptEngine->RegisterObjectBehaviour("vec3", asBEHAVE_CONSTRUCT, "void f(float, float = 0, float = 0)", asFUNCTION(StudioAPI::vec3_constructor_params), asCALL_CDECL_OBJLAST)); - AS_CHECK(scriptEngine->RegisterObjectProperty("vec3", "float x", asOFFSET(glm::vec3, x))); - AS_CHECK(scriptEngine->RegisterObjectProperty("vec3", "float y", asOFFSET(glm::vec3, y))); - AS_CHECK(scriptEngine->RegisterObjectProperty("vec3", "float z", asOFFSET(glm::vec3, z))); - - AS_CHECK(scriptEngine->RegisterObjectType("quat", sizeof(glm::quat), asOBJ_VALUE | asOBJ_POD | asGetTypeTraits() | asOBJ_APP_CLASS_ALLFLOATS)); - AS_CHECK(scriptEngine->RegisterObjectProperty("quat", "float x", asOFFSET(glm::quat, x))); - AS_CHECK(scriptEngine->RegisterObjectProperty("quat", "float y", asOFFSET(glm::quat, y))); - AS_CHECK(scriptEngine->RegisterObjectProperty("quat", "float z", asOFFSET(glm::quat, z))); - AS_CHECK(scriptEngine->RegisterObjectProperty("quat", "float w", asOFFSET(glm::quat, w))); - - AS_CHECK(scriptEngine->RegisterObjectType("Transform", sizeof(TransformComponent), asOBJ_VALUE | asOBJ_POD | asGetTypeTraits())); - AS_CHECK(scriptEngine->RegisterObjectProperty("Transform", "vec3 position", asOFFSET(TransformComponent, position))); - AS_CHECK(scriptEngine->RegisterObjectProperty("Transform", "vec3 scale", asOFFSET(TransformComponent, scale))); - AS_CHECK(scriptEngine->RegisterObjectProperty("Transform", "quat rotation", asOFFSET(TransformComponent, rotation))); - - AS_CHECK(scriptEngine->RegisterObjectType("Camera", sizeof(CameraComponent), asOBJ_VALUE | asOBJ_POD | asGetTypeTraits())); - AS_CHECK(scriptEngine->RegisterObjectProperty("Camera", "float fov", asOFFSET(CameraComponent, fov))); - AS_CHECK(scriptEngine->RegisterObjectProperty("Camera", "float aspect", asOFFSET(CameraComponent, aspect))); - AS_CHECK(scriptEngine->RegisterObjectProperty("Camera", "float nearZ", asOFFSET(CameraComponent, nearZ))); - AS_CHECK(scriptEngine->RegisterObjectProperty("Camera", "float farZ", asOFFSET(CameraComponent, farZ))); - - AS_CHECK(scriptEngine->RegisterObjectType("WorldCamera", sizeof(WorldCamera), asOBJ_VALUE | asOBJ_POD | asGetTypeTraits())); - AS_CHECK(scriptEngine->RegisterObjectProperty("WorldCamera", "Camera camera", asOFFSET(WorldCamera, camera))); - AS_CHECK(scriptEngine->RegisterObjectProperty("WorldCamera", "Transform transform", asOFFSET(WorldCamera, transform))); - } - - void AngelScriptEngine::registerMathFunctions() { - REGISTER_EXT_OBJECT_METHOD("vec3", "vec3 opAdd(const vec3 &in)", StudioAPI::vec3_add); - REGISTER_EXT_OBJECT_METHOD("vec3", "vec3 opSub(const vec3 &in) const", StudioAPI::vec3_sub); - REGISTER_EXT_OBJECT_METHOD("vec3", "vec3 opNeg() const", StudioAPI::vec3_neg); - REGISTER_EXT_OBJECT_METHOD("vec3", "vec3 opMul(float) const", StudioAPI::vec3_mult); - REGISTER_EXT_OBJECT_METHOD("vec3", "vec3 opMul_r(float) const", StudioAPI::vec3_mult); - - REGISTER_EXT_OBJECT_CONSTRUCTOR("quat", "void f()", StudioAPI::quat_construct); - REGISTER_EXT_OBJECT_CONSTRUCTOR("quat", "void f(float, float, float, float)", StudioAPI::quat_constructFromValue); - REGISTER_EXT_OBJECT_DESTRUCTOR("quat", "void f()", StudioAPI::quat_destruct); - - REGISTER_EXT_OBJECT_METHOD("quat", "quat opMul(const quat &in) const", StudioAPI::quat_multiply); - REGISTER_EXT_OBJECT_METHOD("quat", "vec3 getEuler() const", StudioAPI::quat_getEuler); - REGISTER_EXT_OBJECT_METHOD("quat", "void setEuler(vec3)", StudioAPI::quat_setEuler); - - REGISTER_EXT_OBJECT_CONSTRUCTOR("Transform", "void f()", StudioAPI::transform_construct); - REGISTER_EXT_OBJECT_CONSTRUCTOR("Camera", "void f()", StudioAPI::camera_construct); - REGISTER_EXT_OBJECT_CONSTRUCTOR("WorldCamera", "void f()", StudioAPI::sceneCamera_Construct); - - REGISTER_EXT_OBJECT_METHOD("Transform", "vec3 relative(vec3)", StudioAPI::transform_relative); - } -} // namespace Deer diff --git a/DeerStudio/src/DeerStudio/AngelScriptEngine/Service.cpp b/DeerStudio/src/DeerStudio/AngelScriptEngine/Service.cpp index 0660391..4aac3b7 100644 --- a/DeerStudio/src/DeerStudio/AngelScriptEngine/Service.cpp +++ b/DeerStudio/src/DeerStudio/AngelScriptEngine/Service.cpp @@ -8,7 +8,7 @@ #include namespace Deer { - namespace AngelScriptEngine { + namespace StudioAPI { void Service::init() { asIScriptFunction* constructorFunction = getFactory(type); @@ -294,5 +294,5 @@ namespace Deer { } Service::~Service() {} - } // namespace AngelScriptEngine + } // namespace StudioAPI } // namespace Deer \ No newline at end of file diff --git a/DeerStudio/src/DeerStudio/DeerStudio.cpp b/DeerStudio/src/DeerStudio/DeerStudio.cpp index e1a9202..8dcb6a3 100644 --- a/DeerStudio/src/DeerStudio/DeerStudio.cpp +++ b/DeerStudio/src/DeerStudio/DeerStudio.cpp @@ -1,60 +1,62 @@ #include "DeerStudio/DeerStudio.h" -#include "DeerStudio/AngelScriptEngine.h" +// #include "DeerStudio/AngelScriptEngine.h" -#include "DeerRender/DataStore.h" -#include "DeerRender/Tools/Path.h" +#include "DeerRender/Engine.h" +#include "DeerRender/Universe.h" #include "DeerRender/World.h" -#include "DeerRender/Application.h" - // TMP -#include "DeerRender/Mesh.h" -#include "DeerRender/Resource.h" #include "DeerStudio/Fonts.h" #include "DeerStudio/Style.h" // TMP -#include "DeerStudio/StudioAPI/UI.h" +#include "DeerStudio/StudioAPI.h" namespace Deer { + class FrameBuffer; + namespace DeerStudio { + Universe::WorldHandle mainWorld; + + void onUpdate(); + void onRender(); + void onEvent(Event& e); + bool onWindowCloseEvent(WindowCloseEvent&); + + void worldRender(World& world); + void worldUpdate(World& world); + void main() { - Log::init(); - Application::initWindow(); + Engine::init(); + Engine::setEventCallback(DeerStudio::onEvent); - // DataStore::loadVoxelsData(); - // DataStore::loadVoxelsAspect(); - // DataStore::generateTextureAtlas(); - // DataStore::loadVoxelsShaders(); + StudioAPI::registerStudioAPI(); + ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(20, 20)); initializeFonts(); SetupImGuiStyle(); - AngelScriptEngine::initialize(); - RenderUtils::initializeRenderUtils(); + WorldSettings worldSettings = { + .updateCallback = worldUpdate, + .updateFrequency = 60, + .renderCallback = worldRender, + .renderFrequency = 144, + }; - ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(20, 20)); + mainWorld = Universe::createWorld(worldSettings); + Universe::getWorld(mainWorld).execute(); - Application::setTickCallback(DeerStudio::onUpdate); - Application::setRenderCallback(DeerStudio::onRender); - Application::setEventCallback(DeerStudio::onEvent); + StudioAPI::deinitialize(); + Engine::shutdown(); + } // namespace DeerStudio - Application::run(); - - AngelScriptEngine::deinitialize(); - RenderUtils::deinitializeRenderUtils(); - - World::clear(); - - Application::shutdownWindow(); - Log::shutdown(); + void worldUpdate(World& world) { + StudioAPI::update(); } - void onUpdate() { - AngelScriptEngine::update(); - } + void worldRender(World& world) { + Engine::beginRender(); - void onRender() { static bool opt_fullscreen = true; static bool opt_padding = false; static ImGuiDockNodeFlags dockspace_flags = ImGuiDockNodeFlags_None; @@ -102,8 +104,10 @@ namespace Deer { // TerrainEditor::onImGui(); // viewport_onImGui(); - AngelScriptEngine::render(); + StudioAPI::render(); ImGui::End(); + + Engine::endRender(); } void onEvent(Event& e) { @@ -113,7 +117,7 @@ namespace Deer { } bool onWindowCloseEvent(WindowCloseEvent&) { - Application::shutdown(); + Universe::getWorld(mainWorld).stopExecution(); return true; } } // namespace DeerStudio diff --git a/DeerStudio/src/DeerStudio/Fonts.cpp b/DeerStudio/src/DeerStudio/Fonts.cpp index 8cc92d9..ba9dd8f 100644 --- a/DeerStudio/src/DeerStudio/Fonts.cpp +++ b/DeerStudio/src/DeerStudio/Fonts.cpp @@ -1,5 +1,4 @@ #include "Fonts.h" -#include "DeerRender/DataStore.h" namespace Deer { ImFont* normalText; @@ -20,30 +19,29 @@ namespace Deer { config.MergeMode = true; config.PixelSnapH = true; - ImWchar icons_ranges[] = { 0xf000, 0xf3ff, 0 }; - // Merge into normalText (18 px) - { - ImFontConfig cfg; - cfg.MergeMode = true; + ImWchar icons_ranges[] = {0xf000, 0xf3ff, 0}; + // Merge into normalText (18 px) + { + ImFontConfig cfg; + cfg.MergeMode = true; cfg.GlyphRanges = icons_ranges; cfg.GlyphMinAdvanceX = 18.0f; - io.Fonts->AddFontFromFileTTF(faPath.generic_string().c_str(), 14, &cfg); - io.Fonts->AddFontFromFileTTF(faNPath.generic_string().c_str(), 14, &cfg); - } + io.Fonts->AddFontFromFileTTF(faPath.generic_string().c_str(), 14, &cfg); + io.Fonts->AddFontFromFileTTF(faNPath.generic_string().c_str(), 14, &cfg); + } titleText = io.Fonts->AddFontFromFileTTF(rfPath.generic_string().c_str(), 32); - // Merge into titleText (32 px) - { - ImFontConfig cfg; - cfg.MergeMode = true; + // Merge into titleText (32 px) + { + ImFontConfig cfg; + cfg.MergeMode = true; cfg.GlyphRanges = icons_ranges; cfg.GlyphMinAdvanceX = 20.0f; - io.Fonts->AddFontFromFileTTF(faPath.generic_string().c_str(), 20, &cfg); - io.Fonts->AddFontFromFileTTF(faNPath.generic_string().c_str(), 20, &cfg); - } + io.Fonts->AddFontFromFileTTF(faPath.generic_string().c_str(), 20, &cfg); + io.Fonts->AddFontFromFileTTF(faNPath.generic_string().c_str(), 20, &cfg); + } - - io.Fonts->AddFontFromFileTTF(faPath.generic_string().c_str(), 18); + io.Fonts->AddFontFromFileTTF(faPath.generic_string().c_str(), 18); io.Fonts->Build(); } diff --git a/DeerStudio/src/DeerStudio/Registers/Engine.cpp b/DeerStudio/src/DeerStudio/Registers/Engine.cpp new file mode 100644 index 0000000..f3c7363 --- /dev/null +++ b/DeerStudio/src/DeerStudio/Registers/Engine.cpp @@ -0,0 +1,38 @@ +#include "DeerStudio/StudioAPI/Engine.h" +#include "DeerStudio/Registers/Registers.h" +#include "DeerStudio/StudioAPI.h" +#include "DeerStudio/StudioAPI/Debug.h" +#include "DeerStudio/StudioAPI/Resources.h" + +namespace Deer { + void StudioAPI::registerEngineFunctions() { + asIScriptEngine* scriptEngine = engineRegistry->bind(); + + REGISTER_GLOBAL_FUNC(engine, "void print(const string&in text)", StudioAPI::print); + REGISTER_GLOBAL_FUNC(engine, "string getParent(const string&in path)", StudioAPI::getParentPath); + REGISTER_GLOBAL_FUNC(engine, "string getParentName(const string&in path)", StudioAPI::getParentPathName); + REGISTER_GLOBAL_FUNC(engine, "string getName(const string&in path)", StudioAPI::getPathName); + REGISTER_GLOBAL_FUNC(engine, "array@ divide(const string&in path)", StudioAPI::dividePath_angelscript); + engineRegistry->unbind() + } + + void StudioAPI::registerEngineStructs() { + asIScriptEngine* scriptEngine = engineRegistry->bindNoNamespace(); + AS_RET_CHECK(scriptEngine->RegisterInterface("Panel")); + AS_RET_CHECK(scriptEngine->RegisterInterfaceMethod("Panel", "void render()")); + AS_RET_CHECK(scriptEngine->RegisterInterface("Service")); + + AS_CHECK(scriptEngine->RegisterFuncdef("void SimpleFunction()")); + AS_CHECK(scriptEngine->RegisterFuncdef("void ReciverFunction(any@)")); + AS_CHECK(scriptEngine->RegisterFuncdef("void TransferFunction(any@, any@)")); + engineRegistry->unbind(); + } + + void StudioAPI::extractBaseTypes() { + asIScriptEngine* scriptEngine = engineRegistry->bindNoNamespace(); + panelBaseType = scriptEngine->GetTypeInfoByDecl("Panel"); + serviceBaseType = scriptEngine->GetTypeInfoByDecl("Service"); + arrayStringBaseType = scriptEngine->GetTypeInfoByDecl("array"); + engineRegistry->unbind(); + } +} // namespace Deer \ No newline at end of file diff --git a/DeerStudio/src/DeerStudio/Registers/Entity.cpp b/DeerStudio/src/DeerStudio/Registers/Entity.cpp new file mode 100644 index 0000000..d3834d0 --- /dev/null +++ b/DeerStudio/src/DeerStudio/Registers/Entity.cpp @@ -0,0 +1,111 @@ +#include "DeerStudio/StudioAPI/Entity.h" +#include "DeerStudio/Registers/Registers.h" +#include "DeerStudio/StudioAPI.h" +#include "DeerStudio/StudioAPI/Resources.h" + +namespace Deer { + namespace StudioAPI { + void registerTransformComponentFunctions(); + void registerMeshComponentFunction(); + void registerShaderComponentFunctions(); + void registerComponentComponentFunctions(); + } // namespace StudioAPI + + void StudioAPI::registerEntityStructs() { + asIScriptEngine* scriptEngine = entityRegistry->bindNoNamespace(); + AS_CHECK(scriptEngine->RegisterObjectType("Entity", sizeof(StudioAPI::EntityHandleStruct), asOBJ_VALUE | asOBJ_POD | asGetTypeTraits() | asOBJ_APP_CLASS_ALLINTS)); + AS_CHECK(scriptEngine->RegisterObjectType("EntityChilds", sizeof(StudioAPI::EntityHandleStruct), asOBJ_VALUE | asOBJ_POD | asGetTypeTraits() | asOBJ_APP_CLASS_ALLINTS)); + AS_CHECK(scriptEngine->RegisterObjectType("TransformComponent", sizeof(StudioAPI::EntityHandleStruct), asOBJ_VALUE | asOBJ_POD | asGetTypeTraits() | asOBJ_APP_CLASS_ALLINTS)); + AS_CHECK(scriptEngine->RegisterObjectType("MeshComponent", sizeof(StudioAPI::EntityHandleStruct), asOBJ_VALUE | asOBJ_POD | asGetTypeTraits() | asOBJ_APP_CLASS_ALLINTS)); + AS_CHECK(scriptEngine->RegisterObjectType("ShaderComponent", sizeof(StudioAPI::EntityHandleStruct), asOBJ_VALUE | asOBJ_POD | asGetTypeTraits() | asOBJ_APP_CLASS_ALLINTS)); + AS_CHECK(scriptEngine->RegisterObjectType("CameraComponent", sizeof(StudioAPI::EntityHandleStruct), asOBJ_VALUE | asOBJ_POD | asGetTypeTraits() | asOBJ_APP_CLASS_ALLINTS)); + entityRegistry->unbind(); + } + + void StudioAPI::registerEntityFunctions() { + registerTransformComponentFunctions(); + registerMeshComponentFunction(); + registerShaderComponentFunctions(); + registerComponentComponentFunctions(); + + asIScriptEngine* scriptEngine = entityRegistry->bindNoNamespace(); + REGISTER_OBJECT_METHOD(scriptEngine, "Entity", "string get_name() const property", StudioAPI::EntityStruct, getName); + REGISTER_OBJECT_METHOD(scriptEngine, "Entity", "void set_name(string& in) property", StudioAPI::EntityStruct, setName); + REGISTER_OBJECT_METHOD(scriptEngine, "Entity", "int get_id() const property", StudioAPI::EntityStruct, getId); + REGISTER_OBJECT_METHOD(scriptEngine, "Entity", "Entity createChild(const string& in)", StudioAPI::EntityStruct, createChild); + REGISTER_OBJECT_METHOD(scriptEngine, "Entity", "bool get_isRoot() const property", StudioAPI::EntityStruct, isRoot); + REGISTER_OBJECT_METHOD(scriptEngine, "Entity", "void destroy()", StudioAPI::EntityStruct, destroy); + REGISTER_OBJECT_METHOD(scriptEngine, "Entity", "bool get_exists() const property", StudioAPI::EntityStruct, exists); + REGISTER_OBJECT_METHOD(scriptEngine, "Entity", "Entity get_parent() property", StudioAPI::EntityStruct, getParent); + REGISTER_OBJECT_METHOD(scriptEngine, "Entity", "void set_parent(Entity) property", StudioAPI::EntityStruct, setParent); + REGISTER_OBJECT_METHOD(scriptEngine, "Entity", "bool isDescendantOf(Entity)", StudioAPI::EntityStruct, isDescendantOf); + REGISTER_OBJECT_METHOD(scriptEngine, "Entity", "bool opEquals(const Entity &in) const", StudioAPI::EntityStruct, opEquals); + REGISTER_OBJECT_METHOD(scriptEngine, "Entity", "EntityChilds get_childs() const property", StudioAPI::EntityStruct, getSelf); + REGISTER_OBJECT_METHOD(scriptEngine, "Entity", "TransformComponent get_transform() const property", StudioAPI::EntityStruct, getSelf); + REGISTER_OBJECT_METHOD(scriptEngine, "Entity", "MeshComponent getMeshComponent()", StudioAPI::EntityStruct, getMeshComponent); + REGISTER_OBJECT_METHOD(scriptEngine, "Entity", "MeshComponent createMeshComponent()", StudioAPI::EntityStruct, createMeshComponent); + REGISTER_OBJECT_METHOD(scriptEngine, "Entity", "bool hasMeshComponent()", StudioAPI::EntityStruct, hasMeshComponent); + REGISTER_OBJECT_METHOD(scriptEngine, "Entity", "void removeMeshComponent()", StudioAPI::EntityStruct, removeMeshComponent); + REGISTER_OBJECT_METHOD(scriptEngine, "Entity", "ShaderComponent getShaderComponent()", StudioAPI::EntityStruct, getShaderComponent); + REGISTER_OBJECT_METHOD(scriptEngine, "Entity", "ShaderComponent createShaderComponent()", StudioAPI::EntityStruct, createShaderComponent); + REGISTER_OBJECT_METHOD(scriptEngine, "Entity", "bool hasShaderComponent()", StudioAPI::EntityStruct, hasShaderComponent); + REGISTER_OBJECT_METHOD(scriptEngine, "Entity", "void removeShaderComponent()", StudioAPI::EntityStruct, removeShaderComponent); + REGISTER_OBJECT_METHOD(scriptEngine, "Entity", "CameraComponent getCameraComponent()", StudioAPI::EntityStruct, getCameraComponent); + REGISTER_OBJECT_METHOD(scriptEngine, "Entity", "CameraComponent createCameraComponent()", StudioAPI::EntityStruct, createCameraComponent); + REGISTER_OBJECT_METHOD(scriptEngine, "Entity", "bool hasCameraComponent()", StudioAPI::EntityStruct, hasCameraComponent); + REGISTER_OBJECT_METHOD(scriptEngine, "Entity", "void removeCameraComponent()", StudioAPI::EntityStruct, removeCameraComponent); + + REGISTER_OBJECT_METHOD(scriptEngine, "EntityChilds", "int get_count() const property", StudioAPI::EntityChildArrayStruct, getChildCount); + REGISTER_OBJECT_METHOD(scriptEngine, "EntityChilds", "Entity opIndex(int) const", StudioAPI::EntityChildArrayStruct, getChild); + + scriptEngine = entityRegistry->bind(); + REGISTER_GLOBAL_FUNC(scriptEngine, "Entity getRoot()", StudioAPI::getRoot); + entityRegistry->unbind(); + } + + void StudioAPI::registerMeshComponentFunction() { + asIScriptEngine* scriptEngine = entityRegistry->bindNoNamespace(); + REGISTER_OBJECT_METHOD(scriptEngine, "MeshComponent", "bool get_isActive() const property", StudioAPI::MeshComponentStruct, isActive); + REGISTER_OBJECT_METHOD(scriptEngine, "MeshComponent", "bool get_hasMesh() const property", StudioAPI::MeshComponentStruct, hasMesh); + REGISTER_OBJECT_METHOD(scriptEngine, "MeshComponent", "void clear()", StudioAPI::MeshComponentStruct, clear); + REGISTER_OBJECT_METHOD(scriptEngine, "MeshComponent", "GPUMesh get_meshResource() property", StudioAPI::MeshComponentStruct, getMesh); + REGISTER_OBJECT_METHOD(scriptEngine, "MeshComponent", "void set_meshResource(GPUMesh) property", StudioAPI::MeshComponentStruct, setMesh); + REGISTER_OBJECT_METHOD(scriptEngine, "MeshComponent", "void set_isActive(const bool) const property", StudioAPI::MeshComponentStruct, setActive); + entityRegistry->unbind(); + } + + void StudioAPI::registerTransformComponentFunctions() { + asIScriptEngine* scriptEngine = entityRegistry->bindNoNamespace(); + REGISTER_OBJECT_METHOD(scriptEngine, "TransformComponent", "vec3 get_position() const property", StudioAPI::TransformComponentStruct, getPosition); + REGISTER_OBJECT_METHOD(scriptEngine, "TransformComponent", "vec3 get_scale() const property", StudioAPI::TransformComponentStruct, getScale); + REGISTER_OBJECT_METHOD(scriptEngine, "TransformComponent", "vec3 get_rotation() const property", StudioAPI::TransformComponentStruct, getRotation); + REGISTER_OBJECT_METHOD(scriptEngine, "TransformComponent", "void set_position(const vec3) property", StudioAPI::TransformComponentStruct, setPosition); + REGISTER_OBJECT_METHOD(scriptEngine, "TransformComponent", "void set_scale(const vec3) property", StudioAPI::TransformComponentStruct, setScale); + REGISTER_OBJECT_METHOD(scriptEngine, "TransformComponent", "void set_rotation(const vec3) property", StudioAPI::TransformComponentStruct, setRotation); + entityRegistry->unbind(); + } + + void StudioAPI::registerShaderComponentFunctions() { + asIScriptEngine* scriptEngine = entityRegistry->bindNoNamespace(); + REGISTER_OBJECT_METHOD(scriptEngine, "ShaderComponent", "bool get_hasShader() const property", StudioAPI::ShaderComponentStruct, hasShader); + REGISTER_OBJECT_METHOD(scriptEngine, "ShaderComponent", "void clear()", StudioAPI::ShaderComponentStruct, clear); + REGISTER_OBJECT_METHOD(scriptEngine, "ShaderComponent", "Shader get_shader() const property", StudioAPI::ShaderComponentStruct, getShader); + REGISTER_OBJECT_METHOD(scriptEngine, "ShaderComponent", "void set_shader(Shader) property", StudioAPI::ShaderComponentStruct, setShader); + REGISTER_OBJECT_METHOD(scriptEngine, "ShaderComponent", "Texture get_texture() const property", StudioAPI::ShaderComponentStruct, getTexture); + REGISTER_OBJECT_METHOD(scriptEngine, "ShaderComponent", "void set_texture(Texture) property", StudioAPI::ShaderComponentStruct, setTexture); + entityRegistry->unbind(); + } + + void StudioAPI::registerComponentComponentFunctions() { + asIScriptEngine* scriptEngine = entityRegistry->bindNoNamespace(); + REGISTER_OBJECT_METHOD(scriptEngine, "CameraComponent", "float get_fov() const property", StudioAPI::CameraComponentStruct, getFov); + REGISTER_OBJECT_METHOD(scriptEngine, "CameraComponent", "float get_aspectRatio() const property", StudioAPI::CameraComponentStruct, getAspectRation); + REGISTER_OBJECT_METHOD(scriptEngine, "CameraComponent", "float get_nearZ() const property", StudioAPI::CameraComponentStruct, getNearZ); + REGISTER_OBJECT_METHOD(scriptEngine, "CameraComponent", "float get_farZ() const property", StudioAPI::CameraComponentStruct, getFarZ); + REGISTER_OBJECT_METHOD(scriptEngine, "CameraComponent", "void set_fov(float) property", StudioAPI::CameraComponentStruct, setFov); + REGISTER_OBJECT_METHOD(scriptEngine, "CameraComponent", "void set_aspectRatio(float) property", StudioAPI::CameraComponentStruct, setAspectRation); + REGISTER_OBJECT_METHOD(scriptEngine, "CameraComponent", "void set_nearZ(float) property", StudioAPI::CameraComponentStruct, setNearZ); + REGISTER_OBJECT_METHOD(scriptEngine, "CameraComponent", "void set_farZ(float) property", StudioAPI::CameraComponentStruct, setFarZ); + entityRegistry->unbind(); + } +} // namespace Deer diff --git a/DeerStudio/src/DeerStudio/AngelScriptEngine/Registers/Environment.cpp b/DeerStudio/src/DeerStudio/Registers/Environment.cpp similarity index 83% rename from DeerStudio/src/DeerStudio/AngelScriptEngine/Registers/Environment.cpp rename to DeerStudio/src/DeerStudio/Registers/Environment.cpp index 4b0ddd0..dded69a 100644 --- a/DeerStudio/src/DeerStudio/AngelScriptEngine/Registers/Environment.cpp +++ b/DeerStudio/src/DeerStudio/Registers/Environment.cpp @@ -1,15 +1,18 @@ #include "DeerStudio/AngelScriptEngine/AngelScriptRegisters.h" +#include "DeerStudio/Registers/Registers.h" +#include "DeerStudio/StudioAPI.h" #include "DeerStudio/StudioAPI/EntityEnvironment.h" +#include "DeerStudio/StudioAPI/Resources.h" namespace Deer { void registerEntityEnvironmentStructs(); void registerEntityEnvironmentFunctions(); - void AngelScriptEngine::registerEntityEnvironmentStructs() { + void StudioAPI::registerEntityEnvironmentStructs() { AS_CHECK(scriptEngine->RegisterObjectType("EntityEnvironment", sizeof(StudioAPI::EntityEnvironmentStruct), asOBJ_VALUE | asOBJ_POD | asGetTypeTraits() | asOBJ_APP_CLASS_ALLINTS)); } - void AngelScriptEngine::registerEntityEnvironmentFunctions() { + void StudioAPI::registerEntityEnvironmentFunctions() { scriptEngine->SetDefaultNamespace("Resource"); REGISTER_GLOBAL_FUNC("EntityEnvironment getMainEntityEnvironment()", StudioAPI::getMainEntityEnvironment); REGISTER_GLOBAL_FUNC("EntityEnvironment createLoadEntityEnvironment(const string&in envId)", StudioAPI::createLoadEntityEnvironment); diff --git a/DeerStudio/src/DeerStudio/AngelScriptEngine/Registers/FrameBuffer.cpp b/DeerStudio/src/DeerStudio/Registers/FrameBuffer.cpp similarity index 95% rename from DeerStudio/src/DeerStudio/AngelScriptEngine/Registers/FrameBuffer.cpp rename to DeerStudio/src/DeerStudio/Registers/FrameBuffer.cpp index 98d152c..10aab63 100644 --- a/DeerStudio/src/DeerStudio/AngelScriptEngine/Registers/FrameBuffer.cpp +++ b/DeerStudio/src/DeerStudio/Registers/FrameBuffer.cpp @@ -1,5 +1,5 @@ #include "DeerStudio/StudioAPI/FrameBuffer.h" -#include "DeerStudio/AngelScriptEngine/AngelScriptRegisters.h" +#include "DeerStudio/StudioAPI/Resources.h" namespace Deer { void AngelScriptEngine::registerFrameBufferStructs() { diff --git a/DeerStudio/src/DeerStudio/Registers/Math.cpp b/DeerStudio/src/DeerStudio/Registers/Math.cpp new file mode 100644 index 0000000..a7649fb --- /dev/null +++ b/DeerStudio/src/DeerStudio/Registers/Math.cpp @@ -0,0 +1,64 @@ +#include "DeerStudio/StudioAPI/Math.h" +#include "DeerStudio/Registers/Registers.h" +#include "DeerStudio/StudioAPI.h" +#include "DeerStudio/StudioAPI/Resources.h" + +namespace Deer { + void StudioAPI::registerMathStructs() { + asIScriptEngine* scriptEngine = mathRegistry->bindNoNamespace(); + + scriptEngine->RegisterObjectBehaviour("vec3", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(StudioAPI::vec3_constructor), asCALL_CDECL_OBJLAST); + scriptEngine->RegisterObjectBehaviour("vec3", asBEHAVE_CONSTRUCT, "void f(float, float = 0, float = 0)", asFUNCTION(StudioAPI::vec3_constructor_params), asCALL_CDECL_OBJLAST); + scriptEngine->RegisterObjectProperty("vec3", "float x", asOFFSET(glm::vec3, x)); + scriptEngine->RegisterObjectProperty("vec3", "float y", asOFFSET(glm::vec3, y)); + scriptEngine->RegisterObjectProperty("vec3", "float z", asOFFSET(glm::vec3, z)); + + scriptEngine->RegisterObjectType("quat", sizeof(glm::quat), asOBJ_VALUE | asOBJ_POD | asGetTypeTraits() | asOBJ_APP_CLASS_ALLFLOATS); + scriptEngine->RegisterObjectProperty("quat", "float x", asOFFSET(glm::quat, x)); + scriptEngine->RegisterObjectProperty("quat", "float y", asOFFSET(glm::quat, y)); + scriptEngine->RegisterObjectProperty("quat", "float z", asOFFSET(glm::quat, z)); + scriptEngine->RegisterObjectProperty("quat", "float w", asOFFSET(glm::quat, w)); + + scriptEngine->RegisterObjectType("Transform", sizeof(TransformComponent), asOBJ_VALUE | asOBJ_POD | asGetTypeTraits()); + scriptEngine->RegisterObjectProperty("Transform", "vec3 position", asOFFSET(TransformComponent, position)); + scriptEngine->RegisterObjectProperty("Transform", "vec3 scale", asOFFSET(TransformComponent, scale)); + scriptEngine->RegisterObjectProperty("Transform", "quat rotation", asOFFSET(TransformComponent, rotation)); + + scriptEngine->RegisterObjectType("Camera", sizeof(CameraComponent), asOBJ_VALUE | asOBJ_POD | asGetTypeTraits()); + scriptEngine->RegisterObjectProperty("Camera", "float fov", asOFFSET(CameraComponent, fov)); + scriptEngine->RegisterObjectProperty("Camera", "float aspect", asOFFSET(CameraComponent, aspect)); + scriptEngine->RegisterObjectProperty("Camera", "float nearZ", asOFFSET(CameraComponent, nearZ)); + scriptEngine->RegisterObjectProperty("Camera", "float farZ", asOFFSET(CameraComponent, farZ)); + + scriptEngine->RegisterObjectType("WorldCamera", sizeof(WorldCamera), asOBJ_VALUE | asOBJ_POD | asGetTypeTraits()); + scriptEngine->RegisterObjectProperty("WorldCamera", "Camera camera", asOFFSET(WorldCamera, camera)); + scriptEngine->RegisterObjectProperty("WorldCamera", "Transform transform", asOFFSET(WorldCamera, transform)); + + mathRegistry->unbind(); + } + + void StudioAPI::registerMathFunctions() { + asIScriptEngine* scriptEngine = mathRegistry->bindNoNamespace(); + mathRegistry->bindNoNamespace(); + REGISTER_EXT_OBJECT_METHOD("vec3", "vec3 opAdd(const vec3 &in)", StudioAPI::vec3_add); + REGISTER_EXT_OBJECT_METHOD("vec3", "vec3 opSub(const vec3 &in) const", StudioAPI::vec3_sub); + REGISTER_EXT_OBJECT_METHOD("vec3", "vec3 opNeg() const", StudioAPI::vec3_neg); + REGISTER_EXT_OBJECT_METHOD("vec3", "vec3 opMul(float) const", StudioAPI::vec3_mult); + REGISTER_EXT_OBJECT_METHOD("vec3", "vec3 opMul_r(float) const", StudioAPI::vec3_mult); + + REGISTER_EXT_OBJECT_CONSTRUCTOR("quat", "void f()", StudioAPI::quat_construct); + REGISTER_EXT_OBJECT_CONSTRUCTOR("quat", "void f(float, float, float, float)", StudioAPI::quat_constructFromValue); + REGISTER_EXT_OBJECT_DESTRUCTOR("quat", "void f()", StudioAPI::quat_destruct); + + REGISTER_EXT_OBJECT_METHOD("quat", "quat opMul(const quat &in) const", StudioAPI::quat_multiply); + REGISTER_EXT_OBJECT_METHOD("quat", "vec3 getEuler() const", StudioAPI::quat_getEuler); + REGISTER_EXT_OBJECT_METHOD("quat", "void setEuler(vec3)", StudioAPI::quat_setEuler); + + REGISTER_EXT_OBJECT_CONSTRUCTOR("Transform", "void f()", StudioAPI::transform_construct); + REGISTER_EXT_OBJECT_CONSTRUCTOR("Camera", "void f()", StudioAPI::camera_construct); + REGISTER_EXT_OBJECT_CONSTRUCTOR("WorldCamera", "void f()", StudioAPI::sceneCamera_Construct); + + REGISTER_EXT_OBJECT_METHOD("Transform", "vec3 relative(vec3)", StudioAPI::transform_relative); + mathRegistry->unbind(); + } +} // namespace Deer diff --git a/DeerStudio/src/DeerStudio/Registers/Registers.cpp b/DeerStudio/src/DeerStudio/Registers/Registers.cpp new file mode 100644 index 0000000..1c441c3 --- /dev/null +++ b/DeerStudio/src/DeerStudio/Registers/Registers.cpp @@ -0,0 +1,41 @@ +#pragma once +#include "DeerStudio/StudioAPI.h" +#include "DeerStudio/StudioAPI/Resources.h" + +namespace Deer { + namespace StudioAPI { + ScriptSystem::Registry* engineRegistry = nullptr; + ScriptSystem::Registry* uiRegistry = nullptr; + ScriptSystem::Registry* entityRegistry = nullptr; + ScriptSystem::Registry* resourceRegistry = nullptr; + ScriptSystem::Registry* math = nullptr; + } // namespace StudioAPI + + void StudioAPI::registerStudioAPI() { + uiRegistry = ScriptSystem::createRegistry("UI"); + entityRegistry = ScriptSystem::createRegistry("Entity"); + + registerStructs(); + registerFunctions(); + } + + void StudioAPI::registerStructs() { + registerMathStructs(); + registerUIStructs(); + registerResourceStructs(); + registerEntityEnvironmentStructs(); + registerEntityStructs(); + registerEngineStructs(); + registerFrameBufferStructs(); + } + + void StudioAPI::registerFunctions() { + registerUIFunctions(); + registerMathFunctions(); + registerResourceFunctions(); + registerEntityEnvironmentFunctions(); + registerEntityFunctions(); + registerEngineFunctions(); + registerFrameBufferFunctions(); + } +} // namespace Deer \ No newline at end of file diff --git a/DeerStudio/src/DeerStudio/AngelScriptEngine/AngelScriptRegisters.h b/DeerStudio/src/DeerStudio/Registers/Registers.h similarity index 54% rename from DeerStudio/src/DeerStudio/AngelScriptEngine/AngelScriptRegisters.h rename to DeerStudio/src/DeerStudio/Registers/Registers.h index 70f77c8..e7790f2 100644 --- a/DeerStudio/src/DeerStudio/AngelScriptEngine/AngelScriptRegisters.h +++ b/DeerStudio/src/DeerStudio/Registers/Registers.h @@ -4,32 +4,35 @@ #include "DeerStudio/AngelScriptEngine.h" #include "DeerStudio/AngelScriptEngine/ErrorHandle.h" -#define REGISTER_GLOBAL_FUNC(funcdef, func) \ - AS_CHECK(Deer::AngelScriptEngine::scriptEngine->RegisterGlobalFunction( \ - funcdef, \ +#define REGISTER_GLOBAL_FUNC(scriptEngine, funcdef, func) \ + AS_CHECK(scriptEngine->RegisterGlobalFunction( \ + funcdef, \ asFUNCTION(func), asCALL_CDECL)) -#define REGISTER_OBJECT_METHOD(clasdef, funcdef, clas, func) \ - AS_CHECK(Deer::AngelScriptEngine::scriptEngine->RegisterObjectMethod( \ - clasdef, funcdef, \ +#define REGISTER_OBJECT_METHOD(scriptEngine, clasdef, funcdef, clas, func) \ + AS_CHECK(scriptEngine->RegisterObjectMethod( \ + clasdef, funcdef, \ asMETHOD(clas, func), asCALL_THISCALL)) -#define REGISTER_EXT_OBJECT_METHOD(clasdef, funcdef, func) \ - AS_CHECK(Deer::AngelScriptEngine::scriptEngine->RegisterObjectMethod( \ - clasdef, funcdef, \ +#define REGISTER_EXT_OBJECT_METHOD(scriptEngine, clasdef, funcdef, func) \ + AS_CHECK(scriptEngine->RegisterObjectMethod( \ + clasdef, funcdef, \ asFUNCTION(func), asCALL_CDECL_OBJLAST)) -#define REGISTER_EXT_OBJECT_CONSTRUCTOR(clasdef, funcdef, func) \ - AS_CHECK(Deer::AngelScriptEngine::scriptEngine->RegisterObjectBehaviour( \ - clasdef, asBEHAVE_CONSTRUCT, funcdef, \ +#define REGISTER_EXT_OBJECT_CONSTRUCTOR(scriptEngine, clasdef, funcdef, func) \ + AS_CHECK(scriptEngine->RegisterObjectBehaviour( \ + clasdef, asBEHAVE_CONSTRUCT, funcdef, \ asFUNCTION(func), asCALL_CDECL_OBJLAST)) -#define REGISTER_EXT_OBJECT_DESTRUCTOR(clasdef, funcdef, func) \ - AS_CHECK(Deer::AngelScriptEngine::scriptEngine->RegisterObjectBehaviour( \ +#define REGISTER_EXT_OBJECT_DESTRUCTOR(scriptEngine, clasdef, funcdef, func) \ + AS_CHECK(scriptEngine->RegisterObjectBehaviour( \ clasdef, asBEHAVE_DESTRUCT, funcdef, \ asFUNCTION(func), asCALL_CDECL_OBJLAST)) namespace Deer { - namespace AngelScriptEngine { + namespace StudioAPI { + void registerStructs(); + void registerFunctions(); + void registerUIFunctions(); void registerMathFunctions(); void registerResourceFunctions(); @@ -45,6 +48,5 @@ namespace Deer { void registerEntityStructs(); void registerEngineStructs(); void registerFrameBufferStructs(); - - } // namespace AngelScriptEngine -} // namespace Deer + } // namespace StudioAPI +} // namespace Deer \ No newline at end of file diff --git a/DeerStudio/src/DeerStudio/AngelScriptEngine/Registers/Resource.cpp b/DeerStudio/src/DeerStudio/Registers/Resource.cpp similarity index 91% rename from DeerStudio/src/DeerStudio/AngelScriptEngine/Registers/Resource.cpp rename to DeerStudio/src/DeerStudio/Registers/Resource.cpp index 2e784e6..58be286 100644 --- a/DeerStudio/src/DeerStudio/AngelScriptEngine/Registers/Resource.cpp +++ b/DeerStudio/src/DeerStudio/Registers/Resource.cpp @@ -1,10 +1,9 @@ -#include "DeerStudio/AngelScriptEngine/AngelScriptRegisters.h" #include "DeerStudio/StudioAPI/Resources.h" #include "DeerRender/Resource.h" namespace Deer { - namespace AngelScriptEngine { + namespace StudioAPI { template void registerResourceBasics(const char* objName) { REGISTER_OBJECT_METHOD(objName, "bool isValid() const", StudioAPI::APIResource, isValid); @@ -12,9 +11,9 @@ namespace Deer { REGISTER_OBJECT_METHOD(objName, "string get_path() const property", StudioAPI::APIResource, getPath); REGISTER_OBJECT_METHOD(objName, "int get_resourceId() const property", StudioAPI::APIResource, getResourceId); } - } // namespace AngelScriptEngine + } // namespace StudioAPI - void AngelScriptEngine::registerResourceStructs() { + void StudioAPI::registerResourceStructs() { scriptEngine->SetDefaultNamespace(""); AS_CHECK(scriptEngine->RegisterObjectType("GPUMesh", sizeof(StudioAPI::APIResource), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_ALLINTS | asGetTypeTraits>())); AS_CHECK(scriptEngine->RegisterObjectType("Shader", sizeof(StudioAPI::APIResource), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_ALLINTS | asGetTypeTraits>())); @@ -28,7 +27,7 @@ namespace Deer { scriptEngine->RegisterEnumValue("ResourceType", "Texture", (int)StudioAPI::ResourceType::TEXTURE); } - void AngelScriptEngine::registerResourceFunctions() { + void StudioAPI::registerResourceFunctions() { registerResourceBasics("GPUMesh"); registerResourceBasics("Shader"); registerResourceBasics("Texture"); diff --git a/DeerStudio/src/DeerStudio/AngelScriptEngine/Registers/UI.cpp b/DeerStudio/src/DeerStudio/Registers/UI.cpp similarity index 99% rename from DeerStudio/src/DeerStudio/AngelScriptEngine/Registers/UI.cpp rename to DeerStudio/src/DeerStudio/Registers/UI.cpp index 5b9b3c7..15741ae 100644 --- a/DeerStudio/src/DeerStudio/AngelScriptEngine/Registers/UI.cpp +++ b/DeerStudio/src/DeerStudio/Registers/UI.cpp @@ -2,6 +2,7 @@ #include "DeerStudio/AngelScriptEngine/AngelScriptRegisters.h" #include "DeerStudio/StudioAPI/Layout.h" #include "DeerStudio/StudioAPI/Menu.h" +#include "DeerStudio/StudioAPI/Resources.h" #include "imgui.h" namespace Deer { diff --git a/DeerStudio/src/DeerStudio/StudioAPI/Debug.cpp b/DeerStudio/src/DeerStudio/StudioAPI/Debug.cpp index 9180370..2b98051 100644 --- a/DeerStudio/src/DeerStudio/StudioAPI/Debug.cpp +++ b/DeerStudio/src/DeerStudio/StudioAPI/Debug.cpp @@ -6,20 +6,19 @@ namespace Deer { namespace StudioAPI { - void errorCallback_angelscript(const asSMessageInfo* msg, void* param) { if (msg->type == asMSGTYPE_WARNING) { DEER_EDITOR_ENGINE_WARN("{0}:{1}:{2}) : {3}", msg->section, msg->row, msg->col, msg->message); } else if (msg->type == asMSGTYPE_INFORMATION) { DEER_EDITOR_ENGINE_INFO("{0}:{1}:{2}) : {3}", msg->section, - msg->row, msg->col, msg->message); + msg->row, msg->col, msg->message); } else if (msg->type == asMSGTYPE_ERROR) { DEER_EDITOR_ENGINE_ERROR("{0}:{1}:{2}) : {3}", msg->section, msg->row, msg->col, msg->message); - }else { + } else { DEER_EDITOR_ENGINE_INFO("{0}:{1}:{2}) : {3}", msg->section, - msg->row, msg->col, msg->message); + msg->row, msg->col, msg->message); } } diff --git a/DeerStudio/src/DeerStudio/StudioAPI/Engine.cpp b/DeerStudio/src/DeerStudio/StudioAPI/Engine.cpp index b0a02d3..15d59e0 100644 --- a/DeerStudio/src/DeerStudio/StudioAPI/Engine.cpp +++ b/DeerStudio/src/DeerStudio/StudioAPI/Engine.cpp @@ -23,7 +23,7 @@ namespace Deer { } CScriptArray* dividePath_angelscript(std::string& path_s) { - CScriptArray* array = CScriptArray::Create(AngelScriptEngine::arrayStringBaseType); + CScriptArray* array = CScriptArray::Create(StudioAPI::arrayStringBaseType); Path path_p(path_s); for (const auto& part : path_p) { diff --git a/DeerStudio/src/DeerStudio/StudioAPI/Entity.cpp b/DeerStudio/src/DeerStudio/StudioAPI/Entity.cpp index 0d966b4..aa1c176 100644 --- a/DeerStudio/src/DeerStudio/StudioAPI/Entity.cpp +++ b/DeerStudio/src/DeerStudio/StudioAPI/Entity.cpp @@ -2,6 +2,7 @@ #include "DeerStudio/AngelScriptEngine.h" #include "DeerRender/Components.h" +#include "DeerRender/Engine.h" #include "DeerRender/EntityEnviroment.h" #include "DeerRender/World.h" @@ -9,7 +10,7 @@ #include -#define GET_ENV(env) ((env < 0) ? World::environment : Deer::Resource::unsafeFromId(environmentId).getData()) +#define GET_ENV(env) ((env < 0) ? *(Engine::getMainWorld().entityEnvironment.get()) : Deer::Resource::unsafeFromId(environmentId).getData()) #define GET_ENTITY(env, id) GET_ENV(env).getEntity(id) #define GET_MESH_COMPONENT(env, id) \ @@ -46,19 +47,19 @@ namespace Deer { EntityStruct::EntityStruct(uint16_t _entId, int32_t _envId) : EntityHandleStruct(_entId, _envId) {} - EntityEnvironment* EntityHandleStruct::getEntityEntityEnvironment() { + EntityEnvironment* EntityHandleStruct::getEntityEnvironment() { if (environmentId < 0) - return &World::environment; + return Engine::getMainWorld().entityEnvironment.get(); return &Resource::unsafeFromId(environmentId).getData(); } bool EntityHandleStruct::assertEntity(const char* funcName) { - EntityEnvironment* env = getEntityEntityEnvironment(); + EntityEnvironment* env = getEntityEnvironment(); if (!env->entityExists(entityId)) { DEER_EDITOR_ENGINE_ERROR( "Error, invalid entity calling {0}, entityId : {1}, environmentId : {2}", funcName, entityId, environmentId); - AngelScriptEngine::raiseError(); + StudioAPI::raiseError(); return false; } return true; @@ -67,7 +68,7 @@ namespace Deer { std::string EntityStruct::getName() { ASSERT_ENTITY("getName()", return "NULL"); - return GET_ENTITY(environmentId, entityId).getComponent().tag; + return ((environmentId < 0) ? *Engine::getMainWorld().entityEnvironment.get() : Deer::Resource::unsafeFromId(environmentId).getData()).getEntity(entityId).getComponent().tag; } void EntityStruct::setName(std::string& name) { @@ -93,7 +94,7 @@ namespace Deer { bool EntityStruct::exists() { ASSERT_ENTITY("exists()", return false); - return World::environment.entityExists(entityId); + return Engine::getMainWorld().entityEnvironment->entityExists(entityId); } void EntityStruct::setParent(EntityHandleStruct parent_struct) { @@ -198,7 +199,7 @@ namespace Deer { EntityEnvironment* env; if (environmentId < 0) - env = &World::environment; + env = Engine::getMainWorld().entityEnvironment.get(); else env = &Resource::unsafeFromId(environmentId).getData(); @@ -211,7 +212,7 @@ namespace Deer { funcName, ent.getComponent().tag.c_str(), entityId); - AngelScriptEngine::raiseError(); + StudioAPI::raiseError(); return false; } @@ -230,7 +231,7 @@ namespace Deer { "Error while executing EntityChild.getChild(..), id {0} is " "invalid for child count of {1}", i, rc.getChildCount()); - AngelScriptEngine::raiseError(); + StudioAPI::raiseError(); return EntityStruct(0); } @@ -241,7 +242,7 @@ namespace Deer { EntityHandleStruct EntityStruct::createChild(std::string& name) { ASSERT_ENTITY("createChild()", return *this); - EntityEnvironment* entityEnv = getEntityEntityEnvironment(); + EntityEnvironment* entityEnv = getEntityEnvironment(); Entity& newEnt = entityEnv->createEntity(name); Entity& me = GET_ENTITY(environmentId, entityId); @@ -264,7 +265,7 @@ namespace Deer { .tag.c_str(), entityId); - AngelScriptEngine::raiseError(); + StudioAPI::raiseError(); } return *this; @@ -313,7 +314,7 @@ namespace Deer { .tag.c_str(), entityId); - AngelScriptEngine::raiseError(); + StudioAPI::raiseError(); } return *this; @@ -362,7 +363,7 @@ namespace Deer { .tag.c_str(), entityId); - AngelScriptEngine::raiseError(); + StudioAPI::raiseError(); } return *this; @@ -437,7 +438,7 @@ namespace Deer { bool ShaderComponentStruct::assertShaderComponent(const char* funcName) { if (!assertEntity(funcName)) return false; - EntityEnvironment* env = getEntityEntityEnvironment(); + EntityEnvironment* env = getEntityEnvironment(); Entity& ent = env->getEntity(entityId); @@ -448,7 +449,7 @@ namespace Deer { funcName, ent.getComponent().tag.c_str(), entityId); - AngelScriptEngine::raiseError(); + StudioAPI::raiseError(); return false; } @@ -488,7 +489,7 @@ namespace Deer { bool CameraComponentStruct::assertCameraComponent(const char* funcName) { if (!assertEntity(funcName)) return false; - EntityEnvironment* env = getEntityEntityEnvironment(); + EntityEnvironment* env = getEntityEnvironment(); Entity& ent = env->getEntity(entityId); @@ -499,7 +500,7 @@ namespace Deer { funcName, ent.getComponent().tag.c_str(), entityId); - AngelScriptEngine::raiseError(); + StudioAPI::raiseError(); return false; } diff --git a/DeerStudio/src/DeerStudio/StudioAPI/EntityEnvironment.cpp b/DeerStudio/src/DeerStudio/StudioAPI/EntityEnvironment.cpp index fcc458f..718e584 100644 --- a/DeerStudio/src/DeerStudio/StudioAPI/EntityEnvironment.cpp +++ b/DeerStudio/src/DeerStudio/StudioAPI/EntityEnvironment.cpp @@ -1,3 +1,4 @@ +#include "DeerRender/Engine.h" #include "DeerRender/EntityEnviroment.h" #include "DeerRender/Resource.h" #include "DeerRender/Tools/Memory.h" @@ -13,7 +14,7 @@ namespace Deer { namespace StudioAPI { // The first element has to allways point to the main scene env - std::vector environments{&World::environment}; + std::vector environments; void EntityEnvironmentStruct::render(FrameBufferHandleStruct frameBuffer_handle, WorldCamera& sc) { Resource frameBufferResource = Resource::unsafeFromId(frameBuffer_handle.frameBufferId); @@ -21,7 +22,7 @@ namespace Deer { EntityEnvironment* envPtr = nullptr; if (environmentId < 0) { - envPtr = &World::environment; + envPtr = Engine::getMainWorld().entityEnvironment.get(); } else { Resource environmentResource = Resource::unsafeFromId(environmentId); envPtr = &environmentResource.getData(); diff --git a/DeerStudio/src/DeerStudio/StudioAPI/IconManagment.cpp b/DeerStudio/src/DeerStudio/StudioAPI/IconManagment.cpp index 12aa98f..0b5fdf3 100644 --- a/DeerStudio/src/DeerStudio/StudioAPI/IconManagment.cpp +++ b/DeerStudio/src/DeerStudio/StudioAPI/IconManagment.cpp @@ -1,4 +1,3 @@ -#include "DeerRender/DataStore.h" #include "DeerRender/Resource.h" #include "DeerRender/Texture.h" #include "DeerStudio/EditorDataSource.h" diff --git a/DeerStudio/src/DeerStudio/StudioAPI/Layout.cpp b/DeerStudio/src/DeerStudio/StudioAPI/Layout.cpp index 380c0df..f1fb67e 100644 --- a/DeerStudio/src/DeerStudio/StudioAPI/Layout.cpp +++ b/DeerStudio/src/DeerStudio/StudioAPI/Layout.cpp @@ -11,8 +11,8 @@ namespace Deer { void treeNodeLeaf(std::string& txt, bool active) { ImGuiTreeNodeFlags flags = ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen | - ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_Bullet | - ImGuiTreeNodeFlags_DrawLinesToNodes; + ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_Bullet | + ImGuiTreeNodeFlags_DrawLinesToNodes; if (active) flags |= ImGuiTreeNodeFlags_Selected; @@ -25,8 +25,8 @@ namespace Deer { ImGuiTreeNodeFlags flags = ImGuiTreeNodeFlags_OpenOnDoubleClick | ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_SpanFullWidth | - ImGuiTreeNodeFlags_DefaultOpen | - ImGuiTreeNodeFlags_DrawLinesToNodes; + ImGuiTreeNodeFlags_DefaultOpen | + ImGuiTreeNodeFlags_DrawLinesToNodes; if (active) flags |= ImGuiTreeNodeFlags_Selected; @@ -34,12 +34,12 @@ namespace Deer { if (ImGui::TreeNodeEx(txt.c_str(), flags)) { ImGui::PushID(txt.c_str()); - if (AngelScriptEngine::scriptContext && AngelScriptEngine::scriptContext->PushState() == asSUCCESS) { + if (StudioAPI::scriptContext && StudioAPI::scriptContext->PushState() == asSUCCESS) { - AS_CHECK_ADDITIONAL_INFO(AngelScriptEngine::scriptContext->Prepare(&func), func.GetDeclaration()); - AS_CHECK_ADDITIONAL_INFO(AngelScriptEngine::scriptContext->Execute(), func.GetDeclaration()); + AS_CHECK_ADDITIONAL_INFO(StudioAPI::scriptContext->Prepare(&func), func.GetDeclaration()); + AS_CHECK_ADDITIONAL_INFO(StudioAPI::scriptContext->Execute(), func.GetDeclaration()); - AngelScriptEngine::scriptContext->PopState(); + StudioAPI::scriptContext->PopState(); } else { ImGui::Text("Something failed"); } @@ -68,13 +68,13 @@ namespace Deer { if (ImGui::TreeNodeEx(txt.c_str(), flags)) { ImGui::Dummy(ImVec2(0, 10)); ImGui::PushID(txt.c_str()); - if (AngelScriptEngine::scriptContext && - AngelScriptEngine::scriptContext->PushState() == asSUCCESS) { + if (StudioAPI::scriptContext && + StudioAPI::scriptContext->PushState() == asSUCCESS) { - AS_CHECK(AngelScriptEngine::scriptContext->Prepare(func)); - AS_CHECK(AngelScriptEngine::scriptContext->Execute()); + AS_CHECK(StudioAPI::scriptContext->Prepare(func)); + AS_CHECK(StudioAPI::scriptContext->Execute()); - AngelScriptEngine::scriptContext->PopState(); + StudioAPI::scriptContext->PopState(); } else { ImGui::Text("Something failed"); } @@ -105,13 +105,13 @@ namespace Deer { ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(10, 10)); if (ImGui::BeginPopupContextItem(txt.c_str())) { - if (AngelScriptEngine::scriptContext && - AngelScriptEngine::scriptContext->PushState() == asSUCCESS) { + if (StudioAPI::scriptContext && + StudioAPI::scriptContext->PushState() == asSUCCESS) { - AS_CHECK(AngelScriptEngine::scriptContext->Prepare(menu)); - AS_CHECK(AngelScriptEngine::scriptContext->Execute()); + AS_CHECK(StudioAPI::scriptContext->Prepare(menu)); + AS_CHECK(StudioAPI::scriptContext->Execute()); - AngelScriptEngine::scriptContext->PopState(); + StudioAPI::scriptContext->PopState(); } else { ImGui::Text("Something failed"); } @@ -121,13 +121,13 @@ namespace Deer { ImGui::Dummy(ImVec2(0, 10)); ImGui::PushID(txt.c_str()); - if (AngelScriptEngine::scriptContext && - AngelScriptEngine::scriptContext->PushState() == asSUCCESS) { + if (StudioAPI::scriptContext && + StudioAPI::scriptContext->PushState() == asSUCCESS) { - AS_CHECK(AngelScriptEngine::scriptContext->Prepare(func)); - AS_CHECK(AngelScriptEngine::scriptContext->Execute()); + AS_CHECK(StudioAPI::scriptContext->Prepare(func)); + AS_CHECK(StudioAPI::scriptContext->Execute()); - AngelScriptEngine::scriptContext->PopState(); + StudioAPI::scriptContext->PopState(); } else { ImGui::Text("Something failed"); } diff --git a/DeerStudio/src/DeerStudio/StudioAPI/Menu.cpp b/DeerStudio/src/DeerStudio/StudioAPI/Menu.cpp index 23693c0..936cbf9 100644 --- a/DeerStudio/src/DeerStudio/StudioAPI/Menu.cpp +++ b/DeerStudio/src/DeerStudio/StudioAPI/Menu.cpp @@ -17,13 +17,13 @@ namespace Deer { ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(10, 10)); if (ImGui::BeginPopupContextItem(menu_id.c_str())) { - if (AngelScriptEngine::scriptContext && - AngelScriptEngine::scriptContext->PushState() == asSUCCESS) { + if (StudioAPI::scriptContext && + StudioAPI::scriptContext->PushState() == asSUCCESS) { - AS_CHECK(AngelScriptEngine::scriptContext->Prepare(func)); - AS_CHECK(AngelScriptEngine::scriptContext->Execute()); + AS_CHECK(StudioAPI::scriptContext->Prepare(func)); + AS_CHECK(StudioAPI::scriptContext->Execute()); - AngelScriptEngine::scriptContext->PopState(); + StudioAPI::scriptContext->PopState(); } else { ImGui::Text("Something failed"); } @@ -37,13 +37,13 @@ namespace Deer { ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(10, 10)); if (ImGui::BeginPopupContextWindow(menu_id.c_str())) { - if (AngelScriptEngine::scriptContext && - AngelScriptEngine::scriptContext->PushState() == asSUCCESS) { + if (StudioAPI::scriptContext && + StudioAPI::scriptContext->PushState() == asSUCCESS) { - AS_CHECK(AngelScriptEngine::scriptContext->Prepare(func)); - AS_CHECK(AngelScriptEngine::scriptContext->Execute()); + AS_CHECK(StudioAPI::scriptContext->Prepare(func)); + AS_CHECK(StudioAPI::scriptContext->Execute()); - AngelScriptEngine::scriptContext->PopState(); + StudioAPI::scriptContext->PopState(); } else { ImGui::Text("Something failed"); } @@ -91,16 +91,16 @@ namespace Deer { return; } - if (AngelScriptEngine::scriptContext && - AngelScriptEngine::scriptContext->PushState() == asSUCCESS) { - AS_CHECK(AngelScriptEngine::scriptContext->Prepare(func)); + if (StudioAPI::scriptContext && + StudioAPI::scriptContext->PushState() == asSUCCESS) { + AS_CHECK(StudioAPI::scriptContext->Prepare(func)); - AS_CHECK(AngelScriptEngine::scriptContext->SetArgObject( + AS_CHECK(StudioAPI::scriptContext->SetArgObject( 0, MenuContext::payload)); - AS_CHECK(AngelScriptEngine::scriptContext->Execute()); + AS_CHECK(StudioAPI::scriptContext->Execute()); - AngelScriptEngine::scriptContext->PopState(); + StudioAPI::scriptContext->PopState(); } else { ImGui::Text("Something failed"); } @@ -140,12 +140,12 @@ namespace Deer { return; } - if (AngelScriptEngine::scriptContext && - AngelScriptEngine::scriptContext->PushState() == asSUCCESS) { - AS_CHECK(AngelScriptEngine::scriptContext->Prepare(func)); - AS_CHECK(AngelScriptEngine::scriptContext->Execute()); + if (StudioAPI::scriptContext && + StudioAPI::scriptContext->PushState() == asSUCCESS) { + AS_CHECK(StudioAPI::scriptContext->Prepare(func)); + AS_CHECK(StudioAPI::scriptContext->Execute()); - AngelScriptEngine::scriptContext->PopState(); + StudioAPI::scriptContext->PopState(); } else { ImGui::Text("Something failed"); } diff --git a/DeerStudio/src/DeerStudio/StudioAPI/Resources.cpp b/DeerStudio/src/DeerStudio/StudioAPI/Resources.cpp index b2f4069..fe10669 100644 --- a/DeerStudio/src/DeerStudio/StudioAPI/Resources.cpp +++ b/DeerStudio/src/DeerStudio/StudioAPI/Resources.cpp @@ -16,7 +16,7 @@ namespace Deer { if (extension == ".obj" || extension == ".fbx" || extension == ".dae" || extension == ".3ds" || extension == ".ply" || extension == ".stl" || - extension == ".glb" || extension == ".gltf"|| extension == ".mtl") { + extension == ".glb" || extension == ".gltf" || extension == ".mtl") { return ResourceType::MESH; } @@ -30,7 +30,7 @@ namespace Deer { }; CScriptArray* getResourceFolders(std::string& path_s) { - CScriptArray* array = CScriptArray::Create(AngelScriptEngine::arrayStringBaseType); + CScriptArray* array = CScriptArray::Create(StudioAPI::arrayStringBaseType); Path path_p = Path("Resources") / Path(path_s).lexically_normal(); try { @@ -50,7 +50,7 @@ namespace Deer { } CScriptArray* getResourceFiles(std::string& path_s) { - CScriptArray* array = CScriptArray::Create(AngelScriptEngine::arrayStringBaseType); + CScriptArray* array = CScriptArray::Create(StudioAPI::arrayStringBaseType); Path path_p = Path("Resources") / Path(path_s).lexically_normal(); try { diff --git a/DeerStudio/src/DeerStudio/StudioAPI/UI.cpp b/DeerStudio/src/DeerStudio/StudioAPI/UI.cpp index 5b7e172..da4c67c 100644 --- a/DeerStudio/src/DeerStudio/StudioAPI/UI.cpp +++ b/DeerStudio/src/DeerStudio/StudioAPI/UI.cpp @@ -90,111 +90,101 @@ namespace Deer { return ImGui::Button(txt.c_str()); } - bool cartIconButton(const std::string& label, const std::string& icon, int iconSize, int width) - { - ImGui::BeginGroup(); - + bool cartIconButton(const std::string& label, const std::string& icon, int iconSize, int width) { + ImGui::BeginGroup(); + int offset = 16; - float textHeight = ImGui::GetTextLineHeight(); - float itemWidth = width; - float itemHeight = iconSize + textHeight + offset * 2; - - // Clickable area uses the full width - ImGui::InvisibleButton(label.c_str(), ImVec2(itemWidth, itemHeight)); - bool hovered = ImGui::IsItemHovered(); - bool pressed = ImGui::IsItemClicked(); - - ImDrawList* dl = ImGui::GetWindowDrawList(); - ImVec2 p = ImGui::GetItemRectMin(); - ImVec2 q = ImGui::GetItemRectMax(); - - // Background - dl->AddRectFilled( - p, q, - hovered ? IM_COL32(255,255,255,30) : IM_COL32(255,255,255,10), - 4.0f - ); - - // Center icon horizontally - ImVec2 iconPos( - p.x + (itemWidth - iconSize) * 0.5f, - p.y + 2.0f + offset - ); - - int texId = StudioAPI::getIconId("Icons/" + icon); - dl->AddImage( - (ImTextureID)texId, - iconPos, - ImVec2(iconPos.x + iconSize, iconPos.y + iconSize), - ImVec2(0,1), - ImVec2(1,0) - ); - - // Center text - ImVec2 textSize = ImGui::CalcTextSize(label.c_str()); - ImVec2 textPos( - p.x + (itemWidth - textSize.x) * 0.5f, - iconPos.y + iconSize + 2.0f - ); - dl->AddText(textPos, ImGui::GetColorU32(ImGuiCol_Text), label.c_str()); - - ImGui::EndGroup(); - return pressed; + float textHeight = ImGui::GetTextLineHeight(); + float itemWidth = width; + float itemHeight = iconSize + textHeight + offset * 2; + + // Clickable area uses the full width + ImGui::InvisibleButton(label.c_str(), ImVec2(itemWidth, itemHeight)); + bool hovered = ImGui::IsItemHovered(); + bool pressed = ImGui::IsItemClicked(); + + ImDrawList* dl = ImGui::GetWindowDrawList(); + ImVec2 p = ImGui::GetItemRectMin(); + ImVec2 q = ImGui::GetItemRectMax(); + + // Background + dl->AddRectFilled( + p, q, + hovered ? IM_COL32(255, 255, 255, 30) : IM_COL32(255, 255, 255, 10), + 4.0f); + + // Center icon horizontally + ImVec2 iconPos( + p.x + (itemWidth - iconSize) * 0.5f, + p.y + 2.0f + offset); + + int texId = StudioAPI::getIconId("Icons/" + icon); + dl->AddImage( + (ImTextureID)texId, + iconPos, + ImVec2(iconPos.x + iconSize, iconPos.y + iconSize), + ImVec2(0, 1), + ImVec2(1, 0)); + + // Center text + ImVec2 textSize = ImGui::CalcTextSize(label.c_str()); + ImVec2 textPos( + p.x + (itemWidth - textSize.x) * 0.5f, + iconPos.y + iconSize + 2.0f); + dl->AddText(textPos, ImGui::GetColorU32(ImGuiCol_Text), label.c_str()); + + ImGui::EndGroup(); + return pressed; } - bool cartIconButton_frameBuffer(const std::string& label, FrameBufferHandleStruct icon, int iconSize, int width) - { - ImGui::BeginGroup(); - + bool cartIconButton_frameBuffer(const std::string& label, FrameBufferHandleStruct icon, int iconSize, int width) { + ImGui::BeginGroup(); + int offset = 16; - float textHeight = ImGui::GetTextLineHeight(); - float itemWidth = width; - float itemHeight = iconSize + textHeight + offset * 2; - - // Clickable area uses the full width - ImGui::InvisibleButton(label.c_str(), ImVec2(itemWidth, itemHeight)); - bool hovered = ImGui::IsItemHovered(); - bool pressed = ImGui::IsItemClicked(); - - ImDrawList* dl = ImGui::GetWindowDrawList(); - ImVec2 p = ImGui::GetItemRectMin(); - ImVec2 q = ImGui::GetItemRectMax(); - - // Background - dl->AddRectFilled( - p, q, - hovered ? IM_COL32(255,255,255,30) : IM_COL32(255,255,255,10), - 4.0f - ); - - // Center icon horizontally - ImVec2 iconPos( - p.x + (itemWidth - iconSize) * 0.5f, - p.y + 2.0f + offset - ); - - FrameBuffer& frameBuffer = Resource::unsafeFromId(icon.frameBufferId).getData(); + float textHeight = ImGui::GetTextLineHeight(); + float itemWidth = width; + float itemHeight = iconSize + textHeight + offset * 2; + + // Clickable area uses the full width + ImGui::InvisibleButton(label.c_str(), ImVec2(itemWidth, itemHeight)); + bool hovered = ImGui::IsItemHovered(); + bool pressed = ImGui::IsItemClicked(); + + ImDrawList* dl = ImGui::GetWindowDrawList(); + ImVec2 p = ImGui::GetItemRectMin(); + ImVec2 q = ImGui::GetItemRectMax(); + + // Background + dl->AddRectFilled( + p, q, + hovered ? IM_COL32(255, 255, 255, 30) : IM_COL32(255, 255, 255, 10), + 4.0f); + + // Center icon horizontally + ImVec2 iconPos( + p.x + (itemWidth - iconSize) * 0.5f, + p.y + 2.0f + offset); + + FrameBuffer& frameBuffer = Resource::unsafeFromId(icon.frameBufferId).getData(); frameBuffer.bind(); - ImTextureID texId = frameBuffer.getTextureBufferID(); - dl->AddImage( - (ImTextureID)texId, - iconPos, - ImVec2(iconPos.x + iconSize, iconPos.y + iconSize), - ImVec2(0,1), - ImVec2(1,0) - ); + ImTextureID texId = frameBuffer.getTextureBufferID(); + dl->AddImage( + (ImTextureID)texId, + iconPos, + ImVec2(iconPos.x + iconSize, iconPos.y + iconSize), + ImVec2(0, 1), + ImVec2(1, 0)); frameBuffer.unbind(); - - // Center text - ImVec2 textSize = ImGui::CalcTextSize(label.c_str()); - ImVec2 textPos( - p.x + (itemWidth - textSize.x) * 0.5f, - iconPos.y + iconSize + 2.0f - ); - dl->AddText(textPos, ImGui::GetColorU32(ImGuiCol_Text), label.c_str()); - - ImGui::EndGroup(); - return pressed; + + // Center text + ImVec2 textSize = ImGui::CalcTextSize(label.c_str()); + ImVec2 textPos( + p.x + (itemWidth - textSize.x) * 0.5f, + iconPos.y + iconSize + 2.0f); + dl->AddText(textPos, ImGui::GetColorU32(ImGuiCol_Text), label.c_str()); + + ImGui::EndGroup(); + return pressed; } void textColor(float r, float g, float b, std::string& msg) { @@ -247,7 +237,7 @@ namespace Deer { if (iconId < 0) { DEER_EDITOR_ENGINE_ERROR("Invalid icon name {0}", name.c_str()); - AngelScriptEngine::raiseError(); + StudioAPI::raiseError(); return; } @@ -260,7 +250,7 @@ namespace Deer { } void drawIconHighlight_resource(Resource texture, int size) { int iconId = texture.getData().getTextureID(); - ImGui::Image((void*)(uint64_t)iconId, ImVec2(size, size), ImVec2(0, 1), ImVec2(1, 0), ImVec4(0.5f, 0.6f, 0.8f, 1.0f), ImVec4(0,0,0,0)); + ImGui::Image((void*)(uint64_t)iconId, ImVec2(size, size), ImVec2(0, 1), ImVec2(1, 0), ImVec4(0.5f, 0.6f, 0.8f, 1.0f), ImVec4(0, 0, 0, 0)); } void drawIconHighlight(std::string& name, int size) { @@ -268,11 +258,11 @@ namespace Deer { if (iconId < 0) { DEER_EDITOR_ENGINE_ERROR("Invalid icon name {0}", name.c_str()); - AngelScriptEngine::raiseError(); + StudioAPI::raiseError(); return; } - ImGui::Image((void*)(uint64_t)iconId, ImVec2(size, size), ImVec2(0, 1), ImVec2(1, 0), ImVec4(0.5f, 0.6f, 0.8f, 1.0f), ImVec4(0,0,0,0)); + ImGui::Image((void*)(uint64_t)iconId, ImVec2(size, size), ImVec2(0, 1), ImVec2(1, 0), ImVec4(0.5f, 0.6f, 0.8f, 1.0f), ImVec4(0, 0, 0, 0)); } void drawFrameBuffer(FrameBufferHandleStruct frameBuffer_handle, int sizeX, int sizeY) { @@ -327,7 +317,7 @@ namespace Deer { int iconId = StudioAPI::getIconId("Icons/" + name); if (iconId < 0) { DEER_EDITOR_ENGINE_ERROR("Invalid icon name {0}", name.c_str()); - AngelScriptEngine::raiseError(); + StudioAPI::raiseError(); return; } @@ -365,7 +355,7 @@ namespace Deer { int iconId = StudioAPI::getIconId("Icons/" + name); if (iconId < 0) { DEER_EDITOR_ENGINE_ERROR("Invalid icon name {0}", name.c_str()); - AngelScriptEngine::raiseError(); + StudioAPI::raiseError(); return; } @@ -394,13 +384,13 @@ namespace Deer { void subMenu(std::string& txt, asIScriptFunction* func) { if (ImGui::BeginMenu(txt.c_str())) { - if (AngelScriptEngine::scriptContext && - AngelScriptEngine::scriptContext->PushState() == asSUCCESS) { + if (StudioAPI::scriptContext && + StudioAPI::scriptContext->PushState() == asSUCCESS) { - AS_CHECK(AngelScriptEngine::scriptContext->Prepare(func)); - AS_CHECK(AngelScriptEngine::scriptContext->Execute()); + AS_CHECK(StudioAPI::scriptContext->Prepare(func)); + AS_CHECK(StudioAPI::scriptContext->Execute()); - AngelScriptEngine::scriptContext->PopState(); + StudioAPI::scriptContext->PopState(); } ImGui::EndMenu(); } @@ -440,14 +430,14 @@ namespace Deer { if (ImGui::AcceptDragDropPayload(id.c_str()) && DragDropPayload::payload) { - if (AngelScriptEngine::scriptContext && - AngelScriptEngine::scriptContext->PushState() == asSUCCESS) { + if (StudioAPI::scriptContext && + StudioAPI::scriptContext->PushState() == asSUCCESS) { - AS_CHECK(AngelScriptEngine::scriptContext->Prepare(func)); - AS_CHECK(AngelScriptEngine::scriptContext->SetArgObject(0, DragDropPayload::payload)); - AS_CHECK(AngelScriptEngine::scriptContext->Execute()); + AS_CHECK(StudioAPI::scriptContext->Prepare(func)); + AS_CHECK(StudioAPI::scriptContext->SetArgObject(0, DragDropPayload::payload)); + AS_CHECK(StudioAPI::scriptContext->Execute()); - AngelScriptEngine::scriptContext->PopState(); + StudioAPI::scriptContext->PopState(); } DragDropPayload::payload->Release(); diff --git a/Editor/Modules/Previewer/MeshPreview.as b/Editor/Modules/Previewer/MeshPreview.as index e821962..3637813 100644 --- a/Editor/Modules/Previewer/MeshPreview.as +++ b/Editor/Modules/Previewer/MeshPreview.as @@ -1,5 +1,5 @@ class RenderService : Service { - EntityEnvir nment env; + EntityEnvironment env; WorldCamera sceneCamera; MeshComponent meshC; Entity child; diff --git a/Editor/Modules/Viewport/Viewport.as b/Editor/Modules/Viewport/Viewport.as index 65a3f02..45e6ce6 100644 --- a/Editor/Modules/Viewport/Viewport.as +++ b/Editor/Modules/Viewport/Viewport.as @@ -1,7 +1,7 @@ class ViewportPanel : Panel { FrameBuffer frameBuffer; WorldCamera sceneCamera; - EntityEnvir nment mainEnv; + EntityEnvironment env; float pitch = 0; float yaw = 0; @@ -21,7 +21,7 @@ class ViewportPanel : Panel { frameBuffer.clearRGBA(0, 0, 0, 255); sceneCamera.camera.aspect = float(x) / y; - mainEnv.render(frameBuffer, sceneCamera); + env.render(frameBuffer, sceneCamera); UI::drawFrameBufferCentered(frameBuffer, x, y); if (!UI::isPanelActive()) @@ -70,7 +70,7 @@ class ViewportPanel : Panel { void init() { frameBuffer = Resource::createLoadRGBA8FrameBuffer("MainFrameBuffer", 1000, 1000); - mainEnv = Resource::getMainEntityEnvironment(); + env = Resource::getMainEntityEnvironment(); sceneCamera.transform.position = vec3(0, 1, -2); sceneCamera.camera.nearZ = 0.1; diff --git a/imgui.ini b/imgui.ini index b20d25c..1df5648 100644 --- a/imgui.ini +++ b/imgui.ini @@ -16,7 +16,7 @@ Collapsed=0 [Window][TreePanel] Pos=0,34 -Size=486,1336 +Size=486,804 Collapsed=0 DockId=0x00000001,0 @@ -34,7 +34,7 @@ DockId=0xA1672E74,1 [Window][PropertiesPanel] Pos=2002,34 -Size=558,1336 +Size=558,804 Collapsed=0 DockId=0x00000003,0