display.go 13 KB

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