bitmap.go 6.2 KB

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