UFO: Alien Invasion
Loading...
Searching...
No Matches
ui_main.cpp
Go to the documentation of this file.
1
4
5/*
6Copyright (C) 2002-2025 UFO: Alien Invasion.
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; either version 2
11of the License, or (at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
17See the GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
23*/
24
25#include "ui_main.h"
26#include "ui_internal.h"
27#include "ui_draw.h"
28#include "ui_timer.h"
29#include "ui_font.h"
30#include "ui_parse.h"
31#include "ui_sound.h"
32#include "../cl_menu.h"
34#include "ui_lua.h"
35#include <vector>
36#include <string>
37
39
43
44#ifdef DEBUG
45static cvar_t* ui_debug;
46#endif
47
52int UI_DebugMode (void)
53{
54#ifdef DEBUG
55 return ui_debug->integer;
56#else
57 return 0;
58#endif
59}
60
61#ifdef DEBUG
66static void UI_Memory_f (void)
67{
68 int i;
69 const size_t nodeSize = sizeof(uiNode_t);
70 size_t size;
71 Com_Printf("Allocation:\n");
72 Com_Printf("\t-Window allocation: %i/%i\n", ui_global.numWindows, UI_MAX_WINDOWS);
73 Com_Printf("\t-Rendering window stack slot: %i\n", UI_MAX_WINDOWSTACK);
74 Com_Printf("\t-Action allocation: %i/%i\n", ui_global.numActions, UI_MAX_ACTIONS);
75 Com_Printf("\t-Model allocation: %i/%i\n", ui_global.numModels, UI_MAX_MODELS);
76 Com_Printf("\t-Node allocation: %i\n", ui_global.numNodes);
77
78 Com_Printf("Memory:\n");
79 Com_Printf("\t-Action structure size: " UFO_SIZE_T " B\n", sizeof(uiAction_t));
80 Com_Printf("\t-Model structure size: " UFO_SIZE_T " B\n", sizeof(uiModel_t));
81 Com_Printf("\t-Node structure size: " UFO_SIZE_T " B x%d\n", sizeof(uiNode_t), ui_global.numNodes);
82 for (i = 0; i < UI_GetNodeBehaviourCount(); i++) {
84 Com_Printf("\t -Behaviour %20s structure size: " UFO_SIZE_T " (+" UFO_SIZE_T " B) x%4u\n", b->name, sizeof(uiNode_t) + b->extraDataSize, b->extraDataSize, b->count);
85 }
86
87 size = 0;
88 for (i = 0; i < UI_GetNodeBehaviourCount(); i++) {
90 size += nodeSize * b->count + b->extraDataSize * b->count;
91 }
92 Com_Printf("Global memory:\n");
93 Com_Printf("\t-System pool: %ui B\n", _Mem_PoolSize(ui_sysPool));
94 Com_Printf("\t -AData allocation: " UFO_SIZE_T "/%i B\n", (ptrdiff_t)(ui_global.curadata - ui_global.adata), ui_global.adataize);
95 Com_Printf("\t -AData used by nodes: " UFO_SIZE_T " B\n", size);
96 Com_Printf("\t-Dynamic node/data pool: %ui B\n", _Mem_PoolSize(ui_dynPool));
97 Com_Printf("\t-Dynamic strings pool: %ui B\n", _Mem_PoolSize(ui_dynStringPool));
98
100 Com_Printf("\t-Full size: " UFO_SIZE_T " B\n", size);
101}
102#endif
103
104#define MAX_CONFUNC_SIZE 512
110void UI_ExecuteConfunc (const char* fmt, ...)
111{
112 va_list ap;
113 va_start(ap, fmt);
114 Cmd_vExecuteString(fmt, ap);
115 va_end(ap);
116}
117
126void* UI_AllocHunkMemory (size_t size, int align, bool reset)
127{
128 byte* memory = (byte*) ALIGN_PTR(ui_global.curadata, align);
129 if (memory + size > ui_global.adata + ui_global.adataize)
130 return nullptr;
131 if (reset)
132 memset(memory, 0, size);
133 ui_global.curadata = memory + size;
134 return memory;
135}
136
140static void UI_Restart_f (void)
141{
142 typedef std::vector<std::string> Names;
143 Names names;
144 for (int i = 0; i < ui_global.windowStackPos; i++) {
145 names.push_back(std::string(ui_global.windowStack[i]->name));
146 }
147
148 UI_Shutdown();
151 UI_Init();
152 R_FontInit();
153 Com_Printf("%i ui script files\n", FS_BuildFileList("ufos/ui/*.ufo"));
154 FS_NextScriptHeader(nullptr, nullptr, nullptr);
155 const char* type, *name, *text;
156 text = nullptr;
157 while ((type = FS_NextScriptHeader("ufos/*.ufo", &name, &text)) != nullptr) {
158 if (Q_streq(type, "font"))
159 UI_ParseFont(name, &text);
160 else if (Q_streq(type, "menu_model"))
161 UI_ParseUIModel(name, &text);
162 else if (Q_streq(type, "sprite"))
163 UI_ParseSprite(name, &text);
164 }
165 UI_Reinit();
166 FS_NextScriptHeader(nullptr, nullptr, nullptr);
167 text = nullptr;
168 while ((type = FS_NextScriptHeader("ufos/ui/*.ufo", &name, &text)) != nullptr) {
169 if (Q_streq(type, "window"))
170 UI_ParseWindow(type, name, &text);
171 else if (Q_streq(type, "component"))
172 UI_ParseComponent(type, name, &text);
173 else if (Q_streq(type, "menu_model"))
174 UI_ParseUIModel(name, &text);
175 else if (Q_streq(type, "sprite"))
176 UI_ParseSprite(name, &text);
177 else if (Q_streq(type, "lua"))
179 }
180
181 CLMN_Init();
182
183 for (Names::iterator i = names.begin(); i != names.end(); ++i) {
184 UI_PushWindow(i->c_str());
185 }
186}
187
191void UI_Reinit (void)
192{
193 UI_InitFonts();
196}
197
205void UI_Shutdown (void)
206{
207 /* MN is not yet initialized */
208 if (ui_global.adata == nullptr)
209 return;
210
211 const uiBehaviour_t* confunc = UI_GetNodeBehaviour("confunc");
212
213 /* remove all confunc commands */
214 uiNode_t** nodes[] = {ui_global.windows, ui_global.components};
215 for (int nodeType = 0; nodeType < 2; ++nodeType) {
216 for (int i = 0; i < ui_global.numWindows; i++) {
217 uiNode_t* node = nodes[nodeType][i];
218 while (node) {
219 /* remove the command */
220 if (node->behaviour == confunc) {
221 /* many nodes to one command is allowed */
222 if (Cmd_Exists(node->name)) {
223 Cmd_RemoveCommand(node->name);
224 }
225 }
226
227 /* recursive next */
228 if (node->firstChild != nullptr) {
229 node = node->firstChild;
230 continue;
231 }
232 while (node) {
233 if (node->next != nullptr) {
234 node = node->next;
235 break;
236 }
237 node = node->parent;
238 }
239 }
240 }
241 }
246
247#ifdef DEBUG
248 Cmd_RemoveCommand("debug_uimemory");
249#endif
250 Cmd_RemoveCommand("ui_restart");
251
252 Mem_Free(ui_global.adata);
254
255 /* release pools */
259 ui_sysPool = nullptr;
260 ui_dynStringPool = nullptr;
261 ui_dynPool = nullptr;
262}
263
268void UI_FinishInit (void)
269{
270 static bool initialized = false;
271 if (initialized)
272 return;
273 initialized = true;
274
276}
277
278void UI_Init (void)
279{
280 cvar_t* ui_hunkSize = Cvar_Get("ui_hunksize", "3", 0, "UI memory hunk size in megabytes");
281
282#ifdef DEBUG
283 ui_debug = Cvar_Get("debug_ui", "0", CVAR_DEVELOPER, "Prints node names for debugging purposes - valid values are 1 and 2");
284#endif
285
286 /* reset global UI structures */
288
289 ui_sounds = Cvar_Get("ui_sounds", "1", CVAR_ARCHIVE, "Activates UI sounds");
290
291#ifdef DEBUG
292 Cmd_AddCommand("debug_uimemory", UI_Memory_f, "Display info about UI memory allocation");
293#endif
294
295 Cmd_AddCommand("ui_restart", UI_Restart_f, "Reload the whole ui");
296
297 ui_sysPool = Mem_CreatePool("Client: UI");
298 ui_dynStringPool = Mem_CreatePool("Client: Dynamic string for UI");
299 ui_dynPool = Mem_CreatePool("Client: Dynamic memory for UI");
300
301 Com_Printf("Allocate %i megabytes for the ui hunk\n", ui_hunkSize->integer);
302 ui_global.adataize = ui_hunkSize->integer * 1024 * 1024;
303 ui_global.adata = Mem_PoolAllocTypeN(byte, ui_global.adataize, ui_sysPool);
304 ui_global.curadata = ui_global.adata;
305
306 UI_InitLua();
307 UI_InitData();
308 UI_InitNodes();
310 UI_InitDraw();
312}
void CLMN_Init(void)
Initialize the menu data hunk, add cvars and commands.
Definition cl_menu.cpp:129
void CLMN_Shutdown(void)
Definition cl_menu.cpp:142
Header for client menu implementation.
void R_FontInit(void)
Definition r_font.cpp:716
void R_FontShutdown(void)
frees the SDL_ttf fonts
Definition r_font.cpp:144
bool Cmd_Exists(const char *cmdName)
Checks whether a function exists already.
Definition cmd.cpp:887
void Cmd_RemoveCommand(const char *cmdName)
Removes a command from script interface.
Definition cmd.cpp:786
void Cmd_AddCommand(const char *cmdName, xcommand_t function, const char *desc)
Add a new command to the script interface.
Definition cmd.cpp:744
void Cmd_vExecuteString(const char *fmt, va_list ap)
Definition cmd.cpp:952
void Com_Printf(const char *const fmt,...)
Definition common.cpp:428
cvar_t * Cvar_Get(const char *var_name, const char *var_value, int flags, const char *desc)
Init or return a cvar.
Definition cvar.cpp:342
#define CVAR_DEVELOPER
Definition cvar.h:45
#define CVAR_ARCHIVE
Definition cvar.h:40
char * FS_NextScriptHeader(const char *files, const char **name, const char **text)
Definition files.cpp:1196
int FS_BuildFileList(const char *fileList)
Build a filelist.
Definition files.cpp:962
voidpf void uLong size
Definition ioapi.h:42
uint32_t _Mem_PoolSize(memPool_t *pool)
Definition mem.cpp:418
#define Mem_FreePool(pool)
Definition mem.h:37
#define Mem_PoolAllocTypeN(type, n, pool)
Definition mem.h:42
#define Mem_Free(ptr)
Definition mem.h:35
#define Mem_CreatePool(name)
Definition mem.h:32
QGL_EXTERN GLint i
Definition r_gl.h:113
QGL_EXTERN GLint GLenum type
Definition r_gl.h:94
QGL_EXTERN GLuint GLsizei GLsizei GLint GLenum GLchar * name
Definition r_gl.h:110
#define Q_streq(a, b)
Definition shared.h:136
#define OBJZERO(obj)
Definition shared.h:178
This is a cvar definition. Cvars can be user modified and used in our menus e.g.
Definition cvar.h:71
int integer
Definition cvar.h:81
Atomic element to store UI scripts The parser use this atom to translate script action into many tree...
Definition ui_actions.h:144
node behaviour, how a node work
const char * name
intptr_t extraDataSize
Global data shared into all UI code.
Definition ui_internal.h:49
Model that have more than one part (top and down part of an aircraft).
Atomic structure used to define most of the UI.
Definition ui_nodes.h:80
uiNode_t * firstChild
Definition ui_nodes.h:89
uiNode_t * next
Definition ui_nodes.h:91
uiBehaviour_t * behaviour
Definition ui_nodes.h:83
uiNode_t * parent
Definition ui_nodes.h:92
char name[MAX_VAR]
Definition ui_nodes.h:82
#define UFO_SIZE_T
Definition ufotypes.h:89
void UI_InitActions(void)
void UI_InitData(void)
Initialize console command about UI shared data.
Definition ui_data.cpp:521
void UI_InitDraw(void)
Definition ui_draw.cpp:427
void UI_FontShutdown(void)
Definition ui_font.cpp:184
bool UI_ParseFont(const char *name, const char **text)
Definition ui_font.cpp:68
void UI_InitFonts(void)
after a video restart we have to reinitialize the fonts
Definition ui_font.cpp:177
void UI_ReleaseInput(void)
Release all captured input (keyboard or mouse).
Definition ui_input.cpp:496
void UI_ResetInput(void)
Definition ui_input.cpp:538
void UI_InvalidateMouse(void)
Force to invalidate the mouse position and then to update hover nodes...
Definition ui_input.cpp:560
Internal data use by the UI package.
memPool_t * ui_sysPool
Definition ui_main.cpp:42
memPool_t * ui_dynPool
Definition ui_main.cpp:41
uiGlobal_t ui_global
Definition ui_main.cpp:38
#define UI_MAX_WINDOWSTACK
Definition ui_internal.h:31
#define UI_MAX_WINDOWS
Definition ui_internal.h:29
void UI_FinishWindowsInit(void)
Finish windows initialization.
#define UI_MAX_ACTIONS
Definition ui_internal.h:32
bool UI_ParseAndLoadLuaScript(const char *name, const char **text)
Parses and loads a .ufo file holding a lua script.
Definition ui_lua.cpp:386
void UI_ShutdownLua(void)
Performs UI specific lua shutdown. Call this before CL_ShutdownLua.
Definition ui_lua.cpp:61
void UI_InitLua(void)
Performs UI specific initialization. Call this after CL_InitLua.
Definition ui_lua.cpp:55
Basic lua initialization for the ui.
void * UI_AllocHunkMemory(size_t size, int align, bool reset)
Definition ui_main.cpp:126
void UI_ExecuteConfunc(const char *fmt,...)
Executes confunc - just to identify those confuncs in the code - in this frame.
Definition ui_main.cpp:110
void UI_Shutdown(void)
Reset and free the UI data hunk.
Definition ui_main.cpp:205
int UI_DebugMode(void)
Get the current debug mode (0 mean disabled).
Definition ui_main.cpp:52
void UI_Init(void)
Definition ui_main.cpp:278
static void UI_Restart_f(void)
Reloads the ui scripts and reinitializes the ui.
Definition ui_main.cpp:140
void UI_FinishInit(void)
Finish initialization after everything was loaded.
Definition ui_main.cpp:268
void UI_Reinit(void)
Definition ui_main.cpp:191
#define UI_MAX_MODELS
memPool_t * ui_dynStringPool
Definition ui_main.cpp:40
int UI_GetNodeBehaviourCount(void)
Definition ui_nodes.cpp:587
void UI_InitNodes(void)
Definition ui_nodes.cpp:658
uiBehaviour_t * UI_GetNodeBehaviour(const char *name)
Return a node behaviour by name.
Definition ui_nodes.cpp:559
uiBehaviour_t * UI_GetNodeBehaviourByIndex(int index)
Definition ui_nodes.cpp:582
bool UI_ParseWindow(const char *type, const char *name, const char **text)
Parse a window.
bool UI_ParseSprite(const char *name, const char **text)
bool UI_ParseUIModel(const char *name, const char **text)
parses the models.ufo and all files where UI models (menu_model) are defined
bool UI_ParseComponent(const char *type, const char *name, const char **text)
Parse a component.
cvar_t * ui_sounds
Definition ui_sound.cpp:29
void UI_ResetTimers(void)
Definition ui_timer.cpp:185
void UI_InitWindows(void)
uiNode_t * UI_PushWindow(const char *name, const char *parentName, linkedList_t *params)
Push a window onto the window stack.