diff --git dataobj/loadsave.cc dataobj/loadsave.cc index d40a5a7c06..8306203ccf 100644 --- dataobj/loadsave.cc +++ dataobj/loadsave.cc @@ -506,7 +506,7 @@ bool loadsave_t::wr_open( const char *filename_utf8, mode_t m, int level, const saving = true; if( is_zipped() ) { // using zlib on servers, since 3x times faster saving than bz2 - char compr[ 4 ] = { 'w', 'b', (char)('0' + clamp( level, 1, 9 )), 0 }; + char compr[ 4 ] = { 'w', 'b', (char)('0' + sim::clamp( level, 1, 9 )), 0 }; fd->gzfp = dr_gzopen(filename_utf8, compr ); } else if( mode==binary ) { diff --git dataobj/settings.cc dataobj/settings.cc index 307d2ad019..010a78177b 100644 --- dataobj/settings.cc +++ dataobj/settings.cc @@ -724,7 +724,7 @@ void settings_t::rdwr(loadsave_t *file) random_counter = get_random_seed( ); file->rdwr_long( random_counter ); if( !env_t::networkmode || env_t::server ) { - frames_per_second = clamp(env_t::fps,5,100 ); // update it on the server to the current setting + frames_per_second = sim::clamp(env_t::fps,5,100 ); // update it on the server to the current setting frames_per_step = env_t::network_frames_per_step; } file->rdwr_long( frames_per_second ); @@ -924,9 +924,9 @@ void settings_t::parse_simuconf(tabfile_t& simuconf, sint16& disp_width, sint16& env_t::show_names = contents.get_int("show_names", env_t::show_names ); env_t::show_month = contents.get_int("show_month", env_t::show_month ); env_t::max_acceleration = contents.get_int("fast_forward", env_t::max_acceleration ); - env_t::fps = clamp(contents.get_int("frames_per_second", env_t::fps), env_t::min_fps, env_t::max_fps); - env_t::ff_fps = clamp(contents.get_int("fast_forward_frames_per_second", env_t::ff_fps), env_t::min_fps, env_t::max_fps); - env_t::num_threads = clamp( contents.get_int("threads", env_t::num_threads ), 1, MAX_THREADS ); + env_t::fps = sim::clamp(contents.get_int("frames_per_second", env_t::fps), env_t::min_fps, env_t::max_fps); + env_t::ff_fps = sim::clamp(contents.get_int("fast_forward_frames_per_second", env_t::ff_fps), env_t::min_fps, env_t::max_fps); + env_t::num_threads = sim::clamp( contents.get_int("threads", env_t::num_threads ), 1, MAX_THREADS ); env_t::simple_drawing_default = contents.get_int("simple_drawing_tile_size",env_t::simple_drawing_default ); env_t::simple_drawing_fast_forward = contents.get_int("simple_drawing_fast_forward",env_t::simple_drawing_fast_forward ); env_t::visualize_schedule = contents.get_int("visualize_schedule",env_t::visualize_schedule ) != 0; @@ -1159,7 +1159,7 @@ void settings_t::parse_simuconf(tabfile_t& simuconf, sint16& disp_width, sint16& factory_worker_radius = contents.get_int("factory_worker_radius", factory_worker_radius ); factory_worker_minimum_towns = contents.get_int("factory_worker_minimum_towns", factory_worker_minimum_towns ); factory_worker_maximum_towns = contents.get_int("factory_worker_maximum_towns", factory_worker_maximum_towns ); - factory_arrival_periods = clamp( contents.get_int("factory_arrival_periods", factory_arrival_periods), 1, 16 ); + factory_arrival_periods = sim::clamp( contents.get_int("factory_arrival_periods", factory_arrival_periods), 1, 16 ); factory_enforce_demand = contents.get_int("factory_enforce_demand", factory_enforce_demand) != 0; factory_maximum_intransit_percentage = contents.get_int("maximum_intransit_percentage", factory_maximum_intransit_percentage); @@ -1187,7 +1187,7 @@ void settings_t::parse_simuconf(tabfile_t& simuconf, sint16& disp_width, sint16& traffic_level = contents.get_int("citycar_level", traffic_level ); // ten normal years stadtauto_duration = contents.get_int("default_citycar_life", stadtauto_duration ); // ten normal years allow_buying_obsolete_vehicles = contents.get_int("allow_buying_obsolete_vehicles", allow_buying_obsolete_vehicles ); - used_vehicle_reduction = clamp( contents.get_int("used_vehicle_reduction", used_vehicle_reduction ), 0, 1000 ); + used_vehicle_reduction = sim::clamp( contents.get_int("used_vehicle_reduction", used_vehicle_reduction ), 0, 1000 ); // starting money starting_money = contents.get_int64("starting_money", starting_money ); @@ -1347,7 +1347,7 @@ void settings_t::parse_simuconf(tabfile_t& simuconf, sint16& disp_width, sint16& int bounds = (way_height_clearance!=0); way_height_clearance = contents.get_int("way_height_clearance", way_height_clearance ); if( way_height_clearance > 2 && way_height_clearance < bounds ) { - sint8 new_whc = clamp( way_height_clearance, bounds, 2 ); + sint8 new_whc = sim::clamp( way_height_clearance, bounds, 2 ); dbg->warning( "settings_t::parse_simuconf()", "Illegal way_height_clearance of %i set to %i", way_height_clearance, new_whc ); way_height_clearance = new_whc; } diff --git display/font.cc display/font.cc index f55a95f14a..5e52799979 100644 --- display/font.cc +++ display/font.cc @@ -193,7 +193,7 @@ bool font_t::load_from_bdf(FILE *bdf_file) f_numglyphs = atoi(str + 5) <= 0x100 ? 0x100 : 0xFFFE; glyphs.resize(max(f_numglyphs, 0)); - glyphs[(uint32)' '].advance = clamp(f_height / 2, 3, GLYPH_BITMAP_HEIGHT); + glyphs[(uint32)' '].advance = sim::clamp(f_height / 2, 3, GLYPH_BITMAP_HEIGHT); continue; } diff --git gui/components/gui_numberinput.cc gui/components/gui_numberinput.cc index 485b274eb8..d867a7a2c6 100644 --- gui/components/gui_numberinput.cc +++ gui/components/gui_numberinput.cc @@ -68,7 +68,7 @@ scr_size gui_numberinput_t::get_min_size() const void gui_numberinput_t::set_value(sint32 new_value) { // range check - value = clamp( new_value, min_value, max_value ); + value = sim::clamp( new_value, min_value, max_value ); gui_frame_t *win = win_get_top(); if( win && win->get_focus()!=this ) { @@ -93,7 +93,7 @@ sint32 gui_numberinput_t::get_text_value() sint32 gui_numberinput_t::get_value() { - return clamp( value, min_value, max_value ); + return sim::clamp( value, min_value, max_value ); } @@ -150,7 +150,7 @@ sint32 gui_numberinput_t::get_next_value() { sint64 diff = (sint64)max_value - (sint64)min_value; sint32 one_percent = (sint32) (diff / 100l); - return clamp( value+max(1,one_percent), min_value, max_value ); + return sim::clamp( value+max(1,one_percent), min_value, max_value ); } // power of 2 case POWER2: @@ -158,7 +158,7 @@ sint32 gui_numberinput_t::get_next_value() sint32 new_value=1; for( int i=0; i<32; i++ ) { if( value<(new_value<=0; i-- ) { if( value>(new_value<get_name()); int name_width = proportional_string_width(name)+8; - boxpos.x = clamp( boxpos.x, pos.x, pos.x+get_size().w-name_width ); + boxpos.x = sim::clamp( boxpos.x, pos.x, pos.x+get_size().w-name_width ); display_ddd_proportional_clip(boxpos.x, boxpos.y, name_width, 0, color_idx_to_rgb(5), color_idx_to_rgb(COL_WHITE), name, true); } } @@ -1767,7 +1767,7 @@ void minimap_t::draw(scr_coord pos) scr_coord boxpos = fabpos + scr_coord(10, 0); const char * name = translator::translate(fab->get_name()); int name_width = proportional_string_width(name)+8; - boxpos.x = clamp( boxpos.x, 0, 0+get_size().w-name_width ); + boxpos.x = sim::clamp( boxpos.x, 0, 0+get_size().w-name_width ); boxpos += pos; display_ddd_proportional_clip(boxpos.x, boxpos.y, name_width, 0, color_idx_to_rgb(10), color_idx_to_rgb(COL_WHITE), name, true); } diff --git gui/simwin.cc gui/simwin.cc index 054680f008..6988aa9b86 100644 --- gui/simwin.cc +++ gui/simwin.cc @@ -1215,8 +1215,8 @@ void move_win(int win, event_t *ev) } // CLIP(wert,min,max) - to_pos.x = CLIP( to_pos.x, 8-to_size.x, display_get_width()-16 ); - to_pos.y = CLIP( to_pos.y, env_t::iconsize.h, display_get_height() - D_TITLEBAR_HEIGHT - win_get_statusbar_height() - TICKER_HEIGHT); + to_pos.x = sim::clamp( to_pos.x, 8-to_size.x, display_get_width()-16 ); + to_pos.y = sim::clamp( to_pos.y, env_t::iconsize.h, display_get_height() - D_TITLEBAR_HEIGHT - win_get_statusbar_height() - TICKER_HEIGHT); // delta is actual window movement. const scr_coord delta = to_pos - from_pos; diff --git macros.h macros.h index 5197cde1bf..42b66c449c 100644 --- macros.h +++ macros.h @@ -26,7 +26,12 @@ template static inline void lengthof_check(T (&)[N]) {} #define MEMZERON(ptr, n) memset((ptr), 0, sizeof(*(ptr)) * (n)) #define MEMZERO(obj) MEMZERON(&(obj), 1) -// make sure, a value in within the borders + + +namespace sim +{ + +// make sure that a value is within the borders static inline int clamp(int x, int min, int max) { if (x <= min) { @@ -39,8 +44,6 @@ static inline int clamp(int x, int min, int max) } -namespace sim { - template inline void swap(T& a, T& b) { T t = a; diff --git player/ai_goods.cc player/ai_goods.cc index 4b32ae3e21..47e289bb01 100644 --- player/ai_goods.cc +++ player/ai_goods.cc @@ -977,7 +977,7 @@ DBG_MESSAGE("ai_goods_t::do_ki()","No roadway possible."); if( count_road<255 ) { // for short distance: reduce number of cars // calculated here, since the above number was based on production - count_road = CLIP( (sint32)(dist*15)/best_road_speed, 2, count_road ); + count_road = sim::clamp( (sint32)(dist*15)/best_road_speed, 2, count_road ); int freight_price = (freight->get_value()*road_vehicle->get_capacity()*count_road)/24*((8000+(best_road_speed-80)*freight->get_speed_bonus())/1000); cost_road = road_weg->get_maintenance() + 300/dist + (count_road*road_vehicle->get_running_cost()*best_road_speed)/(2*dist+5); income_road = (freight_price*best_road_speed)/(2*dist+5); diff --git simintr.cc simintr.cc index d56a2a73c3..4695720761 100644 --- simintr.cc +++ simintr.cc @@ -70,7 +70,7 @@ uint32 get_frame_time() void set_frame_time(uint32 time) { - frame_time = clamp( time, 1000/env_t::max_fps, 1000/env_t::min_fps )*FRAME_TIME_MULTI; + frame_time = sim::clamp( time, 1000/env_t::max_fps, 1000/env_t::min_fps )*FRAME_TIME_MULTI; } diff --git simmain.cc simmain.cc index f12c1ebc5e..ce0c367873 100644 --- simmain.cc +++ simmain.cc @@ -1023,7 +1023,7 @@ int simu_main(int argc, char** argv) // set number of threads if( const char *ref_str = gimme_arg(argc, argv, "-threads", 1) ) { int want_threads = atoi(ref_str); - env_t::num_threads = clamp(want_threads, 1, MAX_THREADS); + env_t::num_threads = sim::clamp(want_threads, 1, MAX_THREADS); } #else if( env_t::num_threads > 1 ) { @@ -1226,7 +1226,7 @@ DBG_MESSAGE("simmain","loadgame file found at %s",path.c_str()); if( gimme_arg(argc, argv, "-startyear", 0) != NULL ) { const char * ref_str = gimme_arg(argc, argv, "-startyear", 1); //1930 if( ref_str != NULL ) { - env_t::default_settings.set_starting_year( clamp(atoi(ref_str),1,2999) ); + env_t::default_settings.set_starting_year( sim::clamp(atoi(ref_str),1,2999) ); } } diff --git simtool.cc simtool.cc index aae2db78e1..3089b845a4 100644 --- simtool.cc +++ simtool.cc @@ -6392,7 +6392,7 @@ void tool_merge_stop_t::mark_tiles( player_t *player, const koord3d &start, con } if( distance < welt->get_settings().allow_merge_distant_halt ) { - distance = clamp(distance,2,33)-2; + distance = sim::clamp(distance,2,33)-2; workcost = welt->scale_with_month_length( (1<get_settings().cst_multiply_merge_halt ); win_set_static_tooltip( tooltip_with_price("Building costs estimates", workcost) ); } @@ -6424,7 +6424,7 @@ const char *tool_merge_stop_t::do_work( player_t *player, const koord3d &last_po } if( distance < welt->get_settings().allow_merge_distant_halt ) { - distance = clamp(distance,2,33)-2; + distance = sim::clamp(distance,2,33)-2; workcost = welt->scale_with_month_length( (1<get_settings().cst_multiply_merge_halt ); win_set_static_tooltip( tooltip_with_price("Building costs estimates", workcost) ); if( player != welt->get_public_player() && !player->can_afford(workcost) ) { diff --git simtypes.h simtypes.h index c4bcf1ec60..1ab5fa15f2 100644 --- simtypes.h +++ simtypes.h @@ -147,11 +147,6 @@ enum systemtype_t { }; -// makros are not very safe: thus use these macro like functions -// otherwise things may fail or functions are called uneccessarily twice - -#define CLIP(wert,mini,maxi) min(max((wert),(mini)),(maxi)) - // define machine independent types typedef unsigned int uint; typedef signed char sint8; diff --git simworld.cc simworld.cc index bf8213cbe5..691db77d05 100644 --- simworld.cc +++ simworld.cc @@ -6010,8 +6010,8 @@ void karte_t::calc_climate_map_region( sint16 xtop, sint16 ytop, sint16 xbottom, * We will start an ellispe at the first tile than is inmarked with a random allowed climate for that height * The region sizes depends on the map (within reason) */ - const sint16 max_patchsize_x = clamp( 5, xbottom / 24, 256 ); - const sint16 max_patchsize_y = clamp( 5, ybottom / 24, 256 ); + const sint16 max_patchsize_x = sim::clamp( xbottom / 24, 5, 256 ); + const sint16 max_patchsize_y = sim::clamp( ybottom / 24, 5, 256 ); minivec_tpl allowed( 8 ); { @@ -6343,7 +6343,7 @@ void karte_t::reset_timer() } else if(step_mode==FIX_RATIO) { last_frame_idx = 0; - fix_ratio_frame_time = 1000 / clamp(settings.get_frames_per_second(), 5, 100); + fix_ratio_frame_time = 1000 / sim::clamp(settings.get_frames_per_second(), 5, 100); next_step_time = last_tick_sync + fix_ratio_frame_time; set_frame_time( fix_ratio_frame_time ); intr_disable(); @@ -7002,7 +7002,7 @@ bool karte_t::interactive(uint32 quit_month) next_step_time = time; } - const sint32 nst_diff = clamp( ms_difference, -fix_ratio_frame_time * 2, fix_ratio_frame_time * 8 ) / 10; // allows timerate between 83% and 500% of normal + const sint32 nst_diff = sim::clamp( ms_difference, -fix_ratio_frame_time * 2, fix_ratio_frame_time * 8 ) / 10; // allows timerate between 83% and 500% of normal next_step_time += fix_ratio_frame_time - nst_diff; ms_difference -= nst_diff; }