display.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. // Destroys a display. Use this only when really needed!
  81. func (self *Display) Destroy() {
  82. if self.handle != nil {
  83. C.al_destroy_display(self.handle)
  84. }
  85. self.handle = nil
  86. }
  87. // Wraps a C Allegro display in a Display. Sets no finalizer.
  88. func wrapDisplayRaw(handle *C.ALLEGRO_DISPLAY) *Display {
  89. if handle == nil {
  90. return nil
  91. }
  92. return &Display{handle}
  93. }
  94. // Wraps a C Allegro display in a Display. Sets a finalizer that calls Destroy
  95. func wrapDisplay(handle *C.ALLEGRO_DISPLAY) *Display {
  96. self := wrapDisplayRaw(handle)
  97. if self != nil {
  98. runtime.SetFinalizer(self, func(me *Display) { me.Destroy() })
  99. }
  100. return self
  101. }
  102. // Display mode info.
  103. type DisplayMode C.ALLEGRO_DISPLAY_MODE
  104. // Converts display mode to C display mode
  105. func (self *DisplayMode) toC() *C.ALLEGRO_DISPLAY_MODE {
  106. return (*C.ALLEGRO_DISPLAY_MODE)(self)
  107. }
  108. // Returns the width of the display mode self.
  109. func (self *DisplayMode) Width() int {
  110. return int(self.width)
  111. }
  112. // Returns the height of the display mode self.
  113. func (self *DisplayMode) Height() int {
  114. return int(self.height)
  115. }
  116. // Returns the format of the display mode self.
  117. func (self *DisplayMode) Format() int {
  118. return int(self.format)
  119. }
  120. // Returns the refresh rate of the display mode self.
  121. func (self *DisplayMode) RefreshRate() int {
  122. return int(self.refresh_rate)
  123. }
  124. // Monitor info
  125. type MonitorInfo C.ALLEGRO_MONITOR_INFO
  126. // Returns the X1 of the monitor info self.
  127. func (self *MonitorInfo) X1() int {
  128. return int(self.x1)
  129. }
  130. // Returns the Y1 of the monitor info self.
  131. func (self *MonitorInfo) Y1() int {
  132. return int(self.y1)
  133. }
  134. // Returns the X2 of the monitor info self.
  135. func (self *MonitorInfo) X2() int {
  136. return int(self.x2)
  137. }
  138. // Returns the Y2 of the monitor info self.
  139. func (self *MonitorInfo) Y2() int {
  140. return int(self.y2)
  141. }
  142. const (
  143. DEFAULT_DISPLAY_ADAPTER = C.ALLEGRO_DEFAULT_DISPLAY_ADAPTER
  144. )
  145. // Sets the flags that a display created by CreateDisplay will get after
  146. // this function was called.
  147. func SetNewDisplayFlags(flags int) {
  148. C.al_set_new_display_flags(C.int(flags))
  149. }
  150. // Creates a new dosplay with the given size. Influenced by SetNewDisplayFlags.
  151. func CreateDisplay(width, height int) *Display {
  152. return wrapDisplay(C.al_create_display(C.int(width), C.int(height)))
  153. }
  154. // Resizes the display.
  155. func (self *Display) Resize(width, height int) bool {
  156. return bool(C.al_resize_display(self.handle, C.int(width), C.int(height)))
  157. }
  158. // Updates the display to the physical scree no any changes become visible
  159. func FlipDisplay() {
  160. C.al_flip_display()
  161. }
  162. // Same as FlipDisplay, for mere consistency
  163. func (self *Display) Flip() {
  164. C.al_flip_display()
  165. }
  166. // Color type
  167. type Color C.ALLEGRO_COLOR
  168. // Convert from
  169. func wrapColor(color C.ALLEGRO_COLOR) Color {
  170. return Color(color)
  171. }
  172. // Convert to C
  173. func (self Color) toC() C.ALLEGRO_COLOR {
  174. return C.ALLEGRO_COLOR(self)
  175. }
  176. // Creates a new color
  177. func CreateColor(r, g, b, a float32) Color {
  178. return Color{C.float(r), C.float(g), C.float(b), C.float(a)}
  179. }
  180. // Returns the R component of the color self.
  181. func (self Color) R() float32 {
  182. return float32(self.r)
  183. }
  184. // Returns the G component of the color self.
  185. func (self Color) G() float32 {
  186. return float32(self.g)
  187. }
  188. // Returns the B component of the color self.
  189. func (self Color) B() float32 {
  190. return float32(self.b)
  191. }
  192. // Returns the A component of the color self.
  193. func (self Color) A() float32 {
  194. return float32(self.a)
  195. }
  196. // Fills the current active display with a color
  197. func ClearToColor(color Color) {
  198. C.al_clear_to_color(color.toC())
  199. }
  200. // Draws a pixel on the active display at the given location
  201. // with the given color
  202. func DrawPixel(x, y float32, color Color) {
  203. C.al_draw_pixel(C.float(x), C.float(y), C.ALLEGRO_COLOR(color))
  204. }
  205. // Sets the refresh rate that the display should have after CreateDisplay().
  206. func SetNewDisplayRefreshRate(refresh_rate int) {
  207. C.al_set_new_display_refresh_rate(C.int(refresh_rate))
  208. }
  209. // Gets the refresh rate that the display should have after CreateDisplay().
  210. func NewDisplayRefreshRate() int {
  211. return int(C.al_get_new_display_refresh_rate())
  212. }
  213. // Gets the display flags that the display should have after CreateDisplay().
  214. func NewDisplayFlags() int {
  215. return int(C.al_get_new_display_flags())
  216. }
  217. // Gets the width of the display in pixels
  218. func (self *Display) Width() int {
  219. return int(C.al_get_display_width(self.handle))
  220. }
  221. // Gets the height of the display in pixels
  222. func (self *Display) Height() int {
  223. return int(C.al_get_display_height(self.handle))
  224. }
  225. // Gets the refresh rate of the display
  226. func (self *Display) RefreshRate() int {
  227. return int(C.al_get_display_refresh_rate(self.handle))
  228. }
  229. // Gets the display flags of the display
  230. func (self *Display) DisplayFlags() int {
  231. return int(C.al_get_display_flags(self.handle))
  232. }
  233. // Sets a dispay flag on the display
  234. func (self *Display) SetDisplayFlag(flag int, onoff bool) bool {
  235. return cb2b(C.al_set_display_flag(self.handle, C.int(flag), b2cb(onoff)))
  236. }
  237. // Returns the current display
  238. func CurrentDisplay() *Display {
  239. return wrapDisplayRaw(C.al_get_current_display())
  240. }
  241. // Sets the target C bitmap of allegro drawing
  242. func setTargetCBitmap(bmp *C.ALLEGRO_BITMAP) {
  243. C.al_set_target_bitmap(bmp)
  244. }
  245. // Sets the target bitmap of the allegro drawing
  246. func SetTargetBitmap(bmp Bitmap) {
  247. setTargetCBitmap(bmp.handle)
  248. }
  249. // Sets the target C backbuffer of allegro drawing
  250. func setTargetCBackbuffer(display *C.ALLEGRO_DISPLAY) {
  251. C.al_set_target_backbuffer(display)
  252. }
  253. // Sets the target backbuffer of allegro drawing
  254. func SetTargetBackbuffer(display *Display) {
  255. setTargetCBackbuffer(display.handle)
  256. }
  257. // Gets the backbuffer bitmap of the display
  258. func (self *Display) Backbuffer() *Bitmap {
  259. return wrapBitmapRaw(C.al_get_backbuffer(self.handle))
  260. }
  261. // Gets the target bitmap of allegro drawing
  262. func TargetBitmap() *Bitmap {
  263. return wrapBitmapRaw(C.al_get_target_bitmap())
  264. }
  265. // Must be called to aknowledge a RESIZE event
  266. func (self *Display) AcknowledgeResize() bool {
  267. return cb2b(C.al_acknowledge_resize(self.handle))
  268. }
  269. // Updates a region of the display (not the whole display like flip does)
  270. func UpdateDisplayRegion(x, y, width, height int) {
  271. C.al_update_display_region(C.int(x), C.int(y), C.int(width), C.int(height))
  272. }
  273. // Returns true of the bitmap is compatible with the currebt display, false if not.
  274. func (bitmap *Bitmap) IsCompatibleBitmap() bool {
  275. return cb2b(C.al_is_compatible_bitmap(bitmap.handle))
  276. }
  277. // Returns the number of display modes available to Allegro
  278. func NumDisplayModes() int {
  279. return int(C.al_get_num_display_modes())
  280. }
  281. // Returns the index'th display mode. Pass in a DisplayMode struct to store the display
  282. // mode info in.
  283. func (self *DisplayMode) Get(index int) *DisplayMode {
  284. return (*DisplayMode)(C.al_get_display_mode(C.int(index), self.toC()))
  285. }
  286. // Gets display mode info for the index'th display mode
  287. func GetDisplayMode(index int) *DisplayMode {
  288. var mode DisplayMode
  289. if (&mode).Get(index) != nil {
  290. return &mode
  291. }
  292. return nil
  293. }
  294. // Waits for the vertical retrace of the monitor to lessen tearing.
  295. func WaitForVsync() {
  296. C.al_wait_for_vsync()
  297. }
  298. // Gets the event source of the display to registeron an event queue
  299. // with RegisterEventSource.
  300. func (self *Display) GetEventSource() *EventSource {
  301. return (*EventSource)(C.al_get_display_event_source(self.handle))
  302. }
  303. // Sets the display icon the window manager should use for the display window
  304. func (self *Display) SetDisplayIcon(bitmap *Bitmap) {
  305. C.al_set_display_icon(self.handle, bitmap.handle)
  306. }
  307. // Gets the number of available video adapters (I.E. grapic cards)
  308. func NumVideoAdapters() int {
  309. return int(C.al_get_num_video_adapters())
  310. }
  311. // Converts a monitor info pointer to a C * ALLEGRO_MONITOR_INFO
  312. func (self *MonitorInfo) toC() *C.ALLEGRO_MONITOR_INFO {
  313. return (*C.ALLEGRO_MONITOR_INFO)(self)
  314. }
  315. // Gets the monitor info for the index'th video adapter
  316. func (self *MonitorInfo) Get(index int) bool {
  317. return cb2b(C.al_get_monitor_info(C.int(index), self.toC()))
  318. }
  319. // Gets the monitor info for the index'th video adapter
  320. func GetMonitorInfo(index int) *MonitorInfo {
  321. var info MonitorInfo
  322. if (&info).Get(index) {
  323. return &info
  324. }
  325. return nil
  326. }
  327. // Returns the number of the display adapter where new dsplays will be created
  328. func NewDisplayAdapter() int {
  329. return int(C.al_get_new_display_adapter())
  330. }
  331. // Sets the number of the display adapter where new dsplays will be created
  332. func SetNewDisplayAdapter(adapter int) {
  333. C.al_set_new_display_adapter(C.int(adapter))
  334. }
  335. // Returns the position where new windowed displays will be created
  336. func NewWindowPosition() (x, y int) {
  337. var cx, cy C.int
  338. C.al_get_new_window_position(&cx, &cy)
  339. return int(cx), int(cy)
  340. }
  341. // Sets the position where new windowed displays will be created
  342. func SetNewWindowPosition(x, y int) {
  343. C.al_set_new_window_position(C.int(x), C.int(y))
  344. }
  345. // Returns the current position of the windowed display
  346. func (self *Display) WindowPosition() (x, y int) {
  347. var cx, cy C.int
  348. C.al_get_window_position(self.handle, &cx, &cy)
  349. return int(cx), int(cy)
  350. }
  351. // Sets the position where new windowed displays will be created
  352. func (self *Display) SetWindowPosition(x, y int) {
  353. C.al_set_window_position(self.handle, C.int(x), C.int(y))
  354. }
  355. // Sets the title of the windowed display
  356. func (self *Display) SetTitle(str string) {
  357. cstr := cstr(str)
  358. defer cstrFree(cstr)
  359. C.al_set_window_title(self.handle, cstr)
  360. }
  361. // Sets a display option to be used when a new display is created
  362. func SetNewDisplayOption(option, value, importance int) {
  363. C.al_set_new_display_option(C.int(option), C.int(value), C.int(importance))
  364. }
  365. // Resets all display oprions for new displays to their default values.
  366. func ResetNewDisplayOptions() {
  367. C.al_reset_new_display_options()
  368. }
  369. // Gets the display option of this display
  370. func (self *Display) DisplayOption(option int) int {
  371. return int(C.al_get_display_option(self.handle, C.int(option)))
  372. }
  373. // Allows to speed up drawing by holding the display . Only bitmap functions and font
  374. // drawing, as well as tranformations shouldbe done until the hold is released
  375. func HoldBitmapDrawing(hold bool) {
  376. C.al_hold_bitmap_drawing(b2cb(hold))
  377. }
  378. // Returns whether or not the bitmap drawing was held
  379. func IsBitmapDrawingHeld() bool {
  380. return cb2b(C.al_is_bitmap_drawing_held())
  381. }