Skip to content

Commit

Permalink
i am the super star of imgui
Browse files Browse the repository at this point in the history
i
  • Loading branch information
wolf109909 committed Dec 22, 2023
1 parent 460eb72 commit 1d7b2dc
Show file tree
Hide file tree
Showing 28 changed files with 11,932 additions and 1,076 deletions.
1 change: 1 addition & 0 deletions NorthstarDLL/NorthstarDLL.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ IF EXIST "$(SolutionDir)..\NorthstarCN_Binaries\Northstar.dll" del "$(SolutionDi
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="client\audio.h" />
<ClInclude Include="client\fontawesome.h" />
<ClInclude Include="client\imgui.h" />
<ClInclude Include="client\kcpstats.h" />
<ClInclude Include="client\localchatwriter.h" />
Expand Down
3 changes: 3 additions & 0 deletions NorthstarDLL/NorthstarDLL.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,9 @@
<ClInclude Include="engine\netgraph.h">
<Filter>Header Files\engine</Filter>
</ClInclude>
<ClInclude Include="client\fontawesome.h">
<Filter>Header Files\client</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="client\audio.cpp">
Expand Down
9,673 changes: 9,673 additions & 0 deletions NorthstarDLL/client/fontawesome.h

Large diffs are not rendered by default.

113 changes: 26 additions & 87 deletions NorthstarDLL/client/imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "imgui/implot.h"
#include "imgui/imgui_internal.h"
#include <span>
#include "fontawesome.h"


std::vector<imgui_draw*> draw_functions;

Expand All @@ -25,15 +27,23 @@ ImFont* IMGUI_FONT_MSYHBD_16 = NULL;
ImFont* IMGUI_FONT_MSYHBD_22 = NULL;
ImFont* IMGUI_FONT_MSYHBD_28 = NULL;

ImFont* FONT_FONTAWESOME = NULL;

