al.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. 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. // Might be needed on OSX.
  200. func RunMain(args []string, callback RunMainCallback, data interface{}) int {
  201. runMain.fn = callback
  202. runMain.data = data
  203. argc, argv := CStrings(args) ; defer CStringsFree(argc, argv)
  204. return int(C.al_run_main(argc, argv, (*[0]byte)(C.go_run_main_callback)))
  205. }
  206. /** Allegro has it's own string type. While it's nice, it's
  207. not needed in Go, so I will just wrap the basic conversion functions. */
  208. type USTR struct {
  209. handle *C.ALLEGRO_USTR
  210. }
  211. // Frees an Allegro unicode string.
  212. func (self *USTR) Free() {
  213. if self.handle != nil {
  214. C.al_ustr_free(self.handle)
  215. }
  216. self.handle = nil
  217. }
  218. // Just for consistency and to allow SelfDestruuct to work
  219. func (self *USTR) Destroy() {
  220. self.Free()
  221. }
  222. // Converts an Allegro Unicode string to a Go string
  223. func (self *USTR) String() string {
  224. if self.handle == nil {
  225. return "<destroyed>"
  226. }
  227. return C.GoStringN(C.al_cstr(self.handle), C.int(C.al_ustr_size(self.handle)))
  228. }
  229. // Wraps an Allegro USTR into the go struct above, but does not set a finalizer
  230. func wrapUSTRRaw(handle *C.ALLEGRO_USTR) *USTR {
  231. if handle == nil {
  232. return nil
  233. }
  234. return &USTR{handle}
  235. }
  236. // Wraps an Allegro path into the go struct above, and sets the finalizer to
  237. // be the Destroy method of that struct.
  238. func wrapUSTR(handle *C.ALLEGRO_USTR) *USTR {
  239. result := wrapUSTRRaw(handle)
  240. if result != nil {
  241. runtime.SetFinalizer(result, func(me *USTR) { me.Destroy() })
  242. }
  243. return result
  244. }
  245. // Converts a go string to an Allegro Unicode string
  246. func USTRV(str string) *USTR {
  247. cstr := cstr(str)
  248. defer cstrFree(cstr)
  249. return wrapUSTR(C.al_ustr_new(cstr))
  250. }
  251. // Converts a go string to an Allegro Unicode string
  252. func USTRP(str *string) *USTR {
  253. return USTRV(*str)
  254. }
  255. // Allegro's timer functions
  256. // Gets the time since allegro was initialized in seconds
  257. func Time() float64 {
  258. return float64(C.al_get_time())
  259. }
  260. // Sleeps the given amount of seconds
  261. func Rest(seconds float64) {
  262. C.al_rest(C.double(seconds))
  263. }
  264. type Timeout = C.ALLEGRO_TIMEOUT
  265. func (tout * Timeout) Init(seconds float64) {
  266. C.al_init_timeout(tout, C.double(seconds))
  267. }
  268. func (tout * Timeout) toC() (* C.ALLEGRO_TIMEOUT) {
  269. return (* C.ALLEGRO_TIMEOUT)(tout)
  270. }
  271. // Precise (?) Timer
  272. type Timer struct {
  273. handle *C.ALLEGRO_TIMER
  274. }
  275. // Destroys the timer queue.
  276. func (self *Timer) Destroy() {
  277. if self.handle != nil {
  278. C.al_destroy_timer(self.handle)
  279. }
  280. self.handle = nil
  281. }
  282. // Wraps a timer, but does not set a finalizer.
  283. func wrapTimerRaw(handle *C.ALLEGRO_TIMER) *Timer {
  284. if handle == nil {
  285. return nil
  286. }
  287. return &Timer{handle}
  288. }
  289. // Wraps an event queue and sets a finalizer that calls Destroy
  290. func wrapTimer(handle *C.ALLEGRO_TIMER) *Timer {
  291. result := wrapTimerRaw(handle)
  292. if result != nil {
  293. runtime.SetFinalizer(result, func(me *Timer) { me.Destroy() })
  294. }
  295. return result
  296. }
  297. // Creates a timer with the given tick speed.
  298. func CreateTimer(speed_secs float64) *Timer {
  299. return wrapTimer(C.al_create_timer(C.double(speed_secs)))
  300. }
  301. // Starts the timer.
  302. func (self *Timer) Start() {
  303. C.al_start_timer(self.handle)
  304. }
  305. // Stops the timer.
  306. func (self *Timer) Stop() {
  307. C.al_stop_timer(self.handle)
  308. }
  309. // Returns true if the timer was started, false if not
  310. func (self *Timer) IsStarted() bool {
  311. return bool(C.al_get_timer_started(self.handle))
  312. }
  313. // Sets the speed of the timer.
  314. func (self *Timer) SetSpeed(speed_secs float64) {
  315. C.al_set_timer_speed(self.handle, C.double(speed_secs))
  316. }
  317. // Gets the speed of the timer.
  318. func (self *Timer) GetSpeed() float64 {
  319. return float64(C.al_get_timer_speed(self.handle))
  320. }
  321. // Gets the count (in ticks) of the timer
  322. func (self *Timer) GetCount() int {
  323. return int(C.al_get_timer_count(self.handle))
  324. }
  325. // Sets the count (in ticks) of the timer
  326. func (self *Timer) SetCount(count int) {
  327. C.al_set_timer_count(self.handle, C.int64_t(count))
  328. }
  329. // Adds to the count (in ticks) of the timer
  330. func (self *Timer) AddCount(count int) {
  331. C.al_add_timer_count(self.handle, C.int64_t(count))
  332. }
  333. // Gets the event source of this timer that can be registered
  334. // on an event queue with RegisterEventSource.
  335. func (self *Timer) GetEventSource() *EventSource {
  336. return (*EventSource)(C.al_get_timer_event_source(self.handle))
  337. }
  338. // Do nothing function for benchmarking only
  339. func DoNothing() {
  340. }
  341. // Returns Allehro's error number
  342. func Errno() int {
  343. return int(C.al_get_errno())
  344. }