Skip to content

Commit

Permalink
Change: Split up Window::OnTick into OnGameTick and OnRealtimeTick. A…
Browse files Browse the repository at this point in the history
…djust timers to work with milliseconds instead of ticks.
  • Loading branch information
PeterN committed Jan 11, 2019
1 parent 916e911 commit 2a8fa5f
Show file tree
Hide file tree
Showing 21 changed files with 144 additions and 64 deletions.
2 changes: 1 addition & 1 deletion src/airport_gui.cpp
Expand Up @@ -543,7 +543,7 @@ class BuildAirportWindow : public PickerWindowBase {
this->SelectFirstAvailableAirport(false);
}

virtual void OnTick()
virtual void OnRealtimeTick(uint delta_ms)
{
CheckRedrawStationCoverage(this);
}
Expand Down
2 changes: 1 addition & 1 deletion src/dock_gui.cpp
Expand Up @@ -451,7 +451,7 @@ struct BuildDocksStationWindow : public PickerWindowBase {
}
}

virtual void OnTick()
virtual void OnRealtimeTick(uint delta_ms)
{
CheckRedrawStationCoverage(this);
}
Expand Down
10 changes: 5 additions & 5 deletions src/graph_gui.cpp
Expand Up @@ -539,7 +539,7 @@ struct BaseGraphWindow : Window {
if (widget == WID_CV_KEY_BUTTON) ShowGraphLegend();
}

virtual void OnTick()
virtual void OnGameTick()
{
this->UpdateStatistics(false);
}
Expand Down Expand Up @@ -998,9 +998,9 @@ struct PaymentRatesGraphWindow : BaseGraphWindow {
}
}

virtual void OnTick()
virtual void OnGameTick()
{
/* Override default OnTick */
/* Override default OnGameTick */
}

/**
Expand Down Expand Up @@ -1239,7 +1239,7 @@ class CompanyLeagueWindow : public Window {
}


virtual void OnTick()
virtual void OnGameTick()
{
if (this->companies.NeedResort()) {
this->SetDirty();
Expand Down Expand Up @@ -1472,7 +1472,7 @@ struct PerformanceRatingDetailWindow : Window {
}
}

virtual void OnTick()
virtual void OnGameTick()
{
if (_pause_mode != PM_UNPAUSED) return;

Expand Down
2 changes: 1 addition & 1 deletion src/group_gui.cpp
Expand Up @@ -858,7 +858,7 @@ class VehicleGroupWindow : public BaseVehicleListWindow {
this->SetDirty();
}

virtual void OnTick()
virtual void OnGameTick()
{
if (_pause_mode != PM_UNPAUSED) return;
if (this->groups.NeedResort() || this->vehicles.NeedResort()) {
Expand Down
2 changes: 1 addition & 1 deletion src/industry_gui.cpp
Expand Up @@ -647,7 +647,7 @@ class BuildIndustryWindow : public Window {
if (success && !_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
}

virtual void OnTick()
virtual void OnGameTick()
{
if (_pause_mode != PM_UNPAUSED) return;
if (!this->timer_enabled) return;
Expand Down
11 changes: 6 additions & 5 deletions src/main_gui.cpp
Expand Up @@ -235,10 +235,11 @@ enum {

struct MainWindow : Window
{
uint refresh;
int refresh;

static const uint LINKGRAPH_REFRESH_PERIOD = 0xff;
static const uint LINKGRAPH_DELAY = 0xf;
/* Refresh times in milliseconds */
static const uint LINKGRAPH_REFRESH_PERIOD = 7650;
static const uint LINKGRAPH_DELAY = 450;

MainWindow(WindowDesc *desc) : Window(desc)
{
Expand All @@ -253,9 +254,9 @@ struct MainWindow : Window
this->refresh = LINKGRAPH_DELAY;
}