LRESULT __stdcall WndProc(const HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{

ImGuiIO& io = ImGui::GetIO();
if (ImGui_ImplWin32_WndProcHandler(hWnd, uMsg, wParam, lParam))
return true;

if (io.WantCaptureMouse)
return true;
return CallWindowProc(oWndProc, hWnd, uMsg, wParam, lParam);
}

static const ImWchar GLYPH_FUCK_YOU[] = {
0xE005, 0xF8FF,
0,
};

void InitImGui()
{
ImGui::CreateContext();
Expand All @@ -42,14 +52,15 @@ void InitImGui()
io.ConfigFlags = ImGuiConfigFlags_NoMouseCursorChange;
ImGui_ImplWin32_Init(window);
ImGui_ImplDX11_Init(pDevice, pContext);
io.Fonts->AddFontDefault();
// Base font
//io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\msyh.ttc", 13.0f, nullptr, io.Fonts->GetGlyphRangesChineseFull());
// Bold headings H2 and H3
io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\msyh.ttc", 22.0f, nullptr, io.Fonts->GetGlyphRangesChineseSimplifiedCommon());
IMGUI_FONT_MSYHBD_22 =
io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\msyhbd.ttc", 22.0f, nullptr, io.Fonts->GetGlyphRangesChineseSimplifiedCommon());
IMGUI_FONT_MSYHBD_28 =
io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\msyhbd.ttc", 28.0f, nullptr, io.Fonts->GetGlyphRangesChineseSimplifiedCommon());
//io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\msyh.ttc", 22.0f, nullptr, io.Fonts->GetGlyphRangesChineseSimplifiedCommon());
//IMGUI_FONT_MSYHBD_22 =
// io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\msyhbd.ttc", 22.0f, nullptr, io.Fonts->GetGlyphRangesChineseSimplifiedCommon());
//IMGUI_FONT_MSYHBD_28 =
// io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\msyhbd.ttc", 28.0f, nullptr, io.Fonts->GetGlyphRangesChineseSimplifiedCommon());
/*IMGUI_FONT_MSYH_13 =
io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\msyh.ttc", 13.0f, nullptr, io.Fonts->GetGlyphRangesChineseFull());
IMGUI_FONT_MSYH_16 =
Expand All @@ -65,90 +76,16 @@ void InitImGui()
io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\msyhbd.ttc", 22.0f, nullptr, io.Fonts->GetGlyphRangesChineseFull());
IMGUI_FONT_MSYHBD_36 =
io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\msyhbd.ttc", 36.0f, nullptr, io.Fonts->GetGlyphRangesChineseFull());*/

spdlog::info("[IMGUI] Custom fonts loaded.");

// gamma-correct style
const auto ToLinear = [](float x)
{
if (x <= 0.04045f)
{
return x / 12.92f;
}
else
{
return std::pow((x + 0.055f) / 1.055f, 2.4f);
}
};
for (auto& c : std::span {ImGui::GetStyle().Colors, size_t(ImGuiCol_COUNT)})
{
c = {ToLinear(c.x), ToLinear(c.y), ToLinear(c.z), c.w};
}
ImFontConfig icons_config;
icons_config.MergeMode = true;
icons_config.PixelSnapH = true;

ImGuiStyle* style = &ImGui::GetStyle();

style->WindowPadding = ImVec2(15, 15);
style->WindowRounding = 5.0f;
style->FramePadding = ImVec2(5, 5);
style->FrameRounding = 4.0f;
style->ItemSpacing = ImVec2(12, 8);
style->ItemInnerSpacing = ImVec2(8, 6);
style->IndentSpacing = 25.0f;
style->ScrollbarSize = 15.0f;
style->ScrollbarRounding = 9.0f;
style->GrabMinSize = 5.0f;
style->GrabRounding = 3.0f;

style->Colors[ImGuiCol_Text] = ImVec4(0.80f, 0.80f, 0.83f, 1.00f);
style->Colors[ImGuiCol_TextDisabled] = ImVec4(0.24f, 0.23f, 0.29f, 1.00f);
style->Colors[ImGuiCol_WindowBg] = ImVec4(0.06f, 0.05f, 0.07f, 1.00f);
//style->Colors[ImGuiCol_ChildWindowBg] = ImVec4(0.07f, 0.07f, 0.09f, 1.00f);
style->Colors[ImGuiCol_PopupBg] = ImVec4(0.07f, 0.07f, 0.09f, 1.00f);
style->Colors[ImGuiCol_Border] = ImVec4(0.80f, 0.80f, 0.83f, 0.88f);
style->Colors[ImGuiCol_BorderShadow] = ImVec4(0.92f, 0.91f, 0.88f, 0.00f);
style->Colors[ImGuiCol_FrameBg] = ImVec4(0.10f, 0.09f, 0.12f, 1.00f);
style->Colors[ImGuiCol_FrameBgHovered] = ImVec4(0.24f, 0.23f, 0.29f, 1.00f);
style->Colors[ImGuiCol_FrameBgActive] = ImVec4(0.56f, 0.56f, 0.58f, 1.00f);
style->Colors[ImGuiCol_TitleBg] = ImVec4(0.10f, 0.09f, 0.12f, 1.00f);
style->Colors[ImGuiCol_TitleBgCollapsed] = ImVec4(1.00f, 0.98f, 0.95f, 0.75f);
style->Colors[ImGuiCol_TitleBgActive] = ImVec4(0.07f, 0.07f, 0.09f, 1.00f);
style->Colors[ImGuiCol_MenuBarBg] = ImVec4(0.10f, 0.09f, 0.12f, 1.00f);
style->Colors[ImGuiCol_ScrollbarBg] = ImVec4(0.10f, 0.09f, 0.12f, 1.00f);
style->Colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.80f, 0.80f, 0.83f, 0.31f);
style->Colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.56f, 0.56f, 0.58f, 1.00f);
style->Colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.06f, 0.05f, 0.07f, 1.00f);
//style->Colors[ImGuiCol_ComboBg] = ImVec4(0.19f, 0.18f, 0.21f, 1.00f);
style->Colors[ImGuiCol_CheckMark] = ImVec4(0.80f, 0.80f, 0.83f, 0.31f);
style->Colors[ImGuiCol_SliderGrab] = ImVec4(0.80f, 0.80f, 0.83f, 0.31f);
style->Colors[ImGuiCol_SliderGrabActive] = ImVec4(0.06f, 0.05f, 0.07f, 1.00f);
style->Colors[ImGuiCol_Button] = ImVec4(0.10f, 0.09f, 0.12f, 1.00f);
style->Colors[ImGuiCol_ButtonHovered] = ImVec4(0.24f, 0.23f, 0.29f, 1.00f);
style->Colors[ImGuiCol_ButtonActive] = ImVec4(0.56f, 0.56f, 0.58f, 1.00f);
style->Colors[ImGuiCol_Header] = ImVec4(0.10f, 0.09f, 0.12f, 1.00f);
style->Colors[ImGuiCol_HeaderHovered] = ImVec4(0.56f, 0.56f, 0.58f, 1.00f);
style->Colors[ImGuiCol_HeaderActive] = ImVec4(0.06f, 0.05f, 0.07f, 1.00f);
//style->Colors[ImGuiCol_Column] = ImVec4(0.56f, 0.56f, 0.58f, 1.00f);
//style->Colors[ImGuiCol_ColumnHovered] = ImVec4(0.24f, 0.23f, 0.29f, 1.00f);
//style->Colors[ImGuiCol_ColumnActive] = ImVec4(0.56f, 0.56f, 0.58f, 1.00f);
style->Colors[ImGuiCol_ResizeGrip] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
style->Colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.56f, 0.56f, 0.58f, 1.00f);
style->Colors[ImGuiCol_ResizeGripActive] = ImVec4(0.06f, 0.05f, 0.07f, 1.00f);
//style->Colors[ImGuiCol_CloseButton] = ImVec4(0.40f, 0.39f, 0.38f, 0.16f);
//style->Colors[ImGuiCol_CloseButtonHovered] = ImVec4(0.40f, 0.39f, 0.38f, 0.39f);
//style->Colors[ImGuiCol_CloseButtonActive] = ImVec4(0.40f, 0.39f, 0.38f, 1.00f);
style->Colors[ImGuiCol_PlotLines] = ImVec4(0.40f, 0.39f, 0.38f, 0.63f);
style->Colors[ImGuiCol_PlotLinesHovered] = ImVec4(0.25f, 1.00f, 0.00f, 1.00f);
style->Colors[ImGuiCol_PlotHistogram] = ImVec4(0.40f, 0.39f, 0.38f, 0.63f);
style->Colors[ImGuiCol_PlotHistogramHovered] = ImVec4(0.25f, 1.00f, 0.00f, 1.00f);
style->Colors[ImGuiCol_TextSelectedBg] = ImVec4(0.25f, 1.00f, 0.00f, 0.43f);
//style->Colors[ImGuiCol_ModalWindowDarkening] = ImVec4(1.00f, 0.98f, 0.95f, 0.73f);
imgui_isready = true;
FONT_FONTAWESOME =
io.Fonts->AddFontFromMemoryCompressedBase85TTF(FontAwesome_compressed_data_base85, 16.0f, &icons_config, &GLYPH_FUCK_YOU[0]);
io.Fonts->GetGlyphRangesDefault();
}

