al.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. package al
  2. /*
  3. #cgo pkg-config: allegro-5.0
  4. #cgo CFLAGS: -I/usr/local/include
  5. #cgo linux LDFLAGS: -lc_nonshared
  6. #include <stdlib.h>
  7. #include <allegro5/allegro.h>
  8. #include "helpers.h"
  9. */
  10. import "C"
  11. import "unsafe"
  12. import "runtime"
  13. const PI = 3.14159265358979323846
  14. // Allegro library ID calculation.
  15. func AL_ID(a,b,c,d int) int {
  16. return (((a)<<24) | ((b)<<16) | ((c)<<8) | (d))
  17. }
  18. const VERSION = 5
  19. const SUB_VERSION = 0
  20. const WIP_VERSION = 7
  21. const RELEASE_NUMBER = 1
  22. const VERSION_STR = "5.0.7"
  23. const DATE_STR = "2012"
  24. const DATE = 20120624 /* yyyymmdd */
  25. const VERSION_INT =
  26. ((VERSION << 24) | (SUB_VERSION << 16) |
  27. (WIP_VERSION << 8) | RELEASE_NUMBER)
  28. // Checks if the basic Allegro system is installed or not.
  29. func IsSystemInstalled() bool {
  30. return bool(C.al_is_system_installed())
  31. }
  32. // Gets the raw version of Allegro linked to as an integer.
  33. func GetAllegroVersion() uint32 {
  34. return uint32(C.al_get_allegro_version())
  35. }
  36. // Initializes the Allegro system.
  37. func Initialize() bool {
  38. return bool(C.algo_initialize())
  39. }
  40. // Cleans up the Allegro system. Needed after calling Initialize.
  41. func Cleanup() {
  42. C.algo_atexit_cleanup()
  43. }
  44. // Installs the Allegro system.
  45. func InstallSystem() bool {
  46. return bool(C.al_install_system(VERSION_INT, nil))
  47. }
  48. // Uninstalls the Allegro system. Must be called after using InstallSystem.
  49. func UninstallSystem() {
  50. C.al_uninstall_system()
  51. }
  52. // allegro5/path.h
  53. // Wrapper for an Allegro path.
  54. type Path struct {
  55. handle * C.ALLEGRO_PATH
  56. }
  57. // Wraps an Allegro path into the go struct above, but does not set a finalizer
  58. func wrapPathRaw(handle * C.ALLEGRO_PATH) (* Path) {
  59. if handle == nil { return nil }
  60. return &Path{handle}
  61. }
  62. // Wraps an Allegro path into the go struct above, and sets a finalizer
  63. func wrapPathCleanly(handle * C.ALLEGRO_PATH,
  64. clean func(path * Path) ) (* Path) {
  65. result := wrapPathRaw(handle);
  66. if (result == nil) { return result }
  67. runtime.SetFinalizer(result, clean)
  68. return result
  69. }
  70. // Wraps an Allegro path into the go struct above, and sets a default finalizer
  71. func wrapPath(handle * C.ALLEGRO_PATH) (* Path) {
  72. cleanup := func(path * Path) { path.Destroy() }
  73. return wrapPathCleanly(handle,cleanup);
  74. }
  75. // Creates an Allegro path.
  76. func CreatePath(str string) *Path {
  77. cstr := C.CString(str)
  78. defer C.free(unsafe.Pointer(cstr))
  79. return wrapPath(C.al_create_path(cstr))
  80. }
  81. // Creates an allegro path for a directory.
  82. func CreatePathForDirectory(str string) *Path {
  83. cstr := C.CString(str)
  84. defer C.free(unsafe.Pointer(cstr))
  85. return wrapPath(C.al_create_path_for_directory(cstr))
  86. }
  87. // Clones an allegro path.
  88. func (self * Path) ClonePath() *Path {
  89. return wrapPath(C.al_clone_path(self.handle))
  90. }
  91. // Destroys an Allegro path. It may not be used after this.
  92. // Destroy may be called many times.
  93. func (self * Path) Destroy() {
  94. if self.handle != nil { C.al_destroy_path(self.handle) }
  95. self.handle = nil;
  96. }
  97. func (self * Path) GetPathNumComponents() (int) {
  98. return int(C.al_get_path_num_components(self.handle))
  99. }
  100. /*
  101. func (self * Path)
  102. AL_FUNC(int, al_get_path_num_components, (const ALLEGRO_PATH *path));
  103. AL_FUNC(const char*, al_get_path_component, (const ALLEGRO_PATH *path, int i));
  104. AL_FUNC(void, al_replace_path_component, (ALLEGRO_PATH *path, int i, const char *s));
  105. AL_FUNC(void, al_remove_path_component, (ALLEGRO_PATH *path, int i));
  106. AL_FUNC(void, al_insert_path_component, (ALLEGRO_PATH *path, int i, const char *s));
  107. AL_FUNC(const char*, al_get_path_tail, (const ALLEGRO_PATH *path));
  108. AL_FUNC(void, al_drop_path_tail, (ALLEGRO_PATH *path));
  109. AL_FUNC(void, al_append_path_component, (ALLEGRO_PATH *path, const char *s));
  110. AL_FUNC(bool, al_join_paths, (ALLEGRO_PATH *path, const ALLEGRO_PATH *tail));
  111. AL_FUNC(bool, al_rebase_path, (const ALLEGRO_PATH *head, ALLEGRO_PATH *tail));
  112. AL_FUNC(const char*, al_path_cstr, (const ALLEGRO_PATH *path, char delim));
  113. AL_FUNC(void, al_destroy_path, (ALLEGRO_PATH *path));
  114. AL_FUNC(void, al_set_path_drive, (ALLEGRO_PATH *path, const char *drive));
  115. AL_FUNC(const char*, al_get_path_drive, (const ALLEGRO_PATH *path));
  116. AL_FUNC(void, al_set_path_filename, (ALLEGRO_PATH *path, const char *filename));
  117. AL_FUNC(const char*, al_get_path_filename, (const ALLEGRO_PATH *path));
  118. AL_FUNC(const char*, al_get_path_extension, (const ALLEGRO_PATH *path));
  119. AL_FUNC(bool, al_set_path_extension, (ALLEGRO_PATH *path, char const *extension));
  120. AL_FUNC(const char*, al_get_path_basename, (const ALLEGRO_PATH *path));
  121. AL_FUNC(bool, al_make_path_canonical, (ALLEGRO_PATH *path));
  122. */
  123. // Not wrapped yet:
  124. // AL_FUNC(SYSTEM *, al_get_system_driver, (void));
  125. // AL_FUNC(CONFIG *, al_get_system_config, (void));
  126. const (
  127. RESOURCES_PATH = iota
  128. TEMP_PATH
  129. USER_DATA_PATH
  130. USER_HOME_PATH
  131. USER_SETTINGS_PATH
  132. USER_DOCUMENTS_PATH
  133. EXENAME_PATH
  134. LAST_PATH
  135. )
  136. /*
  137. func GetStandardPath(int id) string {
  138. AL_FUNC(PATH *, al_get_standard_path, (int id));
  139. AL_FUNC(void, al_set_exe_name, (char const *path));
  140. AL_FUNC(void, al_set_org_name, (const char *org_name));
  141. AL_FUNC(void, al_set_app_name, (const char *app_name));
  142. AL_FUNC(const char *, al_get_org_name, (void));
  143. AL_FUNC(const char *, al_get_app_name, (void));
  144. AL_FUNC(bool, al_inhibit_screensaver, (bool inhibit));
  145. */
  146. // AL_FUNC(int, al_run_main, (int argc, char **argv, int (*)(int, char **)));
  147. /** Allegro has it's own string type. While it's nice, it's
  148. not needed in Go, so I will just wrap the basic conversion functions */
  149. type USTR struct {
  150. handle * C.ALLEGRO_USTR
  151. }
  152. // Frees an Allegro unicode string.
  153. func (self * USTR) Free() {
  154. if self.handle != nil { C.al_ustr_free(self.handle) }
  155. self.handle = nil
  156. }
  157. // Converts an Allegro Unicode string to a Go string
  158. func (self * USTR) String() string {
  159. if (self.handle == nil) { return "<destroyed>" }
  160. return C.GoStringN(C.al_cstr(self.handle), C.int(C.al_ustr_size(self.handle)))
  161. }
  162. // Wraps an Allegro USTR into the go struct above, but does not set a finalizer
  163. func wrapUSTRRaw(handle * C.ALLEGRO_USTR) (* USTR) {
  164. if handle == nil { return nil }
  165. return &USTR{handle}
  166. }
  167. // Wraps an Allegro path into the go struct above, and sets a finalizer
  168. func wrapUSTRCleanly(handle * C.ALLEGRO_USTR,
  169. clean func(ustr * USTR) ) (* USTR) {
  170. result := wrapUSTRRaw(handle);
  171. if (result == nil) { return result }
  172. runtime.SetFinalizer(result, clean)
  173. return result
  174. }
  175. // Wraps an Allegro path into the go struct above, and sets a default finalizer
  176. func wrapUSTR(handle * C.ALLEGRO_USTR) (* USTR) {
  177. cleanup := func(ustr * USTR) { ustr.Free() }
  178. return wrapUSTRCleanly(handle, cleanup);
  179. }
  180. // Converts a go string to an Allegro Unicode string
  181. func USTRV(str string) (* USTR) {
  182. cstr := C.CString(str)
  183. defer C.free(unsafe.Pointer(cstr))
  184. return wrapUSTR(C.al_ustr_new(cstr))
  185. }
  186. // Converts a go string to an Allegro Unicode string
  187. func USTRP(str * string) (* USTR) {
  188. return USTRV(*str)
  189. }
  190. // Allegro's timer functions
  191. // Gets the time the app is running in seconds
  192. func GetTime() float64 {
  193. return float64(C.al_get_time())
  194. }
  195. // Sleeps the given amount of seconds
  196. func Rest(seconds float64) {
  197. C.al_rest(C.double(seconds))
  198. }