display.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. // Color type
  171. type Color C.ALLEGRO_COLOR
  172. // Convert from
  173. func wrapColor(color C.ALLEGRO_COLOR) Color {
  174. return Color(color)
  175. }
  176. // Convert to C
  177. func (self Color) toC() C.ALLEGRO_COLOR {
  178. return C.ALLEGRO_COLOR(self)
  179. }
  180. // Creates a new color
  181. func CreateColor(r, g, b, a float32) Color {
  182. return Color{C.float(r), C.float(g), C.float(b), C.float(a)}
  183. }
  184. // Returns the R component of the color self.
  185. func (self Color) R() float32 {
  186. return float32(self.r)
  187. }
  188. // Returns the G component of the color self.
  189. func (self Color) G() float32 {
  190. return float32(self.g)
  191. }
  192. // Returns the B component of the color self.
  193. func (self Color) B() float32 {
  194. return float32(self.b)
  195. }
  196. // Returns the A component of the color self.
  197. func (self Color) A() float32 {
  198. return float32(self.a)
  199. }
  200. // Fills the current active display with a color
  201. func ClearToColor(color Color) {
  202. C.al_clear_to_color(color.toC())
  203. }
  204. // Draws a pixel on the active display at the given location
  205. // with the given color
  206. func DrawPixel(x, y float32, color Color) {
  207. C.al_draw_pixel(C.float(x), C.float(y), C.ALLEGRO_COLOR(color))
  208. }
  209. // Sets the refresh rate that the display should have after CreateDisplay().
  210. func SetNewDisplayRefreshRate(refresh_rate int) {
  211. C.al_set_new_display_refresh_rate(C.int(refresh_rate))
  212. }
  213. // Gets the refresh rate that the display should have after CreateDisplay().
  214. func NewDisplayRefreshRate() int {
  215. return int(C.al_get_new_display_refresh_rate())
  216. }
  217. // Gets the display flags that the display should have after CreateDisplay().
  218. func NewDisplayFlags() int {
  219. return int(C.al_get_new_display_flags())
  220. }
  221. // Gets the width of the display in pixels
  222. func (self *Display) Width() int {
  223. return int(C.al_get_display_width(self.handle))
  224. }
  225. // Gets the height of the display in pixels
  226. func (self *Display) Height() int {
  227. return int(C.al_get_display_height(self.handle))
  228. }
  229. // Gets the refresh rate of the display
  230. func (self *Display) RefreshRate() int {
  231. return int(C.al_get_display_refresh_rate(self.handle))
  232. }
  233. // Gets the display flags of the display
  234. func (self *Display) DisplayFlags() int {
  235. return int(C.al_get_display_flags(self.handle))
  236. }
  237. // Sets a dispay flag on the display
  238. func (self *Display) SetDisplayFlag(flag int, onoff bool) bool {
  239. return cb2b(C.al_set_display_flag(self.handle, C.int(flag), b2cb(onoff)))
  240. }
  241. // Returns the current display
  242. func CurrentDisplay() *Display {
  243. return wrapDisplayRaw(C.al_get_current_display())
  244. }
  245. // Sets the target C bitmap of allegro drawing
  246. func setTargetCBitmap(bmp *C.ALLEGRO_BITMAP) {
  247. C.al_set_target_bitmap(bmp)
  248. }
  249. // Sets the target bitmap of the allegro drawing
  250. func SetTargetBitmap(bmp Bitmap) {
  251. setTargetCBitmap(bmp.handle)
  252. }
  253. // Sets the target C backbuffer of allegro drawing
  254. func setTargetCBackbuffer(display *C.ALLEGRO_DISPLAY) {
  255. C.al_set_target_backbuffer(display)
  256. }
  257. // Sets the target backbuffer of allegro drawing
  258. func SetTargetBackbuffer(display *Display) {
  259. setTargetCBackbuffer(display.handle)
  260. }
  261. // Gets the backbuffer bitmap of the display
  262. func (self *Display) Backbuffer() *Bitmap {
  263. return wrapBitmapRaw(C.al_get_backbuffer(self.handle))
  264. }
  265. // Gets the target bitmap of allegro drawing
  266. func TargetBitmap() *Bitmap {
  267. return wrapBitmapRaw(C.al_get_target_bitmap())
  268. }
  269. // Must be called to aknowledge a RESIZE event
  270. func (self *Display) AcknowledgeResize() bool {
  271. return cb2b(C.al_acknowledge_resize(self.handle))
  272. }
  273. // Updates a region of the display (not the whole display like flip does)
  274. func UpdateDisplayRegion(x, y, width, height int) {
  275. C.al_update_display_region(C.int(x), C.int(y), C.int(width), C.int(height))
  276. }
  277. // Returns true of the bitmap is compatible with the currebt display, false if not.
  278. func (bitmap *Bitmap) IsCompatibleBitmap() bool {
  279. return cb2b(C.al_is_compatible_bitmap(bitmap.handle))
  280. }
  281. // Returns the number of display modes available to Allegro
  282. func NumDisplayModes() int {
  283. return int(C.al_get_num_display_modes())
  284. }
  285. // Returns the index'th display mode. Pass in a DisplayMode struct to store the display
  286. // mode info in.
  287. func (self *DisplayMode) Get(index int) *DisplayMode {
  288. return (*DisplayMode)(C.al_get_display_mode(C.int(index), self.toC()))
  289. }
  290. // Gets display mode info for the index'th display mode
  291. func GetDisplayMode(index int) *DisplayMode {
  292. var mode DisplayMode
  293. if (&mode).Get(index) != nil {
  294. return &mode
  295. }
  296. return nil
  297. }
  298. // Waits for the vertical retrace of the monitor to lessen tearing.
  299. func WaitForVsync() {
  300. C.al_wait_for_vsync()
  301. }
  302. // Gets the event source of the display to registeron an event queue
  303. // with RegisterEventSource.
  304. func (self *Display) GetEventSource() *EventSource {
  305. return (*EventSource)(C.al_get_display_event_source(self.handle))
  306. }
  307. // Sets the display icon the window manager should use for the display window
  308. func (self *Display) SetDisplayIcon(bitmap *Bitmap) {
  309. C.al_set_display_icon(self.handle, bitmap.handle)
  310. }
  311. // Gets the number of available video adapters (I.E. grapic cards)
  312. func NumVideoAdapters() int {
  313. return int(C.al_get_num_video_adapters())
  314. }
  315. // Converts a monitor info pointer to a C * ALLEGRO_MONITOR_INFO
  316. func (self *MonitorInfo) toC() *C.ALLEGRO_MONITOR_INFO {
  317. return (*C.ALLEGRO_MONITOR_INFO)(self)
  318. }
  319. // Gets the monitor info for the index'th video adapter
  320. func (self *MonitorInfo) Get(index int) bool {
  321. return cb2b(C.al_get_monitor_info(C.int(index), self.toC()))
  322. }
  323. // Gets the monitor info for the index'th video adapter
  324. func GetMonitorInfo(index int) *MonitorInfo {
  325. var info MonitorInfo
  326. if (&info).Get(index) {
  327. return &info
  328. }
  329. return nil
  330. }
  331. // Returns the number of the display adapter where new dsplays will be created
  332. func NewDisplayAdapter() int {
  333. return int(C.al_get_new_display_adapter())
  334. }
  335. // Sets the number of the display adapter where new dsplays will be created
  336. func SetNewDisplayAdapter(adapter int) {
  337. C.al_set_new_display_adapter(C.int(adapter))
  338. }
  339. // Returns the position where new windowed displays will be created
  340. func NewWindowPosition() (x, y int) {
  341. var cx, cy C.int
  342. C.al_get_new_window_position(&cx, &cy)
  343. return int(cx), int(cy)
  344. }
  345. // Sets the position where new windowed displays will be created
  346. func SetNewWindowPosition(x, y int) {
  347. C.al_set_new_window_position(C.int(x), C.int(y))
  348. }
  349. // Returns the current position of the windowed display
  350. func (self *Display) WindowPosition() (x, y int) {
  351. var cx, cy C.int
  352. C.al_get_window_position(self.handle, &cx, &cy)
  353. return int(cx), int(cy)
  354. }
  355. // Sets the position where new windowed displays will be created
  356. func (self *Display) SetWindowPosition(x, y int) {
  357. C.al_set_window_position(self.handle, C.int(x), C.int(y))
  358. }
  359. // Sets the title of the windowed display
  360. func (self *Display) SetTitle(str string) {
  361. cstr := cstr(str)
  362. defer cstrFree(cstr)
  363. C.al_set_window_title(self.handle, cstr)
  364. }
  365. // Sets a display option to be used when a new display is created
  366. func SetNewDisplayOption(option, value, importance int) {
  367. C.al_set_new_display_option(C.int(option), C.int(value), C.int(importance))
  368. }
  369. // Resets all display oprions for new displays to their default values.
  370. func ResetNewDisplayOptions() {
  371. C.al_reset_new_display_options()
  372. }
  373. // Gets the display option of this display
  374. func (self *Display) DisplayOption(option int) int {
  375. return int(C.al_get_display_option(self.handle, C.int(option)))
  376. }
  377. // Allows to speed up drawing by holding the display . Only bitmap functions and font
  378. // drawing, as well as tranformations shouldbe done until the hold is released
  379. func HoldBitmapDrawing(hold bool) {
  380. C.al_hold_bitmap_drawing(b2cb(hold))
  381. }
  382. // Returns whether or not the bitmap drawing was held
  383. func IsBitmapDrawingHeld() bool {
  384. return cb2b(C.al_is_bitmap_drawing_held())
  385. }