virtual void OnTick()
virtual void OnRealtimeTick(uint delta_ms)
{
if (--this->refresh > 0) return;
if (!TimerElapsed(this->refresh, delta_ms)) return;

this->refresh = LINKGRAPH_REFRESH_PERIOD;

Expand Down
13 changes: 7 additions & 6 deletions src/misc_gui.cpp
Expand Up @@ -454,15 +454,16 @@ static const char * const _credits[] = {

struct AboutWindow : public Window {
int text_position; ///< The top of the scrolling text
byte counter; ///< Used to scroll the text every 5 ticks
int line_height; ///< The height of a single line
static const int num_visible_lines = 19; ///< The number of lines visible simultaneously

static const uint TIMER_INTERVAL = 150; ///< Scrolling interval in ms
uint timer;

AboutWindow() : Window(&_about_desc)
{
this->InitNested(WN_GAME_OPTIONS_ABOUT);

this->counter = 5;
this->text_position = this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->pos_y + this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->current_y;
}

Expand Down Expand Up @@ -502,11 +503,11 @@ struct AboutWindow : public Window {
}
}

virtual void OnTick()
virtual void OnRealtimeTick(uint delta_ms)
{
if (--this->counter == 0) {
this->counter = 5;
this->text_position--;
uint count = CountIntervalElapsed(this->timer, delta_ms, TIMER_INTERVAL);
if (count > 0) {
this->text_position -= count;
/* If the last text has scrolled start a new from the start */
if (this->text_position < (int)(this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->pos_y - lengthof(_credits) * this->line_height)) {
this->text_position = this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->pos_y + this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->current_y;
Expand Down
6 changes: 3 additions & 3 deletions src/newgrf_gui.cpp
Expand Up @@ -403,7 +403,7 @@ struct NewGRFParametersWindow : public Window {
par_info->SetValue(this->grf_config, val);

this->clicked_button = num;
this->timeout = 5;
this->timeout = 150;
}
} else if (par_info->type == PTYPE_UINT_ENUM && !par_info->complete_labels && click_count >= 2) {
/* Display a query box so users can enter a custom value. */
Expand Down Expand Up @@ -483,9 +483,9 @@ struct NewGRFParametersWindow : public Window {
}
}

virtual void OnTick()
virtual void OnRealtimeTick(uint delta_ms)
{
if (--this->timeout == 0) {
if (TimerElapsed(this->timeout, delta_ms)) {
this->clicked_button = UINT_MAX;
this->SetDirty();
}
Expand Down
28 changes: 17 additions & 11 deletions src/news_gui.cpp
Expand Up @@ -260,11 +260,13 @@ struct NewsWindow : Window {
uint16 chat_height; ///< Height of the chat window.
uint16 status_height; ///< Height of the status bar window
const NewsItem *ni; ///< News item to display.
static uint duration; ///< Remaining time for showing current news message (may only be accessed while a news item is displayed).
static int duration; ///< Remaining time for showing the current news message (may only be access while a news item is displayed).

uint timer;

NewsWindow(WindowDesc *desc, const NewsItem *ni) : Window(desc), ni(ni)
{
NewsWindow::duration = 555;
NewsWindow::duration = 16650;
const Window *w = FindWindowByClass(WC_SEND_NETWORK_MSG);
this->chat_height = (w != NULL) ? w->height : 0;
this->status_height = FindWindowById(WC_STATUS_BAR, 0)->height;
Expand Down Expand Up @@ -485,11 +487,18 @@ struct NewsWindow : Window {
this->SetWindowTop(newtop);
}

virtual void OnTick()
virtual void OnRealtimeTick(uint delta_ms)
{
/* Scroll up newsmessages from the bottom in steps of 4 pixels */
int newtop = max(this->top - 4, _screen.height - this->height - this->status_height - this->chat_height);
this->SetWindowTop(newtop);
int count = CountIntervalElapsed(this->timer, delta_ms, 15);
if (count > 0) {
/* Scroll up newsmessages from the bottom */
int newtop = max(this->top - 2 * count, _screen.height - this->height - this->status_height - this->chat_height);
this->SetWindowTop(newtop);
}

/* Decrement the news timer. We don't need to action an elapsed event here,
* so no need to use TimerElapsed(). */
if (NewsWindow::duration > 0) NewsWindow::duration -= delta_ms;
}

private:
Expand Down Expand Up @@ -536,7 +545,7 @@ struct NewsWindow : Window {
}
};

/* static */ uint NewsWindow::duration = 0; // Instance creation.
/* static */ int NewsWindow::duration = 0; // Instance creation.


/** Open up an own newspaper window for the news item */
Expand Down Expand Up @@ -588,11 +597,8 @@ static bool ReadyForNextItem()
* Check if the status bar message is still being displayed? */
if (IsNewsTickerShown()) return false;

/* Newspaper message, decrement duration counter */
if (NewsWindow::duration != 0) NewsWindow::duration--;

/* neither newsticker nor newspaper are running */
return (NewsWindow::duration == 0 || FindWindowById(WC_NEWS_WINDOW, 0) == NULL);
return (NewsWindow::duration <= 0 || FindWindowById(WC_NEWS_WINDOW, 0) == NULL);
}

/** Move to the next news item */
Expand Down
7 changes: 3 additions & 4 deletions src/openttd.cpp
Expand Up @@ -76,7 +76,7 @@ void IncreaseDate();
void DoPaletteAnimations();
void MusicLoop();
void ResetMusic();
void CallWindowTickEvent();
void CallWindowGameTickEvent();
bool HandleBootstrap();

extern Company *DoStartupNewCompany(bool is_ai, CompanyID company = INVALID_COMPANY);
Expand Down Expand Up @@ -1363,7 +1363,6 @@ void StateGameLoop()
#ifndef DEBUG_DUMP_COMMANDS
Game::GameLoop();
#endif
CallWindowTickEvent();
return;
}

Expand All @@ -1381,7 +1380,7 @@ void StateGameLoop()
BasePersistentStorageArray::SwitchMode(PSM_LEAVE_GAMELOOP);
UpdateLandscapingLimits();

CallWindowTickEvent();
CallWindowGameTickEvent();
NewsLoop();
} else {
if (_debug_desync_level > 2 && _date_fract == 0 && (_date & 0x1F) == 0) {
Expand Down Expand Up @@ -1411,7 +1410,7 @@ void StateGameLoop()
#endif
UpdateLandscapingLimits();

CallWindowTickEvent();
CallWindowGameTickEvent();
NewsLoop();
cur_company.Restore();
}
Expand Down
2 changes: 1 addition & 1 deletion src/rail_gui.cpp
Expand Up @@ -1368,7 +1368,7 @@ struct BuildRailStationWindow : public PickerWindowBase {
}
}

virtual void OnTick()
virtual void OnRealtimeTick(uint delta_ms)
{
CheckRedrawStationCoverage(this);
}
Expand Down
2 changes: 1 addition & 1 deletion src/road_gui.cpp
Expand Up @@ -1053,7 +1053,7 @@ struct BuildRoadStationWindow : public PickerWindowBase {
}
}

virtual void OnTick()
virtual void OnRealtimeTick(uint delta_ms)
{
CheckRedrawStationCoverage(this);
}
Expand Down
4 changes: 2 additions & 2 deletions src/smallmap_gui.cpp
Expand Up @@ -1570,10 +1570,10 @@ int SmallMapWindow::GetPositionOnLegend(Point pt)
}
}

/* virtual */ void SmallMapWindow::OnTick()
/* virtual */ void SmallMapWindow::OnRealtimeTick(uint delta_ms)
{
/* Update the window every now and then */
if (--this->refresh != 0) return;
if (!TimerElapsed(this->refresh, delta_ms)) return;

if (this->map_type == SMT_LINKSTATS) {
uint32 company_mask = this->GetOverlayCompanyMask();
Expand Down
8 changes: 4 additions & 4 deletions src/smallmap_gui.h
Expand Up @@ -67,8 +67,8 @@ class SmallMapWindow : public Window {

static const uint LEGEND_BLOB_WIDTH = 8; ///< Width of the coloured blob in front of a line text in the #WID_SM_LEGEND widget.
static const uint INDUSTRY_MIN_NUMBER_OF_COLUMNS = 2; ///< Minimal number of columns in the #WID_SM_LEGEND widget for the #SMT_INDUSTRY legend.
static const uint FORCE_REFRESH_PERIOD = 0x1F; ///< map is redrawn after that many ticks
static const uint BLINK_PERIOD = 0x0F; ///< highlight blinking interval
static const uint FORCE_REFRESH_PERIOD = 930; ///< map is redrawn after that many milliseconds.
static const uint BLINK_PERIOD = 450; ///< highlight blinking interval in milliseconds.

uint min_number_of_columns; ///< Minimal number of columns in legends.
uint min_number_of_fixed_rows; ///< Minimal number of rows in the legends for the fixed layouts only (all except #SMT_INDUSTRY).
Expand All @@ -79,7 +79,7 @@ class SmallMapWindow : public Window {
int32 subscroll; ///< Number of pixels (0..3) between the right end of the base tile and the pixel at the top-left corner of the smallmap display.
int zoom; ///< Zoom level. Bigger number means more zoom-out (further away).

uint8 refresh; ///< Refresh counter, zeroed every FORCE_REFRESH_PERIOD ticks.
int refresh; ///< Refresh timer, in millseconds.
LinkGraphOverlay *overlay;

static void BreakIndustryChainLink();
Expand Down Expand Up @@ -187,7 +187,7 @@ class SmallMapWindow : public Window {
virtual void OnInvalidateData(int data = 0, bool gui_scope = true);
virtual bool OnRightClick(Point pt, int widget);
virtual void OnMouseWheel(int wheel);
virtual void OnTick();
virtual void OnRealtimeTick(uint delta_ms);
virtual void OnScroll(Point delta);
virtual void OnMouseOver(Point pt, int widget);
};
Expand Down
4 changes: 2 additions & 2 deletions src/station_gui.cpp
Expand Up @@ -621,7 +621,7 @@ class CompanyStationsWindow : public Window
}
}

virtual void OnTick()
virtual void OnGameTick()
{
if (_pause_mode != PM_UNPAUSED) return;
if (this->stations.NeedResort()) {
Expand Down Expand Up @@ -2312,7 +2312,7 @@ struct SelectStationWindow : Window {
DeleteWindowById(WC_SELECT_STATION, 0);
}

virtual void OnTick()
virtual void OnRealtimeTick(uint delta_ms)
{
if (_thd.dirty & 2) {
_thd.dirty &= ~2;
Expand Down
19 changes: 11 additions & 8 deletions src/statusbar_gui.cpp
Expand Up @@ -79,10 +79,11 @@ static bool DrawScrollingStatusText(const NewsItem *ni, int scroll_pos, int left
struct StatusBarWindow : Window {
bool saving;
int ticker_scroll;
uint ticker_timer;
int reminder_timeout;

static const int TICKER_STOP = 1640; ///< scrolling is finished when counter reaches this value
static const int REMINDER_START = 91; ///< initial value of the reminder counter (right dot on the right)
static const int REMINDER_START = 1350; ///< time in ms for reminder notification (red dot on the right) to stay
static const int REMINDER_STOP = 0; ///< reminder disappears when counter reaches this value
static const int COUNTER_STEP = 2; ///< this is subtracted from active counters every tick

Expand Down Expand Up @@ -203,6 +204,7 @@ struct StatusBarWindow : Window {
case SBI_SHOW_REMINDER: this->reminder_timeout = REMINDER_START; break;
case SBI_NEWS_DELETED:
this->ticker_scroll = TICKER_STOP; // reset ticker ...
this->ticker_timer = 0;
this->reminder_timeout = REMINDER_STOP; // ... and reminder
break;
}
Expand All @@ -217,19 +219,20 @@ struct StatusBarWindow : Window {
}
}

virtual void OnTick()
virtual void OnRealtimeTick(uint delta_ms)
{
if (_pause_mode != PM_UNPAUSED) return;

if (this->ticker_scroll < TICKER_STOP) { // Scrolling text
this->ticker_scroll += COUNTER_STEP;
this->SetWidgetDirty(WID_S_MIDDLE);
uint count = CountIntervalElapsed(this->ticker_timer, delta_ms, 15);
if (count > 0) {
this->ticker_scroll += count;
this->SetWidgetDirty(WID_S_MIDDLE);
}
}

if (this->reminder_timeout > REMINDER_STOP) { // Red blot to show there are new unread newsmessages
this->reminder_timeout -= COUNTER_STEP;
} else if (this->reminder_timeout < REMINDER_STOP) {
this->reminder_timeout = REMINDER_STOP;
// Red blot to show there are new unread newsmessages
if (TimerElapsed(this->reminder_timeout, delta_ms)) {
this->SetWidgetDirty(WID_S_MIDDLE);
}
}
Expand Down

0 comments on commit 2a8fa5f

Please sign in to comment.