Codechange: use a contructor for NewsItem to set the values

And use std::unique_ptr to manage the memory of the allocated data
pull/332/head
Rubidium 3 years ago committed by rubidium42
parent 9a7750f14e
commit 9c7a7b53a1

@ -355,7 +355,7 @@ struct NewsWindow : Window {
break;
case WID_N_MGR_NAME:
SetDParamStr(0, static_cast<const CompanyNewsInformation *>(this->ni->data)->president_name);
SetDParamStr(0, static_cast<const CompanyNewsInformation *>(this->ni->data.get())->president_name);
str = STR_JUST_RAW_STRING;
break;
@ -433,13 +433,13 @@ struct NewsWindow : Window {
break;
case WID_N_MGR_FACE: {
const CompanyNewsInformation *cni = static_cast<const CompanyNewsInformation*>(this->ni->data);
const CompanyNewsInformation *cni = static_cast<const CompanyNewsInformation*>(this->ni->data.get());
DrawCompanyManagerFace(cni->face, cni->colour, r.left, r.top);
GfxFillRect(r.left, r.top, r.right, r.bottom, PALETTE_NEWSPAPER, FILLRECT_RECOLOUR);
break;
}
case WID_N_MGR_NAME: {
const CompanyNewsInformation *cni = static_cast<const CompanyNewsInformation*>(this->ni->data);
const CompanyNewsInformation *cni = static_cast<const CompanyNewsInformation*>(this->ni->data.get());
SetDParamStr(0, cni->president_name);
DrawStringMultiLine(r.left, r.right, r.top, r.bottom, STR_JUST_RAW_STRING, TC_FROMSTRING, SA_CENTER);
break;
@ -770,6 +770,27 @@ static void DeleteNewsItem(NewsItem *ni)
SetWindowDirty(WC_MESSAGE_HISTORY, 0);
}
/**
* Create a new newsitem to be shown.
* @param string_id String to display.
* @param type The type of news.
* @param flags Flags related to how to display the news.
* @param reftype1 Type of ref1.
* @param ref1 Reference 1 to some object: Used for a possible viewport, scrolling after clicking on the news, and for deleting the news when the object is deleted.
* @param reftype2 Type of ref2.
* @param ref2 Reference 2 to some object: Used for scrolling after clicking on the news, and for deleting the news when the object is deleted.
* @param data Pointer to data that must be released once the news message is cleared.
*
* @see NewsSubtype
*/
NewsItem::NewsItem(StringID string_id, NewsType type, NewsFlag flags, NewsReferenceType reftype1, uint32 ref1, NewsReferenceType reftype2, uint32 ref2, const NewsAllocatedData *data) :
string_id(string_id), date(_date), type(type), flags(flags), reftype1(reftype1), reftype2(reftype2), ref1(ref1), ref2(ref2), data(data)
{
/* show this news message in colour? */
if (_cur_year >= _settings_client.gui.coloured_news_year) this->flags |= NF_INCOLOUR;
CopyOutDParam(this->params, 0, lengthof(this->params));
}
/**
* Add a new newsitem to be shown.
* @param string String to display
@ -779,7 +800,7 @@ static void DeleteNewsItem(NewsItem *ni)
* @param ref1 Reference 1 to some object: Used for a possible viewport, scrolling after clicking on the news, and for deleting the news when the object is deleted.
* @param reftype2 Type of ref2
* @param ref2 Reference 2 to some object: Used for scrolling after clicking on the news, and for deleting the news when the object is deleted.
* @param free_data Pointer to data that must be freed once the news message is cleared
* @param data Pointer to data that must be released once the news message is cleared.
*
* @see NewsSubtype
*/
@ -788,22 +809,7 @@ void AddNewsItem(StringID string, NewsType type, NewsFlag flags, NewsReferenceTy
if (_game_mode == GM_MENU) return;
/* Create new news item node */
NewsItem *ni = new NewsItem;
ni->string_id = string;
ni->type = type;
ni->flags = flags;
/* show this news message in colour? */
if (_cur_year >= _settings_client.gui.coloured_news_year) ni->flags |= NF_INCOLOUR;
ni->reftype1 = reftype1;
ni->reftype2 = reftype2;
ni->ref1 = ref1;
ni->ref2 = ref2;
ni->data = data;
ni->date = _date;
CopyOutDParam(ni->params, 0, lengthof(ni->params));
NewsItem *ni = new NewsItem(string, type, flags, reftype1, ref1, reftype2, ref2, data);
if (_total_news++ == 0) {
assert(_oldest_news == nullptr);

@ -135,14 +135,11 @@ struct NewsItem {
uint32 ref1; ///< Reference 1 to some object: Used for a possible viewport, scrolling after clicking on the news, and for deleting the news when the object is deleted.
uint32 ref2; ///< Reference 2 to some object: Used for scrolling after clicking on the news, and for deleting the news when the object is deleted.
const NewsAllocatedData *data; ///< Custom data for the news item that have to be deallocated (deleted) when the news item has reached its end.
~NewsItem()
{
delete this->data;
}
std::unique_ptr<const NewsAllocatedData> data; ///< Custom data for the news item that will be deallocated (deleted) when the news item has reached its end.
uint64 params[10]; ///< Parameters for string resolving.
NewsItem(StringID string_id, NewsType type, NewsFlag flags, NewsReferenceType reftype1, uint32 ref1, NewsReferenceType reftype2, uint32 ref2, const NewsAllocatedData *data);
};
/** Container for a single string to be passed as NewsAllocatedData. */

Loading…
Cancel
Save