al.go 23 KB

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