al.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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 = 1
  21. const WIP_VERSION = 5
  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.al_install_system(VERSION_INT, nil))
  39. // return bool(C.algo_initialize())
  40. }
  41. // Cleans up the Allegro system. Needed after calling Initialize.
  42. func Cleanup() {
  43. C.algo_atexit_cleanup()
  44. }
  45. // Installs the Allegro system.
  46. func InstallSystem() bool {
  47. return bool(C.al_install_system(VERSION_INT, nil))
  48. }
  49. // Uninstalls the Allegro system. Must be called after using InstallSystem.
  50. func UninstallSystem() {
  51. C.al_uninstall_system()
  52. }
  53. // allegro5/path.h
  54. // Wrapper for an Allegro path.
  55. type Path struct {
  56. handle *C.ALLEGRO_PATH
  57. }
  58. // Wraps an Allegro path into the go struct above, but does not set a finalizer
  59. func wrapPathRaw(handle *C.ALLEGRO_PATH) *Path {
  60. if handle == nil {
  61. return nil
  62. }
  63. return &Path{handle}
  64. }
  65. // Wraps an Allegro path into the go struct above, and sets the finalizer
  66. // to be the struct's Destroy method
  67. func wrapPath(handle *C.ALLEGRO_PATH) *Path {
  68. result := wrapPathRaw(handle)
  69. if result != nil {
  70. runtime.SetFinalizer(result, func(me *Path) { me.Destroy() })
  71. }
  72. return result
  73. }
  74. // Creates an Allegro path.
  75. func CreatePath(str string) *Path {
  76. cstr := cstr(str)
  77. defer cstrFree(cstr)
  78. return wrapPath(C.al_create_path(cstr))
  79. }
  80. // Creates an allegro path for a directory.
  81. func CreatePathForDirectory(str string) *Path {
  82. cstr := cstr(str)
  83. defer cstrFree(cstr)
  84. return wrapPath(C.al_create_path_for_directory(cstr))
  85. }
  86. // Clones an allegro path.
  87. func (self *Path) ClonePath() *Path {
  88. return wrapPath(C.al_clone_path(self.handle))
  89. }
  90. // Destroys an Allegro path. It may not be used after this.
  91. // Destroy may be called many times.
  92. func (self *Path) Destroy() {
  93. if self.handle != nil {
  94. C.al_destroy_path(self.handle)
  95. }
  96. self.handle = nil
  97. }
  98. // Gets amount of components of the path name
  99. func (self *Path) GetPathNumComponents() int {
  100. return int(C.al_get_path_num_components(self.handle))
  101. }
  102. // converst the allegro path to a string
  103. func (self *Path) String() string {
  104. return gostr(C.al_path_cstr(self.handle, C.char(NATIVE_PATH_SEP)))
  105. }
  106. /*
  107. func (self * Path)
  108. AL_FUNC(int, al_get_path_num_components, (const ALLEGRO_PATH *path));
  109. AL_FUNC(const char*, al_get_path_component, (const ALLEGRO_PATH *path, int i));
  110. AL_FUNC(void, al_replace_path_component, (ALLEGRO_PATH *path, int i, const char *s));
  111. AL_FUNC(void, al_remove_path_component, (ALLEGRO_PATH *path, int i));
  112. AL_FUNC(void, al_insert_path_component, (ALLEGRO_PATH *path, int i, const char *s));
  113. AL_FUNC(const char*, al_get_path_tail, (const ALLEGRO_PATH *path));
  114. AL_FUNC(void, al_drop_path_tail, (ALLEGRO_PATH *path));
  115. AL_FUNC(void, al_append_path_component, (ALLEGRO_PATH *path, const char *s));
  116. AL_FUNC(bool, al_join_paths, (ALLEGRO_PATH *path, const ALLEGRO_PATH *tail));
  117. AL_FUNC(bool, al_rebase_path, (const ALLEGRO_PATH *head, ALLEGRO_PATH *tail));
  118. AL_FUNC(const char*, al_path_cstr, (const ALLEGRO_PATH *path, char delim));
  119. AL_FUNC(void, al_destroy_path, (ALLEGRO_PATH *path));
  120. AL_FUNC(void, al_set_path_drive, (ALLEGRO_PATH *path, const char *drive));
  121. AL_FUNC(const char*, al_get_path_drive, (const ALLEGRO_PATH *path));
  122. AL_FUNC(void, al_set_path_filename, (ALLEGRO_PATH *path, const char *filename));
  123. AL_FUNC(const char*, al_get_path_filename, (const ALLEGRO_PATH *path));
  124. AL_FUNC(const char*, al_get_path_extension, (const ALLEGRO_PATH *path));
  125. AL_FUNC(bool, al_set_path_extension, (ALLEGRO_PATH *path, char const *extension));
  126. AL_FUNC(const char*, al_get_path_basename, (const ALLEGRO_PATH *path));
  127. AL_FUNC(bool, al_make_path_canonical, (ALLEGRO_PATH *path));
  128. // defer
  129. */
  130. // Not wrapped yet:
  131. // AL_FUNC(SYSTEM *, al_get_system_driver, (void));
  132. // AL_FUNC(CONFIG *, al_get_system_config, (void));
  133. const (
  134. RESOURCES_PATH = iota
  135. TEMP_PATH
  136. USER_DATA_PATH
  137. USER_HOME_PATH
  138. USER_SETTINGS_PATH
  139. USER_DOCUMENTS_PATH
  140. EXENAME_PATH
  141. LAST_PATH
  142. )
  143. // Gets a standard path location.
  144. func GetStandardPath(id int) *Path {
  145. return wrapPath(C.al_get_standard_path(C.int(id)))
  146. }
  147. // Sets the name of the executable.
  148. func SetExeName(name string) {
  149. C.al_set_exe_name(cstr(name))
  150. }
  151. // Sets the name of the organisation.
  152. func SetOrgName(name string) {
  153. C.al_set_org_name(cstr(name))
  154. }
  155. // Sets the name of the app.
  156. func SetAppName(name string) {
  157. C.al_set_app_name(cstr(name))
  158. }
  159. // Gets the name of the organisation
  160. func GetOrgName() string {
  161. return gostr(C.al_get_org_name())
  162. }
  163. // Sets the name of the app
  164. func GetAppName() string {
  165. return gostr(C.al_get_app_name())
  166. }
  167. // Inibits the screensaver, or not debending on inhibit.
  168. func InhibitScreensaver(inhibit bool) bool {
  169. return bool(C.al_inhibit_screensaver(C.bool(inhibit)))
  170. }
  171. /// XXX How to wrap this and is it needed????
  172. // AL_FUNC(int, al_run_main, (int argc, char **argv, int (*)(int, char **)));
  173. /** Allegro has it's own string type. While it's nice, it's
  174. not needed in Go, so I will just wrap the basic conversion functions. */
  175. type USTR struct {
  176. handle *C.ALLEGRO_USTR
  177. }
  178. // Frees an Allegro unicode string.
  179. func (self *USTR) Free() {
  180. if self.handle != nil {
  181. C.al_ustr_free(self.handle)
  182. }
  183. self.handle = nil
  184. }
  185. // Just for consistency and to allow SelfDestruuct to work
  186. func (self *USTR) Destroy() {
  187. self.Free()
  188. }
  189. // Converts an Allegro Unicode string to a Go string
  190. func (self *USTR) String() string {
  191. if self.handle == nil {
  192. return "<destroyed>"
  193. }
  194. return C.GoStringN(C.al_cstr(self.handle), C.int(C.al_ustr_size(self.handle)))
  195. }
  196. // Wraps an Allegro USTR into the go struct above, but does not set a finalizer
  197. func wrapUSTRRaw(handle *C.ALLEGRO_USTR) *USTR {
  198. if handle == nil {
  199. return nil
  200. }
  201. return &USTR{handle}
  202. }
  203. // Wraps an Allegro path into the go struct above, and sets the finalizer to
  204. // be the Destroy method of that struct.
  205. func wrapUSTR(handle *C.ALLEGRO_USTR) *USTR {
  206. result := wrapUSTRRaw(handle)
  207. if result != nil {
  208. runtime.SetFinalizer(result, func(me *USTR) { me.Destroy() })
  209. }
  210. return result
  211. }
  212. // Converts a go string to an Allegro Unicode string
  213. func USTRV(str string) *USTR {
  214. cstr := cstr(str)
  215. defer cstrFree(cstr)
  216. return wrapUSTR(C.al_ustr_new(cstr))
  217. }
  218. // Converts a go string to an Allegro Unicode string
  219. func USTRP(str *string) *USTR {
  220. return USTRV(*str)
  221. }
  222. // Allegro's timer functions
  223. // Gets the time the app is running in seconds
  224. func GetTime() float64 {
  225. return float64(C.al_get_time())
  226. }
  227. // Sleeps the given amount of seconds
  228. func Rest(seconds float64) {
  229. C.al_rest(C.double(seconds))
  230. }
  231. // Event Type, not to avoid complications.
  232. // type EVENT_TYPE C.ALLEGRO_EVENT_TYPE
  233. // Event Type constants
  234. const (
  235. EVENT_JOYSTICK_AXIS = C.ALLEGRO_EVENT_JOYSTICK_AXIS
  236. EVENT_JOYSTICK_BUTTON_DOWN = C.ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN
  237. EVENT_JOYSTICK_BUTTON_UP = C.ALLEGRO_EVENT_JOYSTICK_BUTTON_UP
  238. EVENT_JOYSTICK_CONFIGURATION = C.ALLEGRO_EVENT_JOYSTICK_CONFIGURATION
  239. EVENT_KEY_DOWN = C.ALLEGRO_EVENT_KEY_DOWN
  240. EVENT_KEY_CHAR = C.ALLEGRO_EVENT_KEY_CHAR
  241. EVENT_KEY_UP = C.ALLEGRO_EVENT_KEY_UP
  242. EVENT_MOUSE_AXES = C.ALLEGRO_EVENT_MOUSE_AXES
  243. EVENT_MOUSE_BUTTON_DOWN = C.ALLEGRO_EVENT_MOUSE_BUTTON_DOWN
  244. EVENT_MOUSE_BUTTON_UP = C.ALLEGRO_EVENT_MOUSE_BUTTON_UP
  245. EVENT_MOUSE_ENTER_DISPLAY = C.ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY
  246. EVENT_MOUSE_LEAVE_DISPLAY = C.ALLEGRO_EVENT_MOUSE_LEAVE_DISPLAY
  247. EVENT_MOUSE_WARPED = C.ALLEGRO_EVENT_MOUSE_WARPED
  248. EVENT_TIMER = C.ALLEGRO_EVENT_TIMER
  249. EVENT_DISPLAY_EXPOSE = C.ALLEGRO_EVENT_DISPLAY_EXPOSE
  250. EVENT_DISPLAY_RESIZE = C.ALLEGRO_EVENT_DISPLAY_RESIZE
  251. EVENT_DISPLAY_CLOSE = C.ALLEGRO_EVENT_DISPLAY_CLOSE
  252. EVENT_DISPLAY_LOST = C.ALLEGRO_EVENT_DISPLAY_LOST
  253. EVENT_DISPLAY_FOUND = C.ALLEGRO_EVENT_DISPLAY_FOUND
  254. EVENT_DISPLAY_SWITCH_IN = C.ALLEGRO_EVENT_DISPLAY_SWITCH_IN
  255. EVENT_DISPLAY_SWITCH_OUT = C.ALLEGRO_EVENT_DISPLAY_SWITCH_OUT
  256. EVENT_DISPLAY_ORIENTATION = C.ALLEGRO_EVENT_DISPLAY_ORIENTATION
  257. )
  258. func EVENT_TYPE_IS_USER(t int) bool {
  259. return ((t) >= 512)
  260. }
  261. func GET_EVENT_TYPE(a, b, c, d int) int {
  262. return AL_ID(a, b, c, d)
  263. }
  264. func getAnyEvenTimestamp(any *C.ALLEGRO_ANY_EVENT) float64 {
  265. return float64(any.timestamp)
  266. }
  267. // Event sources that emit events.
  268. type EventSource C.ALLEGRO_EVENT_SOURCE
  269. // Converts wrapper Event pointer to C Allegro event pointer
  270. func (self *EventSource) toC() *C.ALLEGRO_EVENT_SOURCE {
  271. return (*C.ALLEGRO_EVENT_SOURCE)(self)
  272. }
  273. // Events that the event system emits.
  274. type Event C.ALLEGRO_EVENT
  275. // Converts wrapper Event pointer to C Allegro event pointer
  276. func (self *Event) toC() *C.ALLEGRO_EVENT {
  277. return (*C.ALLEGRO_EVENT)(self)
  278. }
  279. // Returns the type of the event.
  280. func (self *Event) Type() int {
  281. return int(C.algo_event_type(self.toC()))
  282. }
  283. // Returns the timestamp of the event.
  284. func (self *Event) Timestamp() float64 {
  285. return float64(C.algo_event_any(self.toC()).timestamp)
  286. }
  287. // Returns the event source of the event
  288. func (self *Event) EventSource() *EventSource {
  289. return (*EventSource)(C.algo_event_any(self.toC()).source)
  290. }
  291. // Returns true if this is a dispay event, false if not.
  292. func (self *Event) IsDisplay() bool {
  293. t := self.Type()
  294. return (t >= EVENT_DISPLAY_EXPOSE) && (t <= EVENT_DISPLAY_ORIENTATION)
  295. }
  296. // Returns true if this is a mouse event, false if not.
  297. func (self *Event) IsMouse() bool {
  298. t := self.Type()
  299. return (t >= EVENT_MOUSE_AXES) && (t <= EVENT_MOUSE_WARPED)
  300. }
  301. // Returns true if this is a Joystick event, false if not.
  302. func (self *Event) IsJoystick() bool {
  303. t := self.Type()
  304. return (t >= EVENT_JOYSTICK_AXIS) && (t <= EVENT_JOYSTICK_CONFIGURATION)
  305. }
  306. // Returns true if this is a keyboard event, false if not.
  307. func (self *Event) IsKeyboard() bool {
  308. t := self.Type()
  309. return (t >= EVENT_KEY_DOWN) && (t <= EVENT_KEY_UP)
  310. }
  311. // Returns true if this is a timer event, false if not.
  312. func (self *Event) IsTimer() bool {
  313. t := self.Type()
  314. return (t == EVENT_TIMER)
  315. }
  316. // Returns true if this is a user event, false if not.
  317. func (self *Event) IsUser() bool {
  318. t := self.Type()
  319. return EVENT_TYPE_IS_USER(t)
  320. }
  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 wrapDisplayRaw(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 wrapDisplayRaw(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 wrapDisplayRaw(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. }
  623. // Do nothing function for benchmarking only
  624. func DoNothing() {
  625. C.algo_do_nothing()
  626. }