bool imgui_ready()
{
return imgui_isready;
}

HRESULT __stdcall hkPresent(IDXGISwapChain* pSwapChain, UINT SyncInterval, UINT Flags)
{
Expand Down Expand Up @@ -273,3 +210,5 @@ void imgui_setup()
std::jthread setup_thread(setup_thread_func);
setup_thread.detach();
}


9 changes: 2 additions & 7 deletions NorthstarDLL/client/imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,10 @@ typedef void imgui_draw(ID3D11Device* pDevice);

extern std::vector<imgui_draw*> draw_functions;

bool imgui_ready();

void imgui_setup();

void imgui_add_draw(imgui_draw* func);


#ifndef IMGUI_FONTS
#define IMGUI_FONTS

extern ImFont* IMGUI_FONT_MSYH_13;
extern ImFont* IMGUI_FONT_MSYH_16;
extern ImFont* IMGUI_FONT_MSYH_22;
Expand All @@ -50,4 +44,5 @@ extern ImFont* IMGUI_FONT_MSYHBD_13;
extern ImFont* IMGUI_FONT_MSYHBD_16;
extern ImFont* IMGUI_FONT_MSYHBD_22;
extern ImFont* IMGUI_FONT_MSYHBD_28;
#endif
extern ImFont* FONT_FONTAWESOME;

