albitmap.go 6.0 KB

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