(svn r1486) -Codechange: moved all 'signs' stuff to signs.c/h and prepared it for
authortruelight
Wed, 12 Jan 2005 11:21:28 +0000
changeset 988 2482369a61ff
parent 987 ea47ad079de8
child 989 36d9d925825b
(svn r1486) -Codechange: moved all 'signs' stuff to signs.c/h and prepared it for
dynamic arrays
Makefile
functions.h
gui.h
main_gui.c
misc.c
misc_cmd.c
oldloader.c
signs.c
signs.h
terraform_gui.c
tree_cmd.c
ttd.c
ttd.dsp
ttd.h
ttd.vcproj
variables.h
viewport.c
--- a/Makefile	Wed Jan 12 00:50:08 2005 +0000
+++ b/Makefile	Wed Jan 12 11:21:28 2005 +0000
@@ -613,6 +613,7 @@
 C_SOURCES += settings_gui.c
 C_SOURCES += ship_cmd.c
 C_SOURCES += ship_gui.c
+C_SOURCES += signs.c
 C_SOURCES += smallmap_gui.c
 C_SOURCES += sound.c
 C_SOURCES += sprite.c
--- a/functions.h	Wed Jan 12 00:50:08 2005 +0000
+++ b/functions.h	Wed Jan 12 11:21:28 2005 +0000
@@ -232,7 +232,6 @@
 void ShowNetworkGiveMoneyWindow(byte player);
 void ShowNetworkNeedGamePassword();
 void ShowNetworkNeedCompanyPassword();
-void ShowRenameSignWindow(SignStruct *ss);
 void ShowRenameWaypointWindow(Waypoint *cp);
 int FindFirstBit(uint32 x);
 void ShowHighscoreTable(int difficulty, int rank);
--- a/gui.h	Wed Jan 12 00:50:08 2005 +0000
+++ b/gui.h	Wed Jan 12 11:21:28 2005 +0000
@@ -6,7 +6,6 @@
 /* main_gui.c */
 void SetupColorsAndInitialWindow();
 void CcPlaySound10(bool success, uint tile, uint32 p1, uint32 p2);
-void PlaceProc_Sign(uint tile);
 
 /* settings_gui.c */
 void ShowGameOptions();
--- a/main_gui.c	Wed Jan 12 00:50:08 2005 +0000
+++ b/main_gui.c	Wed Jan 12 11:21:28 2005 +0000
@@ -14,6 +14,7 @@
 #include "console.h"
 #include "sound.h"
 #include "network.h"
+#include "signs.h"
 
 #ifdef ENABLE_NETWORK
 #include "network_data.h"
@@ -370,7 +371,7 @@
 
 void ShowRenameSignWindow(SignStruct *ss)
 {
-	_rename_id = ss - _sign_list;
+	_rename_id = ss->index;
 	_rename_what = 0;
 	ShowQueryString(ss->str, STR_280B_EDIT_SIGN_TEXT, 30, 180, 1, 0);
 }
@@ -392,19 +393,6 @@
 	ShowQueryString(STR_WAYPOINT_RAW, STR_EDIT_WAYPOINT_NAME, 30, 180, 1, 0);
 }
 
-void CcPlaceSign(bool success, uint tile, uint32 p1, uint32 p2)
-{
-	if (success) {
-		ShowRenameSignWindow(_new_sign_struct);
-		ResetObjectToPlace();
-	}
-}
-
-void PlaceProc_Sign(uint tile)
-{
-	DoCommandP(tile, 0, 0, CcPlaceSign, CMD_PLACE_SIGN | CMD_MSG(STR_2809_CAN_T_PLACE_SIGN_HERE));
-}
-
 static void SelectSignTool()
 {
 	if (_cursor.sprite == 0x2D2)
--- a/misc.c	Wed Jan 12 00:50:08 2005 +0000
+++ b/misc.c	Wed Jan 12 11:21:28 2005 +0000
@@ -162,6 +162,7 @@
 void InitializeLandscape();
 void InitializeTowns();
 void InitializeTrees();
+void InitializeSigns();
 void InitializeStations();
 static void InitializeNameMgr();
 void InitializePlayers();
@@ -220,6 +221,7 @@
 	InitializeDockGui();
 	InitializeTowns();
 	InitializeTrees();
+	InitializeSigns();
 	InitializeStations();
 	InitializeIndustries();
 
--- a/misc_cmd.c	Wed Jan 12 00:50:08 2005 +0000
+++ b/misc_cmd.c	Wed Jan 12 11:21:28 2005 +0000
@@ -172,89 +172,6 @@
 	return 0;
 }
 
