Quake2 source code analysis (1)
I hope to understand this excellent engine's way of work by quake2 source code analysis. Since I am more familiar with the code organization of Delphi, I downloaded the quake2 Delphi code from http://www.sourceforge.net/quake2delphi/. Note: For the simplicity of the description, I don't describe the details.
Let us enter the topic: starting point: sys_win: Winmain This is our very familiar Windows entry
QCOMMON_INIT / / QCOMMON is a general module, and the general module is initialized. This step is handled quite, the next step will explain the While (TRUE) qcommon_frame (time) // Draw each Frame
Simply say that WinMain is subject to the main two steps
Let us analyze the initialization operation: QCOMMON_INIT content
// An initialization z_chain z_chain.prev: = @z_chain; z_chain.next: = z_chain.prev; // Second initialization parameter com_initargv (argc, argv); // three initialization switching algorithm and command line buffer SWAP_INIT; CBUF_INIT; / / Four Command Interpreter Initialization CMD_INIT; // Five Variable Operations Initialization CVAR_INIT; / / Six Keyboard Mapping Table Initialization KEY_INIT;
// Seven initialization command line, then initialize the file system cbuf_addearlycommands (false); CBUF_EXECUTE;
FS_INITFILESYSTEM; // Eight Operation script CBUF_ADDTEXT ('exec default.cfg' # 10); CBUF_ADDText ('exec control.cfg' # 10);
CBUF_ADDEARLYCOMMANDS (TRUE); / / Add command line and empty the parameters CBUF_EXECUTE; // Nine initialization variable cmd_addcommand ('z_stats', z_stats_f); cmd_addcommand ('error', com_error_f);
Host_Speeds: = CVAR_GET ('host_speeds', '0', 0); log_stats: = cvar_get ('log_stats', '0', 0); developer: = cvar_get ('developer', '0', 0); TIMESCALE: = CVAR_GET ('Timescale', '1', 0); FixedTime: = CVAR_GET ('fixedtime', '0', 0); logfile_active: = cvar_get ('logfile', '0', 0); showtrace: = cvar_Get ('ShowTrace', '0', 0); {$ ifdef Dedicated_only} Dedicated: = CVAR_GET ('Dedicated', '1', CVAR_NOSet); {$ else} Dedicated: = CVAR_GET ('Dedicated', '0', CVAR_NOSet; {$ ENDIF} // Ten Setup S: = Va ('% 4.2F% S% S% S', [Version, Cpustring, __date__, buildstring]); cvar_get ('Version', s, cvar_serverinfo or Cvar_noset; if (Dedicated.Value <> 0) THEN CMD_ADDCOMMAND ('quit', com_quit); // 11: Initialization System (Console) SYS_INIT; / / Twelve: Initialization Network and Port Net_init; NetChan_init; // Thirteen initialization server sv_init; // fourteen initialization client cl_init;
// 15: If the user does not enter command, and no "dedication" flag is displayed, the demo // add commands from command line if not cbuf_addlateCommands the beginning ,/ if the user Didn't Give Any Commands , run default action if (dedicated.value = 0) then Cbuf_AddText ( 'd1' # 10) else Cbuf_AddText ( 'dedicated_start' # 10); Cbuf_Execute; end else begin // the user asked for something explicit // so drop the loading PLAQUE SCR_ENDLOADINGPLAQUE; END; sixteen end com_printf ('======= qake2 initialized ======' # 10 # 10, []);
Next, further analysis of QCOMMON_INIT 1: Initialization Z_Chain This will lead to the understanding of Z_Chain, z_chain is a QUAKE original data linked list for managing allocated memory space to ensure that the interim application is correct. Release. Each memory allocation request calls CommON's z_malloc to assign, z_malloc calls z_tagmalloc to assign memory space with the flag. Let's take a look at Z_Chain Type ZHEAD_T definition ZHEAD_S = Record Prev, Next: ZHEAD_P; // Typical Link Picture Magic: Smallint; // A flag, keep const z_magic = 1d1d; tag: smallint; // Batch release, it seems to be similar to the concept of GC, and the size of the allocated memory block (including ZHEAD_S size) end; zhead_t = zhead_s; second initialization parameter stores command line data into COM_ARGC_ and COM_ARGV_ []
Three initialization switching algorithms and command line buffers In order to adapt to different CPU structures, it is a small Indian format in a small Indian format or small Indian format in the program. Assign memory to the command line buffer
The initialization initialization command interpreter of the four command interpreter. Quake has a very unique mode, which is to use the command line to cooperate with the global variable drive mode. This reminds me of an old DOS operating system. The advantage of this way is that the coupling degree between the modules can be low. It is also easy to configure. This step adds the following default commands cmdlist: list all available commands EXEC: Run the script echo: Display text alias display alias Wait Waiting
The initialization of the five variable operation adds the following command set setting variable cvarlist listing variables for operational variables.
Six keyboard mapping table Initialization Defines the default keyboard mapping table to add a command BIND for operating the keyboard mapping table to make a key bind to a character unbind unbindall Unbindall Cancel all keys Bindlist display binding List
7 Initialize the command line, then initialize the file system to incorporate the command line parameters, set the variable required to initialize the file system, then initialize the file system
Eight run scripts running default.cfg Run config.cfg to overwrite existing variables with the parameters incorporated by the command line, that is, the parameters incorporated by the command line have a high priority
Nine initialization variable Add command: z_stats: Display allocated memory size and block error: throw an exception, estimate to test exception
Add the following variable: Host_SPEEDS Host Log_Stats Log Status DEVELOPER Development? TimeScale time scale fixedTime fixed time? Logfile_Active Log Does SHOWTRACE Displays the tracking information {$ ifdef deted_only} is a word version Dedicated: = CVAR_GET ('Dedicated', '1', cvar_noset); {$ else} Dedicated: = cvar_get ('Dedicated', '0' , Cvar_noset; {$ ENDIF} Ten Setup Settings Software Version Variable 11: Initialization System (Console) This step is to enter commands like a DOS window.
Twelve: Initialization Network and Port Here, there is a tip of a small trick, using milliseconds of the last four-digit as a port, but I didn't see the solution of port conflicts. Probably because this probability is very small. The thirteen initialization server adds the command and variables you want to use on the server.
14 Initialization Client Inputting Console Initialization Rendering Module VID_DLL Initialization Sound Module Input Soothing Input Sounds Input The Local Operation Command Input Input Running Autoexec.cfg Scripts
Fifteen: If the user does not enter command, and no "dedication" flag is displayed, the demo is displayed.
At this point, the first phase analysis comes to a paragraph.