syntax: 'meth' -> 'fun', 'attr' -> 'var'
[nit.git] / lib / sdl.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2006 Jean Privat <jean@pryen.org>
4 #
5 # This file is free software, which comes along with NIT. This software is
6 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
7 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
8 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
9 # is kept unaltered, and a notification of the changes is added.
10 # You are allowed to redistribute it and sell it, alone or is a part of
11 # another product.
12
13 # Binding to the SDL multomedia library
14 package sdl
15
16 universal SDL_Surface
17 special Pointer
18 fun width: Int is extern "sdl_surface_width"
19 fun height: Int is extern "sdl_surface_height"
20
21 fun lock_surface: Int is extern "SDL_LockSurface"
22 fun unlock_surface: Int is extern "SDL_LockSurface"
23
24 fun blit_on(dest: SDL_Surface) is extern "sdl_blit_surface"
25 fun blit_on_xy(dest: SDL_Surface, dest_x: Int, dest_y: Int) is extern "sdl_blit_surface_xy"
26
27 fun update_rect(x: Int, y: Int, w: Int, h: Int) is extern "SDL_UpdateRect"
28 fun update_all
29 do
30 update_rect(0, 0, 0, 0)
31 end
32
33 fun clear is extern "sdl_clear_sruface"
34
35 fun to_display_format: SDL_Surface is extern "SDL_DisplayFormat"
36 fun free is extern "SDL_FreeSurface"
37 end
38
39 universal SDL_Screen
40 special SDL_Surface
41 fun flip is extern "SDL_Flip"
42 end
43
44 universal SDL_Event
45 special Pointer
46 fun is_keyboard: Bool is extern "sdl_evt_is_keyboard"
47 fun as_keyboard: SDL_KeyboardEvent is extern "sdl_evt_as_keyboard"
48 fun is_mouse_button: Bool is extern "sdl_evt_is_mouse_button"
49 fun as_mouse_button: SDL_MouseButtonEvent is extern "sdl_evt_as_mouse_button"
50 fun is_mouse_motion: Bool is extern "sdl_evt_is_mouse_motion"
51 fun as_mouse_motion: SDL_MouseMotionEvent is extern "sdl_evt_as_mouse_motion"
52 fun is_expose: Bool is extern "sdl_evt_is_expose"
53 fun is_quit: Bool is extern "sdl_evt_is_quit"
54 end
55
56 universal SDL_ButtonEvent
57 special SDL_Event
58 fun is_pressed: Bool is abstract
59 end
60
61 universal SDL_MouseEvent
62 special SDL_Event
63 fun x: Int is abstract
64 fun y: Int is abstract
65 end
66
67 universal SDL_KeyboardEvent
68 special SDL_ButtonEvent
69 redef fun is_pressed: Bool is extern "sdl_keyboard_evt_state"
70 end
71
72 universal SDL_MouseButtonEvent
73 special SDL_ButtonEvent
74 special SDL_MouseEvent
75 redef fun is_pressed: Bool is extern "sdl_mouse_button_evt_state"
76 redef fun x: Int is extern "sdl_mouse_button_evt_x"
77 redef fun y: Int is extern "sdl_mouse_button_evt_y"
78 fun button: Int is extern "sdl_mouse_button_evt_button"
79 end
80
81 universal SDL_MouseMotionEvent
82 special SDL_MouseEvent
83 redef fun x: Int is extern "sdl_mouse_evt_x"
84 redef fun y: Int is extern "sdl_mouse_evt_y"
85 fun xrel: Int is extern "sdl_mouse_evt_xrel"
86 fun yrel: Int is extern "sdl_mouse_evt_yrel"
87 end
88
89 class SDL_EventListener
90 fun on_keyboard(evt: SDL_KeyboardEvent)
91 do end
92
93 fun on_mouse_button(evt: SDL_MouseButtonEvent)
94 do end
95
96 fun on_mouse_motion(evt: SDL_MouseMotionEvent)
97 do end
98
99 fun on_expose
100 do end
101
102 fun on_quit
103 do end
104 end
105
106 class SDL_EventProcessor
107 var _listeners: Array[SDL_EventListener]
108
109 fun add_listener(l: SDL_EventListener)
110 do
111 _listeners.add(l)
112 end
113
114 fun remove_listener(l: SDL_EventListener)
115 do
116 _listeners.remove(l)
117 end
118
119 fun process_one_event
120 do
121 if sdl_poll_next_event then
122 process_event(sdl_current_event)
123 end
124 end
125
126 fun process_all_events
127 do
128 while sdl_poll_next_event do
129 process_event(sdl_current_event)
130 end
131 end
132
133 private fun process_event(evt: SDL_Event)
134 do
135 var sdl_listeners = _listeners
136 if evt.is_keyboard then
137 for i in sdl_listeners do
138 i.on_keyboard(evt.as_keyboard)
139 end
140 else if evt.is_mouse_button then
141 for i in sdl_listeners do
142 i.on_mouse_button(evt.as_mouse_button)
143 end
144 else if evt.is_mouse_motion then
145 for i in sdl_listeners do
146 i.on_mouse_motion(evt.as_mouse_motion)
147 end
148 else if evt.is_expose then
149 for i in sdl_listeners do
150 i.on_expose
151 end
152 else if evt.is_quit then
153 for i in sdl_listeners do
154 i.on_quit
155 end
156 end
157 end
158
159 init
160 do
161 _listeners = new Array[SDL_EventListener]
162 end
163 end
164
165 # General
166
167 fun sdl_init is extern
168
169 # Video
170
171 fun sdl_set_video_mode(w: Int, h: Int, d: Int): SDL_Screen is extern
172 fun sdl_set_fullscreen_video_mode(w: Int, h: Int, d: Int): SDL_Screen is extern
173
174 fun sdl_load_raw_bmp(s: String): SDL_Surface
175 do
176 return sdl_load_bmp_native(s.to_cstring)
177 end
178 fun sdl_load_bmp(s: String): SDL_Surface
179 do
180 var raw = sdl_load_raw_bmp(s)
181 var sprite = raw.to_display_format
182 raw.free
183 return sprite
184 end
185
186 fun sdl_load_bmp_native(s: NativeString): SDL_Surface is extern
187
188 fun sdl_show_cursor=(b: Bool) is extern "sdl_show_cursor_1"
189 fun sdl_show_cursor: Bool is extern "sdl_show_cursor_0"
190
191 # WM
192
193 fun sdl_grab=(b: Bool) is extern "sdl_grab_1"
194 fun sdl_grab: Bool is extern "sdl_grab_0"
195
196 # Events
197
198 fun sdl_current_event: SDL_Event is extern
199 fun sdl_poll_next_event: Bool is extern
200
201 # Time
202
203 fun sdl_get_ticks: Int is extern
204 fun sdl_delay(ms: Int) is extern