al.go 23 KB

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