al.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. type Timeout = C.ALLEGRO_TIMEOUT
  266. func (tout * Timeout) Init(seconds float64) {
  267. C.al_init_timeout(tout, C.double(seconds))
  268. }
  269. func (tout * Timeout) toC() (* C.ALLEGRO_TIMEOUT) {
  270. return (* C.ALLEGRO_TIMEOUT)(tout)
  271. }
  272. // Precise (?) Timer
  273. type Timer struct {
  274. handle *C.ALLEGRO_TIMER
  275. }
  276. // Destroys the timer queue.
  277. func (self *Timer) Destroy() {
  278. if self.handle != nil {
  279. C.al_destroy_timer(self.handle)
  280. }
  281. self.handle = nil
  282. }
  283. // Wraps a timer, but does not set a finalizer.
  284. func wrapTimerRaw(handle *C.ALLEGRO_TIMER) *Timer {
  285. if handle == nil {
  286. return nil
  287. }
  288. return &Timer{handle}
  289. }
  290. // Wraps an event queue and sets a finalizer that calls Destroy
  291. func wrapTimer(handle *C.ALLEGRO_TIMER) *Timer {
  292. result := wrapTimerRaw(handle)
  293. if result != nil {
  294. runtime.SetFinalizer(result, func(me *Timer) { me.Destroy() })
  295. }
  296. return result
  297. }
  298. // Creates a timer with the given tick speed.
  299. func CreateTimer(speed_secs float64) *Timer {
  300. return wrapTimer(C.al_create_timer(C.double(speed_secs)))
  301. }
  302. // Starts the timer.
  303. func (self *Timer) Start() {
  304. C.al_start_timer(self.handle)
  305. }
  306. // Stops the timer.
  307. func (self *Timer) Stop() {
  308. C.al_stop_timer(self.handle)
  309. }
  310. // Returns true if the timer was started, false if not
  311. func (self *Timer) IsStarted() bool {
  312. return bool(C.al_get_timer_started(self.handle))
  313. }
  314. // Sets the speed of the timer.
  315. func (self *Timer) SetSpeed(speed_secs float64) {
  316. C.al_set_timer_speed(self.handle, C.double(speed_secs))
  317. }
  318. // Gets the speed of the timer.
  319. func (self *Timer) GetSpeed() float64 {
  320. return float64(C.al_get_timer_speed(self.handle))
  321. }
  322. // Gets the count (in ticks) of the timer
  323. func (self *Timer) GetCount() int {
  324. return int(C.al_get_timer_count(self.handle))
  325. }
  326. // Sets the count (in ticks) of the timer
  327. func (self *Timer) SetCount(count int) {
  328. C.al_set_timer_count(self.handle, C.int64_t(count))
  329. }
  330. // Adds to the count (in ticks) of the timer
  331. func (self *Timer) AddCount(count int) {
  332. C.al_add_timer_count(self.handle, C.int64_t(count))
  333. }
  334. // Gets the event source of this timer that can be registered
  335. // on an event queue with RegisterEventSource.
  336. func (self *Timer) GetEventSource() *EventSource {
  337. return (*EventSource)(C.al_get_timer_event_source(self.handle))
  338. }
  339. // Do nothing function for benchmarking only
  340. func DoNothing() {
  341. }
  342. // Returns Allehro's error number
  343. func Errno() int {
  344. return int(C.al_get_errno())
  345. }