al.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 AllegroVersion() 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) NumComponents() int {
  129. return int(C.al_get_path_num_components(self.handle))
  130. }
  131. // Gets the index'th component of the path name
  132. func (path *Path) Component(index int) string {
  133. return C.GoString(C.al_get_path_component(path.handle, C.int(index)))
  134. }
  135. // Converts the allegro path to a string
  136. func (self *Path) String() string {
  137. return gostr(C.al_path_cstr(self.handle, C.char(NATIVE_PATH_SEP)))
  138. }
  139. func (path * Path) MakeCanonical() * Path {
  140. C.al_make_path_canonical(path.handle)
  141. return path
  142. }
  143. /*
  144. func (self * Path)
  145. AL_FUNC(const char*, al_get_path_component, (const ALLEGRO_PATH *path, int i));
  146. AL_FUNC(void, al_replace_path_component, (ALLEGRO_PATH *path, int i, const char *s));
  147. AL_FUNC(void, al_remove_path_component, (ALLEGRO_PATH *path, int i));
  148. AL_FUNC(void, al_insert_path_component, (ALLEGRO_PATH *path, int i, const char *s));
  149. AL_FUNC(const char*, al_get_path_tail, (const ALLEGRO_PATH *path));
  150. AL_FUNC(void, al_drop_path_tail, (ALLEGRO_PATH *path));
  151. AL_FUNC(void, al_append_path_component, (ALLEGRO_PATH *path, const char *s));
  152. AL_FUNC(bool, al_join_paths, (ALLEGRO_PATH *path, const ALLEGRO_PATH *tail));
  153. AL_FUNC(bool, al_rebase_path, (const ALLEGRO_PATH *head, ALLEGRO_PATH *tail));
  154. AL_FUNC(const char*, al_path_cstr, (const ALLEGRO_PATH *path, char delim));
  155. AL_FUNC(void, al_destroy_path, (ALLEGRO_PATH *path));
  156. AL_FUNC(void, al_set_path_drive, (ALLEGRO_PATH *path, const char *drive));
  157. AL_FUNC(const char*, al_get_path_drive, (const ALLEGRO_PATH *path));
  158. AL_FUNC(void, al_set_path_filename, (ALLEGRO_PATH *path, const char *filename));
  159. AL_FUNC(const char*, al_get_path_filename, (const ALLEGRO_PATH *path));
  160. AL_FUNC(const char*, al_get_path_extension, (const ALLEGRO_PATH *path));
  161. AL_FUNC(bool, al_set_path_extension, (ALLEGRO_PATH *path, char const *extension));
  162. AL_FUNC(const char*, al_get_path_basename, (const ALLEGRO_PATH *path));
  163. AL_FUNC(bool, al_make_path_canonical, (ALLEGRO_PATH *path));
  164. // defer
  165. */
  166. // Not wrapped yet:
  167. // AL_FUNC(SYSTEM *, al_get_system_driver, (void));
  168. const (
  169. RESOURCES_PATH = C.ALLEGRO_RESOURCES_PATH
  170. TEMP_PATH = C.ALLEGRO_TEMP_PATH
  171. USER_DATA_PATH = C.ALLEGRO_USER_DATA_PATH
  172. USER_HOME_PATH = C.ALLEGRO_USER_HOME_PATH
  173. USER_SETTINGS_PATH = C.ALLEGRO_USER_SETTINGS_PATH
  174. USER_DOCUMENTS_PATH = C.ALLEGRO_USER_DOCUMENTS_PATH
  175. EXENAME_PATH = C.ALLEGRO_EXENAME_PATH
  176. LAST_PATH = C.ALLEGRO_LAST_PATH
  177. )
  178. // Gets a standard path location.
  179. func StandardPath(id int) *Path {
  180. return wrapPath(C.al_get_standard_path(C.int(id)))
  181. }
  182. // Sets the name of the executable.
  183. func SetExeName(name string) {
  184. C.al_set_exe_name(cstr(name))
  185. }
  186. // Sets the name of the organisation.
  187. func SetOrgName(name string) {
  188. C.al_set_org_name(cstr(name))
  189. }
  190. // Sets the name of the app.
  191. func SetAppName(name string) {
  192. C.al_set_app_name(cstr(name))
  193. }
  194. // Gets the name of the organisation for an app
  195. func OrgName() string {
  196. return gostr(C.al_get_org_name())
  197. }
  198. // Gets the name of the app
  199. func AppName() string {
  200. return gostr(C.al_get_app_name())
  201. }
  202. // Inibits the screensaver, or not debending on inhibit.
  203. func InhibitScreensaver(inhibit bool) bool {
  204. return bool(C.al_inhibit_screensaver(C.bool(inhibit)))
  205. }
  206. // Might be needed on OSX.
  207. func RunMain(args []string, callback RunMainCallback, data interface{}) int {
  208. runMain.fn = callback
  209. runMain.data = data
  210. argc, argv := CStrings(args) ; defer CStringsFree(argc, argv)
  211. return int(C.al_run_main(argc, argv, (*[0]byte)(C.go_run_main_callback)))
  212. }
  213. /** Allegro has it's own string type. While it's nice, it's
  214. not needed in Go, so I will just wrap the basic conversion functions. */
  215. type USTR struct {
  216. handle *C.ALLEGRO_USTR
  217. }
  218. // Frees an Allegro unicode string.
  219. func (self *USTR) Free() {
  220. if self.handle != nil {
  221. C.al_ustr_free(self.handle)
  222. }
  223. self.handle = nil
  224. }
  225. // Just for consistency and to allow SelfDestruuct to work
  226. func (self *USTR) Destroy() {
  227. self.Free()
  228. }
  229. // Converts an Allegro Unicode string to a Go string
  230. func (self *USTR) String() string {
  231. if self.handle == nil {
  232. return "<destroyed>"
  233. }
  234. return C.GoStringN(C.al_cstr(self.handle), C.int(C.al_ustr_size(self.handle)))
  235. }
  236. // Wraps an Allegro USTR into the go struct above, but does not set a finalizer
  237. func wrapUSTRRaw(handle *C.ALLEGRO_USTR) *USTR {
  238. if handle == nil {
  239. return nil
  240. }
  241. return &USTR{handle}
  242. }
  243. // Wraps an Allegro path into the go struct above, and sets the finalizer to
  244. // be the Destroy method of that struct.
  245. func wrapUSTR(handle *C.ALLEGRO_USTR) *USTR {
  246. result := wrapUSTRRaw(handle)
  247. if result != nil {
  248. runtime.SetFinalizer(result, func(me *USTR) { me.Destroy() })
  249. }
  250. return result
  251. }
  252. // Converts a go string to an Allegro Unicode string
  253. func USTRV(str string) *USTR {
  254. cstr := cstr(str)
  255. defer cstrFree(cstr)
  256. return wrapUSTR(C.al_ustr_new(cstr))
  257. }
  258. // Converts a go string to an Allegro Unicode string
  259. func USTRP(str *string) *USTR {
  260. return USTRV(*str)
  261. }
  262. // Allegro's timer functions
  263. // Gets the time since allegro was initialized in seconds
  264. func Time() float64 {
  265. return float64(C.al_get_time())
  266. }
  267. // Sleeps the given amount of seconds
  268. func Rest(seconds float64) {
  269. C.al_rest(C.double(seconds))
  270. }
  271. type Timeout = C.ALLEGRO_TIMEOUT
  272. func (tout * Timeout) Init(seconds float64) {
  273. C.al_init_timeout(tout, C.double(seconds))
  274. }
  275. func (tout * Timeout) toC() (* C.ALLEGRO_TIMEOUT) {
  276. return (* C.ALLEGRO_TIMEOUT)(tout)
  277. }
  278. // Precise (?) Timer
  279. type Timer struct {
  280. handle *C.ALLEGRO_TIMER
  281. }
  282. // Destroys the timer queue.
  283. func (self *Timer) Destroy() {
  284. if self.handle != nil {
  285. C.al_destroy_timer(self.handle)
  286. }
  287. self.handle = nil
  288. }
  289. // Wraps a timer, but does not set a finalizer.
  290. func wrapTimerRaw(handle *C.ALLEGRO_TIMER) *Timer {
  291. if handle == nil {
  292. return nil
  293. }
  294. return &Timer{handle}
  295. }
  296. // Wraps an event queue and sets a finalizer that calls Destroy
  297. func wrapTimer(handle *C.ALLEGRO_TIMER) *Timer {
  298. result := wrapTimerRaw(handle)
  299. if result != nil {
  300. runtime.SetFinalizer(result, func(me *Timer) { me.Destroy() })
  301. }
  302. return result
  303. }
  304. // Creates a timer with the given tick speed.
  305. func CreateTimer(speed_secs float64) *Timer {
  306. return wrapTimer(C.al_create_timer(C.double(speed_secs)))
  307. }
  308. // Starts the timer.
  309. func (self *Timer) Start() {
  310. C.al_start_timer(self.handle)
  311. }
  312. // Stops the timer.
  313. func (self *Timer) Stop() {
  314. C.al_stop_timer(self.handle)
  315. }
  316. // Returns true if the timer was started, false if not
  317. func (self *Timer) IsStarted() bool {
  318. return bool(C.al_get_timer_started(self.handle))
  319. }
  320. // Sets the speed of the timer.
  321. func (self *Timer) SetSpeed(speed_secs float64) {
  322. C.al_set_timer_speed(self.handle, C.double(speed_secs))
  323. }
  324. // Gets the speed of the timer.
  325. func (self *Timer) Speed() float64 {
  326. return float64(C.al_get_timer_speed(self.handle))
  327. }
  328. // Gets the count (in ticks) of the timer
  329. func (self *Timer) Count() int {
  330. return int(C.al_get_timer_count(self.handle))
  331. }
  332. // Sets the count (in ticks) of the timer
  333. func (self *Timer) SetCount(count int) {
  334. C.al_set_timer_count(self.handle, C.int64_t(count))
  335. }
  336. // Adds to the count (in ticks) of the timer
  337. func (self *Timer) AddCount(count int) {
  338. C.al_add_timer_count(self.handle, C.int64_t(count))
  339. }
  340. // Gets the event source of this timer that can be registered
  341. // on an event queue with RegisterEventSource.
  342. func TimerEventSource(self * Timer) *EventSource {
  343. return (*EventSource)(C.al_get_timer_event_source(self.handle))
  344. }
  345. // Do nothing function for benchmarking only
  346. func DoNothing() {
  347. }
  348. // Returns Allehro's error number
  349. func Errno() int {
  350. return int(C.al_get_errno())
  351. }