在另一个函数 (c++) 中调用变量

Calling Variables in another function (c++)

本文关键字:调用 变量 c++ 另一个 函数      更新时间:2023-10-16

所以我必须为我正在制作的可自定义菜单的某些颜色创建一大堆变量。我想知道是否有办法将变量从第一个函数调用到其他函数中(有很多(这些变量必须在大约 10 个不同函数的 100 多个位置使用,我真的不想每次都重新定义所有 sh*t。(看起来很乱,如果我需要进行更改,会很痛苦。这是我的菜单颜色功能

Color MenuColor(int MenuAlpha_Main)
{
Color Theme;
Color Basic = Color(c_config::get().menu_color_r, c_config::get().menu_color_g, c_config::get().menu_color_b, MenuAlpha_Main);
Color Background = Color(c_config::get().menu_background_r, c_config::get().menu_background_g, c_config::get().menu_background_b, MenuAlpha_Main);
Color MiscSelectedTab_Colour = Color(c_config::get().MiscSelectedTab_r, c_config::get().MiscSelectedTab_g, c_config::get().MiscSelectedTab_b, MenuAlpha_Main);
Color MiscSelectedTab_Highlight_Colour = Color(c_config::get().MiscSelectedTab_Highlight_r, c_config::get().MiscSelectedTab_Highlight_g, c_config::get().MiscSelectedTab_Highlight_b, MenuAlpha_Main);
Color MiscUnSelectedTab_Colour = Color(c_config::get().MiscUnSelectedTab_r, c_config::get().MiscUnSelectedTab_g, c_config::get().MiscUnSelectedTab_b, MenuAlpha_Main);
Color MiscUnSelectedTab_Highlight_Colour = Color(c_config::get().MiscUnSelectedTab_Highlight_r, c_config::get().MiscUnSelectedTab_Highlight_g, c_config::get().MiscUnSelectedTab_Highlight_b, MenuAlpha_Main);
/////rainbow sync//////
static unsigned int last_time;
last_time = GetTickCount();
Color rainbow;
rainbow.FromHSV(fmod(last_time * 0.0002f, 1.f), 1.f, 0.5f);
//////////////////////
// Oh fuck, time for the customizable shit
Color MiscSelectedTab;
Color MiscSelectedTab_Highlight;
Color MiscUnSelectedTab;
Color MiscUnSelectedTab_Highlight;

if (c_config::get().menu_colour_style == 0) {
Theme = Basic; //Normal Style
}
else if (c_config::get().menu_colour_style == 1) {
Theme = rainbow; //Rainbow
}
else if (c_config::get().menu_colour_style == 2) {
//This shit is done below
}
return Theme;
if (c_config::get().menu_colour_style == 0 || c_config::get().menu_colour_style == 0 && !c_config::get().fullmenuhighlight) {
MiscSelectedTab = Background;
MiscSelectedTab_Highlight = Theme;
MiscUnSelectedTab = Background;
MiscUnSelectedTab_Highlight = Theme;
}
else if (c_config::get().menu_colour_style == 0 || c_config::get().menu_colour_style == 0 && c_config::get().fullmenuhighlight) {
MiscSelectedTab = Theme;
MiscSelectedTab_Highlight = Theme;
MiscUnSelectedTab = Theme;
MiscUnSelectedTab_Highlight = Theme;
}                             // MISC SUBTAB (Misc/Colours)
else if (c_config::get().menu_colour_style == 2) {
MiscSelectedTab = MiscSelectedTab_Colour;
MiscSelectedTab_Highlight = MiscSelectedTab_Highlight_Colour;
MiscUnSelectedTab = MiscUnSelectedTab_Colour;
MiscUnSelectedTab_Highlight = MiscUnSelectedTab_Highlight_Colour;
}
return MiscSelectedTab;
return MiscSelectedTab_Highlight;
return MiscUnSelectedTab;
return MiscUnSelectedTab_Highlight;
}

我对cpp还是很新鲜的,所以请不要评判。返回值需要在这样的函数中引用

void miscsubtab(int& current_players_esp_subtab, int tab_amount, Vector _pos, int MenuAlpha_Main)
{
int in_sizew_esp_player_subtabs = GroupBoxSize_Width - 8;
static std::string ESP_Player_SubTabs_Names[2] = { "Misc", "Colours" };
for (int i = 0; i < tab_amount; i++)
{
RECT text_size2 = g_pSurface->GetTextSizeRect(Globals::SmallText, ESP_Player_SubTabs_Names[i].c_str());
int tab_area[4] = {
_pos.x + 9 + (i * (in_sizew_esp_player_subtabs / tab_amount)), _pos.y + 52 + 5,
(in_sizew_esp_player_subtabs / tab_amount), 20
};
if (GetAsyncKeyState(VK_LBUTTON) && g_pSurface->MouseInRegion(tab_area[0], tab_area[1], tab_area[2],
tab_area[3]))
current_players_esp_subtab = i;
if (current_players_esp_subtab == i)
{
g_pSurface->FilledRect(tab_area[0], tab_area[1], tab_area[2], tab_area[3], MiscSelectedTab); //HERE
g_pSurface->FilledRect(tab_area[0], tab_area[1] + tab_area[3], tab_area[2], 3, MiscSelectedTab_Highlight); //HERE
g_pSurface->DrawT(tab_area[0] + (((in_sizew_esp_player_subtabs / tab_amount) / 2) - (text_size2.right / 2)),
tab_area[1] + (tab_area[3] / 2) - (text_size2.bottom / 2),
Color(143, 143, 143, MenuAlpha_Main), Globals::SmallText, false,
ESP_Player_SubTabs_Names[i].c_str());
}
else
{
g_pSurface->FilledRect(tab_area[0], tab_area[1], tab_area[2], tab_area[3], MiscUnSelectedTab); //HERE
g_pSurface->FilledRect(tab_area[0], tab_area[1] + tab_area[3], tab_area[2], 3, MiscUnSelectedTab_Highlight); //HERE
g_pSurface->DrawT(tab_area[0] + (((in_sizew_esp_player_subtabs / tab_amount) / 2) - (text_size2.right / 2)),
tab_area[1] + (tab_area[3] / 2) - (text_size2.bottom / 2),
Color(143, 143, 143, MenuAlpha_Main), Globals::SmallText, false,
ESP_Player_SubTabs_Names[i].c_str());
}
}
}

任何帮助将不胜感激。 谢谢-肯尼

您可以为其定义单独的主题结构,并使用主题变量中的所有颜色。

class Theme {
public:
enum Style {BASIC, RAINBOW, CUSTOM};
Style style;
Color MiscSelectedTab;
Color MiscSelectedTab_Highlight;
Color MiscUnSelectedTab;
Color MiscUnSelectedTab_Highlight;
}
Theme MenuColor(int MenuAlpha_Main)
{
Theme theme;
...
if (c_config::get().menu_colour_style == 0) {
theme.style = Theme::BASIC; //Normal Style
}
else if (c_config::get().menu_colour_style == 1) {
theme.style = Theme::RAINBOW; //Rainbow
}
else if (c_config::get().menu_colour_style == 2) {
theme.style = Theme::CUSTOM;
//This shit is done below
}
....
return theme;
}

Theme myTheme = MenuColor(5);
myTheme.MiscSelectedTab;
myTheme.MiscSelectedTab_Highlight;
myTheme.MiscUnSelectedTab;
myTheme.MiscUnSelectedTab_Highlight;