Impoving api for Environment...

This commit is contained in:
Chewico 2025-05-23 03:24:55 +02:00
parent f81574efed
commit f7d69eb21d
13 changed files with 1648 additions and 56 deletions

View File

@ -165,11 +165,8 @@ namespace Deer {
};
namespace EnvironmentManager {
uint16_t createEnvironment(const std::string& name);
uint16_t getEnvironmentId(const std::string& name);
uint16_t createEnvironment();
Environment& getEnvironment(uint16_t);
const std::string& getEnvironmentName(uint16_t);
void clearAllEnvironments();
}

View File

@ -2,15 +2,6 @@
#include <functional>
#include <thread>
#ifdef DEER_RENDER
#include "DeerRender/Render/RenderCommand.h"
#include "DeerRender/Render/RenderUtils.h"
#include "DeerRender/Render/Render.h"
#include "DeerRender/ImGui/ImGuiLayer.h"
#include "imgui.h"
#endif
namespace Deer {
namespace Application {
// Implemented in DeerRender/Application

View File

@ -6,45 +6,35 @@ namespace Deer {
namespace EnvironmentManager {
struct EnvironmentContainer {
Environment* env_data = nullptr;
std::string env_name;
};
std::unordered_map<std::string, uint16_t> environment_name_id;
EnvironmentContainer environments[MAX_ENVIRONMENT_COUNT]{};
uint16_t maxEnvironmentId = 1;
}
uint16_t EnvironmentManager::createEnvironment(const std::string& name) {
uint16_t EnvironmentManager::createEnvironment() {
if (maxEnvironmentId >= MAX_ENVIRONMENT_COUNT) {
DEER_CORE_ERROR("Max environment count");
return 0;
}
uint16_t envId = maxEnvironmentId;
maxEnvironmentId++;
environments[envId].env_data = new Environment();
environments[envId].env_name = name;
environment_name_id[name] = envId;
return envId;
}
uint16_t EnvironmentManager::getEnvironmentId(const std::string& name) {
return environment_name_id[name];
}
Environment& EnvironmentManager::getEnvironment(uint16_t id) {
DEER_CORE_ASSERT(id >= 0 && id < MAX_ENVIRONMENT_COUNT, "Invalid environment id {0}", id);
if (id == 0)
return Scene::environment;
return *environments[id].env_data;
}
const std::string& EnvironmentManager::getEnvironmentName(uint16_t id) {
const static std::string main_env_name("Main Environment");
if (id == 0)
return main_env_name;
return environments[id].env_name;
}
void EnvironmentManager::clearAllEnvironments() {
for (int i = 1; i < maxEnvironmentId; i++) {
delete environments[i].env_data;
@ -52,11 +42,6 @@ namespace Deer {
}
environments[0].env_data = &Scene::environment;
environments[0].env_name = "Main Environment";
environment_name_id.clear();
environment_name_id["Main Environment"] = 0;
maxEnvironmentId = 1;
}
}

View File

@ -21,6 +21,7 @@ project "angelScript"
defines {
"ANGELSCRIPT_EXPORT",
"AS_ENABLE_METADATA",
"_LIB"}
filter "system:linux"

View File

@ -0,0 +1,240 @@
#ifndef SCRIPTDICTIONARY_H
#define SCRIPTDICTIONARY_H
// The dictionary class relies on the script string object, thus the script
// string type must be registered with the engine before registering the
// dictionary type
#ifndef ANGELSCRIPT_H
// Avoid having to inform include path if header is already include before
#include <angelscript.h>
#endif
// By default the CScriptDictionary use the std::string for the keys.
// If the application uses a custom string type, then this typedef
// can be changed accordingly.
#include <string>
typedef std::string dictKey_t;
// Forward declare CScriptDictValue so we can typedef the internal map type
BEGIN_AS_NAMESPACE
class CScriptDictValue;
END_AS_NAMESPACE
// C++11 introduced the std::unordered_map which is a hash map which is
// is generally more performatic for lookups than the std::map which is a
// binary tree.
// TODO: memory: The map allocator should use the asAllocMem and asFreeMem
#if AS_CAN_USE_CPP11
#include <unordered_map>
typedef std::unordered_map<dictKey_t, AS_NAMESPACE_QUALIFIER CScriptDictValue> dictMap_t;
#else
#include <map>
typedef std::map<dictKey_t, AS_NAMESPACE_QUALIFIER CScriptDictValue> dictMap_t;
#endif
#ifdef _MSC_VER
// Turn off annoying warnings about truncated symbol names
#pragma warning (disable:4786)
#endif
// Sometimes it may be desired to use the same method names as used by C++ STL.
// This may for example reduce time when converting code from script to C++ or
// back.
//
// 0 = off
// 1 = on
#ifndef AS_USE_STLNAMES
#define AS_USE_STLNAMES 0
#endif
BEGIN_AS_NAMESPACE
class CScriptArray;
class CScriptDictionary;
class CScriptDictValue
{
public:
// This class must not be declared as local variable in C++, because it needs
// to receive the script engine pointer in all operations. The engine pointer
// is not kept as member in order to keep the size down
CScriptDictValue();
CScriptDictValue(asIScriptEngine *engine, void *value, int typeId);
// Destructor must not be called without first calling FreeValue, otherwise a memory leak will occur
~CScriptDictValue();
// Replace the stored value
void Set(asIScriptEngine *engine, void *value, int typeId);
void Set(asIScriptEngine *engine, const asINT64 &value);
void Set(asIScriptEngine *engine, const double &value);
void Set(asIScriptEngine *engine, CScriptDictValue &value);
// Gets the stored value. Returns false if the value isn't compatible with the informed typeId
bool Get(asIScriptEngine *engine, void *value, int typeId) const;
bool Get(asIScriptEngine *engine, asINT64 &value) const;
bool Get(asIScriptEngine *engine, double &value) const;
// Returns the address of the stored value for inspection
const void *GetAddressOfValue() const;
// Returns the type id of the stored value
int GetTypeId() const;
// Free the stored value
void FreeValue(asIScriptEngine *engine);
// GC callback
void EnumReferences(asIScriptEngine *engine);
protected:
friend class CScriptDictionary;
union
{
asINT64 m_valueInt;
double m_valueFlt;
void *m_valueObj;
};
int m_typeId;
};
class CScriptDictionary
{
public:
// Factory functions
static CScriptDictionary *Create(asIScriptEngine *engine);
// Called from the script to instantiate a dictionary from an initialization list
static CScriptDictionary *Create(asBYTE *buffer);
// Reference counting
void AddRef() const;
void Release() const;
// Reassign the dictionary
CScriptDictionary &operator =(const CScriptDictionary &other);
// Sets a key/value pair
void Set(const dictKey_t &key, void *value, int typeId);
void Set(const dictKey_t &key, const asINT64 &value);
void Set(const dictKey_t &key, const double &value);
// Gets the stored value. Returns false if the value isn't compatible with the informed typeId
bool Get(const dictKey_t &key, void *value, int typeId) const;
bool Get(const dictKey_t &key, asINT64 &value) const;
bool Get(const dictKey_t &key, double &value) const;
// Index accessors. If the dictionary is not const it inserts the value if it doesn't already exist
// If the dictionary is const then a script exception is set if it doesn't exist and a null pointer is returned
CScriptDictValue *operator[](const dictKey_t &key);
const CScriptDictValue *operator[](const dictKey_t &key) const;
// Returns the type id of the stored value, or negative if it doesn't exist
int GetTypeId(const dictKey_t &key) const;
// Returns true if the key is set
bool Exists(const dictKey_t &key) const;
// Returns true if there are no key/value pairs in the dictionary
bool IsEmpty() const;
// Returns the number of key/value pairs in the dictionary
asUINT GetSize() const;
// Deletes the key
bool Delete(const dictKey_t &key);
// Deletes all keys
void DeleteAll();
// Get an array of all keys
CScriptArray *GetKeys() const;
// STL style iterator
class CIterator
{
public:
void operator++(); // Pre-increment
void operator++(int); // Post-increment
// This is needed to support C++11 range-for
CIterator &operator*();
bool operator==(const CIterator &other) const;
bool operator!=(const CIterator &other) const;
// Accessors
const dictKey_t &GetKey() const;
int GetTypeId() const;
bool GetValue(asINT64 &value) const;
bool GetValue(double &value) const;
bool GetValue(void *value, int typeId) const;
const void * GetAddressOfValue() const;
protected:
friend class CScriptDictionary;
CIterator();
CIterator(const CScriptDictionary &dict,
dictMap_t::const_iterator it);
CIterator &operator=(const CIterator &) {return *this;} // Not used
dictMap_t::const_iterator m_it;
const CScriptDictionary &m_dict;
};
CIterator begin() const;
CIterator end() const;
CIterator find(const dictKey_t &key) const;
// Garbage collections behaviours
int GetRefCount();
void SetGCFlag();
bool GetGCFlag();
void EnumReferences(asIScriptEngine *engine);
void ReleaseAllReferences(asIScriptEngine *engine);
protected:
// Since the dictionary uses the asAllocMem and asFreeMem functions to allocate memory
// the constructors are made protected so that the application cannot allocate it
// manually in a different way
CScriptDictionary(asIScriptEngine *engine);
CScriptDictionary(asBYTE *buffer);
// We don't want anyone to call the destructor directly, it should be called through the Release method
virtual ~CScriptDictionary();
// Cache the object types needed
void Init(asIScriptEngine *engine);
// Our properties
asIScriptEngine *engine;
mutable int refCount;
mutable bool gcFlag;
dictMap_t dict;
};
// This function will determine the configuration of the engine
// and use one of the two functions below to register the dictionary object
void RegisterScriptDictionary(asIScriptEngine *engine);
// Call this function to register the math functions
// using native calling conventions
void RegisterScriptDictionary_Native(asIScriptEngine *engine);
// Use this one instead if native calling conventions
// are not supported on the target platform
void RegisterScriptDictionary_Generic(asIScriptEngine *engine);
END_AS_NAMESPACE
#endif

File diff suppressed because it is too large Load Diff

View File

@ -28,7 +28,8 @@ project "DeerStudio"
"ImGui",
"angelScript"
}
defines { "DEER_RENDER" }
defines { "DEER_RENDER",
"AS_ENABLE_METADATA" }
targetdir ("../bin/" .. OutputDir .. "/%{prj.name}")
objdir ("../bin/int/" .. OutputDir .. "/%{prj.name}")

View File

@ -1,6 +1,6 @@
[Window][DockSpace Demo]
Pos=0,0
Size=1920,1011
Size=1741,1159
Collapsed=0
[Window][Debug##Default]
@ -9,53 +9,53 @@ Size=400,400
Collapsed=0
[Window][Terrain Editor]
Pos=1587,24
Size=333,745
Pos=1315,24
Size=426,893
Collapsed=0
DockId=0x00000006,1
[Window][Viewport]
Pos=294,24
Size=1291,745
Pos=361,24
Size=952,893
Collapsed=0
DockId=0x00000005,1
[Window][ViewportPannel]
Pos=294,24
Size=1291,745
Pos=361,24
Size=952,893
Collapsed=0
DockId=0x00000005,0
[Window][ShaderExplorer]
Pos=0,771
Size=1920,240
Pos=0,919
Size=1741,240
Collapsed=0
DockId=0x00000004,1
[Window][TreePannel]
Pos=0,24
Size=292,745
Size=359,893
Collapsed=0
DockId=0x00000001,0
[Window][MeshExplorer]
Pos=0,771
Size=1920,240
Pos=0,919
Size=1741,240
Collapsed=0
DockId=0x00000004,0
[Window][PropertiesPannel]
Pos=1587,24
Size=333,745
Pos=1315,24
Size=426,893
Collapsed=0
DockId=0x00000006,0
[Docking][Data]
DockSpace ID=0xA1672E74 Window=0x4647B76E Pos=0,24 Size=1920,987 Split=Y Selected=0x34A4C10F
DockSpace ID=0xA1672E74 Window=0x4647B76E Pos=0,24 Size=1741,1135 Split=Y Selected=0x34A4C10F
DockNode ID=0x00000003 Parent=0xA1672E74 SizeRef=1280,454 Split=X
DockNode ID=0x00000001 Parent=0x00000003 SizeRef=292,696 Selected=0xE45B9F93
DockNode ID=0x00000002 Parent=0x00000003 SizeRef=986,696 Split=X Selected=0x34A4C10F
DockNode ID=0x00000005 Parent=0x00000002 SizeRef=651,454 CentralNode=1 Selected=0x34A4C10F
DockNode ID=0x00000006 Parent=0x00000002 SizeRef=333,454 Selected=0xA35A27E3
DockNode ID=0x00000001 Parent=0x00000003 SizeRef=359,696 Selected=0xE45B9F93
DockNode ID=0x00000002 Parent=0x00000003 SizeRef=1380,696 Split=X Selected=0x34A4C10F
DockNode ID=0x00000005 Parent=0x00000002 SizeRef=952,454 CentralNode=1 Selected=0x34A4C10F
DockNode ID=0x00000006 Parent=0x00000002 SizeRef=426,454 Selected=0x2A2C795E
DockNode ID=0x00000004 Parent=0xA1672E74 SizeRef=1280,240 Selected=0xD962995A

View File

@ -11,6 +11,8 @@
#include "angelscript.h"
#include "scriptbuilder.h"
#include "scriptstdstring.h"
#include "scriptarray.h"
#include "scriptdictionary.h"
void extract_angelScript();
@ -34,6 +36,8 @@ namespace Deer {
scriptEngine = asCreateScriptEngine();
RegisterStdString(scriptEngine);
RegisterScriptArray(scriptEngine, true);
RegisterScriptDictionary(scriptEngine);
AS_RET_CHECK(scriptEngine->SetMessageCallback(asFUNCTION(Deer::EditorEngine::errorCallback), 0, asCALL_CDECL));

View File

@ -0,0 +1,14 @@
dictionary meshFrameBuffer;
FrameBuffer getMeshFrameBuffer(string mesh) {
if (meshFrameBuffer.exists(mesh))
return cast<FrameBuffer>(meshFrameBuffer[mesh]);
}
FrameBuffer generateMeshFrameBuffer(string mesh) {
FrameBuffer fb = Engine::createRGBA8FrameBuffer(mesh, 64, 64);
return fb;
}

View File

@ -506,3 +506,63 @@ namespace UI {
int sliderInt(string&in, int value, int min, int max);
float slider(string&in, float value, float min, float max);
}
class array<T> {
T& opIndex(uint index);
const T& opIndex(uint index) const;
T[]& opAssign(const T[]&in);
void insertAt(uint index, const T&in value);
void insertAt(uint index, const T[]&inout arr);
void insertLast(const T&in value);
void removeAt(uint index);
void removeLast();
void removeRange(uint start, uint count);
uint length() const;
void reserve(uint length);
void resize(uint length);
void sortAsc();
void sortAsc(uint startAt, uint count);
void sortDesc();
void sortDesc(uint startAt, uint count);
void reverse();
int find(const T&in value) const;
int find(uint startAt, const T&in value) const;
int findByRef(const T&in value) const;
int findByRef(uint startAt, const T&in value) const;
bool opEquals(const T[]&in) const;
bool isEmpty() const;
//void sort(T[]::less&in, uint startAt = 0, uint count = uint ( - 1 ));
funcdef bool less(const T&in, const T&in);
}
class dictionaryValue {
~dictionaryValue();
dictionaryValue();
dictionaryValue& opAssign(const dictionaryValue&in);
dictionaryValue& opHndlAssign(const ?&in);
dictionaryValue& opHndlAssign(const dictionaryValue&in);
dictionaryValue& opAssign(const ?&in);
dictionaryValue& opAssign(double);
dictionaryValue& opAssign(int64);
void opCast(?&out);
void opConv(?&out);
int64 opConv();
double opConv();
}
class dictionary {
dictionary& opAssign(const dictionary&in);
void set(const string&in, const ?&in);
bool get(const string&in, ?&out) const;
void set(const string&in, const int64&in);
bool get(const string&in, int64&out) const;
void set(const string&in, const double&in);
bool get(const string&in, double&out) const;
bool exists(const string&in) const;
bool isEmpty() const;
uint getSize() const;
bool delete(const string&in);
void deleteAll();
string[]@ getKeys() const;
dictionaryValue& opIndex(const string&in);
const dictionaryValue& opIndex(const string&in) const;
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 120 KiB

After

Width:  |  Height:  |  Size: 119 KiB