70 changes: 17 additions & 53 deletions NorthstarDLL/client/kcpstats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,20 @@
#include "core/hooks.h"
#include "shared/ikcp.h"
#include "shared/kcpintegration.h"
#include "engine/netgraph.h"
#include "fontawesome.h"

ConVar* Cvar_kcp_stats;
ConVar* Cvar_kcp_stats_interval;

AUTOHOOK_INIT()

const char* KCP_NETGRAPH_LABELS[] = {" SRTT", "LOS%", "RTS%", "RCS%"};

sliding_window sw_retrans_segs(10, false, true);
sliding_window sw_lost_segs(10, false, true);
sliding_window sw_out_segs(10, false, true);

sliding_window sw_recon_segs(10, false, true);
sliding_window sw_in_segs(10, false, true);

sliding_window sw_srtt(50);
sliding_window sw_rts(50);
sliding_window sw_los(50);
sliding_window sw_rcs(50);

IUINT32 last_rotate = iclock();
bool has_connection = false;

void draw_kcp_stats(ID3D11Device* device)
{
/*
if (Cvar_kcp_stats == nullptr || !Cvar_kcp_stats->GetBool() || !g_kcp_initialized())
{
if (Cvar_kcp_stats == nullptr || !Cvar_kcp_stats->GetBool())
{
return;
}
if (itimediff(iclock(), last_rotate) > Cvar_kcp_stats_interval->GetInt())
{
auto kcp_stats = g_kcp_manager->get_stats();
if (kcp_stats.size() > 0)
{
sw_srtt.rotate(kcp_stats[0].second.srtt);
sw_retrans_segs.rotate(kcp_stats[0].second.retrans_segs);
sw_lost_segs.rotate(kcp_stats[0].second.lost_segs);
sw_out_segs.rotate(kcp_stats[0].second.out_segs);
sw_in_packets.rotate(kcp_stats[0].second.in_packets);
sw_reconstruct_packets.rotate(kcp_stats[0].second.reconstruct_packets);
sw_rts.rotate(100.0 * sw_retrans_segs.sum() / (sw_out_segs.sum() == 0 ? 1 : sw_out_segs.sum()));
sw_los.rotate(100.0 * sw_lost_segs.sum() / (sw_out_segs.sum() == 0 ? 1 : sw_out_segs.sum()));
sw_rcs.rotate(100.0 * sw_reconstruct_packets.sum() / (sw_in_packets.sum() == 0 ? 1 : sw_in_packets.sum()));
has_connection = true;
}
else
{
has_connection = false;
}
last_rotate = iclock();
}

ImGuiWindowFlags window_flags = 0;
window_flags |= ImGuiWindowFlags_NoDecoration;
window_flags |= ImGuiWindowFlags_AlwaysAutoResize;
Expand All @@ -83,10 +41,17 @@ void draw_kcp_stats(ID3D11Device* device)
auto viewport_size = main_viewport->WorkSize;
ImGui::SetWindowPos(ImVec2(viewport_pos.x + viewport_size.x - current_size.x, viewport_pos.y));

if (!has_connection)
auto ng = NetGraphSink::instance();

if (ng->windows.empty())
{
ImGui::Text("No KCP connection");
}
//auto fa =
ImGui::PushFont(FONT_FONTAWESOME);
ImGui::Text("\xef\x82\xabNo KCP connection\xef\x82\xaa");

ImGui::PopFont();
ImGui::ShowMetricsWindow();
} /*
else
{
if (ImGui::BeginTable("kcp_stats", 8))
Expand Down Expand Up @@ -197,15 +162,14 @@ void draw_kcp_stats(ID3D11Device* device)
ImPlot::PopStyleColor(2);
}
*/
ImGui::End();
*/

}

