Skip to content

Commit d80a82b

Browse files
Merge branch 'main-loop'
2 parents cb8bc84 + e8d29f8 commit d80a82b

24 files changed

+291
-198
lines changed

Hayzen/src/Core/Game.cpp

+5-7
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,17 @@ void Game::Init()
2323
RegisterFontAndMaterial();
2424
}
2525

26-
void Game::Update()
26+
void Game::SCR_DrawScreenFieldHook(const int localClientNum, int refreshedUI)
2727
{
28+
// Call the original SCR_DrawScreenField function
29+
SCR_DrawScreenFieldStub(localClientNum, refreshedUI);
30+
2831
// If the menu is not initialized, no need to go further
2932
if (!s_Menu.IsInitialized())
3033
return;
3134

35+
// Update the menu
3236
s_Menu.Update();
33-
}
34-
35-
void Game::SCR_DrawScreenFieldHook(const int localClientNum, int refreshedUI)
36-
{
37-
// Call the original SCR_DrawScreenField function
38-
SCR_DrawScreenFieldStub(localClientNum, refreshedUI);
3937

4038
// Render the menu
4139
s_Menu.Render();

Hayzen/src/Core/Game.h

+3-6
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ class Game
1212

1313
// Common initialization behavior to all games.
1414
virtual void Init() = 0;
15-
16-
// Update and render the menu.
17-
void Update();
1815
protected:
1916
static Menu s_Menu;
2017
static Option s_RootOption;
@@ -27,11 +24,11 @@ class Game
2724
// Create the menu structure (one implementation per game).
2825
virtual void CreateStructure() = 0;
2926

27+
// Hook of a function that gets called every frame to execute the main loop on the game's thread.
28+
static void SCR_DrawScreenFieldHook(const int localClientNum, int refreshedUI);
29+
3030
// Stub to hold the original code of SCR_DrawScreenField.
3131
static void SCR_DrawScreenFieldStub(const int localClientNum, int refreshedUI);
32-
33-
// Render the menu.
34-
static void SCR_DrawScreenFieldHook(const int localClientNum, int refreshedUI);
3532
private:
3633
// Make the global drawing function pointers point to the current game's drawing functions.
3734
void SetDrawFunctionsPointers();

Hayzen/src/Core/Menu.cpp

+2-51
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ void Menu::Init(int iClientNum, Option *pOption)
2020
m_Instructions = Text("Navigate: " CHAR_UP " - " CHAR_DOWN " | Select: " CHAR_X " | Back: " CHAR_RS,
2121
HudElem::s_MenuX + HudElem::s_Padding, HudElem::s_MenuY + HudElem::s_MenuHeight - HudElem::s_Padding - 80.0f, HudElem::s_ColorWhite, 0.7f);
2222

23-
m_Feedback = Text("", 50.0f, HudElem::s_MenuY + HudElem::s_MenuHeight - HudElem::s_Padding - 120.0f, HudElem::s_ColorWhite, 0.7f);
24-
2523
// Initialize the scroller position
2624
m_iCurrentScrollerPos = 0;
2725

@@ -101,49 +99,8 @@ void Menu::Update()
10199

102100
void Menu::Render()
103101
{
104-
// If the menu is not initialized, don't go further
105-
if (!m_bInitialized)
106-
return;
107-
108-
// This is a terrible way of getting the feedback text to disappear after
109-
// three seconds but I didn't find anything else that worked
110-
111-
// Initialize the timer
112-
static DWORD dwMilliseconds = 0;
113-
114-
// Update the feedback text if one has been pushed to the queue
115-
if (!m_ChangeFeedbackTextQueue.empty())
116-
{
117-
m_Feedback.SetText(m_ChangeFeedbackTextQueue.front());
118-
m_ChangeFeedbackTextQueue.pop();
119-
120-
// Reset the timer when the feedback text gets set
121-
dwMilliseconds = 0;
122-
}
123-
124-
// Only render the feedback text if it's set
125-
if (m_Feedback.GetText() != "")
126-
{
127-
// We incremement the timer by 16 because the game runs at 60fps with means
128-
// a frame is rendered every 16 milliseconds (1000 / 60 = 16)
129-
dwMilliseconds += 16;
130-
131-
// If more than three seconds have elapsed, reset the timer and the feedback
132-
// text
133-
if (dwMilliseconds > 3000)
134-
{
135-
m_Feedback.SetText("");
136-
dwMilliseconds = 0;
137-
}
138-
139-
// Only render the feedback text if it's still set (three seconds have not
140-
// elapsed yet)
141-
if (m_Feedback.GetText() != "")
142-
m_Feedback.Draw();
143-
}
144-
145-
// If the menu is not open, don't go further
146-
if (!m_bOpen)
102+
// If the menu is not initialized or not open, don't go further
103+
if (!m_bInitialized || !m_bOpen)
147104
return;
148105

149106
// Draw the constant HUD elements
@@ -177,12 +134,6 @@ void Menu::Stop()
177134
m_pBotEntity = nullptr;
178135
}
179136

180-
void Menu::SetFeedbackText(const std::string &strText)
181-
{
182-
// Set the feedback text
183-
m_ChangeFeedbackTextQueue.push(strText);
184-
}
185-
186137
void Menu::SetCurrentOption(Option *pOption)
187138
{
188139
// Reset the scroller position

Hayzen/src/Core/Menu.h

-5
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ class Menu
4242
void *GetBot() const { return m_pBotEntity; }
4343
void SetBot(void *pBotEntity) { m_pBotEntity = pBotEntity; }
4444

45-
// Push a new feedback text to the queue.
46-
void SetFeedbackText(const std::string &strText);
47-
4845
// Change the current menu section.
4946
void SetCurrentOption(Option *pOption);
5047
private:
@@ -55,13 +52,11 @@ class Menu
5552

5653
Option *m_pCurrentOption;
5754
std::queue<Option *> m_ChangeSectionQueue;
58-
std::queue<std::string> m_ChangeFeedbackTextQueue;
5955

6056
Rectangle m_Background;
6157
Text m_Title;
6258
Rectangle m_Scroller;
6359
Text m_Instructions;
64-
Text m_Feedback;
6560
int m_iCurrentScrollerPos;
6661

6762
vec3 m_SavedPos;

Hayzen/src/Core/Plugin.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@ DWORD Plugin::Update(void *)
4949
// Initialize a new game if the user launches a new game
5050
if (dwNewTitle != s_dwCurrentTitle)
5151
InitNewGame(dwNewTitle);
52-
53-
// Only update the current game if it's supported
54-
if (s_CurrentGame)
55-
s_CurrentGame->Update();
5652
}
5753

5854
return 0;

Hayzen/src/Games/Alpha/MW2/GameFunctions.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ static std::unordered_map<std::string, DWORD> BrushModelMap;
1010

1111
const char *(*SL_ConvertToString)(uint32_t stringValue) = reinterpret_cast<const char *(*)(uint32_t)>(0x8229A730);
1212

13-
void (*Cbuf_AddText)(int localClientNum, const char *text) = reinterpret_cast<void(*)(int, const char *)>(0x8226F590);
13+
void (*SV_GameSendServerCommand)(int clientNum, int type, const char *text) = reinterpret_cast<void(*)(int, int, const char *)>(0x822B6140);
1414

1515
bool (*Dvar_GetBool)(const char *dvarName) = reinterpret_cast<bool(*)(const char *)>(0x82303B00);
1616

@@ -40,6 +40,11 @@ void (*SV_ExecuteClientCommand)(int client, const char *s, int clientOK, int fro
4040

4141
void (*TeleportPlayer)(gentity_s *player, const float *origin, const float *angles) = reinterpret_cast<void(*)(gentity_s *, const float *, const float *)>(0x8222E5A0);
4242

43+
void iPrintLn(int clientNum, const std::string &text)
44+
{
45+
SV_GameSendServerCommand(clientNum, 0, Formatter::Format("f \"%s\"", text.c_str()).c_str());
46+
}
47+
4348
gclient_s *GetGClient(int clientNum)
4449
{
4550
return reinterpret_cast<gclient_s *>(0x82F01480 + sizeof(gclient_s) * clientNum);
@@ -50,6 +55,11 @@ gentity_s *GetEntity(int entNum)
5055
return reinterpret_cast<gentity_s *>(0x82D47D80 + sizeof(gentity_s) * entNum);
5156
}
5257

58+
void SetClientDvar(int clientNum, const std::string &dvar, const std::string &value)
59+
{
60+
SV_GameSendServerCommand(clientNum, 0, Formatter::Format("v %s \"%s\"", dvar.c_str(), value.c_str()).c_str());
61+
}
62+
5363
bool IsHost(int clientNum)
5464
{
5565
return Session_IsHost(0x83A06F28, clientNum);

Hayzen/src/Games/Alpha/MW2/GameFunctions.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace AlphaMW2GameFunctions
1111

1212
extern const char *(*SL_ConvertToString)(uint32_t stringValue);
1313

14-
extern void (*Cbuf_AddText)(int localClientNum, const char *text);
14+
extern void (*SV_GameSendServerCommand)(int clientNum, int type, const char *text);
1515

1616
extern bool (*Dvar_GetBool)(const char *dvarName);
1717

@@ -41,10 +41,14 @@ extern void (*SV_ExecuteClientCommand)(int client, const char *s, int clientOK,
4141

4242
extern void (*TeleportPlayer)(gentity_s *player, const float *origin, const float *angles);
4343

44+
void iPrintLn(int clientNum, const std::string &text);
45+
4446
gclient_s *GetGClient(int clientNum);
4547

4648
gentity_s *GetEntity(int entNum);
4749

50+
void SetClientDvar(int clientNum, const std::string &dvar, const std::string &value);
51+
4852
bool IsHost(int clientNum);
4953

5054
gentity_s *GetCurrentMapBrushModel();

Hayzen/src/Games/Alpha/MW2/MW2.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,14 @@ void AlphaMW2::Scr_NotifyHook(gentity_s *entity, uint16_t stringValue, uint32_t
7878
// "begin" can happen multiple times a game in round-based gamemodes and we don't want
7979
// to recreate the menu every round so we make sure it's not already initialized
8080
if (!strcmp(szNotify, "begin") && !s_Menu.IsInitialized())
81+
{
8182
s_Menu.Init(iClientNum, &s_RootOption);
83+
84+
// Disable the unlocalized error messages when printing something in the killfeed
85+
int iClientNum = s_Menu.GetClientNum();
86+
AlphaMW2GameFunctions::SetClientDvar(iClientNum, "loc_warnings", "0");
87+
AlphaMW2GameFunctions::SetClientDvar(iClientNum, "loc_warningsUI", "0");
88+
}
8289
}
8390

8491
void AlphaMW2::SV_ExecuteClientCommandHook(int client, const char *s, int clientOK, int fromOldServer)

0 commit comments

Comments
 (0)