display.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 refresh rate of the display
  184. func (self *Display) RefreshRate() int {
  185. return int(C.al_get_display_refresh_rate(self.handle))
  186. }
  187. // Gets the display flags of the display
  188. func (self *Display) DisplayFlags() int {
  189. return int(C.al_get_display_flags(self.handle))
  190. }
  191. // Gets the orientation of the display
  192. func (self *Display) Orientation() int {
  193. return int(C.al_get_display_orientation(self.handle))
  194. }
  195. // Sets a dispay flag on the display
  196. func (self *Display) SetDisplayFlag(flag int, onoff bool) bool {
  197. return cb2b(C.al_set_display_flag(self.handle, C.int(flag), b2cb(onoff)))
  198. }
  199. // Returns the current display
  200. func CurrentDisplay() *Display {
  201. return wrapDisplayRaw(C.al_get_current_display())
  202. }
  203. // Sets the target C bitmap of allegro drawing
  204. func setTargetCBitmap(bmp *C.ALLEGRO_BITMAP) {
  205. C.al_set_target_bitmap(bmp)
  206. }
  207. // Sets the target bitmap of the allegro drawing
  208. func SetTargetBitmap(bmp Bitmap) {
  209. setTargetCBitmap(bmp.handle)
  210. }
  211. // Sets the target C backbuffer of allegro drawing
  212. func setTargetCBackbuffer(display *C.ALLEGRO_DISPLAY) {
  213. C.al_set_target_backbuffer(display)
  214. }
  215. // Sets the target backbuffer of allegro drawing
  216. func SetTargetBackbuffer(display *Display) {
  217. setTargetCBackbuffer(display.handle)
  218. }
  219. // Gets the backbuffer bitmap of the display
  220. func (self *Display) Backbuffer() *Bitmap {
  221. return wrapBitmapRaw(C.al_get_backbuffer(self.handle))
  222. }
  223. // Gets the target bitmap of allegro drawing
  224. func TargetBitmap() *Bitmap {
  225. return wrapBitmapRaw(C.al_get_target_bitmap())
  226. }
  227. // Must be called to acknowledge a RESIZE event
  228. func (self *Display) AcknowledgeResize() bool {
  229. return cb2b(C.al_acknowledge_resize(self.handle))
  230. }
  231. // Updates a region of the display (not the whole display like flip does)
  232. func UpdateDisplayRegion(x, y, width, height int) {
  233. C.al_update_display_region(C.int(x), C.int(y), C.int(width), C.int(height))
  234. }
  235. // Returns true of the bitmap is compatible with the current display, false if not.
  236. func (bitmap *Bitmap) IsCompatibleBitmap() bool {
  237. return cb2b(C.al_is_compatible_bitmap(bitmap.handle))
  238. }
  239. // Waits for the vertical retrace of the monitor to lessen tearing.
  240. func WaitForVsync() {
  241. C.al_wait_for_vsync()
  242. }
  243. // Gets the event source of the display to registeron an event queue
  244. // with RegisterEventSource.
  245. func (self *Display) GetEventSource() *EventSource {
  246. return wrapEventSourceRaw(C.al_get_display_event_source(self.handle))
  247. }
  248. // Sets the display icon the window manager should use for the display window
  249. func (self *Display) SetDisplayIcon(bitmap *Bitmap) {
  250. C.al_set_display_icon(self.handle, bitmap.handle)
  251. }
  252. // Converts an array of Bitmaps to an array of ALLEGRO_BITMAPS and a length
  253. func CBitmaps(bitmaps []*Bitmap) (count C.int, cbitmaps **C.ALLEGRO_BITMAP) {
  254. length := len(bitmaps)
  255. cbitmaps = (**C.ALLEGRO_BITMAP)(malloc(length * int(unsafe.Sizeof(*cbitmaps))))
  256. tmpslice := (*[1 << 30]*C.ALLEGRO_BITMAP)(unsafe.Pointer(cbitmaps))[:length:length]
  257. for i, b := range bitmaps {
  258. tmpslice[i] = b.handle
  259. }
  260. count = C.int(length)
  261. return count, cbitmaps
  262. }
  263. // frees the data allocated by Cstrings
  264. func CBitmapsFree(count C.int, cbitmaps **C.ALLEGRO_BITMAP) {
  265. free(unsafe.Pointer(cbitmaps))
  266. }
  267. // Sets the display icons the window manager should use for the display window
  268. func (self *Display) SetDisplayIcons(bitmaps []*Bitmap) {
  269. count , cbitmaps := CBitmaps(bitmaps)
  270. defer CBitmapsFree(count, cbitmaps)
  271. C.al_set_display_icons(self.handle, count, cbitmaps)
  272. }
  273. // Gets the number of available video adapters (I.E. grapic cards)
  274. func NumVideoAdapters() int {
  275. return int(C.al_get_num_video_adapters())
  276. }
  277. // Converts a monitor info pointer to a C * ALLEGRO_MONITOR_INFO
  278. func (self *MonitorInfo) toC() *C.ALLEGRO_MONITOR_INFO {
  279. return (*C.ALLEGRO_MONITOR_INFO)(self)
  280. }
  281. // Gets the monitor info for the index'th video adapter
  282. func (self *MonitorInfo) Get(index int) bool {
  283. return cb2b(C.al_get_monitor_info(C.int(index), self.toC()))
  284. }
  285. // Gets the monitor info for the index'th video adapter
  286. func GetMonitorInfo(index int) *MonitorInfo {
  287. var info MonitorInfo
  288. if (&info).Get(index) {
  289. return &info
  290. }
  291. return nil
  292. }
  293. func (mi * MonitorInfo) String() string {
  294. return fmt.Sprintf("%d %d %d %d", mi.X1(), mi.Y1(), mi.X2(), mi.Y2())
  295. }
  296. // Gets all available monitors and their info
  297. func GetAllMonitorInfo() []*MonitorInfo {
  298. count := NumVideoAdapters();
  299. info := make([]*MonitorInfo, count)
  300. for i := 0 ; i < count; i ++ {
  301. info[i] = GetMonitorInfo(i)
  302. }
  303. return info
  304. }
  305. // Returns the number of the display adapter where new dsplays will be created
  306. func NewDisplayAdapter() int {
  307. return int(C.al_get_new_display_adapter())
  308. }
  309. // Sets the number of the display adapter where new dsplays will be created
  310. func SetNewDisplayAdapter(adapter int) {
  311. C.al_set_new_display_adapter(C.int(adapter))
  312. }
  313. // Returns the position where new windowed displays will be created
  314. func NewWindowPosition() (x, y int) {
  315. var cx, cy C.int
  316. C.al_get_new_window_position(&cx, &cy)
  317. return int(cx), int(cy)
  318. }
  319. // Sets the position where new windowed displays will be created
  320. func SetNewWindowPosition(x, y int) {
  321. C.al_set_new_window_position(C.int(x), C.int(y))
  322. }
  323. // Returns the current position of the windowed display
  324. func (self *Display) WindowPosition() (x, y int) {
  325. var cx, cy C.int
  326. C.al_get_window_position(self.handle, &cx, &cy)
  327. return int(cx), int(cy)
  328. }
  329. // Sets the position where new windowed displays will be created
  330. func (self *Display) SetWindowPosition(x, y int) {
  331. C.al_set_window_position(self.handle, C.int(x), C.int(y))
  332. }
  333. // Constrains the window of a display. The environment might ignore the restraints.
  334. // 0 means no restraint.
  335. func (self *Display) SetWindowConstraints(min_w, min_h, max_w, max_h int) {
  336. C.al_set_window_constraints(self.handle, C.int(min_w), C.int(min_h), C.int(max_w), C.int(max_h))
  337. }
  338. // Returns the current constraints of the windowed display
  339. func (self *Display) WindowContraints() (min_w, min_h, max_w, max_h int) {
  340. var cmin_w, cmin_h, cmax_w, c_max_h C.int
  341. C.al_get_window_constraints(self.handle, &cmin_w, &cmin_h, &cmax_w, &c_max_h)
  342. return int(cmin_w), int(cmin_h), int(cmax_w), int(c_max_h)
  343. }
  344. // Gets the title for displays that will be newly created
  345. func NewWindowTitle() string {
  346. return C.GoString(C.al_get_new_window_title())
  347. }
  348. // Sets the title of the windowed display
  349. func (self *Display) SetWindowTitle(str string) {
  350. cstr := cstr(str)
  351. defer cstrFree(cstr)
  352. C.al_set_window_title(self.handle, cstr)
  353. }
  354. // Sets the title of newly created windowed displays
  355. func SetNewWindowTitle(str string) {
  356. cstr := cstr(str)
  357. defer cstrFree(cstr)
  358. C.al_set_new_window_title(cstr)
  359. }
  360. // Sets a display option to be used when a new display is created
  361. func SetNewDisplayOption(option, value, importance int) {
  362. C.al_set_new_display_option(C.int(option), C.int(value), C.int(importance))
  363. }
  364. // Resets all display oprions for new displays to their default values.
  365. func ResetNewDisplayOptions() {
  366. C.al_reset_new_display_options()
  367. }
  368. // Gets the display option of this display
  369. func (self *Display) DisplayOption(option int) int {
  370. return int(C.al_get_display_option(self.handle, C.int(option)))
  371. }
  372. // Allows to speed up drawing by holding the display . Only bitmap functions and font
  373. // drawing, as well as tranformations should be used until the hold is released
  374. func HoldBitmapDrawing(hold bool) {
  375. C.al_hold_bitmap_drawing(b2cb(hold))
  376. }
  377. // Returns whether or not the bitmap drawing was held
  378. func IsBitmapDrawingHeld() bool {
  379. return bool(C.al_is_bitmap_drawing_held())
  380. }
  381. /* UNSTABLE API
  382. func (disp * Display) BackupDirtyBitmaps() {
  383. C.backup_dirty_bitmaps(disp.handle)
  384. }
  385. */