Nuclide
Software Development Kit for id Technology
defs.h
1/*
2 * Copyright (c) 2016-2024 Vera Visions LLC.
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
13 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
14 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15*/
16
17
22var bool autocvar_g_logTimestamps = false;
23
24typedef entity id;
25
26typedef enum
27{
28 LOGLEVEL_NONE,
29 LOGLEVEL_ERRORS,
30 LOGLEVEL_WARNINGS,
31 LOGLEVEL_DEBUG,
32} logLevel_t;
33
34#define LOGLEVEL_DEFAULT LOGLEVEL_WARNINGS
35var logLevel_t autocvar_g_logLevel = LOGLEVEL_DEFAULT;
36
37#define printf(...) print(sprintf(__VA_ARGS__))
38
39#ifdef DOXYGEN
41#define enumflags enum
42#endif
43
44void
45_NSLog(string msg)
46{
47 if (autocvar_g_logTimestamps)
48 print(sprintf("^9%f ^7%s\n", time, msg));
49 else
50 print(sprintf("^7%s\n", msg));
51}
52
53void
54_NSError(string functionName, string msg)
55{
56 if (autocvar_g_logTimestamps)
57 print(sprintf("^9%f ^1%s^7: %s\n", time, functionName, msg));
58 else
59 print(sprintf("^1%s^7: %s\n", functionName, msg));
60}
61
62void
63_NSWarning(string functionName, string msg)
64{
65 if (autocvar_g_logTimestamps)
66 print(sprintf("^9%f ^3%s^7: %s\n", time, functionName, msg));
67 else
68 print(sprintf("^3%s^7: %s\n", functionName, msg));
69}
70
71void
72_NSAssert(bool condition, string function, string descr)
73{
74 if (!condition) {
75 print(strcat("^1Assertion failed in ", function, ", reason: ", descr, "\n"));
76#ifndef MENU
77 breakpoint();
78#endif
79 }
80}
81
86#define NSLog(...) if (autocvar_g_logLevel >= LOGLEVEL_DEBUG) _NSLog(sprintf(__VA_ARGS__))
87
92#define NSError(...) if (autocvar_g_logLevel >= LOGLEVEL_ERRORS) _NSError(__FUNC__, sprintf(__VA_ARGS__))
93
98#define NSWarning(...) if (autocvar_g_logLevel >= LOGLEVEL_WARNINGS) _NSWarning(__FUNC__, sprintf(__VA_ARGS__))
99
106#define NSAssert(condition, ...) if (autocvar_g_logLevel >= LOGLEVEL_ERRORS) _NSAssert(condition, __FUNC__, sprintf(__VA_ARGS__))
107
108typedef enumflags
109{
110 SEARCH_INSENSITIVE,
111 SEARCH_FULLPACKAGE,
112 SEARCH_ALLOWDUPES,
113 SEARCH_FORCESEARCH,
114 SEARCH_MULTISEARCH,
115 SEARCH_NAMESORT
116} searchFlags_t;
117
118const vector g_vec_null = [0.0f, 0.0f, 0.0f];
119
120/* the console needs some attention too. */
121var float g_initTime;
122
123void
124InitPrint(string functionName)
125{
126 int chars = 51i;
127 int charsLeft;
128 int charExtra;
129 string sideLeft = "";
130 string sideRight = "";
131
132 if (functionName == __NULL__) {
133 NSLog("---------------------------------------------------");
134 return;
135 }
136
137 /* word and padding */
138 chars = chars - (int)strlen(functionName) - 2i;
139 charsLeft = chars / 2i;
140 charExtra = chars % 2i;
141
142 for (int i = 0i; i < charsLeft; i++)
143 sideLeft = strcat(sideLeft,"-");
144
145 for (int i = 0i; i < (charsLeft + charExtra); i++) {
146 sideRight = strcat(sideRight,"-");
147 }
148
149 NSLog( "%s %s %s", sideLeft, functionName, sideRight);
150}
151
152var string g_lastInitFunc;
153void
154_InitStart(string functionName)
155{
156 if (g_initTime != 0)
157 error(sprintf("Called InitStart() without InitEnd()ing %s!", g_lastInitFunc));
158
159 InitPrint(functionName);
160 g_lastInitFunc = functionName;
161 g_initTime = gettime(1);
162}
163
164#define InitStart() _InitStart(__FUNC__)
165
166void
167_InitEnd(void)
168{
169 float endTime = gettime(1);
170 NSLog("loaded in %.1f seconds", (endTime - g_initTime));
171 NSLog("---------------------------------------------------");
172 g_initTime = 0;
173}
174
175#define InitEnd() _InitEnd()
176
178#define entity_def(x, ...) const string x[] = { __VA_ARGS__ }
179
181#define thread(x) if (fork()) { x; abort(); }
182
183#define STRING_SET(x) ((x != __NULL__) && (x != ""))
184
185bool
186fileExists(string filePath)
187{
188 if (filePath != "") /* not empty */
189 if not(whichpack(filePath)) /* not present on disk */
190 return false;
191
192 return true;
193}
194
195/* other parts of shared to include */
196
197#include "math.h"
198#include "memory.h"
199#include "platform.h"
200#include "colors.h"
201#include "materials.h"
202
203 // end of common
typedef enumflags
Defines the valid alignment flags for text fields.
Definition: font.h:37