diff --git a/src/animation/animation_system.cpp b/src/animation/animation_system.cpp index 5466d14b5..64153c608 100644 --- a/src/animation/animation_system.cpp +++ b/src/animation/animation_system.cpp @@ -92,21 +92,21 @@ struct AnimationSceneImpl : public AnimationScene } - float getAnimableTime(ComponentIndex cmp) override + float getAnimableTime(ComponentHandle cmp) override { - return m_animables[cmp].time; + return m_animables[cmp.index].time; } - void setAnimableTime(ComponentIndex cmp, float time) override + void setAnimableTime(ComponentHandle cmp, float time) override { - m_animables[cmp].time = time; + m_animables[cmp.index].time = time; } - Animation* getAnimableAnimation(ComponentIndex cmp) override + Animation* getAnimableAnimation(ComponentHandle cmp) override { - return m_animables[cmp].animation; + return m_animables[cmp.index].animation; } @@ -115,12 +115,12 @@ struct AnimationSceneImpl : public AnimationScene Universe& getUniverse() override { return m_universe; } - ComponentIndex getComponent(Entity entity, ComponentType type) override + ComponentHandle getComponent(Entity entity, ComponentType type) override { ASSERT(ownComponentType(type)); for (int i = 0; i < m_animables.size(); ++i) { - if ((m_animables[i].flags & Animable::FREE) == 0 && m_animables[i].entity == entity) return i; + if ((m_animables[i].flags & Animable::FREE) == 0 && m_animables[i].entity == entity) return {i}; } return INVALID_COMPONENT; } @@ -129,7 +129,7 @@ struct AnimationSceneImpl : public AnimationScene bool ownComponentType(ComponentType type) const override { return type == ANIMABLE_TYPE; } - ComponentIndex createComponent(ComponentType type, Entity entity) override + ComponentHandle createComponent(ComponentType type, Entity entity) override { if (type == ANIMABLE_TYPE) return createAnimable(entity); return INVALID_COMPONENT; @@ -146,13 +146,13 @@ struct AnimationSceneImpl : public AnimationScene } - void destroyComponent(ComponentIndex component, ComponentType type) override + void destroyComponent(ComponentHandle component, ComponentType type) override { if (type == ANIMABLE_TYPE) { - unloadAnimation(m_animables[component].animation); - m_animables[component].flags |= Animable::FREE; - m_universe.destroyComponent(m_animables[component].entity, type, this, component); + unloadAnimation(m_animables[component.index].animation); + m_animables[component.index].flags |= Animable::FREE; + m_universe.destroyComponent(m_animables[component.index].entity, type, this, component); } } @@ -216,38 +216,38 @@ struct AnimationSceneImpl : public AnimationScene m_animables[i].animation = path[0] == '\0' ? nullptr : loadAnimation(Path(path)); if ((m_animables[i].flags & Animable::FREE) == 0) { - m_universe.addComponent(m_animables[i].entity, ANIMABLE_TYPE, this, i); + m_universe.addComponent(m_animables[i].entity, ANIMABLE_TYPE, this, {i}); } } } - float getTimeScale(ComponentIndex cmp) { return m_animables[cmp].time_scale; } - void setTimeScale(ComponentIndex cmp, float time_scale) { m_animables[cmp].time_scale = time_scale; } - float getStartTime(ComponentIndex cmp) { return m_animables[cmp].start_time; } - void setStartTime(ComponentIndex cmp, float time) { m_animables[cmp].start_time = time; } + float getTimeScale(ComponentHandle cmp) { return m_animables[cmp.index].time_scale; } + void setTimeScale(ComponentHandle cmp, float time_scale) { m_animables[cmp.index].time_scale = time_scale; } + float getStartTime(ComponentHandle cmp) { return m_animables[cmp.index].start_time; } + void setStartTime(ComponentHandle cmp, float time) { m_animables[cmp.index].start_time = time; } - Path getAnimation(ComponentIndex cmp) + Path getAnimation(ComponentHandle cmp) { - return m_animables[cmp].animation ? m_animables[cmp].animation->getPath() : Path(""); + return m_animables[cmp.index].animation ? m_animables[cmp.index].animation->getPath() : Path(""); } - void setAnimation(ComponentIndex cmp, const Path& path) + void setAnimation(ComponentHandle cmp, const Path& path) { - unloadAnimation(m_animables[cmp].animation); - m_animables[cmp].animation = loadAnimation(path); - m_animables[cmp].time = 0; + unloadAnimation(m_animables[cmp.index].animation); + m_animables[cmp.index].animation = loadAnimation(path); + m_animables[cmp.index].time = 0; } - void updateAnimable(ComponentIndex cmp, float time_delta) override + void updateAnimable(ComponentHandle cmp, float time_delta) override { - Animable& animable = m_animables[cmp]; + Animable& animable = m_animables[cmp.index]; if ((animable.flags & Animable::FREE) != 0) return; if (!animable.animation || !animable.animation->isReady()) return; - ComponentIndex renderable = m_render_scene->getRenderableComponent(animable.entity); + ComponentHandle renderable = m_render_scene->getRenderableComponent(animable.entity); if (renderable == INVALID_COMPONENT) return; auto* pose = m_render_scene->getPose(renderable); @@ -277,7 +277,7 @@ struct AnimationSceneImpl : public AnimationScene for (int i = 0, c = m_animables.size(); i < c; ++i) { - AnimationSceneImpl::updateAnimable(i, time_delta); + AnimationSceneImpl::updateAnimable({i}, time_delta); } } @@ -289,7 +289,7 @@ struct AnimationSceneImpl : public AnimationScene } - ComponentIndex createAnimable(Entity entity) + ComponentHandle createAnimable(Entity entity) { Animable* src = nullptr; int cmp = m_animables.size(); @@ -310,8 +310,8 @@ struct AnimationSceneImpl : public AnimationScene animable.time_scale = 1; animable.start_time = 0; - m_universe.addComponent(entity, ANIMABLE_TYPE, this, cmp); - return cmp; + m_universe.addComponent(entity, ANIMABLE_TYPE, this, {cmp}); + return {cmp}; } diff --git a/src/animation/animation_system.h b/src/animation/animation_system.h index e55cc4123..163a55b61 100644 --- a/src/animation/animation_system.h +++ b/src/animation/animation_system.h @@ -12,10 +12,10 @@ namespace Lumix class AnimationScene : public IScene { public: - virtual class Animation* getAnimableAnimation(ComponentIndex cmp) = 0; - virtual float getAnimableTime(ComponentIndex cmp) = 0; - virtual void setAnimableTime(ComponentIndex cmp, float time) = 0; - virtual void updateAnimable(ComponentIndex cmp, float time_delta) = 0; + virtual class Animation* getAnimableAnimation(ComponentHandle cmp) = 0; + virtual float getAnimableTime(ComponentHandle cmp) = 0; + virtual void setAnimableTime(ComponentHandle cmp, float time) = 0; + virtual void updateAnimable(ComponentHandle cmp, float time_delta) = 0; }; diff --git a/src/animation/editor/plugins.cpp b/src/animation/editor/plugins.cpp index a8d6c8a5d..4aee59860 100644 --- a/src/animation/editor/plugins.cpp +++ b/src/animation/editor/plugins.cpp @@ -86,22 +86,22 @@ struct PropertyGridPlugin : PropertyGrid::IPlugin if (cmp.type != ANIMABLE_HASH) return; auto* scene = static_cast(cmp.scene); - auto* animation = scene->getAnimableAnimation(cmp.index); + auto* animation = scene->getAnimableAnimation(cmp.handle); if (!animation) return; if (!animation->isReady()) return; ImGui::Checkbox("Preview", &m_is_playing); - float time = scene->getAnimableTime(cmp.index); + float time = scene->getAnimableTime(cmp.handle); if (ImGui::SliderFloat("Time", &time, 0, animation->getLength())) { - scene->setAnimableTime(cmp.index, time); - scene->updateAnimable(cmp.index, 0); + scene->setAnimableTime(cmp.handle, time); + scene->updateAnimable(cmp.handle, 0); } if (m_is_playing) { float time_delta = m_app.getWorldEditor()->getEngine().getLastTimeDelta(); - scene->updateAnimable(cmp.index, time_delta); + scene->updateAnimable(cmp.handle, time_delta); } } diff --git a/src/audio/audio_scene.cpp b/src/audio/audio_scene.cpp index 3a5739e26..b00c98114 100644 --- a/src/audio/audio_scene.cpp +++ b/src/audio/audio_scene.cpp @@ -44,14 +44,14 @@ struct EchoZone Entity entity; float radius; float delay; - ComponentIndex component; + ComponentHandle component; }; struct AmbientSound { Entity entity; - ComponentIndex component; + ComponentHandle component; AudioScene::ClipInfo* clip; bool is_3d; int playing_sound; @@ -78,8 +78,8 @@ struct AudioSceneImpl : public AudioScene , m_ambient_sounds(allocator) , m_echo_zones(allocator) { - m_last_echo_zone_id = 0; - m_last_ambient_sound_id = 0; + m_last_echo_zone_cmp.index = 0; + m_last_ambient_sound_cmp.index = 0; m_listener.entity = INVALID_ENTITY; for (auto& i : m_playing_sounds) { @@ -137,13 +137,13 @@ struct AudioSceneImpl : public AudioScene } - bool isAmbientSound3D(ComponentIndex cmp) override + bool isAmbientSound3D(ComponentHandle cmp) override { return m_ambient_sounds[getAmbientSoundIdx(cmp)].is_3d; } - void setAmbientSound3D(ComponentIndex cmp, bool is_3d) override + void setAmbientSound3D(ComponentHandle cmp, bool is_3d) override { m_ambient_sounds[getAmbientSoundIdx(cmp)].is_3d = is_3d; } @@ -176,7 +176,7 @@ struct AudioSceneImpl : public AudioScene } - ComponentIndex createListener(Entity entity) + ComponentHandle createListener(Entity entity) { if (m_listener.entity != INVALID_ENTITY) { @@ -185,12 +185,12 @@ struct AudioSceneImpl : public AudioScene } m_listener.entity = entity; - m_universe.addComponent(entity, LISTENER_TYPE, this, 0); - return 0; + m_universe.addComponent(entity, LISTENER_TYPE, this, {0}); + return {0}; } - ClipInfo* getAmbientSoundClip(ComponentIndex cmp) override + ClipInfo* getAmbientSoundClip(ComponentHandle cmp) override { return m_ambient_sounds[getAmbientSoundIdx(cmp)].clip; } @@ -206,29 +206,30 @@ struct AudioSceneImpl : public AudioScene } - int getAmbientSoundClipIndex(ComponentIndex cmp) override + int getAmbientSoundClipIndex(ComponentHandle cmp) override { return m_clips.indexOf(m_ambient_sounds[getAmbientSoundIdx(cmp)].clip); } - void setAmbientSoundClipIndex(ComponentIndex cmp, int index) override + void setAmbientSoundClipIndex(ComponentHandle cmp, int index) override { m_ambient_sounds[getAmbientSoundIdx(cmp)].clip = m_clips[index]; } - void setAmbientSoundClip(ComponentIndex cmp, ClipInfo* clip) override + void setAmbientSoundClip(ComponentHandle cmp, ClipInfo* clip) override { m_ambient_sounds[getAmbientSoundIdx(cmp)].clip = clip; } - ComponentIndex createEchoZone(Entity entity) + ComponentHandle createEchoZone(Entity entity) { auto& zone = m_echo_zones.emplace(); zone.entity = entity; - zone.component = ++m_last_echo_zone_id; + ++m_last_echo_zone_cmp.index; + zone.component = m_last_echo_zone_cmp; zone.delay = 500.0f; zone.radius = 10; m_universe.addComponent(entity, ECHO_ZONE_TYPE, this, zone.component); @@ -236,31 +237,31 @@ struct AudioSceneImpl : public AudioScene } - float getEchoZoneDelay(ComponentIndex cmp) override + float getEchoZoneDelay(ComponentHandle cmp) override { return m_echo_zones[getEchoZoneIdx(cmp)].delay; } - void setEchoZoneDelay(ComponentIndex cmp, float delay) override + void setEchoZoneDelay(ComponentHandle cmp, float delay) override { m_echo_zones[getEchoZoneIdx(cmp)].delay = delay; } - float getEchoZoneRadius(ComponentIndex cmp) override + float getEchoZoneRadius(ComponentHandle cmp) override { return m_echo_zones[getEchoZoneIdx(cmp)].radius; } - void setEchoZoneRadius(ComponentIndex cmp, float radius) override + void setEchoZoneRadius(ComponentHandle cmp, float radius) override { m_echo_zones[getEchoZoneIdx(cmp)].radius = radius; } - int getEchoZoneIdx(ComponentIndex component) + int getEchoZoneIdx(ComponentHandle component) { for(int i = 0, c = m_echo_zones.size(); i < c; ++i) { @@ -270,7 +271,7 @@ struct AudioSceneImpl : public AudioScene } - void destroyEchoZone(ComponentIndex component) + void destroyEchoZone(ComponentHandle component) { int idx = getEchoZoneIdx(component); auto entity = m_echo_zones[idx].entity; @@ -280,10 +281,11 @@ struct AudioSceneImpl : public AudioScene } - ComponentIndex createAmbientSound(Entity entity) + ComponentHandle createAmbientSound(Entity entity) { auto& sound = m_ambient_sounds.emplace(); - sound.component = ++m_last_ambient_sound_id; + ++m_last_ambient_sound_cmp.index; + sound.component = m_last_ambient_sound_cmp; sound.entity = entity; sound.clip = nullptr; sound.playing_sound = -1; @@ -292,10 +294,10 @@ struct AudioSceneImpl : public AudioScene } - ComponentIndex createComponent(ComponentType type, Entity entity) override; + ComponentHandle createComponent(ComponentType type, Entity entity) override; - int getAmbientSoundIdx(ComponentIndex component) const + int getAmbientSoundIdx(ComponentHandle component) const { for (int i = 0, c = m_ambient_sounds.size(); i < c; ++i) { @@ -305,16 +307,16 @@ struct AudioSceneImpl : public AudioScene } - void destroyListener(ComponentIndex component) + void destroyListener(ComponentHandle component) { - ASSERT(component == 0); + ASSERT(component.index == 0); auto entity = m_listener.entity; m_listener.entity = INVALID_ENTITY; m_universe.destroyComponent(entity, LISTENER_TYPE, this, component); } - void destroyAmbientSound(ComponentIndex component) + void destroyAmbientSound(ComponentHandle component) { int idx = getAmbientSoundIdx(component); auto entity = m_ambient_sounds[idx].entity; @@ -323,7 +325,7 @@ struct AudioSceneImpl : public AudioScene } - void destroyComponent(ComponentIndex component, ComponentType type) override; + void destroyComponent(ComponentHandle component, ComponentType type) override; void serialize(OutputBlob& serializer) override @@ -340,7 +342,7 @@ struct AudioSceneImpl : public AudioScene serializer.writeString(clip->clip->getPath().c_str()); } - serializer.write(m_last_ambient_sound_id); + serializer.write(m_last_ambient_sound_cmp); serializer.write(m_ambient_sounds.size()); for (auto& i : m_ambient_sounds) { @@ -375,7 +377,7 @@ struct AudioSceneImpl : public AudioScene serializer.read(m_listener.entity); if (m_listener.entity != INVALID_ENTITY) { - m_universe.addComponent(m_listener.entity, LISTENER_TYPE, this, 0); + m_universe.addComponent(m_listener.entity, LISTENER_TYPE, this, {0}); } int count = 0; @@ -403,7 +405,7 @@ struct AudioSceneImpl : public AudioScene clip->clip = static_cast(m_system.getClipManager().load(Path(path))); } - serializer.read(m_last_ambient_sound_id); + serializer.read(m_last_ambient_sound_cmp); serializer.read(count); m_ambient_sounds.resize(count); for (int i = 0; i < count; ++i) @@ -442,9 +444,13 @@ struct AudioSceneImpl : public AudioScene } - ComponentIndex getComponent(Entity entity, ComponentType type) override + ComponentHandle getComponent(Entity entity, ComponentType type) override { - if (type == LISTENER_TYPE) return m_listener.entity == entity ? 0 : INVALID_COMPONENT; + if (type == LISTENER_TYPE) + { + ComponentHandle listener_cmp = { 0 }; + return m_listener.entity == entity ? listener_cmp : INVALID_COMPONENT; + } if (type == AMBIENT_SOUND_TYPE) { @@ -616,10 +622,10 @@ struct AudioSceneImpl : public AudioScene Universe& getUniverse() override { return m_universe; } IPlugin& getPlugin() const override { return m_system; } - int m_last_ambient_sound_id; + ComponentHandle m_last_ambient_sound_cmp; Array m_ambient_sounds; Array m_echo_zones; - int m_last_echo_zone_id; + ComponentHandle m_last_echo_zone_cmp; AudioDevice& m_device; Listener m_listener; IAllocator& m_allocator; @@ -633,8 +639,8 @@ struct AudioSceneImpl : public AudioScene static struct { ComponentType type; - ComponentIndex(AudioSceneImpl::*creator)(Entity); - void (AudioSceneImpl::*destroyer)(ComponentIndex); + ComponentHandle(AudioSceneImpl::*creator)(Entity); + void (AudioSceneImpl::*destroyer)(ComponentHandle); } COMPONENT_INFOS[] = { { LISTENER_TYPE, &AudioSceneImpl::createListener, &AudioSceneImpl::destroyListener }, { AMBIENT_SOUND_TYPE, &AudioSceneImpl::createAmbientSound, &AudioSceneImpl::destroyAmbientSound }, @@ -642,7 +648,7 @@ static struct }; -ComponentIndex AudioSceneImpl::createComponent(ComponentType type, Entity entity) +ComponentHandle AudioSceneImpl::createComponent(ComponentType type, Entity entity) { for(auto& i : COMPONENT_INFOS) { @@ -656,7 +662,7 @@ ComponentIndex AudioSceneImpl::createComponent(ComponentType type, Entity entity } -void AudioSceneImpl::destroyComponent(ComponentIndex component, ComponentType type) +void AudioSceneImpl::destroyComponent(ComponentHandle component, ComponentType type) { for(auto& i : COMPONENT_INFOS) { diff --git a/src/audio/audio_scene.h b/src/audio/audio_scene.h index 0aca68d9c..9a41b61ea 100644 --- a/src/audio/audio_scene.h +++ b/src/audio/audio_scene.h @@ -46,17 +46,17 @@ public: virtual void removeClip(ClipInfo* clip) = 0; virtual void setClip(int clip_id, const Lumix::Path& path) = 0; - virtual float getEchoZoneRadius(ComponentIndex cmp) = 0; - virtual void setEchoZoneRadius(ComponentIndex cmp, float radius) = 0; - virtual float getEchoZoneDelay(ComponentIndex cmp) = 0; - virtual void setEchoZoneDelay(ComponentIndex cmp, float delay) = 0; + virtual float getEchoZoneRadius(ComponentHandle cmp) = 0; + virtual void setEchoZoneRadius(ComponentHandle cmp, float radius) = 0; + virtual float getEchoZoneDelay(ComponentHandle cmp) = 0; + virtual void setEchoZoneDelay(ComponentHandle cmp, float delay) = 0; - virtual ClipInfo* getAmbientSoundClip(ComponentIndex cmp) = 0; - virtual int getAmbientSoundClipIndex(ComponentIndex cmp) = 0; - virtual void setAmbientSoundClipIndex(ComponentIndex cmp, int index) = 0; - virtual void setAmbientSoundClip(ComponentIndex cmp, ClipInfo* clip) = 0; - virtual bool isAmbientSound3D(ComponentIndex cmp) = 0; - virtual void setAmbientSound3D(ComponentIndex cmp, bool is_3d) = 0; + virtual ClipInfo* getAmbientSoundClip(ComponentHandle cmp) = 0; + virtual int getAmbientSoundClipIndex(ComponentHandle cmp) = 0; + virtual void setAmbientSoundClipIndex(ComponentHandle cmp, int index) = 0; + virtual void setAmbientSoundClip(ComponentHandle cmp, ClipInfo* clip) = 0; + virtual bool isAmbientSound3D(ComponentHandle cmp) = 0; + virtual void setAmbientSound3D(ComponentHandle cmp, bool is_3d) = 0; virtual SoundHandle play(Entity entity, ClipInfo* clip, bool is_3d) = 0; virtual void stop(SoundHandle sound_id) = 0; diff --git a/src/audio/editor/plugins.cpp b/src/audio/editor/plugins.cpp index cc6b4f40f..66a9d6b0c 100644 --- a/src/audio/editor/plugins.cpp +++ b/src/audio/editor/plugins.cpp @@ -204,7 +204,7 @@ struct EditorPlugin : public WorldEditor::Plugin if (cmp.type == ECHO_ZONE_TYPE) { auto* audio_scene = static_cast(cmp.scene); - float radius = audio_scene->getEchoZoneRadius(cmp.index); + float radius = audio_scene->getEchoZoneRadius(cmp.handle); Universe& universe = audio_scene->getUniverse(); Vec3 pos = universe.getPosition(cmp.entity); diff --git a/src/editor/editor_icon.cpp b/src/editor/editor_icon.cpp index 432a7317a..5dba5eb87 100644 --- a/src/editor/editor_icon.cpp +++ b/src/editor/editor_icon.cpp @@ -190,8 +190,8 @@ struct EditorIconsImpl : public EditorIcons if(!render_interface) return hit; const auto& universe = *m_editor.getUniverse(); - ComponentIndex camera = m_editor.getEditCamera().index; - if(camera < 0) return hit; + ComponentHandle camera = m_editor.getEditCamera().handle; + if (!isValid(camera)) return hit; Matrix camera_mtx = universe.getMatrix(m_editor.getEditCamera().entity); Vec3 camera_pos = camera_mtx.getTranslation(); bool is_ortho = render_interface->isCameraOrtho(camera); @@ -279,8 +279,8 @@ struct EditorIconsImpl : public EditorIcons if(!render_interface) return; const auto& universe = *m_editor.getUniverse(); - ComponentIndex camera = m_editor.getEditCamera().index; - if(camera < 0) return; + ComponentHandle camera = m_editor.getEditCamera().handle; + if (!isValid(camera)) return; Matrix camera_mtx = universe.getMatrix(m_editor.getEditCamera().entity); Vec3 camera_pos = camera_mtx.getTranslation(); float fov = m_editor.getRenderInterface()->getCameraFOV(camera); diff --git a/src/editor/gizmo.cpp b/src/editor/gizmo.cpp index 012178b0f..39555eaba 100644 --- a/src/editor/gizmo.cpp +++ b/src/editor/gizmo.cpp @@ -368,7 +368,7 @@ struct GizmoImpl : public Gizmo auto edit_camera = m_editor.getEditCamera(); Vec3 origin, cursor_dir; - m_editor.getRenderInterface()->getRay(edit_camera.index, m_editor.getMouseX(), m_editor.getMouseY(), origin, cursor_dir); + m_editor.getRenderInterface()->getRay(edit_camera.handle, m_editor.getMouseX(), m_editor.getMouseY(), origin, cursor_dir); m_transform_axis = Axis::NONE; m_active = -1; @@ -479,7 +479,7 @@ struct GizmoImpl : public Gizmo auto gizmo_mtx = m_editor.getUniverse()->getMatrix(m_entities[m_active]); auto camera = m_editor.getEditCamera(); Vec3 origin, dir; - m_editor.getRenderInterface()->getRay(camera.index, m_editor.getMouseX(), m_editor.getMouseY(), origin, dir); + m_editor.getRenderInterface()->getRay(camera.handle, m_editor.getMouseX(), m_editor.getMouseY(), origin, dir); dir.normalize(); Matrix camera_mtx = m_editor.getUniverse()->getPositionAndRotation(camera.entity); bool is_two_axed = m_transform_axis == Axis::XZ || m_transform_axis == Axis::XY || m_transform_axis == Axis::YZ; @@ -660,10 +660,10 @@ struct GizmoImpl : public Gizmo { auto edit_camera = m_editor.getEditCamera(); auto* render_interface = m_editor.getRenderInterface(); - bool is_ortho = render_interface->isCameraOrtho(edit_camera.index); + bool is_ortho = render_interface->isCameraOrtho(edit_camera.handle); auto camera_pos = m_editor.getUniverse()->getPosition(edit_camera.entity); auto camera_dir = m_editor.getUniverse()->getRotation(edit_camera.entity) * Vec3(0, 0, -1); - float fov = render_interface->getCameraFOV(edit_camera.index); + float fov = render_interface->getCameraFOV(edit_camera.handle); collide(camera_pos, camera_dir, fov, is_ortho); transform(); diff --git a/src/editor/property_grid.cpp b/src/editor/property_grid.cpp index f42f25a22..bdbeb7d40 100644 --- a/src/editor/property_grid.cpp +++ b/src/editor/property_grid.cpp @@ -231,12 +231,12 @@ void PropertyGrid::showEnumProperty(Lumix::ComponentUID cmp, int index, Lumix::I Lumix::OutputBlob blob(m_editor.getAllocator()); desc.get(cmp, index, blob); int value = *(int*)blob.getData(); - int count = desc.getEnumCount(cmp.scene, cmp.index); + int count = desc.getEnumCount(cmp.scene, cmp.handle); struct Data { Lumix::IEnumPropertyDescriptor* descriptor; - Lumix::ComponentIndex cmp; + Lumix::ComponentHandle cmp; Lumix::IScene* scene; }; @@ -256,7 +256,7 @@ void PropertyGrid::showEnumProperty(Lumix::ComponentUID cmp, int index, Lumix::I }; Data data; - data.cmp = cmp.index; + data.cmp = cmp.handle; data.scene = cmp.scene; data.descriptor = &desc; diff --git a/src/editor/render_interface.h b/src/editor/render_interface.h index 911d3f860..14838940f 100644 --- a/src/editor/render_interface.h +++ b/src/editor/render_interface.h @@ -30,14 +30,14 @@ public: virtual ~RenderInterface() {} virtual AABB getEntityAABB(Universe& universe, Entity entity) = 0; - virtual float getCameraFOV(ComponentIndex cmp) = 0; - virtual bool isCameraOrtho(ComponentIndex cmp) = 0; - virtual float getCameraOrthoSize(ComponentIndex cmp) = 0; - virtual Vec2 getCameraScreenSize(ComponentIndex cmp) = 0; - virtual ComponentIndex getCameraInSlot(const char* slot) = 0; - virtual void setCameraSlot(ComponentIndex cmp, const char* slot) = 0; - virtual Entity getCameraEntity(ComponentIndex cmp) = 0; - virtual void getRay(ComponentIndex camera_index, float x, float y, Vec3& origin, Vec3& dir) = 0; + virtual float getCameraFOV(ComponentHandle cmp) = 0; + virtual bool isCameraOrtho(ComponentHandle cmp) = 0; + virtual float getCameraOrthoSize(ComponentHandle cmp) = 0; + virtual Vec2 getCameraScreenSize(ComponentHandle cmp) = 0; + virtual ComponentHandle getCameraInSlot(const char* slot) = 0; + virtual void setCameraSlot(ComponentHandle cmp, const char* slot) = 0; + virtual Entity getCameraEntity(ComponentHandle cmp) = 0; + virtual void getRay(ComponentHandle camera_index, float x, float y, Vec3& origin, Vec3& dir) = 0; virtual float castRay(ModelHandle model, const Vec3& origin, const Vec3& dir, const Matrix& mtx) = 0; virtual void renderModel(ModelHandle model, const Matrix& mtx) = 0; virtual ModelHandle loadModel(Lumix::Path& path) = 0; @@ -48,8 +48,8 @@ public: virtual void addDebugCube(const Vec3& minimum, const Vec3& maximum, uint32 color, float life) = 0; virtual void addDebugCross(const Vec3& pos, float size, uint32 color, float life) = 0; virtual void addDebugLine(const Vec3& from, const Vec3& to, uint32 color, float life) = 0; - virtual WorldEditor::RayHit castRay(const Vec3& origin, const Vec3& dir, ComponentIndex ignored) = 0; - virtual Path getRenderablePath(ComponentIndex cmp) = 0; + virtual WorldEditor::RayHit castRay(const Vec3& origin, const Vec3& dir, ComponentHandle ignored) = 0; + virtual Path getRenderablePath(ComponentHandle cmp) = 0; virtual void render(const Matrix& mtx, uint16* indices, int indices_count, diff --git a/src/editor/utils.cpp b/src/editor/utils.cpp index a87049542..36c1d8991 100644 --- a/src/editor/utils.cpp +++ b/src/editor/utils.cpp @@ -18,7 +18,7 @@ void getEntityListDisplayName(Lumix::WorldEditor& editor, char* buf, int max_siz if (renderable.isValid()) { auto* render_interface = editor.getRenderInterface(); - auto path = render_interface->getRenderablePath(renderable.index); + auto path = render_interface->getRenderablePath(renderable.handle); if (path.isValid()) { char basename[Lumix::MAX_PATH_LENGTH]; diff --git a/src/editor/world_editor.cpp b/src/editor/world_editor.cpp index 28b9c51c7..c431bcff6 100644 --- a/src/editor/world_editor.cpp +++ b/src/editor/world_editor.cpp @@ -560,7 +560,7 @@ public: { serializer.serialize("inedx", m_index); serializer.serialize("entity_index", m_component.entity); - serializer.serialize("component_index", m_component.index); + serializer.serialize("component_index", m_component.handle); serializer.serialize("component_type", PropertyRegister::getComponentTypeHash(m_component.type)); serializer.serialize("property_name_hash", m_descriptor->getNameHash()); } @@ -570,7 +570,7 @@ public: { serializer.deserialize("inedx", m_index, 0); serializer.deserialize("entity_index", m_component.entity, INVALID_ENTITY); - serializer.deserialize("component_index", m_component.index, 0); + serializer.deserialize("component_index", m_component.handle, INVALID_COMPONENT); uint32 hash; serializer.deserialize("component_type", hash, 0); m_component.type = PropertyRegister::getComponentTypeFromHash(hash); @@ -643,7 +643,7 @@ public: { serializer.serialize("inedx", m_index); serializer.serialize("entity_index", m_component.entity); - serializer.serialize("component_index", m_component.index); + serializer.serialize("component_index", m_component.handle); serializer.serialize("component_type", PropertyRegister::getComponentTypeHash(m_component.type)); serializer.serialize("property_name_hash", m_descriptor->getNameHash()); } @@ -653,7 +653,7 @@ public: { serializer.deserialize("inedx", m_index, 0); serializer.deserialize("entity_index", m_component.entity, INVALID_ENTITY); - serializer.deserialize("component_index", m_component.index, 0); + serializer.deserialize("component_index", m_component.handle, INVALID_COMPONENT); uint32 hash; serializer.deserialize("component_type", hash, 0); m_component.type = PropertyRegister::getComponentTypeFromHash(hash); @@ -991,7 +991,7 @@ private: { const ComponentUID& cmp = m_editor.getComponent(m_entities[i], m_type); - cmp.scene->destroyComponent(cmp.index, cmp.type); + cmp.scene->destroyComponent(cmp.handle, cmp.type); } } @@ -1123,7 +1123,7 @@ private: ComponentUID new_component; for (int i = 0; i < scenes.size(); ++i) { - new_component.index = scenes[i]->createComponent(cmp_type, new_entity); + new_component.handle = scenes[i]->createComponent(cmp_type, new_entity); new_component.entity = new_entity; new_component.scene = scenes[i]; new_component.type = cmp_type; @@ -1190,7 +1190,7 @@ private: void serialize(JsonSerializer& serializer) override { serializer.serialize("entity", m_component.entity); - serializer.serialize("component", m_component.index); + serializer.serialize("component", m_component.handle); serializer.serialize("component_type", PropertyRegister::getComponentTypeHash(m_component.type)); } @@ -1198,7 +1198,7 @@ private: void deserialize(JsonSerializer& serializer) override { serializer.deserialize("entity", m_component.entity, INVALID_ENTITY); - serializer.deserialize("component", m_component.index, 0); + serializer.deserialize("component", m_component.handle, INVALID_COMPONENT); uint32 hash; serializer.deserialize("component_type", hash, 0); m_component.type = PropertyRegister::getComponentTypeFromHash(hash); @@ -1215,11 +1215,11 @@ private: { for (int i = 0; i < scenes.size(); ++i) { - ComponentIndex cmp = + ComponentHandle cmp = scenes[i]->createComponent(m_component.type, m_component.entity); - if (cmp != INVALID_COMPONENT) + if (isValid(cmp)) { - m_component.index = cmp; + m_component.handle = cmp; m_component.scene = scenes[i]; break; } @@ -1289,13 +1289,13 @@ private: ComponentUID cmp = m_editor.getComponent(instances[i], m_component.type); if (cmp.isValid()) { - cmp.scene->destroyComponent(cmp.index, cmp.type); + cmp.scene->destroyComponent(cmp.handle, cmp.type); } } } else { - m_component.scene->destroyComponent(m_component.index, m_component.type); + m_component.scene->destroyComponent(m_component.handle, m_component.type); } return true; } @@ -1586,7 +1586,7 @@ public: ComponentUID camera_cmp = getComponent(m_camera, CAMERA_TYPE); if (camera_cmp.isValid()) { - m_render_interface->getRay(camera_cmp.index, (float)x, (float)y, origin, dir); + m_render_interface->getRay(camera_cmp.handle, (float)x, (float)y, origin, dir); auto hit = m_render_interface->castRay(origin, dir, INVALID_COMPONENT); if (m_gizmo->isActive()) return; @@ -1771,14 +1771,14 @@ public: ComponentUID renderable = getComponent(m_selected_entities[i], RENDERABLE_TYPE); Vec3 origin = universe->getPosition(entity); - auto hit = m_render_interface->castRay(origin, Vec3(0, -1, 0), renderable.index); + auto hit = m_render_interface->castRay(origin, Vec3(0, -1, 0), renderable.handle); if (hit.is_hit) { new_positions.push(origin + Vec3(0, -hit.t, 0)); } else { - hit = m_render_interface->castRay(origin, Vec3(0, 1, 0), renderable.index); + hit = m_render_interface->castRay(origin, Vec3(0, 1, 0), renderable.handle); if (hit.is_hit) { new_positions.push(origin + Vec3(0, hit.t, 0)); @@ -1813,7 +1813,7 @@ public: Entity addEntity() override { ComponentUID cmp = getComponent(m_camera, CAMERA_TYPE); - Vec2 size = m_render_interface->getCameraScreenSize(cmp.index); + Vec2 size = m_render_interface->getCameraScreenSize(cmp.handle); return addEntityAt((int)size.x >> 1, (int)size.y >> 1); } @@ -1825,7 +1825,7 @@ public: Vec3 origin; Vec3 dir; - m_render_interface->getRay(camera_cmp.index, (float)camera_x, (float)camera_y, origin, dir); + m_render_interface->getRay(camera_cmp.handle, (float)camera_x, (float)camera_y, origin, dir); auto hit = m_render_interface->castRay(origin, dir, INVALID_COMPONENT); Vec3 pos; if (hit.is_hit) @@ -1847,12 +1847,12 @@ public: { ComponentUID camera_cmp = getComponent(m_camera, CAMERA_TYPE); Universe* universe = getUniverse(); - Vec2 screen_size = m_render_interface->getCameraScreenSize(camera_cmp.index); + Vec2 screen_size = m_render_interface->getCameraScreenSize(camera_cmp.handle); screen_size *= 0.5f; Vec3 origin; Vec3 dir; - m_render_interface->getRay(camera_cmp.index, (float)screen_size.x, (float)screen_size.y, origin, dir); + m_render_interface->getRay(camera_cmp.handle, (float)screen_size.x, (float)screen_size.y, origin, dir); auto hit = m_render_interface->castRay(origin, dir, INVALID_COMPONENT); Vec3 pos; if (hit.is_hit) @@ -2544,7 +2544,7 @@ public: static const uint32 SLOT_HASH = crc32("Slot"); if (component_type == CAMERA_TYPE && property.getNameHash() == SLOT_HASH) { - if (m_render_interface->getCameraEntity(cmp.index) == m_camera) + if (m_render_interface->getCameraEntity(cmp.handle) == m_camera) { return; } @@ -2772,7 +2772,7 @@ public: universe->setEntityName(m_camera, "editor_camera"); ComponentUID cmp = createComponent(CAMERA_TYPE, m_camera); ASSERT(cmp.isValid()); - m_render_interface->setCameraSlot(cmp.index, "editor"); + m_render_interface->setCameraSlot(cmp.handle, "editor"); } } diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index 2b9a55074..4ba3a9c79 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -134,16 +134,16 @@ public: } - static ComponentIndex LUA_createComponent(IScene* scene, const char* type, int entity_idx) + static ComponentHandle LUA_createComponent(IScene* scene, const char* type, int entity_idx) { - if (!scene) return -1; + if (!scene) return INVALID_COMPONENT; Entity e = {entity_idx}; ComponentType handle = PropertyRegister::getComponentType(type); if (scene->getComponent(e, handle) != INVALID_COMPONENT) { g_log_error.log("Lua Script") << "Component " << type << " already exists in entity " << entity_idx; - return -1; + return INVALID_COMPONENT; } return scene->createComponent(handle, e); diff --git a/src/engine/hash_map.h b/src/engine/hash_map.h index 869be571e..4353d4aab 100644 --- a/src/engine/hash_map.h +++ b/src/engine/hash_map.h @@ -75,6 +75,16 @@ namespace Lumix } }; + template<> + struct HashFunc + { + static uint32 get(const ComponentHandle& key) + { + static_assert(sizeof(int32) == sizeof(key.index), "Check this"); + return HashFunc::get(key.index); + } + }; + template<> struct HashFunc { diff --git a/src/engine/iplugin.h b/src/engine/iplugin.h index b9090276e..482778662 100644 --- a/src/engine/iplugin.h +++ b/src/engine/iplugin.h @@ -19,14 +19,14 @@ namespace Lumix public: virtual ~IScene() {} - virtual ComponentIndex createComponent(ComponentType, Entity) = 0; - virtual void destroyComponent(ComponentIndex component, ComponentType type) = 0; + virtual ComponentHandle createComponent(ComponentType, Entity) = 0; + virtual void destroyComponent(ComponentHandle component, ComponentType type) = 0; virtual void serialize(OutputBlob& serializer) = 0; virtual void deserialize(InputBlob& serializer, int version) = 0; virtual IPlugin& getPlugin() const = 0; virtual void update(float time_delta, bool paused) = 0; virtual bool ownComponentType(ComponentType type) const = 0; - virtual ComponentIndex getComponent(Entity entity, ComponentType type) = 0; + virtual ComponentHandle getComponent(Entity entity, ComponentType type) = 0; virtual Universe& getUniverse() = 0; virtual void startGame() {} virtual void stopGame() {} diff --git a/src/engine/iproperty_descriptor.h b/src/engine/iproperty_descriptor.h index 834b61484..c458bee45 100644 --- a/src/engine/iproperty_descriptor.h +++ b/src/engine/iproperty_descriptor.h @@ -99,9 +99,9 @@ public: { } - virtual int getEnumCount(IScene* scene, ComponentIndex cmp) = 0; - virtual const char* getEnumItemName(IScene* scene, ComponentIndex cmp, int index) = 0; - virtual void getEnumItemName(IScene* scene, ComponentIndex cmp, int index, char* buf, int max_size) {} + virtual int getEnumCount(IScene* scene, ComponentHandle cmp) = 0; + virtual const char* getEnumItemName(IScene* scene, ComponentHandle cmp, int index) = 0; + virtual void getEnumItemName(IScene* scene, ComponentHandle cmp, int index, char* buf, int max_size) {} }; diff --git a/src/engine/json_serializer.cpp b/src/engine/json_serializer.cpp index 629997c74..d62447a1c 100644 --- a/src/engine/json_serializer.cpp +++ b/src/engine/json_serializer.cpp @@ -95,6 +95,12 @@ void JsonSerializer::serialize(const char* label, Entity value) } +void JsonSerializer::serialize(const char* label, ComponentHandle value) +{ + serialize(label, value.index); +} + + void JsonSerializer::serialize(const char* label, unsigned int value) { writeBlockComma(); @@ -239,6 +245,12 @@ void JsonSerializer::serializeArrayItem(Entity value) } +void JsonSerializer::serializeArrayItem(ComponentHandle value) +{ + serializeArrayItem(value.index); +} + + void JsonSerializer::serializeArrayItem(int value) { writeBlockComma(); @@ -296,6 +308,13 @@ void JsonSerializer::deserialize(const char* label, Entity& value, Entity defaul deserialize(label, value.index, default_value.index); } + +void JsonSerializer::deserialize(const char* label, ComponentHandle& value, ComponentHandle default_value) +{ + deserialize(label, value.index, default_value.index); +} + + void JsonSerializer::deserialize(bool& value, bool default_value) { value = !m_is_string_token ? m_token_size == 4 && (compareStringN(m_token, "true", 4) == 0) @@ -551,6 +570,12 @@ void JsonSerializer::deserializeArrayItem(Entity& value, Entity default_value) } +void JsonSerializer::deserializeArrayItem(ComponentHandle& value, ComponentHandle default_value) +{ + deserializeArrayItem(value.index, default_value.index); +} + + void JsonSerializer::deserializeArrayItem(uint32& value, uint32 default_value) { deserializeArrayComma(); diff --git a/src/engine/json_serializer.h b/src/engine/json_serializer.h index 1e61bcece..bd5b8c553 100644 --- a/src/engine/json_serializer.h +++ b/src/engine/json_serializer.h @@ -34,6 +34,7 @@ namespace Lumix // serialize void serialize(const char* label, Entity value); + void serialize(const char* label, ComponentHandle value); void serialize(const char* label, uint32 value); void serialize(const char* label, float value); void serialize(const char* label, int32 value); @@ -46,6 +47,7 @@ namespace Lumix void beginArray(const char* label); void endArray(); void serializeArrayItem(Entity value); + void serializeArrayItem(ComponentHandle value); void serializeArrayItem(uint32 value); void serializeArrayItem(int32 value); void serializeArrayItem(int64 value); @@ -55,6 +57,7 @@ namespace Lumix // deserialize void deserialize(const char* label, Entity& value, Entity default_value); + void deserialize(const char* label, ComponentHandle& value, ComponentHandle default_value); void deserialize(const char* label, uint32& value, uint32 default_value); void deserialize(const char* label, float& value, float default_value); void deserialize(const char* label, int32& value, int32 default_value); @@ -71,6 +74,7 @@ namespace Lumix void deserializeArrayEnd(); bool isArrayEnd(); void deserializeArrayItem(Entity& value, Entity default_value); + void deserializeArrayItem(ComponentHandle& value, ComponentHandle default_value); void deserializeArrayItem(uint32& value, uint32 default_value); void deserializeArrayItem(int32& value, int32 default_value); void deserializeArrayItem(int64& value, int64 default_value); diff --git a/src/engine/lua_wrapper.h b/src/engine/lua_wrapper.h index 8645bf225..083078545 100644 --- a/src/engine/lua_wrapper.h +++ b/src/engine/lua_wrapper.h @@ -25,7 +25,11 @@ template <> inline int toType(lua_State* L, int index) } template <> inline Entity toType(lua_State* L, int index) { - return {(int)lua_tointeger(L, index)}; + return{ (int)lua_tointeger(L, index) }; +} +template <> inline ComponentHandle toType(lua_State* L, int index) +{ + return{ (int)lua_tointeger(L, index) }; } template <> inline Vec3 toType(lua_State* L, int index) { @@ -186,6 +190,10 @@ template <> inline void pushLua(lua_State* L, Entity value) { lua_pushnumber(L, value.index); } +template <> inline void pushLua(lua_State* L, ComponentHandle value) +{ + lua_pushnumber(L, value.index); +} inline void pushLua(lua_State* L, const Vec2& value) { lua_createtable(L, 2, 0); diff --git a/src/engine/lumix.h b/src/engine/lumix.h index ef73f111f..a3599ed33 100644 --- a/src/engine/lumix.h +++ b/src/engine/lumix.h @@ -70,7 +70,16 @@ static_assert(sizeof(int16) == 2, "Incorrect size of int16"); static_assert(sizeof(int8) == 1, "Incorrect size of int8"); const uint32 MAX_PATH_LENGTH = 260; -typedef int ComponentIndex; +struct ComponentHandle +{ + int index; + bool operator==(const ComponentHandle& rhs) const { return rhs.index == index; }; + bool operator<(const ComponentHandle& rhs) const { return rhs.index < index; }; + bool operator>(const ComponentHandle& rhs) const { return rhs.index > index; }; + bool operator!=(const ComponentHandle& rhs) const { return rhs.index != index; }; +}; +inline bool isValid(ComponentHandle cmp) { return cmp.index >= 0; } + struct Entity { int index; @@ -89,7 +98,7 @@ struct ComponentType bool operator!=(const ComponentType& rhs) const { return rhs.index != index; }; }; const Entity INVALID_ENTITY = {-1}; -const int INVALID_COMPONENT = -1; +const ComponentHandle INVALID_COMPONENT = {-1}; template int lengthOf(const T (&)[count]) { diff --git a/src/engine/property_descriptor.h b/src/engine/property_descriptor.h index 091d3cb9c..da54acfeb 100644 --- a/src/engine/property_descriptor.h +++ b/src/engine/property_descriptor.h @@ -30,10 +30,10 @@ private: static const int MAX_STRING_SIZE = 300; public: - typedef const char* (S::*Getter)(ComponentIndex); - typedef void (S::*Setter)(ComponentIndex, const char*); - typedef const char* (S::*ArrayGetter)(ComponentIndex, int); - typedef void (S::*ArraySetter)(ComponentIndex, int, const char*); + typedef const char* (S::*Getter)(ComponentHandle); + typedef void (S::*Setter)(ComponentHandle, const char*); + typedef const char* (S::*ArrayGetter)(ComponentHandle, int); + typedef void (S::*ArraySetter)(ComponentHandle, int, const char*); public: StringPropertyDescriptor(const char* name, Getter getter, Setter setter, IAllocator& allocator) @@ -68,11 +68,11 @@ public: if (index < 0) { - (static_cast(cmp.scene)->*m_single.setter)(cmp.index, tmp); + (static_cast(cmp.scene)->*m_single.setter)(cmp.handle, tmp); } else { - (static_cast(cmp.scene)->*m_array.setter)(cmp.index, index, tmp); + (static_cast(cmp.scene)->*m_array.setter)(cmp.handle, index, tmp); } } @@ -82,11 +82,11 @@ public: const char* value; if (index < 0) { - value = (static_cast(cmp.scene)->*m_single.getter)(cmp.index); + value = (static_cast(cmp.scene)->*m_single.getter)(cmp.handle); } else { - value = (static_cast(cmp.scene)->*m_array.getter)(cmp.index, index); + value = (static_cast(cmp.scene)->*m_array.getter)(cmp.handle, index); } int len = stringLength(value) + 1; stream.write(value, len); @@ -113,9 +113,9 @@ private: template class ArrayDescriptor : public IArrayDescriptor { public: - typedef int (S::*Counter)(ComponentIndex); - typedef void (S::*Adder)(ComponentIndex, int); - typedef void (S::*Remover)(ComponentIndex, int); + typedef int (S::*Counter)(ComponentHandle); + typedef void (S::*Adder)(ComponentHandle, int); + typedef void (S::*Remover)(ComponentHandle, int); public: ArrayDescriptor(const char* name, @@ -188,19 +188,19 @@ public: int getCount(ComponentUID cmp) const override { - return (static_cast(cmp.scene)->*m_counter)(cmp.index); + return (static_cast(cmp.scene)->*m_counter)(cmp.handle); } void addArrayItem(ComponentUID cmp, int index) const override { - if (m_adder) (static_cast(cmp.scene)->*m_adder)(cmp.index, index); + if (m_adder) (static_cast(cmp.scene)->*m_adder)(cmp.handle, index); } void removeArrayItem(ComponentUID cmp, int index) const override { - (static_cast(cmp.scene)->*m_remover)(cmp.index, index); + (static_cast(cmp.scene)->*m_remover)(cmp.handle, index); } @@ -227,10 +227,10 @@ private: template class IntPropertyDescriptor : public IPropertyDescriptor { public: - typedef int (S::*Getter)(ComponentIndex); - typedef void (S::*Setter)(ComponentIndex, int); - typedef int (S::*ArrayGetter)(ComponentIndex, int); - typedef void (S::*ArraySetter)(ComponentIndex, int, int); + typedef int (S::*Getter)(ComponentHandle); + typedef void (S::*Setter)(ComponentHandle, int); + typedef int (S::*ArrayGetter)(ComponentHandle, int); + typedef void (S::*ArraySetter)(ComponentHandle, int, int); public: IntPropertyDescriptor() {} @@ -265,11 +265,11 @@ public: stream.read(&i, sizeof(i)); if(index < 0) { - (static_cast(cmp.scene)->*m_single.setter)(cmp.index, i); + (static_cast(cmp.scene)->*m_single.setter)(cmp.handle, i); } else { - (static_cast(cmp.scene)->*m_array.setter)(cmp.index, index, i); + (static_cast(cmp.scene)->*m_array.setter)(cmp.handle, index, i); } }; @@ -279,11 +279,11 @@ public: int32 i = 0; if(index < 0) { - i = (static_cast(cmp.scene)->*m_single.getter)(cmp.index); + i = (static_cast(cmp.scene)->*m_single.getter)(cmp.handle); } else { - i = (static_cast(cmp.scene)->*m_array.getter)(cmp.index, index); + i = (static_cast(cmp.scene)->*m_array.getter)(cmp.handle, index); } stream.write(i); }; @@ -323,8 +323,8 @@ private: template class BoolPropertyDescriptor : public IPropertyDescriptor { public: - typedef bool (S::*Getter)(ComponentIndex); - typedef void (S::*Setter)(ComponentIndex, bool); + typedef bool (S::*Getter)(ComponentHandle); + typedef void (S::*Setter)(ComponentHandle, bool); public: BoolPropertyDescriptor(const char* name, Getter getter, Setter setter, IAllocator& allocator) @@ -342,14 +342,14 @@ public: ASSERT(index == -1); bool b; stream.read(&b, sizeof(b)); - (static_cast(cmp.scene)->*m_setter)(cmp.index, b); + (static_cast(cmp.scene)->*m_setter)(cmp.handle, b); }; void get(ComponentUID cmp, int index, OutputBlob& stream) const override { ASSERT(index == -1); - bool b = (static_cast(cmp.scene)->*m_getter)(cmp.index); + bool b = (static_cast(cmp.scene)->*m_getter)(cmp.handle); int len = sizeof(b); stream.write(&b, len); }; @@ -363,10 +363,10 @@ private: template class SimplePropertyDescriptor : public IPropertyDescriptor { public: - typedef T (S::*Getter)(ComponentIndex); - typedef void (S::*Setter)(ComponentIndex, const T&); - typedef T(S::*ArrayGetter)(ComponentIndex, int); - typedef void (S::*ArraySetter)(ComponentIndex, int, const T&); + typedef T (S::*Getter)(ComponentHandle); + typedef void (S::*Setter)(ComponentHandle, const T&); + typedef T(S::*ArrayGetter)(ComponentHandle, int); + typedef void (S::*ArraySetter)(ComponentHandle, int, const T&); public: SimplePropertyDescriptor(const char* name, Getter getter, Setter setter, IAllocator& allocator) @@ -395,11 +395,11 @@ public: stream.read(&v, sizeof(v)); if (index < 0) { - (static_cast(cmp.scene)->*m_single.setter)(cmp.index, v); + (static_cast(cmp.scene)->*m_single.setter)(cmp.handle, v); } else { - (static_cast(cmp.scene)->*m_array.setter)(cmp.index, index, v); + (static_cast(cmp.scene)->*m_array.setter)(cmp.handle, index, v); } }; @@ -409,12 +409,12 @@ public: int len = sizeof(T); if (index < 0) { - T v = (static_cast(cmp.scene)->*m_single.getter)(cmp.index); + T v = (static_cast(cmp.scene)->*m_single.getter)(cmp.handle); stream.write(&v, len); } else { - T v = (static_cast(cmp.scene)->*m_array.getter)(cmp.index, index); + T v = (static_cast(cmp.scene)->*m_array.getter)(cmp.handle, index); stream.write(&v, len); } }; @@ -441,10 +441,10 @@ template class FilePropertyDescriptor : public IPropertyDescriptor { public: - typedef Path (S::*Getter)(ComponentIndex); - typedef void (S::*Setter)(ComponentIndex, const Path&); - typedef Path (S::*ArrayGetter)(ComponentIndex, int); - typedef void (S::*ArraySetter)(ComponentIndex, int, const Path&); + typedef Path (S::*Getter)(ComponentHandle); + typedef void (S::*Setter)(ComponentHandle, const Path&); + typedef Path (S::*ArrayGetter)(ComponentHandle, int); + typedef void (S::*ArraySetter)(ComponentHandle, int, const Path&); public: FilePropertyDescriptor(const char* name, @@ -490,11 +490,11 @@ public: if(index < 0) { - (static_cast(cmp.scene)->*m_single.setter)(cmp.index, Path(tmp)); + (static_cast(cmp.scene)->*m_single.setter)(cmp.handle, Path(tmp)); } else { - (static_cast(cmp.scene)->*m_array.setter)(cmp.index, index, Path(tmp)); + (static_cast(cmp.scene)->*m_array.setter)(cmp.handle, index, Path(tmp)); } } @@ -504,11 +504,11 @@ public: Path value; if(index < 0) { - value = (static_cast(cmp.scene)->*m_single.getter)(cmp.index); + value = (static_cast(cmp.scene)->*m_single.getter)(cmp.handle); } else { - value = (static_cast(cmp.scene)->*m_array.getter)(cmp.index, index); + value = (static_cast(cmp.scene)->*m_array.getter)(cmp.handle, index); } int len = value.length() + 1; stream.write(value.c_str(), len); @@ -589,9 +589,9 @@ public: template class SampledFunctionDescriptor : public ISampledFunctionDescriptor { public: - typedef const Lumix::Vec2* (S::*Getter)(ComponentIndex); - typedef int (S::*CountGetter)(ComponentIndex); - typedef void (S::*Setter)(ComponentIndex, const Lumix::Vec2*, int); + typedef const Lumix::Vec2* (S::*Getter)(ComponentHandle); + typedef int (S::*CountGetter)(ComponentHandle); + typedef void (S::*Setter)(ComponentHandle, const Lumix::Vec2*, int); public: SampledFunctionDescriptor(const char* name, @@ -619,16 +619,16 @@ public: int count; stream.read(count); auto* buf = (const Lumix::Vec2*)stream.skip(sizeof(Lumix::Vec2) * count); - (static_cast(cmp.scene)->*m_setter)(cmp.index, buf, count); + (static_cast(cmp.scene)->*m_setter)(cmp.handle, buf, count); }; void get(ComponentUID cmp, int index, OutputBlob& stream) const override { ASSERT(index == -1); - int count = (static_cast(cmp.scene)->*m_count_getter)(cmp.index); + int count = (static_cast(cmp.scene)->*m_count_getter)(cmp.handle); stream.write(count); - const Lumix::Vec2* values = (static_cast(cmp.scene)->*m_getter)(cmp.index); + const Lumix::Vec2* values = (static_cast(cmp.scene)->*m_getter)(cmp.handle); stream.write(values, sizeof(values[0]) * count); }; @@ -649,10 +649,10 @@ private: template class EntityPropertyDescriptor : public IPropertyDescriptor { public: - typedef Entity(S::*Getter)(ComponentIndex); - typedef void (S::*Setter)(ComponentIndex, Entity); - typedef Entity(S::*ArrayGetter)(ComponentIndex, int); - typedef void (S::*ArraySetter)(ComponentIndex, int, Entity); + typedef Entity(S::*Getter)(ComponentHandle); + typedef void (S::*Setter)(ComponentHandle, Entity); + typedef Entity(S::*ArrayGetter)(ComponentHandle, int); + typedef void (S::*ArraySetter)(ComponentHandle, int, Entity); public: EntityPropertyDescriptor(const char* name, @@ -689,11 +689,11 @@ template class EntityPropertyDescriptor : public IPropertyDescriptor value < 0 ? INVALID_ENTITY : cmp.scene->getUniverse().getEntityFromDenseIdx(value); if(index == -1) { - (static_cast(cmp.scene)->*m_single.setter)(cmp.index, entity); + (static_cast(cmp.scene)->*m_single.setter)(cmp.handle, entity); } else { - (static_cast(cmp.scene)->*m_array.setter)(cmp.index, index, entity); + (static_cast(cmp.scene)->*m_array.setter)(cmp.handle, index, entity); } }; @@ -703,11 +703,11 @@ template class EntityPropertyDescriptor : public IPropertyDescriptor Entity value; if(index == -1) { - value = (static_cast(cmp.scene)->*m_single.getter)(cmp.index); + value = (static_cast(cmp.scene)->*m_single.getter)(cmp.handle); } else { - value = (static_cast(cmp.scene)->*m_array.getter)(cmp.index, index); + value = (static_cast(cmp.scene)->*m_array.getter)(cmp.handle, index); } auto dense_idx = cmp.scene->getUniverse().getDenseIdx(value); int len = sizeof(dense_idx); @@ -734,10 +734,10 @@ template class EntityPropertyDescriptor : public IPropertyDescriptor template class DecimalPropertyDescriptor : public IDecimalPropertyDescriptor { public: - typedef float (S::*Getter)(ComponentIndex); - typedef void (S::*Setter)(ComponentIndex, float); - typedef float (S::*ArrayGetter)(ComponentIndex, int); - typedef void (S::*ArraySetter)(ComponentIndex, int,float); + typedef float (S::*Getter)(ComponentHandle); + typedef void (S::*Setter)(ComponentHandle, float); + typedef float (S::*ArrayGetter)(ComponentHandle, int); + typedef void (S::*ArraySetter)(ComponentHandle, int,float); public: DecimalPropertyDescriptor(const char* name, @@ -789,11 +789,11 @@ public: stream.read(&f, sizeof(f)); if(index >= 0) { - (static_cast(cmp.scene)->*m_array_setter)(cmp.index, index, f); + (static_cast(cmp.scene)->*m_array_setter)(cmp.handle, index, f); } else { - (static_cast(cmp.scene)->*m_setter)(cmp.index, f); + (static_cast(cmp.scene)->*m_setter)(cmp.handle, f); } }; @@ -803,11 +803,11 @@ public: float f = 0; if(index >= 0) { - f = (static_cast(cmp.scene)->*m_array_getter)(cmp.index, index); + f = (static_cast(cmp.scene)->*m_array_getter)(cmp.handle, index); } else { - f = (static_cast(cmp.scene)->*m_getter)(cmp.index); + f = (static_cast(cmp.scene)->*m_getter)(cmp.handle); } int len = sizeof(f); stream.write(&f, len); @@ -840,8 +840,8 @@ public: template class EnumPropertyDescriptor : public IEnumPropertyDescriptor { public: - typedef int (S::*Getter)(ComponentIndex); - typedef void (S::*Setter)(ComponentIndex, int); + typedef int (S::*Getter)(ComponentHandle); + typedef void (S::*Setter)(ComponentHandle, int); typedef int (S::*EnumCountGetter)() const; typedef const char* (S::*EnumNameGetter)(int); @@ -868,23 +868,23 @@ public: ASSERT(index == -1); int value; stream.read(&value, sizeof(value)); - (static_cast(cmp.scene)->*m_setter)(cmp.index, value); + (static_cast(cmp.scene)->*m_setter)(cmp.handle, value); }; void get(ComponentUID cmp, int index, OutputBlob& stream) const override { ASSERT(index == -1); - int value = (static_cast(cmp.scene)->*m_getter)(cmp.index); + int value = (static_cast(cmp.scene)->*m_getter)(cmp.handle); int len = sizeof(value); stream.write(&value, len); }; - int getEnumCount(IScene* scene, ComponentIndex) override { return (static_cast(scene)->*m_enum_count_getter)(); } + int getEnumCount(IScene* scene, ComponentHandle) override { return (static_cast(scene)->*m_enum_count_getter)(); } - const char* getEnumItemName(IScene* scene, ComponentIndex, int index) override + const char* getEnumItemName(IScene* scene, ComponentHandle, int index) override { return (static_cast(scene)->*m_enum_name_getter)(index); } diff --git a/src/engine/universe/component.cpp b/src/engine/universe/component.cpp index facced4a0..d190b0266 100644 --- a/src/engine/universe/component.cpp +++ b/src/engine/universe/component.cpp @@ -1,4 +1,4 @@ #include "engine/universe/component.h" -const Lumix::ComponentUID Lumix::ComponentUID::INVALID(Lumix::INVALID_ENTITY, {-1}, 0, -1); +const Lumix::ComponentUID Lumix::ComponentUID::INVALID(Lumix::INVALID_ENTITY, {-1}, 0, Lumix::INVALID_COMPONENT); diff --git a/src/engine/universe/component.h b/src/engine/universe/component.h index e6a37bbf7..3f539f85e 100644 --- a/src/engine/universe/component.h +++ b/src/engine/universe/component.h @@ -16,32 +16,32 @@ struct LUMIX_ENGINE_API ComponentUID final ComponentUID() { - index = -1; + handle = INVALID_COMPONENT; scene = nullptr; entity = INVALID_ENTITY; type = {-1}; } - ComponentUID(Entity _entity, ComponentType _type, IScene* _scene, int _index) + ComponentUID(Entity _entity, ComponentType _type, IScene* _scene, ComponentHandle _handle) : entity(_entity) , type(_type) , scene(_scene) - , index(_index) + , handle(_handle) { } Entity entity; ComponentType type; IScene* scene; - ComponentIndex index; + ComponentHandle handle; static const ComponentUID INVALID; bool operator==(const ComponentUID& rhs) const { - return type == rhs.type && scene == rhs.scene && index == rhs.index; + return type == rhs.type && scene == rhs.scene && handle == rhs.handle; } - bool isValid() const { return index >= 0; } + bool isValid() const { return Lumix::isValid(handle); } }; diff --git a/src/engine/universe/hierarchy.cpp b/src/engine/universe/hierarchy.cpp index 6e5b0ccf1..8dcb4599e 100644 --- a/src/engine/universe/hierarchy.cpp +++ b/src/engine/universe/hierarchy.cpp @@ -45,23 +45,24 @@ public: } - ComponentIndex createComponent(ComponentType type, Entity entity) override + ComponentHandle createComponent(ComponentType type, Entity entity) override { if (HIERARCHY_TYPE_HANDLE == type) { m_parents.insert(entity, INVALID_ENTITY); - m_universe.addComponent(entity, type, this, entity.index); - return entity.index; + m_universe.addComponent(entity, type, this, {entity.index}); + return {entity.index}; } return INVALID_COMPONENT; } - void destroyComponent(ComponentIndex component, ComponentType type) override + void destroyComponent(ComponentHandle component, ComponentType type) override { if (HIERARCHY_TYPE_HANDLE == type) { - auto parent_iter = m_parents.find({component}); + Entity entity = {component.index}; + auto parent_iter = m_parents.find(entity); if (parent_iter.isValid()) { @@ -74,7 +75,7 @@ public: m_parents.erase(parent_iter); } - m_universe.destroyComponent({component}, type, this, component); + m_universe.destroyComponent(entity, type, this, component); } } @@ -86,10 +87,11 @@ public: IAllocator& getAllocator() { return m_allocator; } - ComponentIndex getComponent(Entity entity, ComponentType type) override + ComponentHandle getComponent(Entity entity, ComponentType type) override { ASSERT(ownComponentType(type)); - return m_parents.find(entity) != m_parents.end() ? entity.index : INVALID_COMPONENT; + ComponentHandle cmp = {entity.index}; + return m_parents.find(entity) != m_parents.end() ? cmp : INVALID_COMPONENT; } @@ -151,9 +153,9 @@ public: } - void setLocalPosition(ComponentIndex cmp, const Vec3& position) override + void setLocalPosition(ComponentHandle cmp, const Vec3& position) override { - Entity entity = {cmp}; + Entity entity = {cmp.index}; Parents::iterator parent_iter = m_parents.find(entity); if (parent_iter.isValid()) @@ -168,9 +170,9 @@ public: } - Vec3 getLocalPosition(ComponentIndex cmp) override + Vec3 getLocalPosition(ComponentHandle cmp) override { - Entity entity = {cmp}; + Entity entity = {cmp.index}; Parents::iterator parent_iter = m_parents.find(entity); if (parent_iter.isValid() && isValid(parent_iter.value())) @@ -206,9 +208,9 @@ public: } - Quat getLocalRotation(ComponentIndex cmp) override + Quat getLocalRotation(ComponentHandle cmp) override { - Entity entity = {cmp}; + Entity entity = {cmp.index}; Parents::iterator parent_iter = m_parents.find(entity); if (parent_iter.isValid()) @@ -233,9 +235,9 @@ public: const Children& getAllChildren() const override { return m_children; } - void setParent(ComponentIndex child, Entity parent) override + void setParent(ComponentHandle child, Entity parent) override { - Entity child_entity = {child}; + Entity child_entity = {child.index}; Parents::iterator old_parent_iter = m_parents.find(child_entity); if (old_parent_iter.isValid()) { @@ -275,9 +277,9 @@ public: } - Entity getParent(ComponentIndex child) override + Entity getParent(ComponentHandle child) override { - Entity child_entity = {child}; + Entity child_entity = {child.index}; Parents::iterator parent_iter = m_parents.find(child_entity); if (parent_iter.isValid()) { @@ -307,11 +309,12 @@ public: serializer.read(size); for (int i = 0; i < size; ++i) { - int32 child, parent; + Entity child, parent; serializer.read(child); serializer.read(parent); - setParent({child}, {parent}); - m_universe.addComponent({child}, HIERARCHY_TYPE_HANDLE, this, child); + ComponentHandle cmp = {child.index}; + setParent(cmp, parent); + m_universe.addComponent(child, HIERARCHY_TYPE_HANDLE, this, cmp); } } diff --git a/src/engine/universe/hierarchy.h b/src/engine/universe/hierarchy.h index e3daddc48..b46f7f439 100644 --- a/src/engine/universe/hierarchy.h +++ b/src/engine/universe/hierarchy.h @@ -52,12 +52,12 @@ namespace Lumix virtual ~Hierarchy() {} - virtual void setLocalPosition(ComponentIndex cmp, const Vec3& position) = 0; - virtual Vec3 getLocalPosition(ComponentIndex cmp) = 0; + virtual void setLocalPosition(ComponentHandle cmp, const Vec3& position) = 0; + virtual Vec3 getLocalPosition(ComponentHandle cmp) = 0; virtual void setLocalRotation(Entity entity, const Quat& rotation) = 0; - virtual Quat getLocalRotation(ComponentIndex cmp) = 0; - virtual void setParent(ComponentIndex cmp, Entity parent) = 0; - virtual Entity getParent(ComponentIndex cmp) = 0; + virtual Quat getLocalRotation(ComponentHandle cmp) = 0; + virtual void setParent(ComponentHandle cmp, Entity parent) = 0; + virtual Entity getParent(ComponentHandle cmp) = 0; virtual Array* getChildren(Entity parent) = 0; virtual const Children& getAllChildren() const = 0; }; diff --git a/src/engine/universe/universe.cpp b/src/engine/universe/universe.cpp index 94b623e8c..31647f98f 100644 --- a/src/engine/universe/universe.cpp +++ b/src/engine/universe/universe.cpp @@ -378,7 +378,7 @@ float Universe::getScale(Entity entity) } -void Universe::destroyComponent(Entity entity, ComponentType component_type, IScene* scene, ComponentIndex index) +void Universe::destroyComponent(Entity entity, ComponentType component_type, IScene* scene, ComponentHandle index) { auto mask = m_components[m_entity_map[entity.index]]; auto old_mask = mask; @@ -390,7 +390,7 @@ void Universe::destroyComponent(Entity entity, ComponentType component_type, ISc } -void Universe::addComponent(Entity entity, ComponentType component_type, IScene* scene, ComponentIndex index) +void Universe::addComponent(Entity entity, ComponentType component_type, IScene* scene, ComponentHandle index) { ComponentUID cmp(entity, component_type, scene, index); m_components[m_entity_map[entity.index]] |= (uint64)1 << component_type.index; diff --git a/src/engine/universe/universe.h b/src/engine/universe/universe.h index 02d3d58ff..cf8bfcb50 100644 --- a/src/engine/universe/universe.h +++ b/src/engine/universe/universe.h @@ -32,8 +32,8 @@ public: void createEntity(Entity entity); Entity createEntity(const Vec3& position, const Quat& rotation); void destroyEntity(Entity entity); - void addComponent(Entity entity, ComponentType component_type, IScene* scene, ComponentIndex index); - void destroyComponent(Entity entity, ComponentType component_type, IScene* scene, ComponentIndex index); + void addComponent(Entity entity, ComponentType component_type, IScene* scene, ComponentHandle index); + void destroyComponent(Entity entity, ComponentType component_type, IScene* scene, ComponentHandle index); int getEntityCount() const { return m_transformations.size(); } int getDenseIdx(Entity entity); diff --git a/src/lua_script/editor/plugins.cpp b/src/lua_script/editor/plugins.cpp index 2d2580a9c..167a8c410 100644 --- a/src/lua_script/editor/plugins.cpp +++ b/src/lua_script/editor/plugins.cpp @@ -147,7 +147,10 @@ struct PropertyGridPlugin : public PropertyGrid::IPlugin void serialize(JsonSerializer& serializer) override { serializer.serialize("component", cmp); } - void deserialize(JsonSerializer& serializer) override { serializer.deserialize("component", cmp, 0); } + void deserialize(JsonSerializer& serializer) override + { + serializer.deserialize("component", cmp, INVALID_COMPONENT); + } uint32 getType() override @@ -161,7 +164,7 @@ struct PropertyGridPlugin : public PropertyGrid::IPlugin LuaScriptScene* scene; - ComponentIndex cmp; + ComponentHandle cmp; int scr_index; }; @@ -211,7 +214,7 @@ struct PropertyGridPlugin : public PropertyGrid::IPlugin void deserialize(JsonSerializer& serializer) override { - serializer.deserialize("component", cmp, 0); + serializer.deserialize("component", cmp, INVALID_COMPONENT); serializer.deserialize("scr_index", scr_index, 0); } @@ -227,7 +230,7 @@ struct PropertyGridPlugin : public PropertyGrid::IPlugin OutputBlob blob; LuaScriptScene* scene; - ComponentIndex cmp; + ComponentHandle cmp; int scr_index; }; @@ -244,7 +247,7 @@ struct PropertyGridPlugin : public PropertyGrid::IPlugin SetPropertyCommand(LuaScriptScene* scene, - ComponentIndex cmp, + ComponentHandle cmp, int scr_index, const char* property_name, const char* val, @@ -311,7 +314,7 @@ struct PropertyGridPlugin : public PropertyGrid::IPlugin void deserialize(JsonSerializer& serializer) override { - serializer.deserialize("component", component, 0); + serializer.deserialize("component", component, INVALID_COMPONENT); serializer.deserialize("script_index", script_index, 0); char buf[256]; serializer.deserialize("property_name", buf, lengthOf(buf), ""); @@ -346,7 +349,7 @@ struct PropertyGridPlugin : public PropertyGrid::IPlugin string property_name; string value; string old_value; - ComponentIndex component; + ComponentHandle component; int script_index; }; @@ -389,14 +392,14 @@ struct PropertyGridPlugin : public PropertyGrid::IPlugin { auto* cmd = LUMIX_NEW(allocator, AddScriptCommand); cmd->scene = scene; - cmd->cmp = cmp.index; + cmd->cmp = cmp.handle; editor.executeCommand(cmd); } - for (int j = 0; j < scene->getScriptCount(cmp.index); ++j) + for (int j = 0; j < scene->getScriptCount(cmp.handle); ++j) { char buf[MAX_PATH_LENGTH]; - copyString(buf, scene->getScriptPath(cmp.index, j).c_str()); + copyString(buf, scene->getScriptPath(cmp.handle, j).c_str()); StaticString header; PathUtils::getBasename(header.data, lengthOf(header.data), buf); if (header.data[0] == 0) header << j; @@ -407,7 +410,7 @@ struct PropertyGridPlugin : public PropertyGrid::IPlugin if (ImGui::Button("Remove script")) { auto* cmd = LUMIX_NEW(allocator, RemoveScriptCommand)(allocator); - cmd->cmp = cmp.index; + cmd->cmp = cmp.handle; cmd->scr_index = j; cmd->scene = scene; editor.executeCommand(cmd); @@ -417,16 +420,16 @@ struct PropertyGridPlugin : public PropertyGrid::IPlugin if (m_app.getAssetBrowser()->resourceInput("Source", "src", buf, lengthOf(buf), LUA_SCRIPT_HASH)) { auto* cmd = - LUMIX_NEW(allocator, SetPropertyCommand)(scene, cmp.index, j, "-source", buf, allocator); + LUMIX_NEW(allocator, SetPropertyCommand)(scene, cmp.handle, j, "-source", buf, allocator); editor.executeCommand(cmd); } - for (int k = 0, kc = scene->getPropertyCount(cmp.index, j); k < kc; ++k) + for (int k = 0, kc = scene->getPropertyCount(cmp.handle, j); k < kc; ++k) { char buf[256]; - const char* property_name = scene->getPropertyName(cmp.index, j, k); + const char* property_name = scene->getPropertyName(cmp.handle, j, k); if (!property_name) continue; - scene->getPropertyValue(cmp.index, j, property_name, buf, lengthOf(buf)); - switch (scene->getPropertyType(cmp.index, j, k)) + scene->getPropertyValue(cmp.handle, j, property_name, buf, lengthOf(buf)); + switch (scene->getPropertyType(cmp.handle, j, k)) { case LuaScriptScene::Property::BOOLEAN: { @@ -434,7 +437,7 @@ struct PropertyGridPlugin : public PropertyGrid::IPlugin if (ImGui::Checkbox(property_name, &b)) { auto* cmd = LUMIX_NEW(allocator, SetPropertyCommand)( - scene, cmp.index, j, property_name, b ? "true" : "false", allocator); + scene, cmp.handle, j, property_name, b ? "true" : "false", allocator); editor.executeCommand(cmd); } } @@ -446,7 +449,7 @@ struct PropertyGridPlugin : public PropertyGrid::IPlugin { Lumix::toCString(f, buf, sizeof(buf), 5); auto* cmd = LUMIX_NEW(allocator, SetPropertyCommand)( - scene, cmp.index, j, property_name, buf, allocator); + scene, cmp.handle, j, property_name, buf, allocator); editor.executeCommand(cmd); } } @@ -455,11 +458,11 @@ struct PropertyGridPlugin : public PropertyGrid::IPlugin { Lumix::Entity e; Lumix::fromCString(buf, sizeof(buf), &e.index); - if (grid.entityInput(property_name, StaticString<50>(property_name, cmp.index), e)) + if (grid.entityInput(property_name, StaticString<50>(property_name, cmp.handle.index), e)) { Lumix::toCString(e.index, buf, sizeof(buf)); auto* cmd = LUMIX_NEW(allocator, SetPropertyCommand)( - scene, cmp.index, j, property_name, buf, allocator); + scene, cmp.handle, j, property_name, buf, allocator); editor.executeCommand(cmd); } } @@ -468,13 +471,13 @@ struct PropertyGridPlugin : public PropertyGrid::IPlugin if (ImGui::InputText(property_name, buf, sizeof(buf))) { auto* cmd = LUMIX_NEW(allocator, SetPropertyCommand)( - scene, cmp.index, j, property_name, buf, allocator); + scene, cmp.handle, j, property_name, buf, allocator); editor.executeCommand(cmd); } break; } } - if (auto* call = scene->beginFunctionCall(cmp.index, j, "onGUI")) + if (auto* call = scene->beginFunctionCall(cmp.handle, j, "onGUI")) { scene->endFunctionCall(*call); } @@ -652,7 +655,7 @@ struct AddComponentPlugin : public StudioApp::IAddComponentPlugin cmd->scene = static_cast(editor.getUniverse()->getScene(LUA_SCRIPT_HASH)); Entity entity = editor.getSelectedEntities()[0]; - cmd->cmp = editor.getComponent(entity, LUA_SCRIPT_TYPE).index; + cmd->cmp = editor.getComponent(entity, LUA_SCRIPT_TYPE).handle; editor.executeCommand(cmd); auto* set_source_cmd = LUMIX_NEW(allocator, PropertyGridPlugin::SetPropertyCommand)( diff --git a/src/lua_script/lua_script_system.cpp b/src/lua_script/lua_script_system.cpp index 7cf595bfc..b6383ac5d 100644 --- a/src/lua_script/lua_script_system.cpp +++ b/src/lua_script/lua_script_system.cpp @@ -271,7 +271,7 @@ namespace Lumix int parameter_count; lua_State* state; bool is_in_progress; - ComponentIndex cmp; + ComponentHandle cmp; int scr_index; }; @@ -294,7 +294,7 @@ namespace Lumix } - ComponentIndex getComponent(Entity entity) override + ComponentHandle getComponent(Entity entity) override { auto iter = m_entity_script_map.find(entity); if (!iter.isValid()) return INVALID_COMPONENT; @@ -303,11 +303,11 @@ namespace Lumix } - IFunctionCall* beginFunctionCall(ComponentIndex cmp, int scr_index, const char* function) override + IFunctionCall* beginFunctionCall(ComponentHandle cmp, int scr_index, const char* function) override { ASSERT(!m_function_call.is_in_progress); - auto& script = m_scripts[cmp]->m_scripts[scr_index]; + auto& script = m_scripts[cmp.index]->m_scripts[scr_index]; if (!script.m_state) return nullptr; bool is_env_valid = lua_rawgeti(script.m_state, LUA_REGISTRYINDEX, script.m_environment) == LUA_TTABLE; @@ -335,7 +335,7 @@ namespace Lumix m_function_call.is_in_progress = false; - auto& script = m_scripts[m_function_call.cmp]->m_scripts[m_function_call.scr_index]; + auto& script = m_scripts[m_function_call.cmp.index]->m_scripts[m_function_call.scr_index]; if (!script.m_state) return; if (lua_pcall(script.m_state, m_function_call.parameter_count, 0, 0) != LUA_OK) @@ -347,21 +347,21 @@ namespace Lumix } - int getPropertyCount(ComponentIndex cmp, int scr_index) override + int getPropertyCount(ComponentHandle cmp, int scr_index) override { - return m_scripts[cmp]->m_scripts[scr_index].m_properties.size(); + return m_scripts[cmp.index]->m_scripts[scr_index].m_properties.size(); } - const char* getPropertyName(ComponentIndex cmp, int scr_index, int prop_index) override + const char* getPropertyName(ComponentHandle cmp, int scr_index, int prop_index) override { - return getPropertyName(m_scripts[cmp]->m_scripts[scr_index].m_properties[prop_index].name_hash); + return getPropertyName(m_scripts[cmp.index]->m_scripts[scr_index].m_properties[prop_index].name_hash); } - Property::Type getPropertyType(ComponentIndex cmp, int scr_index, int prop_index) override + Property::Type getPropertyType(ComponentHandle cmp, int scr_index, int prop_index) override { - return m_scripts[cmp]->m_scripts[scr_index].m_properties[prop_index].type; + return m_scripts[cmp.index]->m_scripts[scr_index].m_properties[prop_index].type; } @@ -389,9 +389,9 @@ namespace Lumix } - lua_State* getState(ComponentIndex cmp, int scr_index) override + lua_State* getState(ComponentHandle cmp, int scr_index) override { - return m_scripts[cmp]->m_scripts[scr_index].m_state; + return m_scripts[cmp.index]->m_scripts[scr_index].m_state; } @@ -438,7 +438,7 @@ namespace Lumix Entity entity = LuaWrapper::checkArg(L, 2); int scr_index = LuaWrapper::checkArg(L, 3); - ComponentIndex cmp = scene->getComponent(entity); + ComponentHandle cmp = scene->getComponent(entity); if (cmp == INVALID_COMPONENT) { lua_pushnil(L); @@ -466,7 +466,7 @@ namespace Lumix ComponentType type = { LuaWrapper::toType(L, lua_upvalueindex(2)) }; ComponentUID cmp; cmp.scene = LuaWrapper::checkArg(L, 1); - cmp.index = LuaWrapper::checkArg(L, 2); + cmp.handle = LuaWrapper::checkArg(L, 2); cmp.type = type; cmp.entity = INVALID_ENTITY; switch (desc->getType()) @@ -542,7 +542,7 @@ namespace Lumix ComponentType type = { LuaWrapper::toType(L, lua_upvalueindex(2)) }; ComponentUID cmp; cmp.scene = LuaWrapper::checkArg(L, 1); - cmp.index = LuaWrapper::checkArg(L, 2); + cmp.handle = LuaWrapper::checkArg(L, 2); cmp.type = type; cmp.entity = INVALID_ENTITY; switch(desc->getType()) @@ -711,7 +711,7 @@ namespace Lumix } - void setScriptSource(ComponentIndex cmp, int scr_index, const char* path) + void setScriptSource(ComponentHandle cmp, int scr_index, const char* path) { setScriptPath(cmp, scr_index, Lumix::Path(path)); } @@ -751,9 +751,9 @@ namespace Lumix } - int getEnvironment(ComponentIndex cmp, int scr_index) override + int getEnvironment(ComponentHandle cmp, int scr_index) override { - return m_scripts[cmp]->m_scripts[scr_index].m_environment; + return m_scripts[cmp.index]->m_scripts[scr_index].m_environment; } @@ -802,30 +802,30 @@ namespace Lumix } - void setPropertyValue(Lumix::ComponentIndex cmp, + void setPropertyValue(Lumix::ComponentHandle cmp, int scr_index, const char* name, const char* value) override { - if (!m_scripts[cmp]) return; - if(!m_scripts[cmp]->m_scripts[scr_index].m_state) return; + if (!m_scripts[cmp.index]) return; + if(!m_scripts[cmp.index]->m_scripts[scr_index].m_state) return; Property& prop = getScriptProperty(cmp, scr_index, name); - applyProperty(m_scripts[cmp]->m_scripts[scr_index], prop, value); + applyProperty(m_scripts[cmp.index]->m_scripts[scr_index], prop, value); } - const char* getPropertyName(Lumix::ComponentIndex cmp, int scr_index, int index) const + const char* getPropertyName(Lumix::ComponentHandle cmp, int scr_index, int index) const { - auto& script = m_scripts[cmp]->m_scripts[scr_index]; + auto& script = m_scripts[cmp.index]->m_scripts[scr_index]; return getPropertyName(script.m_properties[index].name_hash); } - int getPropertyCount(Lumix::ComponentIndex cmp, int scr_index) const + int getPropertyCount(Lumix::ComponentHandle cmp, int scr_index) const { - auto& script = m_scripts[cmp]->m_scripts[scr_index]; + auto& script = m_scripts[cmp.index]->m_scripts[scr_index]; return script.m_properties.size(); } @@ -970,25 +970,25 @@ namespace Lumix } - ComponentIndex createComponent(ComponentType type, Entity entity) override + ComponentHandle createComponent(ComponentType type, Entity entity) override { if (type != LUA_SCRIPT_TYPE) return INVALID_COMPONENT; auto& allocator = m_system.getAllocator(); ScriptComponent& script = *LUMIX_NEW(allocator, ScriptComponent)(*this, allocator); - ComponentIndex cmp = INVALID_COMPONENT; + ComponentHandle cmp = INVALID_COMPONENT; for (int i = 0; i < m_scripts.size(); ++i) { if (m_scripts[i] == nullptr) { - cmp = i; + cmp = {i}; m_scripts[i] = &script; break; } } - if (cmp == INVALID_COMPONENT) + if (!isValid(cmp)) { - cmp = m_scripts.size(); + cmp = {m_scripts.size()}; m_scripts.push(&script); } m_entity_script_map.insert(entity, cmp); @@ -999,29 +999,29 @@ namespace Lumix } - void destroyComponent(ComponentIndex component, ComponentType type) override + void destroyComponent(ComponentHandle component, ComponentType type) override { if (type != LUA_SCRIPT_TYPE) return; - for (auto& scr : m_scripts[component]->m_scripts) + auto* script = m_scripts[component.index]; + for (auto& scr : script->m_scripts) { - if (scr.m_state) destroyInstance(*m_scripts[component], scr); + if (scr.m_state) destroyInstance(*script, scr); if (scr.m_script) { auto& cb = scr.m_script->getObserverCb(); - cb.unbind(m_scripts[component]); + cb.unbind(script); m_system.getScriptManager().unload(*scr.m_script); } } - m_entity_script_map.erase(m_scripts[component]->m_entity); - auto* script = m_scripts[component]; - m_scripts[component] = nullptr; + m_entity_script_map.erase(script->m_entity); + m_scripts[component.index] = nullptr; m_universe.destroyComponent(script->m_entity, type, this, component); LUMIX_DELETE(m_system.getAllocator(), script); } - void getPropertyValue(ComponentIndex cmp, + void getPropertyValue(ComponentHandle cmp, int scr_index, const char* property_name, char* out, @@ -1030,7 +1030,7 @@ namespace Lumix ASSERT(max_size > 0); uint32 hash = crc32(property_name); - auto& inst = m_scripts[cmp]->m_scripts[scr_index]; + auto& inst = m_scripts[cmp.index]->m_scripts[scr_index]; if (inst.m_script->isReady()) { for (auto& prop : inst.m_properties) @@ -1148,7 +1148,7 @@ namespace Lumix int scr_count; serializer.read(m_scripts[i]->m_entity); serializer.read(scr_count); - m_entity_script_map.insert(m_scripts[i]->m_entity, i); + m_entity_script_map.insert(m_scripts[i]->m_entity, {i}); for (int j = 0; j < scr_count; ++j) { auto& scr = script.m_scripts.emplace(allocator); @@ -1171,8 +1171,7 @@ namespace Lumix } setScriptPath(*m_scripts[i], scr, Path(tmp)); } - m_universe.addComponent( - Entity(m_scripts[i]->m_entity), LUA_SCRIPT_TYPE, this, i); + m_universe.addComponent(Entity(m_scripts[i]->m_entity), LUA_SCRIPT_TYPE, this, {i}); } } @@ -1195,7 +1194,7 @@ namespace Lumix ScriptComponent& script = *LUMIX_NEW(m_system.getAllocator(), ScriptComponent)(*this, m_system.getAllocator()); m_scripts.push(&script); serializer.read(m_scripts[i]->m_entity); - m_entity_script_map.insert(m_scripts[i]->m_entity, i); + m_entity_script_map.insert(m_scripts[i]->m_entity, {i}); char tmp[MAX_PATH_LENGTH]; serializer.readString(tmp, MAX_PATH_LENGTH); auto& scr = script.m_scripts.emplace(m_system.m_allocator); @@ -1215,7 +1214,7 @@ namespace Lumix prop.stored_value = tmp; } setScriptPath(script, scr, Path(tmp)); - m_universe.addComponent(m_scripts[i]->m_entity, LUA_SCRIPT_TYPE, this, i); + m_universe.addComponent(m_scripts[i]->m_entity, LUA_SCRIPT_TYPE, this, {i}); } } @@ -1285,7 +1284,7 @@ namespace Lumix } - ComponentIndex getComponent(Entity entity, ComponentType type) override + ComponentHandle getComponent(Entity entity, ComponentType type) override { ASSERT(ownComponentType(type)); auto iter = m_entity_script_map.find(entity); @@ -1300,10 +1299,10 @@ namespace Lumix } - Property& getScriptProperty(ComponentIndex cmp, int scr_index, const char* name) + Property& getScriptProperty(ComponentHandle cmp, int scr_index, const char* name) { uint32 name_hash = crc32(name); - for (auto& prop : m_scripts[cmp]->m_scripts[scr_index].m_properties) + for (auto& prop : m_scripts[cmp.index]->m_scripts[scr_index].m_properties) { if (prop.name_hash == name_hash) { @@ -1311,56 +1310,58 @@ namespace Lumix } } - m_scripts[cmp]->m_scripts[scr_index].m_properties.emplace(m_system.getAllocator()); - auto& prop = m_scripts[cmp]->m_scripts[scr_index].m_properties.back(); + m_scripts[cmp.index]->m_scripts[scr_index].m_properties.emplace(m_system.getAllocator()); + auto& prop = m_scripts[cmp.index]->m_scripts[scr_index].m_properties.back(); prop.name_hash = name_hash; return prop; } - Path getScriptPath(ComponentIndex cmp, int scr_index) override + Path getScriptPath(ComponentHandle cmp, int scr_index) override { - return m_scripts[cmp]->m_scripts[scr_index].m_script ? m_scripts[cmp]->m_scripts[scr_index].m_script->getPath() : Path(""); + auto& tmp = m_scripts[cmp.index]->m_scripts[scr_index]; + return tmp.m_script ? tmp.m_script->getPath() : Path(""); } - void setScriptPath(ComponentIndex cmp, int scr_index, const Path& path) override + void setScriptPath(ComponentHandle cmp, int scr_index, const Path& path) override { - if (!m_scripts[cmp]) return; - if (m_scripts[cmp]->m_scripts.size() <= scr_index) return; - setScriptPath(*m_scripts[cmp], m_scripts[cmp]->m_scripts[scr_index], path); + auto* tmp = m_scripts[cmp.index]; + if (!tmp) return; + if (tmp->m_scripts.size() <= scr_index) return; + setScriptPath(*tmp, tmp->m_scripts[scr_index], path); } - int getScriptCount(ComponentIndex cmp) override + int getScriptCount(ComponentHandle cmp) override { - return m_scripts[cmp]->m_scripts.size(); + return m_scripts[cmp.index]->m_scripts.size(); } - void insertScript(ComponentIndex cmp, int idx) override + void insertScript(ComponentHandle cmp, int idx) override { - m_scripts[cmp]->m_scripts.emplaceAt(idx, m_system.m_allocator); + m_scripts[cmp.index]->m_scripts.emplaceAt(idx, m_system.m_allocator); } - int addScript(ComponentIndex cmp) override + int addScript(ComponentHandle cmp) override { - m_scripts[cmp]->m_scripts.emplace(m_system.m_allocator); - return m_scripts[cmp]->m_scripts.size() - 1; + m_scripts[cmp.index]->m_scripts.emplace(m_system.m_allocator); + return m_scripts[cmp.index]->m_scripts.size() - 1; } - void removeScript(ComponentIndex cmp, int scr_index) override + void removeScript(ComponentHandle cmp, int scr_index) override { setScriptPath(cmp, scr_index, Path()); - m_scripts[cmp]->m_scripts.eraseFast(scr_index); + m_scripts[cmp.index]->m_scripts.eraseFast(scr_index); } - void serializeScript(ComponentIndex cmp, int scr_index, OutputBlob& blob) override + void serializeScript(ComponentHandle cmp, int scr_index, OutputBlob& blob) override { - auto& scr = m_scripts[cmp]->m_scripts[scr_index]; + auto& scr = m_scripts[cmp.index]->m_scripts[scr_index]; blob.writeString(scr.m_script ? scr.m_script->getPath().c_str() : ""); blob.write(scr.m_properties.size()); for (auto prop : scr.m_properties) @@ -1381,9 +1382,9 @@ namespace Lumix } - void deserializeScript(ComponentIndex cmp, int scr_index, InputBlob& blob) override + void deserializeScript(ComponentHandle cmp, int scr_index, InputBlob& blob) override { - auto& scr = m_scripts[cmp]->m_scripts[scr_index]; + auto& scr = m_scripts[cmp.index]->m_scripts[scr_index]; int count; char path[MAX_PATH_LENGTH]; blob.readString(path, lengthOf(path)); @@ -1405,7 +1406,7 @@ namespace Lumix LuaScriptSystemImpl& m_system; Array m_scripts; - HashMap m_entity_script_map; + HashMap m_entity_script_map; AssociativeArray m_property_names; Universe& m_universe; Array m_updates; diff --git a/src/lua_script/lua_script_system.h b/src/lua_script/lua_script_system.h index cf4803977..202e7cab1 100644 --- a/src/lua_script/lua_script_system.h +++ b/src/lua_script/lua_script_system.h @@ -53,24 +53,24 @@ public: typedef int (*lua_CFunction) (lua_State *L); public: - virtual Path getScriptPath(ComponentIndex cmp, int scr_index) = 0; - virtual void setScriptPath(ComponentIndex cmp, int scr_index, const Path& path) = 0; - virtual ComponentIndex getComponent(Entity entity) = 0; - virtual int getEnvironment(ComponentIndex cmp, int scr_index) = 0; - virtual IFunctionCall* beginFunctionCall(ComponentIndex cmp, int scr_index, const char* function) = 0; + virtual Path getScriptPath(ComponentHandle cmp, int scr_index) = 0; + virtual void setScriptPath(ComponentHandle cmp, int scr_index, const Path& path) = 0; + virtual ComponentHandle getComponent(Entity entity) = 0; + virtual int getEnvironment(ComponentHandle cmp, int scr_index) = 0; + virtual IFunctionCall* beginFunctionCall(ComponentHandle cmp, int scr_index, const char* function) = 0; virtual void endFunctionCall(IFunctionCall& caller) = 0; - virtual int getScriptCount(ComponentIndex cmp) = 0; - virtual lua_State* getState(ComponentIndex cmp, int scr_index) = 0; - virtual void insertScript(ComponentIndex cmp, int idx) = 0; - virtual int addScript(ComponentIndex cmp) = 0; - virtual void removeScript(ComponentIndex cmp, int scr_index) = 0; - virtual void serializeScript(ComponentIndex cmp, int scr_index, OutputBlob& blob) = 0; - virtual void deserializeScript(ComponentIndex cmp, int scr_index, InputBlob& blob) = 0; - virtual void setPropertyValue(Lumix::ComponentIndex cmp, int scr_index, const char* name, const char* value) = 0; - virtual void getPropertyValue(ComponentIndex cmp, int scr_index, const char* property_name, char* out, int max_size) = 0; - virtual int getPropertyCount(ComponentIndex cmp, int scr_index) = 0; - virtual const char* getPropertyName(ComponentIndex cmp, int scr_index, int prop_index) = 0; - virtual Property::Type getPropertyType(ComponentIndex cmp, int scr_index, int prop_index) = 0; + virtual int getScriptCount(ComponentHandle cmp) = 0; + virtual lua_State* getState(ComponentHandle cmp, int scr_index) = 0; + virtual void insertScript(ComponentHandle cmp, int idx) = 0; + virtual int addScript(ComponentHandle cmp) = 0; + virtual void removeScript(ComponentHandle cmp, int scr_index) = 0; + virtual void serializeScript(ComponentHandle cmp, int scr_index, OutputBlob& blob) = 0; + virtual void deserializeScript(ComponentHandle cmp, int scr_index, InputBlob& blob) = 0; + virtual void setPropertyValue(Lumix::ComponentHandle cmp, int scr_index, const char* name, const char* value) = 0; + virtual void getPropertyValue(ComponentHandle cmp, int scr_index, const char* property_name, char* out, int max_size) = 0; + virtual int getPropertyCount(ComponentHandle cmp, int scr_index) = 0; + virtual const char* getPropertyName(ComponentHandle cmp, int scr_index, int prop_index) = 0; + virtual Property::Type getPropertyType(ComponentHandle cmp, int scr_index, int prop_index) = 0; }; diff --git a/src/navigation/navigation_system.cpp b/src/navigation/navigation_system.cpp index da0a4217d..5fac4dad9 100644 --- a/src/navigation/navigation_system.cpp +++ b/src/navigation/navigation_system.cpp @@ -218,7 +218,7 @@ struct NavigationSceneImpl : public NavigationScene auto render_scene = static_cast(m_universe.getScene(crc32("renderer"))); if (!render_scene) return; - ComponentIndex cmp = render_scene->getFirstTerrain(); + ComponentHandle cmp = render_scene->getFirstTerrain(); while (cmp != INVALID_COMPONENT) { Entity entity = render_scene->getTerrainEntity(cmp); @@ -947,7 +947,7 @@ struct NavigationSceneImpl : public NavigationScene m_aabb.merge(model_bb); } - ComponentIndex cmp = render_scene->getFirstTerrain(); + ComponentHandle cmp = render_scene->getFirstTerrain(); while (cmp != INVALID_COMPONENT) { AABB terrain_aabb = render_scene->getTerrainAABB(cmp); @@ -1041,7 +1041,7 @@ struct NavigationSceneImpl : public NavigationScene } - ComponentIndex createComponent(ComponentType type, Entity entity) override + ComponentHandle createComponent(ComponentType type, Entity entity) override { if (type == NAVMESH_AGENT_TYPE) { @@ -1053,23 +1053,25 @@ struct NavigationSceneImpl : public NavigationScene agent->is_finished = true; if (m_crowd) addCrowdAgent(agent); m_agents.insert(entity, agent); - m_universe.addComponent(entity, type, this, entity.index); - return entity.index; + ComponentHandle cmp = {entity.index}; + m_universe.addComponent(entity, type, this, cmp); + return cmp; } return INVALID_COMPONENT; } - void destroyComponent(ComponentIndex component, ComponentType type) override + void destroyComponent(ComponentHandle component, ComponentType type) override { if (type == NAVMESH_AGENT_TYPE) { - auto iter = m_agents.find({component}); + Entity entity = { component.index }; + auto iter = m_agents.find(entity); Agent* agent = iter.value(); if (m_crowd && agent->agent >= 0) m_crowd->removeAgent(agent->agent); LUMIX_DELETE(m_allocator, iter.value()); m_agents.erase(iter); - m_universe.destroyComponent({component}, type, this, component); + m_universe.destroyComponent(entity, type, this, component); } else { @@ -1108,41 +1110,46 @@ struct NavigationSceneImpl : public NavigationScene serializer.read(agent->height); agent->agent = -1; m_agents.insert(entity, agent); - m_universe.addComponent(entity, NAVMESH_AGENT_TYPE, this, entity.index); + ComponentHandle cmp = { entity.index }; + m_universe.addComponent(entity, NAVMESH_AGENT_TYPE, this, cmp); } } } - void setAgentRadius(ComponentIndex cmp, float radius) + void setAgentRadius(ComponentHandle cmp, float radius) { - m_agents[{cmp}]->radius = radius; + Entity entity = {cmp.index}; + m_agents[entity]->radius = radius; } - float getAgentRadius(ComponentIndex cmp) + float getAgentRadius(ComponentHandle cmp) { - return m_agents[{cmp}]->radius; + Entity entity = { cmp.index }; + return m_agents[entity]->radius; } - void setAgentHeight(ComponentIndex cmp, float height) + void setAgentHeight(ComponentHandle cmp, float height) { - m_agents[{cmp}]->height = height; + Entity entity = { cmp.index }; + m_agents[entity]->height = height; } - float getAgentHeight(ComponentIndex cmp) + float getAgentHeight(ComponentHandle cmp) { - return m_agents[{cmp}]->height; + Entity entity = {cmp.index}; + return m_agents[entity]->height; } IPlugin& getPlugin() const override { return m_system; } bool ownComponentType(ComponentType type) const override { return type == NAVMESH_AGENT_TYPE; } - ComponentIndex getComponent(Entity entity, ComponentType type) override + ComponentHandle getComponent(Entity entity, ComponentType type) override { - if (type == NAVMESH_AGENT_TYPE) return entity.index; + if (type == NAVMESH_AGENT_TYPE) return {entity.index}; return INVALID_COMPONENT; } Universe& getUniverse() override { return m_universe; } diff --git a/src/physics/editor/plugins.cpp b/src/physics/editor/plugins.cpp index b0fbb4fca..68026e48c 100644 --- a/src/physics/editor/plugins.cpp +++ b/src/physics/editor/plugins.cpp @@ -38,8 +38,8 @@ struct EditorPlugin : public WorldEditor::Plugin if (cmp.type == CONTROLLER_TYPE) { auto* scene = static_cast(m_editor.getUniverse()->getScene(crc32("renderer"))); - float height = phy_scene->getControllerHeight(cmp.index); - float radius = phy_scene->getControllerRadius(cmp.index); + float height = phy_scene->getControllerHeight(cmp.handle); + float radius = phy_scene->getControllerRadius(cmp.handle); Universe& universe = scene->getUniverse(); Vec3 pos = universe.getPosition(cmp.entity); @@ -50,7 +50,7 @@ struct EditorPlugin : public WorldEditor::Plugin if (cmp.type == BOX_ACTOR_TYPE) { auto* scene = static_cast(m_editor.getUniverse()->getScene(crc32("renderer"))); - Vec3 extents = phy_scene->getHalfExtents(cmp.index); + Vec3 extents = phy_scene->getHalfExtents(cmp.handle); Universe& universe = scene->getUniverse(); Matrix mtx = universe.getPositionAndRotation(cmp.entity); diff --git a/src/physics/physics_scene.cpp b/src/physics/physics_scene.cpp index 13cfebeca..e3724293a 100644 --- a/src/physics/physics_scene.cpp +++ b/src/physics/physics_scene.cpp @@ -316,14 +316,14 @@ struct PhysicsSceneImpl : public PhysicsScene } - ComponentIndex getComponent(Entity entity, ComponentType type) override + ComponentHandle getComponent(Entity entity, ComponentType type) override { ASSERT(ownComponentType(type)); if (type == BOX_ACTOR_TYPE || type == MESH_ACTOR_TYPE) { for (int i = 0; i < m_actors.size(); ++i) { - if (m_actors[i] && m_actors[i]->getEntity() == entity) return i; + if (m_actors[i] && m_actors[i]->getEntity() == entity) return {i}; } return INVALID_COMPONENT; } @@ -331,7 +331,7 @@ struct PhysicsSceneImpl : public PhysicsScene { for (int i = 0; i < m_controllers.size(); ++i) { - if (!m_controllers[i].m_is_free && m_controllers[i].m_entity == entity) return i; + if (!m_controllers[i].m_is_free && m_controllers[i].m_entity == entity) return {i}; } return INVALID_COMPONENT; } @@ -339,7 +339,7 @@ struct PhysicsSceneImpl : public PhysicsScene { for (int i = 0; i < m_terrains.size(); ++i) { - if (m_terrains[i] && m_terrains[i]->m_entity == entity) return i; + if (m_terrains[i] && m_terrains[i]->m_entity == entity) return {i}; } return INVALID_COMPONENT; } @@ -350,22 +350,22 @@ struct PhysicsSceneImpl : public PhysicsScene IPlugin& getPlugin() const override { return *m_system; } - int getControllerLayer(ComponentIndex cmp) override + int getControllerLayer(ComponentHandle cmp) override { - return m_controllers[cmp].m_layer; + return m_controllers[cmp.index].m_layer; } - void setControllerLayer(ComponentIndex cmp, int layer) override + void setControllerLayer(ComponentHandle cmp, int layer) override { ASSERT(layer < lengthOf(m_layers_names)); - m_controllers[cmp].m_layer = layer; + m_controllers[cmp.index].m_layer = layer; physx::PxFilterData data; data.word0 = 1 << layer; data.word1 = m_collision_filter[layer]; physx::PxShape* shapes[8]; - int shapes_count = m_controllers[cmp].m_controller->getActor()->getShapes(shapes, lengthOf(shapes)); + int shapes_count = m_controllers[cmp.index].m_controller->getActor()->getShapes(shapes, lengthOf(shapes)); for (int i = 0; i < shapes_count; ++i) { shapes[i]->setSimulationFilterData(data); @@ -373,29 +373,30 @@ struct PhysicsSceneImpl : public PhysicsScene } - void setActorLayer(ComponentIndex cmp, int layer) override + void setActorLayer(ComponentHandle cmp, int layer) override { ASSERT(layer < lengthOf(m_layers_names)); - m_actors[cmp]->setLayer(layer); - updateFilterData(m_actors[cmp]->getPhysxActor(), m_actors[cmp]->getLayer()); + auto* actor = m_actors[cmp.index]; + actor->setLayer(layer); + updateFilterData(actor->getPhysxActor(), actor->getLayer()); } - int getActorLayer(ComponentIndex cmp) override { return m_actors[cmp]->getLayer(); } - int getHeightfieldLayer(ComponentIndex cmp) override { return m_terrains[cmp]->m_layer; } + int getActorLayer(ComponentHandle cmp) override { return m_actors[cmp.index]->getLayer(); } + int getHeightfieldLayer(ComponentHandle cmp) override { return m_terrains[cmp.index]->m_layer; } - void setHeightfieldLayer(ComponentIndex cmp, int layer) override + void setHeightfieldLayer(ComponentHandle cmp, int layer) override { ASSERT(layer < lengthOf(m_layers_names)); - m_terrains[cmp]->m_layer = layer; + m_terrains[cmp.index]->m_layer = layer; - if (m_terrains[cmp]->m_actor) + if (m_terrains[cmp.index]->m_actor) { physx::PxFilterData data; data.word0 = 1 << layer; data.word1 = m_collision_filter[layer]; physx::PxShape* shapes[8]; - int shapes_count = m_terrains[cmp]->m_actor->getShapes(shapes, lengthOf(shapes)); + int shapes_count = m_terrains[cmp.index]->m_actor->getShapes(shapes, lengthOf(shapes)); for (int i = 0; i < shapes_count; ++i) { shapes[i]->setSimulationFilterData(data); @@ -404,7 +405,7 @@ struct PhysicsSceneImpl : public PhysicsScene } - ComponentIndex createComponent(ComponentType component_type, Entity entity) override + ComponentHandle createComponent(ComponentType component_type, Entity entity) override { if (component_type == HEIGHTFIELD_TYPE) { @@ -426,27 +427,27 @@ struct PhysicsSceneImpl : public PhysicsScene } - void destroyComponent(ComponentIndex cmp, ComponentType type) override + void destroyComponent(ComponentHandle cmp, ComponentType type) override { if (type == HEIGHTFIELD_TYPE) { - Entity entity = m_terrains[cmp]->m_entity; - LUMIX_DELETE(m_allocator, m_terrains[cmp]); - m_terrains[cmp] = nullptr; + Entity entity = m_terrains[cmp.index]->m_entity; + LUMIX_DELETE(m_allocator, m_terrains[cmp.index]); + m_terrains[cmp.index] = nullptr; m_universe.destroyComponent(entity, type, this, cmp); } else if (type == CONTROLLER_TYPE) { - Entity entity = m_controllers[cmp].m_entity; - m_controllers[cmp].m_is_free = true; + Entity entity = m_controllers[cmp.index].m_entity; + m_controllers[cmp.index].m_is_free = true; m_universe.destroyComponent(entity, type, this, cmp); } else if (type == MESH_ACTOR_TYPE || type == BOX_ACTOR_TYPE) { - Entity entity = m_actors[cmp]->getEntity(); - m_actors[cmp]->setEntity(INVALID_ENTITY); - m_actors[cmp]->setPhysxActor(nullptr); - m_dynamic_actors.eraseItem(m_actors[cmp]); + Entity entity = m_actors[cmp.index]->getEntity(); + m_actors[cmp.index]->setEntity(INVALID_ENTITY); + m_actors[cmp.index]->setPhysxActor(nullptr); + m_dynamic_actors.eraseItem(m_actors[cmp.index]); m_universe.destroyComponent(entity, type, this, cmp); } else @@ -456,7 +457,7 @@ struct PhysicsSceneImpl : public PhysicsScene } - ComponentIndex createHeightfield(Entity entity) + ComponentHandle createHeightfield(Entity entity) { Heightfield* terrain = LUMIX_NEW(m_allocator, Heightfield)(); m_terrains.push(terrain); @@ -464,13 +465,13 @@ struct PhysicsSceneImpl : public PhysicsScene terrain->m_scene = this; terrain->m_actor = nullptr; terrain->m_entity = entity; - m_universe.addComponent( - entity, HEIGHTFIELD_TYPE, this, m_terrains.size() - 1); - return m_terrains.size() - 1; + ComponentHandle cmp = {m_terrains.size() - 1}; + m_universe.addComponent(entity, HEIGHTFIELD_TYPE, this, cmp); + return cmp; } - ComponentIndex createController(Entity entity) + ComponentHandle createController(Entity entity) { physx::PxCapsuleControllerDesc cDesc; cDesc.material = m_default_material; @@ -503,12 +504,13 @@ struct PhysicsSceneImpl : public PhysicsScene shapes[i]->setSimulationFilterData(data); } - m_universe.addComponent(entity, CONTROLLER_TYPE, this, m_controllers.size() - 1); - return m_controllers.size() - 1; + ComponentHandle cmp = { m_controllers.size() - 1 }; + m_universe.addComponent(entity, CONTROLLER_TYPE, this, cmp); + return cmp; } - ComponentIndex createBoxRigidActor(Entity entity) + ComponentHandle createBoxRigidActor(Entity entity) { RigidActor* actor = LUMIX_NEW(m_allocator, RigidActor)(*this); m_actors.push(actor); @@ -521,111 +523,114 @@ struct PhysicsSceneImpl : public PhysicsScene physx::PxTransform transform; Matrix mtx = m_universe.getPositionAndRotation(entity); matrix2Transform(mtx, transform); - + physx::PxRigidStatic* physx_actor = PxCreateStatic(*m_system->getPhysics(), transform, geom, *m_default_material); actor->setPhysxActor(physx_actor); - m_universe.addComponent(entity, BOX_ACTOR_TYPE, this, m_actors.size() - 1); - return m_actors.size() - 1; + ComponentHandle cmp = {m_actors.size() - 1}; + m_universe.addComponent(entity, BOX_ACTOR_TYPE, this, cmp); + return cmp; } - ComponentIndex createMeshRigidActor(Entity entity) + ComponentHandle createMeshRigidActor(Entity entity) { RigidActor* actor = LUMIX_NEW(m_allocator, RigidActor)(*this); m_actors.push(actor); actor->setEntity(entity); - m_universe.addComponent( - entity, MESH_ACTOR_TYPE, this, m_actors.size() - 1); - return m_actors.size() - 1; + ComponentHandle cmp = {m_actors.size() - 1}; + m_universe.addComponent(entity, MESH_ACTOR_TYPE, this, cmp); + return cmp; } - Path getHeightmap(ComponentIndex cmp) override + Path getHeightmap(ComponentHandle cmp) override { - return m_terrains[cmp]->m_heightmap - ? m_terrains[cmp]->m_heightmap->getPath() + return m_terrains[cmp.index]->m_heightmap + ? m_terrains[cmp.index]->m_heightmap->getPath() : Path(""); } - float getHeightmapXZScale(ComponentIndex cmp) override + float getHeightmapXZScale(ComponentHandle cmp) override { - return m_terrains[cmp]->m_xz_scale; + return m_terrains[cmp.index]->m_xz_scale; } - void setHeightmapXZScale(ComponentIndex cmp, float scale) override + void setHeightmapXZScale(ComponentHandle cmp, float scale) override { - if (scale != m_terrains[cmp]->m_xz_scale) + auto* terrain = m_terrains[cmp.index]; + if (scale != terrain->m_xz_scale) { - m_terrains[cmp]->m_xz_scale = scale; - if (m_terrains[cmp]->m_heightmap && - m_terrains[cmp]->m_heightmap->isReady()) + terrain->m_xz_scale = scale; + if (terrain->m_heightmap && terrain->m_heightmap->isReady()) { - heightmapLoaded(m_terrains[cmp]); + heightmapLoaded(terrain); } } } - float getHeightmapYScale(ComponentIndex cmp) override + float getHeightmapYScale(ComponentHandle cmp) override { - return m_terrains[cmp]->m_y_scale; + return m_terrains[cmp.index]->m_y_scale; } - void setHeightmapYScale(ComponentIndex cmp, float scale) override + void setHeightmapYScale(ComponentHandle cmp, float scale) override { - if (scale != m_terrains[cmp]->m_y_scale) + auto* terrain = m_terrains[cmp.index]; + if (scale != terrain->m_y_scale) { - m_terrains[cmp]->m_y_scale = scale; - if (m_terrains[cmp]->m_heightmap && m_terrains[cmp]->m_heightmap->isReady()) + terrain->m_y_scale = scale; + if (terrain->m_heightmap && terrain->m_heightmap->isReady()) { - heightmapLoaded(m_terrains[cmp]); + heightmapLoaded(terrain); } } } - void setHeightmap(ComponentIndex cmp, const Path& str) override + void setHeightmap(ComponentHandle cmp, const Path& str) override { auto& resource_manager = m_engine->getResourceManager(); - auto* old_hm = m_terrains[cmp]->m_heightmap; + auto* terrain = m_terrains[cmp.index]; + auto* old_hm = terrain->m_heightmap; if (old_hm) { resource_manager.get(TEXTURE_HASH)->unload(*old_hm); auto& cb = old_hm->getObserverCb(); - cb.unbind(m_terrains[cmp]); + cb.unbind(terrain); } auto* texture_manager = resource_manager.get(TEXTURE_HASH); if (str.isValid()) { auto* new_hm = static_cast(texture_manager->load(str)); - m_terrains[cmp]->m_heightmap = new_hm; - new_hm->onLoaded(m_terrains[cmp]); + terrain->m_heightmap = new_hm; + new_hm->onLoaded(terrain); new_hm->addDataReference(); } else { - m_terrains[cmp]->m_heightmap = nullptr; + terrain->m_heightmap = nullptr; } } - Path getShapeSource(ComponentIndex cmp) override + Path getShapeSource(ComponentHandle cmp) override { - return m_actors[cmp]->getResource() ? m_actors[cmp]->getResource()->getPath() : Path(""); + return m_actors[cmp.index]->getResource() ? m_actors[cmp.index]->getResource()->getPath() : Path(""); } - void setShapeSource(ComponentIndex cmp, const Path& str) override + void setShapeSource(ComponentHandle cmp, const Path& str) override { - ASSERT(m_actors[cmp]); + ASSERT(m_actors[cmp.index]); bool is_dynamic = isDynamic(cmp); - auto& actor = *m_actors[cmp]; + auto& actor = *m_actors[cmp.index]; if (actor.getResource() && actor.getResource()->getPath() == str && (!actor.getPhysxActor() || is_dynamic == !actor.getPhysxActor()->isRigidStatic())) { @@ -635,8 +640,8 @@ struct PhysicsSceneImpl : public PhysicsScene ResourceManagerBase* manager = m_engine->getResourceManager().get(PHYSICS_HASH); PhysicsGeometry* geom_res = static_cast(manager->load(str)); - m_actors[cmp]->setPhysxActor(nullptr); - m_actors[cmp]->setResource(geom_res); + actor.setPhysxActor(nullptr); + actor.setResource(geom_res); } @@ -723,7 +728,7 @@ struct PhysicsSceneImpl : public PhysicsScene { for (auto& i : m_queued_forces) { - auto* actor = m_actors[i.cmp]; + auto* actor = m_actors[i.cmp.index]; if (!actor->isDynamic()) { g_log_warning.log("Physics") << "Trying to apply force to static object"; @@ -753,56 +758,43 @@ struct PhysicsSceneImpl : public PhysicsScene } - ComponentIndex getActorComponent(Entity entity) override + ComponentHandle getActorComponent(Entity entity) override { for (int i = 0; i < m_actors.size(); ++i) { - if (m_actors[i]->getEntity() == entity) return i; + if (m_actors[i]->getEntity() == entity) return {i}; } - return -1; + return INVALID_COMPONENT; } void startGame() override - { + { auto* scene = m_universe.getScene(crc32("lua_script")); m_script_scene = static_cast(scene); - m_is_game_running = true; + m_is_game_running = true; } void stopGame() override { m_is_game_running = false; } + float getControllerRadius(ComponentHandle cmp) override { return m_controllers[cmp.index].m_radius; } + float getControllerHeight(ComponentHandle cmp) override { return m_controllers[cmp.index].m_height; } - float getControllerRadius(ComponentIndex cmp) override - { - return m_controllers[cmp].m_radius; - } - - - float getControllerHeight(ComponentIndex cmp) override - { - return m_controllers[cmp].m_height; - } - - - ComponentIndex getController(Entity entity) override + ComponentHandle getController(Entity entity) override { for (int i = 0; i < m_controllers.size(); ++i) { if (m_controllers[i].m_entity == entity) { - return i; + return {i}; } } return INVALID_COMPONENT; } - void moveController(ComponentIndex cmp, const Vec3& v) override - { - m_controllers[cmp].m_frame_change += v; - } + void moveController(ComponentHandle cmp, const Vec3& v) override { m_controllers[cmp.index].m_frame_change += v; } static int LUA_raycast(lua_State* L) @@ -832,22 +824,17 @@ struct PhysicsSceneImpl : public PhysicsScene } - bool raycastEx(const Vec3& origin, - const Vec3& dir, - float distance, - RaycastHit& result) override + bool raycastEx(const Vec3& origin, const Vec3& dir, float distance, RaycastHit& result) override { physx::PxVec3 physx_origin(origin.x, origin.y, origin.z); physx::PxVec3 unit_dir(dir.x, dir.y, dir.z); physx::PxReal max_distance = distance; physx::PxRaycastHit hit; - const physx::PxSceneQueryFlags outputFlags = physx::PxSceneQueryFlag::eDISTANCE | - physx::PxSceneQueryFlag::eIMPACT | - physx::PxSceneQueryFlag::eNORMAL; + const physx::PxSceneQueryFlags outputFlags = + physx::PxSceneQueryFlag::eDISTANCE | physx::PxSceneQueryFlag::eIMPACT | physx::PxSceneQueryFlag::eNORMAL; - bool status = - m_scene->raycastSingle(physx_origin, unit_dir, max_distance, outputFlags, hit); + bool status = m_scene->raycastSingle(physx_origin, unit_dir, max_distance, outputFlags, hit); result.normal.x = hit.normal.x; result.normal.y = hit.normal.y; result.normal.z = hit.normal.z; @@ -1151,9 +1138,9 @@ struct PhysicsSceneImpl : public PhysicsScene } - bool isDynamic(ComponentIndex cmp) override + bool isDynamic(ComponentHandle cmp) override { - RigidActor* actor = m_actors[cmp]; + RigidActor* actor = m_actors[cmp.index]; return isDynamic(actor); } @@ -1171,13 +1158,13 @@ struct PhysicsSceneImpl : public PhysicsScene } - Vec3 getHalfExtents(ComponentIndex cmp) override + Vec3 getHalfExtents(ComponentHandle cmp) override { Vec3 size; - physx::PxRigidActor* actor = m_actors[cmp]->getPhysxActor(); + physx::PxRigidActor* actor = m_actors[cmp.index]->getPhysxActor(); physx::PxShape* shapes; if (actor->getNbShapes() == 1 && - m_actors[cmp]->getPhysxActor()->getShapes(&shapes, 1)) + m_actors[cmp.index]->getPhysxActor()->getShapes(&shapes, 1)) { physx::PxVec3& half = shapes->getGeometry().box().halfExtents; size.x = half.x; @@ -1188,12 +1175,11 @@ struct PhysicsSceneImpl : public PhysicsScene } - void setHalfExtents(ComponentIndex cmp, const Vec3& size) override + void setHalfExtents(ComponentHandle cmp, const Vec3& size) override { - physx::PxRigidActor* actor = m_actors[cmp]->getPhysxActor(); + physx::PxRigidActor* actor = m_actors[cmp.index]->getPhysxActor(); physx::PxShape* shapes; - if (actor->getNbShapes() == 1 && - m_actors[cmp]->getPhysxActor()->getShapes(&shapes, 1)) + if (actor->getNbShapes() == 1 && m_actors[cmp.index]->getPhysxActor()->getShapes(&shapes, 1)) { physx::PxBoxGeometry box; bool is_box = shapes->getBoxGeometry(box); @@ -1207,54 +1193,43 @@ struct PhysicsSceneImpl : public PhysicsScene } - void setIsDynamic(ComponentIndex cmp, bool new_value) override + void setIsDynamic(ComponentHandle cmp, bool new_value) override { - RigidActor* actor = m_actors[cmp]; + RigidActor* actor = m_actors[cmp.index]; int dynamic_index = m_dynamic_actors.indexOf(actor); bool is_dynamic = dynamic_index != -1; - if (is_dynamic != new_value) + if (is_dynamic == new_value) return; + + actor->setDynamic(new_value); + if (new_value) { - m_actors[cmp]->setDynamic(new_value); + m_dynamic_actors.push(actor); + } + else + { + m_dynamic_actors.eraseItemFast(actor); + } + physx::PxShape* shapes; + if (actor->getPhysxActor()->getNbShapes() == 1 && actor->getPhysxActor()->getShapes(&shapes, 1, 0)) + { + physx::PxGeometryHolder geom = shapes->getGeometry(); + + physx::PxTransform transform; + matrix2Transform(m_universe.getPositionAndRotation(actor->getEntity()), transform); + + physx::PxRigidActor* physx_actor; if (new_value) { - m_dynamic_actors.push(actor); + physx_actor = PxCreateDynamic(*m_system->getPhysics(), transform, geom.any(), *m_default_material, 1.0f); } else { - m_dynamic_actors.eraseItemFast(actor); - } - physx::PxShape* shapes; - if (m_actors[cmp]->getPhysxActor()->getNbShapes() == 1 && - m_actors[cmp]->getPhysxActor()->getShapes(&shapes, 1, 0)) - { - physx::PxGeometryHolder geom = shapes->getGeometry(); - - physx::PxTransform transform; - matrix2Transform( - m_universe.getPositionAndRotation(m_actors[cmp]->getEntity()), - transform); - - physx::PxRigidActor* actor; - if (new_value) - { - actor = PxCreateDynamic(*m_system->getPhysics(), - transform, - geom.any(), - *m_default_material, - 1.0f); - } - else - { - actor = PxCreateStatic(*m_system->getPhysics(), - transform, - geom.any(), - *m_default_material); - } - ASSERT(actor); - actor->userData = (void*)(intptr_t)m_actors[cmp]->getEntity().index; - actor->setActorFlag(physx::PxActorFlag::eVISUALIZATION, true); - m_actors[cmp]->setPhysxActor(actor); + physx_actor = PxCreateStatic(*m_system->getPhysics(), transform, geom.any(), *m_default_material); } + ASSERT(actor); + physx_actor->userData = (void*)(intptr_t)actor->getEntity().index; + physx_actor->setActorFlag(physx::PxActorFlag::eVISUALIZATION, true); + actor->setPhysxActor(physx_actor); } } @@ -1305,11 +1280,12 @@ struct PhysicsSceneImpl : public PhysicsScene } - void deserializeActor(InputBlob& serializer, int idx, int version) + void deserializeActor(InputBlob& serializer, ComponentHandle cmp, int version) { int layer = 0; if (version > (int)PhysicsSceneVersion::LAYERS) serializer.read(layer); - m_actors[idx]->setLayer(layer); + auto* actor = m_actors[cmp.index]; + actor->setLayer(layer); ActorType type; serializer.read((int32&)type); @@ -1320,24 +1296,23 @@ struct PhysicsSceneImpl : public PhysicsScene { physx::PxBoxGeometry box_geom; physx::PxTransform transform; - Matrix mtx = m_universe.getPositionAndRotation(m_actors[idx]->getEntity()); + Matrix mtx = m_universe.getPositionAndRotation(actor->getEntity()); matrix2Transform(mtx, transform); serializer.read(box_geom.halfExtents.x); serializer.read(box_geom.halfExtents.y); serializer.read(box_geom.halfExtents.z); - physx::PxRigidActor* actor; - if (isDynamic(idx)) + physx::PxRigidActor* physx_actor; + if (isDynamic(cmp)) { - actor = PxCreateDynamic( - *m_system->getPhysics(), transform, box_geom, *m_default_material, 1.0f); + physx_actor = + PxCreateDynamic(*m_system->getPhysics(), transform, box_geom, *m_default_material, 1.0f); } else { - actor = PxCreateStatic( - *m_system->getPhysics(), transform, box_geom, *m_default_material); + physx_actor = PxCreateStatic(*m_system->getPhysics(), transform, box_geom, *m_default_material); } - m_actors[idx]->setPhysxActor(actor); - m_universe.addComponent(m_actors[idx]->getEntity(), BOX_ACTOR_TYPE, this, idx); + actor->setPhysxActor(physx_actor); + m_universe.addComponent(actor->getEntity(), BOX_ACTOR_TYPE, this, cmp); } break; case TRIMESH: @@ -1347,8 +1322,8 @@ struct PhysicsSceneImpl : public PhysicsScene serializer.readString(tmp, sizeof(tmp)); ResourceManagerBase* manager = m_engine->getResourceManager().get(PHYSICS_HASH); auto* geometry = manager->load(Lumix::Path(tmp)); - m_actors[idx]->setResource(static_cast(geometry)); - m_universe.addComponent(m_actors[idx]->getEntity(), MESH_ACTOR_TYPE, this, idx); + actor->setResource(static_cast(geometry)); + m_universe.addComponent(actor->getEntity(), MESH_ACTOR_TYPE, this, cmp); } break; default: @@ -1366,7 +1341,7 @@ struct PhysicsSceneImpl : public PhysicsScene serializer.write((int32)m_actors.size()); for (int i = 0; i < m_actors.size(); ++i) { - serializer.write(isDynamic(i)); + serializer.write(isDynamic({i})); serializer.write(m_actors[i]->getEntity()); if (isValid(m_actors[i]->getEntity())) { @@ -1437,7 +1412,7 @@ struct PhysicsSceneImpl : public PhysicsScene if (isValid(m_actors[i]->getEntity())) { - deserializeActor(serializer, i, version); + deserializeActor(serializer, {i}, version); } } } @@ -1466,32 +1441,30 @@ struct PhysicsSceneImpl : public PhysicsScene c.m_is_free = is_free; c.m_frame_change.set(0, 0, 0); - if (!is_free) + if (is_free) continue; + + if (version > (int)PhysicsSceneVersion::LAYERS) { - if (version > (int)PhysicsSceneVersion::LAYERS) - { - serializer.read(c.m_layer); - } - else - { - c.m_layer = 0; - } - physx::PxCapsuleControllerDesc cDesc; - cDesc.material = m_default_material; - cDesc.height = 1.8f; - cDesc.radius = 0.25f; - cDesc.slopeLimit = 0.0f; - cDesc.contactOffset = 0.1f; - cDesc.stepOffset = 0.02f; - cDesc.callback = nullptr; - cDesc.behaviorCallback = nullptr; - Vec3 position = m_universe.getPosition(e); - cDesc.position.set(position.x, position.y - cDesc.height * 0.5f, position.z); - c.m_controller = - m_controller_manager->createController(*m_system->getPhysics(), m_scene, cDesc); - c.m_entity = e; - m_universe.addComponent(e, CONTROLLER_TYPE, this, i); + serializer.read(c.m_layer); } + else + { + c.m_layer = 0; + } + physx::PxCapsuleControllerDesc cDesc; + cDesc.material = m_default_material; + cDesc.height = 1.8f; + cDesc.radius = 0.25f; + cDesc.slopeLimit = 0.0f; + cDesc.contactOffset = 0.1f; + cDesc.stepOffset = 0.02f; + cDesc.callback = nullptr; + cDesc.behaviorCallback = nullptr; + Vec3 position = m_universe.getPosition(e); + cDesc.position.set(position.x, position.y - cDesc.height * 0.5f, position.z); + c.m_controller = m_controller_manager->createController(*m_system->getPhysics(), m_scene, cDesc); + c.m_entity = e; + m_universe.addComponent(e, CONTROLLER_TYPE, this, {i}); } } @@ -1539,9 +1512,9 @@ struct PhysicsSceneImpl : public PhysicsScene if (m_terrains[i]->m_heightmap == nullptr || !equalStrings(tmp, m_terrains[i]->m_heightmap->getPath().c_str())) { - setHeightmap(i, Path(tmp)); + setHeightmap({i}, Path(tmp)); } - m_universe.addComponent(m_terrains[i]->m_entity, HEIGHTFIELD_TYPE, this, i); + m_universe.addComponent(m_terrains[i]->m_entity, HEIGHTFIELD_TYPE, this, {i}); } } } @@ -1570,9 +1543,9 @@ struct PhysicsSceneImpl : public PhysicsScene int getVersion() const override { return (int)PhysicsSceneVersion::LATEST; } - float getActorSpeed(ComponentIndex cmp) override + float getActorSpeed(ComponentHandle cmp) override { - auto* actor = m_actors[cmp]; + auto* actor = m_actors[cmp.index]; if (!actor->isDynamic()) { g_log_warning.log("Physics") << "Trying to get speed of static object"; @@ -1585,9 +1558,9 @@ struct PhysicsSceneImpl : public PhysicsScene } - void putToSleep(ComponentIndex cmp) override + void putToSleep(ComponentHandle cmp) override { - auto* actor = m_actors[cmp]; + auto* actor = m_actors[cmp.index]; if (!actor->isDynamic()) { g_log_warning.log("Physics") << "Trying to put static object to sleep"; @@ -1600,7 +1573,7 @@ struct PhysicsSceneImpl : public PhysicsScene } - void applyForceToActor(ComponentIndex cmp, const Vec3& force) override + void applyForceToActor(ComponentHandle cmp, const Vec3& force) override { auto& i = m_queued_forces.emplace(); i.cmp = cmp; @@ -1631,7 +1604,7 @@ struct PhysicsSceneImpl : public PhysicsScene struct QueuedForce { - ComponentIndex cmp; + ComponentHandle cmp; Vec3 force; }; diff --git a/src/physics/physics_scene.h b/src/physics/physics_scene.h index 2e899680b..bb7ddad86 100644 --- a/src/physics/physics_scene.h +++ b/src/physics/physics_scene.h @@ -50,34 +50,34 @@ public: virtual bool raycastEx(const Vec3& origin, const Vec3& dir, float distance, RaycastHit& result) = 0; virtual PhysicsSystem& getSystem() const = 0; - virtual ComponentIndex getActorComponent(Entity entity) = 0; - virtual void setActorLayer(ComponentIndex cmp, int layer) = 0; - virtual int getActorLayer(ComponentIndex cmp) = 0; - virtual bool isDynamic(ComponentIndex cmp) = 0; - virtual void setIsDynamic(ComponentIndex cmp, bool) = 0; - virtual Vec3 getHalfExtents(ComponentIndex cmp) = 0; - virtual void setHalfExtents(ComponentIndex cmp, const Vec3& size) = 0; - virtual Path getShapeSource(ComponentIndex cmp) = 0; - virtual void setShapeSource(ComponentIndex cmp, const Path& str) = 0; - virtual Path getHeightmap(ComponentIndex cmp) = 0; - virtual void setHeightmap(ComponentIndex cmp, const Path& path) = 0; - virtual float getHeightmapXZScale(ComponentIndex cmp) = 0; - virtual void setHeightmapXZScale(ComponentIndex cmp, float scale) = 0; - virtual float getHeightmapYScale(ComponentIndex cmp) = 0; - virtual void setHeightmapYScale(ComponentIndex cmp, float scale) = 0; - virtual int getHeightfieldLayer(ComponentIndex cmp) = 0; - virtual void setHeightfieldLayer(ComponentIndex cmp, int layer) = 0; + virtual ComponentHandle getActorComponent(Entity entity) = 0; + virtual void setActorLayer(ComponentHandle cmp, int layer) = 0; + virtual int getActorLayer(ComponentHandle cmp) = 0; + virtual bool isDynamic(ComponentHandle cmp) = 0; + virtual void setIsDynamic(ComponentHandle cmp, bool) = 0; + virtual Vec3 getHalfExtents(ComponentHandle cmp) = 0; + virtual void setHalfExtents(ComponentHandle cmp, const Vec3& size) = 0; + virtual Path getShapeSource(ComponentHandle cmp) = 0; + virtual void setShapeSource(ComponentHandle cmp, const Path& str) = 0; + virtual Path getHeightmap(ComponentHandle cmp) = 0; + virtual void setHeightmap(ComponentHandle cmp, const Path& path) = 0; + virtual float getHeightmapXZScale(ComponentHandle cmp) = 0; + virtual void setHeightmapXZScale(ComponentHandle cmp, float scale) = 0; + virtual float getHeightmapYScale(ComponentHandle cmp) = 0; + virtual void setHeightmapYScale(ComponentHandle cmp, float scale) = 0; + virtual int getHeightfieldLayer(ComponentHandle cmp) = 0; + virtual void setHeightfieldLayer(ComponentHandle cmp, int layer) = 0; - virtual void applyForceToActor(ComponentIndex cmp, const Vec3& force) = 0; - virtual float getActorSpeed(ComponentIndex cmp) = 0; - virtual void putToSleep(ComponentIndex cmp) = 0; + virtual void applyForceToActor(ComponentHandle cmp, const Vec3& force) = 0; + virtual float getActorSpeed(ComponentHandle cmp) = 0; + virtual void putToSleep(ComponentHandle cmp) = 0; - virtual void moveController(ComponentIndex cmp, const Vec3& v) = 0; - virtual ComponentIndex getController(Entity entity) = 0; - virtual int getControllerLayer(ComponentIndex cmp) = 0; - virtual void setControllerLayer(ComponentIndex cmp, int layer) = 0; - virtual float getControllerRadius(ComponentIndex cmp) = 0; - virtual float getControllerHeight(ComponentIndex cmp) = 0; + virtual void moveController(ComponentHandle cmp, const Vec3& v) = 0; + virtual ComponentHandle getController(Entity entity) = 0; + virtual int getControllerLayer(ComponentHandle cmp) = 0; + virtual void setControllerLayer(ComponentHandle cmp, int layer) = 0; + virtual float getControllerRadius(ComponentHandle cmp) = 0; + virtual float getControllerHeight(ComponentHandle cmp) = 0; virtual const char* getCollisionLayerName(int index) = 0; virtual void setCollisionLayerName(int index, const char* name) = 0; diff --git a/src/physics/physics_system.cpp b/src/physics/physics_system.cpp index 9d13c817d..a095370e4 100644 --- a/src/physics/physics_system.cpp +++ b/src/physics/physics_system.cpp @@ -32,10 +32,10 @@ namespace Lumix class PhysicsLayerPropertyDescriptor : public IEnumPropertyDescriptor { public: - typedef int (PhysicsScene::*Getter)(ComponentIndex); - typedef void (PhysicsScene::*Setter)(ComponentIndex, int); - typedef int (PhysicsScene::*ArrayGetter)(ComponentIndex, int); - typedef void (PhysicsScene::*ArraySetter)(ComponentIndex, int, int); + typedef int (PhysicsScene::*Getter)(ComponentHandle); + typedef void (PhysicsScene::*Setter)(ComponentHandle, int); + typedef int (PhysicsScene::*ArrayGetter)(ComponentHandle, int); + typedef void (PhysicsScene::*ArraySetter)(ComponentHandle, int, int); public: PhysicsLayerPropertyDescriptor(const char* name, @@ -70,11 +70,11 @@ namespace Lumix stream.read(&value, sizeof(value)); if (index == -1) { - (static_cast(cmp.scene)->*m_single.setter)(cmp.index, value); + (static_cast(cmp.scene)->*m_single.setter)(cmp.handle, value); } else { - (static_cast(cmp.scene)->*m_array.setter)(cmp.index, index, value); + (static_cast(cmp.scene)->*m_array.setter)(cmp.handle, index, value); } }; @@ -84,30 +84,30 @@ namespace Lumix int value; if (index == -1) { - value = (static_cast(cmp.scene)->*m_single.getter)(cmp.index); + value = (static_cast(cmp.scene)->*m_single.getter)(cmp.handle); } else { - value = (static_cast(cmp.scene)->*m_array.getter)(cmp.index, index); + value = (static_cast(cmp.scene)->*m_array.getter)(cmp.handle, index); } stream.write(&value, sizeof(value)); }; - int getEnumCount(IScene* scene, ComponentIndex) override + int getEnumCount(IScene* scene, ComponentHandle) override { return static_cast(scene)->getCollisionsLayersCount(); } - const char* getEnumItemName(IScene* scene, ComponentIndex, int index) override + const char* getEnumItemName(IScene* scene, ComponentHandle, int index) override { auto* phy_scene = static_cast(scene); return phy_scene->getCollisionLayerName(index); } - void getEnumItemName(IScene* scene, ComponentIndex, int index, char* buf, int max_size) override {} + void getEnumItemName(IScene* scene, ComponentHandle, int index, char* buf, int max_size) override {} private: union diff --git a/src/renderer/culling_system.cpp b/src/renderer/culling_system.cpp index 9b4f5572c..9e1e7cd86 100644 --- a/src/renderer/culling_system.cpp +++ b/src/renderer/culling_system.cpp @@ -13,8 +13,8 @@ namespace Lumix { typedef Array LayerMasks; -typedef Array RenderabletoSphereMap; -typedef Array SphereToRenderableMap; +typedef Array RenderabletoSphereMap; +typedef Array SphereToRenderableMap; static const int MIN_ENTITIES_PER_THREAD = 50; @@ -23,7 +23,7 @@ static void doCulling(int start_index, const Sphere* LUMIX_RESTRICT end, const Frustum* LUMIX_RESTRICT frustum, const int64* LUMIX_RESTRICT layer_masks, - const int* LUMIX_RESTRICT sphere_to_renderable_map, + const ComponentHandle* LUMIX_RESTRICT sphere_to_renderable_map, int64 layer_mask, CullingSystem::Subresults& results) { @@ -236,27 +236,27 @@ public: } - void setLayerMask(ComponentIndex renderable, int64 layer) override + void setLayerMask(ComponentHandle renderable, int64 layer) override { - m_layer_masks[m_renderable_to_sphere_map[renderable]] = layer; + m_layer_masks[m_renderable_to_sphere_map[renderable.index]] = layer; } - int64 getLayerMask(ComponentIndex renderable) override + int64 getLayerMask(ComponentHandle renderable) override { - return m_layer_masks[m_renderable_to_sphere_map[renderable]]; + return m_layer_masks[m_renderable_to_sphere_map[renderable.index]]; } - bool isAdded(ComponentIndex renderable) override + bool isAdded(ComponentHandle renderable) override { - return renderable < m_renderable_to_sphere_map.size() && m_renderable_to_sphere_map[renderable] != -1; + return renderable.index < m_renderable_to_sphere_map.size() && m_renderable_to_sphere_map[renderable.index] != -1; } - void addStatic(ComponentIndex renderable, const Sphere& sphere) override + void addStatic(ComponentHandle renderable, const Sphere& sphere) override { - if (renderable < m_renderable_to_sphere_map.size() && m_renderable_to_sphere_map[renderable] != -1) + if (renderable.index < m_renderable_to_sphere_map.size() && m_renderable_to_sphere_map[renderable.index] != -1) { ASSERT(false); return; @@ -264,22 +264,22 @@ public: m_spheres.push(sphere); m_sphere_to_renderable_map.push(renderable); - while(renderable >= m_renderable_to_sphere_map.size()) + while(renderable.index >= m_renderable_to_sphere_map.size()) { m_renderable_to_sphere_map.push(-1); } - m_renderable_to_sphere_map[renderable] = m_spheres.size() - 1; + m_renderable_to_sphere_map[renderable.index] = m_spheres.size() - 1; m_layer_masks.push(1); } - void removeStatic(ComponentIndex renderable) override + void removeStatic(ComponentHandle renderable) override { - int index = m_renderable_to_sphere_map[renderable]; + int index = m_renderable_to_sphere_map[renderable.index]; if (index < 0) return; ASSERT(index < m_spheres.size()); - m_renderable_to_sphere_map[m_sphere_to_renderable_map.back()] = index; + m_renderable_to_sphere_map[m_sphere_to_renderable_map.back().index] = index; m_spheres[index] = m_spheres.back(); m_sphere_to_renderable_map[index] = m_sphere_to_renderable_map.back(); m_layer_masks[index] = m_layer_masks.back(); @@ -287,41 +287,41 @@ public: m_spheres.pop(); m_sphere_to_renderable_map.pop(); m_layer_masks.pop(); - m_renderable_to_sphere_map[renderable] = -1; + m_renderable_to_sphere_map[renderable.index] = -1; } - void updateBoundingRadius(float radius, ComponentIndex renderable) override + void updateBoundingRadius(float radius, ComponentHandle renderable) override { - m_spheres[m_renderable_to_sphere_map[renderable]].m_radius = radius; + m_spheres[m_renderable_to_sphere_map[renderable.index]].m_radius = radius; } - void updateBoundingPosition(const Vec3& position, ComponentIndex renderable) override + void updateBoundingPosition(const Vec3& position, ComponentHandle renderable) override { - m_spheres[m_renderable_to_sphere_map[renderable]].m_position = position; + m_spheres[m_renderable_to_sphere_map[renderable.index]].m_position = position; } - void insert(const InputSpheres& spheres, const Array& renderables) override + void insert(const InputSpheres& spheres, const Array& renderables) override { for (int i = 0; i < spheres.size(); i++) { m_spheres.push(spheres[i]); - while(m_renderable_to_sphere_map.size() <= renderables[i]) + while(m_renderable_to_sphere_map.size() <= renderables[i].index) { m_renderable_to_sphere_map.push(-1); } - m_renderable_to_sphere_map[renderables[i]] = m_spheres.size() - 1; + m_renderable_to_sphere_map[renderables[i].index] = m_spheres.size() - 1; m_sphere_to_renderable_map.push(renderables[i]); m_layer_masks.push(1); } } - const Sphere& getSphere(ComponentIndex renderable) override + const Sphere& getSphere(ComponentHandle renderable) override { - return m_spheres[m_renderable_to_sphere_map[renderable]]; + return m_spheres[m_renderable_to_sphere_map[renderable.index]]; } diff --git a/src/renderer/culling_system.h b/src/renderer/culling_system.h index cb0296454..92d643d19 100644 --- a/src/renderer/culling_system.h +++ b/src/renderer/culling_system.h @@ -22,7 +22,7 @@ namespace Lumix { public: typedef Array InputSpheres; - typedef Array Subresults; + typedef Array Subresults; typedef Array Results; CullingSystem() { } @@ -37,17 +37,17 @@ namespace Lumix virtual void cullToFrustum(const Frustum& frustum, int64 layer_mask) = 0; virtual void cullToFrustumAsync(const Frustum& frustum, int64 layer_mask) = 0; - virtual bool isAdded(ComponentIndex renderable) = 0; - virtual void addStatic(ComponentIndex renderable, const Sphere& sphere) = 0; - virtual void removeStatic(ComponentIndex renderable) = 0; + virtual bool isAdded(ComponentHandle renderable) = 0; + virtual void addStatic(ComponentHandle renderable, const Sphere& sphere) = 0; + virtual void removeStatic(ComponentHandle renderable) = 0; - virtual void setLayerMask(ComponentIndex renderable, int64 layer) = 0; - virtual int64 getLayerMask(ComponentIndex renderable) = 0; + virtual void setLayerMask(ComponentHandle renderable, int64 layer) = 0; + virtual int64 getLayerMask(ComponentHandle renderable) = 0; - virtual void updateBoundingRadius(float radius, int index) = 0; - virtual void updateBoundingPosition(const Vec3& position, int index) = 0; + virtual void updateBoundingRadius(float radius, ComponentHandle renderable) = 0; + virtual void updateBoundingPosition(const Vec3& position, ComponentHandle renderable) = 0; - virtual void insert(const InputSpheres& spheres, const Array& renderables) = 0; - virtual const Sphere& getSphere(ComponentIndex renderable) = 0; + virtual void insert(const InputSpheres& spheres, const Array& renderables) = 0; + virtual const Sphere& getSphere(ComponentHandle renderable) = 0; }; } // ~namespace Lux \ No newline at end of file diff --git a/src/renderer/editor/plugins.cpp b/src/renderer/editor/plugins.cpp index f2778e6fd..0d443397e 100644 --- a/src/renderer/editor/plugins.cpp +++ b/src/renderer/editor/plugins.cpp @@ -540,9 +540,9 @@ struct ModelPlugin : public AssetBrowser::IPlugin StudioApp& m_app; Universe* m_universe; Pipeline* m_pipeline; - ComponentIndex m_mesh; + ComponentHandle m_mesh; Entity m_camera_entity; - ComponentIndex m_camera_cmp; + ComponentHandle m_camera_cmp; bool m_is_mouse_captured; }; @@ -768,7 +768,7 @@ struct EnvironmentProbePlugin : public PropertyGrid::IPlugin if (!PlatformInterface::makePath(path)) g_log_error.log("Editor") << "Failed to create " << path; path << "/probes/"; if (!PlatformInterface::makePath(path)) g_log_error.log("Editor") << "Failed to create " << path; - path << cmp.index << ".dds"; + path << cmp.handle.index << ".dds"; auto& allocator = m_app.getWorldEditor()->getAllocator(); if (!file.open(path, Lumix::FS::Mode::CREATE_AND_WRITE, allocator)) { @@ -836,11 +836,11 @@ struct EnvironmentProbePlugin : public PropertyGrid::IPlugin Vec3 probe_position = universe->getPosition(cmp.entity); auto* scene = static_cast(universe->getScene(crc32("renderer"))); - ComponentIndex original_camera = scene->getCameraInSlot("main"); + ComponentHandle original_camera = scene->getCameraInSlot("main"); if(original_camera != INVALID_COMPONENT) scene->setCameraSlot(original_camera, ""); Entity camera_entity = universe->createEntity({ 0, 0, 0 }, { 0, 0, 0, 1 }); - ComponentIndex camera_cmp = scene->createComponent(CAMERA_TYPE, camera_entity); + ComponentHandle camera_cmp = scene->createComponent(CAMERA_TYPE, camera_entity); scene->setCameraSlot(camera_cmp, "main"); scene->setCameraFOV(camera_cmp, 90); @@ -901,7 +901,7 @@ struct EnvironmentProbePlugin : public PropertyGrid::IPlugin universe->destroyEntity(camera_entity); if (original_camera != INVALID_COMPONENT) scene->setCameraSlot(original_camera, "main"); - scene->reloadEnvironmentProbe(cmp.index); + scene->reloadEnvironmentProbe(cmp.handle); } @@ -910,7 +910,7 @@ struct EnvironmentProbePlugin : public PropertyGrid::IPlugin if (cmp.type != ENVIRONMENT_PROBE_TYPE) return; auto* scene = static_cast(cmp.scene); - auto* texture = scene->getEnvironmentProbeTexture(cmp.index); + auto* texture = scene->getEnvironmentProbeTexture(cmp.handle); ImGui::LabelText("Path", "%s", texture->getPath().c_str()); if (ImGui::Button("View")) m_app.getAssetBrowser()->selectResource(texture->getPath(), true); ImGui::SameLine(); @@ -941,14 +941,14 @@ struct EmitterPlugin : public PropertyGrid::IPlugin ImGui::Checkbox("Update", &m_particle_emitter_updating); auto* scene = static_cast(cmp.scene); ImGui::SameLine(); - if (ImGui::Button("Reset")) scene->resetParticleEmitter(cmp.index); + if (ImGui::Button("Reset")) scene->resetParticleEmitter(cmp.handle); if (m_particle_emitter_updating) { ImGui::DragFloat("Timescale", &m_particle_emitter_timescale, 0.01f, 0.01f, 10000.0f); float time_delta = m_app.getWorldEditor()->getEngine().getLastTimeDelta(); - scene->updateEmitter(cmp.index, time_delta * m_particle_emitter_timescale); - scene->getParticleEmitter(cmp.index)->drawGizmo(*m_app.getWorldEditor(), *scene); + scene->updateEmitter(cmp.handle, time_delta * m_particle_emitter_timescale); + scene->getParticleEmitter(cmp.handle)->drawGizmo(*m_app.getWorldEditor(), *scene); } } @@ -1027,7 +1027,7 @@ struct SceneViewPlugin : public StudioApp::IPlugin } - WorldEditor::RayHit castRay(const Vec3& origin, const Vec3& dir, ComponentIndex ignored) override + WorldEditor::RayHit castRay(const Vec3& origin, const Vec3& dir, ComponentHandle ignored) override { auto hit = m_render_scene->castRay(origin, dir, ignored); @@ -1035,7 +1035,7 @@ struct SceneViewPlugin : public StudioApp::IPlugin } - void getRay(ComponentIndex camera_index, float x, float y, Vec3& origin, Vec3& dir) override + void getRay(ComponentHandle camera_index, float x, float y, Vec3& origin, Vec3& dir) override { m_render_scene->getRay(camera_index, x, y, origin, dir); } @@ -1083,43 +1083,43 @@ struct SceneViewPlugin : public StudioApp::IPlugin } - void setCameraSlot(ComponentIndex cmp, const char* slot) override + void setCameraSlot(ComponentHandle cmp, const char* slot) override { m_render_scene->setCameraSlot(cmp, slot); } - ComponentIndex getCameraInSlot(const char* slot) override + ComponentHandle getCameraInSlot(const char* slot) override { return m_render_scene->getCameraInSlot(slot); } - Entity getCameraEntity(ComponentIndex cmp) override + Entity getCameraEntity(ComponentHandle cmp) override { return m_render_scene->getCameraEntity(cmp); } - Vec2 getCameraScreenSize(ComponentIndex cmp) override + Vec2 getCameraScreenSize(ComponentHandle cmp) override { return m_render_scene->getCameraScreenSize(cmp); } - float getCameraOrthoSize(ComponentIndex cmp) override + float getCameraOrthoSize(ComponentHandle cmp) override { return m_render_scene->getCameraOrthoSize(cmp); } - bool isCameraOrtho(ComponentIndex cmp) override + bool isCameraOrtho(ComponentHandle cmp) override { return m_render_scene->isCameraOrtho(cmp); } - float getCameraFOV(ComponentIndex cmp) override + float getCameraFOV(ComponentHandle cmp) override { return m_render_scene->getCameraFOV(cmp); } @@ -1190,7 +1190,7 @@ struct SceneViewPlugin : public StudioApp::IPlugin void showEntity(Entity entity) override { - ComponentIndex cmp = m_render_scene->getRenderableComponent(entity); + ComponentHandle cmp = m_render_scene->getRenderableComponent(entity); if (cmp == INVALID_COMPONENT) return; m_render_scene->showRenderable(cmp); } @@ -1198,13 +1198,13 @@ struct SceneViewPlugin : public StudioApp::IPlugin void hideEntity(Entity entity) override { - ComponentIndex cmp = m_render_scene->getRenderableComponent(entity); + ComponentHandle cmp = m_render_scene->getRenderableComponent(entity); if (cmp == INVALID_COMPONENT) return; m_render_scene->hideRenderable(cmp); } - Path getRenderablePath(ComponentIndex cmp) override + Path getRenderablePath(ComponentHandle cmp) override { return m_render_scene->getRenderablePath(cmp); } @@ -1555,7 +1555,7 @@ struct WorldEditorPlugin : public WorldEditor::Plugin RenderScene* scene = static_cast(light.scene); Universe& universe = scene->getUniverse(); - float range = scene->getLightRange(light.index); + float range = scene->getLightRange(light.handle); Vec3 pos = universe.getPosition(light.entity); scene->addDebugSphere(pos, range, 0xff0000ff, 0); @@ -1582,7 +1582,7 @@ struct WorldEditorPlugin : public WorldEditor::Plugin { RenderScene* scene = static_cast(renderable.scene); Universe& universe = scene->getUniverse(); - Model* model = scene->getRenderableModel(renderable.index); + Model* model = scene->getRenderableModel(renderable.handle); Vec3 points[8]; if (!model) return; @@ -1646,19 +1646,19 @@ struct WorldEditorPlugin : public WorldEditor::Plugin Universe& universe = scene->getUniverse(); Vec3 pos = universe.getPosition(cmp.entity); - bool is_ortho = scene->isCameraOrtho(cmp.index); - float near_distance = scene->getCameraNearPlane(cmp.index); - float far_distance = scene->getCameraFarPlane(cmp.index); + bool is_ortho = scene->isCameraOrtho(cmp.handle); + float near_distance = scene->getCameraNearPlane(cmp.handle); + float far_distance = scene->getCameraFarPlane(cmp.handle); Vec3 dir = universe.getRotation(cmp.entity) * Vec3(0, 0, -1); Vec3 right = universe.getRotation(cmp.entity) * Vec3(1, 0, 0); Vec3 up = universe.getRotation(cmp.entity) * Vec3(0, 1, 0); - float w = scene->getCameraScreenWidth(cmp.index); - float h = scene->getCameraScreenHeight(cmp.index); + float w = scene->getCameraScreenWidth(cmp.handle); + float h = scene->getCameraScreenHeight(cmp.handle); float ratio = h < 1.0f ? 1 : w / h; if (is_ortho) { - float ortho_size = scene->getCameraOrthoSize(cmp.index); + float ortho_size = scene->getCameraOrthoSize(cmp.handle); Vec3 center = pos; center += (far_distance - near_distance) * dir * 0.5f; scene->addDebugCube(center, @@ -1670,7 +1670,7 @@ struct WorldEditorPlugin : public WorldEditor::Plugin } else { - float fov = scene->getCameraFOV(cmp.index); + float fov = scene->getCameraFOV(cmp.handle); scene->addDebugFrustum(pos, dir, up, fov, ratio, near_distance, far_distance, 0xffff0000, 0); } } diff --git a/src/renderer/editor/scene_view.cpp b/src/renderer/editor/scene_view.cpp index 654068a7b..29358a103 100644 --- a/src/renderer/editor/scene_view.cpp +++ b/src/renderer/editor/scene_view.cpp @@ -82,19 +82,19 @@ struct InsertMeshCommand : public Lumix::IEditorCommand m_entity = universe->createEntity({ 0, 0, 0 }, { 0, 0, 0, 1 }); universe->setPosition(m_entity, m_position); const auto& scenes = universe->getScenes(); - Lumix::ComponentIndex cmp = -1; + Lumix::ComponentHandle cmp = Lumix::INVALID_COMPONENT; Lumix::IScene* scene = nullptr; for (int i = 0; i < scenes.size(); ++i) { cmp = scenes[i]->createComponent(RENDERABLE_TYPE, m_entity); - if (cmp >= 0) + if (isValid(cmp)) { scene = scenes[i]; break; } } - if (cmp >= 0) static_cast(scene)->setRenderablePath(cmp, m_mesh_path); + if (isValid(cmp)) static_cast(scene)->setRenderablePath(cmp, m_mesh_path); return true; } @@ -280,13 +280,13 @@ Lumix::RayCastModelHit SceneView::castRay(float x, float y) ASSERT(scene); Lumix::ComponentUID camera_cmp = m_editor->getEditCamera(); - Lumix::Vec2 screen_size = scene->getCameraScreenSize(camera_cmp.index); + Lumix::Vec2 screen_size = scene->getCameraScreenSize(camera_cmp.handle); screen_size.x *= x; screen_size.y *= y; Lumix::Vec3 origin; Lumix::Vec3 dir; - scene->getRay(camera_cmp.index, (float)screen_size.x, (float)screen_size.y, origin, dir); + scene->getRay(camera_cmp.handle, (float)screen_size.x, (float)screen_size.y, origin, dir); return scene->castRay(origin, dir, Lumix::INVALID_COMPONENT); } diff --git a/src/renderer/editor/terrain_editor.cpp b/src/renderer/editor/terrain_editor.cpp index 99528a835..910227c07 100644 --- a/src/renderer/editor/terrain_editor.cpp +++ b/src/renderer/editor/terrain_editor.cpp @@ -89,12 +89,10 @@ struct PaintTerrainCommand : public Lumix::IEditorCommand } m_width = m_height = m_x = m_y = -1; - Lumix::Matrix entity_mtx = - editor.getUniverse()->getMatrix(terrain.entity); + Lumix::Matrix entity_mtx = editor.getUniverse()->getMatrix(terrain.entity); entity_mtx.fastInverse(); Lumix::Vec3 local_pos = entity_mtx.multiplyPosition(hit_pos); - float xz_scale = static_cast(terrain.scene) - ->getTerrainXZScale(terrain.index); + float xz_scale = static_cast(terrain.scene)->getTerrainXZScale(terrain.handle); local_pos = local_pos / xz_scale; local_pos.y = -1; @@ -230,13 +228,11 @@ private: private: Lumix::Material* getMaterial() { - auto* material = static_cast(m_terrain.scene) - ->getTerrainMaterial(m_terrain.index); - return static_cast( - m_world_editor.getEngine() - .getResourceManager() - .get(MATERIAL_HASH) - ->get(Lumix::Path(material->getPath().c_str()))); + auto* material = static_cast(m_terrain.scene)->getTerrainMaterial(m_terrain.handle); + return static_cast(m_world_editor.getEngine() + .getResourceManager() + .get(MATERIAL_HASH) + ->get(Lumix::Path(material->getPath().c_str()))); } @@ -525,7 +521,7 @@ private: } } texture->onDataUpdated(m_x, m_y, m_width, m_height); - static_cast(m_terrain.scene)->forceGrassUpdate(m_terrain.index); + static_cast(m_terrain.scene)->forceGrassUpdate(m_terrain.handle); } @@ -636,7 +632,7 @@ TerrainEditor::~TerrainEditor() void TerrainEditor::onUniverseDestroyed() { m_component.scene = nullptr; - m_component.index = Lumix::INVALID_COMPONENT; + m_component.handle = Lumix::INVALID_COMPONENT; } @@ -713,7 +709,7 @@ TerrainEditor::TerrainEditor(Lumix::WorldEditor& editor, StudioApp& app) void TerrainEditor::nextTerrainTexture() { auto* scene = static_cast(m_component.scene); - auto* material = scene->getTerrainMaterial(m_component.index); + auto* material = scene->getTerrainMaterial(m_component.handle); Lumix::Texture* tex = material->getTextureByUniform(TEX_COLOR_UNIFORM); if (tex) { @@ -773,11 +769,11 @@ void TerrainEditor::drawCursor(Lumix::RenderScene& scene, float angle = i * angle_step; float next_angle = i * angle_step + angle_step; Lumix::Vec3 local_from = local_center + Lumix::Vec3(cos(angle), 0, sin(angle)) * brush_size; - local_from.y = scene.getTerrainHeightAt(terrain.index, local_from.x, local_from.z); + local_from.y = scene.getTerrainHeightAt(terrain.handle, local_from.x, local_from.z); local_from.y += 0.25f; Lumix::Vec3 local_to = local_center + Lumix::Vec3(cos(next_angle), 0, sin(next_angle)) * brush_size; - local_to.y = scene.getTerrainHeightAt(terrain.index, local_to.x, local_to.z); + local_to.y = scene.getTerrainHeightAt(terrain.handle, local_to.x, local_to.z); local_to.y += 0.25f; Lumix::Vec3 from = terrain_matrix.multiplyPosition(local_from); @@ -853,7 +849,7 @@ bool TerrainEditor::onEntityMouseDown(const Lumix::WorldEditor::RayHit& hit, int { if (m_world_editor.getSelectedEntities().size() != 1) return false; auto terrain = m_world_editor.getComponent(m_world_editor.getSelectedEntities()[0], TERRAIN_TYPE); - if (terrain.index == Lumix::INVALID_COMPONENT) return false; + if (terrain.handle == Lumix::INVALID_COMPONENT) return false; if (!m_is_enabled) return false; if (m_type == NOT_SET || !m_component.isValid()) return false; @@ -1060,7 +1056,7 @@ void TerrainEditor::paintEntities(const Lumix::Vec3& hit_pos) struct Tpl { - Lumix::ComponentIndex cmp; + Lumix::ComponentHandle cmp; int template_idx; }; @@ -1073,7 +1069,7 @@ void TerrainEditor::paintEntities(const Lumix::Vec3& hit_pos) if(!isValid(tpl)) continue; Lumix::ComponentUID renderable = m_world_editor.getComponent(tpl, RENDERABLE_TYPE); if(!renderable.isValid()) continue; - tpls.push({renderable.index, idx}); + tpls.push({renderable.handle, idx}); } Lumix::Frustum frustum; @@ -1086,7 +1082,7 @@ void TerrainEditor::paintEntities(const Lumix::Vec3& hit_pos) m_terrain_brush_size); auto& meshes = scene->getRenderableInfos(frustum, frustum.position); - Lumix::Vec2 size = scene->getTerrainSize(m_component.index); + Lumix::Vec2 size = scene->getTerrainSize(m_component.handle); float scale = 1.0f - Lumix::Math::maximum(0.01f, m_terrain_brush_strength); for (int i = 0; i <= m_terrain_brush_size * m_terrain_brush_size / 1000.0f; ++i) { @@ -1101,7 +1097,7 @@ void TerrainEditor::paintEntities(const Lumix::Vec3& hit_pos) Lumix::Vec3 terrain_pos = inv_terrain_matrix.multiplyPosition(pos); if (terrain_pos.x >= 0 && terrain_pos.z >= 0 && terrain_pos.x <= size.x && terrain_pos.z <= size.y) { - pos.y = scene->getTerrainHeightAt(m_component.index, terrain_pos.x, terrain_pos.z) + y; + pos.y = scene->getTerrainHeightAt(m_component.handle, terrain_pos.x, terrain_pos.z) + y; pos.y += terrain_matrix.getTranslation().y; if(!isOBBCollision(*scene, meshes, pos, model, scale)) { @@ -1109,7 +1105,7 @@ void TerrainEditor::paintEntities(const Lumix::Vec3& hit_pos) if(m_is_align_with_normal) { Lumix::RenderScene* scene = static_cast(m_component.scene); - Lumix::Vec3 normal = scene->getTerrainNormalAt(m_component.index, pos.x, pos.z); + Lumix::Vec3 normal = scene->getTerrainNormalAt(m_component.handle, pos.x, pos.z); Lumix::Vec3 dir = Lumix::crossProduct(normal, Lumix::Vec3(1, 0, 0)).normalized(); Lumix::Matrix mtx = Lumix::Matrix::IDENTITY; mtx.setXVector(Lumix::crossProduct(normal, dir)); @@ -1160,8 +1156,8 @@ void TerrainEditor::onMouseMove(int x, int y, int, int) Lumix::ComponentUID camera_cmp = m_world_editor.getEditCamera(); Lumix::RenderScene* scene = static_cast(camera_cmp.scene); Lumix::Vec3 origin, dir; - scene->getRay(camera_cmp.index, (float)x, (float)y, origin, dir); - Lumix::RayCastModelHit hit = scene->castRayTerrain(m_component.index, origin, dir); + scene->getRay(camera_cmp.handle, (float)x, (float)y, origin, dir); + Lumix::RayCastModelHit hit = scene->castRayTerrain(m_component.handle, origin, dir); if (hit.m_is_hit) { Lumix::ComponentUID terrain = m_world_editor.getComponent(hit.m_entity, TERRAIN_TYPE); @@ -1192,7 +1188,7 @@ void TerrainEditor::onMouseUp(int, int, Lumix::MouseButton::Value) Lumix::Material* TerrainEditor::getMaterial() { auto* scene = static_cast(m_component.scene); - return scene->getTerrainMaterial(m_component.index); + return scene->getTerrainMaterial(m_component.handle); } @@ -1449,8 +1445,8 @@ void TerrainEditor::onGUI() Lumix::ComponentUID camera_cmp = m_world_editor.getEditCamera(); Lumix::RenderScene* scene = static_cast(camera_cmp.scene); Lumix::Vec3 origin, dir; - scene->getRay(camera_cmp.index, (float)mouse_x, (float)mouse_y, origin, dir); - Lumix::RayCastModelHit hit = scene->castRayTerrain(terrain.index, origin, dir); + scene->getRay(camera_cmp.handle, (float)mouse_x, (float)mouse_y, origin, dir); + Lumix::RayCastModelHit hit = scene->castRayTerrain(terrain.handle, origin, dir); if(hit.m_is_hit) { diff --git a/src/renderer/model.h b/src/renderer/model.h index 1508b21d8..e65cf118e 100644 --- a/src/renderer/model.h +++ b/src/renderer/model.h @@ -40,7 +40,7 @@ struct LUMIX_RENDERER_API RayCastModelHit Vec3 m_origin; Vec3 m_dir; Mesh* m_mesh; - ComponentIndex m_component; + ComponentHandle m_component; Entity m_entity; ComponentType m_component_type; }; diff --git a/src/renderer/pipeline.cpp b/src/renderer/pipeline.cpp index c8e77e2b0..b19dda766 100644 --- a/src/renderer/pipeline.cpp +++ b/src/renderer/pipeline.cpp @@ -701,8 +701,8 @@ struct PipelineImpl : public Pipeline void applyCamera(const char* slot) { - ComponentIndex cmp = m_scene->getCameraInSlot(slot); - if (cmp < 0) return; + ComponentHandle cmp = m_scene->getCameraInSlot(slot); + if (!isValid(cmp)) return; m_scene->setCameraScreenSize(cmp, m_width, m_height); m_applied_camera = cmp; @@ -941,11 +941,11 @@ struct PipelineImpl : public Pipeline { auto scr_scene = static_cast(m_scene->getUniverse().getScene(crc32("lua_script"))); if (!scr_scene) return false; - ComponentIndex camera = m_scene->getCameraInSlot(camera_slot); + ComponentHandle camera = m_scene->getCameraInSlot(camera_slot); if (camera == INVALID_COMPONENT) return false; Entity camera_entity = m_scene->getCameraEntity(camera); - ComponentIndex scr_cmp = scr_scene->getComponent(camera_entity); + ComponentHandle scr_cmp = scr_scene->getComponent(camera_entity); if (scr_cmp == INVALID_COMPONENT) return false; bool ret = false; @@ -1065,7 +1065,7 @@ struct PipelineImpl : public Pipeline } - void renderSpotLightShadowmap(ComponentIndex light) + void renderSpotLightShadowmap(ComponentHandle light) { newView("point_light"); @@ -1102,7 +1102,7 @@ struct PipelineImpl : public Pipeline } - void renderOmniLightShadowmap(ComponentIndex light) + void renderOmniLightShadowmap(ComponentHandle light) { Entity light_entity = m_scene->getPointLightEntity(light); Vec3 light_pos = m_scene->getUniverse().getPosition(light_entity); @@ -1186,15 +1186,15 @@ struct PipelineImpl : public Pipeline } - void renderLocalLightShadowmaps(ComponentIndex camera, FrameBuffer** fbs, int framebuffers_count) + void renderLocalLightShadowmaps(ComponentHandle camera, FrameBuffer** fbs, int framebuffers_count) { - if (camera < 0) return; + if (!isValid(camera)) return; Universe& universe = m_scene->getUniverse(); Entity camera_entity = m_scene->getCameraEntity(camera); Vec3 camera_pos = universe.getPosition(camera_entity); - ComponentIndex lights[16]; + ComponentHandle lights[16]; int light_count = m_scene->getClosestPointLights(camera_pos, lights, lengthOf(lights)); int fb_index = 0; @@ -1238,8 +1238,8 @@ struct PipelineImpl : public Pipeline void renderShadowmap(int split_index) { Universe& universe = m_scene->getUniverse(); - ComponentIndex light_cmp = m_scene->getActiveGlobalLight(); - if (light_cmp < 0 || m_applied_camera < 0) return; + ComponentHandle light_cmp = m_scene->getActiveGlobalLight(); + if (!isValid(light_cmp) || !isValid(m_applied_camera)) return; float camera_height = m_scene->getCameraScreenHeight(m_applied_camera); if (!camera_height) return; @@ -1485,9 +1485,9 @@ struct PipelineImpl : public Pipeline } - void setPointLightUniforms(ComponentIndex light_cmp) + void setPointLightUniforms(ComponentHandle light_cmp) { - if (light_cmp < 0) return; + if (!isValid(light_cmp)) return; Universe& universe = m_scene->getUniverse(); Entity light_entity = m_scene->getPointLightEntity(light_cmp); @@ -1637,7 +1637,7 @@ struct PipelineImpl : public Pipeline } - void renderPointLightInfluencedGeometry(ComponentIndex light) + void renderPointLightInfluencedGeometry(ComponentHandle light) { PROFILE_FUNCTION(); @@ -1653,7 +1653,7 @@ struct PipelineImpl : public Pipeline { PROFILE_FUNCTION(); - Array lights(m_allocator); + Array lights(m_allocator); m_scene->getPointLights(frustum, lights); for (int i = 0; i < lights.size(); ++i) { @@ -1661,7 +1661,7 @@ struct PipelineImpl : public Pipeline m_tmp_meshes.clear(); m_tmp_terrains.clear(); - ComponentIndex light = lights[i]; + ComponentHandle light = lights[i]; m_is_current_light_global = false; setPointLightUniforms(light); m_scene->getPointLightInfluencedGeometry(light, frustum, m_tmp_meshes); @@ -1788,7 +1788,7 @@ struct PipelineImpl : public Pipeline executeCommandBuffer(material->getCommandBuffer(), material); executeCommandBuffer(m_views[m_view_idx].command_buffer.buffer, material); - if (m_applied_camera >= 0) + if (isValid(m_applied_camera)) { Matrix projection_matrix; Universe& universe = m_scene->getUniverse(); @@ -1829,7 +1829,7 @@ struct PipelineImpl : public Pipeline { PROFILE_FUNCTION(); - if (m_applied_camera < 0) return; + if (!isValid(m_applied_camera)) return; m_tmp_grasses.clear(); m_tmp_terrains.clear(); @@ -2241,7 +2241,7 @@ struct PipelineImpl : public Pipeline PROFILE_INT("mesh count", meshes.size()); for(auto& mesh : meshes) { - Renderable& renderable = renderables[mesh.renderable]; + Renderable& renderable = renderables[mesh.renderable.index]; if(renderable.pose && renderable.pose->count > 0) { renderSkinnedMesh(renderable, mesh); @@ -2266,7 +2266,7 @@ struct PipelineImpl : public Pipeline mesh_count += submeshes.size(); for (auto& mesh : submeshes) { - Renderable& renderable = renderables[mesh.renderable]; + Renderable& renderable = renderables[mesh.renderable.index]; if (renderable.pose && renderable.pose->count > 0) { renderSkinnedMesh(renderable, mesh); @@ -2492,7 +2492,7 @@ struct PipelineImpl : public Pipeline struct PointLightShadowmap { - ComponentIndex m_light; + ComponentHandle m_light; FrameBuffer* m_framebuffer; Matrix m_matrices[4]; }; @@ -2529,7 +2529,7 @@ struct PipelineImpl : public Pipeline FrameBuffer* m_global_light_shadowmap; InstanceData m_instances_data[128]; int m_instance_data_idx; - ComponentIndex m_applied_camera; + ComponentHandle m_applied_camera; bgfx::VertexBufferHandle m_cube_vb; bgfx::IndexBufferHandle m_cube_ib; bool m_is_current_light_global; @@ -2552,7 +2552,7 @@ struct PipelineImpl : public Pipeline Array m_tmp_meshes; Array m_tmp_terrains; Array m_tmp_grasses; - Array m_tmp_local_lights; + Array m_tmp_local_lights; bgfx::UniformHandle m_bone_matrices_uniform; bgfx::UniformHandle m_layer_uniform; @@ -2730,7 +2730,7 @@ int renderLocalLightsShadowmaps(lua_State* L) } RenderScene* scene = pipeline->m_scene; - ComponentIndex camera = scene->getCameraInSlot(camera_slot); + ComponentHandle camera = scene->getCameraInSlot(camera_slot); pipeline->renderLocalLightShadowmaps(camera, fbs, len); return 0; diff --git a/src/renderer/render_scene.cpp b/src/renderer/render_scene.cpp index b3fae0413..138fc9173 100644 --- a/src/renderer/render_scene.cpp +++ b/src/renderer/render_scene.cpp @@ -79,7 +79,7 @@ struct PointLight float m_diffuse_intensity; float m_specular_intensity; Entity m_entity; - int m_uid; + ComponentHandle m_component; float m_fov; float m_attenuation_param; float m_range; @@ -89,7 +89,7 @@ struct PointLight struct GlobalLight { - ComponentIndex m_uid; + ComponentHandle m_component; Vec3 m_diffuse_color; float m_specular_intensity; Vec3 m_specular; @@ -199,9 +199,9 @@ public: , m_temporary_infos(m_allocator) , m_sync_point(true, m_allocator) , m_jobs(m_allocator) - , m_active_global_light_uid(-1) - , m_global_light_last_uid(-1) - , m_point_light_last_uid(-1) + , m_active_global_light_cmp(INVALID_COMPONENT) + , m_global_light_last_cmp(INVALID_COMPONENT) + , m_point_light_last_cmp(INVALID_COMPONENT) , m_is_forward_rendered(is_forward_rendered) , m_renderable_created(m_allocator) , m_renderable_destroyed(m_allocator) @@ -266,21 +266,21 @@ public: } - void resetParticleEmitter(ComponentIndex cmp) override + void resetParticleEmitter(ComponentHandle cmp) override { - m_particle_emitters[cmp]->reset(); + m_particle_emitters[cmp.index]->reset(); } - ParticleEmitter* getParticleEmitter(ComponentIndex cmp) override + ParticleEmitter* getParticleEmitter(ComponentHandle cmp) override { - return m_particle_emitters[cmp]; + return m_particle_emitters[cmp.index]; } - void updateEmitter(ComponentIndex cmp, float time_delta) override + void updateEmitter(ComponentHandle cmp, float time_delta) override { - m_particle_emitters[cmp]->update(time_delta); + m_particle_emitters[cmp.index]->update(time_delta); } @@ -290,18 +290,19 @@ public: bool ownComponentType(ComponentType type) const override; - ComponentIndex getComponent(Entity entity, ComponentType type) override + ComponentHandle getComponent(Entity entity, ComponentType type) override { if (type == RENDERABLE_TYPE) { if (entity.index >= m_renderables.size()) return INVALID_COMPONENT; - return isValid(m_renderables[entity.index].entity) ? entity.index : INVALID_COMPONENT; + ComponentHandle cmp = {entity.index}; + return isValid(m_renderables[entity.index].entity) ? cmp : INVALID_COMPONENT; } if (type == POINT_LIGHT_TYPE) { for (auto& i : m_point_lights) { - if (i.m_entity == entity) return i.m_uid; + if (i.m_entity == entity) return i.m_component; } return INVALID_COMPONENT; } @@ -309,7 +310,7 @@ public: { for (auto& i : m_global_lights) { - if (i.m_entity == entity) return i.m_uid; + if (i.m_entity == entity) return i.m_component; } return INVALID_COMPONENT; } @@ -317,7 +318,7 @@ public: { for (int i = 0; i < m_cameras.size(); ++i) { - if (!m_cameras[i].is_free && m_cameras[i].entity == entity) return i; + if (!m_cameras[i].is_free && m_cameras[i].entity == entity) return {i}; } return INVALID_COMPONENT; } @@ -325,7 +326,7 @@ public: { for (int i = 0; i < m_terrains.size(); ++i) { - if (m_terrains[i] && m_terrains[i]->getEntity() == entity) return i; + if (m_terrains[i] && m_terrains[i]->getEntity() == entity) return {i}; } return INVALID_COMPONENT; } @@ -333,7 +334,7 @@ public: { for (int i = 0; i < m_particle_emitters.size(); ++i) { - if (m_particle_emitters[i] && m_particle_emitters[i]->m_entity == entity) return i; + if (m_particle_emitters[i] && m_particle_emitters[i]->m_entity == entity) return {i}; } return INVALID_COMPONENT; } @@ -358,7 +359,7 @@ public: for (int i = 0; i < m_particle_emitters.size(); ++i) { if (!m_particle_emitters[i] || m_particle_emitters[i]->m_entity != entity) return INVALID_COMPONENT; - if (m_particle_emitters[i]->getModule(module.module_type)) return i; + if (m_particle_emitters[i]->getModule(module.module_type)) return {i}; } return INVALID_COMPONENT; } @@ -370,30 +371,30 @@ public: IPlugin& getPlugin() const override { return m_renderer; } - Int2 getParticleEmitterSpawnCount(ComponentIndex cmp) override + Int2 getParticleEmitterSpawnCount(ComponentHandle cmp) override { Int2 ret; - ret.x = m_particle_emitters[cmp]->m_spawn_count.from; - ret.y = m_particle_emitters[cmp]->m_spawn_count.to; + ret.x = m_particle_emitters[cmp.index]->m_spawn_count.from; + ret.y = m_particle_emitters[cmp.index]->m_spawn_count.to; return ret; } - void setParticleEmitterSpawnCount(ComponentIndex cmp, const Int2& value) override + void setParticleEmitterSpawnCount(ComponentHandle cmp, const Int2& value) override { - m_particle_emitters[cmp]->m_spawn_count.from = value.x; - m_particle_emitters[cmp]->m_spawn_count.to = Math::maximum(value.x, value.y); + m_particle_emitters[cmp.index]->m_spawn_count.from = value.x; + m_particle_emitters[cmp.index]->m_spawn_count.to = Math::maximum(value.x, value.y); } - void getRay(ComponentIndex camera_index, + void getRay(ComponentHandle camera_index, float x, float y, Vec3& origin, Vec3& dir) override { - Camera& camera = m_cameras[camera_index]; + Camera& camera = m_cameras[camera_index.index]; origin = m_universe.getPosition(camera.entity); float width = camera.screen_width; @@ -430,9 +431,9 @@ public: } - Frustum getCameraFrustum(ComponentIndex cmp) const override + Frustum getCameraFrustum(ComponentHandle cmp) const override { - const Camera& camera = m_cameras[cmp]; + const Camera& camera = m_cameras[cmp.index]; Matrix mtx = m_universe.getMatrix(camera.entity); Frustum ret; float ratio = camera.screen_height > 0 ? camera.screen_width / camera.screen_height : 1; @@ -459,11 +460,11 @@ public: } - int getBoneAttachmentIdx(ComponentIndex cmp) const + int getBoneAttachmentIdx(ComponentHandle cmp) const { for (int i = 0; i < m_bone_attachments.size(); ++i) { - if (m_bone_attachments[i].entity.index == cmp) return i; + if (m_bone_attachments[i].entity.index == cmp.index) return i; } return -1; } @@ -472,7 +473,7 @@ public: void updateBoneAttachment(const BoneAttachment& bone_attachment) { if (bone_attachment.parent_entity == INVALID_ENTITY) return; - ComponentIndex renderable = getRenderableComponent(bone_attachment.parent_entity); + ComponentHandle renderable = getRenderableComponent(bone_attachment.parent_entity); if (renderable == INVALID_COMPONENT) return; auto* parent_pose = getPose(renderable); if (!parent_pose) return; @@ -487,7 +488,7 @@ public: } - Entity getBoneAttachmentParent(ComponentIndex cmp) override + Entity getBoneAttachmentParent(ComponentHandle cmp) override { int idx = getBoneAttachmentIdx(cmp); return m_bone_attachments[idx].parent_entity; @@ -498,7 +499,7 @@ public: { if (attachment.parent_entity == INVALID_ENTITY) return; if (attachment.bone_index < 0) return; - ComponentIndex renderable = getRenderableComponent(attachment.parent_entity); + ComponentHandle renderable = getRenderableComponent(attachment.parent_entity); if (renderable == INVALID_COMPONENT) return; Pose* pose = getPose(renderable); if (!pose) return; @@ -515,7 +516,7 @@ public: } - Vec3 getBoneAttachmentPosition(ComponentIndex cmp) override + Vec3 getBoneAttachmentPosition(ComponentHandle cmp) override { int idx = getBoneAttachmentIdx(cmp); if (idx < 0) return {0, 0, 0}; @@ -523,7 +524,7 @@ public: } - void setBoneAttachmentPosition(ComponentIndex cmp, const Vec3& pos) override + void setBoneAttachmentPosition(ComponentHandle cmp, const Vec3& pos) override { int idx = getBoneAttachmentIdx(cmp); if (idx < 0) return; @@ -531,7 +532,7 @@ public: } - int getBoneAttachmentBone(ComponentIndex cmp) override + int getBoneAttachmentBone(ComponentHandle cmp) override { int idx = getBoneAttachmentIdx(cmp); if (idx < 0) return -1; @@ -539,7 +540,7 @@ public: } - void setBoneAttachmentBone(ComponentIndex cmp, int value) override + void setBoneAttachmentBone(ComponentHandle cmp, int value) override { int idx = getBoneAttachmentIdx(cmp); if (idx < 0) return; @@ -548,7 +549,7 @@ public: } - void setBoneAttachmentParent(ComponentIndex cmp, Entity entity) override + void setBoneAttachmentParent(ComponentHandle cmp, Entity entity) override { int idx = getBoneAttachmentIdx(cmp); m_bone_attachments[idx].parent_entity = entity; @@ -671,15 +672,15 @@ public: { serializer.write(m_point_lights[i]); } - serializer.write(m_point_light_last_uid); + serializer.write(m_point_light_last_cmp); serializer.write((int32)m_global_lights.size()); for (int i = 0, c = m_global_lights.size(); i < c; ++i) { serializer.write(m_global_lights[i]); } - serializer.write((int32)m_global_light_last_uid); - serializer.write((int32)m_active_global_light_uid); + serializer.write(m_global_light_last_cmp); + serializer.write(m_active_global_light_cmp); } void serializeRenderables(OutputBlob& serializer) @@ -764,7 +765,8 @@ public: Path path(path_str); probe.texture = static_cast(texture_manager->load(path)); m_environment_probes.insert(entity, probe); - m_universe.addComponent(entity, ENVIRONMENT_PROBE_TYPE, this, entity.index); + ComponentHandle cmp = {entity.index}; + m_universe.addComponent(entity, ENVIRONMENT_PROBE_TYPE, this, cmp); } } @@ -778,12 +780,13 @@ public: m_bone_attachments.resize(count); for (int i = 0; i < count; ++i) { - serializer.read(m_bone_attachments[i].bone_index); - serializer.read(m_bone_attachments[i].entity); - serializer.read(m_bone_attachments[i].parent_entity); - updateRelativeMatrix(m_bone_attachments[i]); - m_universe.addComponent( - m_bone_attachments[i].entity, BONE_ATTACHMENT_TYPE, this, m_bone_attachments[i].entity.index); + auto& bone_attachment = m_bone_attachments[i]; + serializer.read(bone_attachment.bone_index); + serializer.read(bone_attachment.entity); + serializer.read(bone_attachment.parent_entity); + updateRelativeMatrix(bone_attachment); + ComponentHandle cmp = {bone_attachment.entity.index}; + m_universe.addComponent(bone_attachment.entity, BONE_ATTACHMENT_TYPE, this, cmp); } } @@ -805,40 +808,41 @@ public: emitter->deserialize(serializer, m_engine.getResourceManager(), version > (int)RenderSceneVersion::PARTICLE_EMITTERS_SPAWN_COUNT); - m_universe.addComponent(emitter->m_entity, PARTICLE_EMITTER_TYPE, this, i); + ComponentHandle cmp = {i}; + m_universe.addComponent(emitter->m_entity, PARTICLE_EMITTER_TYPE, this, cmp); for (auto* module : emitter->m_modules) { if (module->getType() == ParticleEmitter::AlphaModule::s_type) { - m_universe.addComponent(emitter->m_entity, PARTICLE_EMITTER_ALPHA_TYPE, this, i); + m_universe.addComponent(emitter->m_entity, PARTICLE_EMITTER_ALPHA_TYPE, this, cmp); } else if (module->getType() == ParticleEmitter::ForceModule::s_type) { - m_universe.addComponent(emitter->m_entity, PARTICLE_EMITTER_FORCE_HASH, this, i); + m_universe.addComponent(emitter->m_entity, PARTICLE_EMITTER_FORCE_HASH, this, cmp); } else if (module->getType() == ParticleEmitter::SpawnShapeModule::s_type) { - m_universe.addComponent(emitter->m_entity, PARTICLE_EMITTER_SPAWN_SHAPE_HASH, this, i); + m_universe.addComponent(emitter->m_entity, PARTICLE_EMITTER_SPAWN_SHAPE_HASH, this, cmp); } else if (module->getType() == ParticleEmitter::AttractorModule::s_type) { - m_universe.addComponent(emitter->m_entity, PARTICLE_EMITTER_ATTRACTOR_HASH, this, i); + m_universe.addComponent(emitter->m_entity, PARTICLE_EMITTER_ATTRACTOR_HASH, this, cmp); } else if (module->getType() == ParticleEmitter::LinearMovementModule::s_type) { - m_universe.addComponent(emitter->m_entity, PARTICLE_EMITTER_LINEAR_MOVEMENT_HASH, this, i); + m_universe.addComponent(emitter->m_entity, PARTICLE_EMITTER_LINEAR_MOVEMENT_HASH, this, cmp); } else if (module->getType() == ParticleEmitter::PlaneModule::s_type) { - m_universe.addComponent(emitter->m_entity, PARTICLE_EMITTER_PLANE_HASH, this, i); + m_universe.addComponent(emitter->m_entity, PARTICLE_EMITTER_PLANE_HASH, this, cmp); } else if (module->getType() == ParticleEmitter::RandomRotationModule::s_type) { - m_universe.addComponent(emitter->m_entity, PARTICLE_EMITTER_RANDOM_ROTATION_HASH, this, i); + m_universe.addComponent(emitter->m_entity, PARTICLE_EMITTER_RANDOM_ROTATION_HASH, this, cmp); } else if (module->getType() == ParticleEmitter::SizeModule::s_type) { - m_universe.addComponent(emitter->m_entity, PARTICLE_EMITTER_SIZE_HASH, this, i); + m_universe.addComponent(emitter->m_entity, PARTICLE_EMITTER_SIZE_HASH, this, cmp); } } } @@ -933,7 +937,7 @@ public: if (!camera.is_free) { - m_universe.addComponent(m_cameras[i].entity, CAMERA_TYPE, this, i); + m_universe.addComponent(m_cameras[i].entity, CAMERA_TYPE, this, {i}); } } } @@ -946,7 +950,7 @@ public: { if (m_renderables[i].entity != INVALID_ENTITY) { - setModel(i, nullptr); + setModel({i}, nullptr); } } m_culling_system->clear(); @@ -971,10 +975,11 @@ public: uint32 path; serializer.read(path); + ComponentHandle cmp = { r.entity.index }; if (path != 0) { auto* model = static_cast(m_engine.getResourceManager().get(MODEL_HASH)->load(Path(path))); - setModel(r.entity.index, model); + setModel(cmp, model); } if (version > RenderSceneVersion::RENDERABLE_MATERIALS) @@ -995,7 +1000,7 @@ public: } } - m_universe.addComponent(r.entity, RENDERABLE_TYPE, this, r.entity.index); + m_universe.addComponent(r.entity, RENDERABLE_TYPE, this, cmp); } } } @@ -1009,7 +1014,7 @@ public: m_light_influenced_geometry.clear(); for (int i = 0; i < size; ++i) { - m_light_influenced_geometry.push(Array(m_allocator)); + m_light_influenced_geometry.push(Array(m_allocator)); PointLight& light = m_point_lights[i]; if (version > RenderSceneVersion::SPECULAR_INTENSITY) { @@ -1021,7 +1026,7 @@ public: serializer.read(light.m_specular_color); serializer.read(light.m_diffuse_intensity); serializer.read(light.m_entity); - serializer.read(light.m_uid); + serializer.read(light.m_component); serializer.read(light.m_fov); serializer.read(light.m_attenuation_param); serializer.read(light.m_range); @@ -1030,11 +1035,11 @@ public: for(int j = 0; j < 3; ++j) serializer.read(padding); light.m_specular_intensity = 1; } - m_point_lights_map.insert(light.m_uid, i); + m_point_lights_map.insert(light.m_component, i); - m_universe.addComponent(light.m_entity, POINT_LIGHT_TYPE, this, light.m_uid); + m_universe.addComponent(light.m_entity, POINT_LIGHT_TYPE, this, light.m_component); } - serializer.read(m_point_light_last_uid); + serializer.read(m_point_light_last_cmp); serializer.read(size); m_global_lights.resize(size); @@ -1048,7 +1053,7 @@ public: } else { - serializer.read(light.m_uid); + serializer.read(light.m_component); serializer.read(light.m_diffuse_color); serializer.read(light.m_specular); serializer.read(light.m_diffuse_intensity); @@ -1062,10 +1067,10 @@ public: serializer.read(light.m_cascades); light.m_specular_intensity = 1; } - m_universe.addComponent(light.m_entity, GLOBAL_LIGHT_TYPE, this, light.m_uid); + m_universe.addComponent(light.m_entity, GLOBAL_LIGHT_TYPE, this, light.m_component); } - serializer.read(m_global_light_last_uid); - serializer.read(m_active_global_light_uid); + serializer.read(m_global_light_last_cmp); + serializer.read(m_active_global_light_cmp); } void deserializeTerrains(InputBlob& serializer, RenderSceneVersion version) @@ -1091,11 +1096,10 @@ public: { if (!m_terrains[i]) { - m_terrains[i] = LUMIX_NEW(m_allocator, Terrain)( - m_renderer, INVALID_ENTITY, *this, m_allocator); + m_terrains[i] = LUMIX_NEW(m_allocator, Terrain)(m_renderer, INVALID_ENTITY, *this, m_allocator); } Terrain* terrain = m_terrains[i]; - terrain->deserialize(serializer, m_universe, *this, i, (int)version); + terrain->deserialize(serializer, m_universe, *this, {i}, (int)version); } else { @@ -1128,7 +1132,7 @@ public: } - void destroyBoneAttachment(ComponentIndex component) + void destroyBoneAttachment(ComponentHandle component) { int idx = getBoneAttachmentIdx(component); Entity entity = m_bone_attachments[idx].entity; @@ -1137,9 +1141,9 @@ public: } - void destroyEnvironmentProbe(ComponentIndex component) + void destroyEnvironmentProbe(ComponentHandle component) { - Entity entity = {component}; + Entity entity = {component.index}; auto& probe = m_environment_probes[entity]; if (probe.texture) probe.texture->getResourceManager().get(TEXTURE_HASH)->unload(*probe.texture); m_environment_probes.erase(entity); @@ -1147,12 +1151,12 @@ public: } - void destroyRenderable(ComponentIndex component) + void destroyRenderable(ComponentHandle component) { m_renderable_destroyed.invoke(component); for (int i = 0; i < m_light_influenced_geometry.size(); ++i) { - Array& influenced_geometry = m_light_influenced_geometry[i]; + Array& influenced_geometry = m_light_influenced_geometry[i]; for (int j = 0; j < influenced_geometry.size(); ++j) { if (influenced_geometry[j] == component) @@ -1164,75 +1168,81 @@ public: } setModel(component, nullptr); - Entity entity = m_renderables[component].entity; - LUMIX_DELETE(m_allocator, m_renderables[component].pose); - m_renderables[component].pose = nullptr; - m_renderables[component].entity = INVALID_ENTITY; + auto& renderable = m_renderables[component.index]; + Entity entity = renderable.entity; + LUMIX_DELETE(m_allocator, renderable.pose); + renderable.pose = nullptr; + renderable.entity = INVALID_ENTITY; m_universe.destroyComponent(entity, RENDERABLE_TYPE, this, component); } - void destroyGlobalLight(ComponentIndex component) + void destroyGlobalLight(ComponentHandle component) { Entity entity = m_global_lights[getGlobalLightIndex(component)].m_entity; m_universe.destroyComponent(entity, GLOBAL_LIGHT_TYPE, this, component); - if (component == m_active_global_light_uid) + if (component == m_active_global_light_cmp) { - m_active_global_light_uid = -1; + m_active_global_light_cmp = INVALID_COMPONENT; } m_global_lights.eraseFast(getGlobalLightIndex(component)); } - void destroyPointLight(ComponentIndex component) + void destroyPointLight(ComponentHandle component) { int index = getPointLightIndex(component); - Entity entity = m_point_lights[getPointLightIndex(component)].m_entity; + Entity entity = m_point_lights[index].m_entity; m_point_lights.eraseFast(index); m_point_lights_map.erase(component); m_light_influenced_geometry.eraseFast(index); + if (index < m_point_lights.size()) + { + m_point_lights_map[m_point_lights[index].m_component] = index; + } m_universe.destroyComponent(entity, POINT_LIGHT_TYPE, this, component); } - void destroyCamera(ComponentIndex component) + void destroyCamera(ComponentHandle component) { - Entity entity = m_cameras[component].entity; - m_cameras[component].is_free = true; + Entity entity = m_cameras[component.index].entity; + m_cameras[component.index].is_free = true; m_universe.destroyComponent(entity, CAMERA_TYPE, this, component); } - void destroyTerrain(ComponentIndex component) + void destroyTerrain(ComponentHandle component) { - Entity entity = m_terrains[component]->getEntity(); - LUMIX_DELETE(m_allocator, m_terrains[component]); - m_terrains[component] = nullptr; + Entity entity = m_terrains[component.index]->getEntity(); + LUMIX_DELETE(m_allocator, m_terrains[component.index]); + m_terrains[component.index] = nullptr; m_universe.destroyComponent(entity, TERRAIN_TYPE, this, component); } - void destroyParticleEmitter(ComponentIndex component) + void destroyParticleEmitter(ComponentHandle component) { - Entity entity = m_particle_emitters[component]->m_entity; - for (auto* module : m_particle_emitters[component]->m_modules) + auto* emitter = m_particle_emitters[component.index]; + Entity entity = emitter->m_entity; + for (auto* module : emitter->m_modules) { ComponentType cmp_type = module->getType(); LUMIX_DELETE(m_allocator, module); m_universe.destroyComponent(entity, cmp_type, this, component); } - m_particle_emitters[component]->m_modules.clear(); - LUMIX_DELETE(m_allocator, m_particle_emitters[component]); - m_particle_emitters[component] = nullptr; + emitter->m_modules.clear(); + LUMIX_DELETE(m_allocator, emitter); + m_particle_emitters[component.index] = nullptr; m_universe.destroyComponent(entity, PARTICLE_EMITTER_TYPE, this, component); } - void destroyParticleEmitterAlpha(ComponentIndex component) + void destroyParticleEmitterAlpha(ComponentHandle component) { - auto* emitter = m_particle_emitters[component]; + auto* emitter = m_particle_emitters[component.index]; for (auto* module : emitter->m_modules) { if (module->getType() == ParticleEmitter::AlphaModule::s_type) @@ -1246,9 +1256,9 @@ public: } - void destroyParticleEmitterForce(ComponentIndex component) + void destroyParticleEmitterForce(ComponentHandle component) { - auto* emitter = m_particle_emitters[component]; + auto* emitter = m_particle_emitters[component.index]; for(auto* module : emitter->m_modules) { if(module->getType() == ParticleEmitter::ForceModule::s_type) @@ -1262,9 +1272,9 @@ public: } - void destroyParticleEmitterAttractor(ComponentIndex component) + void destroyParticleEmitterAttractor(ComponentHandle component) { - auto* emitter = m_particle_emitters[component]; + auto* emitter = m_particle_emitters[component.index]; for(auto* module : emitter->m_modules) { if(module->getType() == ParticleEmitter::AttractorModule::s_type) @@ -1278,9 +1288,9 @@ public: } - void destroyParticleEmitterSize(ComponentIndex component) + void destroyParticleEmitterSize(ComponentHandle component) { - auto* emitter = m_particle_emitters[component]; + auto* emitter = m_particle_emitters[component.index]; for (auto* module : emitter->m_modules) { if (module->getType() == ParticleEmitter::SizeModule::s_type) @@ -1294,9 +1304,9 @@ public: } - float getParticleEmitterPlaneBounce(ComponentIndex cmp) override + float getParticleEmitterPlaneBounce(ComponentHandle cmp) override { - auto* emitter = m_particle_emitters[cmp]; + auto* emitter = m_particle_emitters[cmp.index]; for(auto* module : emitter->m_modules) { if(module->getType() == ParticleEmitter::PlaneModule::s_type) @@ -1308,12 +1318,12 @@ public: } - void setParticleEmitterPlaneBounce(ComponentIndex cmp, float value) override + void setParticleEmitterPlaneBounce(ComponentHandle cmp, float value) override { - auto* emitter = m_particle_emitters[cmp]; - for(auto* module : emitter->m_modules) + auto* emitter = m_particle_emitters[cmp.index]; + for (auto* module : emitter->m_modules) { - if(module->getType() == ParticleEmitter::PlaneModule::s_type) + if (module->getType() == ParticleEmitter::PlaneModule::s_type) { static_cast(module)->m_bounce = value; break; @@ -1322,12 +1332,12 @@ public: } - float getParticleEmitterAttractorForce(ComponentIndex cmp) override + float getParticleEmitterAttractorForce(ComponentHandle cmp) override { - auto* emitter = m_particle_emitters[cmp]; - for(auto* module : emitter->m_modules) + auto* emitter = m_particle_emitters[cmp.index]; + for (auto* module : emitter->m_modules) { - if(module->getType() == ParticleEmitter::AttractorModule::s_type) + if (module->getType() == ParticleEmitter::AttractorModule::s_type) { return static_cast(module)->m_force; } @@ -1336,12 +1346,12 @@ public: } - void setParticleEmitterAttractorForce(ComponentIndex cmp, float value) override + void setParticleEmitterAttractorForce(ComponentHandle cmp, float value) override { - auto* emitter = m_particle_emitters[cmp]; - for(auto* module : emitter->m_modules) + auto* emitter = m_particle_emitters[cmp.index]; + for (auto* module : emitter->m_modules) { - if(module->getType() == ParticleEmitter::AttractorModule::s_type) + if (module->getType() == ParticleEmitter::AttractorModule::s_type) { static_cast(module)->m_force = value; break; @@ -1350,78 +1360,74 @@ public: } - void destroyParticleEmitterPlane(ComponentIndex component) + void destroyParticleEmitterPlane(ComponentHandle component) { - auto* emitter = m_particle_emitters[component]; + auto* emitter = m_particle_emitters[component.index]; for (auto* module : emitter->m_modules) { if (module->getType() == ParticleEmitter::PlaneModule::s_type) { LUMIX_DELETE(m_allocator, module); emitter->m_modules.eraseItem(module); - m_universe.destroyComponent( - emitter->m_entity, PARTICLE_EMITTER_PLANE_HASH, this, component); + m_universe.destroyComponent(emitter->m_entity, PARTICLE_EMITTER_PLANE_HASH, this, component); break; } } } - void destroyParticleEmitterLinearMovement(ComponentIndex component) + void destroyParticleEmitterLinearMovement(ComponentHandle component) { - auto* emitter = m_particle_emitters[component]; + auto* emitter = m_particle_emitters[component.index]; for (auto* module : emitter->m_modules) { if (module->getType() == ParticleEmitter::LinearMovementModule::s_type) { LUMIX_DELETE(m_allocator, module); emitter->m_modules.eraseItem(module); - m_universe.destroyComponent( - emitter->m_entity, PARTICLE_EMITTER_LINEAR_MOVEMENT_HASH, this, component); + m_universe.destroyComponent(emitter->m_entity, PARTICLE_EMITTER_LINEAR_MOVEMENT_HASH, this, component); break; } } } - void destroyParticleEmitterSpawnShape(ComponentIndex component) + void destroyParticleEmitterSpawnShape(ComponentHandle component) { - auto* emitter = m_particle_emitters[component]; + auto* emitter = m_particle_emitters[component.index]; for (auto* module : emitter->m_modules) { if (module->getType() == ParticleEmitter::SpawnShapeModule::s_type) { LUMIX_DELETE(m_allocator, module); emitter->m_modules.eraseItem(module); - m_universe.destroyComponent( - emitter->m_entity, PARTICLE_EMITTER_SPAWN_SHAPE_HASH, this, component); + m_universe.destroyComponent(emitter->m_entity, PARTICLE_EMITTER_SPAWN_SHAPE_HASH, this, component); break; } } } - void destroyParticleEmitterRandomRotation(ComponentIndex component) + void destroyParticleEmitterRandomRotation(ComponentHandle component) { - auto* emitter = m_particle_emitters[component]; + auto* emitter = m_particle_emitters[component.index]; for (auto* module : emitter->m_modules) { if (module->getType() == ParticleEmitter::RandomRotationModule::s_type) { LUMIX_DELETE(m_allocator, module); emitter->m_modules.eraseItem(module); - m_universe.destroyComponent( - emitter->m_entity, PARTICLE_EMITTER_RANDOM_ROTATION_HASH, this, component); + m_universe.destroyComponent(emitter->m_entity, PARTICLE_EMITTER_RANDOM_ROTATION_HASH, this, component); break; } } } - void destroyComponent(ComponentIndex component, ComponentType type) override; + void destroyComponent(ComponentHandle component, ComponentType type) override; - void setParticleEmitterAlpha(ComponentIndex cmp, const Vec2* values, int count) override + void setParticleEmitterAlpha(ComponentHandle cmp, const Vec2* values, int count) override { ASSERT(count > 0); ASSERT(values[1].x < 0.001f); @@ -1439,35 +1445,35 @@ public: } - void setParticleEmitterAcceleration(ComponentIndex cmp, const Vec3& value) override + void setParticleEmitterAcceleration(ComponentHandle cmp, const Vec3& value) override { auto* module = getEmitterModule(cmp); if (module) module->m_acceleration = value; } - Vec3 getParticleEmitterAcceleration(ComponentIndex cmp) override + Vec3 getParticleEmitterAcceleration(ComponentHandle cmp) override { auto* module = getEmitterModule(cmp); return module ? module->m_acceleration : Vec3(); } - int getParticleEmitterSizeCount(ComponentIndex cmp) override + int getParticleEmitterSizeCount(ComponentHandle cmp) override { auto* module = getEmitterModule(cmp); return module ? module->m_values.size() : 0; } - const Vec2* getParticleEmitterSize(ComponentIndex cmp) override + const Vec2* getParticleEmitterSize(ComponentHandle cmp) override { auto* module = getEmitterModule(cmp); return module ? &module->m_values[0] : nullptr; } - void setParticleEmitterSize(ComponentIndex cmp, const Vec2* values, int count) override + void setParticleEmitterSize(ComponentHandle cmp, const Vec2* values, int count) override { ASSERT(count > 0); ASSERT(values[0].x < 0.001f); @@ -1487,9 +1493,9 @@ public: template - T* getEmitterModule(ComponentIndex cmp) const + T* getEmitterModule(ComponentHandle cmp) const { - auto& modules = m_particle_emitters[cmp]->m_modules; + auto& modules = m_particle_emitters[cmp.index]->m_modules; for (auto* module : modules) { if (module->getType() == T::s_type) @@ -1501,28 +1507,28 @@ public: } - int getParticleEmitterAlphaCount(ComponentIndex cmp) override + int getParticleEmitterAlphaCount(ComponentHandle cmp) override { auto* module = getEmitterModule(cmp); return module ? module->m_values.size() : 0; } - const Vec2* getParticleEmitterAlpha(ComponentIndex cmp) override + const Vec2* getParticleEmitterAlpha(ComponentHandle cmp) override { auto* module = getEmitterModule(cmp); return module ? &module->m_values[0] : 0; } - Vec2 getParticleEmitterLinearMovementX(ComponentIndex cmp) override + Vec2 getParticleEmitterLinearMovementX(ComponentHandle cmp) override { auto* module = getEmitterModule(cmp); return module ? Vec2(module->m_x.from, module->m_x.to) : Vec2(0, 0); } - void setParticleEmitterLinearMovementX(ComponentIndex cmp, const Vec2& value) override + void setParticleEmitterLinearMovementX(ComponentHandle cmp, const Vec2& value) override { auto* module = getEmitterModule(cmp); if (module) @@ -1533,14 +1539,14 @@ public: } - Vec2 getParticleEmitterLinearMovementY(ComponentIndex cmp) override + Vec2 getParticleEmitterLinearMovementY(ComponentHandle cmp) override { auto* module = getEmitterModule(cmp); return module ? Vec2(module->m_y.from, module->m_y.to) : Vec2(0, 0); } - void setParticleEmitterLinearMovementY(ComponentIndex cmp, const Vec2& value) override + void setParticleEmitterLinearMovementY(ComponentHandle cmp, const Vec2& value) override { auto* module = getEmitterModule(cmp); if (module) @@ -1551,14 +1557,14 @@ public: } - Vec2 getParticleEmitterLinearMovementZ(ComponentIndex cmp) override + Vec2 getParticleEmitterLinearMovementZ(ComponentHandle cmp) override { auto* module = getEmitterModule(cmp); return module ? Vec2(module->m_z.from, module->m_z.to) : Vec2(0, 0); } - void setParticleEmitterLinearMovementZ(ComponentIndex cmp, const Vec2& value) override + void setParticleEmitterLinearMovementZ(ComponentHandle cmp, const Vec2& value) override { auto* module = getEmitterModule(cmp); if (module) @@ -1569,48 +1575,48 @@ public: } - Vec2 getParticleEmitterInitialLife(ComponentIndex cmp) override + Vec2 getParticleEmitterInitialLife(ComponentHandle cmp) override { - return m_particle_emitters[cmp]->m_initial_life; + return m_particle_emitters[cmp.index]->m_initial_life; } - Vec2 getParticleEmitterSpawnPeriod(ComponentIndex cmp) override + Vec2 getParticleEmitterSpawnPeriod(ComponentHandle cmp) override { - return m_particle_emitters[cmp]->m_spawn_period; + return m_particle_emitters[cmp.index]->m_spawn_period; } - void setParticleEmitterInitialLife(ComponentIndex cmp, const Vec2& value) override + void setParticleEmitterInitialLife(ComponentHandle cmp, const Vec2& value) override { - m_particle_emitters[cmp]->m_initial_life = value; - m_particle_emitters[cmp]->m_initial_life.checkZero(); + m_particle_emitters[cmp.index]->m_initial_life = value; + m_particle_emitters[cmp.index]->m_initial_life.checkZero(); } - void setParticleEmitterInitialSize(ComponentIndex cmp, const Vec2& value) override + void setParticleEmitterInitialSize(ComponentHandle cmp, const Vec2& value) override { - m_particle_emitters[cmp]->m_initial_size = value; - m_particle_emitters[cmp]->m_initial_size.checkZero(); + m_particle_emitters[cmp.index]->m_initial_size = value; + m_particle_emitters[cmp.index]->m_initial_size.checkZero(); } - Vec2 getParticleEmitterInitialSize(ComponentIndex cmp) override + Vec2 getParticleEmitterInitialSize(ComponentHandle cmp) override { - return m_particle_emitters[cmp]->m_initial_size; + return m_particle_emitters[cmp.index]->m_initial_size; } - void setParticleEmitterSpawnPeriod(ComponentIndex cmp, const Vec2& value) override + void setParticleEmitterSpawnPeriod(ComponentHandle cmp, const Vec2& value) override { - m_particle_emitters[cmp]->m_spawn_period = value; - m_particle_emitters[cmp]->m_spawn_period.from = - Math::maximum(0.01f, m_particle_emitters[cmp]->m_spawn_period.from); - m_particle_emitters[cmp]->m_spawn_period.checkZero(); + auto* emitter = m_particle_emitters[cmp.index]; + emitter->m_spawn_period = value; + emitter->m_spawn_period.from = Math::maximum(0.01f, emitter->m_spawn_period.from); + emitter->m_spawn_period.checkZero(); } - ComponentIndex createCamera(Entity entity) + ComponentHandle createCamera(Entity entity) { Camera& camera = m_cameras.emplace(); camera.is_free = false; @@ -1624,25 +1630,24 @@ public: camera.near = 0.1f; camera.far = 10000.0f; camera.slot[0] = '\0'; - m_universe.addComponent(entity, CAMERA_TYPE, this, m_cameras.size() - 1); - return m_cameras.size() - 1; + m_universe.addComponent(entity, CAMERA_TYPE, this, {m_cameras.size() - 1}); + return {m_cameras.size() - 1}; } - ComponentIndex createTerrain(Entity entity) + ComponentHandle createTerrain(Entity entity) { - Terrain* terrain = LUMIX_NEW(m_allocator, Terrain)( - m_renderer, entity, *this, m_allocator); + Terrain* terrain = LUMIX_NEW(m_allocator, Terrain)(m_renderer, entity, *this, m_allocator); m_terrains.push(terrain); - m_universe.addComponent(entity, TERRAIN_TYPE, this, m_terrains.size() - 1); - return m_terrains.size() - 1; + m_universe.addComponent(entity, TERRAIN_TYPE, this, {m_terrains.size() - 1}); + return {m_terrains.size() - 1}; } - ComponentIndex createComponent(ComponentType type, Entity entity) override; + ComponentHandle createComponent(ComponentType type, Entity entity) override; - ComponentIndex createParticleEmitterRandomRotation(Entity entity) + ComponentHandle createParticleEmitterRandomRotation(Entity entity) { for (int i = 0; i < m_particle_emitters.size(); ++i) { @@ -1651,15 +1656,15 @@ public: { auto module = LUMIX_NEW(m_allocator, ParticleEmitter::RandomRotationModule)(*emitter); emitter->addModule(module); - m_universe.addComponent(entity, PARTICLE_EMITTER_RANDOM_ROTATION_HASH, this, i); - return i; + m_universe.addComponent(entity, PARTICLE_EMITTER_RANDOM_ROTATION_HASH, this, {i}); + return {i}; } } return INVALID_COMPONENT; } - ComponentIndex createParticleEmitterPlane(Entity entity) + ComponentHandle createParticleEmitterPlane(Entity entity) { for (int i = 0; i < m_particle_emitters.size(); ++i) { @@ -1668,15 +1673,15 @@ public: { auto module = LUMIX_NEW(m_allocator, ParticleEmitter::PlaneModule)(*emitter); emitter->addModule(module); - m_universe.addComponent(entity, PARTICLE_EMITTER_PLANE_HASH, this, i); - return i; + m_universe.addComponent(entity, PARTICLE_EMITTER_PLANE_HASH, this, {i}); + return {i}; } } return INVALID_COMPONENT; } - ComponentIndex createParticleEmitterLinearMovement(Entity entity) + ComponentHandle createParticleEmitterLinearMovement(Entity entity) { for (int i = 0; i < m_particle_emitters.size(); ++i) { @@ -1685,15 +1690,15 @@ public: { auto module = LUMIX_NEW(m_allocator, ParticleEmitter::LinearMovementModule)(*emitter); emitter->addModule(module); - m_universe.addComponent(entity, PARTICLE_EMITTER_LINEAR_MOVEMENT_HASH, this, i); - return i; + m_universe.addComponent(entity, PARTICLE_EMITTER_LINEAR_MOVEMENT_HASH, this, {i}); + return {i}; } } return INVALID_COMPONENT; } - ComponentIndex createParticleEmitterSpawnShape(Entity entity) + ComponentHandle createParticleEmitterSpawnShape(Entity entity) { for (int i = 0; i < m_particle_emitters.size(); ++i) { @@ -1702,15 +1707,15 @@ public: { auto module = LUMIX_NEW(m_allocator, ParticleEmitter::SpawnShapeModule)(*emitter); emitter->addModule(module); - m_universe.addComponent(entity, PARTICLE_EMITTER_SPAWN_SHAPE_HASH, this, i); - return i; + m_universe.addComponent(entity, PARTICLE_EMITTER_SPAWN_SHAPE_HASH, this, {i}); + return {i}; } } return INVALID_COMPONENT; } - ComponentIndex createParticleEmitterAlpha(Entity entity) + ComponentHandle createParticleEmitterAlpha(Entity entity) { for (int i = 0; i < m_particle_emitters.size(); ++i) { @@ -1719,15 +1724,15 @@ public: { auto module = LUMIX_NEW(m_allocator, ParticleEmitter::AlphaModule)(*emitter); emitter->addModule(module); - m_universe.addComponent(entity, PARTICLE_EMITTER_ALPHA_TYPE, this, i); - return i; + m_universe.addComponent(entity, PARTICLE_EMITTER_ALPHA_TYPE, this, {i}); + return {i}; } } return INVALID_COMPONENT; } - ComponentIndex createParticleEmitterForce(Entity entity) + ComponentHandle createParticleEmitterForce(Entity entity) { for (int i = 0; i < m_particle_emitters.size(); ++i) { @@ -1736,15 +1741,15 @@ public: { auto module = LUMIX_NEW(m_allocator, ParticleEmitter::ForceModule)(*emitter); emitter->addModule(module); - m_universe.addComponent(entity, PARTICLE_EMITTER_FORCE_HASH, this, i); - return i; + m_universe.addComponent(entity, PARTICLE_EMITTER_FORCE_HASH, this, {i}); + return {i}; } } return INVALID_COMPONENT; } - ComponentIndex createParticleEmitterAttractor(Entity entity) + ComponentHandle createParticleEmitterAttractor(Entity entity) { for(int i = 0; i < m_particle_emitters.size(); ++i) { @@ -1753,8 +1758,8 @@ public: { auto module = LUMIX_NEW(m_allocator, ParticleEmitter::AttractorModule)(*emitter); emitter->addModule(module); - m_universe.addComponent(entity, PARTICLE_EMITTER_ATTRACTOR_HASH, this, i); - return i; + m_universe.addComponent(entity, PARTICLE_EMITTER_ATTRACTOR_HASH, this, {i}); + return {i}; } } return INVALID_COMPONENT; @@ -1762,7 +1767,7 @@ public: - ComponentIndex createParticleEmitterSize(Entity entity) + ComponentHandle createParticleEmitterSize(Entity entity) { for (int i = 0; i < m_particle_emitters.size(); ++i) { @@ -1771,15 +1776,15 @@ public: { auto module = LUMIX_NEW(m_allocator, ParticleEmitter::SizeModule)(*emitter); emitter->addModule(module); - m_universe.addComponent(entity, PARTICLE_EMITTER_SIZE_HASH, this, i); - return i; + m_universe.addComponent(entity, PARTICLE_EMITTER_SIZE_HASH, this, {i}); + return {i}; } } return INVALID_COMPONENT; } - ComponentIndex createParticleEmitter(Entity entity) + ComponentHandle createParticleEmitter(Entity entity) { int index = -1; for (int i = 0, c = m_particle_emitters.size(); i < c; ++i) @@ -1800,9 +1805,9 @@ public: m_particle_emitters[index] = LUMIX_NEW(m_allocator, ParticleEmitter)(entity, m_universe, m_allocator); - m_universe.addComponent(entity, PARTICLE_EMITTER_TYPE, this, index); + m_universe.addComponent(entity, PARTICLE_EMITTER_TYPE, this, {index}); - return index; + return {index}; } @@ -1812,24 +1817,24 @@ public: } - Renderable* getRenderable(ComponentIndex cmp) override + Renderable* getRenderable(ComponentHandle cmp) override { - return &m_renderables[cmp]; + return &m_renderables[cmp.index]; } - ComponentIndex getRenderableComponent(Entity entity) override + ComponentHandle getRenderableComponent(Entity entity) override { - ComponentIndex cmp = entity.index; - if (cmp >= m_renderables.size()) return INVALID_COMPONENT; - if (m_renderables[cmp].entity == INVALID_ENTITY) return INVALID_COMPONENT; + ComponentHandle cmp = {entity.index}; + if (cmp.index >= m_renderables.size()) return INVALID_COMPONENT; + if (!isValid(m_renderables[cmp.index].entity)) return INVALID_COMPONENT; return cmp; } - Frustum getPointLightFrustum(ComponentIndex index) const + Frustum getPointLightFrustum(ComponentHandle cmp) const { - const PointLight& light = m_point_lights[index]; + const PointLight& light = m_point_lights[getPointLightIndex(cmp)]; Frustum frustum; frustum.computeOrtho(m_universe.getPosition(light.m_entity), Vec3(1, 0, 0), @@ -1858,12 +1863,13 @@ public: void onEntityMoved(Entity entity) { - ComponentIndex cmp = entity.index; + int index = entity.index; + ComponentHandle cmp = {index}; - if (cmp < m_renderables.size() && m_renderables[cmp].entity != INVALID_ENTITY && - m_renderables[cmp].model && m_renderables[cmp].model->isReady()) + if (index < m_renderables.size() && isValid(m_renderables[index].entity) && + m_renderables[index].model && m_renderables[index].model->isReady()) { - Renderable& r = m_renderables[cmp]; + Renderable& r = m_renderables[index]; r.matrix = m_universe.getMatrix(entity); m_culling_system->updateBoundingPosition(m_universe.getPosition(entity), cmp); if (r.model && r.model->isReady()) @@ -1887,7 +1893,7 @@ public: } Vec3 pos = m_universe.getPosition(r.entity); - Frustum frustum = getPointLightFrustum(light_idx); + Frustum frustum = getPointLightFrustum({light_idx}); if(frustum.isSphereInside(pos, bounding_radius)) { m_light_influenced_geometry[light_idx].push(cmp); @@ -1900,7 +1906,7 @@ public: { if (m_point_lights[i].m_entity == entity) { - detectLightInfluencedGeometry(i); + detectLightInfluencedGeometry(m_point_lights[i].m_component); break; } } @@ -1931,103 +1937,100 @@ public: Engine& getEngine() const override { return m_engine; } - Entity getTerrainEntity(ComponentIndex cmp) override + Entity getTerrainEntity(ComponentHandle cmp) override { - return m_terrains[cmp]->getEntity(); + return m_terrains[cmp.index]->getEntity(); } - Vec2 getTerrainResolution(ComponentIndex cmp) override + Vec2 getTerrainResolution(ComponentHandle cmp) override { - return Vec2((float)m_terrains[cmp]->getWidth(), (float)m_terrains[cmp]->getHeight()); + auto* terrain = m_terrains[cmp.index]; + return Vec2((float)terrain->getWidth(), (float)terrain->getHeight()); } - ComponentIndex getFirstTerrain() override + ComponentHandle getFirstTerrain() override { if (m_terrains.empty()) return INVALID_COMPONENT; for (int i = 0; i < m_terrains.size(); ++i) { - if (m_terrains[i]) return i; + if (m_terrains[i]) return {i}; } return INVALID_COMPONENT; } - ComponentIndex getNextTerrain(ComponentIndex cmp) override + ComponentHandle getNextTerrain(ComponentHandle cmp) override { - for (int i = cmp + 1; i < m_terrains.size(); ++i) + for (int i = cmp.index + 1; i < m_terrains.size(); ++i) { - if (m_terrains[i]) return i; + if (m_terrains[i]) return {i}; } return INVALID_COMPONENT; } - ComponentIndex getTerrainComponent(Entity entity) override + ComponentHandle getTerrainComponent(Entity entity) override { for (int i = 0; i < m_terrains.size(); ++i) { auto* terrain = m_terrains[i]; if (terrain && terrain->getEntity() == entity) { - return i; + return {i}; } } - return -1; + return INVALID_COMPONENT; } - Vec3 getTerrainNormalAt(ComponentIndex cmp, float x, float z) override + Vec3 getTerrainNormalAt(ComponentHandle cmp, float x, float z) override { - return m_terrains[cmp]->getNormal(x, z); + return m_terrains[cmp.index]->getNormal(x, z); } - float getTerrainHeightAt(ComponentIndex cmp, float x, float z) override + float getTerrainHeightAt(ComponentHandle cmp, float x, float z) override { - return m_terrains[cmp]->getHeight(x, z); + return m_terrains[cmp.index]->getHeight(x, z); } - AABB getTerrainAABB(ComponentIndex cmp) override + AABB getTerrainAABB(ComponentHandle cmp) override { - return m_terrains[cmp]->getAABB(); + return m_terrains[cmp.index]->getAABB(); } - Vec2 getTerrainSize(ComponentIndex cmp) override + Vec2 getTerrainSize(ComponentHandle cmp) override { - return m_terrains[cmp]->getSize(); + return m_terrains[cmp.index]->getSize(); } - void setTerrainMaterialPath(ComponentIndex cmp, const Path& path) override + void setTerrainMaterialPath(ComponentHandle cmp, const Path& path) override { if (path.isValid()) { - Material* material = static_cast( - m_engine.getResourceManager().get(MATERIAL_HASH)->load(path)); - m_terrains[cmp]->setMaterial(material); + Material* material = static_cast(m_engine.getResourceManager().get(MATERIAL_HASH)->load(path)); + m_terrains[cmp.index]->setMaterial(material); } else { - m_terrains[cmp]->setMaterial(nullptr); + m_terrains[cmp.index]->setMaterial(nullptr); } } - Material* getTerrainMaterial(ComponentIndex cmp) override - { - return m_terrains[cmp]->getMaterial(); - } + Material* getTerrainMaterial(ComponentHandle cmp) override { return m_terrains[cmp.index]->getMaterial(); } - Path getTerrainMaterialPath(ComponentIndex cmp) override + Path getTerrainMaterialPath(ComponentHandle cmp) override { - if (m_terrains[cmp]->getMaterial()) + if (m_terrains[cmp.index]->getMaterial()) { - return m_terrains[cmp]->getMaterial()->getPath(); + return m_terrains[cmp.index]->getMaterial()->getPath(); } else { @@ -2036,67 +2039,68 @@ public: } - void setTerrainXZScale(ComponentIndex cmp, float scale) override + void setTerrainXZScale(ComponentHandle cmp, float scale) override { - m_terrains[cmp]->setXZScale(scale); + m_terrains[cmp.index]->setXZScale(scale); } - float getTerrainXZScale(ComponentIndex cmp) override { return m_terrains[cmp]->getXZScale(); } + float getTerrainXZScale(ComponentHandle cmp) override { return m_terrains[cmp.index]->getXZScale(); } - void setTerrainYScale(ComponentIndex cmp, float scale) override + void setTerrainYScale(ComponentHandle cmp, float scale) override { - m_terrains[cmp]->setYScale(scale); + m_terrains[cmp.index]->setYScale(scale); } - float getTerrainYScale(ComponentIndex cmp) override { return m_terrains[cmp]->getYScale(); } + float getTerrainYScale(ComponentHandle cmp) override { return m_terrains[cmp.index]->getYScale(); } - Pose* getPose(ComponentIndex cmp) override { return m_renderables[cmp].pose; } + Pose* getPose(ComponentHandle cmp) override { return m_renderables[cmp.index].pose; } - Entity getRenderableEntity(ComponentIndex cmp) override { return m_renderables[cmp].entity; } + Entity getRenderableEntity(ComponentHandle cmp) override { return m_renderables[cmp.index].entity; } - Model* getRenderableModel(ComponentIndex cmp) override { return m_renderables[cmp].model; } + Model* getRenderableModel(ComponentHandle cmp) override { return m_renderables[cmp.index].model; } - void showRenderable(ComponentIndex cmp) override + void showRenderable(ComponentHandle cmp) override { - if (!m_renderables[cmp].model || !m_renderables[cmp].model->isReady()) return; + auto& renderable = m_renderables[cmp.index]; + if (!renderable.model || !renderable.model->isReady()) return; - Sphere sphere(m_universe.getPosition(m_renderables[cmp].entity), m_renderables[cmp].model->getBoundingRadius()); + Sphere sphere(m_universe.getPosition(renderable.entity), renderable.model->getBoundingRadius()); if(!m_culling_system->isAdded(cmp)) m_culling_system->addStatic(cmp, sphere); } - void hideRenderable(ComponentIndex cmp) override + void hideRenderable(ComponentHandle cmp) override { m_culling_system->removeStatic(cmp); } - Path getRenderablePath(ComponentIndex cmp) override + Path getRenderablePath(ComponentHandle cmp) override { - return m_renderables[cmp].model ? m_renderables[cmp].model->getPath() : Path(""); + return m_renderables[cmp.index].model ? m_renderables[cmp.index].model->getPath() : Path(""); } - void setRenderableLayer(ComponentIndex cmp, const int32& layer) override + void setRenderableLayer(ComponentHandle cmp, const int32& layer) override { m_culling_system->setLayerMask(cmp, (int64)1 << (int64)layer); } - int getRenderableMaterialsCount(ComponentIndex cmp) override + int getRenderableMaterialsCount(ComponentHandle cmp) override { - return m_renderables[cmp].model ? m_renderables[cmp].mesh_count : 0; + return m_renderables[cmp.index].model ? m_renderables[cmp.index].mesh_count : 0; } - void setRenderablePath(ComponentIndex cmp, const Path& path) override + void setRenderablePath(ComponentHandle cmp, const Path& path) override { - Renderable& r = m_renderables[cmp]; + Renderable& r = m_renderables[cmp.index]; auto* manager = m_engine.getResourceManager().get(MODEL_HASH); if (path.isValid()) @@ -2112,7 +2116,7 @@ public: } - void forceGrassUpdate(ComponentIndex cmp) override { m_terrains[cmp]->forceGrassUpdate(); } + void forceGrassUpdate(ComponentHandle cmp) override { m_terrains[cmp.index]->forceGrassUpdate(); } void getTerrainInfos(Array& infos, @@ -2133,7 +2137,7 @@ public: void getGrassInfos(const Frustum& frustum, Array& infos, - ComponentIndex camera) override + ComponentHandle camera) override { PROFILE_FUNCTION(); @@ -2154,9 +2158,10 @@ public: auto* scene = LuaWrapper::checkArg(L, 1); const char* slot = LuaWrapper::checkArg(L, 2); - ComponentIndex camera_cmp = scene->getCameraInSlot(slot); - Vec3 origin = scene->m_universe.getPosition(scene->m_cameras[camera_cmp].entity); - Quat rot = scene->m_universe.getRotation(scene->m_cameras[camera_cmp].entity); + ComponentHandle camera_cmp = scene->getCameraInSlot(slot); + auto& camera = scene->m_cameras[camera_cmp.index]; + Vec3 origin = scene->m_universe.getPosition(camera.entity); + Quat rot = scene->m_universe.getRotation(camera.entity); RayCastModelHit hit = scene->castRay(origin, rot * Vec3(0, 0, -1), INVALID_COMPONENT); LuaWrapper::pushLua(L, hit.m_is_hit); @@ -2176,7 +2181,7 @@ public: static void LUA_setRenderablePath(IScene* scene, int component, const char* path) { RenderScene* render_scene = (RenderScene*)scene; - render_scene->setRenderablePath(component, Path(path)); + render_scene->setRenderablePath({component}, Path(path)); } @@ -2208,7 +2213,7 @@ public: static void LUA_setRenderableMaterial(RenderScene* scene, - ComponentIndex cmp, + ComponentHandle cmp, int index, const char* path) { @@ -2222,15 +2227,15 @@ public: } - float getGrassDistance(ComponentIndex cmp, int index) override + float getGrassDistance(ComponentHandle cmp, int index) override { - return m_terrains[cmp]->getGrassTypeDistance(index); + return m_terrains[cmp.index]->getGrassTypeDistance(index); } - void setGrassDistance(ComponentIndex cmp, int index, float value) override + void setGrassDistance(ComponentHandle cmp, int index, float value) override { - m_terrains[cmp]->setGrassTypeDistance(index, value); + m_terrains[cmp.index]->setGrassTypeDistance(index, value); } @@ -2241,72 +2246,72 @@ public: void - setGrassDensity(ComponentIndex cmp, int index, int density) override + setGrassDensity(ComponentHandle cmp, int index, int density) override { - m_terrains[cmp]->setGrassTypeDensity(index, density); + m_terrains[cmp.index]->setGrassTypeDensity(index, density); } - int getGrassDensity(ComponentIndex cmp, int index) override + int getGrassDensity(ComponentHandle cmp, int index) override { - return m_terrains[cmp]->getGrassTypeDensity(index); + return m_terrains[cmp.index]->getGrassTypeDensity(index); } void - setGrassGround(ComponentIndex cmp, int index, int ground) override + setGrassGround(ComponentHandle cmp, int index, int ground) override { - m_terrains[cmp]->setGrassTypeGround(index, ground); + m_terrains[cmp.index]->setGrassTypeGround(index, ground); } - int getGrassGround(ComponentIndex cmp, int index) override + int getGrassGround(ComponentHandle cmp, int index) override { - return m_terrains[cmp]->getGrassTypeGround(index); + return m_terrains[cmp.index]->getGrassTypeGround(index); } - void setGrassPath(ComponentIndex cmp, int index, const Path& path) override + void setGrassPath(ComponentHandle cmp, int index, const Path& path) override { - m_terrains[cmp]->setGrassTypePath(index, path); + m_terrains[cmp.index]->setGrassTypePath(index, path); } - Path getGrassPath(ComponentIndex cmp, int index) override + Path getGrassPath(ComponentHandle cmp, int index) override { - return m_terrains[cmp]->getGrassTypePath(index); + return m_terrains[cmp.index]->getGrassTypePath(index); } - int getGrassCount(ComponentIndex cmp) override + int getGrassCount(ComponentHandle cmp) override { - return m_terrains[cmp]->getGrassTypeCount(); + return m_terrains[cmp.index]->getGrassTypeCount(); } - void addGrass(ComponentIndex cmp, int index) override + void addGrass(ComponentHandle cmp, int index) override { - m_terrains[cmp]->addGrassType(index); + m_terrains[cmp.index]->addGrassType(index); } - void removeGrass(ComponentIndex cmp, int index) override + void removeGrass(ComponentHandle cmp, int index) override { - m_terrains[cmp]->removeGrassType(index); + m_terrains[cmp.index]->removeGrassType(index); } - ComponentIndex getFirstRenderable() override + ComponentHandle getFirstRenderable() override { - return getNextRenderable(-1); + return getNextRenderable(INVALID_COMPONENT); } - ComponentIndex getNextRenderable(ComponentIndex cmp) override + ComponentHandle getNextRenderable(ComponentHandle cmp) override { - for(int i = cmp + 1; i < m_renderables.size(); ++i) + for(int i = cmp.index + 1; i < m_renderables.size(); ++i) { - if(m_renderables[i].entity != INVALID_ENTITY) return i; + if (m_renderables[i].entity != INVALID_ENTITY) return {i}; } return INVALID_COMPONENT; } @@ -2363,11 +2368,11 @@ public: PROFILE_BLOCK("Temporary Info Job"); PROFILE_INT("Renderable count", results[subresult_index].size()); Vec3 ref_point = lod_ref_point; - const int* LUMIX_RESTRICT raw_subresults = &results[subresult_index][0]; + const ComponentHandle* LUMIX_RESTRICT raw_subresults = &results[subresult_index][0]; Renderable* LUMIX_RESTRICT renderables = &m_renderables[0]; for (int i = 0, c = results[subresult_index].size(); i < c; ++i) { - Renderable* LUMIX_RESTRICT renderable = &renderables[raw_subresults[i]]; + Renderable* LUMIX_RESTRICT renderable = &renderables[raw_subresults[i].index]; Model* LUMIX_RESTRICT model = renderable->model; float squared_distance = (renderable->matrix.getTranslation() - ref_point).squaredLength(); @@ -2390,7 +2395,7 @@ public: int getClosestPointLights(const Vec3& reference_pos, - ComponentIndex* lights, + ComponentHandle* lights, int max_lights) override { @@ -2406,7 +2411,7 @@ public: float dist_squared = (reference_pos - light_pos).squaredLength(); dists[light_count] = dist_squared; - lights[light_count] = light.m_uid; + lights[light_count] = light.m_component; for (int i = light_count; i > 0 && dists[i - 1] > dists[i]; --i) { @@ -2414,7 +2419,7 @@ public: dists[i] = dists[i - 1]; dists[i - 1] = tmp; - ComponentIndex tmp2 = lights[i]; + ComponentHandle tmp2 = lights[i]; lights[i] = lights[i - 1]; lights[i - 1] = tmp2; } @@ -2434,7 +2439,7 @@ public: if (dist_squared < dists[max_lights - 1]) { dists[max_lights - 1] = dist_squared; - lights[max_lights - 1] = light.m_uid; + lights[max_lights - 1] = light.m_component; for (int i = max_lights - 1; i > 0 && dists[i - 1] > dists[i]; --i) @@ -2443,7 +2448,7 @@ public: dists[i] = dists[i - 1]; dists[i - 1] = tmp; - ComponentIndex tmp2 = lights[i]; + ComponentHandle tmp2 = lights[i]; lights[i] = lights[i - 1]; lights[i - 1] = tmp2; } @@ -2454,42 +2459,36 @@ public: } - void getPointLights(const Frustum& frustum, - Array& lights) override + void getPointLights(const Frustum& frustum, Array& lights) override { for (int i = 0, ci = m_point_lights.size(); i < ci; ++i) { PointLight& light = m_point_lights[i]; - if (frustum.isSphereInside(m_universe.getPosition(light.m_entity), - light.m_range)) + if (frustum.isSphereInside(m_universe.getPosition(light.m_entity), light.m_range)) { - lights.push(light.m_uid); + lights.push(light.m_component); } } } - Entity getCameraEntity(ComponentIndex camera) const override - { - return m_cameras[camera].entity; - } + Entity getCameraEntity(ComponentHandle camera) const override { return m_cameras[camera.index].entity; } - void setLightCastShadows(ComponentIndex cmp, - bool cast_shadows) override + void setLightCastShadows(ComponentHandle cmp, bool cast_shadows) override { m_point_lights[getPointLightIndex(cmp)].m_cast_shadows = cast_shadows; } - bool getLightCastShadows(ComponentIndex cmp) override + bool getLightCastShadows(ComponentHandle cmp) override { return m_point_lights[getPointLightIndex(cmp)].m_cast_shadows; } - void getPointLightInfluencedGeometry(ComponentIndex light_cmp, + void getPointLightInfluencedGeometry(ComponentHandle light_cmp, const Frustum& frustum, Array& infos) override { @@ -2498,8 +2497,8 @@ public: int light_index = getPointLightIndex(light_cmp); for (int j = 0, cj = m_light_influenced_geometry[light_index].size(); j < cj; ++j) { - ComponentIndex renderable_cmp = m_light_influenced_geometry[light_index][j]; - Renderable& renderable = m_renderables[renderable_cmp]; + ComponentHandle renderable_cmp = m_light_influenced_geometry[light_index][j]; + Renderable& renderable = m_renderables[renderable_cmp.index]; const Sphere& sphere = m_culling_system->getSphere(renderable_cmp); if (frustum.isSphereInside(sphere.m_position, sphere.m_radius)) { @@ -2514,8 +2513,7 @@ public: } - void getPointLightInfluencedGeometry(ComponentIndex light_cmp, - Array& infos) override + void getPointLightInfluencedGeometry(ComponentHandle light_cmp, Array& infos) override { PROFILE_FUNCTION(); @@ -2523,7 +2521,7 @@ public: auto& geoms = m_light_influenced_geometry[light_index]; for (int j = 0, cj = geoms.size(); j < cj; ++j) { - const Renderable& renderable = m_renderables[geoms[j]]; + const Renderable& renderable = m_renderables[geoms[j].index]; for (int k = 0, kc = renderable.model->getMeshCount(); k < kc; ++k) { auto& info = infos.emplace(); @@ -2543,9 +2541,9 @@ public: for (auto& subresults : *results) { - for (ComponentIndex renderable_cmp : subresults) + for (ComponentHandle renderable_cmp : subresults) { - entities.push(m_renderables[renderable_cmp].entity); + entities.push(m_renderables[renderable_cmp.index].entity); } } } @@ -2565,35 +2563,36 @@ public: } - void setCameraSlot(ComponentIndex camera, const char* slot) override + void setCameraSlot(ComponentHandle cmp, const char* slot) override { - copyString(m_cameras[camera].slot, lengthOf(m_cameras[camera].slot), slot); + auto& camera = m_cameras[cmp.index]; + copyString(camera.slot, lengthOf(camera.slot), slot); } - ComponentIndex getCameraComponent(Entity entity) + ComponentHandle getCameraComponent(Entity entity) { for (int i = 0; i < m_cameras.size(); ++i) { - if (m_cameras[i].entity == entity) return i; + if (m_cameras[i].entity == entity) return {i}; } return INVALID_COMPONENT; } - const char* getCameraSlot(ComponentIndex camera) override { return m_cameras[camera].slot; } - float getCameraFOV(ComponentIndex camera) override { return m_cameras[camera].fov; } - void setCameraFOV(ComponentIndex camera, float fov) override { m_cameras[camera].fov = fov; } - void setCameraNearPlane(ComponentIndex camera, float near_plane) override { m_cameras[camera].near = near_plane; } - float getCameraNearPlane(ComponentIndex camera) override { return m_cameras[camera].near; } - void setCameraFarPlane(ComponentIndex camera, float far_plane) override { m_cameras[camera].far = far_plane; } - float getCameraFarPlane(ComponentIndex camera) override { return m_cameras[camera].far; } - float getCameraScreenWidth(ComponentIndex camera) override { return m_cameras[camera].screen_width; } - float getCameraScreenHeight(ComponentIndex camera) override { return m_cameras[camera].screen_height; } + const char* getCameraSlot(ComponentHandle camera) override { return m_cameras[camera.index].slot; } + float getCameraFOV(ComponentHandle camera) override { return m_cameras[camera.index].fov; } + void setCameraFOV(ComponentHandle camera, float fov) override { m_cameras[camera.index].fov = fov; } + void setCameraNearPlane(ComponentHandle camera, float near_plane) override { m_cameras[camera.index].near = near_plane; } + float getCameraNearPlane(ComponentHandle camera) override { return m_cameras[camera.index].near; } + void setCameraFarPlane(ComponentHandle camera, float far_plane) override { m_cameras[camera.index].far = far_plane; } + float getCameraFarPlane(ComponentHandle camera) override { return m_cameras[camera.index].far; } + float getCameraScreenWidth(ComponentHandle camera) override { return m_cameras[camera.index].screen_width; } + float getCameraScreenHeight(ComponentHandle camera) override { return m_cameras[camera.index].screen_height; } - Matrix getCameraProjection(ComponentIndex cmp) override + Matrix getCameraProjection(ComponentHandle cmp) override { - Camera& camera = m_cameras[cmp]; + Camera& camera = m_cameras[cmp.index]; Matrix mtx; float ratio = camera.screen_height > 0 ? camera.screen_width / camera.screen_height : 1; if (camera.is_ortho) @@ -2614,24 +2613,24 @@ public: } - void setCameraScreenSize(ComponentIndex camera, int w, int h) override + void setCameraScreenSize(ComponentHandle camera, int w, int h) override { - m_cameras[camera].screen_width = (float)w; - m_cameras[camera].screen_height = (float)h; - m_cameras[camera].aspect = w / (float)h; + m_cameras[camera.index].screen_width = (float)w; + m_cameras[camera.index].screen_height = (float)h; + m_cameras[camera.index].aspect = w / (float)h; } - Vec2 getCameraScreenSize(ComponentIndex camera) override + Vec2 getCameraScreenSize(ComponentHandle camera) override { - return Vec2(m_cameras[camera].screen_width, m_cameras[camera].screen_height); + return Vec2(m_cameras[camera.index].screen_width, m_cameras[camera.index].screen_height); } - float getCameraOrthoSize(ComponentIndex camera) override { return m_cameras[camera].ortho_size; } - void setCameraOrthoSize(ComponentIndex camera, float value) override { m_cameras[camera].ortho_size = value; } - bool isCameraOrtho(ComponentIndex camera) override { return m_cameras[camera].is_ortho; } - void setCameraOrtho(ComponentIndex camera, bool is_ortho) override { m_cameras[camera].is_ortho = is_ortho; } + float getCameraOrthoSize(ComponentHandle camera) override { return m_cameras[camera.index].ortho_size; } + void setCameraOrthoSize(ComponentHandle camera, float value) override { m_cameras[camera.index].ortho_size = value; } + bool isCameraOrtho(ComponentHandle camera) override { return m_cameras[camera.index].is_ortho; } + void setCameraOrtho(ComponentHandle camera, bool is_ortho) override { m_cameras[camera.index].is_ortho = is_ortho; } const Array& getDebugTriangles() const override { return m_debug_triangles; } @@ -3077,22 +3076,22 @@ public: } - RayCastModelHit castRayTerrain(ComponentIndex terrain, const Vec3& origin, const Vec3& dir) override + RayCastModelHit castRayTerrain(ComponentHandle terrain, const Vec3& origin, const Vec3& dir) override { RayCastModelHit hit; hit.m_is_hit = false; - if (m_terrains[terrain]) + if (m_terrains[terrain.index]) { - hit = m_terrains[terrain]->castRay(origin, dir); + hit = m_terrains[terrain.index]->castRay(origin, dir); hit.m_component = terrain; hit.m_component_type = TERRAIN_TYPE; - hit.m_entity = m_terrains[terrain]->getEntity(); + hit.m_entity = m_terrains[terrain.index]->getEntity(); } return hit; } - RayCastModelHit castRay(const Vec3& origin, const Vec3& dir, ComponentIndex ignored_renderable) override + RayCastModelHit castRay(const Vec3& origin, const Vec3& dir, ComponentHandle ignored_renderable) override { PROFILE_FUNCTION(); RayCastModelHit hit; @@ -3101,7 +3100,7 @@ public: for (int i = 0; i < m_renderables.size(); ++i) { auto& r = m_renderables[i]; - if (ignored_renderable != i && r.model) + if (ignored_renderable.index != i && r.model) { const Vec3& pos = r.matrix.getTranslation(); float radius = r.model->getBoundingRadius(); @@ -3113,7 +3112,7 @@ public: RayCastModelHit new_hit = r.model->castRay(origin, dir, r.matrix); if (new_hit.m_is_hit && (!hit.m_is_hit || new_hit.m_t < hit.m_t)) { - new_hit.m_component = i; + new_hit.m_component = {i}; new_hit.m_entity = r.entity; new_hit.m_component_type = RENDERABLE_TYPE; hit = new_hit; @@ -3129,7 +3128,7 @@ public: RayCastModelHit terrain_hit = m_terrains[i]->castRay(origin, dir); if (terrain_hit.m_is_hit && (!hit.m_is_hit || terrain_hit.m_t < hit.m_t)) { - terrain_hit.m_component = i; + terrain_hit.m_component = {i}; terrain_hit.m_component_type = TERRAIN_TYPE; terrain_hit.m_entity = m_terrains[i]->getEntity(); hit = terrain_hit; @@ -3140,19 +3139,19 @@ public: } - int getPointLightIndex(ComponentIndex cmp) const + int getPointLightIndex(ComponentHandle cmp) const { return m_point_lights_map[cmp]; } - Vec4 getShadowmapCascades(ComponentIndex cmp) override + Vec4 getShadowmapCascades(ComponentHandle cmp) override { return m_global_lights[getGlobalLightIndex(cmp)].m_cascades; } - void setShadowmapCascades(ComponentIndex cmp, const Vec4& value) override + void setShadowmapCascades(ComponentHandle cmp, const Vec4& value) override { Vec4 valid_value = value; valid_value.x = Math::maximum(valid_value.x, 0.02f); @@ -3164,11 +3163,11 @@ public: } - int getGlobalLightIndex(int uid) const + int getGlobalLightIndex(ComponentHandle cmp) const { for (int i = 0; i < m_global_lights.size(); ++i) { - if (m_global_lights[i].m_uid == uid) + if (m_global_lights[i].m_component == cmp) { return i; } @@ -3177,210 +3176,212 @@ public: } - void setFogDensity(ComponentIndex cmp, float density) override + void setFogDensity(ComponentHandle cmp, float density) override { m_global_lights[getGlobalLightIndex(cmp)].m_fog_density = density; } - void setFogColor(ComponentIndex cmp, const Vec3& color) override + void setFogColor(ComponentHandle cmp, const Vec3& color) override { m_global_lights[getGlobalLightIndex(cmp)].m_fog_color = color; } - float getFogDensity(ComponentIndex cmp) override + float getFogDensity(ComponentHandle cmp) override { return m_global_lights[getGlobalLightIndex(cmp)].m_fog_density; } - float getFogBottom(ComponentIndex cmp) override + float getFogBottom(ComponentHandle cmp) override { return m_global_lights[getGlobalLightIndex(cmp)].m_fog_bottom; } - void setFogBottom(ComponentIndex cmp, float bottom) override + void setFogBottom(ComponentHandle cmp, float bottom) override { m_global_lights[getGlobalLightIndex(cmp)].m_fog_bottom = bottom; } - float getFogHeight(ComponentIndex cmp) override + float getFogHeight(ComponentHandle cmp) override { return m_global_lights[getGlobalLightIndex(cmp)].m_fog_height; } - void setFogHeight(ComponentIndex cmp, float height) override + void setFogHeight(ComponentHandle cmp, float height) override { m_global_lights[getGlobalLightIndex(cmp)].m_fog_height = height; } - Vec3 getFogColor(ComponentIndex cmp) override + Vec3 getFogColor(ComponentHandle cmp) override { return m_global_lights[getGlobalLightIndex(cmp)].m_fog_color; } - float getLightAttenuation(ComponentIndex cmp) override + float getLightAttenuation(ComponentHandle cmp) override { return m_point_lights[getPointLightIndex(cmp)].m_attenuation_param; } - void setLightAttenuation(ComponentIndex cmp, + void setLightAttenuation(ComponentHandle cmp, float attenuation) override { int index = getPointLightIndex(cmp); m_point_lights[index].m_attenuation_param = attenuation; } - float getLightRange(ComponentIndex cmp) override + float getLightRange(ComponentHandle cmp) override { return m_point_lights[getPointLightIndex(cmp)].m_range; } - void setLightRange(ComponentIndex cmp, float value) override + void setLightRange(ComponentHandle cmp, float value) override { m_point_lights[getPointLightIndex(cmp)].m_range = value; } - void setPointLightIntensity(ComponentIndex cmp, float intensity) override + void setPointLightIntensity(ComponentHandle cmp, float intensity) override { m_point_lights[getPointLightIndex(cmp)].m_diffuse_intensity = intensity; } - void setGlobalLightIntensity(ComponentIndex cmp, float intensity) override + void setGlobalLightIntensity(ComponentHandle cmp, float intensity) override { m_global_lights[getGlobalLightIndex(cmp)].m_diffuse_intensity = intensity; } - void setPointLightColor(ComponentIndex cmp, const Vec3& color) override + void setPointLightColor(ComponentHandle cmp, const Vec3& color) override { m_point_lights[getPointLightIndex(cmp)].m_diffuse_color = color; } - void setGlobalLightColor(ComponentIndex cmp, const Vec3& color) override + void setGlobalLightColor(ComponentHandle cmp, const Vec3& color) override { m_global_lights[getGlobalLightIndex(cmp)].m_diffuse_color = color; } - void setGlobalLightSpecular(ComponentIndex cmp, const Vec3& color) override + void setGlobalLightSpecular(ComponentHandle cmp, const Vec3& color) override { m_global_lights[getGlobalLightIndex(cmp)].m_specular = color; } - void setGlobalLightSpecularIntensity(ComponentIndex cmp, float intensity) override + void setGlobalLightSpecularIntensity(ComponentHandle cmp, float intensity) override { m_global_lights[getGlobalLightIndex(cmp)].m_specular_intensity = intensity; } - void setLightAmbientIntensity(ComponentIndex cmp, float intensity) override + void setLightAmbientIntensity(ComponentHandle cmp, float intensity) override { m_global_lights[getGlobalLightIndex(cmp)].m_ambient_intensity = intensity; } - void setLightAmbientColor(ComponentIndex cmp, const Vec3& color) override + void setLightAmbientColor(ComponentHandle cmp, const Vec3& color) override { m_global_lights[getGlobalLightIndex(cmp)].m_ambient_color = color; } - float getPointLightIntensity(ComponentIndex cmp) override + float getPointLightIntensity(ComponentHandle cmp) override { return m_point_lights[getPointLightIndex(cmp)].m_diffuse_intensity; } - float getGlobalLightIntensity(ComponentIndex cmp) override + float getGlobalLightIntensity(ComponentHandle cmp) override { return m_global_lights[getGlobalLightIndex(cmp)].m_diffuse_intensity; } - Vec3 getPointLightColor(ComponentIndex cmp) override + Vec3 getPointLightColor(ComponentHandle cmp) override { return m_point_lights[getPointLightIndex(cmp)].m_diffuse_color; } - void setPointLightSpecularColor(ComponentIndex cmp, const Vec3& color) override + void setPointLightSpecularColor(ComponentHandle cmp, const Vec3& color) override { m_point_lights[getPointLightIndex(cmp)].m_specular_color = color; } - Vec3 getPointLightSpecularColor(ComponentIndex cmp) override + Vec3 getPointLightSpecularColor(ComponentHandle cmp) override { return m_point_lights[getPointLightIndex(cmp)].m_specular_color; } - void setPointLightSpecularIntensity(ComponentIndex cmp, float intensity) override + void setPointLightSpecularIntensity(ComponentHandle cmp, float intensity) override { m_point_lights[getPointLightIndex(cmp)].m_specular_intensity = intensity; } - float getPointLightSpecularIntensity(ComponentIndex cmp) override + float getPointLightSpecularIntensity(ComponentHandle cmp) override { return m_point_lights[getPointLightIndex(cmp)].m_specular_intensity; } - Vec3 getGlobalLightColor(ComponentIndex cmp) override + Vec3 getGlobalLightColor(ComponentHandle cmp) override { return m_global_lights[getGlobalLightIndex(cmp)].m_diffuse_color; } - Vec3 getGlobalLightSpecular(ComponentIndex cmp) override + Vec3 getGlobalLightSpecular(ComponentHandle cmp) override { return m_global_lights[getGlobalLightIndex(cmp)].m_specular; } - float getGlobalLightSpecularIntensity(ComponentIndex cmp) override + float getGlobalLightSpecularIntensity(ComponentHandle cmp) override { return m_global_lights[getGlobalLightIndex(cmp)].m_specular_intensity; } - float getLightAmbientIntensity(ComponentIndex cmp) override + float getLightAmbientIntensity(ComponentHandle cmp) override { return m_global_lights[getGlobalLightIndex(cmp)].m_ambient_intensity; } - Vec3 getLightAmbientColor(ComponentIndex cmp) override + Vec3 getLightAmbientColor(ComponentHandle cmp) override { return m_global_lights[getGlobalLightIndex(cmp)].m_ambient_color; } - void setActiveGlobalLight(ComponentIndex cmp) override + void setActiveGlobalLight(ComponentHandle cmp) override { - m_active_global_light_uid = cmp; + m_active_global_light_cmp = cmp; } - ComponentIndex getActiveGlobalLight() override + ComponentHandle getActiveGlobalLight() override { - return m_active_global_light_uid; + return m_active_global_light_cmp; }; - Entity getPointLightEntity(ComponentIndex cmp) const override + Entity getPointLightEntity(ComponentHandle cmp) const override { return m_point_lights[getPointLightIndex(cmp)].m_entity; } - Entity getGlobalLightEntity(ComponentIndex cmp) const override + Entity getGlobalLightEntity(ComponentHandle cmp) const override { return m_global_lights[getGlobalLightIndex(cmp)].m_entity; } - void reloadEnvironmentProbe(ComponentIndex cmp) override + void reloadEnvironmentProbe(ComponentHandle cmp) override { - auto& probe = m_environment_probes[{cmp}]; + Entity entity = {cmp.index}; + auto& probe = m_environment_probes[entity]; auto* texture_manager = m_engine.getResourceManager().get(TEXTURE_HASH); if (probe.texture) texture_manager->unload(*probe.texture); uint64 universe_guid = m_universe.getPath().getHash(); - StaticString path("universes/", universe_guid, "/probes/", cmp, ".dds"); + StaticString path("universes/", universe_guid, "/probes/", cmp.index, ".dds"); probe.texture = static_cast(texture_manager->load(Path(path))); } - Texture* getEnvironmentProbeTexture(ComponentIndex cmp) const + Texture* getEnvironmentProbeTexture(ComponentHandle cmp) const { - return m_environment_probes[{cmp}].texture; + Entity entity = { cmp.index }; + return m_environment_probes[entity].texture; } - ComponentIndex getCameraInSlot(const char* slot) override + ComponentHandle getCameraInSlot(const char* slot) override { for (int i = 0, c = m_cameras.size(); i < c; ++i) { if (!m_cameras[i].is_free && equalStrings(m_cameras[i].slot, slot)) { - return i; + return {i}; } } return INVALID_COMPONENT; @@ -3389,9 +3390,9 @@ public: float getTime() const override { return m_time; } - void modelUnloaded(Model*, ComponentIndex component) + void modelUnloaded(Model*, ComponentHandle component) { - auto& r = m_renderables[component]; + auto& r = m_renderables[component.index]; if (!r.custom_meshes) { r.meshes = nullptr; @@ -3423,12 +3424,12 @@ public: } - void modelLoaded(Model* model, ComponentIndex component) + void modelLoaded(Model* model, ComponentHandle component) { auto& rm = m_engine.getResourceManager(); auto* material_manager = static_cast(rm.get(MATERIAL_HASH)); - auto& r = m_renderables[component]; + auto& r = m_renderables[component.index]; float bounding_radius = r.model->getBoundingRadius(); float scale = m_universe.getScale(r.entity); Sphere sphere(r.matrix.getTranslation(), bounding_radius * scale); @@ -3489,7 +3490,7 @@ public: { if (m_renderables[i].entity != INVALID_ENTITY && m_renderables[i].model == model) { - modelUnloaded(model, i); + modelUnloaded(model, {i}); } } } @@ -3501,7 +3502,7 @@ public: { if (m_renderables[i].entity != INVALID_ENTITY && m_renderables[i].model == model) { - modelLoaded(model, i); + modelLoaded(model, {i}); } } @@ -3575,9 +3576,9 @@ public: } - void setRenderableMaterial(ComponentIndex cmp, int index, const Path& path) override + void setRenderableMaterial(ComponentHandle cmp, int index, const Path& path) override { - auto& r = m_renderables[cmp]; + auto& r = m_renderables[cmp.index]; if (r.meshes && r.mesh_count > index && path == r.meshes[index].material->getPath()) return; auto& rm = r.model->getResourceManager(); @@ -3593,20 +3594,20 @@ public: } - Path getRenderableMaterial(ComponentIndex cmp, int index) override + Path getRenderableMaterial(ComponentHandle cmp, int index) override { - auto& r = m_renderables[cmp]; + auto& r = m_renderables[cmp.index]; if (!r.meshes) return Path(""); return r.meshes[index].material->getPath(); } - void setModel(ComponentIndex component, Model* model) + void setModel(ComponentHandle component, Model* model) { - ASSERT(m_renderables[component].entity != INVALID_ENTITY); - - Model* old_model = m_renderables[component].model; + auto& renderable = m_renderables[component.index]; + ASSERT(isValid(renderable.entity)); + Model* old_model = renderable.model; bool no_change = model == old_model && old_model; if (no_change) { @@ -3617,7 +3618,7 @@ public: { auto& rm = old_model->getResourceManager(); auto* material_manager = static_cast(rm.get(MATERIAL_HASH)); - freeCustomMeshes(m_renderables[component], material_manager); + freeCustomMeshes(renderable, material_manager); int callback_idx = getModelLoadedCallback(old_model); ModelLoadedCallback* callback = m_model_loaded_callbacks[callback_idx]; --callback->m_ref_count; @@ -3633,11 +3634,11 @@ public: } old_model->getResourceManager().get(MODEL_HASH)->unload(*old_model); } - m_renderables[component].model = model; - m_renderables[component].meshes = nullptr; - m_renderables[component].mesh_count = 0; - LUMIX_DELETE(m_allocator, m_renderables[component].pose); - m_renderables[component].pose = nullptr; + renderable.model = model; + renderable.meshes = nullptr; + renderable.mesh_count = 0; + LUMIX_DELETE(m_allocator, renderable.pose); + renderable.pose = nullptr; if (model) { ModelLoadedCallback* callback = m_model_loaded_callbacks[getModelLoadedCallback(model)]; @@ -3653,22 +3654,19 @@ public: IAllocator& getAllocator() override { return m_allocator; } - void detectLightInfluencedGeometry(int light_index) + void detectLightInfluencedGeometry(ComponentHandle cmp) { if (!m_is_forward_rendered) return; - Frustum frustum = getPointLightFrustum(light_index); + Frustum frustum = getPointLightFrustum(cmp); m_culling_system->cullToFrustum(frustum, 0xffffFFFF); - const CullingSystem::Results& results = - m_culling_system->getResult(); - Array& influenced_geometry = - m_light_influenced_geometry[light_index]; + const CullingSystem::Results& results = m_culling_system->getResult(); + auto& influenced_geometry = m_light_influenced_geometry[cmp.index]; influenced_geometry.clear(); for (int i = 0; i < results.size(); ++i) { const CullingSystem::Subresults& subresult = results[i]; - influenced_geometry.reserve(influenced_geometry.size() + - subresult.size()); + influenced_geometry.reserve(influenced_geometry.size() + subresult.size()); for (int j = 0, c = subresult.size(); j < c; ++j) { influenced_geometry.push(subresult[j]); @@ -3677,14 +3675,14 @@ public: } - int getParticleEmitterAttractorCount(ComponentIndex cmp) override + int getParticleEmitterAttractorCount(ComponentHandle cmp) override { auto* module = getEmitterModule(cmp); return module ? module->m_count : 0; } - void addParticleEmitterAttractor(ComponentIndex cmp, int index) override + void addParticleEmitterAttractor(ComponentHandle cmp, int index) override { auto* module = getEmitterModule(cmp); if (!module) return; @@ -3708,7 +3706,7 @@ public: } - void removeParticleEmitterAttractor(ComponentIndex cmp, int index) override + void removeParticleEmitterAttractor(ComponentHandle cmp, int index) override { auto* module = getEmitterModule(cmp); if (!module) return; @@ -3721,42 +3719,42 @@ public: } - Entity getParticleEmitterAttractorEntity(ComponentIndex cmp, int index) override + Entity getParticleEmitterAttractorEntity(ComponentHandle cmp, int index) override { auto* module = getEmitterModule(cmp); return module ? module->m_entities[index] : INVALID_ENTITY; } - void setParticleEmitterAttractorEntity(ComponentIndex cmp, int index, Entity entity) override + void setParticleEmitterAttractorEntity(ComponentHandle cmp, int index, Entity entity) override { auto* module = getEmitterModule(cmp); if(module) module->m_entities[index] = entity; } - float getParticleEmitterShapeRadius(ComponentIndex cmp) override + float getParticleEmitterShapeRadius(ComponentHandle cmp) override { auto* module = getEmitterModule(cmp); return module ? module->m_radius : 0.0f; } - void setParticleEmitterShapeRadius(ComponentIndex cmp, float value) override + void setParticleEmitterShapeRadius(ComponentHandle cmp, float value) override { auto* module = getEmitterModule(cmp); if (module) module->m_radius = value; } - int getParticleEmitterPlaneCount(ComponentIndex cmp) override + int getParticleEmitterPlaneCount(ComponentHandle cmp) override { auto* module = getEmitterModule(cmp); return module ? module->m_count : 0; } - void addParticleEmitterPlane(ComponentIndex cmp, int index) override + void addParticleEmitterPlane(ComponentHandle cmp, int index) override { auto* plane_module = getEmitterModule(cmp); if (!plane_module) return; @@ -3780,7 +3778,7 @@ public: } - void removeParticleEmitterPlane(ComponentIndex cmp, int index) override + void removeParticleEmitterPlane(ComponentHandle cmp, int index) override { auto* plane_module = getEmitterModule(cmp); if (!plane_module) return; @@ -3793,45 +3791,45 @@ public: } - Entity getParticleEmitterPlaneEntity(ComponentIndex cmp, int index) override + Entity getParticleEmitterPlaneEntity(ComponentHandle cmp, int index) override { auto* module = getEmitterModule(cmp); return module ? module->m_entities[index] : INVALID_ENTITY; } - void setParticleEmitterPlaneEntity(ComponentIndex cmp, int index, Entity entity) override + void setParticleEmitterPlaneEntity(ComponentHandle cmp, int index, Entity entity) override { auto* module = getEmitterModule(cmp); if (module) module->m_entities[index] = entity; } - DelegateList& renderableCreated() override + DelegateList& renderableCreated() override { return m_renderable_created; } - DelegateList& renderableDestroyed() override + DelegateList& renderableDestroyed() override { return m_renderable_destroyed; } - float getLightFOV(ComponentIndex cmp) override + float getLightFOV(ComponentHandle cmp) override { return m_point_lights[getPointLightIndex(cmp)].m_fov; } - void setLightFOV(ComponentIndex cmp, float fov) override + void setLightFOV(ComponentHandle cmp, float fov) override { m_point_lights[getPointLightIndex(cmp)].m_fov = fov; } - ComponentIndex createGlobalLight(Entity entity) + ComponentHandle createGlobalLight(Entity entity) { GlobalLight& light = m_global_lights.emplace(); light.m_entity = entity; @@ -3841,7 +3839,8 @@ public: light.m_ambient_intensity = 1; light.m_fog_color.set(1, 1, 1); light.m_fog_density = 0; - light.m_uid = ++m_global_light_last_uid; + ++m_global_light_last_cmp.index; + light.m_component = {m_global_light_last_cmp}; light.m_cascades.set(3, 8, 100, 300); light.m_fog_bottom = 0.0f; light.m_fog_height = 10.0f; @@ -3850,63 +3849,66 @@ public: if (m_global_lights.size() == 1) { - m_active_global_light_uid = light.m_uid; + m_active_global_light_cmp = light.m_component; } - m_universe.addComponent(entity, GLOBAL_LIGHT_TYPE, this, light.m_uid); - return light.m_uid; + m_universe.addComponent(entity, GLOBAL_LIGHT_TYPE, this, light.m_component); + return light.m_component; } - ComponentIndex createPointLight(Entity entity) + ComponentHandle createPointLight(Entity entity) { PointLight& light = m_point_lights.emplace(); - m_light_influenced_geometry.push(Array(m_allocator)); + m_light_influenced_geometry.emplace(m_allocator); light.m_entity = entity; light.m_diffuse_color.set(1, 1, 1); light.m_diffuse_intensity = 1; - light.m_uid = ++m_point_light_last_uid; + ++m_point_light_last_cmp.index; + light.m_component = {m_point_light_last_cmp}; light.m_fov = 999; light.m_specular_color.set(1, 1, 1); light.m_specular_intensity = 1; light.m_cast_shadows = false; light.m_attenuation_param = 2; light.m_range = 10; - m_point_lights_map.insert(light.m_uid, m_point_lights.size() - 1); + m_point_lights_map.insert(light.m_component, m_point_lights.size() - 1); - m_universe.addComponent(entity, POINT_LIGHT_TYPE, this, light.m_uid); + m_universe.addComponent(entity, POINT_LIGHT_TYPE, this, light.m_component); - detectLightInfluencedGeometry(m_point_lights.size() - 1); + detectLightInfluencedGeometry(light.m_component); - return light.m_uid; + return light.m_component; } - ComponentIndex createEnvironmentProbe(Entity entity) + ComponentHandle createEnvironmentProbe(Entity entity) { EnvironmentProbe probe; auto* texture_manager = m_engine.getResourceManager().get(TEXTURE_HASH); probe.texture = static_cast(texture_manager->load(Path("models/editor/default_probe.dds"))); m_environment_probes.insert(entity, probe); - m_universe.addComponent(entity, ENVIRONMENT_PROBE_TYPE, this, entity.index); - return entity.index; + ComponentHandle cmp = {entity.index}; + m_universe.addComponent(entity, ENVIRONMENT_PROBE_TYPE, this, cmp); + return cmp; } - ComponentIndex createBoneAttachment(Entity entity) + ComponentHandle createBoneAttachment(Entity entity) { BoneAttachment& attachment = m_bone_attachments.emplace(); attachment.entity = entity; attachment.parent_entity = INVALID_ENTITY; attachment.bone_index = -1; - m_universe.addComponent(entity, BONE_ATTACHMENT_TYPE, this, entity.index); - return entity.index; + ComponentHandle cmp = { entity.index }; + m_universe.addComponent(entity, BONE_ATTACHMENT_TYPE, this, cmp); + return cmp; } - ComponentIndex createRenderable(Entity entity) + ComponentHandle createRenderable(Entity entity) { while(entity.index >= m_renderables.size()) { @@ -3924,25 +3926,26 @@ public: r.custom_meshes = false; r.mesh_count = 0; r.matrix = m_universe.getMatrix(entity); - m_universe.addComponent(entity, RENDERABLE_TYPE, this, entity.index); - m_renderable_created.invoke(m_renderables.size() - 1); - return entity.index; + ComponentHandle cmp = {entity.index}; + m_universe.addComponent(entity, RENDERABLE_TYPE, this, cmp); + m_renderable_created.invoke(cmp); + return cmp; } - void setParticleEmitterMaterialPath(ComponentIndex cmp, const Path& path) override + void setParticleEmitterMaterialPath(ComponentHandle cmp, const Path& path) override { - if (!m_particle_emitters[cmp]) return; + if (!m_particle_emitters[cmp.index]) return; auto* manager = m_engine.getResourceManager().get(MATERIAL_HASH); Material* material = static_cast(manager->load(path)); - m_particle_emitters[cmp]->setMaterial(material); + m_particle_emitters[cmp.index]->setMaterial(material); } - Path getParticleEmitterMaterialPath(ComponentIndex cmp) override + Path getParticleEmitterMaterialPath(ComponentHandle cmp) override { - ParticleEmitter* emitter = m_particle_emitters[cmp]; + ParticleEmitter* emitter = m_particle_emitters[cmp.index]; if (!emitter) return Path(""); if (!emitter->getMaterial()) return Path(""); @@ -3962,12 +3965,12 @@ private: Array m_renderables; - int m_point_light_last_uid; + ComponentHandle m_point_light_last_cmp; Array m_point_lights; - HashMap m_point_lights_map; - Array> m_light_influenced_geometry; - int m_active_global_light_uid; - int m_global_light_last_uid; + HashMap m_point_lights_map; + Array> m_light_influenced_geometry; + ComponentHandle m_active_global_light_cmp; + ComponentHandle m_global_light_last_cmp; Array m_global_lights; Array m_cameras; Array m_bone_attachments; @@ -3989,8 +3992,8 @@ private: bool m_is_forward_rendered; bool m_is_grass_enabled; bool m_is_game_running; - DelegateList m_renderable_created; - DelegateList m_renderable_destroyed; + DelegateList m_renderable_created; + DelegateList m_renderable_destroyed; }; @@ -3998,8 +4001,8 @@ private: static struct { ComponentType type; - ComponentIndex(RenderSceneImpl::*creator)(Entity); - void (RenderSceneImpl::*destroyer)(ComponentIndex); + ComponentHandle(RenderSceneImpl::*creator)(Entity); + void (RenderSceneImpl::*destroyer)(ComponentHandle); } COMPONENT_INFOS[] = {{RENDERABLE_TYPE, &RenderSceneImpl::createRenderable, &RenderSceneImpl::destroyRenderable}, {GLOBAL_LIGHT_TYPE, &RenderSceneImpl::createGlobalLight, &RenderSceneImpl::destroyGlobalLight}, {POINT_LIGHT_TYPE, &RenderSceneImpl::createPointLight, &RenderSceneImpl::destroyPointLight}, @@ -4045,7 +4048,7 @@ bool RenderSceneImpl::ownComponentType(ComponentType type) const } -ComponentIndex RenderSceneImpl::createComponent(ComponentType type, Entity entity) +ComponentHandle RenderSceneImpl::createComponent(ComponentType type, Entity entity) { for (auto& i : COMPONENT_INFOS) { @@ -4059,7 +4062,7 @@ ComponentIndex RenderSceneImpl::createComponent(ComponentType type, Entity entit } -void RenderSceneImpl::destroyComponent(ComponentIndex component, ComponentType type) +void RenderSceneImpl::destroyComponent(ComponentHandle component, ComponentType type) { for (auto& i : COMPONENT_INFOS) { diff --git a/src/renderer/render_scene.h b/src/renderer/render_scene.h index 860732068..1561c835a 100644 --- a/src/renderer/render_scene.h +++ b/src/renderer/render_scene.h @@ -81,7 +81,7 @@ struct Renderable struct RenderableMesh { - ComponentIndex renderable; + ComponentHandle renderable; Mesh* mesh; }; @@ -142,24 +142,24 @@ public: static void destroyInstance(RenderScene* scene); static void registerLuaAPI(lua_State* L); - virtual RayCastModelHit castRay(const Vec3& origin, const Vec3& dir, ComponentIndex ignore) = 0; + virtual RayCastModelHit castRay(const Vec3& origin, const Vec3& dir, ComponentHandle ignore) = 0; - virtual RayCastModelHit castRayTerrain(ComponentIndex terrain, + virtual RayCastModelHit castRayTerrain(ComponentHandle terrain, const Vec3& origin, const Vec3& dir) = 0; - virtual void getRay(ComponentIndex camera, float x, float y, Vec3& origin, Vec3& dir) = 0; + virtual void getRay(ComponentHandle camera, float x, float y, Vec3& origin, Vec3& dir) = 0; - virtual Frustum getCameraFrustum(ComponentIndex camera) const = 0; + virtual Frustum getCameraFrustum(ComponentHandle camera) const = 0; virtual float getTime() const = 0; virtual Engine& getEngine() const = 0; virtual IAllocator& getAllocator() = 0; - virtual Pose* getPose(ComponentIndex cmp) = 0; - virtual ComponentIndex getActiveGlobalLight() = 0; - virtual void setActiveGlobalLight(ComponentIndex cmp) = 0; - virtual Vec4 getShadowmapCascades(ComponentIndex cmp) = 0; - virtual void setShadowmapCascades(ComponentIndex cmp, const Vec4& value) = 0; + virtual Pose* getPose(ComponentHandle cmp) = 0; + virtual ComponentHandle getActiveGlobalLight() = 0; + virtual void setActiveGlobalLight(ComponentHandle cmp) = 0; + virtual Vec4 getShadowmapCascades(ComponentHandle cmp) = 0; + virtual void setShadowmapCascades(ComponentHandle cmp, const Vec4& value) = 0; virtual void addDebugTriangle(const Vec3& p0, const Vec3& p1, @@ -208,192 +208,192 @@ public: uint32 color, float life) = 0; - virtual Entity getBoneAttachmentParent(ComponentIndex cmp) = 0; - virtual void setBoneAttachmentParent(ComponentIndex cmp, Entity entity) = 0; - virtual void setBoneAttachmentBone(ComponentIndex cmp, int value) = 0; - virtual int getBoneAttachmentBone(ComponentIndex cmp) = 0; - virtual Vec3 getBoneAttachmentPosition(ComponentIndex cmp) = 0; - virtual void setBoneAttachmentPosition(ComponentIndex cmp, const Vec3& pos) = 0; + virtual Entity getBoneAttachmentParent(ComponentHandle cmp) = 0; + virtual void setBoneAttachmentParent(ComponentHandle cmp, Entity entity) = 0; + virtual void setBoneAttachmentBone(ComponentHandle cmp, int value) = 0; + virtual int getBoneAttachmentBone(ComponentHandle cmp) = 0; + virtual Vec3 getBoneAttachmentPosition(ComponentHandle cmp) = 0; + virtual void setBoneAttachmentPosition(ComponentHandle cmp, const Vec3& pos) = 0; virtual const Array& getDebugTriangles() const = 0; virtual const Array& getDebugLines() const = 0; virtual const Array& getDebugPoints() const = 0; - virtual Matrix getCameraProjection(ComponentIndex camera) = 0; - virtual Entity getCameraEntity(ComponentIndex camera) const = 0; - virtual ComponentIndex getCameraInSlot(const char* slot) = 0; - virtual float getCameraFOV(ComponentIndex camera) = 0; - virtual void setCameraFOV(ComponentIndex camera, float fov) = 0; - virtual void setCameraFarPlane(ComponentIndex camera, float far) = 0; - virtual void setCameraNearPlane(ComponentIndex camera, float near) = 0; - virtual float getCameraFarPlane(ComponentIndex camera) = 0; - virtual float getCameraNearPlane(ComponentIndex camera) = 0; - virtual float getCameraScreenWidth(ComponentIndex camera) = 0; - virtual float getCameraScreenHeight(ComponentIndex camera) = 0; - virtual void setCameraSlot(ComponentIndex camera, const char* slot) = 0; - virtual const char* getCameraSlot(ComponentIndex camera) = 0; - virtual void setCameraScreenSize(ComponentIndex camera, int w, int h) = 0; - virtual bool isCameraOrtho(ComponentIndex camera) = 0; - virtual void setCameraOrtho(ComponentIndex camera, bool is_ortho) = 0; - virtual float getCameraOrthoSize(ComponentIndex camera) = 0; - virtual void setCameraOrthoSize(ComponentIndex camera, float value) = 0; - virtual Vec2 getCameraScreenSize(ComponentIndex camera) = 0; + virtual Matrix getCameraProjection(ComponentHandle camera) = 0; + virtual Entity getCameraEntity(ComponentHandle camera) const = 0; + virtual ComponentHandle getCameraInSlot(const char* slot) = 0; + virtual float getCameraFOV(ComponentHandle camera) = 0; + virtual void setCameraFOV(ComponentHandle camera, float fov) = 0; + virtual void setCameraFarPlane(ComponentHandle camera, float far) = 0; + virtual void setCameraNearPlane(ComponentHandle camera, float near) = 0; + virtual float getCameraFarPlane(ComponentHandle camera) = 0; + virtual float getCameraNearPlane(ComponentHandle camera) = 0; + virtual float getCameraScreenWidth(ComponentHandle camera) = 0; + virtual float getCameraScreenHeight(ComponentHandle camera) = 0; + virtual void setCameraSlot(ComponentHandle camera, const char* slot) = 0; + virtual const char* getCameraSlot(ComponentHandle camera) = 0; + virtual void setCameraScreenSize(ComponentHandle camera, int w, int h) = 0; + virtual bool isCameraOrtho(ComponentHandle camera) = 0; + virtual void setCameraOrtho(ComponentHandle camera, bool is_ortho) = 0; + virtual float getCameraOrthoSize(ComponentHandle camera) = 0; + virtual void setCameraOrthoSize(ComponentHandle camera, float value) = 0; + virtual Vec2 getCameraScreenSize(ComponentHandle camera) = 0; - virtual class ParticleEmitter* getParticleEmitter(ComponentIndex cmp) = 0; - virtual void resetParticleEmitter(ComponentIndex cmp) = 0; - virtual void updateEmitter(ComponentIndex cmp, float time_delta) = 0; + virtual class ParticleEmitter* getParticleEmitter(ComponentHandle cmp) = 0; + virtual void resetParticleEmitter(ComponentHandle cmp) = 0; + virtual void updateEmitter(ComponentHandle cmp, float time_delta) = 0; virtual const Array& getParticleEmitters() const = 0; - virtual const Vec2* getParticleEmitterAlpha(ComponentIndex cmp) = 0; - virtual int getParticleEmitterAlphaCount(ComponentIndex cmp) = 0; - virtual const Vec2* getParticleEmitterSize(ComponentIndex cmp) = 0; - virtual int getParticleEmitterSizeCount(ComponentIndex cmp) = 0; - virtual Vec3 getParticleEmitterAcceleration(ComponentIndex cmp) = 0; - virtual Vec2 getParticleEmitterLinearMovementX(ComponentIndex cmp) = 0; - virtual Vec2 getParticleEmitterLinearMovementY(ComponentIndex cmp) = 0; - virtual Vec2 getParticleEmitterLinearMovementZ(ComponentIndex cmp) = 0; - virtual Vec2 getParticleEmitterInitialLife(ComponentIndex cmp) = 0; - virtual Int2 getParticleEmitterSpawnCount(ComponentIndex cmp) = 0; - virtual Vec2 getParticleEmitterSpawnPeriod(ComponentIndex cmp) = 0; - virtual Vec2 getParticleEmitterInitialSize(ComponentIndex cmp) = 0; - virtual void setParticleEmitterAlpha(ComponentIndex cmp, const Vec2* value, int count) = 0; - virtual void setParticleEmitterSize(ComponentIndex cmp, const Vec2* values, int count) = 0; - virtual void setParticleEmitterAcceleration(ComponentIndex cmp, const Vec3& value) = 0; - virtual void setParticleEmitterLinearMovementX(ComponentIndex cmp, const Vec2& value) = 0; - virtual void setParticleEmitterLinearMovementY(ComponentIndex cmp, const Vec2& value) = 0; - virtual void setParticleEmitterLinearMovementZ(ComponentIndex cmp, const Vec2& value) = 0; - virtual void setParticleEmitterInitialLife(ComponentIndex cmp, const Vec2& value) = 0; - virtual void setParticleEmitterSpawnCount(ComponentIndex cmp, const Int2& value) = 0; - virtual void setParticleEmitterSpawnPeriod(ComponentIndex cmp, const Vec2& value) = 0; - virtual void setParticleEmitterInitialSize(ComponentIndex cmp, const Vec2& value) = 0; - virtual void setParticleEmitterMaterialPath(ComponentIndex cmp, const Path& path) = 0; - virtual Path getParticleEmitterMaterialPath(ComponentIndex cmp) = 0; - virtual int getParticleEmitterPlaneCount(ComponentIndex cmp) = 0; - virtual void addParticleEmitterPlane(ComponentIndex cmp, int index) = 0; - virtual void removeParticleEmitterPlane(ComponentIndex cmp, int index) = 0; - virtual Entity getParticleEmitterPlaneEntity(ComponentIndex cmp, int index) = 0; - virtual void setParticleEmitterPlaneEntity(ComponentIndex cmp, int index, Entity entity) = 0; - virtual float getParticleEmitterPlaneBounce(ComponentIndex cmp) = 0; - virtual void setParticleEmitterPlaneBounce(ComponentIndex cmp, float value) = 0; - virtual float getParticleEmitterShapeRadius(ComponentIndex cmp) = 0; - virtual void setParticleEmitterShapeRadius(ComponentIndex cmp, float value) = 0; + virtual const Vec2* getParticleEmitterAlpha(ComponentHandle cmp) = 0; + virtual int getParticleEmitterAlphaCount(ComponentHandle cmp) = 0; + virtual const Vec2* getParticleEmitterSize(ComponentHandle cmp) = 0; + virtual int getParticleEmitterSizeCount(ComponentHandle cmp) = 0; + virtual Vec3 getParticleEmitterAcceleration(ComponentHandle cmp) = 0; + virtual Vec2 getParticleEmitterLinearMovementX(ComponentHandle cmp) = 0; + virtual Vec2 getParticleEmitterLinearMovementY(ComponentHandle cmp) = 0; + virtual Vec2 getParticleEmitterLinearMovementZ(ComponentHandle cmp) = 0; + virtual Vec2 getParticleEmitterInitialLife(ComponentHandle cmp) = 0; + virtual Int2 getParticleEmitterSpawnCount(ComponentHandle cmp) = 0; + virtual Vec2 getParticleEmitterSpawnPeriod(ComponentHandle cmp) = 0; + virtual Vec2 getParticleEmitterInitialSize(ComponentHandle cmp) = 0; + virtual void setParticleEmitterAlpha(ComponentHandle cmp, const Vec2* value, int count) = 0; + virtual void setParticleEmitterSize(ComponentHandle cmp, const Vec2* values, int count) = 0; + virtual void setParticleEmitterAcceleration(ComponentHandle cmp, const Vec3& value) = 0; + virtual void setParticleEmitterLinearMovementX(ComponentHandle cmp, const Vec2& value) = 0; + virtual void setParticleEmitterLinearMovementY(ComponentHandle cmp, const Vec2& value) = 0; + virtual void setParticleEmitterLinearMovementZ(ComponentHandle cmp, const Vec2& value) = 0; + virtual void setParticleEmitterInitialLife(ComponentHandle cmp, const Vec2& value) = 0; + virtual void setParticleEmitterSpawnCount(ComponentHandle cmp, const Int2& value) = 0; + virtual void setParticleEmitterSpawnPeriod(ComponentHandle cmp, const Vec2& value) = 0; + virtual void setParticleEmitterInitialSize(ComponentHandle cmp, const Vec2& value) = 0; + virtual void setParticleEmitterMaterialPath(ComponentHandle cmp, const Path& path) = 0; + virtual Path getParticleEmitterMaterialPath(ComponentHandle cmp) = 0; + virtual int getParticleEmitterPlaneCount(ComponentHandle cmp) = 0; + virtual void addParticleEmitterPlane(ComponentHandle cmp, int index) = 0; + virtual void removeParticleEmitterPlane(ComponentHandle cmp, int index) = 0; + virtual Entity getParticleEmitterPlaneEntity(ComponentHandle cmp, int index) = 0; + virtual void setParticleEmitterPlaneEntity(ComponentHandle cmp, int index, Entity entity) = 0; + virtual float getParticleEmitterPlaneBounce(ComponentHandle cmp) = 0; + virtual void setParticleEmitterPlaneBounce(ComponentHandle cmp, float value) = 0; + virtual float getParticleEmitterShapeRadius(ComponentHandle cmp) = 0; + virtual void setParticleEmitterShapeRadius(ComponentHandle cmp, float value) = 0; - virtual int getParticleEmitterAttractorCount(ComponentIndex cmp) = 0; - virtual void addParticleEmitterAttractor(ComponentIndex cmp, int index) = 0; - virtual void removeParticleEmitterAttractor(ComponentIndex cmp, int index) = 0; - virtual Entity getParticleEmitterAttractorEntity(ComponentIndex cmp, int index) = 0; - virtual void setParticleEmitterAttractorEntity(ComponentIndex cmp, + virtual int getParticleEmitterAttractorCount(ComponentHandle cmp) = 0; + virtual void addParticleEmitterAttractor(ComponentHandle cmp, int index) = 0; + virtual void removeParticleEmitterAttractor(ComponentHandle cmp, int index) = 0; + virtual Entity getParticleEmitterAttractorEntity(ComponentHandle cmp, int index) = 0; + virtual void setParticleEmitterAttractorEntity(ComponentHandle cmp, int index, Entity entity) = 0; - virtual float getParticleEmitterAttractorForce(ComponentIndex cmp) = 0; - virtual void setParticleEmitterAttractorForce(ComponentIndex cmp, float value) = 0; + virtual float getParticleEmitterAttractorForce(ComponentHandle cmp) = 0; + virtual void setParticleEmitterAttractorForce(ComponentHandle cmp, float value) = 0; - virtual DelegateList& renderableCreated() = 0; - virtual DelegateList& renderableDestroyed() = 0; - virtual void showRenderable(ComponentIndex cmp) = 0; - virtual void hideRenderable(ComponentIndex cmp) = 0; - virtual ComponentIndex getRenderableComponent(Entity entity) = 0; - virtual Renderable* getRenderable(ComponentIndex cmp) = 0; + virtual DelegateList& renderableCreated() = 0; + virtual DelegateList& renderableDestroyed() = 0; + virtual void showRenderable(ComponentHandle cmp) = 0; + virtual void hideRenderable(ComponentHandle cmp) = 0; + virtual ComponentHandle getRenderableComponent(Entity entity) = 0; + virtual Renderable* getRenderable(ComponentHandle cmp) = 0; virtual Renderable* getRenderables() = 0; - virtual Path getRenderablePath(ComponentIndex cmp) = 0; - virtual void setRenderableMaterial(ComponentIndex cmp, int index, const Path& path) = 0; - virtual Path getRenderableMaterial(ComponentIndex cmp, int index) = 0; - virtual int getRenderableMaterialsCount(ComponentIndex cmp) = 0; - virtual void setRenderableLayer(ComponentIndex cmp, const int32& layer) = 0; - virtual void setRenderablePath(ComponentIndex cmp, const Path& path) = 0; + virtual Path getRenderablePath(ComponentHandle cmp) = 0; + virtual void setRenderableMaterial(ComponentHandle cmp, int index, const Path& path) = 0; + virtual Path getRenderableMaterial(ComponentHandle cmp, int index) = 0; + virtual int getRenderableMaterialsCount(ComponentHandle cmp) = 0; + virtual void setRenderableLayer(ComponentHandle cmp, const int32& layer) = 0; + virtual void setRenderablePath(ComponentHandle cmp, const Path& path) = 0; virtual Array>& getRenderableInfos(const Frustum& frustum, const Vec3& lod_ref_point) = 0; virtual void getRenderableEntities(const Frustum& frustum, Array& entities) = 0; - virtual Entity getRenderableEntity(ComponentIndex cmp) = 0; - virtual ComponentIndex getFirstRenderable() = 0; - virtual ComponentIndex getNextRenderable(ComponentIndex cmp) = 0; - virtual Model* getRenderableModel(ComponentIndex cmp) = 0; + virtual Entity getRenderableEntity(ComponentHandle cmp) = 0; + virtual ComponentHandle getFirstRenderable() = 0; + virtual ComponentHandle getNextRenderable(ComponentHandle cmp) = 0; + virtual Model* getRenderableModel(ComponentHandle cmp) = 0; virtual void getGrassInfos(const Frustum& frustum, Array& infos, - ComponentIndex camera) = 0; - virtual void forceGrassUpdate(ComponentIndex cmp) = 0; + ComponentHandle camera) = 0; + virtual void forceGrassUpdate(ComponentHandle cmp) = 0; virtual void getTerrainInfos(Array& infos, const Vec3& camera_pos, LIFOAllocator& allocator) = 0; - virtual float getTerrainHeightAt(ComponentIndex cmp, float x, float z) = 0; - virtual Vec3 getTerrainNormalAt(ComponentIndex cmp, float x, float z) = 0; - virtual void setTerrainMaterialPath(ComponentIndex cmp, const Path& path) = 0; - virtual Path getTerrainMaterialPath(ComponentIndex cmp) = 0; - virtual Material* getTerrainMaterial(ComponentIndex cmp) = 0; - virtual void setTerrainXZScale(ComponentIndex cmp, float scale) = 0; - virtual float getTerrainXZScale(ComponentIndex cmp) = 0; - virtual void setTerrainYScale(ComponentIndex cmp, float scale) = 0; - virtual float getTerrainYScale(ComponentIndex cmp) = 0; - virtual Vec2 getTerrainSize(ComponentIndex cmp) = 0; - virtual AABB getTerrainAABB(ComponentIndex cmp) = 0; - virtual ComponentIndex getTerrainComponent(Entity entity) = 0; - virtual Entity getTerrainEntity(ComponentIndex cmp) = 0; - virtual Vec2 getTerrainResolution(ComponentIndex cmp) = 0; - virtual ComponentIndex getFirstTerrain() = 0; - virtual ComponentIndex getNextTerrain(ComponentIndex cmp) = 0; + virtual float getTerrainHeightAt(ComponentHandle cmp, float x, float z) = 0; + virtual Vec3 getTerrainNormalAt(ComponentHandle cmp, float x, float z) = 0; + virtual void setTerrainMaterialPath(ComponentHandle cmp, const Path& path) = 0; + virtual Path getTerrainMaterialPath(ComponentHandle cmp) = 0; + virtual Material* getTerrainMaterial(ComponentHandle cmp) = 0; + virtual void setTerrainXZScale(ComponentHandle cmp, float scale) = 0; + virtual float getTerrainXZScale(ComponentHandle cmp) = 0; + virtual void setTerrainYScale(ComponentHandle cmp, float scale) = 0; + virtual float getTerrainYScale(ComponentHandle cmp) = 0; + virtual Vec2 getTerrainSize(ComponentHandle cmp) = 0; + virtual AABB getTerrainAABB(ComponentHandle cmp) = 0; + virtual ComponentHandle getTerrainComponent(Entity entity) = 0; + virtual Entity getTerrainEntity(ComponentHandle cmp) = 0; + virtual Vec2 getTerrainResolution(ComponentHandle cmp) = 0; + virtual ComponentHandle getFirstTerrain() = 0; + virtual ComponentHandle getNextTerrain(ComponentHandle cmp) = 0; virtual bool isGrassEnabled() const = 0; - virtual float getGrassDistance(ComponentIndex cmp, int index) = 0; - virtual void setGrassDistance(ComponentIndex cmp, int index, float value) = 0; + virtual float getGrassDistance(ComponentHandle cmp, int index) = 0; + virtual void setGrassDistance(ComponentHandle cmp, int index, float value) = 0; virtual void enableGrass(bool enabled) = 0; - virtual void setGrassPath(ComponentIndex cmp, int index, const Path& path) = 0; - virtual Path getGrassPath(ComponentIndex cmp, int index) = 0; - virtual void setGrassGround(ComponentIndex cmp, int index, int ground) = 0; - virtual int getGrassGround(ComponentIndex cmp, int index) = 0; - virtual void setGrassDensity(ComponentIndex cmp, int index, int density) = 0; - virtual int getGrassDensity(ComponentIndex cmp, int index) = 0; - virtual int getGrassCount(ComponentIndex cmp) = 0; - virtual void addGrass(ComponentIndex cmp, int index) = 0; - virtual void removeGrass(ComponentIndex cmp, int index) = 0; + virtual void setGrassPath(ComponentHandle cmp, int index, const Path& path) = 0; + virtual Path getGrassPath(ComponentHandle cmp, int index) = 0; + virtual void setGrassGround(ComponentHandle cmp, int index, int ground) = 0; + virtual int getGrassGround(ComponentHandle cmp, int index) = 0; + virtual void setGrassDensity(ComponentHandle cmp, int index, int density) = 0; + virtual int getGrassDensity(ComponentHandle cmp, int index) = 0; + virtual int getGrassCount(ComponentHandle cmp) = 0; + virtual void addGrass(ComponentHandle cmp, int index) = 0; + virtual void removeGrass(ComponentHandle cmp, int index) = 0; - virtual int getClosestPointLights(const Vec3& pos, ComponentIndex* lights, int max_lights) = 0; - virtual void getPointLights(const Frustum& frustum, Array& lights) = 0; - virtual void getPointLightInfluencedGeometry(ComponentIndex light_cmp, + virtual int getClosestPointLights(const Vec3& pos, ComponentHandle* lights, int max_lights) = 0; + virtual void getPointLights(const Frustum& frustum, Array& lights) = 0; + virtual void getPointLightInfluencedGeometry(ComponentHandle light_cmp, Array& infos) = 0; - virtual void getPointLightInfluencedGeometry(ComponentIndex light_cmp, + virtual void getPointLightInfluencedGeometry(ComponentHandle light_cmp, const Frustum& frustum, Array& infos) = 0; - virtual void setLightCastShadows(ComponentIndex cmp, bool cast_shadows) = 0; - virtual bool getLightCastShadows(ComponentIndex cmp) = 0; - virtual float getLightAttenuation(ComponentIndex cmp) = 0; - virtual void setLightAttenuation(ComponentIndex cmp, float attenuation) = 0; - virtual float getLightFOV(ComponentIndex cmp) = 0; - virtual void setLightFOV(ComponentIndex cmp, float fov) = 0; - virtual float getLightRange(ComponentIndex cmp) = 0; - virtual void setLightRange(ComponentIndex cmp, float value) = 0; - virtual void setPointLightIntensity(ComponentIndex cmp, float intensity) = 0; - virtual void setGlobalLightIntensity(ComponentIndex cmp, float intensity) = 0; - virtual void setPointLightColor(ComponentIndex cmp, const Vec3& color) = 0; - virtual void setGlobalLightColor(ComponentIndex cmp, const Vec3& color) = 0; - virtual void setGlobalLightSpecular(ComponentIndex cmp, const Vec3& color) = 0; - virtual void setGlobalLightSpecularIntensity(ComponentIndex cmp, float intensity) = 0; - virtual void setLightAmbientIntensity(ComponentIndex cmp, float intensity) = 0; - virtual void setLightAmbientColor(ComponentIndex cmp, const Vec3& color) = 0; - virtual void setFogDensity(ComponentIndex cmp, float density) = 0; - virtual void setFogColor(ComponentIndex cmp, const Vec3& color) = 0; - virtual float getPointLightIntensity(ComponentIndex cmp) = 0; - virtual Entity getPointLightEntity(ComponentIndex cmp) const = 0; - virtual Entity getGlobalLightEntity(ComponentIndex cmp) const = 0; - virtual float getGlobalLightIntensity(ComponentIndex cmp) = 0; - virtual Vec3 getPointLightColor(ComponentIndex cmp) = 0; - virtual Vec3 getGlobalLightColor(ComponentIndex cmp) = 0; - virtual Vec3 getGlobalLightSpecular(ComponentIndex cmp) = 0; - virtual float getGlobalLightSpecularIntensity(ComponentIndex cmp) = 0; - virtual float getLightAmbientIntensity(ComponentIndex cmp) = 0; - virtual Vec3 getLightAmbientColor(ComponentIndex cmp) = 0; - virtual float getFogDensity(ComponentIndex cmp) = 0; - virtual float getFogBottom(ComponentIndex cmp) = 0; - virtual float getFogHeight(ComponentIndex cmp) = 0; - virtual void setFogBottom(ComponentIndex cmp, float value) = 0; - virtual void setFogHeight(ComponentIndex cmp, float value) = 0; - virtual Vec3 getFogColor(ComponentIndex cmp) = 0; - virtual Vec3 getPointLightSpecularColor(ComponentIndex cmp) = 0; - virtual void setPointLightSpecularColor(ComponentIndex cmp, const Vec3& color) = 0; - virtual float getPointLightSpecularIntensity(ComponentIndex cmp) = 0; - virtual void setPointLightSpecularIntensity(ComponentIndex cmp, float color) = 0; + virtual void setLightCastShadows(ComponentHandle cmp, bool cast_shadows) = 0; + virtual bool getLightCastShadows(ComponentHandle cmp) = 0; + virtual float getLightAttenuation(ComponentHandle cmp) = 0; + virtual void setLightAttenuation(ComponentHandle cmp, float attenuation) = 0; + virtual float getLightFOV(ComponentHandle cmp) = 0; + virtual void setLightFOV(ComponentHandle cmp, float fov) = 0; + virtual float getLightRange(ComponentHandle cmp) = 0; + virtual void setLightRange(ComponentHandle cmp, float value) = 0; + virtual void setPointLightIntensity(ComponentHandle cmp, float intensity) = 0; + virtual void setGlobalLightIntensity(ComponentHandle cmp, float intensity) = 0; + virtual void setPointLightColor(ComponentHandle cmp, const Vec3& color) = 0; + virtual void setGlobalLightColor(ComponentHandle cmp, const Vec3& color) = 0; + virtual void setGlobalLightSpecular(ComponentHandle cmp, const Vec3& color) = 0; + virtual void setGlobalLightSpecularIntensity(ComponentHandle cmp, float intensity) = 0; + virtual void setLightAmbientIntensity(ComponentHandle cmp, float intensity) = 0; + virtual void setLightAmbientColor(ComponentHandle cmp, const Vec3& color) = 0; + virtual void setFogDensity(ComponentHandle cmp, float density) = 0; + virtual void setFogColor(ComponentHandle cmp, const Vec3& color) = 0; + virtual float getPointLightIntensity(ComponentHandle cmp) = 0; + virtual Entity getPointLightEntity(ComponentHandle cmp) const = 0; + virtual Entity getGlobalLightEntity(ComponentHandle cmp) const = 0; + virtual float getGlobalLightIntensity(ComponentHandle cmp) = 0; + virtual Vec3 getPointLightColor(ComponentHandle cmp) = 0; + virtual Vec3 getGlobalLightColor(ComponentHandle cmp) = 0; + virtual Vec3 getGlobalLightSpecular(ComponentHandle cmp) = 0; + virtual float getGlobalLightSpecularIntensity(ComponentHandle cmp) = 0; + virtual float getLightAmbientIntensity(ComponentHandle cmp) = 0; + virtual Vec3 getLightAmbientColor(ComponentHandle cmp) = 0; + virtual float getFogDensity(ComponentHandle cmp) = 0; + virtual float getFogBottom(ComponentHandle cmp) = 0; + virtual float getFogHeight(ComponentHandle cmp) = 0; + virtual void setFogBottom(ComponentHandle cmp, float value) = 0; + virtual void setFogHeight(ComponentHandle cmp, float value) = 0; + virtual Vec3 getFogColor(ComponentHandle cmp) = 0; + virtual Vec3 getPointLightSpecularColor(ComponentHandle cmp) = 0; + virtual void setPointLightSpecularColor(ComponentHandle cmp, const Vec3& color) = 0; + virtual float getPointLightSpecularIntensity(ComponentHandle cmp) = 0; + virtual void setPointLightSpecularIntensity(ComponentHandle cmp, float color) = 0; - virtual Texture* getEnvironmentProbeTexture(ComponentIndex cmp) const = 0; - virtual void reloadEnvironmentProbe(ComponentIndex cmp) = 0; + virtual Texture* getEnvironmentProbeTexture(ComponentHandle cmp) const = 0; + virtual void reloadEnvironmentProbe(ComponentHandle cmp) = 0; protected: virtual ~RenderScene() {} diff --git a/src/renderer/renderer.cpp b/src/renderer/renderer.cpp index 620d4e052..c60efa417 100644 --- a/src/renderer/renderer.cpp +++ b/src/renderer/renderer.cpp @@ -97,7 +97,7 @@ struct BonePropertyDescriptor : public IEnumPropertyDescriptor int value; stream.read(&value, sizeof(value)); auto* render_scene = static_cast(cmp.scene); - render_scene->setBoneAttachmentBone(cmp.index, value); + render_scene->setBoneAttachmentBone(cmp.handle, value); } @@ -105,25 +105,25 @@ struct BonePropertyDescriptor : public IEnumPropertyDescriptor { ASSERT(index == -1); auto* render_scene = static_cast(cmp.scene); - int value = render_scene->getBoneAttachmentBone(cmp.index); + int value = render_scene->getBoneAttachmentBone(cmp.handle); int len = sizeof(value); stream.write(&value, len); } - ComponentIndex getRenderable(RenderScene* render_scene, ComponentIndex bone_attachment_cmp) + ComponentHandle getRenderable(RenderScene* render_scene, ComponentHandle bone_attachment_cmp) { Entity parent_entity = render_scene->getBoneAttachmentParent(bone_attachment_cmp); if (parent_entity == INVALID_ENTITY) return INVALID_COMPONENT; - ComponentIndex renderable = render_scene->getRenderableComponent(parent_entity); + ComponentHandle renderable = render_scene->getRenderableComponent(parent_entity); return renderable; } - int getEnumCount(IScene* scene, ComponentIndex cmp) override + int getEnumCount(IScene* scene, ComponentHandle cmp) override { auto* render_scene = static_cast(scene); - ComponentIndex renderable = getRenderable(render_scene, cmp); + ComponentHandle renderable = getRenderable(render_scene, cmp); if (renderable == INVALID_COMPONENT) return 0; auto* model = render_scene->getRenderableModel(renderable); if (!model || !model->isReady()) return 0; @@ -131,10 +131,10 @@ struct BonePropertyDescriptor : public IEnumPropertyDescriptor } - const char* getEnumItemName(IScene* scene, ComponentIndex cmp, int index) override + const char* getEnumItemName(IScene* scene, ComponentHandle cmp, int index) override { auto* render_scene = static_cast(scene); - ComponentIndex renderable = getRenderable(render_scene, cmp); + ComponentHandle renderable = getRenderable(render_scene, cmp); if (renderable == INVALID_COMPONENT) return ""; auto* model = render_scene->getRenderableModel(renderable); if (!model) return ""; diff --git a/src/renderer/terrain.cpp b/src/renderer/terrain.cpp index bb5f1a074..264f9de4d 100644 --- a/src/renderer/terrain.cpp +++ b/src/renderer/terrain.cpp @@ -387,7 +387,7 @@ void Terrain::forceGrassUpdate() } } -Array& Terrain::getQuads(ComponentIndex camera) +Array& Terrain::getQuads(ComponentHandle camera) { int quads_index = m_grass_quads.find(camera); if (quads_index < 0) @@ -442,7 +442,7 @@ void Terrain::generateGrassTypeQuad(GrassPatch& patch, } -void Terrain::updateGrass(ComponentIndex camera) +void Terrain::updateGrass(ComponentHandle camera) { PROFILE_FUNCTION(); if (!m_splatmap) @@ -554,7 +554,7 @@ void Terrain::GrassType::grassLoaded(Resource::State, Resource::State) } -void Terrain::getGrassInfos(const Frustum& frustum, Array& infos, ComponentIndex camera) +void Terrain::getGrassInfos(const Frustum& frustum, Array& infos, ComponentHandle camera) { if (!m_material || !m_material->isReady()) return; @@ -613,7 +613,11 @@ void Terrain::setMaterial(Material* material) } } -void Terrain::deserialize(InputBlob& serializer, Universe& universe, RenderScene& scene, int index, int version) +void Terrain::deserialize(InputBlob& serializer, + Universe& universe, + RenderScene& scene, + ComponentHandle cmp, + int version) { serializer.read(m_entity); serializer.read(m_layer_mask); @@ -646,7 +650,7 @@ void Terrain::deserialize(InputBlob& serializer, Universe& universe, RenderScene } setGrassTypePath(i, Path(path)); } - universe.addComponent(m_entity, TERRAIN_HASH, &scene, index); + universe.addComponent(m_entity, TERRAIN_HASH, &scene, cmp); } diff --git a/src/renderer/terrain.h b/src/renderer/terrain.h index 71261971c..2bd43ac55 100644 --- a/src/renderer/terrain.h +++ b/src/renderer/terrain.h @@ -109,21 +109,25 @@ class Terrain void setMaterial(Material* material); void getInfos(Array& infos, const Vec3& camera_pos, LIFOAllocator& allocator); - void getGrassInfos(const Frustum& frustum, Array& infos, ComponentIndex camera); + void getGrassInfos(const Frustum& frustum, Array& infos, ComponentHandle camera); RayCastModelHit castRay(const Vec3& origin, const Vec3& dir); void serialize(OutputBlob& serializer); - void deserialize(InputBlob& serializer, Universe& universe, RenderScene& scene, int index, int version); + void deserialize(InputBlob& serializer, + Universe& universe, + RenderScene& scene, + ComponentHandle cmp, + int version); void addGrassType(int index); void removeGrassType(int index); void forceGrassUpdate(); private: - Array& getQuads(ComponentIndex camera); + Array& getQuads(ComponentHandle camera); TerrainQuad* generateQuadTree(float size); float getHeight(int x, int z) const; - void updateGrass(ComponentIndex camera); + void updateGrass(ComponentHandle camera); void generateGrassTypeQuad(GrassPatch& patch, const Matrix& terrain_matrix, float quad_x, @@ -150,8 +154,8 @@ class Terrain RenderScene& m_scene; Array m_grass_types; Array m_free_grass_quads; - AssociativeArray > m_grass_quads; - AssociativeArray m_last_camera_position; + AssociativeArray > m_grass_quads; + AssociativeArray m_last_camera_position; bool m_force_grass_update; Renderer& m_renderer; }; diff --git a/src/unit_tests/graphics/ut_clipper.cpp b/src/unit_tests/graphics/ut_clipper.cpp index 18476591f..cf044a430 100644 --- a/src/unit_tests/graphics/ut_clipper.cpp +++ b/src/unit_tests/graphics/ut_clipper.cpp @@ -37,12 +37,12 @@ namespace { Lumix::DefaultAllocator allocator; Lumix::Array spheres(allocator); - Lumix::Array renderables(allocator); + Lumix::Array renderables(allocator); int renderable = 0; for (float i = 0.f; i < 30000000.0f; i += 15.f) { spheres.push(Lumix::Sphere(i, 0.f, 50.f, 5.f)); - renderables.push(renderable); + renderables.push({renderable}); ++renderable; } @@ -73,7 +73,7 @@ namespace const Lumix::CullingSystem::Subresults& subresult = result[i]; for (int j = 0; j < subresult.size(); ++j) { - LUMIX_EXPECT(subresult[i] < 6); + LUMIX_EXPECT(subresult[i].index < 6); } } @@ -87,12 +87,12 @@ namespace { Lumix::DefaultAllocator allocator; Lumix::Array spheres(allocator); - Lumix::Array renderables(allocator); + Lumix::Array renderables(allocator); int renderable = 0; for(float i = 0.f; i < 30000000.0f; i += 15.f) { spheres.push(Lumix::Sphere(i, 0.f, 50.f, 5.f)); - renderables.push(renderable); + renderables.push({renderable}); ++renderable; } @@ -124,7 +124,7 @@ namespace const Lumix::CullingSystem::Subresults& subresult = result[i]; for (int j = 0; j < subresult.size(); ++j) { - LUMIX_EXPECT(subresult[i] < 6); + LUMIX_EXPECT(subresult[i].index < 6); } }