diff options
author | dominik <dominik@openttd.org> | 2004-08-19 13:39:50 +0000 |
---|---|---|
committer | dominik <dominik@openttd.org> | 2004-08-19 13:39:50 +0000 |
commit | 80b1e25b6ce190a773ab9fe50927a983c8f2d038 (patch) | |
tree | f07bda475c86d63d3a49a6a5e8fd1f673cc24e2a | |
parent | daffe479bbe8e26611b465b07ae85aa0a287f618 (diff) | |
download | openttd-80b1e25b6ce190a773ab9fe50927a983c8f2d038.tar.xz |
(svn r84) Improved news system code a little to avoid potential memory leaks
-rw-r--r-- | news_gui.c | 34 |
1 files changed, 19 insertions, 15 deletions
diff --git a/news_gui.c b/news_gui.c index 54b341360..dd51fc6e9 100644 --- a/news_gui.c +++ b/news_gui.c @@ -62,7 +62,7 @@ GetNewsStringCallbackProc * const _get_news_string_callback[] = { void InitNewsItemStructs() { - memset(_news_items, 0, sizeof(_news_items)); + memset(_news_items, 0, sizeof(NewsItem)*MAX_NEWS); } void DrawNewsBorder(Window *w) @@ -176,15 +176,13 @@ static void NewsWindowProc(Window *w, WindowEvent *e) // returns the correct index in the array // (to deal with overflows) -byte getIndex(byte i) +byte increaseIndex(byte i) { - if(i==255) { - if(_oldest_news <= _latest_news) - return _latest_news; - else - return MAX_NEWS; - } - if(i >= MAX_NEWS) i %= MAX_NEWS; + if(i==255) + return 0; + i++; + if(i >= MAX_NEWS) + i = i % MAX_NEWS; return i; } @@ -199,13 +197,13 @@ void AddNewsItem(StringID string, uint32 flags, uint data_a, uint data_b) _forced_news = 255; if(_total_news < MAX_NEWS) _total_news++; - + // make sure our pointer isn't overflowing - _latest_news = getIndex(++_latest_news); + _latest_news = increaseIndex(_latest_news); // overwrite oldest news entry if( _oldest_news == _latest_news && _news_items[_oldest_news].string_id != 0) - _oldest_news = getIndex(++_oldest_news); // but make sure we're not overflowing here + _oldest_news = increaseIndex(_oldest_news); // but make sure we're not overflowing here // add news to _latest_news ni = &_news_items[_latest_news]; @@ -365,7 +363,7 @@ static void MoveToNexItem() { NewsItem *ni; - _current_news = getIndex(++_current_news); + _current_news = increaseIndex(_current_news); ni = &_news_items[_current_news]; // check the date, don't show too old items @@ -413,8 +411,14 @@ void ShowLastNewsMessage() { if(_forced_news==255) ShowNewsMessage(_current_news); - else - ShowNewsMessage( getIndex(_forced_news-1) ); + else if(_forced_news!=0) + ShowNewsMessage(_forced_news-1); + else { + if(_total_news != MAX_NEWS) + ShowNewsMessage(_latest_news); + else + ShowNewsMessage(MAX_NEWS-1); + } } |