Skip to content

Commit

Permalink
CHG: split the unrelated code for displaying relative time difference…
Browse files Browse the repository at this point in the history
…s and absolute times (as preparation for coming scheduling)

git-svn-id: svn://tron.homeunix.org/simutrans/simutrans/trunk@9490 8aca7d54-2c30-db11-9de9-000461428c89
  • Loading branch information
prissi committed Dec 6, 2020
1 parent 411e4fe commit 40b531d
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 67 deletions.
17 changes: 13 additions & 4 deletions gui/halt_info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ void gui_departure_board_t::update_departures(halthandle_t halt)
convoihandle_t cnv = line->get_convoy(j);
if( cnv.is_bound() && ( cnv->get_state() == convoi_t::DRIVING || cnv->is_waiting() ) && haltestelle_t::get_halt( cnv->get_schedule()->get_current_entry().pos, cnv->get_owner() ) == halt ) {
halthandle_t prev_halt = haltestelle_t::get_halt( cnv->front()->last_stop_pos, cnv->get_owner() );
sint32 delta_t = cur_ticks + calc_ticks_until_arrival( cnv );
sint32 delta_t = calc_ticks_until_arrival( cnv );
if( prev_halt.is_bound() ) {
dest_info_t prev( prev_halt, delta_t, cnv );
// smooth times a little
Expand Down Expand Up @@ -837,9 +837,13 @@ void gui_departure_board_t::update_departures(halthandle_t halt)
FOR( vector_tpl<dest_info_t>, hi, destinations ) {
if( freight_list_sorter_t::by_via_sum != env_t::default_sortmode || !exclude.is_contained( hi.halt ) ) {
gui_label_buf_t *lb = new_component<gui_label_buf_t>(SYSCOL_TEXT, gui_label_t::right);
lb->buf().printf("%s", tick_to_string( hi.delta_ticks, false ) );
if( hi.delta_ticks == 0 ) {
lb->buf().append( translator::translate( "now" ) );
}
else {
lb->buf().printf("%s", difftick_to_string( hi.delta_ticks, true ) );
}
lb->update();

insert_image(hi.cnv);

new_component<gui_label_t>(hi.halt->get_name() );
Expand All @@ -854,7 +858,12 @@ void gui_departure_board_t::update_departures(halthandle_t halt)
FOR( vector_tpl<dest_info_t>, hi, origins ) {
if( freight_list_sorter_t::by_via_sum != env_t::default_sortmode || !exclude.is_contained( hi.halt ) ) {
gui_label_buf_t *lb = new_component<gui_label_buf_t>(SYSCOL_TEXT, gui_label_t::right);
lb->buf().printf("%s", tick_to_string( hi.delta_ticks, false ) );
if( hi.delta_ticks == 0 ) {
lb->buf().append( translator::translate( "now" ) );
}
else {
lb->buf().printf("%s", difftick_to_string( hi.delta_ticks, true ) );
}
lb->update();

insert_image(hi.cnv);
Expand Down
2 changes: 1 addition & 1 deletion gui/simwin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1722,7 +1722,7 @@ void win_display_flush(double konto)
}
}

char const *time = tick_to_string( wl->get_ticks(), true );
char const *time = tick_to_string( wl->get_ticks() );

// statusbar background
KOORD_VAL const status_bar_height = win_get_statusbar_height();
Expand Down
140 changes: 81 additions & 59 deletions simintr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ void intr_enable()
}


char const *tick_to_string( sint32 ticks, bool show_full )
char const *tick_to_string( uint32 ticks )
{
static sint32 tage_per_month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
static char const* const seasons[] = { "q2", "q3", "q4", "q1" };
Expand Down Expand Up @@ -187,42 +187,64 @@ char const *tick_to_string( sint32 ticks, bool show_full )
minuten = ((ticks_this_month * 24 * 60) >> welt_modell->ticks_per_world_month_shift)%60;
}

if( show_full || env_t::show_month == env_t::DATE_FMT_SEASON ) {

//DBG_MESSAGE("env_t::show_month","%d",env_t::show_month);
// since seasons 0 is always summer for backward compatibility
char const* const date = translator::get_date(year, month, tage, translator::translate(seasons[welt_modell->get_season()]));
switch (env_t::show_month) {
case env_t::DATE_FMT_US:
case env_t::DATE_FMT_US_NO_SEASON: {
uint32 hours_ = hours % 12;
if (hours_ == 0) hours_ = 12;
sprintf(time, "%s %2d:%02d%s", date, hours_, minuten, hours < 12 ? "am" : "pm");
break;
}
case env_t::DATE_FMT_SEASON:
sprintf(time, "%s", date);
break;
default:
sprintf(time, "%s %2d:%02dh", date, hours, minuten);
break;
}
//DBG_MESSAGE("env_t::show_month","%d",env_t::show_month);
// since seasons 0 is always summer for backward compatibility
char const* const date = translator::get_date(year, month, tage, translator::translate(seasons[welt_modell->get_season()]));
switch (env_t::show_month) {
case env_t::DATE_FMT_US:
case env_t::DATE_FMT_US_NO_SEASON: {
uint32 hours_ = hours % 12;
if (hours_ == 0) hours_ = 12;
sprintf(time, "%s %2d:%02d%s", date, hours_, minuten, hours < 12 ? "am" : "pm");
break;
}
case env_t::DATE_FMT_SEASON:
sprintf(time, "%s", date);
break;
default:
sprintf(time, "%s %2d:%02dh", date, hours, minuten);
break;
}
return time;
}


char const *difftick_to_string( sint32 ticks, bool round_to_quaters )
{
static sint32 tage_per_month = 31;
static char time [128];

time[0] = 0;

// World model might not be initalized if this is called while reading saved windows.
if (welt_modell == NULL) {
return time;
}

assert( env_t::show_month > env_t::DATE_FMT_MONTH );

// suppress as much as possible, assuming this is an relative offset to the current month
sint32 num_days = ( ticks * (env_t::show_month==env_t::DATE_FMT_MONTH? 3 : 31) ) >> welt_modell->ticks_per_world_month_shift;
char days[64];
days[0] = 0;
if( num_days!=0 ) {
sprintf( days, "%+i ", num_days );
}

uint32 tage, hours, minuten;
if (env_t::show_month > env_t::DATE_FMT_MONTH) {
hours = (((sint64)ticks*31) >> (welt_modell->ticks_per_world_month_shift-16));
minuten = (((hours*3) % 8192)*60)/8192;
hours = ((hours*3) / 8192)%24;
}
else {
if( ticks == 0 ) {
return translator::translate("now");
}
hours = (ticks * 24) >> welt_modell->ticks_per_world_month_shift;
minuten = ((ticks * 24 * 60) >> welt_modell->ticks_per_world_month_shift) % 60;
}

// suppress as much as possible, assuming this is an relative offset to the current month
sint32 num_days = ( ticks * (env_t::show_month==env_t::DATE_FMT_MONTH? 3 : tage_per_month[month]) ) >> welt_modell->ticks_per_world_month_shift;
num_days -= ( (welt_modell->get_ticks() % welt_modell->ticks_per_world_month) * (env_t::show_month==env_t::DATE_FMT_MONTH? 3 : tage_per_month[month]) ) >> welt_modell->ticks_per_world_month_shift;
char days[64];
days[0] = 0;
if( num_days!=0 ) {
sprintf( days, "%+i ", num_days );
}

// maybe round minutes
// maybe round minutes
if( round_to_quaters ) {
int switchtick = welt_modell->ticks_per_world_month_shift;
if( env_t::show_month == env_t::DATE_FMT_MONTH ) {
// since a month is then just three days instead of about 30 ...
Expand All @@ -249,34 +271,34 @@ char const *tick_to_string( sint32 ticks, bool show_full )
else if( switchtick == 22 ) {
minuten = ( (minuten + 2) / 5 ) * 5;
}
// take care of overflow
hours += (minuten / 60);
minuten %= 60;
hours %= 24;

switch(env_t::show_month) {
case env_t::DATE_FMT_GERMAN:
case env_t::DATE_FMT_GERMAN_NO_SEASON:
sprintf(time, "%s%2d:%02dh", days, hours, minuten);
break;

case env_t::DATE_FMT_US:
case env_t::DATE_FMT_US_NO_SEASON: {
uint32 hours_ = hours % 12;
if (hours_ == 0) hours_ = 12;
sprintf(time, "%s%2d:%02d%s", days, hours_, minuten, hours < 12 ? "am" : "pm");
break;
}
}
// take care of overflow
hours += (minuten / 60);
minuten %= 60;
hours %= 24;

switch(env_t::show_month) {
case env_t::DATE_FMT_GERMAN:
case env_t::DATE_FMT_GERMAN_NO_SEASON:
sprintf(time, "%s%2d:%02dh", days, hours, minuten);
break;

case env_t::DATE_FMT_US:
case env_t::DATE_FMT_US_NO_SEASON: {
uint32 hours_ = hours % 12;
if (hours_ == 0) hours_ = 12;
sprintf(time, "%s%2d:%02d%s", days, hours_, minuten, hours < 12 ? "am" : "pm");
break;
}

case env_t::DATE_FMT_JAPANESE:
case env_t::DATE_FMT_JAPANESE_NO_SEASON:
sprintf(time, "%s%2d:%02dh", days, hours, minuten);
break;
case env_t::DATE_FMT_JAPANESE:
case env_t::DATE_FMT_JAPANESE_NO_SEASON:
sprintf(time, "%s%2d:%02dh", days, hours, minuten);
break;

case env_t::DATE_FMT_MONTH:
sprintf(time, "%s%2d:%02dh", days, hours, minuten);
break;
}
case env_t::DATE_FMT_MONTH:
sprintf(time, "%s%2d:%02dh", days, hours, minuten);
break;
}
return time;
}
11 changes: 8 additions & 3 deletions simintr.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,13 @@ void interrupt_check(const char* caller_info = "0");
#endif
#endif

// returns a time string in the desired format
// Returns an empty string if called before the world model defining time is initalized.
char const *tick_to_string( sint32 ticks, bool show_full );
// returns a time string in the desired format
// Returns an empty string if called before the world model defining time is initalized.
char const *tick_to_string( uint32 ticks );

// returns a time difference string in the desired format
// assume the month has 31 days
// round will reduce presion to qrater or less (depending on month length)
char const *difftick_to_string( sint32 ticks, bool round );

#endif

0 comments on commit 40b531d

Please sign in to comment.