1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code ("Doom 3 Source Code").
8 
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code.  If not, see <http://www.gnu.org/licenses/>.
21 
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code.  If not, please request a copy in writing from id Software at the address below.
23 
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25 
26 ===========================================================================
27 */
28 
29 #ifndef __COMMON_H__
30 #define __COMMON_H__
31 
32 #include "framework/CVarSystem.h"
33 
34 /*
35 ==============================================================
36 
37   Common
38 
39 ==============================================================
40 */
41 
42 typedef enum {
43 	EDITOR_NONE					= 0,
44 	EDITOR_RADIANT				= BIT(1),
45 	EDITOR_GUI					= BIT(2),
46 	EDITOR_DEBUGGER				= BIT(3),
47 	EDITOR_SCRIPT				= BIT(4),
48 	EDITOR_LIGHT				= BIT(5),
49 	EDITOR_SOUND				= BIT(6),
50 	EDITOR_DECL					= BIT(7),
51 	EDITOR_AF					= BIT(8),
52 	EDITOR_PARTICLE				= BIT(9),
53 	EDITOR_PDA					= BIT(10),
54 	EDITOR_AAS					= BIT(11),
55 	EDITOR_MATERIAL				= BIT(12)
56 } toolFlag_t;
57 
58 #define STRTABLE_ID				"#str_"
59 #define STRTABLE_ID_LENGTH		5
60 
61 extern idCVar		com_version;
62 extern idCVar		com_skipRenderer;
63 extern idCVar		com_asyncInput;
64 extern idCVar		com_asyncSound;
65 extern idCVar		com_machineSpec;
66 extern idCVar		com_purgeAll;
67 extern idCVar		com_developer;
68 extern idCVar		com_allowConsole;
69 extern idCVar		com_speeds;
70 extern idCVar		com_showFPS;
71 extern idCVar		com_showMemoryUsage;
72 extern idCVar		com_showAsyncStats;
73 extern idCVar		com_showSoundDecoders;
74 extern idCVar		com_makingBuild;
75 extern idCVar		com_updateLoadSize;
76 
77 extern int			time_gameFrame;			// game logic time
78 extern int			time_gameDraw;			// game present time
79 extern int			time_frontend;			// renderer frontend time
80 extern int			time_backend;			// renderer backend time
81 
82 extern int			com_frameTime;			// time for the current frame in milliseconds
83 extern volatile int	com_ticNumber;			// 60 hz tics, incremented by async function
84 extern int			com_editors;			// current active editor(s)
85 extern bool			com_editorActive;		// true if an editor has focus
86 
87 #ifdef _WIN32
88 const char			DMAP_MSGID[] = "DMAPOutput";
89 const char			DMAP_DONE[] = "DMAPDone";
90 extern HWND			com_hwndMsg;
91 extern bool			com_outputMsg;
92 #endif
93 
94 struct MemInfo_t {
95 	idStr			filebase;
96 
97 	int				total;
98 	int				assetTotals;
99 
100 	// memory manager totals
101 	int				memoryManagerTotal;
102 
103 	// subsystem totals
104 	int				gameSubsystemTotal;
105 	int				renderSubsystemTotal;
106 
107 	// asset totals
108 	int				imageAssetsTotal;
109 	int				modelAssetsTotal;
110 	int				soundAssetsTotal;
111 };
112 
113 class idLangDict;
114 
115 class idCommon {
116 public:
~idCommon(void)117 	virtual						~idCommon( void ) {}
118 
119 								// Initialize everything.
120 								// if the OS allows, pass argc/argv directly (without executable name)
121 								// otherwise pass the command line in a single string (without executable name)
122 	virtual void				Init( int argc, char **argv ) = 0;
123 
124 								// Shuts down everything.
125 	virtual void				Shutdown( void ) = 0;
126 
127 								// Shuts down everything.
128 	virtual void				Quit( void ) = 0;
129 
130 								// Returns true if common initialization is complete.
131 	virtual bool				IsInitialized( void ) const = 0;
132 
133 								// Called repeatedly as the foreground thread for rendering and game logic.
134 	virtual void				Frame( void ) = 0;
135 
136 								// Called repeatedly by blocking function calls with GUI interactivity.
137 	virtual void				GUIFrame( bool execCmd, bool network ) = 0;
138 
139 								// Called 60 times a second from a background thread for sound mixing,
140 								// and input generation. Not called until idCommon::Init() has completed.
141 	virtual void				Async( void ) = 0;
142 
143 								// Checks for and removes command line "+set var arg" constructs.
144 								// If match is NULL, all set commands will be executed, otherwise
145 								// only a set with the exact name.  Only used during startup.
146 								// set once to clear the cvar from +set for early init code
147 	virtual void				StartupVariable( const char *match, bool once ) = 0;
148 
149 								// Initializes a tool with the given dictionary.
150 	virtual void				InitTool( const toolFlag_t tool, const idDict *dict ) = 0;
151 
152 								// Activates or deactivates a tool.
153 	virtual void				ActivateTool( bool active ) = 0;
154 
155 								// Writes the user's configuration to a file
156 	virtual void				WriteConfigToFile( const char *filename ) = 0;
157 
158 								// Writes cvars with the given flags to a file.
159 	virtual void				WriteFlaggedCVarsToFile( const char *filename, int flags, const char *setCmd ) = 0;
160 
161 								// Begins redirection of console output to the given buffer.
162 	virtual void				BeginRedirect( char *buffer, int buffersize, void (*flush)( const char * ) ) = 0;
163 
164 								// Stops redirection of console output.
165 	virtual void				EndRedirect( void ) = 0;
166 
167 								// Update the screen with every message printed.
168 	virtual void				SetRefreshOnPrint( bool set ) = 0;
169 
170 								// Prints message to the console, which may cause a screen update if com_refreshOnPrint is set.
171 	virtual void				Printf( const char *fmt, ... )id_attribute((format(printf,2,3))) = 0;
172 
173 								// Same as Printf, with a more usable API - Printf pipes to this.
174 	virtual void				VPrintf( const char *fmt, va_list arg ) = 0;
175 
176 								// Prints message that only shows up if the "developer" cvar is set,
177 								// and NEVER forces a screen update, which could cause reentrancy problems.
178 	virtual void				DPrintf( const char *fmt, ... ) id_attribute((format(printf,2,3))) = 0;
179 
180 								// Prints WARNING %s message and adds the warning message to a queue for printing later on.
181 	virtual void				Warning( const char *fmt, ... ) id_attribute((format(printf,2,3))) = 0;
182 
183 								// Prints WARNING %s message in yellow that only shows up if the "developer" cvar is set.
184 	virtual void				DWarning( const char *fmt, ...) id_attribute((format(printf,2,3))) = 0;
185 
186 								// Prints all queued warnings.
187 	virtual void				PrintWarnings( void ) = 0;
188 
189 								// Removes all queued warnings.
190 	virtual void				ClearWarnings( const char *reason ) = 0;
191 
192 								// Issues a C++ throw. Normal errors just abort to the game loop,
193 								// which is appropriate for media or dynamic logic errors.
194 	virtual void				Error( const char *fmt, ... ) id_attribute((format(printf,2,3))) = 0;
195 
196 								// Fatal errors quit all the way to a system dialog box, which is appropriate for
197 								// static internal errors or cases where the system may be corrupted.
198 	virtual void				FatalError( const char *fmt, ... ) id_attribute((format(printf,2,3))) = 0;
199 
200 								// Returns a pointer to the dictionary with language specific strings.
201 	virtual const idLangDict *	GetLanguageDict( void ) = 0;
202 
203 								// Returns key bound to the command
204 	virtual const char *		KeysFromBinding( const char *bind ) = 0;
205 
206 								// Returns the binding bound to the key
207 	virtual const char *		BindingFromKey( const char *key ) = 0;
208 
209 								// Directly sample a button.
210 	virtual int					ButtonState( int key ) = 0;
211 
212 								// Directly sample a keystate.
213 	virtual int					KeyState( int key ) = 0;
214 
215 	/* Some Mods (like Ruiner and DarkMod when it still was a mod) used "SourceHook"
216 	 * to override Doom3 Methods to call their own code before the original method
217 	 * was executed.. this is super ugly and probably not super portable either.
218 	 *
219 	 * So let's offer something that's slightly less ugly: A function pointer based
220 	 * interface to provide similar (but known!) hacks.
221 	 * For example, Ruiner used SourceHook to intercept idCmdSystem::BufferCommandText()
222 	 * and recreate some cooked rendering data in case reloadImages or vid_restart was executed.
223 	 * Now, instead of doing ugly hacks with SourceHook, Ruiner can just call
224 	 *   common->SetCallback( idCommon::CB_ReloadImages,
225 	 *                        (idCommon::FunctionPointer)functionToCall,
226 	 *                        (void*)argForFunctionToCall );
227 	 *
228 	 * (the Mod needs to check if SetCallback() returned true; if it didn't the used version
229 	 *  of dhewm3 doesn't support the given CallBackType and the Mod must either error out
230 	 *  or handle the case that the callback doesn't work)
231 	 *
232 	 * Of course this means that for every new SourceHook hack a Mod (that's ported to dhewm3)
233 	 * uses, a corresponding entry must be added to enum CallbackType and it must be handled,
234 	 * which implies that the Mod will only properly work with the latest dhewm3 git code
235 	 * or the next release..
236 	 * I guess most mods don't need this hack though, so I think it's feasible.
237 	 *
238 	 * Note that this allows adding new types of callbacks without breaking the API and ABI
239 	 * between dhewm3 and the Game DLLs; the alternative would be something like
240 	 * idCommon::RegisterReloadImagesCallback(), and maybe other similar methods later, which
241 	 * would break the ABI and API each time and all Mods would have to be adjusted, even if
242 	 * they don't even need that functionality (because they never needed SourceHook or similar).
243 	 *
244 	 * Similar to SetCallback() I've also added GetAdditionalFunction() to get a function pointer
245 	 * from dhewm3 that Mods can call (and that's not exported via the normal interface classes).
246 	 * Right now it's only used for a Doom3 Demo specific hack only relevant for base.dll (not for Mods)
247 	 */
248 
249 	typedef void* (*FunctionPointer)(void*); // needs to be cast to/from real type!
250 	enum CallbackType {
251 		// called on reloadImages and vid_restart commands (before anything "real" happens)
252 		// expecting callback to be like void cb(void* userarg, const idCmdArgs& cmdArgs)
253 		// where cmdArgs contains the command+arguments that was called
254 		CB_ReloadImages = 1,
255 	};
256 
257 	// returns true if setting the callback was successful, else false
258 	// When a game DLL is unloaded the callbacks are automatically removed from the Engine
259 	// so you usually don't have to worry about that; but you can call this with cb = NULL
260 	// and userArg = NULL to remove a callback manually (e.g. if userArg refers to an object you deleted)
261 	virtual bool				SetCallback(CallbackType cbt, FunctionPointer cb, void* userArg) = 0;
262 
263 	enum FunctionType {
264 		// the function's signature is bool fn(void) - no arguments.
265 		// it returns true if we're currently running the doom3 demo
266 		// not relevant for mods, only for game/ aka base.dll/base.so/...
267 		FT_IsDemo = 1,
268 	};
269 
270 	// returns true if that function is available in this version of dhewm3
271 	// *out_fnptr will be the function (you'll have to cast it probably)
272 	// *out_userArg will be an argument you have to pass to the function, if appropriate (else NULL)
273 	virtual bool				GetAdditionalFunction(FunctionType ft, FunctionPointer* out_fnptr, void** out_userArg) = 0;
274 };
275 
276 extern idCommon *		common;
277 
278 #endif /* !__COMMON_H__ */
279