changeset 3501:08573f22dee3 draft

(svn r4352) - NewGRF Codechange: dynamically allocate the memory used to store custom station data. This saves us approximately 40KB per GRF file, if there are no stations defined.
author peter1138 <peter1138@openttd.org>
date Tue, 11 Apr 2006 10:05:52 +0000
parents 853efd6b1c88
children 612e6827848b
files newgrf.c newgrf.h
diffstat 2 files changed, 19 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/newgrf.c
+++ b/newgrf.c
@@ -762,10 +762,17 @@
 	int i;
 	int ret = 0;
 
-	/* This is one single huge TODO. It doesn't handle anything more than
-	 * just waypoints for now. */
-
-	//printf("sci %d %d [0x%02x]\n", stid, numinfo, prop);
+	/* Allocate station specs if necessary */
+	if (_cur_grffile->num_stations < stid + numinfo) {
+		_cur_grffile->stations = realloc(_cur_grffile->stations, (stid + numinfo) * sizeof(*_cur_grffile->stations))
+;
+
+		while (_cur_grffile->num_stations < stid + numinfo) {
+			memset(&_cur_grffile->stations[_cur_grffile->num_stations], 0, sizeof(*_cur_grffile->stations));
+			_cur_grffile->num_stations++;
+		}
+	}
+
 	switch (prop) {
 		case 0x08:
 		{	/* Class ID */
@@ -2357,7 +2364,7 @@
 	CargoID c;
 
 	for (file = _first_grffile; file != NULL; file = file->next) {
-		for (i = 0; i < lengthof(file->stations); i++) {
+		for (i = 0; i < file->num_stations; i++) {
 			if (file->stations[i].grfid != file->grfid) continue;
 
 			// TODO: Release renderdata, platforms and layouts
@@ -2368,6 +2375,11 @@
 					UnloadSpriteGroup(&file->stations[i].spritegroup[c]);
 			}
 		}
+
+		/* Free and reset the station data */
+		free(file->stations);
+		file->stations = NULL;
+		file->num_stations = 0;
 	}
 }
 
--- a/newgrf.h
+++ b/newgrf.h
@@ -34,7 +34,8 @@
 	int spritegroups_count;
 	SpriteGroup **spritegroups;
 
-	StationSpec stations[256];
+	uint num_stations;
+	StationSpec *stations;
 
 	uint32 param[0x80];
 	uint param_end; /// one more than the highest set parameter