Hazel game engine (#116) handles ISSUE on GitHub

If there are errors in the code, terminology, etc. in the text, please correct me

foreword

  • of this program

    As the title, handles pull requests on GitHub in case there is something wrong with the project

about optimization

Focusing too much on cache optimization can degrade performance, Cherno said, because cache optimization requires opening up additional space on the CPU, which can consume more space than the ± operations that run code directly without caching.

Some important Pull Requests

  • The Add Component button of the Properties panel

    For each component, you have to write a piece of code to create an ImGui UI button, which means adding this component to the current entity.

    Code redundancy can be optimized with template

    if (ImGui::BeginPopup("AddComponent"))
    {
          
          
        if (!m_SelectionContext.HasComponent<CircleCollider2DComponent>())
        {
          
          
            if (ImGui::MenuItem("Circle Collider 2D"))
            {
          
          
                m_SelectionContext.AddComponent<CircleCollider2DComponent>();
                ImGui::CloseCurrentPopup();
            }
        }
        ......
    
    if (ImGui::BeginPopup("AddComponent")) {
          
          
        DisplayAddComponentEntry<CameraComponent>("Camera");
        DisplayAddComponentEntry<SpriteRendererComponent>("Sprite Renderer");
        DisplayAddComponentEntry<CircleRendererComponent>("Circle Renderer");
        DisplayAddComponentEntry<Rigidbody2DComponent>("Rigidbody 2D");
        DisplayAddComponentEntry<BoxCollider2DComponent>("Box Collider 2D");
        DisplayAddComponentEntry<CircleCollider2DComponent>("Circle Collider 2D");
        ImGui::EndPopup();
    }
    
    template<typename T>
    void SceneHierarchyPanel::DisplayAddComponentEntry(const std::string& entryName)
    {
          
          
        if (!m_SelectionContext.HasComponent<T>()) {
          
          
            if (ImGui::MenuItem(entryName.c_str())) {
          
          
                m_SelectionContext.AddComponent<T>();
                ImGui::CloseCurrentPopup();
            }
        }
    }
    
  • “std::filesystem::relative”

    The calculation of relative paths in this code will consume a lot of money in the loop. You should change the relative path to an absolute path , and then modify it to a relative path when setting the drag source .

    for (auto& directoryEntry : std::filesystem::directory_iterator(m_CurrentDirectory))
    {
          
          
        const auto& path = directoryEntry.path();
        //auto relativePath = std::filesystem::relative(path, g_AssetPath);
        //std::string filenameString = relativePath.filename().string();
        std::string filenameString = path.filename().string();
    
    if (ImGui::BeginDragDropSource())
    {
          
          
        auto relativePath = std::filesystem::relative(path, g_AssetPath);
        const wchar_t* itemPath = relativePath.c_str();
        ImGui::SetDragDropPayload("CONTENT_BROWSER_ITEM", itemPath, (wcslen(itemPath) + 1) * sizeof(wchar_t));
        ImGui::EndDragDropSource();
    
  • memory not cleared

    • explain steps
      1. When the current scene is running, create a new empty scene
      2. The original Editor scene has not been destroyed, it still exists.
    • Why
      1. Because m_ActiveScene copies m_EditorScene during runtime, each of them points to a different scene memory ,
      2. When the New empty scene, m_ActiveScene points to the memory again, and the previously pointed memory will be destroyed, but the scene memory pointed to by m_EditorScene still exists .
    void EditorLayer::NewScene()
    {
          
          
        // 原先代码
        // 让m_ActiveScene指向新场景内存 旧的场景内存会自动被销毁
        m_ActiveScene = CreateRef<Scene>();
        // 但是m_EditorScene指向的场景内存还存在
        m_ActiveScene->OnViewportResize((uint32_t)m_ViewportSize.x, (uint32_t)m_ViewportSize.y);
        m_SceneHierarchyPanel.SetContext(m_ActiveScene);
    }
    
    void EditorLayer::NewScene()
    {
          
          
        m_EditorScene = CreateRef<Scene>();// 让m_EditorScene指向新场景内存 旧的编辑场景内存会自动被销毁
        m_ActiveScene = m_EditorScene; // 让m_ActiveScene也指向新场景内存 它指向的旧的运行场景内存也会被销毁。
        
        m_ActiveScene = CreateRef<Scene>();
        m_ActiveScene->OnViewportResize((uint32_t)m_ViewportSize.x, (uint32_t)m_ViewportSize.y);
        m_SceneHierarchyPanel.SetContext(m_ActiveScene);
    }
    

    Let m_EditorScene point to the new scene memory first, and the old editing scene memory will be destroyed , then let m_ActiveScene also point to the new scene memory, and the old running scene memory it points to will also be destroyed.

  • hash of uuid

    The uuid is almost unique, and there is no need to hash again, just return the uuid as the hash code

    namespace std {
          
          
    	template<typename T> struct hash;// 不清楚这个干嘛用的
    
    	template<>
    	struct hash<Hazel::UUID> {
          
          
    		std::size_t operator()(const Hazel::UUID& uuid) const {
          
          
    			//return hash<uint64_t>()((uint64_t)uuid);
    			return (uint64_t)uuid;
    		}
    	};
    }
    

Guess you like

Origin blog.csdn.net/qq_34060370/article/details/132266346