indexing description: "[ The EM_TIME class provide some features related to time measurement. You can inherit from EM_TIME_SINGLETON if you need any of those features. EM_TIME also holds a list of FUNCTION[ANY,TUPLE[INTEGER],INTEGER] those functions are called repeatedly after some defined interval time. All this functions are executed in the same thread. This thread is an other than the thread where the main program is running. ]" date: "$Date$" revision: "$Revision$" class EM_TIME inherit EM_DELAYED_PROCEDURES redefine make end SDL_TIMER_FUNCTIONS_EXTERNAL export {NONE} all end SDL_NEW_TIMER_CALLBACK_CALLBACK export {NONE} all end create {EM_TIME_SINGLETON} make feature {NONE} -- Initialization make is -- Create a EM_TIME do Precursor create user_callbacks.make end feature -- Access ticks: INTEGER is -- Milliseconds since EiffelMedia library initialization. Wraps after ~49 days. do result := sdl_get_ticks_external end feature -- Basic operations delay (milliseconds: INTEGER) is -- Waits at least `milliseconds' milliseconds require positiv: milliseconds>=0 do sdl_delay_external (milliseconds) end add_timed_callback (interval: INTEGER; an_action: FUNCTION[ANY, TUPLE[INTEGER], INTEGER]) is -- Triggers `an_action' every `interval' milliseconds -- The INTEGER value passed to `an_action' is `interval' -- The result of `an_action' may be result <= 0 : to never call `an_action' again -- result = `interval': to keep the same trigger interval -- result > 0 : to set a new interval -- hint 1: `an_action' should always be executed faster than interval -- hint 2: `an_action' runs in a separate thread, so be careful! require not_less_then_overhead: interval >= 10 an_action_not_void: an_action /= void local timer: EM_TIMER success: INTEGER do create timer.make (interval, an_action) user_callbacks.sort user_callbacks.extend (timer) if (id = default_pointer) then id := sdl_add_timer_external (interval,dispatcher.c_dispatcher,default_pointer) elseif (user_callbacks.first=timer) then -- Now we have to schedule a new dispatcher success := sdl_remove_timer_external(id) id := sdl_add_timer_external(interval,dispatcher.c_dispatcher,default_pointer) end end quit is -- Clean up the environment local success: INTEGER do if (id /= default_pointer) then success := sdl_remove_timer_external(id) end end feature {NONE} -- callback dispatcher dispatcher: SDL_NEW_TIMER_CALLBACK_DISPATCHER is once create Result.make (current) end on_callback (an_interval: INTEGER; a_param: POINTER): INTEGER is -- `on_callback' is called by the dispatcher. On_callback is -- a dispatcher as well. It calls the right function in -- user_callbacks. local a_result: INTEGER success: INTEGER cur_time: INTEGER tmp_interval: INTEGER removed: BOOLEAN do result := result.Max_value cur_time:= ticks from user_callbacks.start until user_callbacks.after loop removed:=false if user_callbacks.item.next_trigger_time=1 end user_callbacks:SORTED_TWO_WAY_LIST[EM_TIMER] -- A list of callbacks id: POINTER -- The pointer to the `id' given by sdl to on_callback end