summaryrefslogtreecommitdiff
path: root/strings.c
diff options
context:
space:
mode:
authorludde <ludde@openttd.org>2005-07-14 09:43:59 +0000
committerludde <ludde@openttd.org>2005-07-14 09:43:59 +0000
commit3e62457107457c8295076370425e9a1df44fa58c (patch)
treef6981bd89e076320ae37ab99f9dbf0619bca554c /strings.c
parent1a046ff4c6f0295328ccc0cd6a4cd7a491f7ddbf (diff)
downloadopenttd-3e62457107457c8295076370425e9a1df44fa58c.tar.xz
(svn r2564) Fix: Fixed conceptual issue in network_gui.c. AllocateName is not meant to be used by GUI-code, because it modifies the "game-state".
Added a way to bind a C-string to an openttd string which doesn't modify the game state.
Diffstat (limited to 'strings.c')
-rw-r--r--strings.c36
1 files changed, 32 insertions, 4 deletions
diff --git a/strings.c b/strings.c
index c50933321..f91d17e1a 100644
--- a/strings.c
+++ b/strings.c
@@ -125,6 +125,17 @@ static const StringID _cargo_string_list[NUM_LANDSCAPE][NUM_CARGO] = {
};
+#define NUM_BOUND_STRINGS 8
+
+// Array to hold the bound strings.
+static const char *_bound_strings[NUM_BOUND_STRINGS];
+
+// This index is used to implement a "round-robin" allocating of
+// slots for BindCString. NUM_BOUND_STRINGS slots are reserved.
+// Which means that after NUM_BOUND_STRINGS calls to BindCString,
+// the indices will be reused.
+static int _bind_index;
+
static const char *GetStringPtr(StringID string)
{
return _langpack_offs[_langtab_start[string >> 11] + (string & 0x7FF)];
@@ -142,9 +153,6 @@ char *GetString(char *buffr, StringID string)
case 0x30D1:
return StationGetSpecialString(buffr);
-
- case STR_SPEC_SCREENSHOT_NAME:
- return DecodeString(buffr, _screenshot_name);
}
switch (tab) {
@@ -160,7 +168,11 @@ char *GetString(char *buffr, StringID string)
return GetName(index, buffr);
case 31: // special or dynamic strings
- return DecodeString(buffr, _userstring);
+ if (index < (STR_SPEC_USERSTRING & 0x7FF)) {
+ return DecodeString(buffr, _bound_strings[index]);
+ } else {
+ return DecodeString(buffr, _userstring);
+ }
default:
break;
@@ -175,6 +187,22 @@ char *GetString(char *buffr, StringID string)
return DecodeString(buffr, GetStringPtr(string));
}
+// This function takes a C-string and allocates a temporary string ID.
+// The duration of the bound string is valid only until the next GetString,
+// so be careful.
+StringID BindCString(const char *str)
+{
+ int idx = (++_bind_index) & (NUM_BOUND_STRINGS - 1);
+ _bound_strings[idx] = str;
+ return idx + STR_SPEC_DYNSTRING;
+}
+
+// This function is used to "bind" a C string to a OpenTTD dparam slot.
+void SetDParamStr(uint n, const char *str)
+{
+ SetDParam(n, BindCString(str));
+}
+
void InjectDParam(int amount)
{
memmove(_decode_parameters + amount, _decode_parameters, sizeof(_decode_parameters) - amount * sizeof(uint32));