From 97f708b6e7d4fa79c8cce465bc040a5440c9fa47 Mon Sep 17 00:00:00 2001 From: Mikulas Florek Date: Sun, 24 Jul 2016 23:04:36 +0200 Subject: [PATCH] cleanup --- src/animation/animation_system.cpp | 11 +--- src/editor/world_editor.cpp | 94 +++++++++--------------------- src/engine/engine.cpp | 38 ++++-------- src/renderer/editor/plugins.cpp | 10 ++-- src/renderer/editor/scene_view.cpp | 18 ++---- 5 files changed, 48 insertions(+), 123 deletions(-) diff --git a/src/animation/animation_system.cpp b/src/animation/animation_system.cpp index d987825ed..513269af6 100644 --- a/src/animation/animation_system.cpp +++ b/src/animation/animation_system.cpp @@ -63,16 +63,7 @@ struct AnimationSceneImpl : public AnimationScene , m_animables(allocator) { m_is_game_running = false; - m_render_scene = nullptr; - uint32 hash = crc32("renderer"); - for (auto* scene : universe.getScenes()) - { - if (crc32(scene->getPlugin().getName()) == hash) - { - m_render_scene = static_cast(scene); - break; - } - } + m_render_scene = static_cast(universe.getScene(crc32("renderer"))); universe.registerComponentTypeScene(ANIMABLE_TYPE, this); ASSERT(m_render_scene); } diff --git a/src/editor/world_editor.cpp b/src/editor/world_editor.cpp index d15fef524..e03b67eb9 100644 --- a/src/editor/world_editor.cpp +++ b/src/editor/world_editor.cpp @@ -980,18 +980,14 @@ private: bool execute() override { bool ret = false; - const Array& scenes = m_editor.getUniverse()->getScenes(); + IScene* scene = m_editor.getUniverse()->getScene(m_type); for (int j = 0; j < m_entities.size(); ++j) { - for (auto* scene : scenes) + ComponentHandle cmp = scene->createComponent(m_type, m_entities[j]); + if (isValid(cmp)) { - ComponentHandle cmp = scene->createComponent(m_type, m_entities[j]); - if (isValid(cmp)) - { - ret = true; - break; - } + ret = true; } } return ret; @@ -1129,7 +1125,6 @@ private: void undo() override { Universe* universe = m_editor.getUniverse(); - const Array& scenes = universe->getScenes(); InputBlob blob(m_old_values); for (int i = 0; i < m_entities.size(); ++i) { @@ -1142,17 +1137,12 @@ private: ComponentType cmp_type; blob.read(cmp_type); ComponentUID new_component; - for (int i = 0; i < scenes.size(); ++i) - { - 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; - if (new_component.isValid()) - { - break; - } - } + IScene* scene = universe->getScene(cmp_type); + ASSERT(scene); + new_component.handle = scene->createComponent(cmp_type, new_entity); + new_component.entity = new_entity; + new_component.scene = scene; + new_component.type = cmp_type; Array& props = PropertyRegister::getDescriptors(cmp_type); @@ -1236,24 +1226,15 @@ private: void undo() override { uint32 template_hash = m_editor.m_template_system->getTemplate(m_component.entity); - const Array& scenes = m_editor.getUniverse()->getScenes(); - + IScene* scene = m_editor.getUniverse()->getScene(m_component.type); + ASSERT(scene); if (template_hash == 0) { - for (int i = 0; i < scenes.size(); ++i) - { - ComponentHandle cmp = - scenes[i]->createComponent(m_component.type, m_component.entity); - if (isValid(cmp)) - { - m_component.handle = cmp; - m_component.scene = scenes[i]; - break; - } - } + ComponentHandle cmp = scene->createComponent(m_component.type, m_component.entity); + m_component.handle = cmp; + m_component.scene = scene; InputBlob blob(m_old_values); - const Array& props = - PropertyRegister::getDescriptors(m_component.type); + const Array& props = PropertyRegister::getDescriptors(m_component.type); for (int i = 0; i < props.size(); ++i) { props[i]->set(m_component, -1, blob); @@ -1261,27 +1242,16 @@ private: } else { - const Array& entities = - m_editor.m_template_system->getInstances(template_hash); - for (int entity_index = 0, c = entities.size(); entity_index < c; ++entity_index) + const Array& entities = m_editor.m_template_system->getInstances(template_hash); + for (Entity entity : entities) { - for (int scene_index = 0; scene_index < scenes.size(); ++scene_index) + ComponentUID cmp_new( + entity, m_component.type, scene, scene->createComponent(m_component.type, entity)); + InputBlob blob(m_old_values); + const Array& props = PropertyRegister::getDescriptors(m_component.type); + for (int i = 0; i < props.size(); ++i) { - ComponentUID cmp_new(entities[entity_index], - m_component.type, - scenes[scene_index], - scenes[scene_index]->createComponent( - m_component.type, entities[entity_index])); - if (cmp_new.isValid()) - { - InputBlob blob(m_old_values); - const Array& props = - PropertyRegister::getDescriptors(m_component.type); - for (int i = 0; i < props.size(); ++i) - { - props[i]->set(cmp_new, -1, blob); - } - } + props[i]->set(cmp_new, -1, blob); } } } @@ -2178,20 +2148,8 @@ public: void cloneComponent(const ComponentUID& src, Entity entity) override { - ComponentUID clone = ComponentUID::INVALID; - - const Array& scenes = m_universe->getScenes(); - for (int i = 0; i < scenes.size(); ++i) - { - clone = ComponentUID(entity, - src.type, - scenes[i], - scenes[i]->createComponent(src.type, entity)); - if (clone.isValid()) - { - break; - } - } + IScene* scene = m_universe->getScene(src.type); + ComponentUID clone(entity, src.type, scene, scene->createComponent(src.type, entity)); const auto& properties = PropertyRegister::getDescriptors(src.type); OutputBlob stream(m_allocator); diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index ae4761240..cfd6e97d6 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -279,11 +279,9 @@ public: { if (!universe->hasComponent(entity, {component_type})) return INVALID_COMPONENT; ComponentType type = {component_type}; - for (auto* scene : universe->getScenes()) - { - ComponentHandle cmp = scene->getComponent(entity, type); - if (Lumix::isValid(cmp)) return cmp; - } + IScene* scene = universe->getScene(type); + if (scene) return scene->getComponent(entity, type); + ASSERT(false); return INVALID_COMPONENT; } @@ -309,10 +307,11 @@ public: } else { - ComponentType type_handle = PropertyRegister::getComponentType(parameter_name); - for (auto* scene : ctx->getScenes()) + ComponentType cmp_type = PropertyRegister::getComponentType(parameter_name); + IScene* scene = ctx->getScene(cmp_type); + if (scene) { - ComponentUID cmp(e, type_handle, scene, scene->createComponent(type_handle, e)); + ComponentUID cmp(e, cmp_type, scene, scene->createComponent(cmp_type, e)); if (cmp.isValid()) { lua_pushvalue(L, -1); @@ -320,7 +319,7 @@ public: while (lua_next(L, -2) != 0) { const char* property_name = luaL_checkstring(L, -2); - auto* desc = PropertyRegister::getDescriptor(type_handle, crc32(property_name)); + auto* desc = PropertyRegister::getDescriptor(cmp_type, crc32(property_name)); if (!desc) { g_log_error.log("Lua Script") << "Unknown property " << property_name; @@ -333,7 +332,6 @@ public: lua_pop(L, 1); } lua_pop(L, 1); - break; } } } @@ -664,13 +662,6 @@ public: for (auto* plugin : plugins) { IScene* scene = plugin->createScene(*universe); - bool is_some_registered = false; - for (int i = 0; i < MAX_COMPONENTS_TYPES_COUNT; ++i) - { - ComponentType type = {i}; - is_some_registered = is_some_registered || universe->getScene(type) == scene; - } - ASSERT(is_some_registered); if (scene) { universe->addScene(scene); @@ -960,18 +951,11 @@ public: ComponentUID createComponent(Universe& universe, Entity entity, ComponentType type) { - const Array& scenes = universe.getScenes(); ComponentUID cmp; - for (int i = 0; i < scenes.size(); ++i) - { - cmp = ComponentUID(entity, type, scenes[i], scenes[i]->createComponent(type, entity)); + IScene* scene = universe.getScene(type); + if (!scene) return ComponentUID::INVALID; - if (cmp.isValid()) - { - return cmp; - } - } - return ComponentUID::INVALID; + return ComponentUID(entity, type, scene, scene->createComponent(type, entity)); } diff --git a/src/renderer/editor/plugins.cpp b/src/renderer/editor/plugins.cpp index 42bd1afd2..0b43bfdeb 100644 --- a/src/renderer/editor/plugins.cpp +++ b/src/renderer/editor/plugins.cpp @@ -357,7 +357,7 @@ struct ModelPlugin : public AssetBrowser::IPlugin m_pipeline->load(); auto mesh_entity = m_universe->createEntity({ 0, 0, 0 }, { 0, 0, 0, 1 }); - auto* render_scene = static_cast(m_universe->getScene(crc32("renderer"))); + auto* render_scene = static_cast(m_universe->getScene(RENDERER_HASH)); m_mesh = render_scene->createComponent(RENDERABLE_TYPE, mesh_entity); auto light_entity = m_universe->createEntity({ 0, 0, 0 }, { 0, 0, 0, 1 }); @@ -376,7 +376,7 @@ struct ModelPlugin : public AssetBrowser::IPlugin void showPreview(Model& model) { auto& engine = m_app.getWorldEditor()->getEngine(); - auto* render_scene = static_cast(m_universe->getScene(crc32("renderer"))); + auto* render_scene = static_cast(m_universe->getScene(RENDERER_HASH)); if (!render_scene) return; if (!model.isReady()) return; @@ -859,7 +859,7 @@ struct EnvironmentProbePlugin : public PropertyGrid::IPlugin bgfx::createTexture2D(TEXTURE_SIZE, TEXTURE_SIZE, 1, bgfx::TextureFormat::RGBA8, BGFX_TEXTURE_READ_BACK); Vec3 probe_position = universe->getPosition(cmp.entity); - auto* scene = static_cast(universe->getScene(crc32("renderer"))); + auto* scene = static_cast(universe->getScene(RENDERER_HASH)); ComponentHandle original_camera = scene->getCameraInSlot("main"); if(original_camera != INVALID_COMPONENT) scene->setCameraSlot(original_camera, ""); @@ -1192,7 +1192,7 @@ struct SceneViewPlugin : public StudioApp::IPlugin void onUniverseCreated() { - m_render_scene = static_cast(m_editor.getUniverse()->getScene(crc32("renderer"))); + m_render_scene = static_cast(m_editor.getUniverse()->getScene(RENDERER_HASH)); } @@ -1443,7 +1443,7 @@ struct GameViewPlugin : public StudioApp::IPlugin void onUniverseCreated() { auto* universe = m_app.getWorldEditor()->getUniverse(); - auto* scene = static_cast(universe->getScene(crc32("renderer"))); + auto* scene = static_cast(universe->getScene(RENDERER_HASH)); m_gui_pipeline->setScene(scene); } diff --git a/src/renderer/editor/scene_view.cpp b/src/renderer/editor/scene_view.cpp index 772d37432..081f075b1 100644 --- a/src/renderer/editor/scene_view.cpp +++ b/src/renderer/editor/scene_view.cpp @@ -82,20 +82,12 @@ struct InsertMeshCommand : public Lumix::IEditorCommand auto* universe = m_editor.getUniverse(); m_entity = universe->createEntity({0, 0, 0}, {0, 0, 0, 1}); universe->setPosition(m_entity, m_position); - const auto& scenes = universe->getScenes(); - 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); + auto* scene = static_cast(universe->getScene(RENDERABLE_TYPE)); + if (!scene) return false; - if (isValid(cmp)) - { - scene = scenes[i]; - break; - } - } - if (isValid(cmp)) static_cast(scene)->setRenderablePath(cmp, m_mesh_path); + Lumix::ComponentHandle cmp = scene->createComponent(RENDERABLE_TYPE, m_entity); + + if (isValid(cmp)) scene->setRenderablePath(cmp, m_mesh_path); return true; }