Skip to content

Commit 4fb6e64

Browse files
ImGui Refactor
1 parent d67418b commit 4fb6e64

File tree

12 files changed

+764
-707
lines changed

12 files changed

+764
-707
lines changed

neo/framework/Common.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2461,7 +2461,9 @@ void idCommonLocal::Frame( void ) {
24612461
}
24622462

24632463
// DG: prepare new ImGui frame - I guess this is a good place, as all new events should be available?
2464-
D3::ImGuiHooks::NewFrame();
2464+
if ( imgui ) {
2465+
imgui->NewFrame();
2466+
}
24652467

24662468
com_frameTime = Sys_Milliseconds();
24672469

neo/framework/Console.cpp

Lines changed: 71 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@ class idConsoleLocal : public idConsole {
8989
void DrawTextLeftAlign( float x, float &y, const char *text, ... );
9090
void DrawTextRightAlign( float x, float &y, const char *text, ... );
9191

92-
float DrawFPS( float y );
93-
float DrawMemoryUsage( float y );
9492
float DrawAsyncStats( float y );
9593
float DrawSoundDecoders( float y );
9694

@@ -201,65 +199,77 @@ void idConsoleLocal::DrawTextRightAlign( float x, float &y, const char *text, ..
201199
idConsoleLocal::DrawFPS
202200
==================
203201
*/
204-
#define FPS_FRAMES 6
205-
float idConsoleLocal::DrawFPS( float y ) {
206-
static int previousTimes[FPS_FRAMES];
207-
static int index;
208-
static int previous;
209-
idStr showFPS;
210-
211-
// don't use serverTime, because that will be drifting to
212-
// correct for internet lag changes, timescales, timedemos, etc
213-
int t = Sys_Milliseconds();
214-
int frameTime = t - previous;
215-
previous = t;
216-
217-
previousTimes[index % FPS_FRAMES] = frameTime;
218-
index++;
219-
if ( index > FPS_FRAMES ) {
220-
// average multiple frames together to smooth changes out a bit
221-
int total = 0;
222-
for ( int i = 0 ; i < FPS_FRAMES ; i++ ) {
223-
total += previousTimes[i];
224-
}
225-
if ( !total ) {
226-
total = 1;
227-
}
228-
int fps = 1000000 * FPS_FRAMES / total;
229-
fps = ( fps + 500 ) / 1000;
230-
231-
showFPS = va( "%ifps\n", fps );
232-
}
233202

234-
static idOverlayHandle handle;
235-
PrintOverlay( handle, JUSTIFY_RIGHT, showFPS.c_str() );
236-
237-
return y + BIGCHAR_HEIGHT + 4;
238-
}
239-
240-
/*
241-
==================
242-
idConsoleLocal::DrawMemoryUsage
243-
==================
244-
*/
245-
float idConsoleLocal::DrawMemoryUsage( float y ) {
246-
memoryStats_t allocs, frees;
247-
idStr showMemStats;
248-
249-
Mem_GetStats( allocs );
250-
showMemStats = va( "Alloc Mem: %4d, %4dkB\n", allocs.num, allocs.totalSize >> 10 );
251-
252-
Mem_GetFrameStats( allocs, frees );
253-
idStr frameStats;
254-
frameStats = va( "Frame Alloc:%4d, %4dkB\nFrame Free:%4d, %4dkB\n", allocs.num, allocs.totalSize >> 10, frees.num, frees.totalSize >> 10 );
255-
showMemStats += frameStats;
203+
#define FPS_FRAMES 6
204+
#define FPS_FRAMES_HISTORY 90
256205

257-
static idOverlayHandle handle;
258-
PrintOverlay( handle, JUSTIFY_LEFT, showMemStats.c_str() );
206+
void Com_DrawOverlays() {
207+
bool toolActive = false;
259208

260-
Mem_ClearFrameStats();
209+
if ( com_showFPS.GetBool() ) {
210+
static float previousTimes[FPS_FRAMES];
211+
static int index = 0;
212+
static int previous;
213+
static int valuesOffset = 0;
214+
215+
// don't use serverTime, because that will be drifting to
216+
// correct for internet lag changes, timescales, timedemos, etc
217+
int t = Sys_Milliseconds();
218+
int frameTime = t - previous;
219+
previous = t;
220+
221+
int fps = 0;
222+
223+
const float milliSecondsPerFrame = 1000.0f / common->Get_com_engineHz_latched();
224+
225+
previousTimes[index % FPS_FRAMES] = frameTime;
226+
valuesOffset = ( valuesOffset + 1 ) % FPS_FRAMES_HISTORY;
227+
index++;
228+
if ( index > FPS_FRAMES ) {
229+
// average multiple frames together to smooth changes out a bit
230+
int total = 0;
231+
for ( int i = 0 ; i < FPS_FRAMES ; i++ ) {
232+
total += previousTimes[i];
233+
}
234+
if ( !total ) {
235+
total = 1;
236+
}
237+
fps = 1000000 * FPS_FRAMES / total;
238+
fps = ( fps + 500 ) / 1000;
239+
}
261240

262-
return y + BIGCHAR_HEIGHT + 4;
241+
#ifndef IMGUI_DISABLE
242+
ImGuiStyle& style = ImGui::GetStyle();
243+
style.FramePadding = ImVec2(0, 0);
244+
ImGui::SetNextWindowSize( ImVec2( 284, 80 ) );
245+
ImGui::SetNextWindowPos( ImVec2( renderSystem->GetScreenWidth() - 325, 20 ) );
246+
ImGui::Begin( "Framerate", &toolActive, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoInputs );
247+
ImGui::Text( "Frame Metrics" );
248+
ImGui::LabelText( va( "%i", fps ), "Average FPS" );
249+
ImGui::LabelText( va( "%i", time_gameFrame ), "Game Time (In ms)" );
250+
ImGui::End();
251+
#endif
252+
}
253+
if ( com_showMemoryUsage.GetBool() ) {
254+
#ifndef IMGUI_DISABLE
255+
memoryStats_t allocs, frees;
256+
ImGuiStyle& style = ImGui::GetStyle();
257+
style.FramePadding = ImVec2(0, 0);
258+
style.SeparatorTextAlign = ImVec2( 0.5f, 0.5f );
259+
ImGui::SetNextWindowSize( ImVec2( 324, 145 ) );
260+
ImGui::SetNextWindowPos( ImVec2( renderSystem->GetScreenWidth() - 975, 20 ) );
261+
ImGui::Begin( "Memory Usage", &toolActive, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoInputs );
262+
ImGui::SeparatorText( "Alloc Stats" );
263+
Mem_GetStats( allocs );
264+
ImGui::LabelText( va( "%4d,%4dkB\n", allocs.num, allocs.totalSize >> 10 ), "Alloc Mem" );
265+
ImGui::SeparatorText( "Frame Stats" );
266+
Mem_GetFrameStats( allocs, frees );
267+
ImGui::LabelText( va( "%4d,%4dkB %4d,%4dkB\n", allocs.num, allocs.totalSize >> 10, frees.num, frees.totalSize >> 10 ), "Alloc % Free" );
268+
ImGui::End();
269+
270+
Mem_ClearFrameStats();
271+
#endif
272+
}
263273
}
264274

265275
/*
@@ -1231,19 +1241,17 @@ void idConsoleLocal::Draw( bool forceFullScreen ) {
12311241
float lefty = LOCALSAFE_TOP;
12321242
float righty = LOCALSAFE_TOP;
12331243
float centery = LOCALSAFE_TOP;
1234-
if ( com_showFPS.GetBool() ) {
1235-
lefty = DrawFPS( lefty );
1236-
}
1237-
if ( com_showMemoryUsage.GetBool() ) {
1238-
righty = DrawMemoryUsage( righty );
1239-
}
12401244
if ( com_showAsyncStats.GetBool() ) {
12411245
righty = DrawAsyncStats( righty );
12421246
}
12431247
if ( com_showSoundDecoders.GetBool() ) {
12441248
righty = DrawSoundDecoders( righty );
1249+
}
12451250

1251+
if ( imgui ) {
1252+
imgui->OpenWindow( idImGuiWindow::WINDOW_OVERLAYS );
12461253
}
1254+
12471255
DrawOverlayText( lefty, righty, centery );
12481256
DrawDebugGraphs();
12491257
}

neo/framework/Dhewm3SettingsMenu.cpp

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,11 @@ namespace {
327327
bool nothingToClear = false;
328328
if ( bindIdx == BIND_ALL ) {
329329
if ( bindings.Num() == 0 ) {
330-
D3::ImGuiHooks::ShowWarningOverlay( "No keys are bound to this command, so there's nothing to unbind" );
330+
imguiLocal.ShowWarningOverlay( "No keys are bound to this command, so there's nothing to unbind" );
331331
nothingToClear = true;
332332
}
333333
} else if ( bindIdx < 0 || bindIdx >= bindings.Num() || bindings[bindIdx].keyNum == -1 ) {
334-
D3::ImGuiHooks::ShowWarningOverlay( "No bound key selected for unbind" );
334+
imguiLocal.ShowWarningOverlay( "No bound key selected for unbind" );
335335
nothingToClear = true;
336336
}
337337

@@ -829,7 +829,7 @@ namespace {
829829
ImGuiIO& io = ImGui::GetIO();
830830

831831
if ( newOpen ) {
832-
D3::ImGuiHooks::SetKeyBindMode( true );
832+
imguiLocal.SetKeyBindMode( true );
833833

834834
// disable keyboard and gamepad input while the bind popup is open
835835
// (the mouse can still be used to click the Cancel button, and Escape
@@ -873,7 +873,7 @@ namespace {
873873
ImGui::CloseCurrentPopup();
874874
ret = BESS_Selected;
875875
io.ConfigFlags |= (ImGuiConfigFlags_NavEnableGamepad | ImGuiConfigFlags_NavEnableKeyboard);
876-
D3::ImGuiHooks::SetKeyBindMode( false );
876+
imguiLocal.SetKeyBindMode( false );
877877
} else if ( !newOpen ) {
878878
// find out if any key is pressed and bind that (except for Esc which can't be
879879
// bound and is already handled though IsCancelKeyPressed() above)
@@ -905,7 +905,7 @@ namespace {
905905
pressedKey = K_RIGHT_SHIFT;
906906
}
907907

908-
D3::ImGuiHooks::SetKeyBindMode( false );
908+
imguiLocal.SetKeyBindMode( false );
909909

910910
const char* oldBinding = idKeyInput::GetBinding( pressedKey );
911911
if ( oldBinding[0] == '\0' ) {
@@ -920,7 +920,7 @@ namespace {
920920
const char* keyName = GetKeyName( pressedKey );
921921
idStr warning = idStr::Format( "Key '%s' is already bound to this command (%s)!",
922922
keyName, displayName.c_str() );
923-
D3::ImGuiHooks::ShowWarningOverlay( warning );
923+
imguiLocal.ShowWarningOverlay( warning );
924924
ret = BESS_Selected;
925925
// TODO: select column with that specific binding?
926926
} else {
@@ -1636,7 +1636,7 @@ namespace {
16361636
// this was just set with GLimp_SetSwapInterval(), no reason to set it again in R_CheckCvars()
16371637
r_swapInterval.ClearModified();
16381638
} else {
1639-
D3::ImGuiHooks::ShowWarningOverlay( "Setting VSync (GL SwapInterval) failed, maybe try another mode" );
1639+
imguiLocal.ShowWarningOverlay( "Setting VSync (GL SwapInterval) failed, maybe try another mode" );
16401640
}
16411641
} else {
16421642
AddTooltip( "r_swapInterval" );
@@ -1685,7 +1685,7 @@ namespace {
16851685
cvar.SetBool( enable );
16861686
if ( enable && r_enableDepthCapture.GetInteger() == 0 ) {
16871687
r_enableDepthCapture.SetInteger(-1);
1688-
D3::ImGuiHooks::ShowWarningOverlay( "Capturing the Depth Buffer was disabled.\nEnabled it because soft particles need it!" );
1688+
imguiLocal.ShowWarningOverlay( "Capturing the Depth Buffer was disabled.\nEnabled it because soft particles need it!" );
16891689
}
16901690
}
16911691
const char* descr = "! Can slow down rendering !\nSoften particle transitions when player walks through them or they cross solid geometry. Needs r_enableDepthCapture.";
@@ -1700,7 +1700,7 @@ namespace {
17001700
cvar.SetInteger( sel );
17011701
if ( sel == 0 && r_useSoftParticles.GetBool() ) {
17021702
r_useSoftParticles.SetBool( false );
1703-
D3::ImGuiHooks::ShowWarningOverlay( "You disabled capturing the Depth Buffer.\nDisabling Soft Particles because they need the depth buffer texture." );
1703+
imguiLocal.ShowWarningOverlay( "You disabled capturing the Depth Buffer.\nDisabling Soft Particles because they need the depth buffer texture." );
17041704
}
17051705
}
17061706
AddCVarOptionTooltips( cvar );
@@ -2030,7 +2030,7 @@ namespace {
20302030
} else {
20312031
idSoundSystemLocal::s_device.SetString( alDevices[selAlDevice] );
20322032
}
2033-
D3::ImGuiHooks::ShowWarningOverlay( "Changing the sound device only takes effect after restarting the game!" );
2033+
imguiLocal.ShowWarningOverlay( "Changing the sound device only takes effect after restarting the game!" );
20342034
}
20352035
AddTooltip( "s_device" );
20362036

@@ -2045,7 +2045,7 @@ namespace {
20452045
if ( ImGui::Checkbox( "Use EAX/EFX Reverb Effects", &useReverb ) ) {
20462046
idSoundSystemLocal::s_useEAXReverb.SetBool( useReverb );
20472047
if ( useReverb != idSoundSystemLocal::useEFXReverb ) {
2048-
D3::ImGuiHooks::ShowWarningOverlay( "Enabling/disabling EFX only takes effect after restarting the game!" );
2048+
imguiLocal.ShowWarningOverlay( "Enabling/disabling EFX only takes effect after restarting the game!" );
20492049
}
20502050
}
20512051
AddTooltip( "s_useEAXReverb" );
@@ -2289,7 +2289,7 @@ namespace {
22892289
// and possibly containing '?' from non-translatable unicode chars
22902290
D3_ISO8859_1toUTF8( ui_nameVar->GetString(), playerNameBuf, sizeof(playerNameBuf) );
22912291
} else {
2292-
D3::ImGuiHooks::ShowWarningOverlay( "Player Name way too long (max 40 chars) or contains invalid UTF-8 encoding!" );
2292+
imguiLocal.ShowWarningOverlay( "Player Name way too long (max 40 chars) or contains invalid UTF-8 encoding!" );
22932293
playerNameBuf[0] = '\0';
22942294
}
22952295
}
@@ -2303,23 +2303,23 @@ namespace {
23032303

23042304
static void DrawOtherOptionsMenu( void ) {
23052305
ImGui::Spacing();
2306-
float scale = D3::ImGuiHooks::GetScale();
2306+
float scale = imguiLocal.GetScale();
23072307
if ( ImGui::DragFloat("ImGui scale", &scale, 0.005f, 0.25f, 8.0f, "%.3f") ) {
2308-
D3::ImGuiHooks::SetScale( scale );
2308+
imguiLocal.SetScale( scale );
23092309
}
23102310
ImGui::SameLine();
23112311
if ( ImGui::Button("Reset") ) {
2312-
D3::ImGuiHooks::SetScale( -1.0f );
2312+
imguiLocal.SetScale( -1.0f );
23132313
}
23142314

23152315
int style_idx = imgui_style.GetInteger();
23162316
if ( ImGui::Combo( "ImGui Style", &style_idx, "dhewm3\0ImGui Default\0Userstyle\0") )
23172317
{
23182318
switch( style_idx )
23192319
{
2320-
case 0: D3::ImGuiHooks::SetImGuiStyle( D3::ImGuiHooks::Style::Dhewm3 ); break;
2321-
case 1: D3::ImGuiHooks::SetImGuiStyle( D3::ImGuiHooks::Style::ImGui_Default ); break;
2322-
case 2: D3::ImGuiHooks::SetImGuiStyle( D3::ImGuiHooks::Style::User ); break;
2320+
case 0: imguiLocal.SetImGuiStyle( idImGuiStyle::Dhewm3 ); break;
2321+
case 1: imguiLocal.SetImGuiStyle( idImGuiStyle::ImGui_Default ); break;
2322+
case 2: imguiLocal.SetImGuiStyle( idImGuiStyle::User ); break;
23232323
}
23242324
imgui_style.SetInteger( style_idx );
23252325
}
@@ -2331,14 +2331,14 @@ namespace {
23312331
ImGui::SameLine();
23322332

23332333
if ( ImGui::Button( "Write Userstyle" ) ) {
2334-
D3::ImGuiHooks::WriteUserStyle();
2334+
imguiLocal.WriteUserStyle();
23352335
imgui_style.SetInteger( 2 );
23362336
}
23372337
AddTooltip( "Writes the current style settings (incl. colors) as userstyle" );
23382338

23392339
static bool onlyChanges = false;
23402340
if ( ImGui::Button( "Copy style code to clipboard" ) ) {
2341-
D3::ImGuiHooks::CopyCurrentStyle( onlyChanges );
2341+
imguiLocal.CopyCurrentStyle( onlyChanges );
23422342
}
23432343
AddTooltip( "Generates C++ code for the current style settings (incl. colors) and copies it into the clipboard" );
23442344

@@ -2350,7 +2350,7 @@ namespace {
23502350
ImGui::Spacing();
23512351

23522352
if ( ImGui::Button( "Show ImGui Demo" ) ) {
2353-
D3::ImGuiHooks::OpenWindow( D3::ImGuiHooks::D3_ImGuiWin_Demo );
2353+
imguiLocal.OpenWindow( idImGuiWindow::WINDOW_DEMO );
23542354
}
23552355
}
23562356
} //anon namespace
@@ -2386,7 +2386,7 @@ static void InitSettingsMenu( void ) {
23862386
settingsWinInitialized = true;
23872387
}
23882388

2389-
// called from D3::ImGuiHooks::NewFrame() (if this window is enabled)
2389+
// called from imguiLocal.NewFrame() (if this window is enabled)
23902390
void Com_DrawSettingsMenu( void ) {
23912391
bool showSettingsWindow = true;
23922392

@@ -2469,14 +2469,14 @@ void Com_DrawSettingsMenu( void ) {
24692469

24702470
ImGui::End();
24712471
if ( !showSettingsWindow ) {
2472-
D3::ImGuiHooks::CloseWindow( D3::ImGuiHooks::D3_ImGuiWin_Settings );
2472+
imguiLocal.CloseWindow( idImGuiWindow::WINDOW_SETTINGS );
24732473
}
24742474
}
24752475

24762476
/*!
24772477
WARNING: Don't call this function directly, always use
2478-
D3::ImGuiHooks::OpenWindow( D3::ImGuiHooks::D3_ImGuiWin_Settings )
2479-
or D3::ImGuiHooks::CloseWindow( D3::ImGuiHooks::D3_ImGuiWin_Settings )
2478+
imguiLocal.OpenWindow( idImGuiWindow::WINDOW_SETTINGS )
2479+
or imguiLocal.CloseWindow( idImGuiWindow::WINDOW_SETTINGS )
24802480
(unless you're implementing those two functions, they call this..)
24812481
*/
24822482
void Com_OpenCloseSettingsMenu( bool open ) {
@@ -2505,14 +2505,14 @@ void Com_OpenCloseSettingsMenu( bool open ) {
25052505
}
25062506

25072507
void Com_Settings_f( const idCmdArgs &args ) {
2508-
bool menuOpen = ( D3::ImGuiHooks::GetOpenWindowsMask() & D3::ImGuiHooks::D3_ImGuiWin_Settings ) != 0;
2508+
bool menuOpen = ( imguiLocal.GetOpenWindowsMask() & idImGuiWindow::WINDOW_SETTINGS ) != 0;
25092509
if ( !menuOpen ) {
2510-
D3::ImGuiHooks::OpenWindow( D3::ImGuiHooks::D3_ImGuiWin_Settings );
2510+
imguiLocal.OpenWindow( idImGuiWindow::WINDOW_SETTINGS );
25112511
} else {
25122512
if ( ImGui::IsWindowFocused( ImGuiFocusedFlags_AnyWindow ) ) {
25132513
// if the settings window is open and an ImGui window has focus,
25142514
// close the settings window when "settings" is executed
2515-
D3::ImGuiHooks::CloseWindow( D3::ImGuiHooks::D3_ImGuiWin_Settings );
2515+
imguiLocal.CloseWindow( idImGuiWindow::WINDOW_SETTINGS );
25162516
} else {
25172517
// if the settings window is open but no ImGui window has focus,
25182518
// give focus to one of the ImGui windows.

neo/framework/Game.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,6 @@ If you have questions concerning this license or the applicable additional terms
2929
#ifndef __GAME_H__
3030
#define __GAME_H__
3131

32-
#include "idlib/BitMsg.h"
33-
#include "idlib/Dict.h"
34-
#include "idlib/containers/StrList.h"
35-
#include "framework/UsercmdGen.h"
36-
#include "renderer/RenderWorld.h"
37-
#include "sound/sound.h"
38-
3932
class idAASFileManager;
4033
class idCollisionModelManager;
4134
class idRenderSystem;

0 commit comments

Comments
 (0)