Changeset - r2055:3f0149453249
[Not reviewed]
master
0 9 0
ludde - 19 years ago 2005-07-14 09:43:59
ludde@openttd.org
(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.
9 files changed with 68 insertions and 78 deletions:
0 comments (0 inline, 0 general)
landscape.c
Show inline comments
 
@@ -419,13 +419,13 @@ void RunTileLoop(void)
 
	tile += 9;
 
	if (tile & TILELOOP_CHKMASK)
 
		tile = (tile + MapSizeX()) & TILELOOP_ASSERTMASK;
 
	_cur_tileloop_tile = tile;
 
}
 

	
 
void InitializeLandscape()
 
void InitializeLandscape(void)
 
{
 
	uint map_size;
 
	uint i;
 

	
 

	
 
	map_size = MapSize();
network.c
Show inline comments
 
@@ -90,13 +90,12 @@ void CDECL NetworkTextMessage(NetworkAct
 
{
 
	char buf[1024];
 
	va_list va;
 
	const int duration = 10; // Game days the messages stay visible
 
	char message[1024];
 
	char temp[1024];
 
	StringID TempStr = STR_NULL;
 

	
 
	va_start(va, str);
 
	vsprintf(buf, str, va);
 
	va_end(va);
 

	
 
	switch (action) {
 
@@ -107,63 +106,51 @@ void CDECL NetworkTextMessage(NetworkAct
 
		case NETWORK_ACTION_LEAVE:
 
			GetString(temp, STR_NETWORK_ERR_LEFT);
 
			snprintf(message, sizeof(message), "*** %s %s (%s)", name, temp, buf);
 
			break;
 
		case NETWORK_ACTION_GIVE_MONEY:
 
			if (self_send) {
 
				TempStr = AllocateName(name, 0);
 
				SetDParam(0, TempStr);
 
				SetDParamStr(0, name);
 
				SetDParam(1, atoi(buf));
 
				GetString(temp, STR_NETWORK_GAVE_MONEY_AWAY);
 
				DeleteName(TempStr);
 
				snprintf(message, sizeof(message), "*** %s", temp);
 
			} else {
 
				SetDParam(0, atoi(buf));
 
				GetString(temp, STR_NETWORK_GIVE_MONEY);
 
				snprintf(message, sizeof(message), "*** %s %s", name, temp);
 
			}
 
			break;
 
		case NETWORK_ACTION_CHAT_PLAYER:
 
			if (self_send) {
 
				TempStr = AllocateName(name, 0);
 
				SetDParam(0, TempStr);
 
				SetDParamStr(0, name);
 
				GetString(temp, STR_NETWORK_CHAT_TO_COMPANY);
 
				DeleteName(TempStr);
 
				snprintf(message, sizeof(message), "%s %s", temp, buf);
 
			} else {
 
				TempStr = AllocateName(name, 0);
 
				SetDParam(0, TempStr);
 
				SetDParamStr(0, name);
 
				GetString(temp, STR_NETWORK_CHAT_COMPANY);
 
				DeleteName(TempStr);
 
				snprintf(message, sizeof(message), "%s %s", temp, buf);
 
			}
 
			break;
 
		case NETWORK_ACTION_CHAT_CLIENT:
 
			if (self_send) {
 
				TempStr = AllocateName(name, 0);
 
				SetDParam(0, TempStr);
 
				SetDParamStr(0, name);
 
				GetString(temp, STR_NETWORK_CHAT_TO_CLIENT);
 
				DeleteName(TempStr);
 
				snprintf(message, sizeof(message), "%s %s", temp, buf);
 
			} else {
 
				TempStr = AllocateName(name, 0);
 
				SetDParam(0, TempStr);
 
				SetDParamStr(0, name);
 
				GetString(temp, STR_NETWORK_CHAT_CLIENT);
 
				DeleteName(TempStr);
 
				snprintf(message, sizeof(message), "%s %s", temp, buf);
 
			}
 
			break;
 
		case NETWORK_ACTION_NAME_CHANGE:
 
			GetString(temp, STR_NETWORK_NAME_CHANGE);
 
			snprintf(message, sizeof(message), "*** %s %s %s", name, temp, buf);
 
			break;
 
		default:
 
			TempStr = AllocateName(name, 0);
 
			SetDParam(0, TempStr);
 
			SetDParamStr(0, name);
 
			GetString(temp, STR_NETWORK_CHAT_ALL);
 
			DeleteName(TempStr);
 
			snprintf(message, sizeof(message), "%s %s", temp, buf);
 
			break;
 
	}
 

	
 
	IConsolePrintF(color, "%s", message);
 
	AddTextMessage(color, duration, "%s", message);
network_gui.c
Show inline comments
 
@@ -187,16 +187,17 @@ static void NetworkGameWindowWndProc(Win
 
			DrawStringMultiCenter(365, 110, STR_NETWORK_SERVER_OFFLINE, 2); // server offline
 
		} else { // show game info
 
			uint16 y = 70;
 

	
 
			DrawStringMultiCenter(365, 30, STR_NETWORK_GAME_INFO, 0);
 

	
 
			SetDParam(0, _str_game_name);
 

	
 
			SetDParamStr(0, _selected_item->info.server_name);
 
			DrawStringMultiCenter(365, 42, STR_ORANGE, 2); // game name
 

	
 
			SetDParam(0, _str_map_name);
 
			SetDParamStr(0, _selected_item->info.map_name);
 
			DrawStringMultiCenter(365, 54, STR_02BD, 2); // map name
 

	
 
			SetDParam(0, _selected_item->info.clients_on);
 
			SetDParam(1, _selected_item->info.clients_max);
 
			DrawString(260, y, STR_NETWORK_CLIENTS, 2); // clients on the server / maximum slots
 
			y+=10;
 
@@ -211,17 +212,17 @@ static void NetworkGameWindowWndProc(Win
 

	
 
			SetDParam(0, _selected_item->info.map_width);
 
			SetDParam(1, _selected_item->info.map_height);
 
			DrawString(260, y, STR_NETWORK_MAP_SIZE, 2); // map size
 
			y+=10;
 

	
 
			SetDParam(0, _str_server_version);
 
			SetDParamStr(0, _selected_item->info.server_revision);
 
			DrawString(260, y, STR_NETWORK_SERVER_VERSION, 2); // server version
 
			y+=10;
 

	
 
			SetDParam(0, _str_server_address);
 
			SetDParamStr(0, _selected_item->info.hostname);
 
			SetDParam(1, _selected_item->port);
 
			DrawString(260, y, STR_NETWORK_SERVER_ADDRESS, 2); // server address
 
			y+=10;
 

	
 
			SetDParam(0, _selected_item->info.start_date);
 
			DrawString(260, y, STR_NETWORK_START_DATE, 2); // start date
 
@@ -270,56 +271,29 @@ static void NetworkGameWindowWndProc(Win
 
					// click out of vehicle bounds
 
					_selected_item = NULL;
 
					SetWindowDirty(w);
 
					return;
 
				}
 
				_selected_item = cur_item;
 

	
 
				DeleteName(_str_game_name);
 
				DeleteName(_str_map_name);
 
				DeleteName(_str_server_version);
 
				DeleteName(_str_server_address);
 
				if (_selected_item->info.server_name[0] != '\0')
 
					_str_game_name = AllocateName(_selected_item->info.server_name, 0);
 
				else
 
					_str_game_name = STR_EMPTY;
 

	
 
				if (_selected_item->info.map_name[0] != '\0')
 
					_str_map_name = AllocateName(_selected_item->info.map_name, 0);
 
				else
 
					_str_map_name = STR_EMPTY;
 

	
 
				if (_selected_item->info.server_revision[0] != '\0')
 
					_str_server_version = AllocateName(_selected_item->info.server_revision, 0);
 
				else
 
					_str_server_version = STR_EMPTY;
 

	
 
				if (_selected_item->info.hostname[0] != '\0')
 
					_str_server_address = AllocateName(_selected_item->info.hostname, 0);
 
				else
 
					_str_server_address = STR_EMPTY;
 
			}
 
			SetWindowDirty(w);
 
		} break;
 
		case 11: /* Find server automatically */
 
			switch (_network_lan_internet) {
 
				case 0: NetworkUDPSearchGame(); break;
 
				case 1: NetworkUDPQueryMasterServer(); break;
 
			}
 
			break;
 
		case 12: { // Add a server
 
				StringID str = AllocateName(_network_default_ip, 0);
 

	
 
				ShowQueryString(
 
				str,
 
				BindCString(_network_default_ip),
 
				STR_NETWORK_ENTER_IP,
 
				31 | 0x1000,  // maximum number of characters OR
 
				250, // characters up to this width pixels, whichever is satisfied first
 
				w->window_class,
 
				w->window_number);
 
				DeleteName(str);
 
		} break;
 
		case 13: /* Start server */
 
			ShowNetworkStartServerWindow();
 
			break;
 
		case 17: /* Join Game */
 
			if (_selected_item != NULL) {
 
@@ -533,16 +507,14 @@ static void NetworkStartServerWindowWndP
 
		_selected_field = e->click.widget;
 
		switch(e->click.widget) {
 
		case 0: case 15: /* Close 'X' | Cancel button */
 
			ShowNetworkGameWindow();
 
			break;
 
		case 4: { /* Set password button */
 
			StringID str;
 
			str = AllocateName(_network_server_password, 0);
 
			ShowQueryString(str, STR_NETWORK_SET_PASSWORD, 20, 250, w->window_class, w->window_number);
 
			DeleteName(str);
 
			ShowQueryString(BindCString(_network_server_password),
 
				STR_NETWORK_SET_PASSWORD, 20, 250, w->window_class, w->window_number);
 
			} break;
 
		case 5: { /* Select map */
 
			int y = (e->click.pt.y - NSSWND_START) / NSSWND_ROWSIZE;
 
			if ((y += w->vscroll.pos) >= w->vscroll.count)
 
				return;
 
			if (y == 0) selected_map = NULL;
 
@@ -694,13 +666,12 @@ static byte NetworkLobbyFindCompanyIndex
 

	
 
static void NetworkLobbyWindowWndProc(Window *w, WindowEvent *e)
 
{
 
	switch(e->event) {
 
	case WE_PAINT: {
 
		int y = NET_PRC__OFFSET_TOP_WIDGET_COMPANY, pos;
 
		StringID str;
 

	
 
		w->disabled_state = (_selected_company_item == -1) ? 1 << 7 : 0;
 

	
 
		if (_network_lobby_company_count == MAX_PLAYERS)
 
			SETBIT(w->disabled_state, 8);
 
		/* You can not join a server as spectator when it has no companies active..
 
@@ -742,16 +713,14 @@ static void NetworkLobbyWindowWndProc(Wi
 
		if (_selected_company_item != -1) { // if a company is selected...
 
			// show company info
 
			const uint x = 183;
 
			uint xm;
 
			y = 65;
 

	
 
			str = AllocateName(_network_player_info[_selected_company_item].company_name, 0);
 
			SetDParam(0, str);
 
			SetDParamStr(0, _network_player_info[_selected_company_item].company_name);
 
			DrawString(x, y, STR_NETWORK_COMPANY_NAME, 2);
 
			DeleteName(str);
 
			y += 10;
 

	
 
			SetDParam(0, _network_player_info[_selected_company_item].inaugurated_year + MAX_YEAR_BEGIN_REAL);
 
			DrawString(x, y, STR_NETWORK_INAUGURATION_YEAR, 2); // inauguration year
 
			y += 10;
 

	
 
@@ -784,16 +753,14 @@ static void NetworkLobbyWindowWndProc(Wi
 
			SetDParam(2, _network_player_info[_selected_company_item].num_station[2]);
 
			SetDParam(3, _network_player_info[_selected_company_item].num_station[3]);
 
			SetDParam(4, _network_player_info[_selected_company_item].num_station[4]);
 
			DrawString(x, y, STR_NETWORK_STATIONS, 2); // stations
 
			y += 10;
 

	
 
			str = AllocateName(_network_player_info[_selected_company_item].players, 0);
 
			SetDParam(0, str);
 
			SetDParamStr(0, _network_player_info[_selected_company_item].players);
 
			xm = DrawString(x, y, STR_NETWORK_PLAYERS, 2); // players
 
			DeleteName(str);
 
			y += 10;
 
		}
 
	}	break;
 

	
 
	case WE_CLICK:
 
		switch(e->click.widget) {
openttd.c
Show inline comments
 
@@ -735,13 +735,13 @@ int ttd_main(int argc, char* argv[])
 
	return 0;
 
}
 

	
 
static void ShowScreenshotResult(bool b)
 
{
 
	if (b) {
 
		SetDParam(0, STR_SPEC_SCREENSHOT_NAME);
 
		SetDParamStr(0, _screenshot_name);
 
		ShowErrorMessage(INVALID_STRING_ID, STR_031B_SCREENSHOT_SUCCESSFULLY, 0, 0);
 
	} else {
 
		ShowErrorMessage(INVALID_STRING_ID, STR_031C_SCREENSHOT_FAILED, 0, 0);
 
	}
 

	
 
}
openttd.h
Show inline comments
 
@@ -514,14 +514,15 @@ enum SpecialStrings {
 
	SPECSTR_RESOLUTION_END = 0x713f,
 

	
 
	// reserve 32 strings for screenshot formats
 
	SPECSTR_SCREENSHOT_START = 0x7140,
 
	SPECSTR_SCREENSHOT_END = 0x715F,
 

	
 
	STR_SPEC_SCREENSHOT_NAME = 0xF800,
 
	STR_SPEC_USERSTRING = 0xF801,
 
	// Used to implement SetDParamStr
 
	STR_SPEC_DYNSTRING = 0xF800,
 
	STR_SPEC_USERSTRING = 0xF808,
 
};
 

	
 
typedef void PlaceProc(TileIndex tile);
 

	
 
VARDEF byte _savegame_sort_order;
 

	
player_gui.c
Show inline comments
 
@@ -611,17 +611,15 @@ static void PlayerCompanyWndProc(Window 
 
		case 10: /* sell 25% */
 
			DoCommandP(0, w->window_number, 0, NULL, CMD_SELL_SHARE_IN_COMPANY | CMD_MSG(STR_707C_CAN_T_SELL_25_SHARE_IN));
 
			break;
 
		case 11: { /* Password protect company */
 
			#ifdef ENABLE_NETWORK
 
			if (!IsWindowOfPrototype(w, _other_player_company_widgets)) {
 
				StringID str;
 
				WP(w,def_d).byte_1 = 2;
 
				str = AllocateName(_network_player_info[_local_player].password, 0);
 
				ShowQueryString(str, STR_SET_COMPANY_PASSWORD, sizeof(_network_player_info[_local_player].password), 250, w->window_class, w->window_number);
 
				DeleteName(str);
 
				ShowQueryString(BindCString(_network_player_info[_local_player].password),
 
					STR_SET_COMPANY_PASSWORD, sizeof(_network_player_info[_local_player].password), 250, w->window_class, w->window_number);
 
			}
 
			#endif
 
		}	break;
 
		}
 

	
 
	case WE_MOUSELOOP:
settings_gui.c
Show inline comments
 
@@ -1381,27 +1381,27 @@ static void CustCurrencyWndProc(Window *
 
					edittext = true;
 
				}
 
			break;
 
			case 1: // separator
 
				if ( IS_INT_INSIDE(x, 10, 30) )  // clicked button
 
					WP(w,def_d).data_1 =  (1 << (line * 2 + 1));
 
				str = AllocateName(_str_separator, 0);
 
				str = BindCString(_str_separator);
 
				len = 1;
 
				edittext = true;
 
			break;
 
			case 2: // prefix
 
				if ( IS_INT_INSIDE(x, 10, 30) )  // clicked button
 
					WP(w,def_d).data_1 =  (1 << (line * 2 + 1));
 
				str = AllocateName(_currency_specs[23].prefix, 0);
 
				str = BindCString(_currency_specs[23].prefix);
 
				len = 12;
 
				edittext = true;
 
			break;
 
			case 3: // suffix
 
				if ( IS_INT_INSIDE(x, 10, 30) )  // clicked button
 
					WP(w,def_d).data_1 =  (1 << (line * 2 + 1));
 
				str = AllocateName(_currency_specs[23].suffix, 0);
 
				str = BindCString(_currency_specs[23].suffix);
 
				len = 12;
 
				edittext = true;
 
			break;
 
			case 4: // to euro
 
				if ( IS_INT_INSIDE(x, 10, 30) ) { // clicked buttons
 
					if (x < 20) {
 
@@ -1429,13 +1429,12 @@ static void CustCurrencyWndProc(Window *
 
			str,
 
			STR_CURRENCY_CHANGE_PARAMETER,
 
			len + 1, // maximum number of characters OR
 
			250, // characters up to this width pixels, whichever is satisfied first
 
			w->window_class,
 
			w->window_number);
 
			if (str !=  STR_CONFIG_PATCHES_INT32) DeleteName(str);
 
		}
 

	
 
		w->flags4 |= 5 << WF_TIMEOUT_SHL;
 
		SetWindowDirty(w);
 
	} break;
 

	
strings.c
Show inline comments
 
@@ -122,12 +122,23 @@ static const StringID _cargo_string_list
 
		STR_LITERS,
 
		STR_NOTHING
 
	}
 
};
 

	
 

	
 
#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)];
 
}
 

	
 
char *GetString(char *buffr, StringID string)
 
@@ -139,15 +150,12 @@ char *GetString(char *buffr, StringID st
 
		case 0:
 
			error("!invalid string id 0 in GetString");
 
			break;
 

	
 
		case 0x30D1:
 
			return StationGetSpecialString(buffr);
 

	
 
		case STR_SPEC_SCREENSHOT_NAME:
 
			return DecodeString(buffr, _screenshot_name);
 
	}
 

	
 
	switch (tab) {
 
		case 4:
 
			if (index >= 0xC0) return GetSpecialTownNameString(buffr, index - 0xC0);
 
			break;
 
@@ -157,13 +165,17 @@ char *GetString(char *buffr, StringID st
 
			break;
 

	
 
		case 15:
 
			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;
 
	}
 

	
 
	if (index >= _langtab_num[tab])
 
@@ -172,12 +184,28 @@ char *GetString(char *buffr, StringID st
 
			"Probably because an old version of the .lng file.\n", 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));
 
}
 

	
 

	
variables.h
Show inline comments
 
@@ -421,12 +421,22 @@ static inline void SetDParam64(uint n, u
 
static inline uint32 GetDParam(uint n)
 
{
 
	assert(n < lengthof(_decode_parameters));
 
	return _decode_parameters[n];
 
}
 

	
 
// Used to bind a C string name to a dparam number.
 
// NOTE: This has a short lifetime. You can't
 
//       use this string much later or it will be gone.
 
void SetDParamStr(uint n, const char *str);
 

	
 
// This function takes a C-string and allocates a temporary string ID.
 
// The duration of the bound string is valid only until the next acll to GetString,
 
// so be careful.
 
StringID BindCString(const char *str);
 

	
 

	
 
#define COPY_IN_DPARAM(offs,src,num) memcpy(_decode_parameters + offs, src, sizeof(uint32) * (num))
 
#define COPY_OUT_DPARAM(dst,offs,num) memcpy(dst,_decode_parameters + offs, sizeof(uint32) * (num))
 

	
 

	
 
#define SET_EXPENSES_TYPE(x) _yearly_expenses_type = x;
0 comments (0 inline, 0 general)