-static void UpdateSignVirtCoords(SignStruct *ss)
-{
-	Point pt = RemapCoords(ss->x, ss->y, ss->z);
-	SetDParam(0, ss->str);
-	UpdateViewportSignPos(&ss->sign, pt.x, pt.y - 6, STR_2806);
-}
-
-void UpdateAllSignVirtCoords()
-{
-	SignStruct *ss;
-	for(ss=_sign_list; ss != endof(_sign_list); ss++)
-		if (ss->str != 0)
-			UpdateSignVirtCoords(ss);
-
-}
-
-static void MarkSignDirty(SignStruct *ss)
-{
-	MarkAllViewportsDirty(
-		ss->sign.left-6,
-		ss->sign.top-3,
-		ss->sign.left+ss->sign.width_1*4+12,
-		ss->sign.top + 45
-	);
-}
-
-
-int32 CmdPlaceSign(int x, int y, uint32 flags, uint32 p1, uint32 p2)
-{
-	SignStruct *ss;
-
-	for(ss=_sign_list; ss != endof(_sign_list); ss++) {
-		if (ss->str == 0) {
-			if (flags & DC_EXEC) {
-				ss->str = STR_280A_SIGN;
-				ss->x = x;
-				ss->y = y;
-				ss->z = GetSlopeZ(x,y);
-				UpdateSignVirtCoords(ss);
-				MarkSignDirty(ss);
-				_new_sign_struct = ss;
-			}
-			return 0;
-		}
-	}
-
-	return_cmd_error(STR_2808_TOO_MANY_SIGNS);
-}
-
-// p1 = sign index
-// p2: 1 -> remove sign
-int32 CmdRenameSign(int x, int y, uint32 flags, uint32 p1, uint32 p2)
-{
-	StringID str,old_str;
-	SignStruct *ss;
-
-	if (_decode_parameters[0] != 0 && !p2) {
-		str = AllocateNameUnique((byte*)_decode_parameters, 0);
-		if (str == 0)
-			return CMD_ERROR;
-
-		if (flags & DC_EXEC) {
-			ss = &_sign_list[p1];
-			MarkSignDirty(ss);
-			DeleteName(ss->str);
-			ss->str = str;
-			UpdateSignVirtCoords(ss);
-			MarkSignDirty(ss);
-		} else {
-			DeleteName(str);
-		}
-	}	else {
-		if (flags & DC_EXEC) {
-			ss = &_sign_list[p1];
-			old_str = ss->str;
-			ss->str = 0;
-			DeleteName(old_str);
-			MarkSignDirty(ss);
-		}
-	}
-	return 0;
-}
-
 // p1 = 0   decrease pause counter
 // p1 = 1   increase pause counter
 int32 CmdPause(int x, int y, uint32 flags, uint32 p1, uint32 p2)
@@ -318,39 +235,3 @@
 	}
 	return 0;
 }
-
-static const byte _sign_desc[] = {
-	SLE_VAR(SignStruct,str,						SLE_UINT16),
-	SLE_CONDVAR(SignStruct,x,         SLE_FILE_I16 | SLE_VAR_I32, 0, 4),
-	SLE_CONDVAR(SignStruct,y,         SLE_FILE_I16 | SLE_VAR_I32, 0, 4),
-	SLE_CONDVAR(SignStruct,x,         SLE_INT32, 5, 255),
-	SLE_CONDVAR(SignStruct,y,         SLE_INT32, 5, 255),
-	SLE_VAR(SignStruct,z,							SLE_UINT8),
-	SLE_END()
-};
-
-static void Save_SIGN()
-{
-	SignStruct *s;
-	int i;
-	for(i=0,s=_sign_list; i!=lengthof(_sign_list); i++,s++) {
-		if (s->str != 0) {
-			SlSetArrayIndex(i);
-			SlObject(s, _sign_desc);
-		}
-	}
-}
-
-static void Load_SIGN()
-{
-	int index;
-	while ((index = SlIterateArray()) != -1) {
-		SlObject(&_sign_list[index], _sign_desc);
-	}
-}
-
-const ChunkHandler _sign_chunk_handlers[] = {
-	{ 'SIGN', Save_SIGN, Load_SIGN, CH_ARRAY | CH_LAST},
-};
-
-
--- a/oldloader.c	Wed Jan 12 00:50:08 2005 +0000
+++ b/oldloader.c	Wed Jan 12 11:21:28 2005 +0000
@@ -9,6 +9,7 @@
 #include "player.h"
 #include "engine.h"
 #include "vehicle.h"
