diff --git a/bin/data/shaders/display.subpass.frag.glsl b/bin/data/shaders/display.subpass.frag.glsl index aa5af603..716f099c 100644 --- a/bin/data/shaders/display.subpass.frag.glsl +++ b/bin/data/shaders/display.subpass.frag.glsl @@ -44,8 +44,8 @@ layout (binding = 0) uniform UBO { } ubo; void fog( inout vec3 i ) { - vec3 color = vec3( 0, 0, 0 ); - float inner = 8, outer = 64; + vec3 color = vec3( 0.1, 0.1, 0.1 ); + float inner = 4, outer = 32; float distance = length(-position.eye); float factor = (distance - inner) / (outer - inner); factor = clamp( factor, 0.0, 1.0 ); diff --git a/client/main.cpp b/client/main.cpp index ea73f94a..793a9557 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -3,6 +3,8 @@ #include #include +#include + int main(int argc, char** argv){ std::atexit([]{ uf::iostream << "Termination via std::atexit()!" << "\n"; diff --git a/engine/inc/uf/engine/entity/entity.h b/engine/inc/uf/engine/entity/entity.h index ad3108de..b55dd59e 100644 --- a/engine/inc/uf/engine/entity/entity.h +++ b/engine/inc/uf/engine/entity/entity.h @@ -6,13 +6,14 @@ #include #include +#include + namespace uf { class UF_API Entity : public uf::Component { public: typedef std::vector container_t; protected: static uf::Entity null; - static std::vector entities; static std::size_t uids; uf::Entity* m_parent = NULL; @@ -21,6 +22,7 @@ namespace uf { std::size_t m_uid = 0; std::string m_name = "Entity"; public: + static uf::MemoryPool memoryPool; Entity( bool = false ); virtual ~Entity(); @@ -45,6 +47,9 @@ namespace uf { virtual void tick(); virtual void render(); + void* operator new(size_t, const std::string& = ""); + void operator delete(void*); + template void initialize(); template void destroy(); template void tick(); @@ -58,37 +63,8 @@ namespace uf { void process( std::function, int depth = 0 ); // void process( std::function ) const ; // void process( std::function, int depth = 0 ) const; - + static uf::Entity* globalFindByUid( size_t id ); static uf::Entity* globalFindByName( const std::string& name ); }; -/* -namespace uf { - class UF_API Entity : public uf::Component { - public: - typedef std::vector container_t; - protected: - static uf::Entity null; - - uf::Entity* m_parent = NULL; - uf::Entity::container_t m_children; - public: - Entity(); - virtual ~Entity(); - - template T& getParent(); - template T& getRootParent(); - template const T& getParent() const; - template const T& getRootParent() const; - - void setParent( uf::Entity& parent = null ); - void addChild( uf::Entity& child ); - - virtual void initialize(); - virtual void destroy(); - - virtual void tick(); - virtual void render(); - }; -*/ } #include "entity.inl" \ No newline at end of file diff --git a/engine/inc/uf/engine/instantiator/instantiator.h b/engine/inc/uf/engine/instantiator/instantiator.h index 2b191321..97ae9a43 100644 --- a/engine/inc/uf/engine/instantiator/instantiator.h +++ b/engine/inc/uf/engine/instantiator/instantiator.h @@ -27,26 +27,17 @@ namespace uf { extern UF_API std::unordered_map* names; extern UF_API std::unordered_map* map; - template - void add( const std::string& name ) { - if ( !names ) names = new std::unordered_map; - if ( !map ) map = new std::unordered_map; - - auto& names = *uf::instantiator::names; - auto& map = *uf::instantiator::map; - - names[std::type_index(typeid(T))] = name; - map[name] = [](){ - return new T; - }; - - std::cout << "Registered instantiation for " << name << std::endl; - } + uf::Entity* UF_API alloc( size_t ); + void UF_API free( uf::Entity* ); uf::Entity* UF_API instantiate( const std::string& ); - template - T& instantiate() { - auto& names = *uf::instantiator::names; - return *((T*) uf::instantiator::instantiate( names[std::type_index(typeid(T))] )); - } + + template void add( const std::string& name ); + + template T* alloc(); + template T& instantiate( size_t size ); + template T& instantiate(); + template T* _instantiate(); }; -} \ No newline at end of file +} + +#include "instantiator.inl" \ No newline at end of file diff --git a/engine/inc/uf/engine/instantiator/instantiator.inl b/engine/inc/uf/engine/instantiator/instantiator.inl new file mode 100644 index 00000000..11f105cc --- /dev/null +++ b/engine/inc/uf/engine/instantiator/instantiator.inl @@ -0,0 +1,32 @@ +template +T* uf::instantiator::alloc() { + return (T*) alloc( sizeof(T) ); +// return new T; +} +template +void uf::instantiator::add( const std::string& name ) { + if ( !names ) names = new std::unordered_map; + if ( !map ) map = new std::unordered_map; + + auto& names = *uf::instantiator::names; + auto& map = *uf::instantiator::map; + + names[std::type_index(typeid(T))] = name; + map[name] = _instantiate; + + std::cout << "Registered instantiation for " << name << std::endl; +} +template +T& uf::instantiator::instantiate( size_t size ) { + T* entity = alloc(); + ::new (entity) T(); + return *entity; +} +template +T& uf::instantiator::instantiate() { + return instantiate( sizeof(T) ); +} +template +T* uf::instantiator::_instantiate() { + return &instantiate( sizeof(T) ); +} \ No newline at end of file diff --git a/engine/inc/uf/spec/renderer/universal.h b/engine/inc/uf/spec/renderer/universal.h new file mode 100644 index 00000000..299b9b2d --- /dev/null +++ b/engine/inc/uf/spec/renderer/universal.h @@ -0,0 +1,7 @@ +#pragma once + +#if defined(UF_USE_VULKAN) && UF_USE_VULKAN == 1 + #include "vulkan.h" +#elif defined(UF_USE_OPENGL) && UF_USE_OPENGL == 1 + #include "opengl.h" +#endif \ No newline at end of file diff --git a/engine/inc/uf/spec/renderer/vulkan.h b/engine/inc/uf/spec/renderer/vulkan.h new file mode 100644 index 00000000..10a7a339 --- /dev/null +++ b/engine/inc/uf/spec/renderer/vulkan.h @@ -0,0 +1,13 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +namespace spec { + namespace renderer = ext::vulkan; +} \ No newline at end of file diff --git a/engine/inc/uf/utils/component/component.h b/engine/inc/uf/utils/component/component.h index 0cc12017..96042485 100644 --- a/engine/inc/uf/utils/component/component.h +++ b/engine/inc/uf/utils/component/component.h @@ -19,6 +19,7 @@ namespace pod { } namespace uf { namespace component { + extern UF_API uf::MemoryPool memoryPool; template pod::Component::id_t type(); template bool is( const pod::Component& ); } diff --git a/engine/inc/uf/utils/component/component.inl b/engine/inc/uf/utils/component/component.inl index 945d924d..41a13d3f 100644 --- a/engine/inc/uf/utils/component/component.inl +++ b/engine/inc/uf/utils/component/component.inl @@ -86,7 +86,14 @@ template T& uf::Component::addComponent( const T& data ) { pod::Component::id_t id = this->getType(); pod::Component& component = this->m_container[id]; - component.userdata = uf::userdata::create(data); + component.userdata = uf::userdata::create(uf::component::memoryPool, data); +/* + if ( uf::component::memoryPool.size() > 0 ) { + component.userdata = uf::userdata::create(uf::component::memoryPool, data); + } else { + component.userdata = uf::userdata::create(data); + } +*/ component.id = id; T* pointer = &uf::userdata::get(component.userdata); @@ -96,5 +103,21 @@ template void uf::Component::deleteComponent() { if ( !this->hasComponent() ) return; pod::Component::id_t id = this->getType(); pod::Component& component = this->m_container[id]; - uf::userdata::destroy(component.userdata); + + uf::userdata::destroy(uf::component::memoryPool, component.userdata); +/* + if ( uf::component::memoryPool.size() > 0 ) { + uf::userdata::destroy(component.userdata); + } else { + uf::userdata::destroy(uf::component::memoryPool, component.userdata); + } +*/ +// uf::userdata::destroy(uf::component::memoryPool, component.userdata); +/* + if ( component.userdata->pointer ) { + uf::userdata::destroy(uf::component::memoryPool, component.userdata); + } else { + uf::userdata::destroy(component.userdata); + } +*/ } \ No newline at end of file diff --git a/engine/inc/uf/utils/mempool/mempool.h b/engine/inc/uf/utils/mempool/mempool.h new file mode 100644 index 00000000..cecea236 --- /dev/null +++ b/engine/inc/uf/utils/mempool/mempool.h @@ -0,0 +1,72 @@ +#pragma once + +#include +#include +#include +#include +#include + +#define UF_MEMORYPOOL_MUTEX 1 +#define UF_MEMORYPOOL_INVALID_MALLOC 0 +#define UF_MEMORYPOOL_INVALID_FREE 0 +#define UF_MEMORYPOOL_OVERRIDE_NEW_DELETE 0 + +namespace pod { + struct UF_API Userdata; + struct UF_API Allocation { + size_t index = 0; + size_t size = 0; + void* pointer = NULL; + }; +} + +namespace uf { + class UF_API MemoryPool { + protected: + size_t m_size; + uint8_t* m_pool; + std::mutex m_mutex; + + typedef std::vector allocations_t; + allocations_t m_allocations; + public: + static bool globalOverride; + static bool subPool; + static MemoryPool global; + + MemoryPool( size_t = 0 ); + ~MemoryPool(); + + size_t size() const; + size_t allocated() const; + std::string stats() const; + void initialize( size_t ); + void destroy(); + + pod::Allocation allocate( void*, size_t ); + void* alloc( void*, size_t ); + inline void* alloc( size_t size, void* data = NULL ) { return alloc(data, size); } + pod::Allocation& fetch( size_t, size_t = 0 ); + bool exists( void*, size_t = 0 ); + bool free( void*, size_t = 0 ); + + template T& alloc( const T& = T() ); + template pod::Allocation allocate( const T& = T() ); + template bool exists( const T& = T() ); + template bool free( const T& = T() ); + + const allocations_t& allocations() const; + /* + struct Iterator { + uf::MemoryPool& pool; + pod::Allocation& allocation; + pod::Allocation& next; + pod::Allocation& prev; + }; + Iterator begin(); + Iterator end(); + */ + }; +} + +#include "mempool.inl" \ No newline at end of file diff --git a/engine/inc/uf/utils/mempool/mempool.inl b/engine/inc/uf/utils/mempool/mempool.inl new file mode 100644 index 00000000..38cc2428 --- /dev/null +++ b/engine/inc/uf/utils/mempool/mempool.inl @@ -0,0 +1,33 @@ +template +T& uf::MemoryPool::alloc( const T& data ) { + auto allocation = this->allocate( data ); + union { + uint8_t* from; + T* to; + } static kludge; + return *kludge.to; +} +template +pod::Allocation uf::MemoryPool::allocate( const T& data ) { + auto allocation = this->allocate( NULL, sizeof(data) ); + if ( !allocation.pointer ) return allocation; + union { + uint8_t* from; + T* to; + } static kludge; + kludge.from = (uint8_t*) allocation.pointer; + ::new (kludge.to) T(data); + return allocation; +} +template +bool uf::MemoryPool::exists( const T& data ) { + if ( std::is_pointer::value ) return this->exists( (void*) data ); + return this->exists( (void*) &data, sizeof(data) ); +// return this->exists( (void*) (std::is_pointer::value ? data : &data), sizeof(data) ); +} +template +bool uf::MemoryPool::free( const T& data ) { + if ( std::is_pointer::value ) return this->free( (void*) data ); + return this->free( (void*) &data, sizeof(data) ); +// return this->free( (void*) (std::is_pointer::value ? data : &data), sizeof(data) ); +} \ No newline at end of file diff --git a/engine/inc/uf/utils/renderer/renderer.h b/engine/inc/uf/utils/renderer/renderer.h new file mode 100644 index 00000000..39736f38 --- /dev/null +++ b/engine/inc/uf/utils/renderer/renderer.h @@ -0,0 +1,7 @@ +#pragma once + +#include + +namespace uf { + namespace renderer = spec::renderer; +} \ No newline at end of file diff --git a/engine/inc/uf/utils/renderer/vulkan.h b/engine/inc/uf/utils/renderer/vulkan.h new file mode 100644 index 00000000..ad0403b0 --- /dev/null +++ b/engine/inc/uf/utils/renderer/vulkan.h @@ -0,0 +1,13 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +namespace uf { + namespace renderer = ext::vulkan; +} \ No newline at end of file diff --git a/engine/inc/uf/utils/string/ext.h b/engine/inc/uf/utils/string/ext.h index 71a084fa..968ba1fc 100644 --- a/engine/inc/uf/utils/string/ext.h +++ b/engine/inc/uf/utils/string/ext.h @@ -6,7 +6,6 @@ namespace uf { namespace string { - std::string UF_API filename( const std::string& ); std::string UF_API extension( const std::string& ); std::string UF_API directory( const std::string& ); diff --git a/engine/inc/uf/utils/userdata/userdata.h b/engine/inc/uf/utils/userdata/userdata.h index 146f799c..90203c83 100644 --- a/engine/inc/uf/utils/userdata/userdata.h +++ b/engine/inc/uf/utils/userdata/userdata.h @@ -1,11 +1,17 @@ #pragma once #include + +#include + #include #include #include #include +#define UF_USERDATA_POOLED 0 +#define UF_USERDATA_KLUDGE 0 + namespace pod { struct UF_API Userdata { std::size_t len = 0; @@ -15,8 +21,9 @@ namespace pod { namespace uf { namespace userdata { - pod::Userdata* UF_API create( std::size_t len ); - pod::Userdata* UF_API create( std::size_t len, void* data ); + extern UF_API uf::MemoryPool memoryPool; + + pod::Userdata* UF_API create( std::size_t len, void* data = NULL ); void UF_API destroy( pod::Userdata* userdata ); template pod::Userdata* create( const T& data = T() ); @@ -28,6 +35,11 @@ namespace uf { // void move( pod::Userdata& to, pod::Userdata&& from ); // void copy( pod::Userdata& to, const pod::Userdata& from ); + + // usage with MemoryPool + pod::Userdata* UF_API create( uf::MemoryPool&, std::size_t len, void* data = NULL ); + template pod::Userdata* create( uf::MemoryPool&, const T& data = T() ); + void UF_API destroy( uf::MemoryPool&, pod::Userdata* userdata ); } } diff --git a/engine/inc/uf/utils/userdata/userdata.inl b/engine/inc/uf/utils/userdata/userdata.inl index 012234e1..885d20ca 100644 --- a/engine/inc/uf/utils/userdata/userdata.inl +++ b/engine/inc/uf/utils/userdata/userdata.inl @@ -3,32 +3,112 @@ // Allows copy via assignment! template pod::Userdata* uf::userdata::create( const T& data ) { - void* pointer = operator new( sizeof(pod::Userdata) + sizeof(uint8_t) * (sizeof data) ); + return uf::userdata::create( uf::userdata::memoryPool, data ); +/* + if ( uf::userdata::memoryPool.size() > 0 ) { + return uf::userdata::create( uf::userdata::memoryPool, data ); + } else { +#if UF_USERDATA_KLUDGE + void* pointer = operator new( sizeof(pod::Userdata) + sizeof data ); pod::Userdata* userdata = (pod::Userdata*) pointer; userdata->len = sizeof data; - //memcpy( userdata->data, &data, sizeof data ); - //new (userdata->data) T(data); + userdata->pointer = NULL; union { uint8_t* from; T* to; } static kludge; kludge.from = userdata->data; - new (kludge.to) T(data); + ::new (kludge.to) T(data); + return userdata; +#else + pod::Userdata* userdata = new pod::Userdata; + userdata->len = sizeof data; + userdata->data = (uint8_t*) operator new(userdata->len); + union { + uint8_t* from; + T* to; + } static kludge; + kludge.from = userdata->data; + ::new (kludge.to) T(data); + return userdata; +#endif + } +*/ +} +template +pod::Userdata* uf::userdata::create( uf::MemoryPool& requestedMemoryPool, const T& data ) { +// uf::MemoryPool& memoryPool = uf::MemoryPool::global.size() > 0 ? uf::MemoryPool::global : requestedMemoryPool; +#if UF_MEMORYPOOL_INVALID_MALLOC + uf::MemoryPool& memoryPool = requestedMemoryPool.size() > 0 ? requestedMemoryPool : uf::MemoryPool::global; + pod::Userdata* userdata = (pod::Userdata*) memoryPool.alloc( NULL, sizeof(pod::Userdata) + sizeof(data) ); +#else + uf::MemoryPool* memoryPool = NULL; + if ( requestedMemoryPool.size() > 0 ) memoryPool = &requestedMemoryPool; + else if ( uf::MemoryPool::global.size() > 0 ) memoryPool = &uf::MemoryPool::global; + pod::Userdata* userdata = NULL; + if ( memoryPool ) + userdata = (pod::Userdata*) memoryPool->alloc( NULL, sizeof(pod::Userdata) + sizeof(data) ); + else + userdata = (pod::Userdata*) operator new( sizeof(pod::Userdata) + sizeof(data) ); // allocate data for the userdata struct, and then some } +#endif + userdata->len = sizeof(data); + union { + uint8_t* from; + T* to; + } static kludge; + kludge.from = userdata->data; + ::new (kludge.to) T(data); return userdata; /* - std::size_t len = sizeof data; // get size of data -// void* pointer = malloc( sizeof(pod::Userdata) + sizeof(uint8_t) * len ); // allocate data for the userdata struct, and then some - void* pointer = operator new( sizeof(pod::Userdata) + sizeof(uint8_t) * (len) ); // allocate data for the userdata struct, and then some - pod::Userdata* userdata = (pod::Userdata*) pointer; - userdata->len = len; // don't forget to store its data's length! - // Allows warningless conversion from placeholder storage type to userdata type + uf::MemoryPool* memoryPool = NULL; + if ( requestedMemoryPool.size() > 0 ) memoryPool = &requestedMemoryPool; + else if ( uf::MemoryPool::global.size() > 0 ) memoryPool = &uf::MemoryPool::global; + pod::Userdata* userdata; + if ( memoryPool ) + userdata = (pod::Userdata*) memoryPool->alloc( NULL, sizeof(pod::Userdata) + sizeof(data) ); + else { + userdata = uf::userdata::create( requestedMemoryPool, sizeof(data), NULL ); + } + userdata->len = sizeof(data); +#if UF_USERDATA_KLUDGE + userdata->pointer = NULL; +#endif union { uint8_t* from; T* to; } static kludge; kludge.from = userdata->data; - new (kludge.to) T(data); // copy via placement new w/ copy constructor - return userdata; // return address of userdata + ::new (kludge.to) T(data); + return userdata; +*/ +/* + if ( uf::userdata::memoryPool.size() > 0 ) { + // pod::Userdata* userdata = uf::userdata::create( memoryPool, sizeof(pod::Userdata) + sizeof(data), NULL ); + void* pointer = uf::userdata::memoryPool.alloc( NULL, sizeof(pod::Userdata) + sizeof(data) ); + pod::Userdata* userdata = (pod::Userdata*) pointer; + userdata->len = sizeof(data); + #if UF_USERDATA_KLUDGE + userdata->pointer = NULL; + #endif + union { + uint8_t* from; + T* to; + } static kludge; + kludge.from = userdata->data; + ::new (kludge.to) T(data); + return userdata; + } else { + if ( memoryPool.size() <= 0 ) return uf::userdata::create(data); + pod::Userdata* userdata = new pod::Userdata; + auto allocation = memoryPool.allocate( data ); + userdata->len = allocation.size; + #if UF_USERDATA_KLUDGE + userdata->pointer = (uint8_t*) allocation.pointer; + #else + userdata->data = (uint8_t*) allocation.pointer; + #endif + return userdata; + } */ } // Easy way to get the userdata as a reference @@ -50,7 +130,7 @@ const T& uf::userdata::get( const pod::Userdata* userdata ) { union { uint8_t* original; const T* casted; - } cast; + } static cast; cast.original = userdata->data; return *cast.casted; } @@ -66,7 +146,7 @@ T& uf::Userdata::get() { union { uint8_t* original; T* casted; - } cast; + } static cast; cast.original = this->m_pod->data; return *cast.casted; } @@ -76,7 +156,7 @@ const T& uf::Userdata::get() const { union { uint8_t* original; const T* casted; - } cast; + } static cast; cast.original = this->m_pod->data; return *cast.casted; } \ No newline at end of file diff --git a/engine/src/engine/entity/entity.cpp b/engine/src/engine/entity/entity.cpp index 01159739..d515dbb2 100644 --- a/engine/src/engine/entity/entity.cpp +++ b/engine/src/engine/entity/entity.cpp @@ -1,11 +1,11 @@ #include +#include #include uf::Entity uf::Entity::null; -std::vector uf::Entity::entities; std::size_t uf::Entity::uids = 0; - -uf::Entity::Entity(bool shouldInitialize){ +uf::MemoryPool uf::Entity::memoryPool; +uf::Entity::Entity( bool shouldInitialize ){ if ( shouldInitialize ) this->initialize(); } uf::Entity::~Entity(){ @@ -26,15 +26,10 @@ uf::Entity& uf::Entity::addChild( uf::Entity& child ) { return child; } void uf::Entity::removeChild( uf::Entity& child ) { - for ( uf::Entity::container_t::iterator it = this->m_children.begin(); it != this->m_children.end(); ++it ) { - uf::Entity* entity = *it; - if ( &child == entity ) { - *it = NULL; - it = this->m_children.erase( it ); - child.setParent(); - return; - } - } + auto it = std::find( this->m_children.begin(), this->m_children.end(), &child ); + if ( it == this->m_children.end() ) return; + this->m_children.erase(it); + child.setParent(); } void uf::Entity::moveChild( uf::Entity& child ) { if ( !child.m_parent ) return; @@ -55,10 +50,7 @@ std::size_t uf::Entity::getUid() const { return this->m_uid; } void uf::Entity::initialize(){ - if ( this->m_uid == 0 ) { - uf::Entity::entities.push_back(this); - this->m_uid = ++uf::Entity::uids; - } + if ( this->m_uid == 0 ) this->m_uid = ++uf::Entity::uids; } void uf::Entity::destroy(){ for ( uf::Entity* kv : this->m_children ) { @@ -66,18 +58,14 @@ void uf::Entity::destroy(){ if ( kv->getUid() == 0 ) continue; kv->destroy(); kv->setParent(); + delete kv; } this->m_children.clear(); - - { - auto it = std::find(uf::Entity::entities.begin(), uf::Entity::entities.end(), this); - if ( it != uf::Entity::entities.end() ) { - uf::Entity::entities.erase(it); - // *it = NULL; - } - } - this->m_uid = 0; +/* + auto it = std::find(uf::Entity::entities.begin(), uf::Entity::entities.end(), this); + if ( it != uf::Entity::entities.end() ) uf::Entity::entities.erase(it); +*/ } void uf::Entity::tick(){ for ( uf::Entity* kv : this->m_children ) { @@ -94,6 +82,13 @@ void uf::Entity::render(){ } } +void* uf::Entity::operator new(size_t size, const std::string& type ) { + return type != "" && size == sizeof(uf::Entity) ? uf::instantiator::instantiate( type ) : uf::instantiator::alloc( size ); +} +void uf::Entity::operator delete( void* pointer ) { + uf::instantiator::free( (uf::Entity*) pointer ); +} + uf::Entity* uf::Entity::findByName( const std::string& name ) { for ( uf::Entity* entity : this->getChildren() ) { if ( entity->getName() == name ) return entity; @@ -158,11 +153,25 @@ void uf::Entity::process( std::function fn, int de } } */ +uf::Entity* uf::Entity::globalFindByUid( size_t uid ) { + for ( auto& allocation : uf::Entity::memoryPool.allocations() ) { + uf::Entity* entity = (uf::Entity*) allocation.pointer; + if ( entity->getUid() == uid ) return entity; + } + return NULL; +} uf::Entity* uf::Entity::globalFindByName( const std::string& name ) { + for ( auto& allocation : uf::Entity::memoryPool.allocations() ) { + uf::Entity* entity = (uf::Entity*) allocation.pointer; + if ( entity->getUid() == 0 ) continue; + if ( entity->getName() == name ) return entity; + } +/* for ( uf::Entity* e : uf::Entity::entities ) { if ( !e ) continue; if ( e->getUid() == 0 ) continue; if ( e->getName() == name ) return e; } +*/ return NULL; } \ No newline at end of file diff --git a/engine/src/engine/instantiator/instantiator.cpp b/engine/src/engine/instantiator/instantiator.cpp index a682ae50..fee1539e 100644 --- a/engine/src/engine/instantiator/instantiator.cpp +++ b/engine/src/engine/instantiator/instantiator.cpp @@ -4,9 +4,61 @@ std::unordered_map* uf::instantiator::names = NULL; std::unordered_map* uf::instantiator::map = NULL; +uf::Entity* uf::instantiator::alloc( size_t size ) { +/* + uf::Entity* pointer = (uf::Entity*) uf::Entity::memoryPool.alloc( NULL, size ); + std::cout << "malloc uf::Entity: "<< pointer <<" (size: " << size << ")" << std::endl; + return pointer; +*/ +// uf::MemoryPool& memoryPool = uf::MemoryPool::global.size() > 0 ? uf::MemoryPool::global : uf::Entity::memoryPool; +#if UF_MEMORYPOOL_INVALID_MALLOC + uf::MemoryPool& memoryPool = uf::Entity::memoryPool.size() > 0 ? uf::Entity::memoryPool : uf::MemoryPool::global; + return (uf::Entity*) memoryPool.alloc( NULL, size ); +#else + uf::Entity* pointer = NULL; + uf::MemoryPool* memoryPool = NULL; + if ( uf::Entity::memoryPool.size() > 0 ) + memoryPool = &uf::Entity::memoryPool; + else if ( uf::MemoryPool::global.size() > 0 ) + memoryPool = &uf::MemoryPool::global; + if ( memoryPool ) pointer = (uf::Entity*) memoryPool->alloc( NULL, size ); + else pointer = (uf::Entity*) malloc( size ); + return pointer; +#endif +/* + uf::Entity* pointer = ( memoryPool.size() <= 0 ) ? (uf::Entity*) ::malloc( size ) : (uf::Entity*) memoryPool.alloc( NULL, size ); + // std::cout << "malloc uf::Entity: "<< pointer <<" (size: " << size << ")" << std::endl; + return pointer; +*/ +/* + std::cout << "malloc uf::Entity (size: " << size << ")" << std::endl; + if ( memoryPool.size() <= 0 ) return (uf::Entity*) ::malloc( size ); + return (uf::Entity*) memoryPool.alloc( NULL, size ); +*/ +} +void uf::instantiator::free( uf::Entity* pointer ) { +// uf::MemoryPool& memoryPool = uf::MemoryPool::global.size() > 0 ? uf::MemoryPool::global : uf::Entity::memoryPool; +#if UF_MEMORYPOOL_INVALID_FREE + uf::MemoryPool& memoryPool = uf::Entity::memoryPool.size() > 0 ? uf::Entity::memoryPool : uf::MemoryPool::global; + memoryPool.free( pointer ); +#else + uf::MemoryPool* memoryPool = NULL; + if ( uf::Entity::memoryPool.size() > 0 ) + memoryPool = &uf::Entity::memoryPool; + else if ( uf::MemoryPool::global.size() > 0 ) + memoryPool = &uf::MemoryPool::global; + if ( memoryPool ) memoryPool->free( pointer ); + else ::free( pointer ); +#endif +/* + // std::cout << "free uf::Entity: " << pointer << std::endl; + if ( !uf::Entity::memoryPool.free( pointer ) ) + ::free(pointer); +*/ +} uf::Entity* uf::instantiator::instantiate( const std::string& name ) { + // std::cout << "instantiating " << name << std::endl; auto& map = *uf::instantiator::map; - // assert it's already in map assert( map.count(name) > 0 ); return map[name](); } \ No newline at end of file diff --git a/engine/src/engine/scene/scene.cpp b/engine/src/engine/scene/scene.cpp index f46a4e02..cb3d4e75 100644 --- a/engine/src/engine/scene/scene.cpp +++ b/engine/src/engine/scene/scene.cpp @@ -154,7 +154,7 @@ void uf::Scene::tick() { light.type.x = metadata["light"]["type"].asUInt64(); light.type.y = metadata["light"]["shadows"]["enabled"].asBool(); - + if ( !hasCompute && entity->hasComponent() ) { auto& renderMode = entity->getComponent(); auto& renderTarget = renderMode.renderTarget; @@ -250,12 +250,23 @@ const uf::Entity* uf::Scene::getController() const { return cachedController = this->findByName("Player"); // return this; } - +#include std::vector uf::scene::scenes; uf::Scene& uf::scene::loadScene( const std::string& name, const std::string& filename ) { uf::Scene* scene = (uf::Scene*) uf::instantiator::instantiate( name ); uf::scene::scenes.push_back(scene); - scene->load(filename != "" ? filename : "./scenes/"+uf::string::lowercase(name)+"/scene.json"); + + std::string target = name; + + std::regex regex("^(TestScene_?)?(.+?)(_?Scene)?$"); + std::smatch match; + + if ( std::regex_search( target, match, regex ) ) { + target = match[2]; + } + target = uf::string::lowercase( target ); + + scene->load(filename != "" ? filename : "./scenes/" + target + "/scene.json"); scene->initialize(); return *scene; } diff --git a/engine/src/ext/vulkan/graphic.cpp b/engine/src/ext/vulkan/graphic.cpp index 31b91866..81becf94 100644 --- a/engine/src/ext/vulkan/graphic.cpp +++ b/engine/src/ext/vulkan/graphic.cpp @@ -15,10 +15,11 @@ ext::vulkan::Shader::~Shader() { if ( !aliased ) destroy(); } */ -void ext::vulkan::Shader::initialize( ext::vulkan::Device& device, const std::string& filename, VkShaderStageFlagBits stage ) { +void ext::vulkan::Shader::initialize( ext::vulkan::Device& _device, const std::string& filename, VkShaderStageFlagBits stage ) { + auto& device = &_device ? _device : ext::vulkan::device; + this->device = &device; ext::vulkan::Buffers::initialize( device ); aliased = false; - this->device = &device; std::string spirv; @@ -61,9 +62,11 @@ void ext::vulkan::Shader::initialize( ext::vulkan::Device& device, const std::st auto parseResource = [&]( const spirv_cross::Resource& resource, VkDescriptorType descriptorType ) { switch ( descriptorType ) { case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER: { - auto& uniform = uniforms.emplace_back(); const auto& base_type = comp.get_type(resource.base_type_id); - uniform.create( comp.get_declared_struct_size(base_type) ); + size_t size = comp.get_declared_struct_size(base_type); + if ( size <= 0 ) break; + auto& uniform = uniforms.emplace_back(); + uniform.create( size ); } break; /* case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER: { @@ -96,12 +99,12 @@ void ext::vulkan::Shader::initialize( ext::vulkan::Device& device, const std::st #undef LOOP_RESOURCES for ( const auto& resource : res.push_constant_buffers ) { - auto& pushConstant = pushConstants.emplace_back(); const auto& type = comp.get_type(resource.base_type_id); size_t size = comp.get_declared_struct_size(type); + if ( size <= 0 ) continue; + auto& pushConstant = pushConstants.emplace_back(); pushConstant.create( size ); } - size_t specializationSize = 0; for ( const auto& constant : comp.get_specialization_constants() ) { @@ -171,7 +174,7 @@ void ext::vulkan::Shader::initialize( ext::vulkan::Device& device, const std::st } ); } - // update uniform buffers + // update uniform buffers for ( auto& uniform : uniforms ) { pod::Userdata& userdata = uniform.data(); initializeBuffer( @@ -182,7 +185,6 @@ void ext::vulkan::Shader::initialize( ext::vulkan::Device& device, const std::st false ); } - } void ext::vulkan::Shader::destroy() { @@ -218,12 +220,17 @@ void ext::vulkan::Pipeline::initialize( Graphic& graphic ) { std::size_t offset = 0; for ( auto& pushConstant : shader.pushConstants ) { + size_t len = pushConstant.data().len; + if ( len <= 0 || len > device.properties.limits.maxPushConstantsSize ) { + std::cout << "INVALID PUSH CONSTANT LEN OF : " << len << " FOR " << shader.filename << std::endl; + len = 4; + } pushConstantRanges.push_back(ext::vulkan::initializers::pushConstantRange( shader.descriptor.stage, - pushConstant.data().len, + len, offset )); - offset += pushConstant.data().len; + offset += len; } } for ( auto& descriptor : descriptorSetLayoutBindings ) { diff --git a/engine/src/spec/window/universal.cpp b/engine/src/spec/window/universal.cpp index 4a357235..ce30d3e9 100644 --- a/engine/src/spec/window/universal.cpp +++ b/engine/src/spec/window/universal.cpp @@ -11,8 +11,14 @@ void UF_API_CALL spec::uni::Window::pushEvent( const uf::ReadableHook::name_t& n this->m_events.readable.push({name, argument}); } void UF_API_CALL spec::uni::Window::pushEvent( const uf::OptimalHook::name_t& name, const uf::OptimalHook::argument_t& argument ) { - if ( uf::hooks.prefersReadable() ) return; - if ( !uf::hooks.exists(name) ) return; + if ( uf::hooks.prefersReadable() ) { + uf::userdata::destroy(argument); + return; + } + if ( !uf::hooks.exists(name) ) { + uf::userdata::destroy(argument); + return; + } this->m_events.optimal.push({name}); this->m_events.optimal.back().argument = argument; } @@ -31,7 +37,10 @@ void UF_API_CALL spec::uni::Window::pushEvent( const uf::OptimalHook::argument_t // Header header = userdata.get
(); const Header& header = uf::userdata::get
(userdata); this->m_events.optimal.push({header.type}); - if ( !uf::hooks.exists(header.type) ) return; + if ( !uf::hooks.exists(header.type) ) { + uf::userdata::destroy(userdata); + return; + } this->m_events.optimal.back().argument = userdata; } void UF_API_CALL spec::uni::Window::processEvents() { diff --git a/engine/src/utils/component/component.cpp b/engine/src/utils/component/component.cpp index 28119c96..fdf7bcfc 100644 --- a/engine/src/utils/component/component.cpp +++ b/engine/src/utils/component/component.cpp @@ -1,9 +1,17 @@ #include +UF_API uf::MemoryPool uf::component::memoryPool; uf::Component::~Component() { for ( auto& kv : this->m_container ) { pod::Component& component = kv.second; - uf::userdata::destroy(component.userdata); + uf::userdata::destroy(uf::component::memoryPool, component.userdata); + /* + if ( uf::component::memoryPool.size() > 0 ) { + uf::userdata::destroy(uf::component::memoryPool, component.userdata); + } else { + uf::userdata::destroy(component.userdata); + } + */ } } diff --git a/engine/src/utils/mempool/mempool.cpp b/engine/src/utils/mempool/mempool.cpp new file mode 100644 index 00000000..182c6546 --- /dev/null +++ b/engine/src/utils/mempool/mempool.cpp @@ -0,0 +1,258 @@ +#include +#include + +#include +#include +#include +#include + +namespace { + bool IGNORE_GLOBAL_MEMORYPOOL = false; +} + +#define DEBUG_PRINT 0 + +bool uf::MemoryPool::globalOverride = true; +bool uf::MemoryPool::subPool = true; +uf::MemoryPool uf::MemoryPool::global; + +uf::MemoryPool::MemoryPool( size_t size ) { + if ( size > 0 ) this->initialize( size ); +} +uf::MemoryPool::~MemoryPool( ) { + this->destroy(); +} +size_t uf::MemoryPool::size() const { + return this->m_size; +} +size_t uf::MemoryPool::allocated() const { + size_t allocated = 0; + for ( auto& allocation : this->m_allocations ) { + allocated += allocation.size; + } + return allocated; +} +std::string uf::MemoryPool::stats() const { + uf::Serializer metadata; + + size_t size = this->size(); + size_t allocated = this->allocated(); + + metadata["size"] = size; + metadata["used"] = allocated; + metadata["free"] = size - allocated; + metadata["objects"] = this->m_allocations.size(); + { + std::stringstream ss; + ss << std::hex << (void*) this->m_pool; + metadata["pool"] = ss.str(); + } + + return metadata; +} +void uf::MemoryPool::initialize( size_t size ) { + if ( size <= 0 ) return; + if ( this->size() > 0 ) this->destroy(); +// this->m_pool = (uint8_t*) malloc( size ); +// this->m_allocations.reserve( 128 ); + this->m_size = size; + if ( uf::MemoryPool::subPool && uf::MemoryPool::global.size() > 0 && this != &uf::MemoryPool::global ) { + this->m_pool = (uint8_t*) uf::MemoryPool::global.alloc( NULL, size ); + } else { + this->m_pool = (uint8_t*) malloc( size ); + } +// this->m_pool = (uint8_t*) operator new( size ); + memset( this->m_pool, 0, size ); +} +void uf::MemoryPool::destroy() { + if ( this->size() <= 0 ) return; +// ::free(this->m_pool); + if ( uf::MemoryPool::subPool && this != &uf::MemoryPool::global ) { + uf::MemoryPool::global.free( this->m_pool ); + } else { + delete[] this->m_pool; + } + this->m_size = 0; + this->m_pool = NULL; +} + +pod::Allocation uf::MemoryPool::allocate( void* data, size_t size ) { + if ( UF_MEMORYPOOL_MUTEX ) this->m_mutex.lock(); + // find next available allocation + // RLE-esque + size_t index = 0; + size_t len = this->size(); + pod::Allocation allocation; + if ( len <= 0 ) { + if ( DEBUG_PRINT ) std::cout << "CANNOT MALLOC: " << size << ", POOL NOT INITIALIZED" << std::endl; + goto MANUAL_MALLOC; + } + // find any availble spots in-between existing allocations + { + auto next = this->m_allocations.begin(); + for ( auto it = next; it != this->m_allocations.end(); ++it ) { + index = it->index + it->size; + if ( ++next == this->m_allocations.end() ) break; + if ( index < next->index ) break; + } + if ( index + size > len ) { + std::cout << "MemoryPool: " << this << ": Out of Memory!" << std::endl; + goto MANUAL_MALLOC; + } + allocation.index = index; + allocation.size = size; + allocation.pointer = &this->m_pool[0] + index; + if ( data ) memcpy( allocation.pointer, data, size ); + else memset( allocation.pointer, 0, size ); + IGNORE_GLOBAL_MEMORYPOOL = true; + this->m_allocations.insert(next, allocation); + IGNORE_GLOBAL_MEMORYPOOL = false; + } + goto RETURN; +MANUAL_MALLOC: + if ( UF_MEMORYPOOL_INVALID_MALLOC ) { + allocation.index = -1; + allocation.size = size; + allocation.pointer = malloc(size); + } else { + throw; + } +RETURN: + if ( UF_MEMORYPOOL_MUTEX ) this->m_mutex.unlock(); + if ( DEBUG_PRINT ) std::cout << "MALLOC'd: " << allocation.pointer << ", " << allocation.size << ", " << allocation.index << std::endl; + return allocation; +} +void* uf::MemoryPool::alloc( void* data, size_t size ) { + auto allocation = this->allocate( data, size ); + return allocation.pointer; +} + +pod::Allocation& uf::MemoryPool::fetch( size_t index, size_t size ) { + static pod::Allocation missing; + for ( auto& allocation : this->m_allocations ) { + if ( index == allocation.index ) { + if ( (size > 0 && allocation.size == size) || (size == 0) ) return allocation; + } + } + return missing; +} +bool uf::MemoryPool::exists( void* pointer, size_t size ) { + // bound check + if ( pointer < &this->m_pool[0] ) return false; + size_t index = (uint8_t*) pointer - &this->m_pool[0]; + // size check + if ( size > 0 ) { + auto& allocation = this->fetch( index, size ); + return allocation.index == index && allocation.size == size; + } + return true; +} +bool uf::MemoryPool::free( void* pointer, size_t size ) { + if ( UF_MEMORYPOOL_MUTEX ) this->m_mutex.lock(); + if ( this->m_size <= 0 || pointer < &this->m_pool[0] || pointer >= &this->m_pool[0] + this->m_size ) { + if ( DEBUG_PRINT ) std::cout << "CANNOT FREE: " << pointer << ", ERROR: " << (this->m_size <= 0) << " " << (pointer < &this->m_pool[0]) << " " << (pointer >= &this->m_pool[0] + this->m_size) << std::endl; + goto MANUAL_FREE; + } + { + size_t index = (uint8_t*) pointer - &this->m_pool[0]; + auto it = this->m_allocations.begin(); + pod::Allocation allocation; + for ( ; it != this->m_allocations.end(); ++it ) { + if ( it->index == index ) { + allocation = *it; + break; + } + } + // if ( allocation.index != index || (size > 0 && allocation.size != size) ) { + if ( allocation.index != index ) { + if ( DEBUG_PRINT ) std::cout << "CANNOT FREE: " << pointer << ", NOT FOUND" << std::endl; + goto MANUAL_FREE; + } + if (size > 0 && allocation.size != size) { + if ( DEBUG_PRINT ) std::cout << "CANNOT FREE: " << pointer << ", MISMATCHED SIZES (" << size << " != " << allocation.size << ")" << std::endl; + goto MANUAL_FREE; + } + if ( DEBUG_PRINT ) std::cout << "FREE'D ALLOCATION: " << pointer << ", " << size << "\t" << allocation.pointer << ", " << allocation.size << ", " << allocation.index << std::endl; + this->m_allocations.erase(it); + if ( UF_MEMORYPOOL_MUTEX ) this->m_mutex.unlock(); + return true; + } +MANUAL_FREE: + if ( UF_MEMORYPOOL_MUTEX ) this->m_mutex.unlock(); + if ( UF_MEMORYPOOL_INVALID_FREE ) ::free(pointer); + return false; +} + +const uf::MemoryPool::allocations_t& uf::MemoryPool::allocations() const { + return this->m_allocations; +} +#if UF_MEMORYPOOL_OVERRIDE_NEW_DELETE +void* operator new( size_t size ) { + if ( !uf::MemoryPool::globalOverride || IGNORE_GLOBAL_MEMORYPOOL || uf::MemoryPool::global.size() <= 0 ) + return malloc(size); + return uf::MemoryPool::global.alloc( (void*) NULL, size ); +} +void operator delete( void* pointer ) { + if ( !uf::MemoryPool::globalOverride ) return ::free( pointer ); + uf::MemoryPool::global.free( pointer ); +} +#endif +/* +uf::MemoryPool::Iterator uf::MemoryPool::begin() { + static pod::Allocation end; + end = { + .index = 0 + .size = 0, + .pointer = this->m_pool + }; + struct { + pod::Allocation* begin = NULL; + pod::Allocation* end = NULL; + pod::Allocation* next = NULL; + } pointers; + pointers.begin = &end; + pointers.end = &end; + pointers.prev = &end; + + if ( this->m_allocations.size() > 0 ) { + auto it = this->m_allocations.begin(); + pointers.begin = &(*it++); + if ( it != this->m_allocations.end() ) + pointers.next = &(*it); + } + + return { + .pool = *this, + .allocation = *pointers.begin, + .prev = *pointers.end, + .next = *pointers.next + }; +} +uf::MemoryPool::Iterator uf::MemoryPool::end() { + static pod::Allocation end; + end = { + .index = this->m_size, + .size = this->m_size, + .pointer = this->m_pool + this->m_size; + } + struct { + pod::Allocation* end = NULL; + pod::Allocation* prev = NULL; + } pointers; + + if ( this->m_allocations.size() > 0 ) { + auto it = this->m_allocations.begin(); + pointers.end = &(*it++); + if ( it != this->m_allocations.end() ) + pointers.prev = &(*it); + } + pointers.prev = &end; + + return { + .pool = *this, + .allocation = *pointers.end, + .prev = *pointers.prev, + .next = *pointers.end + }; +} +*/ \ No newline at end of file diff --git a/engine/src/utils/renderer/renderer.cpp b/engine/src/utils/renderer/renderer.cpp new file mode 100644 index 00000000..a8a25614 --- /dev/null +++ b/engine/src/utils/renderer/renderer.cpp @@ -0,0 +1 @@ +#include \ No newline at end of file diff --git a/engine/src/utils/userdata/userdata.cpp b/engine/src/utils/userdata/userdata.cpp index a5363f45..eca20f28 100644 --- a/engine/src/utils/userdata/userdata.cpp +++ b/engine/src/utils/userdata/userdata.cpp @@ -2,6 +2,7 @@ #include // base64 #include // malloc, free #include // memcpy +#include // Description: Allows abstract creation of anything // Use: hook subsystem for passing hook data @@ -9,24 +10,129 @@ * [?] create a singleton class to destroy any remaining userdata */ -// Constructs a pod::Userdata struct with it's -pod::Userdata* UF_API uf::userdata::create( std::size_t len ) { - std::vector empty( len, 0 ); - return create( len, empty.data() ); -} +uf::MemoryPool uf::userdata::memoryPool; +// Constructs a pod::Userdata struct pod::Userdata* UF_API uf::userdata::create( std::size_t len, void* data ) { - if ( !data ) return NULL; - void* pointer = operator new( sizeof(pod::Userdata) + sizeof(uint8_t) * len ); // allocate data for the userdata struct, and then some + return uf::userdata::create( uf::userdata::memoryPool, len, data ); +/* + if ( uf::userdata::memoryPool.size() > 0 ) return uf::userdata::create( uf::userdata::memoryPool, len, data ); +#if UF_USERDATA_KLUDGE + void* pointer = operator new( sizeof(pod::Userdata) + len ); // allocate data for the userdata struct, and then some // void* pointer = malloc( sizeof(pod::Userdata) + sizeof(uint8_t) * len ); // allocate data for the userdata struct, and then some pod::Userdata& userdata = *(pod::Userdata*) pointer; // cast to reference - memcpy( userdata.data, data, len ); // copy contents from data to Userdata's data + if ( data ) memcpy( userdata.data, data, len ); // copy contents from data to Userdata's data + else memset( userdata.data, 0, len ); userdata.len = len; // don't forget to store its data's length! + userdata.pointer = NULL; //&userdata.data[0]; return &userdata; // return address of userdata +#else + pod::Userdata* userdata = new pod::Userdata; + userdata->len = len; + userdata->data = (uint8_t*) operator new(len); + if ( data ) memcpy( userdata->data, data, len ); + else memset( userdata->data, 0, len ); + return userdata; +#endif +*/ } void UF_API uf::userdata::destroy( pod::Userdata* userdata ) { -// free(userdata); + return uf::userdata::destroy( uf::userdata::memoryPool, userdata ); +/* + if ( uf::userdata::memoryPool.size() > 0 ) return uf::userdata::destroy( uf::userdata::memoryPool, userdata ); +#if UF_USERDATA_KLUDGE + if ( userdata->pointer ) return; // allocated with a pool, do not free delete[] userdata; +#else + delete[] userdata->data; + userdata->len = 0; + userdata->data = NULL; + delete userdata; +#endif +*/ } + +// +pod::Userdata* UF_API uf::userdata::create( uf::MemoryPool& requestedMemoryPool, std::size_t len, void* data ) { + if ( len <= 0 ) return NULL; +// uf::MemoryPool& memoryPool = uf::MemoryPool::global.size() > 0 ? uf::MemoryPool::global : requestedMemoryPool; +#if UF_MEMORYPOOL_INVALID_MALLOC + uf::MemoryPool& memoryPool = requestedMemoryPool.size() > 0 ? requestedMemoryPool : uf::MemoryPool::global; + pod::Userdata* userdata = (pod::Userdata*) memoryPool.alloc( data, sizeof(pod::Userdata) + len ); +#else + uf::MemoryPool* memoryPool = NULL; + if ( requestedMemoryPool.size() > 0 ) memoryPool = &requestedMemoryPool; + else if ( uf::MemoryPool::global.size() > 0 ) memoryPool = &uf::MemoryPool::global; + pod::Userdata* userdata = NULL; + if ( memoryPool ) + userdata = (pod::Userdata*) memoryPool->alloc( data, sizeof(pod::Userdata) + len ); + else { + userdata = (pod::Userdata*) operator new( sizeof(pod::Userdata) + len ); // allocate data for the userdata struct, and then some + if ( data ) memcpy( userdata->data, data, len ); + else memset( userdata->data, 0, len ); + } +#endif + userdata->len = len; + return userdata; +/* + if ( uf::userdata::memoryPool.size() > 0 ) { + auto allocation = uf::userdata::memoryPool.allocate( NULL, sizeof(pod::Userdata) + len ); + pod::Userdata* userdata = (pod::Userdata*) allocation.pointer; + if ( data ) memcpy( userdata->data, data, len ); + else memset( userdata->data, 0, len ); + userdata->len = len; + #if UF_USERDATA_KLUDGE + userdata->pointer = NULL; + #endif + return userdata; + } else { + if ( memoryPool.size() <= 0 ) return uf::userdata::create( len, data ); + pod::Userdata* userdata = new pod::Userdata; + auto allocation = memoryPool.allocate( data, len ); + userdata->len = allocation.size; + #if UF_USERDATA_KLUDGE + userdata->pointer = (uint8_t*) allocation.pointer; + #else + userdata->data = (uint8_t*) allocation.pointer; + #endif + return userdata; + } +*/ +} +#include +void UF_API uf::userdata::destroy( uf::MemoryPool& requestedMemoryPool, pod::Userdata* userdata ) { +// uf::MemoryPool& memoryPool = uf::MemoryPool::global.size() > 0 ? uf::MemoryPool::global : requestedMemoryPool; + +#if UF_MEMORYPOOL_INVALID_FREE + uf::MemoryPool& memoryPool = requestedMemoryPool.size() > 0 ? requestedMemoryPool : uf::MemoryPool::global; + memoryPool.free( userdata, sizeof(pod::Userdata) + userdata->len ); +#else + uf::MemoryPool* memoryPool = NULL; + if ( requestedMemoryPool.size() > 0 ) memoryPool = &requestedMemoryPool; + else if ( uf::MemoryPool::global.size() > 0 ) memoryPool = &uf::MemoryPool::global; + + if ( memoryPool ) memoryPool->free( userdata, sizeof(pod::Userdata) + userdata->len ); + else { + // free(userdata); + delete[] userdata; + } +#endif +/* + if ( uf::userdata::memoryPool.size() > 0 ) { + memoryPool.free( userdata, sizeof(pod::Userdata) + userdata->len ); + userdata->len = 0; + } else { + #if UF_USERDATA_KLUDGE + if ( !userdata->pointer ) { + uf::userdata::destroy( userdata ); + return; + } + #endif + memoryPool.free( userdata->data, userdata->len ); + delete userdata; + } +*/ +} + std::string UF_API uf::userdata::toBase64( pod::Userdata* userdata ) { return uf::base64::encode( userdata->data, userdata->len ); } @@ -49,25 +155,29 @@ uf::Userdata::Userdata( Userdata&& move ) : } // Copy c-tor uf::Userdata::Userdata( const Userdata& copy ) { - if ( copy.m_pod ) this->create( copy.m_pod->len, copy.m_pod->data ); + if ( copy.m_pod && copy.m_pod->len ) this->create( copy.m_pod->len, copy.m_pod->data ); } // Creates the POD pod::Userdata* uf::Userdata::create( std::size_t len ) { + if ( len <= 0 ) return NULL; this->destroy(); - return this->m_pod = uf::userdata::create( len ); + return this->m_pod = uf::userdata::create( len, NULL ); } pod::Userdata* uf::Userdata::create( std::size_t len, void* data ) { + if ( len <= 0 ) return NULL; this->destroy(); return this->m_pod = uf::userdata::create( len, data ); } void uf::Userdata::move( Userdata&& move ) { // if ( this->m_pod && move.m_pod ) uf::userdata::move( *this->m_pod, *move.m_pod ); + std::cout << "MOVE: " << this << " <- " << &move << ", " << this->m_pod << " <- " << move.m_pod << std::endl; this->m_pod = move.m_pod; - move.m_pod = NULL;; + move.m_pod = NULL; } void uf::Userdata::copy( const Userdata& copy ) { // if ( this->m_pod && copy.m_pod ) uf::userdata::copy( *this->m_pod, *copy.m_pod ); + std::cout << "COPY: " << this << " <- " << © << ", " << this->m_pod << " <- " << copy.m_pod << std::endl; if ( copy.m_pod ) this->create( copy.m_pod->len, copy.m_pod->data ); } // D-tor @@ -95,10 +205,14 @@ bool uf::Userdata::initialized() const { } // Overloaded ops uf::Userdata::operator void*() { - return this->m_pod ? (void*) this->m_pod->data : NULL; + // return this->m_pod ? (void*) this->m_pod->data : NULL; + if ( !this->m_pod ) return NULL; + return this->m_pod->data; } uf::Userdata::operator void*() const { - return this->m_pod ? (void*) this->m_pod->data : NULL; + // return this->m_pod ? (void*) this->m_pod->data : NULL; + if ( !this->m_pod ) return NULL; + return this->m_pod->data; } uf::Userdata::operator bool() const { diff --git a/ext/gui/gui.cpp b/ext/gui/gui.cpp index f48f0d8e..9150c6f4 100644 --- a/ext/gui/gui.cpp +++ b/ext/gui/gui.cpp @@ -18,9 +18,7 @@ #include #include -#include -#include -#include +#include #include #include @@ -40,8 +38,8 @@ namespace { struct { pod::Vector2ui current = { - ext::vulkan::width, - ext::vulkan::height, + uf::renderer::width, + uf::renderer::height, }; pod::Vector2ui reference = { 1280, @@ -1022,7 +1020,7 @@ void ext::Gui::render() { auto& scene = this->getRootParent(); auto& controller = *scene.getController(); auto& camera = controller.getComponent(); - auto* renderMode = (ext::vulkan::RenderTargetRenderMode*) &ext::vulkan::getRenderMode("Gui"); + auto* renderMode = (uf::renderer::RenderTargetRenderMode*) &uf::renderer::getRenderMode("Gui"); if ( renderMode->getName() == "Gui" ) { auto& blitter = renderMode->blitter; auto& metadata = controller.getComponent(); @@ -1062,7 +1060,7 @@ void ext::Gui::render() { metadata["overlay"]["orientation"][2].asFloat(), metadata["overlay"]["orientation"][3].asFloat(), }; - if ( ext::openvr::enabled && (metadata["overlay"]["enabled"].asBool() || ext::vulkan::getRenderMode("Stereoscopic Deferred", true).getType() == "Stereoscopic Deferred" )) { + if ( ext::openvr::enabled && (metadata["overlay"]["enabled"].asBool() || uf::renderer::getRenderMode("Stereoscopic Deferred", true).getType() == "Stereoscopic Deferred" )) { if ( metadata["overlay"]["floating"].asBool() ) { pod::Matrix4f model = uf::transform::model( transform ); uniforms.matrices.models[i] = camera.getProjection(i) * ext::openvr::hmdEyePositionMatrix( i == 0 ? vr::Eye_Left : vr::Eye_Right ) * model; diff --git a/ext/main.cpp b/ext/main.cpp index d48b3833..4c7f42da 100644 --- a/ext/main.cpp +++ b/ext/main.cpp @@ -31,15 +31,14 @@ #include #include +#include + #include "ext.h" #include #include -#include -#include -#include -#include +#include #include #include @@ -86,8 +85,53 @@ void EXT_API ext::initialize() { } ::config = ext::getConfig(); - /* Parse config */ { + // Set memory pool sizes + { + auto deduceSize = []( auto& value )->size_t{ + if ( value.isNumeric() ) return value.asUInt64(); + if ( value.isString() ) { + std::string str = value.asString(); + std::regex regex("^(\\d+) ?((?:K|M|G)?(?:i?B)?)$"); + std::smatch match; + if ( std::regex_search( str, match, regex ) ) { + size_t requested = std::stoi( match[1].str() ); + std::string prefix = match[2].str(); + switch ( prefix.at(0) ) { + case 'K': return requested * 1024; + case 'M': return requested * 1024 * 1024; + case 'G': return requested * 1024 * 1024 * 1024; + } + return requested; + } + } + return 0; + }; + { + size_t size = deduceSize( ::config["engine"]["memory pool"]["size"] ); + uf::MemoryPool::globalOverride = ::config["engine"]["memory pool"]["globalOverride"].asBool(); + std::cout << "Requesting " << (int) size << " bytes for global memory pool: " << &uf::MemoryPool::global << std::endl; + uf::MemoryPool::global.initialize( size ); + uf::MemoryPool::subPool = ::config["engine"]["memory pool"]["subPools"].asBool(); + if ( size <= 0 || uf::MemoryPool::subPool ) { + { + size_t size = deduceSize( ::config["engine"]["memory pools"]["component"] ); + std::cout << "Requesting " << (int) size << " bytes for component memory pool: " << &uf::component::memoryPool << std::endl; + uf::component::memoryPool.initialize( size ); + } + { + size_t size = deduceSize( ::config["engine"]["memory pools"]["userdata"] ); + std::cout << "Requesting " << (int) size << " bytes for userdata memory pool: " << &uf::userdata::memoryPool << std::endl; + uf::userdata::memoryPool.initialize( size ); + } + { + size_t size = deduceSize( ::config["engine"]["memory pools"]["entity"] ); + std::cout << "Requesting " << (int) size << " bytes for entity memory pool: " << &uf::Entity::memoryPool << std::endl; + uf::Entity::memoryPool.initialize( size ); + } + } + } + } /* Frame limiter */ { double limit = ::config["engine"]["frame limit"].asDouble(); if ( limit != 0 ) @@ -103,19 +147,19 @@ void EXT_API ext::initialize() { // Set worker threads uf::thread::workers = ::config["engine"]["worker threads"].asUInt64(); // Enable valiation layer - ext::vulkan::validation = ::config["engine"]["ext"]["vulkan"]["validation"]["enabled"].asBool(); + uf::renderer::validation = ::config["engine"]["ext"]["vulkan"]["validation"]["enabled"].asBool(); for ( int i = 0; i < ::config["engine"]["ext"]["vulkan"]["validation"]["filters"].size(); ++i ) { - ext::vulkan::validationFilters.push_back( ::config["engine"]["ext"]["vulkan"]["validation"]["filters"][i].asString() ); + uf::renderer::validationFilters.push_back( ::config["engine"]["ext"]["vulkan"]["validation"]["filters"][i].asString() ); } for ( int i = 0; i < ::config["engine"]["ext"]["vulkan"]["extensions"]["device"].size(); ++i ) { - ext::vulkan::requestedDeviceExtensions.push_back( ::config["engine"]["ext"]["vulkan"]["extensions"]["device"][i].asString() ); + uf::renderer::requestedDeviceExtensions.push_back( ::config["engine"]["ext"]["vulkan"]["extensions"]["device"][i].asString() ); } for ( int i = 0; i < ::config["engine"]["ext"]["vulkan"]["extensions"]["instance"].size(); ++i ) { - ext::vulkan::requestedInstanceExtensions.push_back( ::config["engine"]["ext"]["vulkan"]["extensions"]["instance"][i].asString() ); + uf::renderer::requestedInstanceExtensions.push_back( ::config["engine"]["ext"]["vulkan"]["extensions"]["instance"][i].asString() ); } for ( int i = 0; i < ::config["engine"]["ext"]["vulkan"]["features"].size(); ++i ) { - ext::vulkan::requestedDeviceFeatures.push_back( ::config["engine"]["ext"]["vulkan"]["features"][i].asString() ); + uf::renderer::requestedDeviceFeatures.push_back( ::config["engine"]["ext"]["vulkan"]["features"][i].asString() ); } ext::openvr::enabled = ::config["engine"]["ext"]["vr"]["enable"].asBool(); ext::openvr::swapEyes = ::config["engine"]["ext"]["vr"]["swap eyes"].asBool(); @@ -131,30 +175,30 @@ void EXT_API ext::initialize() { /* Create initial scene (kludge) */ { - uf::Scene* scene = new uf::Scene; - uf::scene::scenes.push_back(scene); - auto& metadata = scene->getComponent(); + uf::Scene& scene = uf::instantiator::instantiate(); //new uf::Scene; + uf::scene::scenes.push_back(&scene); + auto& metadata = scene.getComponent(); metadata["system"]["config"] = ::config; } /* Initialize Vulkan */ { - // ext::vulkan::width = ::config["window"]["size"]["x"].asInt(); - // ext::vulkan::height = ::config["window"]["size"]["y"].asInt(); + // uf::renderer::width = ::config["window"]["size"]["x"].asInt(); + // uf::renderer::height = ::config["window"]["size"]["y"].asInt(); // setup render mode if ( ::config["engine"]["render modes"]["gui"].asBool() ) { - auto* renderMode = new ext::vulkan::RenderTargetRenderMode; - ext::vulkan::addRenderMode( renderMode, "Gui" ); + auto* renderMode = new uf::renderer::RenderTargetRenderMode; + uf::renderer::addRenderMode( renderMode, "Gui" ); renderMode->blitter.descriptor.subpass = 1; } if ( ::config["engine"]["render modes"]["stereo deferred"].asBool() ) - ext::vulkan::addRenderMode( new ext::vulkan::StereoscopicDeferredRenderMode, "" ); + uf::renderer::addRenderMode( new uf::renderer::StereoscopicDeferredRenderMode, "" ); else if ( ::config["engine"]["render modes"]["deferred"].asBool() ) - ext::vulkan::addRenderMode( new ext::vulkan::DeferredRenderMode, "" ); + uf::renderer::addRenderMode( new uf::renderer::DeferredRenderMode, "" ); // if ( ::config["engine"]["render modes"]["compute"].asBool() ) - // ext::vulkan::addRenderMode( new ext::vulkan::ComputeRenderMode, "C:RT:0" ); + // uf::renderer::addRenderMode( new uf::renderer::ComputeRenderMode, "C:RT:0" ); if ( ext::openvr::enabled ) { ext::openvr::initialize(); @@ -162,14 +206,14 @@ void EXT_API ext::initialize() { uint32_t width, height; ext::openvr::recommendedResolution( width, height ); - auto& renderMode = ext::vulkan::getRenderMode("Stereoscopic Deferred", true); + auto& renderMode = uf::renderer::getRenderMode("Stereoscopic Deferred", true); renderMode.width = width; renderMode.height = height; std::cout << "Recommended VR Resolution: " << width << ", " << height << std::endl; } - ext::vulkan::initialize(); + uf::renderer::initialize(); } /* */ { pod::Thread& threadMain = uf::thread::has("Main") ? uf::thread::get("Main") : uf::thread::create( "Main", false, true ); @@ -237,7 +281,7 @@ void EXT_API ext::tick() { } uf::iostream << "\n"; }; - for ( uf::Scene* scene : ext::vulkan::scenes ) { + for ( uf::Scene* scene : uf::renderer::scenes ) { if ( !scene ) continue; std::cout << "Scene: " << scene->getName() << ": " << scene << std::endl; scene->process(filter, 1); @@ -248,7 +292,21 @@ void EXT_API ext::tick() { static uf::Timer timer(false); if ( !timer.running() ) timer.start(); if ( uf::Window::isKeyPressed("P") && timer.elapsed().asDouble() >= 1 ) { timer.reset(); - uf::iostream << ext::vulkan::allocatorStats() << "\n"; + // uf::iostream << uf::renderer::allocatorStats() << "\n"; + if ( uf::MemoryPool::global.size() > 0 ) uf::iostream << "Global Memory Pool:\n" << uf::MemoryPool::global.stats() << "\n"; + if ( uf::Entity::memoryPool.size() > 0 ) uf::iostream << "Entity Memory Pool:\n" << uf::Entity::memoryPool.stats() << "\n"; + if ( uf::component::memoryPool.size() > 0 ) uf::iostream << "Components Memory Pool:\n" << uf::component::memoryPool.stats() << "\n"; + if ( uf::userdata::memoryPool.size() > 0 ) uf::iostream << "Userdata Memory Pool:\n" << uf::userdata::memoryPool.stats() << "\n"; + /* + size_t size = uf::component::memoryPool.size(); + size_t allocated = uf::component::memoryPool.allocated(); + uf::iostream << "Memory Pools:\n" + << "\tComponents:\n" + << "\t\tSize : " << size << "\n" + << "\t\tAllocated: " << allocated << "\n" + << "\t\tFree : " << (size-allocated) + << "\n"; + */ } } /* Attempt to reset VR position */ { @@ -301,7 +359,7 @@ void EXT_API ext::tick() { } /* Update vulkan */ { - ext::vulkan::tick(); + uf::renderer::tick(); } /* Discord */ if ( ::config["engine"]["ext"]["discord"]["enable"].asBool() ) { ext::discord::tick(); @@ -322,7 +380,7 @@ void EXT_API ext::tick() { void EXT_API ext::render() { // uf::scene::render(); - ext::vulkan::render(); + uf::renderer::render(); /* OpenVR */ if ( ext::openvr::context ) { ext::openvr::submit(); @@ -338,7 +396,7 @@ void EXT_API ext::terminate() { } /* Close vulkan */ { - ext::vulkan::destroy(); + uf::renderer::destroy(); } @@ -385,25 +443,28 @@ std::string EXT_API ext::getConfig() { file.exists = config.file.readFromFile(file.filename); } /* Initialize default configuration */ { - config.fallback["window"]["terminal"]["ncurses"] = true; - config.fallback["window"]["terminal"]["visible"] = true; - config.fallback["window"]["title"] = "Grimgram"; - config.fallback["window"]["icon"] = ""; - config.fallback["window"]["size"]["x"] = 0; - config.fallback["window"]["size"]["y"] = 0; - config.fallback["window"]["visible"] = true; - // config.fallback["window"]["fullscreen"] = false; - config.fallback["window"]["mode"] = "windowed"; - config.fallback["window"]["cursor"]["visible"] = true; - config.fallback["window"]["cursor"]["center"] = false; - config.fallback["window"]["keyboard"]["repeat"] = true; + config.fallback["window"]["terminal"]["ncurses"] = true; + config.fallback["window"]["terminal"]["visible"] = true; + config.fallback["window"]["title"] = "Grimgram"; + config.fallback["window"]["icon"] = ""; + config.fallback["window"]["size"]["x"] = 0; + config.fallback["window"]["size"]["y"] = 0; + config.fallback["window"]["visible"] = true; + // config.fallback["window"]["fullscreen"] = false; + config.fallback["window"]["mode"] = "windowed"; + config.fallback["window"]["cursor"]["visible"] = true; + config.fallback["window"]["cursor"]["center"] = false; + config.fallback["window"]["keyboard"]["repeat"] = true; - config.fallback["engine"]["scenes"]["start"] = "StartMenu"; - config.fallback["engine"]["scenes"]["max lights"] = 32; - config.fallback["engine"]["hook"]["mode"] = "Readable"; - config.fallback["engine"]["frame limit"] = 60; - config.fallback["engine"]["delta limit"] = 120; - config.fallback["engine"]["worker threads"] = 1; + config.fallback["engine"]["scenes"]["start"] = "StartMenu"; + config.fallback["engine"]["scenes"]["max lights"] = 32; + config.fallback["engine"]["hook"]["mode"] = "Readable"; + config.fallback["engine"]["frame limit"] = 60; + config.fallback["engine"]["delta limit"] = 120; + config.fallback["engine"]["worker threads"] = 1; + config.fallback["engine"]["memory pool"]["size"] = "512 MiB"; + config.fallback["engine"]["memory pool"]["globalOverride"] = false; + config.fallback["engine"]["memory pool"]["subPools"] = true; } /* Merge */ if ( file.exists ){ config.merged = config.file; diff --git a/ext/scenes/test/scene.cpp b/ext/scenes/map/scene.cpp similarity index 88% rename from ext/scenes/test/scene.cpp rename to ext/scenes/map/scene.cpp index 046b109c..eaafddae 100644 --- a/ext/scenes/test/scene.cpp +++ b/ext/scenes/map/scene.cpp @@ -1,4 +1,5 @@ #include "scene.h" + #include #include #include @@ -12,10 +13,8 @@ #include #include -#include -#include -#include -#include +#include + #include #include @@ -23,8 +22,8 @@ #include "../../ext.h" #include "../../gui/gui.h" -EXT_OBJECT_REGISTER_CPP(TestScene) -void ext::TestScene::initialize() { +EXT_OBJECT_REGISTER_CPP(TestScene_Map) +void ext::TestScene_Map::initialize() { uf::Scene::initialize(); uf::Serializer& metadata = this->getComponent(); uf::Asset& assetLoader = this->getComponent(); @@ -59,7 +58,7 @@ void ext::TestScene::initialize() { ext::Gui* manager = (ext::Gui*) this->findByName("Gui Manager"); if ( !manager ) return "false"; uf::Serializer payload; - ext::Gui* gui = (ext::Gui*) manager->findByUid( (payload["uid"] = manager->loadChild("/scenes/world/gui/pause/menu.json", false)).asUInt64() ); + ext::Gui* gui = (ext::Gui*) manager->findByUid( (payload["uid"] = manager->loadChild("/scenes/worldscape/gui/pause/menu.json", false)).asUInt64() ); uf::Serializer& metadata = gui->getComponent(); metadata["menu"] = json["menu"]; gui->initialize(); @@ -68,8 +67,8 @@ void ext::TestScene::initialize() { } /* store viewport size */ { - metadata["window"]["size"]["x"] = ext::vulkan::width; - metadata["window"]["size"]["y"] = ext::vulkan::height; + metadata["window"]["size"]["x"] = uf::renderer::width; + metadata["window"]["size"]["y"] = uf::renderer::height; this->addHook( "window:Resized", [&](const std::string& event)->std::string{ uf::Serializer json = event; @@ -94,10 +93,10 @@ void ext::TestScene::initialize() { } } -void ext::TestScene::render() { +void ext::TestScene_Map::render() { uf::Scene::render(); } -void ext::TestScene::tick() { +void ext::TestScene_Map::tick() { uf::Scene::tick(); uf::Serializer& metadata = this->getComponent(); diff --git a/ext/scenes/test/scene.h b/ext/scenes/map/scene.h similarity index 79% rename from ext/scenes/test/scene.h rename to ext/scenes/map/scene.h index 96b6ec51..7d8d6569 100644 --- a/ext/scenes/test/scene.h +++ b/ext/scenes/map/scene.h @@ -6,7 +6,7 @@ #include namespace ext { - class EXT_API TestScene : public uf::Scene { + class EXT_API TestScene_Map : public uf::Scene { public: virtual void initialize(); virtual void tick(); diff --git a/ext/scenes/marchingcubes/scene.cpp b/ext/scenes/marchingcubes/scene.cpp new file mode 100644 index 00000000..5edd9f70 --- /dev/null +++ b/ext/scenes/marchingcubes/scene.cpp @@ -0,0 +1,173 @@ +#include "scene.h" +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include + +#include +#include + +#include + +#include "../../ext.h" +#include "../../gui/gui.h" + +EXT_OBJECT_REGISTER_CPP(TestScene_MarchingCubes) +void ext::TestScene_MarchingCubes::initialize() { + uf::Scene::initialize(); + uf::Serializer& metadata = this->getComponent(); + uf::Asset& assetLoader = this->getComponent(); + + this->addHook( "system:Quit.%UID%", [&](const std::string& event)->std::string{ + std::cout << event << std::endl; + ext::ready = false; + return "true"; + }); + { + static uf::Timer timer(false); + if ( !timer.running() ) timer.start(); + this->addHook( "world:Entity.LoadAsset", [&](const std::string& event)->std::string{ + uf::Serializer json = event; + + std::string asset = json["asset"].asString(); + std::string uid = json["uid"].asString(); + + assetLoader.load(asset, "asset:Load." + uid); + + return "true"; + }); + } + { + static uf::Timer timer(false); + if ( !timer.running() ) timer.start(); + this->addHook( "menu:Pause", [&](const std::string& event)->std::string{ + if ( timer.elapsed().asDouble() < 1 ) return "false"; + timer.reset(); + + uf::Serializer json = event; + ext::Gui* manager = (ext::Gui*) this->findByName("Gui Manager"); + if ( !manager ) return "false"; + uf::Serializer payload; + ext::Gui* gui = (ext::Gui*) manager->findByUid( (payload["uid"] = manager->loadChild("/scenes/worldscape/gui/pause/menu.json", false)).asUInt64() ); + uf::Serializer& metadata = gui->getComponent(); + metadata["menu"] = json["menu"]; + gui->initialize(); + return payload; + }); + } + + /* store viewport size */ { + metadata["window"]["size"]["x"] = uf::renderer::width; + metadata["window"]["size"]["y"] = uf::renderer::height; + + this->addHook( "window:Resized", [&](const std::string& event)->std::string{ + uf::Serializer json = event; + + pod::Vector2ui size; { + size.x = json["window"]["size"]["x"].asUInt64(); + size.y = json["window"]["size"]["y"].asUInt64(); + } + + metadata["window"] = json["window"]; + + return "true"; + }); + } + + // lock control + { + uf::Serializer payload; + payload["state"] = true; + uf::hooks.call("window:Mouse.CursorVisibility", payload); + uf::hooks.call("window:Mouse.Lock"); + } +} + +void ext::TestScene_MarchingCubes::render() { + uf::Scene::render(); +} +void ext::TestScene_MarchingCubes::tick() { + uf::Scene::tick(); + + uf::Serializer& metadata = this->getComponent(); + uf::Asset& assetLoader = this->getComponent(); + + /* Regain control if nothing requests it */ { + ext::Gui* menu = (ext::Gui*) this->findByName("Gui: Menu"); + if ( !menu ) { + uf::Serializer payload; + payload["state"] = false; + uf::hooks.call("window:Mouse.CursorVisibility", payload); + uf::hooks.call("window:Mouse.Lock"); + } + } + + /* Updates Sound Listener */ { + pod::Transform<>& transform = this->getController()->getComponent>(); + + ext::oal.listener( "POSITION", { transform.position.x, transform.position.y, transform.position.z } ); + ext::oal.listener( "VELOCITY", { 0, 0, 0 } ); + ext::oal.listener( "ORIENTATION", { 0, 0, 1, 1, 0, 0 } ); + } + + /* Collision */ { + bool local = false; + bool sort = false; + bool useStrongest = false; + // pod::Thread& thread = uf::thread::fetchWorker(); + pod::Thread& thread = uf::thread::has("Physics") ? uf::thread::get("Physics") : uf::thread::create( "Physics", true, false ); + auto function = [&]() -> int { + std::vector entities; + std::function filter = [&]( uf::Entity* entity ) { + auto& metadata = entity->getComponent(); + if ( !metadata["system"]["physics"]["collision"].isNull() && !metadata["system"]["physics"]["collision"].asBool() ) return; + if ( entity->hasComponent() ) + entities.push_back((uf::Object*) entity); + }; + this->process(filter); + auto onCollision = []( pod::Collider::Manifold& manifold, uf::Object* a, uf::Object* b ){ + uf::Serializer payload; + payload["normal"][0] = manifold.normal.x; + payload["normal"][1] = manifold.normal.y; + payload["normal"][2] = manifold.normal.z; + payload["entity"] = b->getUid(); + payload["depth"] = -manifold.depth; + a->callHook("world:Collision.%UID%", payload); + + payload["entity"] = a->getUid(); + payload["depth"] = manifold.depth; + b->callHook("world:Collision.%UID%", payload); + }; + auto testColliders = [&]( uf::Collider& colliderA, uf::Collider& colliderB, uf::Object* a, uf::Object* b, bool useStrongest ){ + pod::Collider::Manifold strongest; + auto manifolds = colliderA.intersects(colliderB); + for ( auto manifold : manifolds ) { + if ( manifold.colliding && manifold.depth > 0 ) { + if ( !useStrongest ) onCollision(manifold, a, b); + else if ( strongest.depth < manifold.depth ) strongest = manifold; + } + } + if ( useStrongest && strongest.colliding ) onCollision(strongest, a, b); + }; + // collide with others + for ( auto* _a : entities ) { + uf::Object& entityA = *_a; + for ( auto* _b : entities ) { if ( _a == _b ) continue; + uf::Object& entityB = *_b; + testColliders( entityA.getComponent(), entityB.getComponent(), &entityA, &entityB, useStrongest ); + } + } + + return 0; + }; + if ( local ) function(); else uf::thread::add( thread, function, true ); + } +} \ No newline at end of file diff --git a/ext/scenes/marchingcubes/scene.h b/ext/scenes/marchingcubes/scene.h new file mode 100644 index 00000000..453cdbef --- /dev/null +++ b/ext/scenes/marchingcubes/scene.h @@ -0,0 +1,15 @@ +#pragma once + +#include +#include +#include +#include + +namespace ext { + class EXT_API TestScene_MarchingCubes : public uf::Scene { + public: + virtual void initialize(); + virtual void tick(); + virtual void render(); + }; +} \ No newline at end of file diff --git a/ext/scenes/test/terrain/heightmap.cpp b/ext/scenes/marchingcubes/terrain/heightmap.cpp similarity index 100% rename from ext/scenes/test/terrain/heightmap.cpp rename to ext/scenes/marchingcubes/terrain/heightmap.cpp diff --git a/ext/scenes/test/terrain/heightmap.h b/ext/scenes/marchingcubes/terrain/heightmap.h similarity index 100% rename from ext/scenes/test/terrain/heightmap.h rename to ext/scenes/marchingcubes/terrain/heightmap.h diff --git a/ext/scenes/test/terrain/marching.cpp b/ext/scenes/marchingcubes/terrain/marching.cpp similarity index 100% rename from ext/scenes/test/terrain/marching.cpp rename to ext/scenes/marchingcubes/terrain/marching.cpp diff --git a/ext/scenes/test/terrain/marching.h b/ext/scenes/marchingcubes/terrain/marching.h similarity index 100% rename from ext/scenes/test/terrain/marching.h rename to ext/scenes/marchingcubes/terrain/marching.h diff --git a/ext/scenes/raytrace/scene.cpp b/ext/scenes/raytrace/scene.cpp index bfdf63d3..ddfba573 100644 --- a/ext/scenes/raytrace/scene.cpp +++ b/ext/scenes/raytrace/scene.cpp @@ -13,11 +13,7 @@ #include #include -#include -#include -#include -#include -#include +#include #include #include @@ -25,16 +21,16 @@ #include "../../ext.h" #include "../../gui/gui.h" -EXT_OBJECT_REGISTER_CPP(RaytracedScene) -void ext::RaytracedScene::initialize() { +EXT_OBJECT_REGISTER_CPP(TestScene_RayTracing) +void ext::TestScene_RayTracing::initialize() { uf::Scene::initialize(); uf::Serializer& metadata = this->getComponent(); uf::Asset& assetLoader = this->getComponent(); { - auto& renderMode = this->getComponent(); + auto& renderMode = this->getComponent(); std::string name = "C:RT:" + std::to_string((int) this->getUid()); - ext::vulkan::addRenderMode( &renderMode, name ); + uf::renderer::addRenderMode( &renderMode, name ); if ( metadata["light"]["shadows"]["resolution"].isArray() ) { renderMode.width = metadata["light"]["shadows"]["resolution"][0].asUInt64(); renderMode.height = metadata["light"]["shadows"]["resolution"][1].asUInt64(); @@ -66,7 +62,7 @@ void ext::RaytracedScene::initialize() { shapes.push_back( {{-1.0f, 0.0f, 0.0f, roomDim}, {1.0f, 0.0f, 0.0f}, 32.0f, 2} ); shapes.push_back( {{1.0f, 0.0f, 0.0f, roomDim}, {0.0f, 1.0f, 0.0f}, 32.0f, 2} ); } - renderMode.compute.device = &ext::vulkan::device; + renderMode.compute.device = &uf::renderer::device; renderMode.compute.initializeBuffer( (void*) shapes.data(), shapes.size() * sizeof(Shape), @@ -107,7 +103,7 @@ void ext::RaytracedScene::initialize() { ext::Gui* manager = (ext::Gui*) this->findByName("Gui Manager"); if ( !manager ) return "false"; uf::Serializer payload; - ext::Gui* gui = (ext::Gui*) manager->findByUid( (payload["uid"] = manager->loadChild("/scenes/world/gui/pause/menu.json", false)).asUInt64() ); + ext::Gui* gui = (ext::Gui*) manager->findByUid( (payload["uid"] = manager->loadChild("/scenes/worldscape/gui/pause/menu.json", false)).asUInt64() ); uf::Serializer& metadata = gui->getComponent(); metadata["menu"] = json["menu"]; gui->initialize(); @@ -116,8 +112,8 @@ void ext::RaytracedScene::initialize() { } /* store viewport size */ { - metadata["window"]["size"]["x"] = ext::vulkan::width; - metadata["window"]["size"]["y"] = ext::vulkan::height; + metadata["window"]["size"]["x"] = uf::renderer::width; + metadata["window"]["size"]["y"] = uf::renderer::height; this->addHook( "window:Resized", [&](const std::string& event)->std::string{ uf::Serializer json = event; @@ -142,24 +138,24 @@ void ext::RaytracedScene::initialize() { } } -void ext::RaytracedScene::render() { +void ext::TestScene_RayTracing::render() { uf::Scene::render(); } -void ext::RaytracedScene::destroy() { - if ( this->hasComponent() ) { - auto& renderMode = this->getComponent(); - ext::vulkan::removeRenderMode( &renderMode, false ); +void ext::TestScene_RayTracing::destroy() { + if ( this->hasComponent() ) { + auto& renderMode = this->getComponent(); + uf::renderer::removeRenderMode( &renderMode, false ); } uf::Scene::destroy(); } -void ext::RaytracedScene::tick() { +void ext::TestScene_RayTracing::tick() { uf::Scene::tick(); uf::Serializer& metadata = this->getComponent(); uf::Asset& assetLoader = this->getComponent(); #if 1 - if ( this->hasComponent() ) { - auto& renderMode = this->getComponent(); + if ( this->hasComponent() ) { + auto& renderMode = this->getComponent(); /* Add lights to scene */ if ( renderMode.compute.initialized ) { struct UniformDescriptor { alignas(16) pod::Matrix4f matrices[2]; @@ -202,7 +198,7 @@ void ext::RaytracedScene::tick() { if ( !entity || entity->getName() != "Light" ) return; entities.push_back(entity); }; - for ( uf::Scene* scene : ext::vulkan::scenes ) { if ( !scene ) continue; + for ( uf::Scene* scene : uf::renderer::scenes ) { if ( !scene ) continue; scene->process(filter); } { diff --git a/ext/scenes/raytrace/scene.h b/ext/scenes/raytrace/scene.h index 4563a59e..13cc7e39 100644 --- a/ext/scenes/raytrace/scene.h +++ b/ext/scenes/raytrace/scene.h @@ -6,7 +6,7 @@ #include namespace ext { - class EXT_API RaytracedScene : public uf::Scene { + class EXT_API TestScene_RayTracing : public uf::Scene { public: virtual void initialize(); virtual void tick(); diff --git a/ext/scenes/start/menu.cpp b/ext/scenes/start/menu.cpp index 03fca1be..db2bf75a 100644 --- a/ext/scenes/start/menu.cpp +++ b/ext/scenes/start/menu.cpp @@ -12,7 +12,7 @@ #include #include -#include +#include #include #include "../../ext.h" @@ -112,8 +112,8 @@ void ext::StartMenu::initialize() { }); /* store viewport size */ { - metadata["window"]["size"]["x"] = ext::vulkan::width; - metadata["window"]["size"]["y"] = ext::vulkan::height; + metadata["window"]["size"]["x"] = uf::renderer::width; + metadata["window"]["size"]["y"] = uf::renderer::height; this->addHook( "window:Resized", [&](const std::string& event)->std::string{ uf::Serializer json = event; diff --git a/ext/scenes/world/craeture/craeture.cpp b/ext/scenes/worldscape/craeture/craeture.cpp similarity index 96% rename from ext/scenes/world/craeture/craeture.cpp rename to ext/scenes/worldscape/craeture/craeture.cpp index e9458d9b..f812c138 100644 --- a/ext/scenes/world/craeture/craeture.cpp +++ b/ext/scenes/worldscape/craeture/craeture.cpp @@ -15,7 +15,8 @@ #include "..//battle.h" #include "../terrain/generator.h" -#include "../world.h" +#include "../scene.h" + #include EXT_OBJECT_REGISTER_CPP(Craeture) @@ -100,7 +101,7 @@ void ext::Craeture::initialize() { this->addHook( "asset:Cache.Sound.%UID%", [&](const std::string& event)->std::string{ uf::Serializer json = event; - ext::World& world = this->getRootParent(); + uf::Scene& world = uf::scene::getCurrentScene(); uf::Serializer& masterdata = world.getComponent(); std::string filename = json["filename"].asString(); @@ -137,8 +138,8 @@ void ext::Craeture::initialize() { if ( metadata["timers"]["hurt"].asFloat() < timer.elapsed().asDouble() ) { metadata["timers"]["hurt"] = timer.elapsed().asDouble() + 1.0f; - ext::World& world = this->getRootParent(); - uf::Asset& assetLoader = world.getComponent(); + uf::Scene& scene = uf::scene::getCurrentScene(); + uf::Asset& assetLoader = scene.getComponent(); assetLoader.cache("./data/audio/battle/hurt.ogg", "asset:Cache.Sound." + std::to_string(this->getUid())); } @@ -167,7 +168,7 @@ void ext::Craeture::tick() { uf::Object::tick(); uf::Serializer& metadata = this->getComponent(); - ext::World& world = this->getRootParent(); + uf::Scene& world = uf::scene::getCurrentScene(); uf::Serializer& pMetadata = world.getController()->getComponent(); if ( !pMetadata["system"]["control"].asBool() ) return; diff --git a/ext/scenes/world/craeture/craeture.h b/ext/scenes/worldscape/craeture/craeture.h similarity index 100% rename from ext/scenes/world/craeture/craeture.h rename to ext/scenes/worldscape/craeture/craeture.h diff --git a/ext/scenes/world/gui/battle.cpp b/ext/scenes/worldscape/gui/battle.cpp similarity index 94% rename from ext/scenes/world/gui/battle.cpp rename to ext/scenes/worldscape/gui/battle.cpp index 5fdc7a28..252cae90 100644 --- a/ext/scenes/world/gui/battle.cpp +++ b/ext/scenes/worldscape/gui/battle.cpp @@ -8,7 +8,7 @@ #include #include // #include -#include "../world.h" + #include "..//battle.h" #include @@ -42,18 +42,15 @@ namespace { ext::HousamoBattle* battleManager; - ext::World* world; uf::Serializer masterTableGet( const std::string& table ) { - if ( !world ) world = (ext::World*) uf::Entity::globalFindByName("World"); - if ( !world ) return std::string(); - uf::Serializer& mastertable = world->getComponent(); + uf::Scene& scene = uf::scene::getCurrentScene(); + uf::Serializer& mastertable = scene.getComponent(); return mastertable["system"]["mastertable"][table]; } uf::Serializer masterDataGet( const std::string& table, const std::string& key ) { - if ( !world ) world = (ext::World*) uf::Entity::globalFindByName("World"); - if ( !world ) return std::string(); - uf::Serializer& mastertable = world->getComponent(); + uf::Scene& scene = uf::scene::getCurrentScene(); + uf::Serializer& mastertable = scene.getComponent(); return mastertable["system"]["mastertable"][table][key]; } inline int64_t parseInt( const std::string& str ) { @@ -64,9 +61,8 @@ namespace { } void playSound( ext::GuiBattle& entity, const std::string& id, const std::string& key ) { - if ( !world ) world = (ext::World*) uf::Entity::globalFindByName("World"); - if ( !world ) return; - uf::Serializer& masterdata = world->getComponent(); + uf::Scene& scene = uf::scene::getCurrentScene(); + uf::Serializer& masterdata = scene.getComponent(); uf::Serializer cardData = masterDataGet("Card", id); uf::Serializer charaData = masterDataGet("Chara", cardData["character_id"].asString()); @@ -79,39 +75,36 @@ namespace { url = "/smtsamo/voice/voice_" + name + "_" + key + ".ogg"; } - uf::Asset& assetLoader = world->getComponent(); + uf::Asset& assetLoader = scene.getComponent(); assetLoader.cache(url, "asset:Cache.Voice." + std::to_string(entity.getUid())); } void playSound( ext::GuiBattle& entity, std::size_t uid, const std::string& key ) { - if ( !world ) world = (ext::World*) uf::Entity::globalFindByName("World"); - if ( !world ) return; + uf::Scene& scene = uf::scene::getCurrentScene(); uf::Serializer& pMetadata = entity.getComponent(); - uf::Serializer& masterdata = world->getComponent(); + uf::Serializer& masterdata = scene.getComponent(); - uf::Entity* = world->findByUid(uid); + uf::Entity* = scene.findByUid(uid); if ( ! ) return; uf::Serializer& metadata = ->getComponent(); std::string id = metadata[""]["id"].asString(); playSound( entity, id, key ); } void playSound( ext::GuiBattle& entity, const std::string& key ) { - if ( !world ) world = (ext::World*) uf::Entity::globalFindByName("World"); - if ( !world ) return; + uf::Scene& scene = uf::scene::getCurrentScene(); uf::Serializer& metadata = entity.getComponent(); - uf::Serializer& masterdata = world->getComponent(); + uf::Serializer& masterdata = scene.getComponent(); std::string url = "./data/audio/ui/" + key + ".ogg"; - uf::Asset& assetLoader = world->getComponent(); + uf::Asset& assetLoader = scene.getComponent(); assetLoader.cache(url, "asset:Cache.SFX." + std::to_string(entity.getUid())); } void playMusic( ext::GuiBattle& entity, const std::string& filename ) { - if ( !world ) world = (ext::World*) uf::Entity::globalFindByName("World"); - if ( !world ) return; - uf::Asset& assetLoader = world->getComponent(); - uf::Serializer& masterdata = world->getComponent(); + uf::Scene& scene = uf::scene::getCurrentScene(); + uf::Asset& assetLoader = scene.getComponent(); + uf::Serializer& masterdata = scene.getComponent(); - assetLoader.load(filename, "asset:Load." + std::to_string(world->getUid())); + assetLoader.load(filename, "asset:Load." + std::to_string(scene.getUid())); } std::string getKeyFromIndex( uf::Serializer& object, uint64_t index ) { uint64_t i = 0; @@ -155,6 +148,7 @@ namespace { void ext::GuiBattle::initialize() { ext::Gui::initialize(); + uf::Scene& scene = uf::scene::getCurrentScene(); uf::Serializer& metadata = this->getComponent(); battleManager = (ext::HousamoBattle*) this->getRootParent().findByName( "Battle Manager" ); @@ -168,7 +162,7 @@ void ext::GuiBattle::initialize() { if ( uf::string::extension(filename) != "ogg" ) return "false"; if ( filename == "" ) return "false"; - uf::Serializer& masterdata = world->getComponent(); + uf::Serializer& masterdata = scene.getComponent(); uf::Audio& sfx = this->getComponent().add(filename); /* uf::Audio& sfx = this->getComponent(); @@ -190,7 +184,7 @@ void ext::GuiBattle::initialize() { if ( uf::string::extension(filename) != "ogg" ) return "false"; if ( filename == "" ) return "false"; - uf::Serializer& masterdata = world->getComponent(); + uf::Serializer& masterdata = scene.getComponent(); uf::Audio& voice = this->getComponent(); if ( voice.playing() ) voice.stop(); voice = uf::Audio(); @@ -631,8 +625,8 @@ void ext::GuiBattle::initialize() { void ext::GuiBattle::tick() { if ( !battleManager ) return; - ext::World& world = this->getRootParent(); - uf::Serializer& masterdata = world.getComponent(); + uf::Scene& scene = uf::scene::getCurrentScene(); + uf::Serializer& masterdata = scene.getComponent(); uf::Serializer& bMetadata = this->getComponent(); static float alpha = 0.0f; diff --git a/ext/scenes/world/gui/battle.h b/ext/scenes/worldscape/gui/battle.h similarity index 100% rename from ext/scenes/world/gui/battle.h rename to ext/scenes/worldscape/gui/battle.h diff --git a/ext/scenes/world/gui/dialogue.cpp b/ext/scenes/worldscape/gui/dialogue.cpp similarity index 85% rename from ext/scenes/world/gui/dialogue.cpp rename to ext/scenes/worldscape/gui/dialogue.cpp index 039d8272..dd63777a 100644 --- a/ext/scenes/world/gui/dialogue.cpp +++ b/ext/scenes/worldscape/gui/dialogue.cpp @@ -20,7 +20,6 @@ #include #include -#include "../world.h" #include "..//dialogue.h" namespace { @@ -31,18 +30,14 @@ namespace { ext::DialogueManager* dialogueManager; - ext::World* world; - uf::Serializer masterTableGet( const std::string& table ) { - if ( !world ) world = (ext::World*) uf::Entity::globalFindByName("World"); - if ( !world ) return std::string(); - uf::Serializer& mastertable = world->getComponent(); + auto& scene = uf::scene::getCurrentScene(); + uf::Serializer& mastertable = scene.getComponent(); return mastertable["system"]["mastertable"][table]; } uf::Serializer masterDataGet( const std::string& table, const std::string& key ) { - if ( !world ) world = (ext::World*) uf::Entity::globalFindByName("World"); - if ( !world ) return std::string(); - uf::Serializer& mastertable = world->getComponent(); + auto& scene = uf::scene::getCurrentScene(); + uf::Serializer& mastertable = scene.getComponent(); return mastertable["system"]["mastertable"][table][key]; } inline int64_t parseInt( const std::string& str ) { @@ -50,9 +45,8 @@ namespace { } void playSound( ext::GuiDialogue& entity, const std::string& id, const std::string& key ) { - if ( !world ) world = (ext::World*) uf::Entity::globalFindByName("World"); - if ( !world ) return; - uf::Serializer& masterdata = world->getComponent(); + auto& scene = uf::scene::getCurrentScene(); + uf::Serializer& masterdata = scene.getComponent(); uf::Serializer cardData = masterDataGet("Card", id); uf::Serializer charaData = masterDataGet("Chara", cardData["character_id"].asString()); @@ -62,39 +56,36 @@ namespace { url = "/smtsamo/voice/voice_" + name + "_" + key + ".ogg"; } - uf::Asset& assetLoader = world->getComponent(); + uf::Asset& assetLoader = scene.getComponent(); assetLoader.cache(url, "asset:Cache.Voice." + std::to_string(entity.getUid())); } void playSound( ext::GuiDialogue& entity, std::size_t uid, const std::string& key ) { - if ( !world ) world = (ext::World*) uf::Entity::globalFindByName("World"); - if ( !world ) return; + auto& scene = uf::scene::getCurrentScene(); uf::Serializer& pMetadata = entity.getComponent(); - uf::Serializer& masterdata = world->getComponent(); + uf::Serializer& masterdata = scene.getComponent(); - uf::Entity* = world->findByUid(uid); + uf::Entity* = scene.findByUid(uid); if ( ! ) return; uf::Serializer& metadata = ->getComponent(); std::string id = metadata[""]["id"].asString(); playSound( entity, id, key ); } void playSound( ext::GuiDialogue& entity, const std::string& key ) { - if ( !world ) world = (ext::World*) uf::Entity::globalFindByName("World"); - if ( !world ) return; + auto& scene = uf::scene::getCurrentScene(); uf::Serializer& metadata = entity.getComponent(); - uf::Serializer& masterdata = world->getComponent(); + uf::Serializer& masterdata = scene.getComponent(); std::string url = "./data/audio/ui/" + key + ".ogg"; - uf::Asset& assetLoader = world->getComponent(); + uf::Asset& assetLoader = scene.getComponent(); assetLoader.cache(url, "asset:Cache.SFX." + std::to_string(entity.getUid())); } void playMusic( ext::GuiDialogue& entity, const std::string& filename ) { - if ( !world ) world = (ext::World*) uf::Entity::globalFindByName("World"); - if ( !world ) return; - uf::Asset& assetLoader = world->getComponent(); - uf::Serializer& masterdata = world->getComponent(); + auto& scene = uf::scene::getCurrentScene(); + uf::Asset& assetLoader = scene.getComponent(); + uf::Serializer& masterdata = scene.getComponent(); - assetLoader.load(filename, "asset:Load." + std::to_string(world->getUid())); + assetLoader.load(filename, "asset:Load." + std::to_string(scene.getUid())); } std::string getKeyFromIndex( uf::Serializer& object, uint64_t index ) { uint64_t i = 0; @@ -126,6 +117,7 @@ namespace { void ext::GuiDialogue::initialize() { ext::Gui::initialize(); + uf::Scene& scene = uf::scene::getCurrentScene(); uf::Serializer& metadata = this->getComponent(); dialogueManager = (ext::DialogueManager*) this->getRootParent().findByName( "Dialogue Manager" ); @@ -137,7 +129,7 @@ void ext::GuiDialogue::initialize() { if ( uf::string::extension(filename) != "ogg" ) return "false"; if ( filename == "" ) return "false"; - uf::Serializer& masterdata = world->getComponent(); + uf::Serializer& masterdata = scene.getComponent(); uf::Audio& sfx = this->getComponent(); if ( sfx.playing() ) sfx.stop(); sfx = uf::Audio(); @@ -156,7 +148,7 @@ void ext::GuiDialogue::initialize() { if ( uf::string::extension(filename) != "ogg" ) return "false"; if ( filename == "" ) return "false"; - uf::Serializer& masterdata = world->getComponent(); + uf::Serializer& masterdata = scene.getComponent(); uf::Audio& voice = this->getComponent(); if ( voice.playing() ) voice.stop(); voice = uf::Audio(); @@ -188,8 +180,8 @@ void ext::GuiDialogue::initialize() { void ext::GuiDialogue::tick() { if ( !dialogueManager ) return; - ext::World& world = this->getRootParent(); - uf::Serializer& masterdata = world.getComponent(); + uf::Scene& scene = uf::scene::getCurrentScene(); + uf::Serializer& masterdata = scene.getComponent(); uf::Serializer& bMetadata = this->getComponent(); static float alpha = 0.0f; diff --git a/ext/scenes/world/gui/dialogue.h b/ext/scenes/worldscape/gui/dialogue.h similarity index 100% rename from ext/scenes/world/gui/dialogue.h rename to ext/scenes/worldscape/gui/dialogue.h diff --git a/ext/scenes/world/gui/pause.cpp b/ext/scenes/worldscape/gui/pause.cpp similarity index 94% rename from ext/scenes/world/gui/pause.cpp rename to ext/scenes/worldscape/gui/pause.cpp index 772fc13f..64b45507 100644 --- a/ext/scenes/world/gui/pause.cpp +++ b/ext/scenes/worldscape/gui/pause.cpp @@ -12,16 +12,13 @@ #include -#include -#include +#include #include #include #include #include -#include "../world.h" - namespace { ext::Gui* mainText; ext::Gui* commandText; @@ -37,7 +34,7 @@ namespace { namespace { void playSound( ext::GuiWorldPauseMenu& entity, const std::string& key ) { - uf::Scene& scene = entity.getRootParent(); + uf::Scene& scene = uf::scene::getCurrentScene(); uf::Serializer& metadata = entity.getComponent(); uf::Serializer& masterdata = scene.getComponent(); @@ -47,17 +44,14 @@ namespace { assetLoader.cache(url, "asset:Cache." + std::to_string(entity.getUid())); } - uf::Scene* scene; uf::Serializer masterTableGet( const std::string& table ) { - if ( !scene ) scene = (uf::Scene*) &uf::scene::getCurrentScene(); - if ( !scene ) return std::string(); - uf::Serializer& mastertable = scene->getComponent(); + uf::Scene& scene = uf::scene::getCurrentScene(); + uf::Serializer& mastertable = scene.getComponent(); return mastertable["system"]["mastertable"][table]; } uf::Serializer masterDataGet( const std::string& table, const std::string& key ) { - if ( !scene ) scene = (uf::Scene*) &uf::scene::getCurrentScene(); - if ( !scene ) return std::string(); - uf::Serializer& mastertable = scene->getComponent(); + uf::Scene& scene = uf::scene::getCurrentScene(); + uf::Serializer& mastertable = scene.getComponent(); return mastertable["system"]["mastertable"][table][key]; } inline int64_t parseInt( const std::string& str ) { @@ -70,7 +64,7 @@ EXT_OBJECT_REGISTER_CPP(GuiWorldPauseMenu) void ext::GuiWorldPauseMenu::initialize() { ext::Gui::initialize(); - uf::Scene& scene = this->getRootParent(); + uf::Scene& scene = uf::scene::getCurrentScene(); uf::Serializer& masterdata = scene.getComponent(); uf::Serializer& metadata = this->getComponent(); diff --git a/ext/scenes/world/gui/pause.h b/ext/scenes/worldscape/gui/pause.h similarity index 100% rename from ext/scenes/world/gui/pause.h rename to ext/scenes/worldscape/gui/pause.h diff --git a/ext/scenes/world/housamo/battle.cpp b/ext/scenes/worldscape/housamo/battle.cpp similarity index 93% rename from ext/scenes/world/housamo/battle.cpp rename to ext/scenes/worldscape/housamo/battle.cpp index 1d62859c..13ec65e8 100644 --- a/ext/scenes/world/housamo/battle.cpp +++ b/ext/scenes/worldscape/housamo/battle.cpp @@ -9,32 +9,28 @@ #include #include #include -#include -#include -#include -#include + +#include + + #include #include -#include "../world.h" #include "../gui/battle.h" #include namespace { - ext::World* world; ext::Gui* gui; std::string previousMusic; uf::Serializer masterTableGet( const std::string& table ) { - if ( !world ) world = (ext::World*) uf::Entity::globalFindByName("World"); - if ( !world ) return std::string(); - uf::Serializer& mastertable = world->getComponent(); + uf::Scene& scene = uf::scene::getCurrentScene(); + uf::Serializer& mastertable = scene.getComponent(); return mastertable["system"]["mastertable"][table]; } uf::Serializer masterDataGet( const std::string& table, const std::string& key ) { - if ( !world ) world = (ext::World*) uf::Entity::globalFindByName("World"); - if ( !world ) return std::string(); - uf::Serializer& mastertable = world->getComponent(); + uf::Scene& scene = uf::scene::getCurrentScene(); + uf::Serializer& mastertable = scene.getComponent(); return mastertable["system"]["mastertable"][table][key]; } inline int64_t parseInt( const std::string& str ) { @@ -49,8 +45,8 @@ namespace { namespace { void playSound( ext::HousamoBattle& entity, const std::string& id, const std::string& key ) { - ext::World& world = entity.getRootParent(); - uf::Serializer& masterdata = world.getComponent(); + uf::Scene& scene = uf::scene::getCurrentScene(); + uf::Serializer& masterdata = scene.getComponent(); uf::Serializer cardData = masterDataGet("Card", id); uf::Serializer charaData = masterDataGet("Chara", cardData["character_id"].asString()); @@ -63,43 +59,43 @@ namespace { url = "./data/smtsamo/voice/voice_" + name + "_" + key + ".ogg"; } - uf::Asset& assetLoader = world.getComponent(); + uf::Asset& assetLoader = scene.getComponent(); assetLoader.cache(url, "asset:Cache.Voice." + std::to_string(entity.getUid())); } void playSound( ext::HousamoBattle& entity, std::size_t uid, const std::string& key ) { - ext::World& world = entity.getRootParent(); + uf::Scene& scene = uf::scene::getCurrentScene(); uf::Serializer& pMetadata = entity.getComponent(); - uf::Serializer& masterdata = world.getComponent(); + uf::Serializer& masterdata = scene.getComponent(); - uf::Entity* = world.findByUid(uid); + uf::Entity* = scene.findByUid(uid); if ( ! ) return; uf::Serializer& metadata = ->getComponent(); std::string id = metadata[""]["id"].asString(); playSound( entity, id, key ); } void playSound( ext::HousamoBattle& entity, const std::string& key ) { - ext::World& world = entity.getRootParent(); + uf::Scene& scene = uf::scene::getCurrentScene(); uf::Serializer& metadata = entity.getComponent(); - uf::Serializer& masterdata = world.getComponent(); + uf::Serializer& masterdata = scene.getComponent(); std::string url = "./data/audio/ui/" + key + ".ogg"; - uf::Asset& assetLoader = world.getComponent(); + uf::Asset& assetLoader = scene.getComponent(); assetLoader.cache(url, "asset:Cache.SFX." + std::to_string(entity.getUid())); } void playMusic( ext::HousamoBattle& entity, const std::string& filename ) { - ext::World& world = entity.getRootParent(); - uf::Asset& assetLoader = world.getComponent(); - uf::Serializer& masterdata = world.getComponent(); + uf::Scene& scene = uf::scene::getCurrentScene(); + uf::Asset& assetLoader = scene.getComponent(); + uf::Serializer& masterdata = scene.getComponent(); - uf::Audio& audio = world.getComponent(); + uf::Audio& audio = scene.getComponent(); if ( audio.playing() ) { - uf::Serializer& metadata = world.getComponent(); + uf::Serializer& metadata = scene.getComponent(); metadata["previous bgm"]["filename"] = audio.getFilename(); metadata["previous bgm"]["timestamp"] = audio.getTime(); } - assetLoader.load(filename, "asset:Load." + std::to_string(world.getUid())); + assetLoader.load(filename, "asset:Load." + std::to_string(scene.getUid())); } uf::Audio sfx; @@ -112,6 +108,7 @@ void ext::HousamoBattle::initialize() { gui = NULL; transients.clear(); + uf::Scene& scene = uf::scene::getCurrentScene(); uf::Serializer& metadata = this->getComponent(); // std::vector& transients = this->getComponent>(); @@ -120,9 +117,9 @@ void ext::HousamoBattle::initialize() { // asset loading this->addHook( "asset:Cache.Voice.%UID%", [&](const std::string& event)->std::string{ uf::Serializer json = event; - ext::World& world = this->getRootParent(); + uf::Scene& scene = uf::scene::getCurrentScene(); uf::Serializer& metadata = this->getComponent(); - uf::Serializer& masterdata = world.getComponent(); + uf::Serializer& masterdata = scene.getComponent(); std::string filename = json["filename"].asString(); @@ -140,8 +137,8 @@ void ext::HousamoBattle::initialize() { this->addHook( "asset:Cache.SFX.%UID%", [&](const std::string& event)->std::string{ uf::Serializer json = event; - ext::World& world = this->getRootParent(); - uf::Serializer& masterdata = world.getComponent(); + uf::Scene& scene = uf::scene::getCurrentScene(); + uf::Serializer& masterdata = scene.getComponent(); std::string filename = json["filename"].asString(); @@ -613,7 +610,7 @@ void ext::HousamoBattle::initialize() { uf::Serializer& member = transient->getComponent(); uint64_t entid = metadata["battle"]["enemy"]["uid"].asUInt64(); - uf::Entity* = this->getRootParent().findByUid(entid); + uf::Entity* = scene.findByUid(entid); if ( ! ) return "false"; uf::Serializer& hMetadata = ->getComponent(); uint64_t phase = json["phase"].asUInt64(); @@ -1436,8 +1433,8 @@ void ext::HousamoBattle::initialize() { std::cout << "Previous BGM: " << previousMusic << std::endl; playMusic(*this, previousMusic); */ - ext::World& world = this->getRootParent(); - world.callHook("world:Music.LoadPrevious.%UID%"); + uf::Scene& scene = uf::scene::getCurrentScene(); + scene.callHook("world:Music.LoadPrevious.%UID%"); gui->callHook("menu:Close.%UID%"); // cleanup @@ -1453,9 +1450,9 @@ void ext::HousamoBattle::initialize() { // playSound(*this, metadata["battle"]["enemy"]["uid"].asUInt64(), "killed"); // kill { - ext::World& world = this->getRootParent(); + uf::Scene& scene = uf::scene::getCurrentScene(); std::size_t uid = metadata["battle"]["enemy"]["uid"].asUInt64(); - uf::Entity* = world.findByUid(uid); + uf::Entity* = scene.findByUid(uid); if ( ) { uf::Object& parent = ->getParent(); ->destroy(); @@ -1479,9 +1476,9 @@ void ext::HousamoBattle::initialize() { playSound(*this, metadata["battle"]["enemy"]["uid"].asUInt64(), "killed"); // kill { - ext::World& world = this->getRootParent(); + uf::Scene& scene = uf::scene::getCurrentScene(); std::size_t uid = metadata["battle"]["enemy"]["uid"].asUInt64(); - uf::Entity* = world.findByUid(uid); + uf::Entity* = scene.findByUid(uid); if ( ) { uf::Object& parent = ->getParent(); ->destroy(); @@ -1492,9 +1489,9 @@ void ext::HousamoBattle::initialize() { // clear battle data on player { - ext::World& world = this->getRootParent(); + uf::Scene& scene = uf::scene::getCurrentScene(); std::size_t uid = metadata["battle"]["player"]["uid"].asUInt64(); - uf::Entity* = world.findByUid(uid); + uf::Entity* = scene.findByUid(uid); if ( ) { uf::Serializer& pMetadata = ->getComponent(); pMetadata["system"].removeMember("battle"); diff --git a/ext/scenes/world/housamo/battle.h b/ext/scenes/worldscape/housamo/battle.h similarity index 100% rename from ext/scenes/world/housamo/battle.h rename to ext/scenes/worldscape/housamo/battle.h diff --git a/ext/scenes/world/housamo/dialogue.cpp b/ext/scenes/worldscape/housamo/dialogue.cpp similarity index 80% rename from ext/scenes/world/housamo/dialogue.cpp rename to ext/scenes/worldscape/housamo/dialogue.cpp index 96a6c9fa..0e4c0177 100644 --- a/ext/scenes/world/housamo/dialogue.cpp +++ b/ext/scenes/worldscape/housamo/dialogue.cpp @@ -17,23 +17,19 @@ #include #include -#include "../world.h" #include "../gui/dialogue.h" namespace { - ext::World* world; ext::Gui* gui; uf::Serializer masterTableGet( const std::string& table ) { - if ( !world ) world = (ext::World*) uf::Entity::globalFindByName("World"); - if ( !world ) return std::string(); - uf::Serializer& mastertable = world->getComponent(); + uf::Scene& scene = uf::scene::getCurrentScene(); + uf::Serializer& mastertable = scene.getComponent(); return mastertable["system"]["mastertable"][table]; } uf::Serializer masterDataGet( const std::string& table, const std::string& key ) { - if ( !world ) world = (ext::World*) uf::Entity::globalFindByName("World"); - if ( !world ) return std::string(); - uf::Serializer& mastertable = world->getComponent(); + uf::Scene& scene = uf::scene::getCurrentScene(); + uf::Serializer& mastertable = scene.getComponent(); return mastertable["system"]["mastertable"][table][key]; } inline int64_t parseInt( const std::string& str ) { @@ -43,8 +39,8 @@ namespace { namespace { void playSound( ext::DialogueManager& entity, const std::string& id, const std::string& key ) { - ext::World& world = entity.getRootParent(); - uf::Serializer& masterdata = world.getComponent(); + uf::Scene& scene = uf::scene::getCurrentScene(); + uf::Serializer& masterdata = scene.getComponent(); uf::Serializer cardData = masterDataGet("Card", id); uf::Serializer charaData = masterDataGet("Chara", cardData["character_id"].asString()); @@ -57,36 +53,36 @@ namespace { url = "./data/smtsamo/voice/voice_" + name + "_" + key + ".ogg"; } - uf::Asset& assetLoader = world.getComponent(); + uf::Asset& assetLoader = scene.getComponent(); assetLoader.cache(url, "asset:Cache.Voice." + std::to_string(entity.getUid())); } void playSound( ext::DialogueManager& entity, std::size_t uid, const std::string& key ) { - ext::World& world = entity.getRootParent(); + uf::Scene& scene = uf::scene::getCurrentScene(); uf::Serializer& pMetadata = entity.getComponent(); - uf::Serializer& masterdata = world.getComponent(); + uf::Serializer& masterdata = scene.getComponent(); - uf::Entity* = world.findByUid(uid); + uf::Entity* = scene.findByUid(uid); if ( ! ) return; uf::Serializer& metadata = ->getComponent(); std::string id = metadata[""]["id"].asString(); playSound( entity, id, key ); } void playSound( ext::DialogueManager& entity, const std::string& key ) { - ext::World& world = entity.getRootParent(); + uf::Scene& scene = uf::scene::getCurrentScene(); uf::Serializer& metadata = entity.getComponent(); - uf::Serializer& masterdata = world.getComponent(); + uf::Serializer& masterdata = scene.getComponent(); std::string url = "./" + key + ".ogg"; - uf::Asset& assetLoader = world.getComponent(); + uf::Asset& assetLoader = scene.getComponent(); assetLoader.cache(url, "asset:Cache.SFX." + std::to_string(entity.getUid())); } void playMusic( ext::DialogueManager& entity, const std::string& filename ) { - ext::World& world = entity.getRootParent(); - uf::Asset& assetLoader = world.getComponent(); - uf::Serializer& masterdata = world.getComponent(); + uf::Scene& scene = uf::scene::getCurrentScene(); + uf::Asset& assetLoader = scene.getComponent(); + uf::Serializer& masterdata = scene.getComponent(); - assetLoader.load(filename, "asset:Load." + std::to_string(world.getUid())); + assetLoader.load(filename, "asset:Load." + std::to_string(scene.getUid())); } uf::Audio sfx; @@ -107,9 +103,9 @@ void ext::DialogueManager::initialize() { // asset loading this->addHook( "asset:Cache.Voice.%UID%", [&](const std::string& event)->std::string{ uf::Serializer json = event; - ext::World& world = this->getRootParent(); + uf::Scene& scene = uf::scene::getCurrentScene(); uf::Serializer& metadata = this->getComponent(); - uf::Serializer& masterdata = world.getComponent(); + uf::Serializer& masterdata = scene.getComponent(); std::string filename = json["filename"].asString(); @@ -127,8 +123,8 @@ void ext::DialogueManager::initialize() { this->addHook( "asset:Cache.SFX.%UID%", [&](const std::string& event)->std::string{ uf::Serializer json = event; - ext::World& world = this->getRootParent(); - uf::Serializer& masterdata = world.getComponent(); + uf::Scene& scene = uf::scene::getCurrentScene(); + uf::Serializer& masterdata = scene.getComponent(); std::string filename = json["filename"].asString(); @@ -253,8 +249,8 @@ void ext::DialogueManager::initialize() { this->addHook( "menu:Dialogue.End.%UID%", [&](const std::string& event)->std::string{ uf::Serializer json = event; // play music - ext::World& world = this->getRootParent(); - world.callHook("world:Music.LoadPrevious.%UID%"); + uf::Scene& scene = uf::scene::getCurrentScene(); + scene.callHook("world:Music.LoadPrevious.%UID%"); gui->callHook("menu:Close.%UID%"); this->callHook("menu:Dialogue.End"); diff --git a/ext/scenes/world/housamo/dialogue.h b/ext/scenes/worldscape/housamo/dialogue.h similarity index 100% rename from ext/scenes/world/housamo/dialogue.h rename to ext/scenes/worldscape/housamo/dialogue.h diff --git a/ext/scenes/world/housamo/housamo.cpp b/ext/scenes/worldscape/housamo/housamo.cpp similarity index 89% rename from ext/scenes/world/housamo/housamo.cpp rename to ext/scenes/worldscape/housamo/housamo.cpp index 1828097a..67901bfb 100644 --- a/ext/scenes/world/housamo/housamo.cpp +++ b/ext/scenes/worldscape/housamo/housamo.cpp @@ -1,21 +1,17 @@ #include ".h" +#include #include -#include "../world.h" namespace { - ext::World* world; - uf::Serializer masterTableGet( const std::string& table ) { - if ( !world ) world = (ext::World*) uf::Entity::globalFindByName("World"); - if ( !world ) return std::string(); - uf::Serializer& mastertable = world->getComponent(); + uf::Scene& scene = uf::scene::getCurrentScene(); + uf::Serializer& mastertable = scene.getComponent(); return mastertable["system"]["mastertable"][table]; } uf::Serializer masterDataGet( const std::string& table, const std::string& key ) { - if ( !world ) world = (ext::World*) uf::Entity::globalFindByName("World"); - if ( !world ) return std::string(); - uf::Serializer& mastertable = world->getComponent(); + uf::Scene& scene = uf::scene::getCurrentScene(); + uf::Serializer& mastertable = scene.getComponent(); return mastertable["system"]["mastertable"][table][key]; } inline int64_t parseInt( const std::string& str ) { diff --git a/ext/scenes/world/housamo/housamo.h b/ext/scenes/worldscape/housamo/housamo.h similarity index 100% rename from ext/scenes/world/housamo/housamo.h rename to ext/scenes/worldscape/housamo/housamo.h diff --git a/ext/scenes/world/housamo/sprite.cpp b/ext/scenes/worldscape/housamo/sprite.cpp similarity index 87% rename from ext/scenes/world/housamo/sprite.cpp rename to ext/scenes/worldscape/housamo/sprite.cpp index 93a1f9b8..33d7508b 100644 --- a/ext/scenes/world/housamo/sprite.cpp +++ b/ext/scenes/worldscape/housamo/sprite.cpp @@ -8,30 +8,24 @@ #include #include #include -#include -#include -#include -#include + +#include + #include -#include "../world.h" #include #include "battle.h" #include ".h" namespace { - ext::World* world; - uf::Serializer masterTableGet( const std::string& table ) { - if ( !world ) world = (ext::World*) uf::Entity::globalFindByName("World"); - if ( !world ) return std::string(); - uf::Serializer& mastertable = world->getComponent(); + uf::Scene& scene = uf::scene::getCurrentScene(); + uf::Serializer& mastertable = scene.getComponent(); return mastertable["system"]["mastertable"][table]; } uf::Serializer masterDataGet( const std::string& table, const std::string& key ) { - if ( !world ) world = (ext::World*) uf::Entity::globalFindByName("World"); - if ( !world ) return std::string(); - uf::Serializer& mastertable = world->getComponent(); + uf::Scene& scene = uf::scene::getCurrentScene(); + uf::Serializer& mastertable = scene.getComponent(); return mastertable["system"]["mastertable"][table][key]; } inline int64_t parseInt( const std::string& str ) { @@ -49,10 +43,10 @@ void ext::HousamoSprite::initialize() { // this->addAlias(); } - ext::World& world = this->getRootParent(); + uf::Scene& scene = uf::scene::getCurrentScene(); uf::Serializer& metadata = this->getComponent(); - uf::Serializer& masterdata = world.getComponent(); - uf::Asset& assetLoader = world.getComponent(); + uf::Serializer& masterdata = scene.getComponent(); + uf::Asset& assetLoader = scene.getComponent(); this->addHook( "graphics:Assign.%UID%", [&](const std::string& event)->std::string{ uf::Serializer json = event; diff --git a/ext/scenes/world/housamo/sprite.h b/ext/scenes/worldscape/housamo/sprite.h similarity index 100% rename from ext/scenes/world/housamo/sprite.h rename to ext/scenes/worldscape/housamo/sprite.h diff --git a/ext/scenes/world/light/light.cpp b/ext/scenes/worldscape/light/light.cpp similarity index 73% rename from ext/scenes/world/light/light.cpp rename to ext/scenes/worldscape/light/light.cpp index ce406800..cde7934a 100644 --- a/ext/scenes/world/light/light.cpp +++ b/ext/scenes/worldscape/light/light.cpp @@ -1,7 +1,7 @@ #include "light.h" -#include -#include +#include + #include #include #include @@ -13,9 +13,9 @@ void ext::Light::initialize() { auto& transform = this->getComponent>(); auto& camera = this->getComponent(); if ( metadata["light"]["shadows"]["enabled"].asBool() ) { - auto& renderMode = this->getComponent(); + auto& renderMode = this->getComponent(); std::string name = "RT:" + std::to_string((int) this->getUid()); - ext::vulkan::addRenderMode( &renderMode, name ); + uf::renderer::addRenderMode( &renderMode, name ); if ( metadata["light"]["shadows"]["resolution"].isArray() ) { renderMode.width = metadata["light"]["shadows"]["resolution"][0].asUInt64(); renderMode.height = metadata["light"]["shadows"]["resolution"][1].asUInt64(); @@ -45,8 +45,8 @@ void ext::Light::initialize() { void ext::Light::tick() { uf::Object::tick(); - if ( this->hasComponent() ) { - auto& renderMode = this->getComponent(); + if ( this->hasComponent() ) { + auto& renderMode = this->getComponent(); renderMode.target = ""; } @@ -55,21 +55,24 @@ void ext::Light::tick() { pod::Physics& physics = this->getComponent(); transform = uf::physics::update( transform, physics ); } + { + uf::transform::rotate( transform, {0, 1, 0}, uf::physics::time::delta ); + } auto& camera = this->getComponent(); auto& metadata = this->getComponent(); - if ( metadata["light"]["external update"].isNull() || (!metadata["light"]["external update"].isNull() && !metadata["light"]["external update"].asBool()) ) { +// if ( metadata["light"]["external update"].isNull() || (!metadata["light"]["external update"].isNull() && !metadata["light"]["external update"].asBool()) ) { for ( std::size_t i = 0; i < 2; ++i ) { camera.setView( uf::matrix::inverse( uf::transform::model( transform ) ), i ); } - } +// } } void ext::Light::render() { uf::Object::render(); } void ext::Light::destroy() { - if ( this->hasComponent() ) { - auto& renderMode = this->getComponent(); - ext::vulkan::removeRenderMode( &renderMode, false ); + if ( this->hasComponent() ) { + auto& renderMode = this->getComponent(); + uf::renderer::removeRenderMode( &renderMode, false ); } uf::Object::destroy(); } \ No newline at end of file diff --git a/ext/scenes/world/light/light.h b/ext/scenes/worldscape/light/light.h similarity index 100% rename from ext/scenes/world/light/light.h rename to ext/scenes/worldscape/light/light.h diff --git a/ext/scenes/world/player/hands.cpp b/ext/scenes/worldscape/player/hands.cpp similarity index 97% rename from ext/scenes/world/player/hands.cpp rename to ext/scenes/worldscape/player/hands.cpp index eedce6ef..8d9b13d6 100644 --- a/ext/scenes/world/player/hands.cpp +++ b/ext/scenes/worldscape/player/hands.cpp @@ -12,7 +12,6 @@ #include #include "../terrain/generator.h" -#include "../world.h" namespace { struct { diff --git a/ext/scenes/world/player/hands.h b/ext/scenes/worldscape/player/hands.h similarity index 100% rename from ext/scenes/world/player/hands.h rename to ext/scenes/worldscape/player/hands.h diff --git a/ext/scenes/world/player/player.cpp b/ext/scenes/worldscape/player/player.cpp similarity index 95% rename from ext/scenes/world/player/player.cpp rename to ext/scenes/worldscape/player/player.cpp index aa1b7899..30a48f10 100644 --- a/ext/scenes/world/player/player.cpp +++ b/ext/scenes/worldscape/player/player.cpp @@ -14,21 +14,16 @@ #include "../gui/pause.h" #include "../gui/battle.h" #include "..//.h" -#include "../world.h" namespace { - ext::World* world; - uf::Serializer masterTableGet( const std::string& table ) { - if ( !world ) world = (ext::World*) uf::Entity::globalFindByName("World"); - if ( !world ) return std::string(); - uf::Serializer& mastertable = world->getComponent(); + uf::Scene& scene = uf::scene::getCurrentScene(); + uf::Serializer& mastertable = scene.getComponent(); return mastertable["system"]["mastertable"][table]; } uf::Serializer masterDataGet( const std::string& table, const std::string& key ) { - if ( !world ) world = (ext::World*) uf::Entity::globalFindByName("World"); - if ( !world ) return std::string(); - uf::Serializer& mastertable = world->getComponent(); + uf::Scene& scene = uf::scene::getCurrentScene(); + uf::Serializer& mastertable = scene.getComponent(); return mastertable["system"]["mastertable"][table][key]; } inline int64_t parseInt( const std::string& str ) { @@ -226,8 +221,8 @@ void ext::Player::initialize() { std::string state = metadata["system"]["state"].asString(); if ( state != "" && state != "null" ) return "false"; - ext::World& world = this->getRootParent(); - uf::Entity* entity = world.findByUid(json["entity"].asUInt64()); + uf::Scene& scene = uf::scene::getCurrentScene(); + uf::Entity* entity = scene.findByUid(json["entity"].asUInt64()); if ( !entity ) return "false"; @@ -301,7 +296,7 @@ void ext::Player::tick() { pod::Transform<>& transform = this->getComponent>(); pod::Physics& physics = this->getComponent(); uf::Serializer& metadata = this->getComponent(); - ext::World& world = this->getRootParent(); + uf::Scene& scene = uf::scene::getCurrentScene(); struct { bool running = uf::Window::isKeyPressed("LShift"); diff --git a/ext/scenes/world/player/player.h b/ext/scenes/worldscape/player/player.h similarity index 100% rename from ext/scenes/world/player/player.h rename to ext/scenes/worldscape/player/player.h diff --git a/ext/scenes/world/portal/portal.cpp b/ext/scenes/worldscape/portal/portal.cpp similarity index 89% rename from ext/scenes/world/portal/portal.cpp rename to ext/scenes/worldscape/portal/portal.cpp index 0bf01498..7fe479ac 100644 --- a/ext/scenes/world/portal/portal.cpp +++ b/ext/scenes/worldscape/portal/portal.cpp @@ -14,10 +14,9 @@ #include -#include +#include #include "../terrain/generator.h" -#include "../world.h" namespace { @@ -68,9 +67,9 @@ void ext::Portal::initialize() { camera = controller.getComponent(); } { - auto& renderMode = this->getComponent(); + auto& renderMode = this->getComponent(); std::string name = "RT:" + std::to_string((int) this->getUid()); - ext::vulkan::addRenderMode( &renderMode, name ); + uf::renderer::addRenderMode( &renderMode, name ); if ( ext::openvr::enabled ) { ext::openvr::initialize(); @@ -85,7 +84,7 @@ void ext::Portal::initialize() { void ext::Portal::tick() { uf::Object::tick(); - auto& renderMode = this->getComponent(); + auto& renderMode = this->getComponent(); renderMode.target = ""; auto& scene = uf::scene::getCurrentScene(); @@ -124,7 +123,7 @@ void ext::Portal::tick() { void ext::Portal::render() { uf::Object::render(); { - auto& renderMode = this->getComponent(); + auto& renderMode = this->getComponent(); auto& blitter = renderMode.blitter; auto& transform = this->getComponent>(); @@ -171,7 +170,7 @@ void ext::Portal::render() { } } void ext::Portal::destroy() { - auto& renderMode = this->getComponent(); - ext::vulkan::removeRenderMode( &renderMode, false ); + auto& renderMode = this->getComponent(); + uf::renderer::removeRenderMode( &renderMode, false ); uf::Object::destroy(); } \ No newline at end of file diff --git a/ext/scenes/world/portal/portal.h b/ext/scenes/worldscape/portal/portal.h similarity index 100% rename from ext/scenes/world/portal/portal.h rename to ext/scenes/worldscape/portal/portal.h diff --git a/ext/scenes/world/world.cpp b/ext/scenes/worldscape/scene.cpp similarity index 88% rename from ext/scenes/world/world.cpp rename to ext/scenes/worldscape/scene.cpp index 9e2bd434..b3ade2b6 100644 --- a/ext/scenes/world/world.cpp +++ b/ext/scenes/worldscape/scene.cpp @@ -1,4 +1,5 @@ -#include "world.h" +#include "scene.h" + #include #include #include @@ -23,14 +24,11 @@ #include ".//battle.h" #include ".//dialogue.h" -#include -#include -#include -#include +#include #include -EXT_OBJECT_REGISTER_CPP(World) -void ext::World::initialize() { +EXT_OBJECT_REGISTER_CPP(TestScene_WorldScape) +void ext::TestScene_WorldScape::initialize() { uf::Scene::initialize(); this->m_name = "World"; // this->load("./scenes/world/scene.json"); @@ -114,7 +112,7 @@ void ext::World::initialize() { ext::Gui* manager = (ext::Gui*) this->findByName("Gui Manager"); if ( !manager ) return "false"; uf::Serializer payload; - ext::Gui* gui = (ext::Gui*) manager->findByUid( (payload["uid"] = manager->loadChild("/scenes/world/gui/pause/menu.json", false)).asUInt64() ); + ext::Gui* gui = (ext::Gui*) manager->findByUid( (payload["uid"] = manager->loadChild("/scenes/worldscape/gui/pause/menu.json", false)).asUInt64() ); uf::Serializer& metadata = gui->getComponent(); metadata["menu"] = json["menu"]; gui->initialize(); @@ -204,8 +202,8 @@ void ext::World::initialize() { }); /* store viewport size */ { - metadata["window"]["size"]["x"] = ext::vulkan::width; - metadata["window"]["size"]["y"] = ext::vulkan::height; + metadata["window"]["size"]["x"] = uf::renderer::width; + metadata["window"]["size"]["y"] = uf::renderer::height; this->addHook( "window:Resized", [&](const std::string& event)->std::string{ uf::Serializer json = event; @@ -230,7 +228,7 @@ void ext::World::initialize() { } } -void ext::World::tick() { +void ext::TestScene_WorldScape::tick() { uf::Scene::tick(); uf::Serializer& metadata = this->getComponent(); @@ -271,9 +269,18 @@ void ext::World::tick() { static uf::Timer timer(false); if ( !timer.running() ) timer.start(); if ( uf::Window::isKeyPressed("U") && timer.elapsed().asDouble() >= 1 ) { timer.reset(); - uf::iostream << "Current size: " << uf::Entity::entities.size() << " | UIDs: " << uf::Entity::uids << "\n"; + auto& allocations = uf::Entity::memoryPool.allocations(); + uf::iostream << "Current size: " << allocations.size() << " | UIDs: " << uf::Entity::uids << "\n"; uint orphans = 0; uint empty = 0; + for ( auto& allocation : allocations ) { + uf::Entity* e = (uf::Entity*) allocation.pointer; + if ( !e->hasParent() ) { + ++orphans; + uf::iostream << "Orphan: " << e->getName() << ": " << e << "\n"; + } + } + /* for ( uf::Entity* e : uf::Entity::entities ) { if ( e && !e->hasParent() ) { ++orphans; @@ -281,6 +288,7 @@ void ext::World::tick() { } if ( !e ) ++empty; } + */ uf::iostream << "Orphans: " << orphans << "\n"; uf::iostream << "Empty: " << empty << "\n"; } @@ -295,6 +303,6 @@ void ext::World::tick() { } } -void ext::World::render() { +void ext::TestScene_WorldScape::render() { uf::Scene::render(); } \ No newline at end of file diff --git a/ext/scenes/world/world.h b/ext/scenes/worldscape/scene.h similarity index 77% rename from ext/scenes/world/world.h rename to ext/scenes/worldscape/scene.h index 3daed9a0..8538ac58 100644 --- a/ext/scenes/world/world.h +++ b/ext/scenes/worldscape/scene.h @@ -7,7 +7,7 @@ #include namespace ext { - class EXT_API World : public uf::Scene { + class EXT_API TestScene_WorldScape : public uf::Scene { public: virtual void initialize(); virtual void tick(); diff --git a/ext/scenes/world/terrain/generator.cpp b/ext/scenes/worldscape/terrain/generator.cpp similarity index 100% rename from ext/scenes/world/terrain/generator.cpp rename to ext/scenes/worldscape/terrain/generator.cpp diff --git a/ext/scenes/world/terrain/generator.h b/ext/scenes/worldscape/terrain/generator.h similarity index 93% rename from ext/scenes/world/terrain/generator.h rename to ext/scenes/worldscape/terrain/generator.h index 56c39b89..97fe9364 100644 --- a/ext/scenes/world/terrain/generator.h +++ b/ext/scenes/worldscape/terrain/generator.h @@ -55,7 +55,7 @@ namespace ext { void generate( ext::Region& ); void rasterize( std::vector& vertices, const ext::Region& ); - // void vectorize( std::vector&, const ext::Region& ); + // void vectorize( std::vector&, const ext::Region& ); std::vector getRawVoxels(); const pod::RLE::string_t& getVoxels() const; diff --git a/ext/scenes/world/terrain/maze.cpp b/ext/scenes/worldscape/terrain/maze.cpp similarity index 100% rename from ext/scenes/world/terrain/maze.cpp rename to ext/scenes/worldscape/terrain/maze.cpp diff --git a/ext/scenes/world/terrain/maze.h b/ext/scenes/worldscape/terrain/maze.h similarity index 100% rename from ext/scenes/world/terrain/maze.h rename to ext/scenes/worldscape/terrain/maze.h diff --git a/ext/scenes/world/terrain/region.cpp b/ext/scenes/worldscape/terrain/region.cpp similarity index 96% rename from ext/scenes/world/terrain/region.cpp rename to ext/scenes/worldscape/terrain/region.cpp index cc50620a..a233f8de 100644 --- a/ext/scenes/world/terrain/region.cpp +++ b/ext/scenes/worldscape/terrain/region.cpp @@ -1,6 +1,5 @@ #include "region.h" -#include "../world.h" #include "../../../ext.h" #include "generator.h" #include "..//sprite.h" @@ -63,8 +62,14 @@ void ext::Region::initialize() { std::string _ = this->getRootParent().getComponent()["shaders"]["region"]["suffix"].asString(); if ( _ != "" ) suffix = _ + "."; } + /* graphic.material.attachShader("./data/shaders/terrain.stereo.vert.spv", VK_SHADER_STAGE_VERTEX_BIT); graphic.material.attachShader("./data/shaders/terrain."+suffix+"frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); + */ + graphic.material.initializeShaders({ + {"./data/shaders/terrain.stereo.vert.spv", VK_SHADER_STAGE_VERTEX_BIT}, + {"./data/shaders/terrain."+suffix+"frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT} + }); } this->addHook( "region:Generate.%UID%", [&](const std::string& event)->std::string{ diff --git a/ext/scenes/world/terrain/region.h b/ext/scenes/worldscape/terrain/region.h similarity index 100% rename from ext/scenes/world/terrain/region.h rename to ext/scenes/worldscape/terrain/region.h diff --git a/ext/scenes/world/terrain/terrain.cpp b/ext/scenes/worldscape/terrain/terrain.cpp similarity index 95% rename from ext/scenes/world/terrain/terrain.cpp rename to ext/scenes/worldscape/terrain/terrain.cpp index 4dae8f19..6d27cd99 100644 --- a/ext/scenes/world/terrain/terrain.cpp +++ b/ext/scenes/worldscape/terrain/terrain.cpp @@ -3,7 +3,6 @@ #include "../../../ext.h" #include -#include "../../world/world.h" #include "generator.h" #include "region.h" @@ -485,8 +484,8 @@ bool ext::Terrain::exists( const pod::Vector3i& position ) const { } bool ext::Terrain::inBounds( const pod::Vector3i& position ) const { - const ext::World& parent = this->getRootParent(); - const pod::Transform<>& player = parent.getController()->getComponent>(); + const uf::Scene& scene = uf::scene::getCurrentScene(); + const pod::Transform<>& player = scene.getController()->getComponent>(); const uf::Serializer& metadata = this->getComponent(); pod::Vector3ui size = { metadata["region"]["size"][0].asUInt(), @@ -521,8 +520,8 @@ void ext::Terrain::generate( bool single ) { metadata["region"]["size"][2].asInt(), }; - ext::World& parent = this->getRootParent(); - const pod::Transform<>& player = parent.getController()->getComponent>(); + uf::Scene& scene = uf::scene::getCurrentScene(); + const pod::Transform<>& player = scene.getController()->getComponent>(); pod::Vector3i location = { (int) player.position.x / size.x, (int) player.position.y / size.y, @@ -578,8 +577,9 @@ void ext::Terrain::generate( const pod::Vector3i& position ) { if ( this->exists(position) ) return; if ( !this->inBounds(position) ) return; - uf::Entity* base = new ext::Region; this->addChild(*base); { - ext::Region& region = *((ext::Region*)base); +// uf::Entity* base = new ext::Region; + auto& region = uf::instantiator::instantiate(); + this->addChild(region); { uf::Serializer& m = region.getComponent(); // = metadata; m["system"]["root"] = metadata["system"]["root"]; m["system"]["source"] = metadata["system"]["source"]; diff --git a/ext/scenes/world/terrain/terrain.h b/ext/scenes/worldscape/terrain/terrain.h similarity index 100% rename from ext/scenes/world/terrain/terrain.h rename to ext/scenes/worldscape/terrain/terrain.h diff --git a/ext/scenes/world/terrain/voxel.cpp b/ext/scenes/worldscape/terrain/voxel.cpp similarity index 100% rename from ext/scenes/world/terrain/voxel.cpp rename to ext/scenes/worldscape/terrain/voxel.cpp diff --git a/ext/scenes/world/terrain/voxel.h b/ext/scenes/worldscape/terrain/voxel.h similarity index 100% rename from ext/scenes/world/terrain/voxel.h rename to ext/scenes/worldscape/terrain/voxel.h