bitmap.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // albitmap
  2. package al
  3. /*
  4. #include <stdlib.h>
  5. #include <allegro5/allegro.h>
  6. #include "helpers.h"
  7. #include "callbacks.h"
  8. */
  9. import "C"
  10. import "runtime"
  11. import "unsafe"
  12. // Type that wraps a Bitmap
  13. type Bitmap struct {
  14. handle *C.ALLEGRO_BITMAP
  15. }
  16. // Destroys a bitmap. Use this only when really needed!
  17. func (self *Bitmap) Destroy() {
  18. if self.handle != nil {
  19. C.al_destroy_bitmap(self.handle)
  20. }
  21. self.handle = nil
  22. }
  23. // Wraps a C Allegro bitmap in a Bitmap. Sets no finalizer.
  24. func wrapBitmapRaw(handle *C.ALLEGRO_BITMAP) *Bitmap {
  25. if handle == nil {
  26. return nil
  27. }
  28. return &Bitmap{handle}
  29. }
  30. // Wraps a C Allegro Bitmap in a Bitmap. Sets a finalizer that calls Destroy.
  31. func wrapBitmap(handle *C.ALLEGRO_BITMAP) *Bitmap {
  32. self := wrapBitmapRaw(handle)
  33. if self != nil {
  34. runtime.SetFinalizer(self, func(me *Bitmap) { me.Destroy() })
  35. }
  36. return self
  37. }
  38. const (
  39. MEMORY_BITMAP = C.ALLEGRO_MEMORY_BITMAP
  40. FORCE_LOCKING = C.ALLEGRO_FORCE_LOCKING
  41. NO_PRESERVE_TEXTURE = C.ALLEGRO_NO_PRESERVE_TEXTURE
  42. MIN_LINEAR = C.ALLEGRO_MIN_LINEAR
  43. MAG_LINEAR = C.ALLEGRO_MAG_LINEAR
  44. MIPMAP = C.ALLEGRO_MIPMAP
  45. VIDEO_BITMAP = C.ALLEGRO_VIDEO_BITMAP
  46. CONVERT_BITMAP = C.ALLEGRO_CONVERT_BITMAP
  47. )
  48. // Sets the format for new bitmaps that are created using CreateBitmap
  49. func SetNewBitmapFormat(format int) {
  50. C.al_set_new_bitmap_format(C.int(format))
  51. }
  52. // Sets the flags for new bitmaps that are created using CreateBitmap
  53. func SetNewBitmapFlags(flags int) {
  54. C.al_set_new_bitmap_flags(C.int(flags))
  55. }
  56. // Adds a flags to the flags that will be used for new bitmaps that are created
  57. // using CreateBitmap
  58. func AddNewBitmapFlag(flags int) {
  59. C.al_add_new_bitmap_flag(C.int(flags))
  60. }
  61. // Gets the format for new bitmaps that are created using CreateBitmap
  62. func NewBitmapFormat(format int) int {
  63. return int(C.al_get_new_bitmap_format())
  64. }
  65. // Gets the flags for new bitmaps that are created using CreateBitmap
  66. func NewBitmapFlags(flags int) int {
  67. return int(C.al_get_new_bitmap_flags())
  68. }
  69. // Gets the width of the bitmap.
  70. func (self *Bitmap) Width() int {
  71. return int(C.al_get_bitmap_width(self.handle))
  72. }
  73. // Gets the height of the bitmap.
  74. func (self *Bitmap) Height() int {
  75. return int(C.al_get_bitmap_height(self.handle))
  76. }
  77. // Gets the format of the bitmap.
  78. func (self *Bitmap) Format() int {
  79. return int(C.al_get_bitmap_format(self.handle))
  80. }
  81. // Gets the flags of the bitmap.
  82. func (self *Bitmap) Flags() int {
  83. return int(C.al_get_bitmap_flags(self.handle))
  84. }
  85. // Creates a new RAW bitmap. It will not be automatically destroyed!
  86. func CreateBitmapRaw(w, h int) *Bitmap {
  87. return wrapBitmapRaw(C.al_create_bitmap(C.int(w), C.int(h)))
  88. }
  89. // Creates a new bitmap. It has a finalizer in place that will let it be automatically
  90. // destroyed.
  91. func CreateBitmap(w, h int) *Bitmap {
  92. return wrapBitmap(C.al_create_bitmap(C.int(w), C.int(h)))
  93. }
  94. // Puts a pixel to the currently active bitmap
  95. func PutPixel(x, y int, color Color) {
  96. C.al_put_pixel(C.int(x), C.int(y), color.toC())
  97. }
  98. // Blends a pixel to the currently active bitmap
  99. func PutBlendedPixel(x, y int, color Color) {
  100. C.al_put_blended_pixel(C.int(x), C.int(y), color.toC())
  101. }
  102. // Gets a pixel from the bitmap
  103. func (self *Bitmap) GetPixel(x, y int) (color Color) {
  104. return wrapColor(C.al_get_pixel(self.handle, C.int(x), C.int(y)))
  105. }
  106. // Converts pixels of the mask color to transparent pixels (with an alpha channel)
  107. // for the given bitmap. Useful for, say "magic pink" backgrounds.
  108. func (self *Bitmap) ConvertMaskToAlpha(mask_color Color) {
  109. C.al_convert_mask_to_alpha(self.handle, mask_color.toC())
  110. }
  111. // Sets the clipping rectangle for the currently active bitmap. Anything drawn outside
  112. // this rectangle will be cut off or "clipped".
  113. func SetClippingRectangle(x, y, w, h int) {
  114. C.al_set_clipping_rectangle(C.int(x), C.int(y), C.int(w), C.int(h))
  115. }
  116. // Resets the clipping rectangle for the currently active bitmap to the full size.
  117. func ResetClippingRectangle() {
  118. C.al_reset_clipping_rectangle()
  119. }
  120. // Gets the clipping rectangle for the currently active bitmap.
  121. func ClippingRectangle() (x, y, w, h int) {
  122. var cx, cy, cw, ch C.int
  123. C.al_get_clipping_rectangle(&cx, &cy, &cw, &ch)
  124. return int(cx), int(cy), int(cw), int(ch)
  125. }
  126. // Creates a RAW sub bitmap of the given bitmap that must be manually destoyed with
  127. // Destroy()
  128. func (self *Bitmap) CreateSubBitmapRaw(x, y, w, h int) *Bitmap {
  129. return wrapBitmapRaw(C.al_create_sub_bitmap(self.handle,
  130. C.int(x), C.int(y), C.int(w), C.int(h)))
  131. }
  132. // Creates a sub bitmap of the given bitmap that will automatically be destoyed
  133. // through a finalizer. However, you must ensure that this destroy happens before the
  134. // parent bitmap is disposed of. You may need to call Destroy manyally anyway.
  135. func (self *Bitmap) CreateSubBitmap(x, y, w, h int) *Bitmap {
  136. return wrapBitmap(C.al_create_sub_bitmap(self.handle,
  137. C.int(x), C.int(y), C.int(w), C.int(h)))
  138. }
  139. // Returns whether or not the bitmap is a sub bitmap
  140. func (self *Bitmap) IsSubBitmap() bool {
  141. return cb2b(C.al_is_sub_bitmap(self.handle))
  142. }
  143. // Returns the parent bitmap of this sub bitmap, or nil if there is no parent.
  144. // This is a raw bitmap that has no finaliser set on it since likely
  145. /// this function will only be used for inspection.
  146. func (self *Bitmap) Parent() *Bitmap {
  147. return wrapBitmapRaw(C.al_get_parent_bitmap(self.handle))
  148. }
  149. // Returns a raw clone of the bitmap, that will not be automatically
  150. // destroyed.
  151. func (self *Bitmap) CloneRaw() *Bitmap {
  152. return wrapBitmapRaw(C.al_clone_bitmap(self.handle))
  153. }
  154. // Returns a clone of the bitmap, that will automatically be
  155. // destroyed.
  156. func (self *Bitmap) Clone() *Bitmap {
  157. return wrapBitmap(C.al_clone_bitmap(self.handle))
  158. }
  159. // Converts the bitmap to the current screen format, to ensure the blitting goes fast
  160. func (self *Bitmap) Convert() {
  161. C.al_convert_bitmap(self.handle)
  162. }
  163. // Converts all known unconverted bitmaps to the current screen format,
  164. // to ensure the blitting goes fast.
  165. func ConvertBitmaps() {
  166. C.al_convert_bitmaps()
  167. }
  168. /*
  169. void qsort(void *base, size_t nmemb, size_t size,
  170. int (*compar)(const void *, const void *));
  171. */
  172. // Creates a custom bitmap by calling the C wrapper
  173. func createCustomBitmap(w int, h int, upload CreateCustomBitmapCallback, data interface{}) *C.ALLEGRO_BITMAP {
  174. context := unsafe.Pointer(&createCustomBitmapContext{upload, data})
  175. var res *C.ALLEGRO_BITMAP = nil
  176. res = C.go_create_custom_bitmap(C.int(w), C.int(h), context)
  177. return res
  178. }
  179. // Creates a custom bitmap, raw, with no automatic Destroy set
  180. func CreateCustomBitmap(w int, h int, upload CreateCustomBitmapCallback, data interface{}) *Bitmap {
  181. return wrapBitmap(createCustomBitmap(w, h, upload, data))
  182. }
  183. // Creates a custom bitmap, raw, with no automatic Destroy set
  184. func CreateCustomBitmapRaw(w int, h int, upload CreateCustomBitmapCallback, data interface{}) *Bitmap {
  185. return wrapBitmapRaw(createCustomBitmap(w, h, upload, data))
  186. }