+#include "signs.h"
 
 extern byte _name_array[512][32];
 extern TileIndex _animated_tile_list[256];
@@ -1027,14 +1028,22 @@
 	}
 }
 
-static void FixSign(SignStruct *n, OldSign *o, int num)
+static void FixSign(OldSign *o, int num)
 {
+	SignStruct *n;
+	uint i = 0;
+
 	do {
+		if (o->text == 0)
+			continue;
+
+		n = GetSign(i);
+
 		n->str = o->text;
 		n->x = o->x;
 		n->y = o->y;
 		n->z = o->z;
-	} while (n++,o++,--num);
+	} while (i++,o++,--num);
 }
 
 static void FixEngine(Engine *n, OldEngine *o, int num)
@@ -1452,7 +1461,7 @@
 
 	FixPlayer(_players, m->players, lengthof(m->players), m->town_name_type);
 	FixName(m->names, lengthof(m->names));
-	FixSign(_sign_list, m->signs, lengthof(m->signs));
+	FixSign(m->signs, lengthof(m->signs));
 	FixEngine(_engines, m->engines, lengthof(m->engines));
 
 	_opt.diff_level = m->difficulty_level;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/signs.c	Wed Jan 12 11:21:28 2005 +0000
@@ -0,0 +1,233 @@
+#include "stdafx.h"
+#include "ttd.h"
+#include "table/strings.h"
+#include "signs.h"
+#include "saveload.h"
+#include "command.h"
+
+/**
+ *
+ * Update the coordinate of one sign
+ *
+ */
+static void UpdateSignVirtCoords(SignStruct *ss)
+{
+	Point pt = RemapCoords(ss->x, ss->y, ss->z);
+	SetDParam(0, ss->str);
+	UpdateViewportSignPos(&ss->sign, pt.x, pt.y - 6, STR_2806);
+}
+
+/**
+ *
+ * Update all coordinates of a sign
+ *
+ */
+void UpdateAllSignVirtCoords()
+{
+	SignStruct *ss;
+
+	FOR_ALL_SIGNS(ss)
+		if (ss->str != 0)
+			UpdateSignVirtCoords(ss);
+
+}
+
+/**
+ *
+ * Marks the region of a sign as dirty
+ *
+ * @param ss Pointer to the SignStruct
+ */
+static void MarkSignDirty(SignStruct *ss)
+{
+	MarkAllViewportsDirty(
+		ss->sign.left - 6,
+		ss->sign.top  - 3,
+		ss->sign.left + ss->sign.width_1 * 4 + 12,
+		ss->sign.top  + 45);
+}
+
+/**
+ *
+ * Allocates a new sign
+ *
+ * @return The pointer to the new sign, or NULL if there is no more free space
+ */
+static SignStruct *AllocateSign()
+{
+	SignStruct *s;
+	FOR_ALL_SIGNS(s)
+		if (s->str == 0)
+			return s;
+
+	return NULL;
+}
+
+/**
+ *
+ * Place a sign at the giving x/y
+ *
+ * @param p1 not used
+ * @param p2 not used
+ */
+int32 CmdPlaceSign(int x, int y, uint32 flags, uint32 p1, uint32 p2)
+{
+	SignStruct *ss;
+
+	/* Try to locate a new sign */
+	ss = AllocateSign();
+	if (ss == NULL)
+		return_cmd_error(STR_2808_TOO_MANY_SIGNS);
+
+	/* When we execute, really make the sign */
+	if (flags & DC_EXEC) {
+		ss->str = STR_280A_SIGN;
+		ss->x = x;
+		ss->y = y;
+		ss->z = GetSlopeZ(x,y);
+		UpdateSignVirtCoords(ss);
+		MarkSignDirty(ss);
+		_new_sign_struct = ss;
+	}
+
+	return 0;
+}
+
+/**
+ * Rename a sign
+ *
+ * @param sign_id Index of the sign
+ * @param remove  If 1, sign will be removed
+ */
+int32 CmdRenameSign(int x, int y, uint32 flags, uint32 sign_id, uint32 remove)
+{
+	StringID str;
+	SignStruct *ss;
+
+	/* If GetDParam(0) == nothing, we delete the sign */
+	if (GetDParam(0) != 0 && remove != 1) {
+		/* Create the name */
+		str = AllocateName((byte*)_decode_parameters, 0);
+		if (str == 0)
+			return CMD_ERROR;
+
+		if (flags & DC_EXEC) {
+			ss = GetSign(sign_id);
+
+			MarkSignDirty(ss);
+
+			/* Delete the old name */
+			DeleteName(ss->str);
+			/* Assign the new one */
+			ss->str = str;
+
+			/* Update */
+			UpdateSignVirtCoords(ss);
+			MarkSignDirty(ss);
+		} else {
+			/* Free the name, because we did not assign it yet */
+			DeleteName(str);
+		}
+	} else {
+		/* Delete sign */
+		if (flags & DC_EXEC) {
+			ss = GetSign(sign_id);
+
+			/* Delete the name */
+			DeleteName(ss->str);
+			ss->str = 0;
+
+			MarkSignDirty(ss);
+		}
+	}
+
+	return 0;
+}
+
+/**
+ *
+ * Callback function that is called after a sign is placed
+ *
+ */
+void CcPlaceSign(bool success, uint tile, uint32 p1, uint32 p2)
+{
+	if (success) {
+		ShowRenameSignWindow(_new_sign_struct);
+		ResetObjectToPlace();
+	}
+}
+
+/**
+ *
+ * PlaceProc function, called when someone pressed the button if the
+ *  sign-tool is selected
+ *
+ */
+void PlaceProc_Sign(uint tile)
+{
+	DoCommandP(tile, 0, 0, CcPlaceSign, CMD_PLACE_SIGN | CMD_MSG(STR_2809_CAN_T_PLACE_SIGN_HERE));
+}
+
+/**
+ *
+ * Initialize the signs
+ *
+ */
+void InitializeSigns()
+{
+	SignStruct *s;
+	int i;
+
+	memset(_sign_list, 0, sizeof(_sign_list[0]) * _sign_size);
+
+	i = 0;
+	FOR_ALL_SIGNS(s)
+		s->index = i++;
+}
+
+static const byte _sign_desc[] = {
+	SLE_VAR(SignStruct,str,						SLE_UINT16),
+	SLE_CONDVAR(SignStruct,x,					SLE_FILE_I16 | SLE_VAR_I32, 0, 4),
+	SLE_CONDVAR(SignStruct,y,					SLE_FILE_I16 | SLE_VAR_I32, 0, 4),
+	SLE_CONDVAR(SignStruct,x,					SLE_INT32, 5, 255),
+	SLE_CONDVAR(SignStruct,y,					SLE_INT32, 5, 255),
+	SLE_VAR(SignStruct,z,							SLE_UINT8),
+	SLE_END()
+};
+
+/**
+ *
+ * Save all signs
+ *
+ */
+static void Save_SIGN()
+{
+	SignStruct *s;
+
+	FOR_ALL_SIGNS(s) {
+		/* Don't save empty signs */
+		if (s->str != 0) {
+			SlSetArrayIndex(s->index);
+			SlObject(s, _sign_desc);
+		}
+	}
+}
+
+/**
+ *
+ * Load all signs
+ *
+ */
+static void Load_SIGN()
+{
+	int index;
+	while ((index = SlIterateArray()) != -1) {
+		SignStruct *s = GetSign(index);
+
+		SlObject(s, _sign_desc);
+	}
+}
+
+const ChunkHandler _sign_chunk_handlers[] = {
+	{ 'SIGN', Save_SIGN, Load_SIGN, CH_ARRAY | CH_LAST},
+};
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/signs.h	Wed Jan 12 11:21:28 2005 +0000
@@ -0,0 +1,33 @@
+#ifndef SIGNS_H
+#define SIGNS_H
+
+typedef struct SignStruct {
+	StringID     str;
+	ViewportSign sign;
+	int32        x;
+	int32        y;
+	byte         z;
+
+	uint16       index;
+} SignStruct;
+
+VARDEF SignStruct _sign_list[40];
+VARDEF uint _sign_size;
+
+static inline SignStruct *GetSign(uint index)
+{
+	assert(index < _sign_size);
+	return &_sign_list[index];
+}
+
+#define FOR_ALL_SIGNS(s) for(s = _sign_list; s != &_sign_list[_sign_size]; s++)
+
+VARDEF SignStruct *_new_sign_struct;
+
+void UpdateAllSignVirtCoords();
+void PlaceProc_Sign(uint tile);
+
+/* misc.c */
+void ShowRenameSignWindow(SignStruct *ss);
+
+#endif /* SIGNS_H */
--- a/terraform_gui.c	Wed Jan 12 00:50:08 2005 +0000
+++ b/terraform_gui.c	Wed Jan 12 11:21:28 2005 +0000
@@ -8,7 +8,7 @@
 #include "sound.h"
 #include "command.h"
 #include "vehicle.h"