ON_DLL_LOAD("client.dll", KCPSTATS, (CModule module))
{
Cvar_kcp_stats = new ConVar("kcp_stats", "0", FCVAR_NONE, "kcp stats window");
Cvar_kcp_stats_interval = new ConVar("kcp_stats_interval", "100", FCVAR_NONE, "kcp stats interval");
imgui_add_draw(draw_kcp_stats);
}

Expand Down
4 changes: 2 additions & 2 deletions NorthstarDLL/client/kcpstats.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

extern ConVar* Cvar_kcp_stats;
extern ConVar* Cvar_kcp_stats_interval;

class linear_regression
{
Expand All @@ -25,7 +24,7 @@ class linear_regression
};

const int MAX_DEGREE = 10;
std::vector<std::vector<std::vector<double>>> CORRECTION_DATA {
inline std::vector<std::vector<std::vector<double>>> CORRECTION_DATA {
{}, // not defined for degree 0
{}, // no correction required for degree 2
{}, // no correction required for degree 4
Expand Down Expand Up @@ -65,6 +64,7 @@ struct sliding_window
double last_val = 0;
double sumed = 0;

inline sliding_window() : sliding_window(50, false, false) {};
sliding_window(size_t samples);
sliding_window(size_t samples, bool smooth, bool delta);
~sliding_window();
Expand Down
17 changes: 13 additions & 4 deletions NorthstarDLL/engine/netgraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ void sendThreadPayload(std::stop_token stoken, NetGraphSink* ng)
for (const auto& entry : manager->routingTable)
{
NetBuffer buf(128, 0, 128);
ng->localStat.encode(buf);
std::shared_lock lk(ng->windowsMutex);
std::get<0>(ng->windows[entry.first]).encode(buf);
entry.second.first.second->sendto(std::move(buf), entry.first, NetGraphSink::instance().get());
}
}
Expand All @@ -43,9 +44,8 @@ int NetGraphSink::input(NetBuffer&& buf, const NetContext& ctx, const NetSource*
{
NetStats s {};
s.decode(buf);
std::shared_lock lk(remoteStatsMutex);
remoteStats.insert(std::make_pair(ctx, s));
NS::log::NEW_NET->error("[NG] {}: {} {}", ctx, s.frameTime, s.lostsegs);
std::shared_lock lk(windowsMutex);
std::get<3>(windows[ctx]).rotate(s);
return 0;
}

Expand Down Expand Up @@ -100,3 +100,12 @@ void NetStats::sync(ikcpcb* cb)
insegs = cb->insegs;
reconsegs = cb->reconsegs;
}

void NetSlidingWindows::rotate(const NetStats& s) {
sw_frameTime.rotate(s.frameTime);
sw_outsegs.rotate(s.outsegs);
sw_lostsegs.rotate(s.lostsegs);
sw_retranssegs.rotate(s.retranssegs);
sw_insegs.rotate(s.insegs);
sw_reconsegs.rotate(s.reconsegs);
}
Loading

0 comments on commit 1d7b2dc

Please sign in to comment.