al.go 25 KB

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