Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· openaiHugging Face Suzhen/CodeChat

well, i put only similar objects into imgui drawlist and wanna calculate later amount of…

  1. User

    well, i put only similar objects into imgui drawlist and wanna calculate later amount of that items being put into it.

  2. Assistant

    To calculate the number of similar objects in an ImGui drawlist, you can iterate through the draw commands and count the number of times a particular object is being drawn.

    For example, let's say you are drawing circles onto an ImGui drawlist. You could add a custom parameter to the circle draw command to specify the color of the circle:

    draw_list->AddCircle(ImVec2(100, 100), 50, IM_COL32(255, 0, 0, 255), 32);
    draw_list->AddCircle(ImVec2(200, 100), 50, IM_COL32(0, 255, 0, 255), 32);
    draw_list->AddCircle(ImVec2(300, 100), 50, IM_COL32(0, 0, 255, 255), 32);
    draw_list->AddCircle(ImVec2(400, 100), 50, IM_COL32(255, 0, 0, 255), 32);
    

    To count the number of red circles, you could iterate through the draw commands and count how many times the IM_COL32(255, 0, 0, 255) color is used:

    int red_circle_count = 0;
    for (int i = 0; i < draw_list->CmdBuffer.Size; i++)
    {
        const ImDrawCmd* cmd = &draw_list->CmdBuffer[i];
        if (cmd->UserCallback != nullptr)
        {
            // Handle custom user callbacks
        }
        else
        {
            const ImDrawIdx* idx_buffer = draw_list->IdxBuffer.Data + cmd->IdxOffset;
            for (int j = 0; j < cmd->ElemCount; j += 3)
            {
                ImDrawIdx idx0 = idx_buffer[j + 0];
                ImDrawIdx idx1 = idx_buffer[j + 1];
                ImDrawIdx idx2 = idx_buffer[j + 2];
    
                // Check the color of the circle being drawn
                const ImDrawVert* v0 = &draw_list->VtxBuffer[idx0];
                const ImDrawVert* v1 = &draw_list->VtxBuffer[idx1];
                const ImDrawVert* v2 = &draw_list->VtxBuffer[idx2];
                if (v0->col == IM_COL32(255, 0, 0, 255) &&
                    v1->col == IM_COL32(255, 0, 0, 255) &&
                    v2->col == IM_COL32(255, 0, 0, 255))
                {
                    red_circle_count++;
                }
            }
        }
    }
    
    ImGui::Text("Red circles: %d", red_circle_count);
    

    This code iterates through each command in the drawlist and checks the color of each vertex in the triangle. If all three vertices have the same color (in this case, IM_COL32(255, 0, 0, 255) for red circles), then the count for that color is incremented. Finally, the count is displayed using a ImGui::Text command.

    You could modify this code to count the number of other types of objects (e.g. rectangles, lines, text) by checking for their respective vertex patterns.

  3. User

    no, i need only int value, which will return me amount of objects, that will be draw

  4. Assistant

    If you're only interested in getting the number of objects being drawn in an ImGui drawlist, you can count the number of element commands in the command buffer:

    int object_count = 0;
    for (int i = 0; i < draw_list->CmdBuffer.Size; i++)
    {
        const ImDrawCmd* cmd = &draw_list->CmdBuffer[i];
        if (cmd->UserCallback != nullptr)
        {
            // Handle custom user callbacks
        }
        else
        {
            object_count++; // Count each non-callback command as an object
        }
    }
    
    ImGui::Text("Object count: %d", object_count);
    

    This code iterates through each command in the drawlist and increments the object_count for each non-callback command. Finally, the count is displayed using a ImGui::Text command.

    This approach doesn't differentiate between different types of objects (e.g. circles, rectangles, lines), but simply counts the number of non-callback commands, which should give you the total number of objects being drawn.

  5. User

    is it possible to do it by that way

  6. Assistant

    Yes, it is possible to count the number of specific objects being drawn within an ImGui drawlist by checking for the corresponding draw commands and incrementing a counter for each one. Here's an example of how to count the number of circles being drawn:

    int circle_count = 0;
    for (int i = 0; i < draw_list->CmdBuffer.Size; i++)
    {
        const ImDrawCmd* cmd = &draw_list->CmdBuffer[i];
        if (cmd->UserCallback != nullptr)
        {
            // Handle custom user callbacks
        }
        else if (cmd->ElemCount > 0 && cmd->UserCallback == nullptr)
        {
            const ImDrawIdx* idx_buffer = draw_list->IdxBuffer.Data + cmd->IdxOffset;
            const ImDrawVert* vtx_buffer = draw_list->VtxBuffer.Data + cmd->VtxOffset;
            for (int j = 0; j < cmd->ElemCount; j += 3)
            {
                ImDrawIdx idx0 = idx_buffer[j + 0];
                ImDrawIdx idx1 = idx_buffer[j + 1];
                ImDrawIdx idx2 = idx_buffer[j + 2];
    
                const ImVec2& v0 = vtx_buffer[idx0].pos;
                const ImVec2& v1 = vtx_buffer[idx1].pos;
                const ImVec2& v2 = vtx_buffer[idx2].pos;
    
                // Check if the vertices form a circle
                if (v0.x == v1.x && v0.y == v1.y && // Center vertex
                    v1.x == v2.x && v1.y == v2.y)   // Two circumference vertices
                {
                    circle_count++;
                }
            }
        }
    }
    
    ImGui::Text("Circle count: %d", circle_count);
    

    This code iterates through each command in the drawlist and checks whether it is a ImDrawCmd element command. It then verifies whether the vertices of the triangle are circling around the same point. If so, it increments the circle_count. Finally, the count is displayed using a ImGui::Text command.

    You can modify this code to count other types of drawn objects (e.g. rectangles, lines, text) by checking for their corresponding draw commands and vertex patterns.

  7. User

    drawBox->IdxBuffer.size();

  8. Assistant

    drawBox->IdxBuffer.size() returns the size of the ImVector that stores the indices for the vertices used in the draw commands of an ImGui drawlist.

    Since the indices are used to specify the order of the vertex buffer for rendering the objects, the size of the index buffer is typically larger than the size of the vertex buffer. The index buffer stores indices that reference the actual vertex data in the vertex buffer, and provides a way to organize the vertices to form the desired objects being drawn.

    If you want to count the number of objects being drawn in an ImGui drawlist, it's better to iterate through the command buffer and count the number of element commands, as shown in my earlier example.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.