1
0
Fork 0

(svn r7369) -Cleanup: Some coding style, usage of increaseIndex and decreaseIndex to loop through the

fifo-array-buffer, typo's.
release/0.5
Darkvater 2006-12-05 11:59:28 +00:00
parent 8904c04192
commit a46daf3fb1
1 changed files with 51 additions and 41 deletions

View File

@ -219,14 +219,20 @@ static void NewsWindowProc(Window *w, WindowEvent *e)
} }
} }
// returns the correct index in the array /** Return the correct index in the pseudo-fifo
// (to deal with overflows) * queue and deals with overflows when increasing the index */
static byte increaseIndex(byte i) static inline byte increaseIndex(byte i)
{ {
if (i == INVALID_NEWS) return 0; if (i == INVALID_NEWS) return 0;
i++; return (i + 1) % MAX_NEWS;
if (i >= MAX_NEWS) i = i % MAX_NEWS; }
return i;
/** 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. /** Add a new newsitem to be shown.
@ -251,14 +257,12 @@ static byte increaseIndex(byte i)
* @see NewsCallback */ * @see NewsCallback */
void AddNewsItem(StringID string, uint32 flags, uint data_a, uint data_b) void AddNewsItem(StringID string, uint32 flags, uint data_a, uint data_b)
{ {
NewsItem *ni;
Window *w;
byte l_news; byte l_news;
if (_game_mode == GM_MENU) return; if (_game_mode == GM_MENU) return;
// check the rare case that the oldest (to be overwritten) news item is open // 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(); MoveToNexItem();
_forced_news = INVALID_NEWS; _forced_news = INVALID_NEWS;
@ -269,32 +273,35 @@ void AddNewsItem(StringID string, uint32 flags, uint data_a, uint data_b)
_latest_news = increaseIndex(_latest_news); _latest_news = increaseIndex(_latest_news);
/* If the fifo-buffer is full, overwrite the oldest entry */ /* If the fifo-buffer is full, overwrite the oldest entry */
if (l_news != INVALID_NEWS && _latest_news == _oldest_news) if (l_news != INVALID_NEWS && _latest_news == _oldest_news) {
_oldest_news = increaseIndex(_oldest_news); // but make sure we're not overflowing here assert(_total_news == MAX_NEWS);
_oldest_news = increaseIndex(_oldest_news);
}
// add news to _latest_news { /* Add news to _latest_news */
ni = &_news_items[_latest_news]; Window *w;
memset(ni, 0, sizeof(*ni)); NewsItem *ni = &_news_items[_latest_news];
memset(ni, 0, sizeof(*ni));
ni->string_id = string; ni->string_id = string;
ni->display_mode = (byte)flags; ni->display_mode = (byte)flags;
ni->flags = (byte)(flags >> 8); ni->flags = (byte)(flags >> 8);
// show this news message in color? // show this news message in color?
if (_cur_year >= _patches.colored_news_year) if (_cur_year >= _patches.colored_news_year) ni->flags |= NF_INCOLOR;
ni->flags |= NF_INCOLOR;
ni->type = (byte)(flags >> 16); ni->type = (byte)(flags >> 16);
ni->callback = (byte)(flags >> 24); ni->callback = (byte)(flags >> 24);
ni->data_a = data_a; ni->data_a = data_a;
ni->data_b = data_b; ni->data_b = data_b;
ni->date = _date; ni->date = _date;
COPY_OUT_DPARAM(ni->params, 0, lengthof(ni->params)); COPY_OUT_DPARAM(ni->params, 0, lengthof(ni->params));
w = FindWindowById(WC_MESSAGE_HISTORY, 0); w = FindWindowById(WC_MESSAGE_HISTORY, 0);
if (w == NULL) return; if (w == NULL) return;
SetWindowDirty(w); SetWindowDirty(w);
w->vscroll.count = _total_news; w->vscroll.count = _total_news;
}
} }
@ -471,7 +478,7 @@ static void MoveToNexItem(void)
DeleteWindowById(WC_NEWS_WINDOW, 0); DeleteWindowById(WC_NEWS_WINDOW, 0);
_forced_news = INVALID_NEWS; _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) { if (_current_news != _latest_news) {
NewsItem *ni; NewsItem *ni;
@ -536,12 +543,16 @@ static void ShowNewsMessage(byte i)
void ShowLastNewsMessage(void) void ShowLastNewsMessage(void)
{ {
if (_forced_news == INVALID_NEWS) { switch (_forced_news) {
ShowNewsMessage(_current_news); case INVALID_NEWS: // Not forced any news yet, show the current one
} else if (_forced_news != 0) { ShowNewsMessage(_current_news);
ShowNewsMessage(_forced_news - 1); break;
} else { case 0: //
ShowNewsMessage(_total_news != MAX_NEWS ? _latest_news : MAX_NEWS - 1); 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 @@ void DeleteVehicleNews(VehicleID vid, StringID news)
{ {
byte n; byte n;
for (n = _oldest_news; _latest_news != INVALID_NEWS && n != (_latest_news + 1) % MAX_NEWS; n = (n + 1) % MAX_NEWS) { for (n = _oldest_news; _latest_news != INVALID_NEWS && n != increaseIndex(_latest_news); n = increaseIndex(n)) {
const NewsItem* ni = &_news_items[n]; const NewsItem *ni = &_news_items[n];
if (ni->flags & NF_VEHICLE && if (ni->flags & NF_VEHICLE &&
ni->data_a == vid && ni->data_a == vid &&
@ -885,8 +896,7 @@ void DeleteVehicleNews(VehicleID vid, StringID news)
Window *w; Window *w;
byte i; byte i;
if (_forced_news == n) MoveToNexItem(); if (_forced_news == n || _current_news == n) MoveToNexItem();
if (_current_news == n) MoveToNexItem();
// If this is the last news item, invalidate _latest_news // If this is the last news item, invalidate _latest_news
if (_latest_news == _oldest_news) _latest_news = INVALID_NEWS; if (_latest_news == _oldest_news) _latest_news = INVALID_NEWS;