display.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. package al
  2. /*
  3. #include <stdlib.h>
  4. #include <allegro5/allegro.h>
  5. #include "helpers.h"
  6. */
  7. import "C"
  8. import "runtime"
  9. import "unsafe"
  10. // Usful regexp for KATE: ALLEGRO_([A-Z0-9_]+)(.*) -> \1 = C.ALLEGRO_\1
  11. // Display functions.
  12. // Possible bit combinations for the flags parameter of CreateDisplay.
  13. const (
  14. WINDOWED = C.ALLEGRO_WINDOWED
  15. FULLSCREEN = C.ALLEGRO_FULLSCREEN
  16. OPENGL = C.ALLEGRO_OPENGL
  17. DIRECT3D_INTERNAL = C.ALLEGRO_DIRECT3D_INTERNAL
  18. RESIZABLE = C.ALLEGRO_RESIZABLE
  19. FRAMELESS = C.ALLEGRO_FRAMELESS
  20. NOFRAME = C.ALLEGRO_NOFRAME
  21. GENERATE_EXPOSE_EVENTS = C.ALLEGRO_GENERATE_EXPOSE_EVENTS
  22. OPENGL_3_0 = C.ALLEGRO_OPENGL_3_0
  23. OPENGL_FORWARD_COMPATIBLE = C.ALLEGRO_OPENGL_FORWARD_COMPATIBLE
  24. FULLSCREEN_WINDOW = C.ALLEGRO_FULLSCREEN_WINDOW
  25. MINIMIZED = C.ALLEGRO_MINIMIZED
  26. )
  27. /* Possible parameters for SetDisplayOption. */
  28. const (
  29. RED_SIZE = C.ALLEGRO_RED_SIZE
  30. GREEN_SIZE = C.ALLEGRO_GREEN_SIZE
  31. BLUE_SIZE = C.ALLEGRO_BLUE_SIZE
  32. ALPHA_SIZE = C.ALLEGRO_ALPHA_SIZE
  33. RED_SHIFT = C.ALLEGRO_RED_SHIFT
  34. GREEN_SHIFT = C.ALLEGRO_GREEN_SHIFT
  35. BLUE_SHIFT = C.ALLEGRO_BLUE_SHIFT
  36. ALPHA_SHIFT = C.ALLEGRO_ALPHA_SHIFT
  37. ACC_RED_SIZE = C.ALLEGRO_ACC_RED_SIZE
  38. ACC_GREEN_SIZE = C.ALLEGRO_ACC_GREEN_SIZE
  39. ACC_BLUE_SIZE = C.ALLEGRO_ACC_BLUE_SIZE
  40. ACC_ALPHA_SIZE = C.ALLEGRO_ACC_ALPHA_SIZE
  41. STEREO = C.ALLEGRO_STEREO
  42. AUX_BUFFERS = C.ALLEGRO_AUX_BUFFERS
  43. COLOR_SIZE = C.ALLEGRO_COLOR_SIZE
  44. DEPTH_SIZE = C.ALLEGRO_DEPTH_SIZE
  45. STENCIL_SIZE = C.ALLEGRO_STENCIL_SIZE
  46. SAMPLE_BUFFERS = C.ALLEGRO_SAMPLE_BUFFERS
  47. SAMPLES = C.ALLEGRO_SAMPLES
  48. RENDER_METHOD = C.ALLEGRO_RENDER_METHOD
  49. FLOAT_COLOR = C.ALLEGRO_FLOAT_COLOR
  50. FLOAT_DEPTH = C.ALLEGRO_FLOAT_DEPTH
  51. SINGLE_BUFFER = C.ALLEGRO_SINGLE_BUFFER
  52. SWAP_METHOD = C.ALLEGRO_SWAP_METHOD
  53. COMPATIBLE_DISPLAY = C.ALLEGRO_COMPATIBLE_DISPLAY
  54. UPDATE_DISPLAY_REGION = C.ALLEGRO_UPDATE_DISPLAY_REGION
  55. VSYNC = C.ALLEGRO_VSYNC
  56. MAX_BITMAP_SIZE = C.ALLEGRO_MAX_BITMAP_SIZE
  57. SUPPORT_NPOT_BITMAP = C.ALLEGRO_SUPPORT_NPOT_BITMAP
  58. CAN_DRAW_INTO_BITMAP = C.ALLEGRO_CAN_DRAW_INTO_BITMAP
  59. SUPPORT_SEPARATE_ALPHA = C.ALLEGRO_SUPPORT_SEPARATE_ALPHA
  60. DISPLAY_OPTIONS_COUNT = C.ALLEGRO_DISPLAY_OPTIONS_COUNT
  61. )
  62. // Constants that determine if a setting is required or not.
  63. const (
  64. DONTCARE = C.ALLEGRO_DONTCARE
  65. REQUIRE = C.ALLEGRO_REQUIRE
  66. SUGGEST = C.ALLEGRO_SUGGEST
  67. )
  68. // Display orientations
  69. const (
  70. DISPLAY_ORIENTATION_0_DEGREES = C.ALLEGRO_DISPLAY_ORIENTATION_0_DEGREES
  71. DISPLAY_ORIENTATION_90_DEGREES = C.ALLEGRO_DISPLAY_ORIENTATION_90_DEGREES
  72. DISPLAY_ORIENTATION_180_DEGREES = C.ALLEGRO_DISPLAY_ORIENTATION_180_DEGREES
  73. DISPLAY_ORIENTATION_270_DEGREES = C.ALLEGRO_DISPLAY_ORIENTATION_270_DEGREES
  74. DISPLAY_ORIENTATION_FACE_UP = C.ALLEGRO_DISPLAY_ORIENTATION_FACE_UP
  75. DISPLAY_ORIENTATION_FACE_DOWN = C.ALLEGRO_DISPLAY_ORIENTATION_FACE_DOWN
  76. )
  77. // Type that wraps a Display (a main window)
  78. type Display struct {
  79. handle *C.ALLEGRO_DISPLAY
  80. }
  81. // Converts display to C display
  82. func (disp *Display) toC() *C.ALLEGRO_DISPLAY {
  83. return disp.handle
  84. }
  85. // Destroys a display. Use this only when really needed!
  86. func (self *Display) Destroy() {
  87. if self.handle != nil {
  88. C.al_destroy_display(self.handle)
  89. }
  90. self.handle = nil
  91. }
  92. // Wraps a C Allegro display in a Display. Sets no finalizer.
  93. func wrapDisplayRaw(handle *C.ALLEGRO_DISPLAY) *Display {
  94. if handle == nil {
  95. return nil
  96. }
  97. return &Display{handle}
  98. }
  99. // Wraps a C Allegro display in a Display. Sets a finalizer that calls Destroy
  100. func wrapDisplay(handle *C.ALLEGRO_DISPLAY) *Display {
  101. self := wrapDisplayRaw(handle)
  102. if self != nil {
  103. runtime.SetFinalizer(self, func(me *Display) { me.Destroy() })
  104. }
  105. return self
  106. }
  107. // Monitor info
  108. type MonitorInfo C.ALLEGRO_MONITOR_INFO
  109. // Returns the X1 of the monitor info self.
  110. func (self *MonitorInfo) X1() int {
  111. return int(self.x1)
  112. }
  113. // Returns the Y1 of the monitor info self.
  114. func (self *MonitorInfo) Y1() int {
  115. return int(self.y1)
  116. }
  117. // Returns the X2 of the monitor info self.
  118. func (self *MonitorInfo) X2() int {
  119. return int(self.x2)
  120. }
  121. // Returns the Y2 of the monitor info self.
  122. func (self *MonitorInfo) Y2() int {
  123. return int(self.y2)
  124. }
  125. const (
  126. DEFAULT_DISPLAY_ADAPTER = C.ALLEGRO_DEFAULT_DISPLAY_ADAPTER
  127. )
  128. // Sets the flags that a display created by CreateDisplay will get after
  129. // this function was called.
  130. func SetNewDisplayFlags(flags int) {
  131. C.al_set_new_display_flags(C.int(flags))
  132. }
  133. // Creates a new dosplay with the given size. Influenced by SetNewDisplayFlags.
  134. func CreateDisplay(width, height int) *Display {
  135. return wrapDisplay(C.al_create_display(C.int(width), C.int(height)))
  136. }
  137. // Resizes the display.
  138. func (self *Display) Resize(width, height int) bool {
  139. return bool(C.al_resize_display(self.handle, C.int(width), C.int(height)))
  140. }
  141. // Updates the display to the physical scree no any changes become visible
  142. func FlipDisplay() {
  143. C.al_flip_display()
  144. }
  145. // Same as FlipDisplay, for mere consistency
  146. func (self *Display) Flip() {
  147. C.al_flip_display()
  148. }
  149. // Fills the current active display with a color
  150. func ClearToColor(color Color) {
  151. C.al_clear_to_color(color.toC())
  152. }
  153. // Clears the depth buffer of the active display
  154. func ClearDepthBuffer(z float32) {
  155. C.al_clear_depth_buffer(C.float(z))
  156. }
  157. // Draws a pixel on the active display at the given location
  158. // with the given color
  159. func DrawPixel(x, y float32, color Color) {
  160. C.al_draw_pixel(C.float(x), C.float(y), C.ALLEGRO_COLOR(color))
  161. }
  162. // Sets the refresh rate that the display should have after CreateDisplay().
  163. func SetNewDisplayRefreshRate(refresh_rate int) {
  164. C.al_set_new_display_refresh_rate(C.int(refresh_rate))
  165. }
  166. // Gets the refresh rate that the display should have after CreateDisplay().
  167. func NewDisplayRefreshRate() int {
  168. return int(C.al_get_new_display_refresh_rate())
  169. }
  170. // Gets the display flags that the display should have after CreateDisplay().
  171. func NewDisplayFlags() int {
  172. return int(C.al_get_new_display_flags())
  173. }
  174. // Gets the width of the display in pixels
  175. func (self *Display) Width() int {
  176. return int(C.al_get_display_width(self.handle))
  177. }
  178. // Gets the height of the display in pixels
  179. func (self *Display) Height() int {
  180. return int(C.al_get_display_height(self.handle))
  181. }
  182. // Gets the refresh rate of the display
  183. func (self *Display) RefreshRate() int {
  184. return int(C.al_get_display_refresh_rate(self.handle))
  185. }
  186. // Gets the display flags of the display
  187. func (self *Display) DisplayFlags() int {
  188. return int(C.al_get_display_flags(self.handle))
  189. }
  190. // Gets the orientation of the display
  191. func (self *Display) Orientation() int {
  192. return int(C.al_get_display_orientation(self.handle))
  193. }
  194. // Sets a dispay flag on the display
  195. func (self *Display) SetDisplayFlag(flag int, onoff bool) bool {
  196. return cb2b(C.al_set_display_flag(self.handle, C.int(flag), b2cb(onoff)))
  197. }
  198. // Returns the current display
  199. func CurrentDisplay() *Display {
  200. return wrapDisplayRaw(C.al_get_current_display())
  201. }
  202. // Sets the target C bitmap of allegro drawing
  203. func setTargetCBitmap(bmp *C.ALLEGRO_BITMAP) {
  204. C.al_set_target_bitmap(bmp)
  205. }
  206. // Sets the target bitmap of the allegro drawing
  207. func SetTargetBitmap(bmp Bitmap) {
  208. setTargetCBitmap(bmp.handle)
  209. }
  210. // Sets the target C backbuffer of allegro drawing
  211. func setTargetCBackbuffer(display *C.ALLEGRO_DISPLAY) {
  212. C.al_set_target_backbuffer(display)
  213. }
  214. // Sets the target backbuffer of allegro drawing
  215. func SetTargetBackbuffer(display *Display) {
  216. setTargetCBackbuffer(display.handle)
  217. }
  218. // Gets the backbuffer bitmap of the display
  219. func (self *Display) Backbuffer() *Bitmap {
  220. return wrapBitmapRaw(C.al_get_backbuffer(self.handle))
  221. }
  222. // Gets the target bitmap of allegro drawing
  223. func TargetBitmap() *Bitmap {
  224. return wrapBitmapRaw(C.al_get_target_bitmap())
  225. }
  226. // Must be called to acknowledge a RESIZE event
  227. func (self *Display) AcknowledgeResize() bool {
  228. return cb2b(C.al_acknowledge_resize(self.handle))
  229. }
  230. // Updates a region of the display (not the whole display like flip does)
  231. func UpdateDisplayRegion(x, y, width, height int) {
  232. C.al_update_display_region(C.int(x), C.int(y), C.int(width), C.int(height))
  233. }
  234. // Returns true of the bitmap is compatible with the current display, false if not.
  235. func (bitmap *Bitmap) IsCompatibleBitmap() bool {
  236. return cb2b(C.al_is_compatible_bitmap(bitmap.handle))
  237. }
  238. // Waits for the vertical retrace of the monitor to lessen tearing.
  239. func WaitForVsync() {
  240. C.al_wait_for_vsync()
  241. }
  242. // Gets the event source of the display to registeron an event queue
  243. // with RegisterEventSource.
  244. func (self *Display) GetEventSource() *EventSource {
  245. return wrapEventSourceRaw(C.al_get_display_event_source(self.handle))
  246. }
  247. // Sets the display icon the window manager should use for the display window
  248. func (self *Display) SetDisplayIcon(bitmap *Bitmap) {
  249. C.al_set_display_icon(self.handle, bitmap.handle)
  250. }
  251. // Converts an array of Bitmaps to an array of ALLEGRO_BITMAPS and a length
  252. func CBitmaps(bitmaps []*Bitmap) (count C.int, cbitmaps **C.ALLEGRO_BITMAP) {
  253. length := len(bitmaps)
  254. cbitmaps = (**C.ALLEGRO_BITMAP)(malloc(length * int(unsafe.Sizeof(*cbitmaps))))
  255. tmpslice := (*[1 << 30]*C.ALLEGRO_BITMAP)(unsafe.Pointer(cbitmaps))[:length:length]
  256. for i, b := range bitmaps {
  257. tmpslice[i] = b.handle
  258. }
  259. count = C.int(length)
  260. return count, cbitmaps
  261. }
  262. // frees the data allocated by Cstrings
  263. func CBitmapsFree(count C.int, cbitmaps **C.ALLEGRO_BITMAP) {
  264. free(unsafe.Pointer(cbitmaps))
  265. }
  266. // Sets the display icons the window manager should use for the display window
  267. func (self *Display) SetDisplayIcons(bitmaps []*Bitmap) {
  268. count , cbitmaps := CBitmaps(bitmaps)
  269. defer CBitmapsFree(count, cbitmaps)
  270. C.al_set_display_icons(self.handle, count, cbitmaps)
  271. }
  272. // Gets the number of available video adapters (I.E. grapic cards)
  273. func NumVideoAdapters() int {
  274. return int(C.al_get_num_video_adapters())
  275. }
  276. // Converts a monitor info pointer to a C * ALLEGRO_MONITOR_INFO
  277. func (self *MonitorInfo) toC() *C.ALLEGRO_MONITOR_INFO {
  278. return (*C.ALLEGRO_MONITOR_INFO)(self)
  279. }
  280. // Gets the monitor info for the index'th video adapter
  281. func (self *MonitorInfo) Get(index int) bool {
  282. return cb2b(C.al_get_monitor_info(C.int(index), self.toC()))
  283. }
  284. // Gets the monitor info for the index'th video adapter
  285. func GetMonitorInfo(index int) *MonitorInfo {
  286. var info MonitorInfo
  287. if (&info).Get(index) {
  288. return &info
  289. }
  290. return nil
  291. }
  292. // Returns the number of the display adapter where new dsplays will be created
  293. func NewDisplayAdapter() int {
  294. return int(C.al_get_new_display_adapter())
  295. }
  296. // Sets the number of the display adapter where new dsplays will be created
  297. func SetNewDisplayAdapter(adapter int) {
  298. C.al_set_new_display_adapter(C.int(adapter))
  299. }
  300. // Returns the position where new windowed displays will be created
  301. func NewWindowPosition() (x, y int) {
  302. var cx, cy C.int
  303. C.al_get_new_window_position(&cx, &cy)
  304. return int(cx), int(cy)
  305. }
  306. // Sets the position where new windowed displays will be created
  307. func SetNewWindowPosition(x, y int) {
  308. C.al_set_new_window_position(C.int(x), C.int(y))
  309. }
  310. // Returns the current position of the windowed display
  311. func (self *Display) WindowPosition() (x, y int) {
  312. var cx, cy C.int
  313. C.al_get_window_position(self.handle, &cx, &cy)
  314. return int(cx), int(cy)
  315. }
  316. // Sets the position where new windowed displays will be created
  317. func (self *Display) SetWindowPosition(x, y int) {
  318. C.al_set_window_position(self.handle, C.int(x), C.int(y))
  319. }
  320. // Constrains the window of a display. The environment might ignore the restraints.
  321. // 0 means no restraint.
  322. func (self *Display) SetWindowConstraints(min_w, min_h, max_w, max_h int) {
  323. C.al_set_window_constraints(self.handle, C.int(min_w), C.int(min_h), C.int(max_w), C.int(max_h))
  324. }
  325. // Returns the current constraints of the windowed display
  326. func (self *Display) WindowContraints() (min_w, min_h, max_w, max_h int) {
  327. var cmin_w, cmin_h, cmax_w, c_max_h C.int
  328. C.al_get_window_constraints(self.handle, &cmin_w, &cmin_h, &cmax_w, &c_max_h)
  329. return int(cmin_w), int(cmin_h), int(cmax_w), int(c_max_h)
  330. }
  331. // Gets the title for displays that will be newly created
  332. func NewWindowTitle() string {
  333. return C.GoString(C.al_get_new_window_title())
  334. }
  335. // Sets the title of the windowed display
  336. func (self *Display) SetWindowTitle(str string) {
  337. cstr := cstr(str)
  338. defer cstrFree(cstr)
  339. C.al_set_window_title(self.handle, cstr)
  340. }
  341. // Sets the title of newly created windowed displays
  342. func SetNewWindowTitle(str string) {
  343. cstr := cstr(str)
  344. defer cstrFree(cstr)
  345. C.al_set_new_window_title(cstr)
  346. }
  347. // Sets a display option to be used when a new display is created
  348. func SetNewDisplayOption(option, value, importance int) {
  349. C.al_set_new_display_option(C.int(option), C.int(value), C.int(importance))
  350. }
  351. // Resets all display oprions for new displays to their default values.
  352. func ResetNewDisplayOptions() {
  353. C.al_reset_new_display_options()
  354. }
  355. // Gets the display option of this display
  356. func (self *Display) DisplayOption(option int) int {
  357. return int(C.al_get_display_option(self.handle, C.int(option)))
  358. }
  359. // Allows to speed up drawing by holding the display . Only bitmap functions and font
  360. // drawing, as well as tranformations should be used until the hold is released
  361. func HoldBitmapDrawing(hold bool) {
  362. C.al_hold_bitmap_drawing(b2cb(hold))
  363. }
  364. // Returns whether or not the bitmap drawing was held
  365. func IsBitmapDrawingHeld() bool {
  366. return bool(C.al_is_bitmap_drawing_held())
  367. }
  368. /* UNSTABLE API
  369. func (disp * Display) BackupDirtyBitmaps() {
  370. C.backup_dirty_bitmaps(disp.handle)
  371. }
  372. */