Analog clock
From FSDeveloper Wiki
Revision as of 05:26, 5 January 2020 by Dragonflightdesign (talk | contribs) (C: 12-hour analog clock)
The problem with using the CLOCK_HOUR and CLOCK_MINUTE variables to build a clock is that they 'flick over' - great for building a digital clock but no use for an analog clock. To drive the hands smoothly you really need to use just the CLOCK_SECONDS variable and apply it to all three hands on the clock. This is copy'n'paste code.
//---------------------------------------------------------- // Time - 12 hour analog clock // Driven from seconds only to get better movement of the hands // 3,600 seconds to an hour // 43,200 seconds to twelve hours //---------------------------------------------------------- MODULE_VAR localsecond = { CLOCK_SECOND }; // Global clock hand variables for the gauge display double second_hand = 0; double minute_hand = 0; double hour_hand = 0; // Function to be called from the gauge - can be used in any project without having to change anything // Can also be called from multiple clocks in the same aircraft void analogClock() { int x = 0; static double mins = -1; static double hours = -1; static double prev_secs = -1; static double set_clock = 0; static double prev_tick = -1; // Trap a time change by menu. Video processing is suspended while the menu is active // Use this to force a clock update (set_clock = 0) when video processing resumes if (prev_tick == -1)prev_tick = getTick(); if (prev_tick != getTick()) { if (getTick() > prev_tick + 1)set_clock = 0; prev_tick = getTick(); } // Set clock to the current simulator time if (set_clock == 0) { set_clock = 1; mins = localminutes.var_value.n; hours = localhours.var_value.n; // 12 hour trap if (hours > 12) hours -= 12; // Adjust needle positioning for when normal updates resume hours *= 3600; mins *= 60; hours += mins; } else { // Resume normal updates second_hand = localsecond.var_value.n; x = (int)second_hand; if (x != prev_secs) { prev_secs = x; // Minute hand mins += 1; if (mins >= 3599)mins = 0; minute_hand = mins; // Hour hand hours += 1; // Reset if passing through 00.00 or 12.00 if (hours >= 43199)hours = 0; hour_hand = hours; } } return; }