The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
timer.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2016 by Mark de Wever <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file
17  * Contains the gui2 timer routines.
18  *
19  * This code avoids the following problems with the sdl timers:
20  * - the callback must be a C function with a fixed signature.
21  * - the callback needs to push an event in the event queue, between the
22  * pushing and execution of that event the timer can't be stopped. (Makes
23  * sense since the timer has expired, but not what the user wants.)
24  *
25  * With these functions it's possible to remove the event between pushing in
26  * the queue and the actual execution. Since the callback is a std::function
27  * object it's possible to make the callback as fancy as wanted.
28  */
29 
30 #ifndef GUI_WIDGETS_AUXILIARY_TIMER_HPP_INCLUDED
31 #define GUI_WIDGETS_AUXILIARY_TIMER_HPP_INCLUDED
32 
33 #include "utils/functional.hpp"
34 
35 #include <SDL_types.h>
36 
37 namespace gui2
38 {
39 
40 /**
41  * Adds a new timer.
42  *
43  * @param interval The timer interval in ms.
44  * @param callback The function to call when the timer expires,
45  * the id send as parameter is the id of the
46  * timer.
47  * @param repeat If true the timer will restart after it
48  * expires.
49  *
50  * @returns The id of the timer.
51  * @retval [0] Failed to create a timer.
52  */
53 size_t add_timer(const Uint32 interval,
54  const std::function<void(size_t id)>& callback,
55  const bool repeat = false);
56 
57 /**
58  * Removes a timer.
59  *
60  * It's save to remove a timer in its own callback, only the value returned
61  * might not be accurate. The destruction is postponed until the execution is
62  * finished and the return value is whether the postponing was successful.
63  *
64  * @param id The id of the timer to remove, this is the id
65  * returned by add_timer.
66  *
67  * @returns Status, false if the timer couldn't be
68  * removed.
69  */
70 bool remove_timer(const size_t id);
71 
72 /**
73  * Executes a timer.
74  *
75  * @note this function is only meant to be executed by the event handling
76  * system.
77  *
78  * @param id The id of the timer to execute, this is the
79  * id returned by add_timer.
80  *
81  * @returns Status, false if the timer couldn't be
82  * executed.
83  */
84 bool execute_timer(const size_t id);
85 
86 } // namespace gui2
87 
88 #endif
bool remove_timer(const size_t id)
Removes a timer.
Definition: timer.cpp:144
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
bool execute_timer(const size_t id)
Executes a timer.
Definition: timer.cpp:175
size_t add_timer(const Uint32 interval, const std::function< void(size_t id)> &callback, const bool repeat)
Adds a new timer.
Definition: timer.cpp:112