-
Notifications
You must be signed in to change notification settings - Fork 0
/
rudl_timer.c
119 lines (101 loc) · 2.88 KB
/
rudl_timer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* RUDL - a C library wrapping SDL for use in Ruby. Copyright (C) 2001 Danny van Bruggen */
#include "rudl_timer.h"
#include "rudl_video.h"
#include "rudl_events.h"
void initTimer()
{
if(!SDL_WasInit(SDL_INIT_TIMER)){
DEBUG_S("Starting timer subsystem");
SDL_VERIFY(SDL_Init(SDL_INIT_TIMER)!=-1);
}
}
///////////////////////////////// TIME
/**
@file Timer
@class Timer
@section Class Methods
@method delay( milliseconds ) -> nil
Will do nothing for @milliseconds milliseconds.
It is not guaranteed to be exact and has different resolution on different platforms.
Expect a resolution of 10 to 20 milliseconds at worst.
*/
static VALUE timer_delay(VALUE obj, VALUE delay)
{
initTimer();
SDL_Delay(NUM2INT(delay));
return Qnil;
}
/**
@method ticks -> Number
Returns the time in milliseconds since RUDL was required.
*/
static VALUE timer_getTicks(VALUE obj)
{
initTimer();
return INT2NUM(SDL_GetTicks());
}
///////////////////////////////// EVENTTIMER
void freeEventTimer(SDL_TimerID freeMe)
{
SDL_RemoveTimer(freeMe);
}
Uint32 timerCallback(Uint32 interval, void *param)
{
SDL_Event user_event;
user_event.type=RUDL_TIMEREVENT;
user_event.user.code=(int)param;
user_event.user.data1=NULL;
user_event.user.data2=NULL;
SDL_PushEvent(&user_event);
return interval;
}
/**
@class EventTimer
A class that controls delivering @TimerEvent s on a regular basis.
*/
/**
@section Class Methods
@method new( interval, id ) -> EventTimer
Will create a EventTimer object that controls sending @TimerEvent s every @interval
milliseconds, having their id-field set to @id - which is a number.
*/
static VALUE eventTimer_new(VALUE obj, VALUE interval, VALUE id)
{
SDL_TimerID timerID;
initTimer();
timerID=SDL_AddTimer(NUM2INT(interval), timerCallback, (void*)(NUM2INT(id)));
SDL_VERIFY(timerID);
return Data_Wrap_Struct(classEventTimer, 0, freeEventTimer, timerID);
}
/**
@section Instance Methods
@method stop -> boolean
Will stop this EventTimer from posting more @TimerEvent s.
*/
static VALUE eventTimer_stop(VALUE obj)
{
SDL_TimerID timerID;
// Data_Get_Struct hack because of unavailability of struct _SDL_TimerID
Check_Type(obj, T_DATA);
timerID=(SDL_TimerID)DATA_PTR(obj);
return INT2BOOL(SDL_RemoveTimer(timerID));
}
void initTimerClasses()
{
classTimer=rb_define_module_under(moduleRUDL, "Timer");
rb_define_singleton_method(classTimer, "delay", timer_delay, 1);
rb_define_singleton_method(classTimer, "ticks", timer_getTicks, 0);
classEventTimer=rb_define_class_under(moduleRUDL, "EventTimer", rb_cObject);
rb_define_singleton_method(classEventTimer, "new", eventTimer_new, 2);
rb_define_method(classEventTimer, "stop", eventTimer_stop, 0);
/**
@class TimerEvent
This event is posted regularly by an @EventTimer object.
*/
/**
@method id -> Number
This is the EventTimer's @id.
*/
classTimerEvent=rb_define_class_under(moduleRUDL, "TimerEvent", classEvent);
rb_define_attr(classTimerEvent, "id", 1, 1);
}