bitmap.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 width of the bitmap as a float32.
  81. func (bmp *Bitmap) Widthf() float32 {
  82. return float32(bmp.Width())
  83. }
  84. // Gets the height of the bitmap as a float32.
  85. func (bmp *Bitmap) Heightf() float32 {
  86. return float32(bmp.Height())
  87. }
  88. // Gets the format of the bitmap.
  89. func (self *Bitmap) Format() int {
  90. return int(C.al_get_bitmap_format(self.handle))
  91. }
  92. // Gets the flags of the bitmap.
  93. func (self *Bitmap) Flags() int {
  94. return int(C.al_get_bitmap_flags(self.handle))
  95. }
  96. // Creates a new RAW bitmap. It will not be automatically destroyed!
  97. func CreateBitmapRaw(w, h int) *Bitmap {
  98. return wrapBitmapRaw(C.al_create_bitmap(C.int(w), C.int(h)))
  99. }
  100. // Creates a new bitmap. It has a finalizer in place that will let it be automatically
  101. // destroyed.
  102. func CreateBitmap(w, h int) *Bitmap {
  103. return wrapBitmap(C.al_create_bitmap(C.int(w), C.int(h)))
  104. }
  105. // Puts a pixel to the currently active bitmap
  106. func PutPixel(x, y int, color Color) {
  107. C.al_put_pixel(C.int(x), C.int(y), color.toC())
  108. }
  109. // Blends a pixel to the currently active bitmap
  110. func PutBlendedPixel(x, y int, color Color) {
  111. C.al_put_blended_pixel(C.int(x), C.int(y), color.toC())
  112. }
  113. // Gets a pixel from the bitmap
  114. func (self *Bitmap) GetPixel(x, y int) (color Color) {
  115. return wrapColor(C.al_get_pixel(self.handle, C.int(x), C.int(y)))
  116. }
  117. // Converts pixels of the mask color to transparent pixels (with an alpha channel)
  118. // for the given bitmap. Useful for, say "magic pink" backgrounds.
  119. func (self *Bitmap) ConvertMaskToAlpha(mask_color Color) {
  120. C.al_convert_mask_to_alpha(self.handle, mask_color.toC())
  121. }
  122. // Sets the clipping rectangle for the currently active bitmap. Anything drawn outside
  123. // this rectangle will be cut off or "clipped".
  124. func SetClippingRectangle(x, y, w, h int) {
  125. C.al_set_clipping_rectangle(C.int(x), C.int(y), C.int(w), C.int(h))
  126. }
  127. // Resets the clipping rectangle for the currently active bitmap to the full size.
  128. func ResetClippingRectangle() {
  129. C.al_reset_clipping_rectangle()
  130. }
  131. // Gets the clipping rectangle for the currently active bitmap.
  132. func ClippingRectangle() (x, y, w, h int) {
  133. var cx, cy, cw, ch C.int
  134. C.al_get_clipping_rectangle(&cx, &cy, &cw, &ch)
  135. return int(cx), int(cy), int(cw), int(ch)
  136. }
  137. // Creates a RAW sub bitmap of the given bitmap that must be manually destoyed with
  138. // Destroy()
  139. func (self *Bitmap) CreateSubBitmapRaw(x, y, w, h int) *Bitmap {
  140. return wrapBitmapRaw(C.al_create_sub_bitmap(self.handle,
  141. C.int(x), C.int(y), C.int(w), C.int(h)))
  142. }
  143. // Creates a sub bitmap of the given bitmap that will automatically be destoyed
  144. // through a finalizer. However, you must ensure that this destroy happens before the
  145. // parent bitmap is disposed of. You may need to call Destroy manyally anyway.
  146. func (self *Bitmap) CreateSubBitmap(x, y, w, h int) *Bitmap {
  147. return wrapBitmap(C.al_create_sub_bitmap(self.handle,
  148. C.int(x), C.int(y), C.int(w), C.int(h)))
  149. }
  150. // Returns whether or not the bitmap is a sub bitmap
  151. func (self *Bitmap) IsSubBitmap() bool {
  152. return cb2b(C.al_is_sub_bitmap(self.handle))
  153. }
  154. // Returns the parent bitmap of this sub bitmap, or nil if there is no parent.
  155. // This is a raw bitmap that has no finaliser set on it since likely
  156. /// this function will only be used for inspection.
  157. func (self *Bitmap) Parent() *Bitmap {
  158. return wrapBitmapRaw(C.al_get_parent_bitmap(self.handle))
  159. }
  160. // Returns a raw clone of the bitmap, that will not be automatically
  161. // destroyed.
  162. func (self *Bitmap) CloneRaw() *Bitmap {
  163. return wrapBitmapRaw(C.al_clone_bitmap(self.handle))
  164. }
  165. // Returns a clone of the bitmap, that will automatically be
  166. // destroyed.
  167. func (self *Bitmap) Clone() *Bitmap {
  168. return wrapBitmap(C.al_clone_bitmap(self.handle))
  169. }
  170. // Converts the bitmap to the current screen format, to ensure the blitting goes fast
  171. func (self *Bitmap) Convert() {
  172. C.al_convert_bitmap(self.handle)
  173. }
  174. // Converts all known unconverted memory bitmaps to the current screen format,
  175. // to ensure the blitting goes fast.
  176. func ConvertMemoryBitmaps() {
  177. C.al_convert_memory_bitmaps()
  178. }