Tutorial *141*

This will combine FrikaC's file access tutorial with its string functions and my enhanced zone handling tutorial. If you haven't implemented those tutorials in your engine yet, then do it now.

FrikaC's functions will be modified to use a separate zone for the created strings. There will also be a cvar "pr_zone_min_strings" to define the size in KB of this zone, which can be set through the QuakeC code in worldspawn(). The zone will be allocated when it is accessed the first time.

First we define a new zone just for QuakeC created strings at the top of PR_CMDS.C...



memzone_t	*zone_progstrings;	// 2001-09-20 QuakeC string zone by Maddes

The memory for the zone is allocated in a separate function which must be placed before FrikaC's string and file access functions...



// 2001-09-20 QuakeC string zone by Maddes  start

/*

=================

PF_allocate_zone_progstrings

=================

*/

void PF_allocate_zone_progstrings (void)

{

	int	zonesize_progstrings;



	if (pr_zone_min_strings.value < 64)	// rangecheck/minimum

	{

		Cvar_Set("pr_zone_min_strings", "64");

	}

	if (pr_zone_min_strings.value > 8192)	// rangecheck/maximum (8MB)

	{

		Cvar_Set("pr_zone_min_strings", "8192");

	}

	zonesize_progstrings = pr_zone_min_strings->value * 1024;

	zone_progstrings = Hunk_AllocName (zonesize_progstrings, "qcstrings");	// note only 8 chars copied

	Z_ClearZone (zone_progstrings, zonesize_progstrings);

}

// 2001-09-20 QuakeC string zone by Maddes  end

The new function must be called whenever a new string is created and the zone isn't allocated. So place the following code at top of PF_strzone, PF_strunzone and the start of the append case in PF_fopen...



// 2001-09-20 QuakeC string zone by Maddes  start

	if (!zone_progstrings)

	{

		PF_allocate_zone_progstrings();

	}

// 2001-09-20 QuakeC string zone by Maddes  end

Now you can change all Z_Malloc and Z_Free calls in these three routines to use the new zone.

Another important thing is to initialize the zone pointer before the PROGS.DAT is processed in SV_SpawnServer() of SV_MAIN.C, so before(!)...



	ED_LoadFromFile (sv.worldmodel->entities);

... place ...



	zone_progstrings = NULL;	// 2001-09-20 QuakeC string zone by Maddes

The cvar "pr_zone_min_strings" will be defined in PR_EDICT.C by placing the following definition at top of it...



cvar_t	pr_zone_min_strings = {"pr_zone_min_strings", "0", false, false};	// 2001-09-20 QuakeC string zone by Maddes

... and registering it in PR_Init with ...



	Cvar_RegisterVariable (&pr_zone_min_strings);	// 2001-09-20 QuakeC string zone by Maddes

As the cvar and the zone pointer are used in several files, they must be declared in PROGS.H...



// 2001-09-20 QuakeC string zone by Maddes  start

extern cvar_t		pr_zone_min_strings;

extern memzone_t	*zone_progstrings;

// 2001-09-20 QuakeC string zone by Maddes  end

Engine done.

QuakeC coders can now easily define how much mem they need for strings.

Here's an example code:



void() worldspawn =

{

	local float	strzone;



	strzone = cvar("pr_zone_min_strings");

	if (strzone < 128)	// check minimum, if user wants more then let him get it

	{

		cvar_set("pr_zone_min_strings", "128");

	}

	...

};



 
Sign up
Login:
Passwd:
[Remember Me]