display.go 15 KB

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