UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ui_main.cpp
Go to the documentation of this file.
1 
5 /*
6 Copyright (C) 2002-2020 UFO: Alien Invasion.
7 
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 
17 See the GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, 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
45 static cvar_t* ui_debug;
46 #endif
47 
52 int UI_DebugMode (void)
53 {
54 #ifdef DEBUG
55  return ui_debug->integer;
56 #else
57  return 0;
58 #endif
59 }
60 
61 #ifdef DEBUG
62 
66 static 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 
99  size = _Mem_PoolSize(ui_sysPool) + _Mem_PoolSize(ui_dynPool) + _Mem_PoolSize(ui_dynStringPool);
100  Com_Printf("\t-Full size: " UFO_SIZE_T " B\n", size);
101 }
102 #endif
103 
104 #define MAX_CONFUNC_SIZE 512
105 
110 void 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 
126 void* 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 
140 static 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();
149  CLMN_Shutdown();
150  R_FontShutdown();
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"))
178  UI_ParseAndLoadLuaScript(name, &text);
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 
191 void UI_Reinit (void)
192 {
193  UI_InitFonts();
194  UI_ReleaseInput();
196 }
197 
205 void 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  }
242  UI_ShutdownLua();
243  UI_FontShutdown();
244  UI_ResetInput();
245  UI_ResetTimers();
246 
247 #ifdef DEBUG
248  Cmd_RemoveCommand("debug_uimemory");
249 #endif
250  Cmd_RemoveCommand("ui_restart");
251 
252  Mem_Free(ui_global.adata);
253  OBJZERO(ui_global);
254 
255  /* release pools */
256  Mem_FreePool(ui_sysPool);
257  Mem_FreePool(ui_dynStringPool);
258  Mem_FreePool(ui_dynPool);
259  ui_sysPool = nullptr;
260  ui_dynStringPool = nullptr;
261  ui_dynPool = nullptr;
262 }
263 
268 void UI_FinishInit (void)
269 {
270  static bool initialized = false;
271  if (initialized)
272  return;
273  initialized = true;
274 
276 }
277 
278 void 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 */
287  OBJZERO(ui_global);
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();
309  UI_InitWindows();
310  UI_InitDraw();
311  UI_InitActions();
312 }
int numModels
Definition: ui_internal.h:84
int numActions
Definition: ui_internal.h:81
void Cmd_vExecuteString(const char *fmt, va_list ap)
Definition: cmd.cpp:952
void UI_InitActions(void)
uiNode_t * windowStack[UI_MAX_WINDOWSTACK]
Definition: ui_internal.h:77
void Cmd_AddCommand(const char *cmdName, xcommand_t function, const char *desc)
Add a new command to the script interface.
Definition: cmd.cpp:744
#define UI_MAX_ACTIONS
Definition: ui_internal.h:32
void UI_InitDraw(void)
Definition: ui_draw.cpp:427
uiNode_t * parent
Definition: ui_nodes.h:92
void Cmd_RemoveCommand(const char *cmdName)
Removes a command from script interface.
Definition: cmd.cpp:786
QGL_EXTERN GLint GLenum type
Definition: r_gl.h:94
bool UI_ParseAndLoadLuaScript(const char *name, const char **text)
Parses and loads a .ufo file holding a lua script.
Definition: ui_lua.cpp:386
uiNode_t * next
Definition: ui_nodes.h:91
void UI_Init(void)
Definition: ui_main.cpp:278
Basic lua initialization for the ui.
bool UI_ParseComponent(const char *type, const char *name, const char **text)
Parse a component.
Definition: ui_parse.cpp:1261
const char * name
Definition: ui_behaviour.h:41
void UI_Shutdown(void)
Reset and free the UI data hunk.
Definition: ui_main.cpp:205
This is a cvar definition. Cvars can be user modified and used in our menus e.g.
Definition: cvar.h:71
uiNode_t * components[UI_MAX_COMPONENTS]
Definition: ui_internal.h:71
int windowStackPos
Definition: ui_internal.h:78
char name[MAX_VAR]
Definition: ui_nodes.h:82
uiBehaviour_t * behaviour
Definition: ui_nodes.h:83
#define UI_MAX_MODELS
Definition: ui_node_model.h:44
void UI_FinishInit(void)
Finish initialization after everything was loaded.
Definition: ui_main.cpp:268
uiBehaviour_t * UI_GetNodeBehaviourByIndex(int index)
Definition: ui_nodes.cpp:582
void UI_InvalidateMouse(void)
Force to invalidate the mouse position and then to update hover nodes...
Definition: ui_input.cpp:560
void Com_Printf(const char *const fmt,...)
Definition: common.cpp:386
void UI_FontShutdown(void)
Definition: ui_font.cpp:184
void UI_InitLua(void)
Performs UI specific initialization. Call this after CL_InitLua.
Definition: ui_lua.cpp:55
int FS_BuildFileList(const char *fileList)
Build a filelist.
Definition: files.cpp:960
uint32_t _Mem_PoolSize(memPool_t *pool)
Definition: mem.cpp:414
void UI_InitNodes(void)
Definition: ui_nodes.cpp:658
int integer
Definition: cvar.h:81
#define CVAR_ARCHIVE
Definition: cvar.h:40
bool UI_ParseUIModel(const char *name, const char **text)
parses the models.ufo and all files where UI models (menu_model) are defined
Definition: ui_parse.cpp:1134
Internal data use by the UI package.
memPool_t * ui_dynPool
Definition: ui_main.cpp:41
static void UI_Restart_f(void)
Reloads the ui scripts and reinitializes the ui.
Definition: ui_main.cpp:140
#define UI_MAX_WINDOWSTACK
Definition: ui_internal.h:31
uiNode_t * UI_PushWindow(const char *name, const char *parentName, linkedList_t *params)
Push a window onto the window stack.
Definition: ui_windows.cpp:170
void UI_InitData(void)
Initialize console command about UI shared data.
Definition: ui_data.cpp:521
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
void UI_Reinit(void)
Definition: ui_main.cpp:191
GLsizei size
Definition: r_gl.h:152
#define OBJZERO(obj)
Definition: shared.h:178
bool Cmd_Exists(const char *cmdName)
Checks whether a function exists already.
Definition: cmd.cpp:887
bool UI_ParseWindow(const char *type, const char *name, const char **text)
Parse a window.
Definition: ui_parse.cpp:1336
#define Mem_CreatePool(name)
Definition: mem.h:32
void R_FontShutdown(void)
frees the SDL_ttf fonts
Definition: r_font.cpp:144
Atomic structure used to define most of the UI.
Definition: ui_nodes.h:80
int adataize
Definition: ui_internal.h:75
uiGlobal_t ui_global
Definition: ui_main.cpp:38
#define Mem_FreePool(pool)
Definition: mem.h:37
void CLMN_Shutdown(void)
Definition: cl_menu.cpp:142
uiNode_t * windows[UI_MAX_WINDOWS]
Definition: ui_internal.h:68
bool UI_ParseSprite(const char *name, const char **text)
Definition: ui_parse.cpp:1213
int numNodes
Definition: ui_internal.h:66
void UI_ResetTimers(void)
Definition: ui_timer.cpp:185
#define Mem_PoolAllocTypeN(type, n, pool)
Definition: mem.h:42
void CLMN_Init(void)
Initialize the menu data hunk, add cvars and commands.
Definition: cl_menu.cpp:129
memPool_t * ui_sysPool
Definition: ui_main.cpp:42
int numWindows
Definition: ui_internal.h:69
intptr_t extraDataSize
Definition: ui_behaviour.h:54
QGL_EXTERN GLint i
Definition: r_gl.h:113
void UI_ResetInput(void)
Definition: ui_input.cpp:538
cvar_t * ui_sounds
Definition: ui_sound.cpp:29
node behaviour, how a node work
Definition: ui_behaviour.h:39
#define UI_MAX_WINDOWS
Definition: ui_internal.h:29
void UI_ReleaseInput(void)
Release all captured input (keyboard or mouse)
Definition: ui_input.cpp:496
char * FS_NextScriptHeader(const char *files, const char **name, const char **text)
Definition: files.cpp:1194
uiBehaviour_t * UI_GetNodeBehaviour(const char *name)
Return a node behaviour by name.
Definition: ui_nodes.cpp:559
QGL_EXTERN GLuint GLsizei GLsizei GLint GLenum GLchar * name
Definition: r_gl.h:110
memPool_t * ui_dynStringPool
Definition: ui_main.cpp:40
#define CVAR_DEVELOPER
Definition: cvar.h:45
#define Mem_Free(ptr)
Definition: mem.h:35
void * UI_AllocHunkMemory(size_t size, int align, bool reset)
Definition: ui_main.cpp:126
#define UFO_SIZE_T
Definition: ufotypes.h:89
int UI_DebugMode(void)
Get the current debug mode (0 mean disabled)
Definition: ui_main.cpp:52
void UI_InitWindows(void)
Definition: ui_windows.cpp:747
byte * adata
Definition: ui_internal.h:74
byte * curadata
Definition: ui_internal.h:74
void UI_FinishWindowsInit(void)
Finish windows initialization.
Definition: ui_windows.cpp:665
Global data shared into all UI code.
Definition: ui_internal.h:49
uiNode_t * firstChild
Definition: ui_nodes.h:89
#define Q_streq(a, b)
Definition: shared.h:136
Model that have more than one part (top and down part of an aircraft)
Definition: ui_node_model.h:47
void UI_ShutdownLua(void)
Performs UI specific lua shutdown. Call this before CL_ShutdownLua.
Definition: ui_lua.cpp:61
uint8_t byte
Definition: ufotypes.h:34
int UI_GetNodeBehaviourCount(void)
Definition: ui_nodes.cpp:587
bool UI_ParseFont(const char *name, const char **text)
Definition: ui_font.cpp:68
Atomic element to store UI scripts The parser use this atom to translate script action into many tree...
Definition: ui_actions.h:144
void R_FontInit(void)
Definition: r_font.cpp:722
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_InitFonts(void)
after a video restart we have to reinitialize the fonts
Definition: ui_font.cpp:177