Skip to content

Commit

Permalink
use a "last_update" instead of sleep()ing the entire application
Browse files Browse the repository at this point in the history
  • Loading branch information
surepy committed Mar 17, 2024
1 parent 457982f commit bc9988a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ using namespace tf2_bot_detector;
using namespace std::string_literals;
using namespace std::string_view_literals;

// TODO: investigate if this even prints nowadays
std::shared_ptr<IConsoleLine> TeamsSwitchedLine::TryParse(const ConsoleLineTryParseArgs& args)
{
if (args.m_Text == "Teams have been switched."sv)
Expand Down
34 changes: 21 additions & 13 deletions tf2_bot_detector/DLLMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "UI/MainWindow.h"
#include "UI/SettingsWindow.h"
#include "UI/PlayerListManagementWindow.h"
#include <chrono>

#ifdef WIN32
#include "Platform/Windows/WindowsHelpers.h"
Expand Down Expand Up @@ -134,19 +135,7 @@ TF2_BOT_DETECTOR_EXPORT int tf2_bot_detector::RunProgram(int argc, const char**
mainwin->OpenGLInit();

// renderer.RegisterDrawCallback([]() {});
renderer.RegisterDrawCallback([main_window = std::move(mainwin), &renderer, app]() {
// TODO: Put this in a different thread?
// update our main state instantly, if we are focused or we're forced by application log
if ((renderer.InFocus() || app->ShouldUpdate())) {
app->Update();
}
// update our main state, after we wait 100ms anyway (FIXME/HACK: this replicates the "FIXME" behaivor in imgui_desktop).
// https://github.com/PazerOP/imgui_desktop/blob/2e103fd66a39725a75fe93267bff1c701f246c34/imgui_desktop/src/Application.cpp#L52-L53
else {
Sleep(100);
app->Update();
}

renderer.RegisterDrawCallback([main_window = std::move(mainwin), &renderer]() {
// important note: while mainwindow handles only drawing related stuff,
// it also handles "wake from sleep", when our application log (not tf2 log!) has new stuff
main_window->Draw();
Expand All @@ -162,8 +151,27 @@ TF2_BOT_DETECTOR_EXPORT int tf2_bot_detector::RunProgram(int argc, const char**
});
}

std::chrono::milliseconds last_update;
auto now_milis = []() {
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now().time_since_epoch()
);
};

DebugLog("Entering event loop...");
while (!renderer.ShouldQuit()) {
// app is in focus or app queued update (/sleep disabled)
if ((renderer.InFocus() || app->ShouldUpdate())) {
app->Update();
last_update = now_milis();
}
// 100ms since last update (and sleeping)
// TODO: make this configurable?
else if (last_update + std::chrono::milliseconds(100) < now_milis()) {
app->Update();
last_update = now_milis();
}

renderer.DrawFrame();
}
#endif
Expand Down
1 change: 1 addition & 0 deletions tf2_bot_detector/WorldState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@ void WorldState::OnConsoleLineParsed(IWorldState& world, IConsoleLine& parsed)
if (member.m_Index < vec.size())
vec[member.m_Index] = member;

// FIXME: this seems to never update, so we might as well just not bother.
const TFTeam tfTeam = member.m_Team == LobbyMemberTeam::Defenders ? TFTeam::Red : TFTeam::Blue;
FindOrCreatePlayer(member.m_SteamID).m_Team = tfTeam;

Expand Down
12 changes: 0 additions & 12 deletions tf2_bot_detector_renderer/imgui.ini

This file was deleted.

0 comments on commit bc9988a

Please sign in to comment.