-
+#include "signs.h"
 
 void CcTerraform(bool success, uint tile, uint32 p1, uint32 p2)
 {
--- a/tree_cmd.c	Wed Jan 12 00:50:08 2005 +0000
+++ b/tree_cmd.c	Wed Jan 12 11:21:28 2005 +0000
@@ -641,7 +641,6 @@
 
 void InitializeTrees()
 {
-	memset(_sign_list, 0, sizeof(_sign_list));
 	_trees_tick_ctr = 0;
 }
 
--- a/ttd.c	Wed Jan 12 00:50:08 2005 +0000
+++ b/ttd.c	Wed Jan 12 11:21:28 2005 +0000
@@ -26,6 +26,7 @@
 #include "console.h"
 #include "screenshot.h"
 #include "network.h"
+#include "signs.h"
 
 #include <stdarg.h>
 
@@ -44,7 +45,6 @@
 
 extern void SetDifficultyLevel(int mode, GameOptions *gm_opt);
 extern void DoStartupNewPlayer(bool is_ai);
-extern void UpdateAllSignVirtCoords();
 extern void ShowOSErrorBox(const char *buf);
 
 void redsq_debug(int tile);
@@ -496,6 +496,8 @@
 
 	_industries_size = lengthof(_industries);
 	_industry_sort = NULL;
+
+	_sign_size = lengthof(_sign_list);
 }
 
 static void UnInitializeDynamicVariables(void)
