al.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. package al
  2. /*
  3. #cgo pkg-config: allegro-5.0
  4. #cgo CFLAGS: -I/usr/local/include
  5. #cgo linux LDFLAGS: -lc_nonshared
  6. #include <stdlib.h>
  7. #include <allegro5/allegro.h>
  8. #include <allegro5/events.h>
  9. #include "helpers.h"
  10. */
  11. import "C"
  12. // import "unsafe"
  13. import "runtime"
  14. const PI = 3.14159265358979323846
  15. // Allegro library ID calculation.
  16. func AL_ID(a, b, c, d int) int {
  17. return (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
  18. }
  19. const VERSION = 5
  20. const SUB_VERSION = 0
  21. const WIP_VERSION = 7
  22. const RELEASE_NUMBER = 1
  23. const VERSION_STR = "5.0.7"
  24. const DATE_STR = "2012"
  25. const DATE = 20120624 /* yyyymmdd */
  26. const VERSION_INT = ((VERSION << 24) | (SUB_VERSION << 16) |
  27. (WIP_VERSION << 8) | RELEASE_NUMBER)
  28. // Checks if the basic Allegro system is installed or not.
  29. func IsSystemInstalled() bool {
  30. return bool(C.al_is_system_installed())
  31. }
  32. // Gets the raw version of Allegro linked to as an integer.
  33. func GetAllegroVersion() uint32 {
  34. return uint32(C.al_get_allegro_version())
  35. }
  36. // Initializes the Allegro system.
  37. func Initialize() bool {
  38. return bool(C.algo_initialize())
  39. }
  40. // Cleans up the Allegro system. Needed after calling Initialize.
  41. func Cleanup() {
  42. C.algo_atexit_cleanup()
  43. }
  44. // Installs the Allegro system.
  45. func InstallSystem() bool {
  46. return bool(C.al_install_system(VERSION_INT, nil))
  47. }
  48. // Uninstalls the Allegro system. Must be called after using InstallSystem.
  49. func UninstallSystem() {
  50. C.al_uninstall_system()
  51. }
  52. // allegro5/path.h
  53. // Wrapper for an Allegro path.
  54. type Path struct {
  55. handle *C.ALLEGRO_PATH
  56. }
  57. // Wraps an Allegro path into the go struct above, but does not set a finalizer
  58. func wrapPathRaw(handle *C.ALLEGRO_PATH) *Path {
  59. if handle == nil {
  60. return nil
  61. }
  62. return &Path{handle}
  63. }
  64. // Wraps an Allegro path into the go struct above, and sets the finalizer
  65. // to be the struct's Destroy method
  66. func wrapPath(handle *C.ALLEGRO_PATH) *Path {
  67. result := wrapPathRaw(handle)
  68. if result != nil {
  69. runtime.SetFinalizer(result, func(me *Path) { me.Destroy() })
  70. }
  71. return result
  72. }
  73. // Creates an Allegro path.
  74. func CreatePath(str string) *Path {
  75. cstr := cstr(str)
  76. defer cstrFree(cstr)
  77. return wrapPath(C.al_create_path(cstr))
  78. }
  79. // Creates an allegro path for a directory.
  80. func CreatePathForDirectory(str string) *Path {
  81. cstr := cstr(str)
  82. defer cstrFree(cstr)
  83. return wrapPath(C.al_create_path_for_directory(cstr))
  84. }
  85. // Clones an allegro path.
  86. func (self *Path) ClonePath() *Path {
  87. return wrapPath(C.al_clone_path(self.handle))
  88. }
  89. // Destroys an Allegro path. It may not be used after this.
  90. // Destroy may be called many times.
  91. func (self *Path) Destroy() {
  92. if self.handle != nil {
  93. C.al_destroy_path(self.handle)
  94. }
  95. self.handle = nil
  96. }
  97. // Gets amount of components of the path name
  98. func (self *Path) GetPathNumComponents() int {
  99. return int(C.al_get_path_num_components(self.handle))
  100. }
  101. // converst the allegro path to a string
  102. func (self *Path) String() string {
  103. return gostr(C.al_path_cstr(self.handle, C.char(NATIVE_PATH_SEP)))
  104. }
  105. /*
  106. func (self * Path)
  107. AL_FUNC(int, al_get_path_num_components, (const ALLEGRO_PATH *path));
  108. AL_FUNC(const char*, al_get_path_component, (const ALLEGRO_PATH *path, int i));
  109. AL_FUNC(void, al_replace_path_component, (ALLEGRO_PATH *path, int i, const char *s));
  110. AL_FUNC(void, al_remove_path_component, (ALLEGRO_PATH *path, int i));
  111. AL_FUNC(void, al_insert_path_component, (ALLEGRO_PATH *path, int i, const char *s));
  112. AL_FUNC(const char*, al_get_path_tail, (const ALLEGRO_PATH *path));
  113. AL_FUNC(void, al_drop_path_tail, (ALLEGRO_PATH *path));
  114. AL_FUNC(void, al_append_path_component, (ALLEGRO_PATH *path, const char *s));
  115. AL_FUNC(bool, al_join_paths, (ALLEGRO_PATH *path, const ALLEGRO_PATH *tail));
  116. AL_FUNC(bool, al_rebase_path, (const ALLEGRO_PATH *head, ALLEGRO_PATH *tail));
  117. AL_FUNC(const char*, al_path_cstr, (const ALLEGRO_PATH *path, char delim));
  118. AL_FUNC(void, al_destroy_path, (ALLEGRO_PATH *path));
  119. AL_FUNC(void, al_set_path_drive, (ALLEGRO_PATH *path, const char *drive));
  120. AL_FUNC(const char*, al_get_path_drive, (const ALLEGRO_PATH *path));
  121. AL_FUNC(void, al_set_path_filename, (ALLEGRO_PATH *path, const char *filename));
  122. AL_FUNC(const char*, al_get_path_filename, (const ALLEGRO_PATH *path));
  123. AL_FUNC(const char*, al_get_path_extension, (const ALLEGRO_PATH *path));
  124. AL_FUNC(bool, al_set_path_extension, (ALLEGRO_PATH *path, char const *extension));
  125. AL_FUNC(const char*, al_get_path_basename, (const ALLEGRO_PATH *path));
  126. AL_FUNC(bool, al_make_path_canonical, (ALLEGRO_PATH *path));
  127. // defer
  128. */
  129. // Not wrapped yet:
  130. // AL_FUNC(SYSTEM *, al_get_system_driver, (void));
  131. // AL_FUNC(CONFIG *, al_get_system_config, (void));
  132. const (
  133. RESOURCES_PATH = iota
  134. TEMP_PATH
  135. USER_DATA_PATH
  136. USER_HOME_PATH
  137. USER_SETTINGS_PATH
  138. USER_DOCUMENTS_PATH
  139. EXENAME_PATH
  140. LAST_PATH
  141. )
  142. // Gets a standard path location.
  143. func GetStandardPath(id int) *Path {
  144. return wrapPath(C.al_get_standard_path(C.int(id)))
  145. }
  146. // Sets the name of the executable.
  147. func SetExeName(name string) {
  148. C.al_set_exe_name(cstr(name))
  149. }
  150. // Sets the name of the organisation.
  151. func SetOrgName(name string) {
  152. C.al_set_org_name(cstr(name))
  153. }
  154. // Sets the name of the app.
  155. func SetAppName(name string) {
  156. C.al_set_app_name(cstr(name))
  157. }
  158. // Gets the name of the organisation
  159. func GetOrgName() string {
  160. return gostr(C.al_get_org_name())
  161. }
  162. // Sets the name of the app
  163. func GetAppName() string {
  164. return gostr(C.al_get_app_name())
  165. }
  166. // Inibits the screensaver, or not debending on inhibit.
  167. func InhibitScreensaver(inhibit bool) bool {
  168. return bool(C.al_inhibit_screensaver(C.bool(inhibit)))
  169. }
  170. /// XXX How to wrap this and is it needed????
  171. // AL_FUNC(int, al_run_main, (int argc, char **argv, int (*)(int, char **)));
  172. /** Allegro has it's own string type. While it's nice, it's
  173. not needed in Go, so I will just wrap the basic conversion functions. */
  174. type USTR struct {
  175. handle *C.ALLEGRO_USTR
  176. }
  177. // Frees an Allegro unicode string.
  178. func (self *USTR) Free() {
  179. if self.handle != nil {
  180. C.al_ustr_free(self.handle)
  181. }
  182. self.handle = nil
  183. }
  184. // Just for consistency and to allow SelfDestruuct to work
  185. func (self *USTR) Destroy() {
  186. self.Free()
  187. }
  188. // Converts an Allegro Unicode string to a Go string
  189. func (self *USTR) String() string {
  190. if self.handle == nil {
  191. return "<destroyed>"
  192. }
  193. return C.GoStringN(C.al_cstr(self.handle), C.int(C.al_ustr_size(self.handle)))
  194. }
  195. // Wraps an Allegro USTR into the go struct above, but does not set a finalizer
  196. func wrapUSTRRaw(handle *C.ALLEGRO_USTR) *USTR {
  197. if handle == nil {
  198. return nil
  199. }
  200. return &USTR{handle}
  201. }
  202. // Wraps an Allegro path into the go struct above, and sets the finalizer to
  203. // be the Destroy method of that struct.
  204. func wrapUSTR(handle *C.ALLEGRO_USTR) *USTR {
  205. result := wrapUSTRRaw(handle)
  206. if result != nil {
  207. runtime.SetFinalizer(result, func(me *USTR) { me.Destroy() })
  208. }
  209. return result
  210. }
  211. // Converts a go string to an Allegro Unicode string
  212. func USTRV(str string) *USTR {
  213. cstr := cstr(str)
  214. defer cstrFree(cstr)
  215. return wrapUSTR(C.al_ustr_new(cstr))
  216. }
  217. // Converts a go string to an Allegro Unicode string
  218. func USTRP(str *string) *USTR {
  219. return USTRV(*str)
  220. }
  221. // Allegro's timer functions
  222. // Gets the time the app is running in seconds
  223. func GetTime() float64 {
  224. return float64(C.al_get_time())
  225. }
  226. // Sleeps the given amount of seconds
  227. func Rest(seconds float64) {
  228. C.al_rest(C.double(seconds))
  229. }
  230. // Event Type, not to avoid complications.
  231. // type EVENT_TYPE C.ALLEGRO_EVENT_TYPE
  232. // Event Type constants
  233. const (
  234. EVENT_JOYSTICK_AXIS = C.ALLEGRO_EVENT_JOYSTICK_AXIS
  235. EVENT_JOYSTICK_BUTTON_DOWN = C.ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN
  236. EVENT_JOYSTICK_BUTTON_UP = C.ALLEGRO_EVENT_JOYSTICK_BUTTON_UP
  237. EVENT_JOYSTICK_CONFIGURATION = C.ALLEGRO_EVENT_JOYSTICK_CONFIGURATION
  238. EVENT_KEY_DOWN = C.ALLEGRO_EVENT_KEY_DOWN
  239. EVENT_KEY_CHAR = C.ALLEGRO_EVENT_KEY_CHAR
  240. EVENT_KEY_UP = C.ALLEGRO_EVENT_KEY_UP
  241. EVENT_MOUSE_AXES = C.ALLEGRO_EVENT_MOUSE_AXES
  242. EVENT_MOUSE_BUTTON_DOWN = C.ALLEGRO_EVENT_MOUSE_BUTTON_DOWN
  243. EVENT_MOUSE_BUTTON_UP = C.ALLEGRO_EVENT_MOUSE_BUTTON_UP
  244. EVENT_MOUSE_ENTER_DISPLAY = C.ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY
  245. EVENT_MOUSE_LEAVE_DISPLAY = C.ALLEGRO_EVENT_MOUSE_LEAVE_DISPLAY
  246. EVENT_MOUSE_WARPED = C.ALLEGRO_EVENT_MOUSE_WARPED
  247. EVENT_TIMER = C.ALLEGRO_EVENT_TIMER
  248. EVENT_DISPLAY_EXPOSE = C.ALLEGRO_EVENT_DISPLAY_EXPOSE
  249. EVENT_DISPLAY_RESIZE = C.ALLEGRO_EVENT_DISPLAY_RESIZE
  250. EVENT_DISPLAY_CLOSE = C.ALLEGRO_EVENT_DISPLAY_CLOSE
  251. EVENT_DISPLAY_LOST = C.ALLEGRO_EVENT_DISPLAY_LOST
  252. EVENT_DISPLAY_FOUND = C.ALLEGRO_EVENT_DISPLAY_FOUND
  253. EVENT_DISPLAY_SWITCH_IN = C.ALLEGRO_EVENT_DISPLAY_SWITCH_IN
  254. EVENT_DISPLAY_SWITCH_OUT = C.ALLEGRO_EVENT_DISPLAY_SWITCH_OUT
  255. EVENT_DISPLAY_ORIENTATION = C.ALLEGRO_EVENT_DISPLAY_ORIENTATION
  256. )
  257. func EVENT_TYPE_IS_USER(t int) bool {
  258. return ((t) >= 512)
  259. }
  260. func GET_EVENT_TYPE(a, b, c, d int) int {
  261. return AL_ID(a, b, c, d)
  262. }
  263. func getAnyEvenTimestamp(any *C.ALLEGRO_ANY_EVENT) float64 {
  264. return float64(any.timestamp)
  265. }
  266. // Event sources that emit events.
  267. type EventSource C.ALLEGRO_EVENT_SOURCE
  268. // Converts wrapper Event pointer to C Allegro event pointer
  269. func (self *EventSource) toC() *C.ALLEGRO_EVENT_SOURCE {
  270. return (*C.ALLEGRO_EVENT_SOURCE)(self)
  271. }
  272. // Events that the event system emits.
  273. type Event C.ALLEGRO_EVENT
  274. // Converts wrapper Event pointer to C Allegro event pointer
  275. func (self *Event) toC() *C.ALLEGRO_EVENT {
  276. return (*C.ALLEGRO_EVENT)(self)
  277. }
  278. // Returns the type of the event.
  279. func (self *Event) Type() int {
  280. return int(C.algo_event_type(self.toC()))
  281. }
  282. // Returns the timestamp of the event.
  283. func (self *Event) Timestamp() float64 {
  284. return float64(C.algo_event_any(self.toC()).timestamp)
  285. }
  286. // Returns the event source of the event
  287. func (self *Event) EventSource() *EventSource {
  288. return (*EventSource)(C.algo_event_any(self.toC()).source)
  289. }
  290. // Returns true if this is a dispay event, false if not.
  291. func (self *Event) IsDisplay() bool {
  292. t := self.Type()
  293. return (t >= EVENT_DISPLAY_EXPOSE) && (t <= EVENT_DISPLAY_ORIENTATION)
  294. }
  295. // Returns true if this is a mouse event, false if not.
  296. func (self *Event) IsMouse() bool {
  297. t := self.Type()
  298. return (t >= EVENT_MOUSE_AXES) && (t <= EVENT_MOUSE_WARPED)
  299. }
  300. // Returns true if this is a Joystick event, false if not.
  301. func (self *Event) IsJoystick() bool {
  302. t := self.Type()
  303. return (t >= EVENT_JOYSTICK_AXIS) && (t <= EVENT_JOYSTICK_CONFIGURATION)
  304. }
  305. // Returns true if this is a keyboard event, false if not.
  306. func (self *Event) IsKeyboard() bool {
  307. t := self.Type()
  308. return (t >= EVENT_KEY_DOWN) && (t <= EVENT_KEY_UP)
  309. }
  310. // Returns true if this is a timer event, false if not.
  311. func (self *Event) IsTimer() bool {
  312. t := self.Type()
  313. return (t == EVENT_TIMER)
  314. }
  315. // Returns true if this is a user event, false if not.
  316. func (self *Event) IsUser() bool {
  317. t := self.Type()
  318. return EVENT_TYPE_IS_USER(t)
  319. }
  320. type Display C.ALLEGRO_DISPLAY
  321. // Returns the display that has emitted the event. Will return nil if
  322. // this is not a display event.
  323. func (self *Event) DisplayDisplay() *Display {
  324. if !(self.IsDisplay()) {
  325. return nil
  326. }
  327. return (*Display)(C.algo_event_display(self.toC()).source)
  328. }
  329. // Returns the X position of the display event. Will return garbage
  330. // if this is not a display event.
  331. func (self *Event) DisplayX() int {
  332. return int(C.algo_event_display(self.toC()).x)
  333. }
  334. // Returns the Y position of the display event. Will return garbage
  335. // if this is not a display event.
  336. func (self *Event) DisplayY() int {
  337. return int(C.algo_event_display(self.toC()).y)
  338. }
  339. // Returns the width of the display event. Will return garbage
  340. // if this is not a display event.
  341. func (self *Event) DisplayWidth() int {
  342. return int(C.algo_event_display(self.toC()).width)
  343. }
  344. // Returns the height of the display event. Will return garbage
  345. // if this is not a display event.
  346. func (self *Event) DisplayHeight() int {
  347. return int(C.algo_event_display(self.toC()).height)
  348. }
  349. // Returns the orientation of the display event. Will return garbage
  350. // if this is not a display event.
  351. func (self *Event) DisplayOrientation() int {
  352. return int(C.algo_event_display(self.toC()).orientation)
  353. }
  354. // XXX: maybe also wrap the source in a Joystick type?
  355. // Returns the stick number of the joystick event. Will return garbage
  356. // if this is not a joystick event.
  357. func (self *Event) JoystickStick() int {
  358. return int(C.algo_event_joystick(self.toC()).stick)
  359. }
  360. // Returns the axis number of the joystick event. Will return garbage
  361. // if this is not a joystick event.
  362. func (self *Event) JoystickAxis() int {
  363. return int(C.algo_event_joystick(self.toC()).axis)
  364. }
  365. // Returns the button number of the joystick event. Will return garbage
  366. // if this is not a joystick event.
  367. func (self *Event) JoystickButton() int {
  368. return int(C.algo_event_joystick(self.toC()).button)
  369. }
  370. // Returns the position of the joystick event. Will return garbage
  371. // if this is not a joystick event.
  372. func (self *Event) JoystickPos() float32 {
  373. return float32(C.algo_event_joystick(self.toC()).pos)
  374. }
  375. /// XXX also wrap Keyboard event source?
  376. // Returns the display that has emitted the keyboard event. Will return nil if
  377. // this is not a keyboard event.
  378. func (self *Event) KeyboardDisplay() *Display {
  379. if !(self.IsKeyboard()) {
  380. return nil
  381. }
  382. return (*Display)(C.algo_event_keyboard(self.toC()).display)
  383. }
  384. // Returns the keycode of the keyboard event. Returns garbage
  385. // if this is not a keyboard event.
  386. func (self *Event) KeyboardKeycode() int {
  387. return int(C.algo_event_keyboard(self.toC()).keycode)
  388. }
  389. // Returns the unichar of the keyboard event. Returns garbage
  390. // if this is not a keyboard event.
  391. func (self *Event) KeyboardUnichar() rune {
  392. return rune(C.algo_event_keyboard(self.toC()).unichar)
  393. }
  394. // Returns the modifiers of the keyboard event. Returns garbage
  395. // if this is not a keyboard event.
  396. func (self *Event) KeyboardModifiers() int {
  397. return int(C.algo_event_keyboard(self.toC()).modifiers)
  398. }
  399. // Returns is the keyboard event was autorepeated or not. Returns garbage
  400. // if this is not a keyboard event.
  401. func (self *Event) KeyboardRepeat() bool {
  402. return bool(C.algo_event_keyboard(self.toC()).repeat)
  403. }
  404. // Returns the x postion of the mouse event. Returns garbage
  405. // if this is not a mouse event.
  406. func (self *Event) MouseX() int {
  407. return int(C.algo_event_mouse(self.toC()).x)
  408. }
  409. // Returns the y postion of the mouse event. Returns garbage
  410. // if this is not a mouse event.
  411. func (self *Event) MouseY() int {
  412. return int(C.algo_event_mouse(self.toC()).y)
  413. }
  414. // Returns the z postion of the mouse event. Returns garbage
  415. // if this is not a mouse event.
  416. func (self *Event) MouseZ() int {
  417. return int(C.algo_event_mouse(self.toC()).z)
  418. }
  419. // Returns the w postion of the mouse event. Returns garbage
  420. // if this is not a mouse event.
  421. func (self *Event) MouseW() int {
  422. return int(C.algo_event_mouse(self.toC()).w)
  423. }
  424. // Returns the dx of the mouse event. Returns garbage
  425. // if this is not a mouse event.
  426. func (self *Event) MouseDX() int {
  427. return int(C.algo_event_mouse(self.toC()).dx)
  428. }
  429. // Returns the dy of the mouse event. Returns garbage
  430. // if this is not a mouse event.
  431. func (self *Event) MouseDY() int {
  432. return int(C.algo_event_mouse(self.toC()).dy)
  433. }
  434. // Returns the dz of the mouse event. Returns garbage
  435. // if this is not a mouse event.
  436. func (self *Event) MouseDZ() int {
  437. return int(C.algo_event_mouse(self.toC()).dz)
  438. }
  439. // Returns the dw of the mouse event. Returns garbage
  440. // if this is not a mouse event.
  441. func (self *Event) MouseDW() int {
  442. return int(C.algo_event_mouse(self.toC()).dw)
  443. }
  444. // Returns the button of the mouse event. Returns garbage
  445. // if this is not a mouse event.
  446. func (self *Event) MouseButton() int {
  447. return int(C.algo_event_mouse(self.toC()).button)
  448. }
  449. // Returns the pressure of the mouse event. Returns garbage
  450. // if this is not a mouse event.
  451. func (self *Event) MousePressure() float32 {
  452. return float32(C.algo_event_mouse(self.toC()).pressure)
  453. }
  454. // Returns the display that has emitted the mouse event. Will return nil if
  455. // this is not a mouse event.
  456. func (self *Event) MouseDisplay() *Display {
  457. if !(self.IsMouse()) {
  458. return nil
  459. }
  460. return (*Display)(C.algo_event_mouse(self.toC()).display)
  461. }
  462. // Returns the error of the timer event. Returns garbage
  463. // if this is not a timer event.
  464. func (self *Event) TimerError() float64 {
  465. return float64(C.algo_event_timer(self.toC()).error)
  466. }
  467. // Returns the ticks of the timer event. Returns garbage
  468. // if this is not a timer event.
  469. func (self *Event) TimerCount() int64 {
  470. return int64(C.algo_event_timer(self.toC()).count)
  471. }
  472. // Wrapping of user event seems not really meaningful in Go so leave that out.
  473. // Event queues.
  474. type EventQueue struct {
  475. handle *C.ALLEGRO_EVENT_QUEUE
  476. }
  477. // Destroys the event queue.
  478. func (self *EventQueue) Destroy() {
  479. if self.handle != nil {
  480. C.al_destroy_event_queue(self.handle)
  481. }
  482. self.handle = nil
  483. }
  484. // Wraps an event queue, but does not set a finalizer.
  485. func wrapEventQueueRaw(handle *C.ALLEGRO_EVENT_QUEUE) *EventQueue {
  486. if handle == nil {
  487. return nil
  488. }
  489. return &EventQueue{handle}
  490. }
  491. // Wraps an event queue and sets a finalizer that calls Destroy
  492. func wrapEventQueue(handle *C.ALLEGRO_EVENT_QUEUE) *EventQueue {
  493. result := wrapEventQueueRaw(handle)
  494. if result != nil {
  495. runtime.SetFinalizer(result, func(me *EventQueue) { me.Destroy() })
  496. }
  497. return result
  498. }
  499. // Create an event queue.
  500. func CreateEventQueue() *EventQueue {
  501. return wrapEventQueue(C.al_create_event_queue())
  502. }
  503. // Register an event source with self.
  504. func (self *EventQueue) RegisterEventSource(src *EventSource) {
  505. C.al_register_event_source(self.handle, src.toC())
  506. }
  507. // Unregister an event source with self.
  508. func (self *EventQueue) UnregisterEventSource(src *EventSource) {
  509. C.al_unregister_event_source(self.handle, src.toC())
  510. }
  511. // Returns true if the event queue self is empty, false if not.
  512. func (self *EventQueue) IsEmpty() bool {
  513. return bool(C.al_is_event_queue_empty(self.handle))
  514. }
  515. // Returns the next event from the event queue as well as a bool
  516. // to signify if an event was fetched sucessfully or not.
  517. func (self *EventQueue) GetNextEvent() (event *Event, ok bool) {
  518. event = &Event{}
  519. ok = bool(C.al_get_next_event(self.handle, event.toC()))
  520. return event, ok
  521. }
  522. // Peeks at the next event in the event queue and returns it as well as a bool
  523. // to signify if an event was fetched sucessfully or not.
  524. func (self *EventQueue) PeekNextEvent() (event *Event, ok bool) {
  525. event = &Event{}
  526. ok = bool(C.al_peek_next_event(self.handle, event.toC()))
  527. return event, ok
  528. }
  529. // Drops the next event from the event queue
  530. func (self *EventQueue) DropNextEvent() bool {
  531. return bool(C.al_drop_next_event(self.handle))
  532. }
  533. // Flushes the event queue
  534. func (self *EventQueue) Flush() {
  535. C.al_flush_event_queue(self.handle)
  536. }
  537. // Waits for the next event from the event queue
  538. func (self *EventQueue) WaitForEvent() (event *Event) {
  539. event = &Event{}
  540. C.al_wait_for_event(self.handle, event.toC())
  541. return event
  542. }
  543. // Waits for secs seconds the next event from the event queue
  544. func (self *EventQueue) WaitForEventTimed(secs float32) (event *Event, ok bool) {
  545. event = &Event{}
  546. ok = bool(C.al_wait_for_event_timed(self.handle, event.toC(), C.float(secs)))
  547. return event, ok
  548. }
  549. /*
  550. // Emitting user events is omitted for now.
  551. TODO:
  552. AL_FUNC(bool, al_wait_for_event_until, (ALLEGRO_EVENT_QUEUE *queue,
  553. ALLEGRO_EVENT *ret_event,
  554. ALLEGRO_TIMEOUT *timeout));
  555. */
  556. // Precise (?) Timer
  557. type Timer struct {
  558. handle *C.ALLEGRO_TIMER
  559. }
  560. // Destroys the timer queue.
  561. func (self *Timer) Destroy() {
  562. if self.handle != nil {
  563. C.al_destroy_timer(self.handle)
  564. }
  565. self.handle = nil
  566. }
  567. // Wraps a timer, but does not set a finalizer.
  568. func wrapTimerRaw(handle *C.ALLEGRO_TIMER) *Timer {
  569. if handle == nil {
  570. return nil
  571. }
  572. return &Timer{handle}
  573. }
  574. // Wraps an event queue and sets a finalizer that calls Destroy
  575. func wrapTimer(handle *C.ALLEGRO_TIMER) *Timer {
  576. result := wrapTimerRaw(handle)
  577. if result != nil {
  578. runtime.SetFinalizer(result, func(me *Timer) { me.Destroy() })
  579. }
  580. return result
  581. }
  582. // Creates a timer wih the given tick speed.
  583. func CreateTimer(speed_secs float64) *Timer {
  584. return wrapTimer(C.al_create_timer(C.double(speed_secs)))
  585. }
  586. // Starts the timer.
  587. func (self *Timer) Start() {
  588. C.al_start_timer(self.handle)
  589. }
  590. // Stops the timer.
  591. func (self *Timer) Stop() {
  592. C.al_stop_timer(self.handle)
  593. }
  594. // Returns true if the timer was started, false if not
  595. func (self *Timer) IsStarted() bool {
  596. return bool(C.al_get_timer_started(self.handle))
  597. }
  598. // Sets the speed of the timer.
  599. func (self *Timer) SetSpeed(speed_secs float64) {
  600. C.al_set_timer_speed(self.handle, C.double(speed_secs))
  601. }
  602. // Gets the speed of the timer.
  603. func (self *Timer) GetSpeed() float64 {
  604. return float64(C.al_get_timer_speed(self.handle))
  605. }
  606. // Gets the count (in ticks) of the timer
  607. func (self *Timer) GetCount() int {
  608. return int(C.al_get_timer_count(self.handle))
  609. }
  610. // Sets the count (in ticks) of the timer
  611. func (self *Timer) SetCount(count int) {
  612. C.al_set_timer_count(self.handle, C.int64_t(count))
  613. }
  614. // Adds to the count (in ticks) of the timer
  615. func (self *Timer) AddCount(count int) {
  616. C.al_add_timer_count(self.handle, C.int64_t(count))
  617. }
  618. // Gets the event source of this timer that can be registered
  619. // on an event queue with RegisterEventSource.
  620. func (self *Timer) GetEventSource() *EventSource {
  621. return (*EventSource)(C.al_get_timer_event_source(self.handle))
  622. }