tools: add nits to .gitignore
[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 extern SDL_Surface
17 fun width: Int is extern "sdl_surface_width"
18 fun height: Int is extern "sdl_surface_height"
19
20 fun lock_surface: Int is extern "SDL_LockSurface"
21 fun unlock_surface: Int is extern "SDL_LockSurface"
22
23 fun blit_on(dest: SDL_Surface) is extern "sdl_blit_surface"
24 fun blit_on_xy(dest: SDL_Surface, dest_x: Int, dest_y: Int) is extern "sdl_blit_surface_xy"
25
26 fun update_rect(x: Int, y: Int, w: Int, h: Int) is extern "SDL_UpdateRect"
27 fun update_all
28 do
29 update_rect(0, 0, 0, 0)
30 end
31
32 fun clear is extern "sdl_clear_sruface"
33
34 fun to_display_format: SDL_Surface is extern "SDL_DisplayFormat"
35 fun free is extern "SDL_FreeSurface"
36 end
37
38 extern SDL_Screen
39 super SDL_Surface
40 fun flip is extern "SDL_Flip"
41 end
42
43 extern SDL_Event
44 fun is_keyboard: Bool is extern "sdl_evt_is_keyboard"
45 fun as_keyboard: SDL_KeyboardEvent is extern "sdl_evt_as_keyboard"
46 fun is_mouse_button: Bool is extern "sdl_evt_is_mouse_button"
47 fun as_mouse_button: SDL_MouseButtonEvent is extern "sdl_evt_as_mouse_button"
48 fun is_mouse_motion: Bool is extern "sdl_evt_is_mouse_motion"
49 fun as_mouse_motion: SDL_MouseMotionEvent is extern "sdl_evt_as_mouse_motion"
50 fun is_expose: Bool is extern "sdl_evt_is_expose"
51 fun is_quit: Bool is extern "sdl_evt_is_quit"
52 end
53
54 extern SDL_ButtonEvent
55 super SDL_Event
56 fun is_pressed: Bool is abstract
57 end
58
59 extern SDL_MouseEvent
60 super SDL_Event
61 fun x: Int is abstract
62 fun y: Int is abstract
63 end
64
65 extern SDL_KeyboardEvent
66 super SDL_ButtonEvent
67 redef fun is_pressed: Bool is extern "sdl_keyboard_evt_state"
68 end
69
70 extern SDL_MouseButtonEvent
71 super SDL_ButtonEvent
72 super SDL_MouseEvent
73 redef fun is_pressed: Bool is extern "sdl_mouse_button_evt_state"
74 redef fun x: Int is extern "sdl_mouse_button_evt_x"
75 redef fun y: Int is extern "sdl_mouse_button_evt_y"
76 fun button: Int is extern "sdl_mouse_button_evt_button"
77 end
78
79 extern SDL_MouseMotionEvent
80 super SDL_MouseEvent
81 redef fun x: Int is extern "sdl_mouse_evt_x"
82 redef fun y: Int is extern "sdl_mouse_evt_y"
83 fun xrel: Int is extern "sdl_mouse_evt_xrel"
84 fun yrel: Int is extern "sdl_mouse_evt_yrel"
85 end
86
87 class SDL_EventListener
88 fun on_keyboard(evt: SDL_KeyboardEvent)
89 do end
90
91 fun on_mouse_button(evt: SDL_MouseButtonEvent)
92 do end
93
94 fun on_mouse_motion(evt: SDL_MouseMotionEvent)
95 do end
96
97 fun on_expose
98 do end
99
100 fun on_quit
101 do end
102 end
103
104 class SDL_EventProcessor
105 var _listeners: Array[SDL_EventListener]
106
107 fun add_listener(l: SDL_EventListener)
108 do
109 _listeners.add(l)
110 end
111
112 fun remove_listener(l: SDL_EventListener)
113 do
114 _listeners.remove(l)
115 end
116
117 fun process_one_event
118 do
119 if sdl_poll_next_event then
120 process_event(sdl_current_event)
121 end
122 end
123
124 fun process_all_events
125 do
126 while sdl_poll_next_event do
127 process_event(sdl_current_event)
128 end
129 end
130
131 private fun process_event(evt: SDL_Event)
132 do
133 var sdl_listeners = _listeners
134 if evt.is_keyboard then
135 for i in sdl_listeners do
136 i.on_keyboard(evt.as_keyboard)
137 end
138 else if evt.is_mouse_button then
139 for i in sdl_listeners do
140 i.on_mouse_button(evt.as_mouse_button)
141 end
142 else if evt.is_mouse_motion then
143 for i in sdl_listeners do
144 i.on_mouse_motion(evt.as_mouse_motion)
145 end
146 else if evt.is_expose then
147 for i in sdl_listeners do
148 i.on_expose
149 end
150 else if evt.is_quit then
151 for i in sdl_listeners do
152 i.on_quit
153 end
154 end
155 end
156
157 init
158 do
159 _listeners = new Array[SDL_EventListener]
160 end
161 end
162
163 # General
164
165 fun sdl_init is extern
166
167 # Video
168
169 fun sdl_set_video_mode(w: Int, h: Int, d: Int): SDL_Screen is extern
170 fun sdl_set_fullscreen_video_mode(w: Int, h: Int, d: Int): SDL_Screen is extern
171
172 fun sdl_load_raw_bmp(s: String): SDL_Surface
173 do
174 return sdl_load_bmp_native(s.to_cstring)
175 end
176 fun sdl_load_bmp(s: String): SDL_Surface
177 do
178 var raw = sdl_load_raw_bmp(s)
179 var sprite = raw.to_display_format
180 raw.free
181 return sprite
182 end
183
184 fun sdl_load_bmp_native(s: NativeString): SDL_Surface is extern
185
186 fun sdl_show_cursor=(b: Bool) is extern "sdl_show_cursor_1"
187 fun sdl_show_cursor: Bool is extern "sdl_show_cursor_0"
188
189 # WM
190
191 fun sdl_grab=(b: Bool) is extern "sdl_grab_1"
192 fun sdl_grab: Bool is extern "sdl_grab_0"
193
194 # Events
195
196 fun sdl_current_event: SDL_Event is extern
197 fun sdl_poll_next_event: Bool is extern
198
199 # Time
200
201 fun sdl_get_ticks: Int is extern
202 fun sdl_delay(ms: Int) is extern