Code Monkey home page Code Monkey logo

easy-dear-imgui's Introduction

Easy Dear ImGui

Design goals

There are several variants for implementing Dear ImGui using C++. Each project has its implementation. My class has the following goals:

Single-header integration:

The class was built in a single header and is not linked to dependencies.

Don't fail:

If the window cannot be built or some state of the context of the imgui cannot be defined, it is safe to call the functions in the loop, nothing will be drawn and no exception will be thrown.

Assuming Visual Studio, a call to OutputDebugStringA through a log macro and you can directly view the line, function and reason for the failure.

Examples

Get the latest ImGui release and download my release.

The imgui files need to be in a 'dear_imgui' folder and the easy_dear_imgui.hpp outside that folder.

Visual Studio 2019

main.cpp

int __stdcall WinMain(
	_In_ HINSTANCE,
	_In_opt_ HINSTANCE,
	_In_ LPSTR,
	_In_ int
)
{
	// Use the easy_di namespace
	//
	easy_di::window window
	{
		"My Window",                                                  // Window name
		"my_window",                                                  // Class window name
		{ 0, 0 },                                                     // Start position window
		{ 1280, 720 },                                                // Start size window
		SW_SHOW,                                                      // Window show state       ( default parameter = SW_SHOW )
		CS_CLASSDC,                                                   // Window class style      ( default parameter = CS_CLASSDC )
		WS_OVERLAPPEDWINDOW,                                          // Window style            ( default parameter = WS_OVERLAPPEDWINDOW )
		// A wrapper for adding an icon has been added (add the icon to your project's resource and use your ID)
		//
		easy_di::utils::icon( 0 /*RESOURCE ICON ID*/, { 128, 128 } ), // Icon                    ( default parameter = nullptr )
		easy_di::utils::icon( 0 /*RESOURCE ICON ID*/, { 16, 16 } ),   // Small icon              ( default parameter = nullptr )
		true                                                          // Vertical sincronization ( default parameter = true )
	};

	// If process_message() returns false, the message was equal to WM_QUIT or CreateWindowExA failed and returned a nullptr handle.
	//
	while ( window.process_message() )
	{
		if ( window.imgui_start_frame() )
		{
			ImGui::ShowDemoWindow();

			window.imgui_end_frame();
		}

		window.set_background_color();
	}
}

Screenshot

Custom window procedure template

If you want to use a custom window procedure, use the template below and write your own.

LRESULT __stdcall custom_wnd_procedure( HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param )
{
	if ( ImGui_ImplWin32_WndProcHandler( hwnd, msg, w_param, l_param ) )
		return true;

	switch ( msg )
	{
	case WM_DESTROY:
		PostQuitMessage( 0 );
		return 0;
	case WM_CLOSE:
		DestroyWindow( hwnd );
		return 0;
	case WM_SIZE:
		if ( easy_di::dx::g_ptr_d3d_device && ( w_param != SIZE_MINIMIZED ) )
		{
			easy_di::dx::g_d3d_pp.BackBufferWidth  = easy_di::low_order < unsigned short, LPARAM >( l_param );
			easy_di::dx::g_d3d_pp.BackBufferHeight = easy_di::high_order< unsigned short, LPARAM >( l_param );
			easy_di::dx::reset_device();
		}
		return 0;
	default:
		break;
	}

	return DefWindowProcA( hwnd, msg, w_param, l_param );
}

Then just define g_cwnd_proc before creating the object.

// before create the object
//
easy_di::dx::g_cwnd_proc = custom_wnd_procedure;

// create the object
//
easy_di::window window
{
	"My Window",                                                  // Window name
	"my_window",                                                  // Class window name
	{ 0, 0 },                                                     // Start position window
	{ 1280, 720 },                                                // Start size window
	SW_SHOW,                                                      // Window show state       ( default parameter = SW_SHOW )
	CS_CLASSDC,                                                   // Window class style      ( default parameter = CS_CLASSDC )
	WS_OVERLAPPEDWINDOW,                                          // Window style            ( default parameter = WS_OVERLAPPEDWINDOW )
	easy_di::utils::icon( 0 /*RESOURCE ICON ID*/, { 128, 128 } ), // Icon                    ( default parameter = nullptr )
	easy_di::utils::icon( 0 /*RESOURCE ICON ID*/, { 16, 16 } ),   // Small icon              ( default parameter = nullptr )
	true                                                          // Vertical sincronization ( default parameter = true )
};

User functions

The following functions are available:

Function name Description
bool process_message() Process the message queue and message
bool imgui_start_frame() Creates a dear imgui frame.
bool imgui_end_frame() Ends a dear imgui frame.
void set_background_color(int,int,int) Define a new background color
bool get_vsync_state() const Returns the state of vertical synchronization.
MSG& get_msg() Returns a reference to m_msg

Special thanks

Thanks to @Darkratos and @Nomade040 by test and point me improvements.

easy-dear-imgui's People

Contributors

theluc4s avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.