@@ -1263,7 +1265,7 @@
 {
 	Window *w;
 	ViewPort *vp;
-	
+
 	// in version 2.1 of the savegame, town owner was unified.
 	if (version <= 0x200) {
 		ConvertTownOwner();
--- a/ttd.dsp	Wed Jan 12 00:50:08 2005 +0000
+++ b/ttd.dsp	Wed Jan 12 11:21:28 2005 +0000
@@ -297,6 +297,10 @@
 # End Source File
 
 # Begin Source File
+SOURCE=.\signs.c
+# End Source File
+
+# Begin Source File
 SOURCE=.\sound.c
 # End Source File
 
@@ -510,6 +514,10 @@
 # End Source File
 
 # Begin Source File
+SOURCE=.\signs.h
+# End Source File
+
+# Begin Source File
 SOURCE=.\sound.h
 # End Source File
 
--- a/ttd.h	Wed Jan 12 00:50:08 2005 +0000
+++ b/ttd.h	Wed Jan 12 11:21:28 2005 +0000
@@ -285,15 +285,6 @@
 	byte width_1, width_2;
 } ViewportSign;
 
-typedef struct SignStruct {
-	StringID str;
-	ViewportSign sign;
-	int32 x;
-	int32 y;
-	byte z;
-} SignStruct;
-
-
 typedef int32 CommandProc(int x, int y, uint32 flags, uint32 p1, uint32 p2);
 
 typedef void DrawTileProc(TileInfo *ti);
--- a/ttd.vcproj	Wed Jan 12 00:50:08 2005 +0000
+++ b/ttd.vcproj	Wed Jan 12 11:21:28 2005 +0000
@@ -753,6 +753,9 @@
 				RelativePath=".\settings.c">
 			</File>
 			<File
+				RelativePath=".\signs.c">
+			</File>
+			<File
 				RelativePath="sound.c">
 				<FileConfiguration
 					Name="Release|Win32">
@@ -1219,6 +1222,9 @@
 				RelativePath=".\screenshot.h">
 			</File>
 			<File
+				RelativePath=".\signs.h">
+			</File>
+			<File
 				RelativePath="sound.h">
 			</File>
 			<File
--- a/variables.h	Wed Jan 12 00:50:08 2005 +0000
+++ b/variables.h	Wed Jan 12 11:21:28 2005 +0000
@@ -425,9 +425,6 @@
 VARDEF char _userstring[USERSTRING_LEN];
 VARDEF byte _vehicle_design_names;
 
-VARDEF SignStruct _sign_list[40];
-VARDEF SignStruct *_new_sign_struct;
-
 /* tunnelbridge */
 #define MAX_BRIDGES 13
 
--- a/viewport.c	Wed Jan 12 00:50:08 2005 +0000
+++ b/viewport.c	Wed Jan 12 11:21:28 2005 +0000
@@ -8,6 +8,7 @@
 #include "station.h"
 #include "gfx.h"
 #include "town.h"
+#include "signs.h"
 
 #define VIEWPORT_DRAW_MEM (65536 * 2)
 
@@ -885,7 +886,7 @@
 	bottom = top + dpi->height;
 
 	if (dpi->zoom < 1) {
-		for(ss=_sign_list; ss != endof(_sign_list); ss++) {
+		FOR_ALL_SIGNS(ss) {
 			if (ss->str &&
 					bottom > ss->sign.top &&
 					top < ss->sign.top + 12 &&
@@ -902,7 +903,7 @@
 	} else if (dpi->zoom == 1) {
 		right += 2;
 		bottom += 2;
-		for(ss=_sign_list; ss != endof(_sign_list); ss++) {
+		FOR_ALL_SIGNS(ss) {
 			if (ss->str &&
 					bottom > ss->sign.top &&
 					top < ss->sign.top + 24 &&
@@ -920,7 +921,7 @@
 		right += 4;
 		bottom += 5;
 
-		for(ss=_sign_list; ss != endof(_sign_list); ss++) {
+		FOR_ALL_SIGNS(ss) {
 			if (ss->str &&
 					bottom > ss->sign.top &&
 					top < ss->sign.top + 24 &&
@@ -1541,7 +1542,7 @@
 		x = x - vp->left + vp->virtual_left;
 		y = y - vp->top + vp->virtual_top;
 
-		for(ss = _sign_list; ss != endof(_sign_list); ss++) {
+		FOR_ALL_SIGNS(ss) {
 			if (ss->str &&
 			    y >= ss->sign.top &&
 					y < ss->sign.top + 12 &&
@@ -1554,7 +1555,7 @@
 	} else if (vp->zoom == 1) {
 		x = (x - vp->left + 1) * 2 + vp->virtual_left;
 		y = (y - vp->top + 1) * 2 + vp->virtual_top;
-		for(ss = _sign_list; ss != endof(_sign_list); ss++) {
+		FOR_ALL_SIGNS(ss) {
 			if (ss->str &&
 			    y >= ss->sign.top &&
 					y < ss->sign.top + 24 &&
@@ -1567,7 +1568,7 @@
 	} else {
 		x = (x - vp->left + 3) * 4 + vp->virtual_left;
 		y = (y - vp->top + 3) * 4 + vp->virtual_top;
-		for(ss = _sign_list; ss != endof(_sign_list); ss++) {
+		FOR_ALL_SIGNS(ss) {
 			if (ss->str &&
 			    y >= ss->sign.top &&
 					y < ss->sign.top + 24 &&