(svn r7369) -Cleanup: Some coding style, usage of increaseIndex and decreaseIndex to loop through the
authorDarkvater
Tue, 05 Dec 2006 11:59:28 +0000
changeset 5245 5f1a514e8bfe
parent 5244 e110f52333e8
child 5246 29224c621fd6
(svn r7369) -Cleanup: Some coding style, usage of increaseIndex and decreaseIndex to loop through the
fifo-array-buffer, typo's.
news_gui.c
--- a/news_gui.c	Tue Dec 05 11:51:23 2006 +0000
+++ b/news_gui.c	Tue Dec 05 11:59:28 2006 +0000
@@ -219,14 +219,20 @@
 	}
 }
 
-// returns the correct index in the array
-// (to deal with overflows)
-static byte increaseIndex(byte i)
+/** Return the correct index in the pseudo-fifo
+ * queue and deals with overflows when increasing the index */
+static inline byte increaseIndex(byte i)
 {
 	if (i == INVALID_NEWS) return 0;
-	i++;
-	if (i >= MAX_NEWS) i = i % MAX_NEWS;
-	return i;
+	return (i + 1) % MAX_NEWS;
+}
+
+/** Return the correct index in the pseudo-fifo
+ * queue and deals with overflows when decreasing the index */
+static inline byte decreaseIndex(byte i)
+{
+	assert(i != INVALID_NEWS);
+	return (i + MAX_NEWS - 1) % MAX_NEWS;
 }
 
 /** Add a new newsitem to be shown.
@@ -251,14 +257,12 @@
  * @see NewsCallback */
 void AddNewsItem(StringID string, uint32 flags, uint data_a, uint data_b)
 {
-	NewsItem *ni;
-	Window *w;
 	byte l_news;
 
 	if (_game_mode == GM_MENU) return;
 
 	// check the rare case that the oldest (to be overwritten) news item is open
-	if (_total_news==MAX_NEWS && (_oldest_news == _current_news || _oldest_news == _forced_news))
+	if (_total_news == MAX_NEWS && (_oldest_news == _current_news || _oldest_news == _forced_news))
 		MoveToNexItem();
 
 	_forced_news = INVALID_NEWS;
@@ -269,32 +273,35 @@
 	_latest_news = increaseIndex(_latest_news);
 
 	/* If the fifo-buffer is full, overwrite the oldest entry */
-	if (l_news != INVALID_NEWS && _latest_news == _oldest_news)
-		_oldest_news = increaseIndex(_oldest_news); // but make sure we're not overflowing here
-
-	// add news to _latest_news
-	ni = &_news_items[_latest_news];
-	memset(ni, 0, sizeof(*ni));
-
-	ni->string_id = string;
-	ni->display_mode = (byte)flags;
-	ni->flags = (byte)(flags >> 8);
+	if (l_news != INVALID_NEWS && _latest_news == _oldest_news) {
+		assert(_total_news == MAX_NEWS);
+		_oldest_news = increaseIndex(_oldest_news);
+	}
 
-	// show this news message in color?
-	if (_cur_year >= _patches.colored_news_year)
-		ni->flags |= NF_INCOLOR;
+	{ /* Add news to _latest_news */
+		Window *w;
+		NewsItem *ni = &_news_items[_latest_news];
+		memset(ni, 0, sizeof(*ni));
 
-	ni->type = (byte)(flags >> 16);
-	ni->callback = (byte)(flags >> 24);
-	ni->data_a = data_a;
-	ni->data_b = data_b;
-	ni->date = _date;
-	COPY_OUT_DPARAM(ni->params, 0, lengthof(ni->params));
+		ni->string_id = string;
+		ni->display_mode = (byte)flags;
+		ni->flags = (byte)(flags >> 8);
 
-	w = FindWindowById(WC_MESSAGE_HISTORY, 0);
-	if (w == NULL) return;
-	SetWindowDirty(w);
-	w->vscroll.count = _total_news;
+		// show this news message in color?
+		if (_cur_year >= _patches.colored_news_year) ni->flags |= NF_INCOLOR;
+
+		ni->type = (byte)(flags >> 16);
+		ni->callback = (byte)(flags >> 24);
+		ni->data_a = data_a;
+		ni->data_b = data_b;
+		ni->date = _date;
+		COPY_OUT_DPARAM(ni->params, 0, lengthof(ni->params));
+
+		w = FindWindowById(WC_MESSAGE_HISTORY, 0);
+		if (w == NULL) return;
+		SetWindowDirty(w);
+		w->vscroll.count = _total_news;
+	}
 }
 
 
@@ -471,7 +478,7 @@
 	DeleteWindowById(WC_NEWS_WINDOW, 0);
 	_forced_news = INVALID_NEWS;
 
-	// if we're not at the last item, than move on
+	// if we're not at the last item, then move on
 	if (_current_news != _latest_news) {
 		NewsItem *ni;
 
@@ -536,12 +543,16 @@
 
 void ShowLastNewsMessage(void)
 {
-	if (_forced_news == INVALID_NEWS) {
-		ShowNewsMessage(_current_news);
-	} else if (_forced_news != 0) {
-		ShowNewsMessage(_forced_news - 1);
-	} else {
-		ShowNewsMessage(_total_news != MAX_NEWS ? _latest_news : MAX_NEWS - 1);
+	switch (_forced_news) {
+		case INVALID_NEWS: // Not forced any news yet, show the current one
+			ShowNewsMessage(_current_news);
+			break;
+		case 0: //
+			ShowNewsMessage(_total_news != MAX_NEWS ? _latest_news : MAX_NEWS - 1);
+			break;
+		default: // 'Scrolling' through news history show each one in turn
+			ShowNewsMessage(_forced_news - 1);
+			break;
 	}
 }
 
@@ -876,8 +887,8 @@
 {
 	byte n;
 
-	for (n = _oldest_news; _latest_news != INVALID_NEWS && n != (_latest_news + 1) % MAX_NEWS; n = (n + 1) % MAX_NEWS) {
-		const NewsItem* ni = &_news_items[n];
+	for (n = _oldest_news; _latest_news != INVALID_NEWS && n != increaseIndex(_latest_news); n = increaseIndex(n)) {
+		const NewsItem *ni = &_news_items[n];
 
 		if (ni->flags & NF_VEHICLE &&
 				ni->data_a == vid &&
@@ -885,8 +896,7 @@
 			Window *w;
 			byte i;
 
-			if (_forced_news  == n) MoveToNexItem();
-			if (_current_news == n) MoveToNexItem();
+			if (_forced_news  == n || _current_news == n) MoveToNexItem();
 
 			// If this is the last news item, invalidate _latest_news
 			if (_latest_news == _oldest_news) _latest_news = INVALID_NEWS;