changeset 9010:b4fd0c0b6129 draft

(svn r12805) -Codechange: remove some bit magic related to the news display states. Patch by cirdan.
author rubidium <rubidium@openttd.org>
date Sun, 20 Apr 2008 11:40:33 +0000
parents 870efbdb988d
children 66fc4316eb79
files src/news_func.h src/news_gui.cpp src/news_type.h src/settings.cpp
diffstat 4 files changed, 52 insertions(+), 75 deletions(-) [+]
line wrap: on
line diff
--- a/src/news_func.h
+++ b/src/news_func.h
@@ -14,10 +14,9 @@
 void InitNewsItemStructs();
 
 extern NewsItem _statusbar_news_item;
-extern uint32 _news_display_opt;
 extern bool _news_ticker_sound;
 
-extern const NewsTypeData _news_type_data[NT_END];
+extern NewsTypeData _news_type_data[NT_END];
 
 /**
  * Delete a news item type about a vehicle
--- a/src/news_gui.cpp
+++ b/src/news_gui.cpp
@@ -53,7 +53,6 @@
 #define INVALID_NEWS 255
 
 NewsItem _statusbar_news_item;
-uint32 _news_display_opt;
 bool _news_ticker_sound;
 static NewsItem _news_items[MAX_NEWS];      ///< The news FIFO queue
 static NewsID _current_news = INVALID_NEWS; ///< points to news item that should be shown next
@@ -337,22 +336,22 @@
 /**
  * Per-NewsType data
  */
-const NewsTypeData _news_type_data[NT_END] = {
-	/* name,              age, sound           */
-	{ "arrival_player",    60, SND_1D_APPLAUSE },  ///< NT_ARRIVAL_PLAYER
-	{ "arrival_other",     60, SND_1D_APPLAUSE },  ///< NT_ARRIVAL_OTHER
-	{ "accident",          90, SND_BEGIN       },  ///< NT_ACCIDENT
-	{ "company_info",      60, SND_BEGIN       },  ///< NT_COMPANY_INFO
-	{ "openclose",         90, SND_BEGIN       },  ///< NT_OPENCLOSE
-	{ "economy",           30, SND_BEGIN       },  ///< NT_ECONOMY
-	{ "production_player", 30, SND_BEGIN       },  ///< NT_INDUSTRY_PLAYER
-	{ "production_other",  30, SND_BEGIN       },  ///< NT_INDUSTRY_OTHER
-	{ "production_nobody", 30, SND_BEGIN       },  ///< NT_INDUSTRY_NOBODY
-	{ "advice",           150, SND_BEGIN       },  ///< NT_ADVICE
-	{ "new_vehicles",      30, SND_1E_OOOOH    },  ///< NT_NEW_VEHICLES
-	{ "acceptance",        90, SND_BEGIN       },  ///< NT_ACCEPTANCE
-	{ "subsidies",        180, SND_BEGIN       },  ///< NT_SUBSIDIES
-	{ "general",           60, SND_BEGIN       },  ///< NT_GENERAL
+NewsTypeData _news_type_data[NT_END] = {
+	/* name,              age, sound,           display */
+	{ "arrival_player",    60, SND_1D_APPLAUSE, ND_FULL },  ///< NT_ARRIVAL_PLAYER
+	{ "arrival_other",     60, SND_1D_APPLAUSE, ND_FULL },  ///< NT_ARRIVAL_OTHER
+	{ "accident",          90, SND_BEGIN,       ND_FULL },  ///< NT_ACCIDENT
+	{ "company_info",      60, SND_BEGIN,       ND_FULL },  ///< NT_COMPANY_INFO
+	{ "openclose",         90, SND_BEGIN,       ND_FULL },  ///< NT_OPENCLOSE
+	{ "economy",           30, SND_BEGIN,       ND_FULL },  ///< NT_ECONOMY
+	{ "production_player", 30, SND_BEGIN,       ND_FULL },  ///< NT_INDUSTRY_PLAYER
+	{ "production_other",  30, SND_BEGIN,       ND_FULL },  ///< NT_INDUSTRY_OTHER
+	{ "production_nobody", 30, SND_BEGIN,       ND_FULL },  ///< NT_INDUSTRY_NOBODY
+	{ "advice",           150, SND_BEGIN,       ND_FULL },  ///< NT_ADVICE
+	{ "new_vehicles",      30, SND_1E_OOOOH,    ND_FULL },  ///< NT_NEW_VEHICLES
+	{ "acceptance",        90, SND_BEGIN,       ND_FULL },  ///< NT_ACCEPTANCE
+	{ "subsidies",        180, SND_BEGIN,       ND_FULL },  ///< NT_SUBSIDIES
+	{ "general",           60, SND_BEGIN,       ND_FULL },  ///< NT_GENERAL
 };
 
 
@@ -401,30 +400,6 @@
 };
 
 
