c++imgui

ImGui::Text is not showing the string of my ImVector<Pin>


I'm trying to print in the screen the name of a Pin, but it's showing strange characters:

The pin with the strange characters

My class is like this:

struct Pin
{
    int ID;
    char Name[32];

    Pin(int id, const char *name)
    {
        ID = id;
        strcpy(Name, name);
    }
};

struct Node
{
    int ID;
    char Name[32];
    ImVec2 Pos, Size;
    float Value;
    ImVector<Pin> Inputs, Outputs;

    Node(int id, const char *name, const ImVec2 &pos, const ImVector<Pin> inputs, const ImVector<Pin> outputs)
    {
        ID = id;
        strcpy(Name, name);
        Pos = pos;
        Inputs = inputs;
        Outputs = outputs;
    }

    ImRect GetInputFlowSlotPos(ImVec2 offset) const
    {
        return ImRect(offset + ImVec2(Pos.x + 24.0f, Pos.y + 44.0f), offset + ImVec2(Pos.x + 4.0f, Pos.y + 24.0f));
    }
    ImRect GetOutputFlowSlotPos(ImVec2 offset) const
    {
        return ImRect(offset + ImVec2(Pos.x + Size.x - 24.0f, Pos.y + 24.0f), offset + ImVec2(Pos.x + Size.x - 4.0f, Pos.y + 44.0f));
    }
    ImVec2 GetInputSlotPos(int slot_no) const { return ImVec2(Pos.x, Pos.y + Size.y * ((float)slot_no + 1) / ((float)Inputs.Size + 1)); }
    ImVec2 GetOutputSlotPos(int slot_no) const { return ImVec2(Pos.x + Size.x, Pos.y + Size.y * ((float)slot_no + 1) / ((float)Outputs.Size + 1)); }
};

And I initialize like this:

inputsPin.push_back(Pin(0, "X"));
inputsPin.push_back(Pin(1, "Y"));
inputsPin.push_back(Pin(2, "Z"));

nodes.push_back(Node(0, "Di", ImVec2(40, 50), inputsPin, outputsPin));
nodes.push_back(Node(1, "Di", ImVec2(40, 150), inputsPin, outputsPin));
nodes.push_back(Node(2, "Di", ImVec2(270, 80), inputsPin, outputsPin));

When I print:

for (int slot_idx = 0; slot_idx < node->Inputs.Size; slot_idx++)
{
    Pin *pin = &node->Inputs[slot_idx];

    ImGui::SetCursorScreenPos(offset + node->GetInputSlotPos(slot_idx));
    ImGui::Text("%s", pin->Name);
}

*Edit: After I changed to std::string, the printed string is even more weird:

Another bug


Solution

  • I believe your issue was related to the fact that you assumed ImVector would behave like std::vector<>. ImVector<> is used internally by Dear ImGui and is NOT expected to be used by you. Note that its description says:

    // - Important: our implementation does NOT call C++ constructors/destructors, we treat everything as raw data! This is intentional but be extra mindful of that,
    //   Do NOT use this class as a std::vector replacement in your own code! Many of the structures used by dear imgui can be safely initialized by a zero-memset.