Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standalone ImDrawList use and rendering #1878

Closed
maxkunes opened this issue Jun 13, 2018 · 6 comments
Closed

Standalone ImDrawList use and rendering #1878

maxkunes opened this issue Jun 13, 2018 · 6 comments

Comments

@maxkunes
Copy link

v1.6 Master

I created this issue to ask if you had any plans of making IMGUI's renderer standalone or know of anyone that has tried to slim IMGUI to its base renderer.. I do not use the gui system but I do utilize the renderer and I do believe that IMGUI's renderer is quite nice considering it is largely platform independent. I currently have to do some hackish abuse of the api such as making fullscreen transparent windows. There are few if any "fully featured" platform independent renderers.

@ocornut
Copy link
Owner

ocornut commented Jun 13, 2018

I don't understand the question/request, please clarify.
Are you talking about ImDrawList?
You can already use ImDrawList independently from ImGui.

@maxkunes
Copy link
Author

maxkunes commented Jun 14, 2018

@ocornut How exactly is the best way to do that. I will for example am interested in using the d3d9 impl.

ImGui_ImplDX9_RenderDrawData has the ImDrawData* param that has some things that need to be filled. Do I have to fill these manually or is there a better way to do this. It also seems to do font drawing I will need to create an ImGui context.

@maxkunes
Copy link
Author

So here is my progress now :
Current Render

	ImGui_ImplDX9_NewFrame();
	ImGui_ImplWin32_NewFrame();
    
	static ImDrawData drawData = ImDrawData();
	drawData.DisplayPos = ImVec2(0, 0);
	drawData.DisplaySize = ImVec2(g_d3dpp.BackBufferWidth, g_d3dpp.BackBufferHeight);

	static ImDrawListSharedData sharedData;
	sharedData.ClipRectFullscreen = ImVec4(0, 0, g_d3dpp.BackBufferWidth, g_d3dpp.BackBufferHeight);
	auto* drawList = new ImDrawList(&sharedData);
	

	if(drawList->CmdBuffer.size() == 0)
		drawList->AddDrawCmd();

	drawList->PushClipRectFullScreen();
	drawList->PushTextureID(defaultFont->ContainerAtlas->TexID);
	drawList->AddText(defaultFont, 13, ImVec2(10, 200), ImColor(0, 0, 0, 255), "Test Standalone ImGui DrawList");
	drawList->PopTextureID();
	drawList->PopClipRect();
	drawList->AddCircle(ImVec2(100, 100), 100, ImColor(0, 0, 0, 255), 12, 1.f);

	drawData.CmdLists = &drawList;
	drawData.CmdListsCount = 1;
	drawData.TotalIdxCount = drawList->IdxBuffer.size();
	drawData.TotalVtxCount = drawList->VtxBuffer.size();
	drawData.Valid = true;


    g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, false);
    g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
    g_pd3dDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, false);
    D3DCOLOR clear_col_dx = D3DCOLOR_RGBA((int)(clear_color.x*255.0f), (int)(clear_color.y*255.0f), (int)(clear_color.z*255.0f), (int)(clear_color.w*255.0f));
    g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, clear_col_dx, 1.0f, 0);
    if (g_pd3dDevice->BeginScene() >= 0)
    {
        ImGui_ImplDX9_RenderDrawData(&drawData);
		delete drawList;
        g_pd3dDevice->EndScene();
    }

A couple things I'm wondering :
I don't think I should have to call AddDrawCmd, I'm guessing I'm missing something
Am I doing something wrong regarding PushClipRect / PushTextureId?
Anything else I might be misssing?

@ocornut
Copy link
Owner

ocornut commented Jun 14, 2018

It also seems to do font drawing I will need to create an ImGui context.

You can create a ImFontAtlas, build the font and use it without an ImGui context.
You aren't getting much advantage from just using an ImGui context and transparent+NoInput window or GetOverlayDrawList() if that works for you too. But otherwise what you done looks alright and should work.

There are setup minutia that would be better hidden in a helper function. (e.g. The draw list always needs a draw command (the last draw cmd is stripped when empty at the end of the frame). But this should work, and calling PushClipRectFullScreen() and PushTextureID will create the draw cmd, so I don't think you need to call AddDrawCmd() manually.

You can also reuse the ImDrawList and call Clear() each frame, this will improve performance as we rely on amortized allocations (on a newly created drawlist there will be a few realloc/memcpy which will be amortized and down to zero once the vectors have enough capacity to hold your display).

You can also setup sharedData.Font if you want to use the shorter drawList->AddText() function call.

PS: Best to use IM_COL32(0,0,0,255) instead of ImColor().

@ocornut ocornut changed the title Standalone IMGUI Renderer. Standalone ImDrawList use and rendering Jun 14, 2018
@maxkunes
Copy link
Author

maxkunes commented Jun 14, 2018

@ocornut This seems like all I needed to know. Thanks. I'll close this for now.

@ocornut
Copy link
Owner

ocornut commented May 25, 2023

I have posted a simpler example here:
#6406 (comment)

ocornut added a commit that referenced this issue Jul 12, 2023
…DrawData itself. Faclitate user-manipulation of the array (#6406, #4879, #1878) + deep swap. (#6597, #6475, #6167, #5776, #5109, #4763, #3515, #1860)

+ Metrics: avoid misleadingly iterating all layers of DrawDataBuilder as everything is flattened into Layers[0] at this point.

# Conflicts:
#	imgui.cpp
#	imgui_internal.h
ocornut added a commit that referenced this issue Jul 12, 2023
…rawList(). (#6406, #4879, #1878)

# Conflicts:
#	imgui.cpp
#	imgui_internal.h
ocornut added a commit that referenced this issue Jul 12, 2023
…s() rely on a valid command being there (especially in docking branch). (#6406, #4879, #1878)

Amend/fix dbeeeae for docking.
+ Build fix when using IMGUI_DISABLE_DEBUG_TOOLS
ocornut added a commit that referenced this issue Jul 12, 2023
…s() rely on a valid command being there (especially in docking branch). (#6406, #4879, #1878)

Amend/fix dbeeeae for docking.
+ Build fix when using IMGUI_DISABLE_DEBUG_TOOLS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants