al.go 25 KB

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