-/**
- * Get the value of an item of the news-display settings. This is
- * a little tricky since on/off/summary must use 2 bits to store the value
- * @param item the item whose value is requested
- * @return return the found value which is between 0-2
- */
-static inline byte GetNewsDisplayValue(byte item)
-{
-	assert(item < NT_END && GB(_news_display_opt, item * 2, 2) <= 2);
-	return GB(_news_display_opt, item * 2, 2);
-}
-
-/**
- * Set the value of an item in the news-display settings. This is
- * a little tricky since on/off/summary must use 2 bits to store the value
- * @param item the item whose value is being set
- * @param val new value
- */
-static inline void SetNewsDisplayValue(byte item, byte val)
-{
-	assert(item < NT_END && val <= 2);
-	SB(_news_display_opt, item * 2, 2, val);
-}
-
 /** Open up an own newspaper window for the news item */
 static void ShowNewspaper(NewsItem *ni)
 {
@@ -521,9 +496,9 @@
 		/* check the date, don't show too old items */
 		if (_date - _news_type_data[ni->type].age > ni->date) return;
 
-		switch (GetNewsDisplayValue(ni->type)) {
+		switch (_news_type_data[ni->type].display) {
 			default: NOT_REACHED();
-			case 0: { // Off - show nothing only a small reminder in the status bar
+			case ND_OFF: { // Off - show nothing only a small reminder in the status bar
 				Window *w = FindWindowById(WC_STATUS_BAR, 0);
 
 				if (w != NULL) {
@@ -533,14 +508,14 @@
 				break;
 			}
 
-			case 1: // Summary - show ticker, but if forced big, cascade to full
+			case ND_SUMMARY: // Summary - show ticker, but if forced big, cascade to full
 				if (!(ni->flags & NF_FORCE_BIG)) {
 					ShowTicker(ni);
 					break;
 				}
 				/* Fallthrough */
 
-			case 2: // Full - show newspaper
+			case ND_FULL: // Full - show newspaper
 				ShowNewspaper(ni);
 				break;
 		}
@@ -769,33 +744,30 @@
 	/* WP(w, def_d).data_1 stores state of the ALL on/off/summary button */
 	switch (e->event) {
 		case WE_CREATE: {
-			uint32 val = _news_display_opt;
-			uint32 all_val;
+			NewsDisplay all_val;
 
 			/* Set up the initial disabled buttons in the case of 'off' or 'full' */
-			all_val = val & 0x3;
-			for (int i = 0; i < NT_END; i++, val >>= 2) {
-				SetMessageButtonStates(w, val & 0x3, i);
+			all_val = _news_type_data[0].display;
+			for (int i = 0; i < NT_END; i++) {
+				SetMessageButtonStates(w, _news_type_data[i].display, i);
 				/* If the value doesn't match the ALL-button value, set the ALL-button value to 'off' */
-				if ((val & 0x3) != all_val) all_val = 0;
+				if (_news_type_data[i].display != all_val) all_val = ND_OFF;
 			}
 			/* If all values are the same value, the ALL-button will take over this value */
 			WP(w, def_d).data_1 = all_val;
 		} break;
 
 		case WE_PAINT: {
-			uint32 val = _news_display_opt;
-
 			if (_news_ticker_sound) w->LowerWidget(WIDGET_NEWSOPT_SOUNDTICKER);
 
 			w->widget[WIDGET_NEWSOPT_DROP_SUMMARY].data = message_opt[WP(w, def_d).data_1];
 			DrawWindowWidgets(w);
 
 			/* Draw the string of each setting on each button. */
-			for (int i = 0, y = 26; i < NT_END; i++, y += 12, val >>= 2) {
+			for (int i = 0, y = 26; i < NT_END; i++, y += 12) {
 				/* 51 comes from 13 + 89 (left and right of the button)+1, shiefted by one as to get division,
 				 * which will give centered position */
-				DrawStringCentered(51, y + 1, message_opt[val & 0x3], TC_BLACK);
+				DrawStringCentered(51, y + 1, message_opt[_news_type_data[i].display], TC_BLACK);
 			}
 		} break;
 
@@ -815,10 +787,10 @@
 					int wid = e->we.click.widget - WIDGET_NEWSOPT_START_OPTION;
 					if (wid >= 0 && wid < (NB_WIDG_PER_SETTING * NT_END)) {
 						int element = wid / NB_WIDG_PER_SETTING;
-						byte val = (GetNewsDisplayValue(element) + ((wid % NB_WIDG_PER_SETTING) ? 1 : -1)) % 3;
+						byte val = (_news_type_data[element].display + ((wid % NB_WIDG_PER_SETTING) ? 1 : -1)) % 3;
 
 						SetMessageButtonStates(w, val, element);
-						SetNewsDisplayValue(element, val);
+						_news_type_data[element].display = (NewsDisplay)val;
 						SetWindowDirty(w);
 					}
 				} break;
@@ -830,7 +802,7 @@
 
 			for (int i = 0; i < NT_END; i++) {
 				SetMessageButtonStates(w, e->we.dropdown.index, i);
-				SetNewsDisplayValue(i, e->we.dropdown.index);
+				_news_type_data[i].display = (NewsDisplay)e->we.dropdown.index;
 			}
 			SetWindowDirty(w);
 			break;
--- a/src/news_type.h
+++ b/src/news_type.h
@@ -77,12 +77,22 @@
 };
 
 /**
+ * News display options
+ */
+enum NewsDisplay {
+	ND_OFF,        ///< Only show a reminder in the status bar
+	ND_SUMMARY,    ///< Show ticker
+	ND_FULL,       ///< Show newspaper
+};
+
+/**
  * Per-NewsType data
  */
 struct NewsTypeData {
 	const char *const name; ///< Name
 	const byte age;         ///< Maximum age of news items (in days)
 	const SoundFx sound;    ///< Sound
+	NewsDisplay display;    ///< Display mode (off, summary, full)
 };
 
 struct NewsItem {
--- a/src/settings.cpp
+++ b/src/settings.cpp
@@ -1654,15 +1654,13 @@
 #undef NO
 #undef CR
 
-static uint NewsDisplayLoadConfig(IniFile *ini, const char *grpname)
+static void NewsDisplayLoadConfig(IniFile *ini, const char *grpname)
 {
 	IniGroup *group = ini_getgroup(ini, grpname, -1);
 	IniItem *item;
-	/* By default, set everything to full (0xAAAAAAAA = 1010101010101010) */
-	uint res = 0xAAAAAAAA;
 
-	/* If no group exists, return everything full */
-	if (group == NULL) return res;
+	/* If no group exists, return */
+	if (group == NULL) return;
 
 	for (item = group->item; item != NULL; item = item->next) {
 		int news_item = -1;
@@ -1678,18 +1676,16 @@
 		}
 
 		if (strcasecmp(item->value, "full") == 0) {
-			SB(res, news_item * 2, 2, 2);
+			_news_type_data[news_item].display = ND_FULL;
 		} else if (strcasecmp(item->value, "off") == 0) {
-			SB(res, news_item * 2, 2, 0);
+			_news_type_data[news_item].display = ND_OFF;
 		} else if (strcasecmp(item->value, "summarized") == 0) {
-			SB(res, news_item * 2, 2, 1);
+			_news_type_data[news_item].display = ND_SUMMARY;
 		} else {
 			DEBUG(misc, 0, "Invalid display value: %s", item->value);
 			continue;
 		}
 	}
-
-	return res;
 }
 
 /* Load a GRF configuration from the given group name */
@@ -1745,7 +1741,7 @@
 	return first;
 }
 
-static void NewsDisplaySaveConfig(IniFile *ini, const char *grpname, uint news_display)
+static void NewsDisplaySaveConfig(IniFile *ini, const char *grpname)
 {
 	IniGroup *group = ini_getgroup(ini, grpname, -1);
 	IniItem **item;
@@ -1756,9 +1752,9 @@
 
 	for (int i = 0; i < NT_END; i++) {
 		const char *value;
-		int v = GB(news_display, i * 2, 2);
+		int v = _news_type_data[i].display;
 
-		value = (v == 0 ? "off" : (v == 1 ? "summarized" : "full"));
+		value = (v == ND_OFF ? "off" : (v == ND_SUMMARY ? "summarized" : "full"));
 
 		*item = ini_item_alloc(group, _news_type_data[i].name, strlen(_news_type_data[i].name));
 		(*item)->value = (char*)pool_strdup(&ini->pool, value, strlen(value));
@@ -1847,7 +1843,7 @@
 	HandleSettingDescs(ini, ini_load_settings, ini_load_setting_list);
 	_grfconfig_newgame = GRFLoadConfig(ini, "newgrf", false);
 	_grfconfig_static  = GRFLoadConfig(ini, "newgrf-static", true);
-	_news_display_opt  = NewsDisplayLoadConfig(ini, "news_display");
+	NewsDisplayLoadConfig(ini, "news_display");
 	CheckDifficultyLevels();
 	ini_free(ini);
 }
@@ -1859,7 +1855,7 @@
 	HandleSettingDescs(ini, ini_save_settings, ini_save_setting_list);
 	GRFSaveConfig(ini, "newgrf", _grfconfig_newgame);
 	GRFSaveConfig(ini, "newgrf-static", _grfconfig_static);
-	NewsDisplaySaveConfig(ini, "news_display", _news_display_opt);
+	NewsDisplaySaveConfig(ini, "news_display");
 	SaveVersionInConfig(ini);
 	ini_save(_config_file, ini);
 	ini_free(ini);