bitmap.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. import "unsafe"
  11. // #include "_cgo_export.h" doesn't work
  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. /* TODO:
  95. AL_FUNC(ALLEGRO_BITMAP*, al_create_custom_bitmap, (int w, int h, bool (*upload)(ALLEGRO_BITMAP *bitmap, void *data), void *data));
  96. */
  97. type UploadBitmapC func(bitmap C.ALLEGRO_BITMAP, data unsafe.Pointer) C.bool
  98. type UploadBitmapFunction func(bitmap *Bitmap, data unsafe.Pointer) bool
  99. func (upload *UploadBitmapFunction) toC() UploadBitmapC {
  100. // res := func
  101. return nil
  102. }
  103. func CreateCustomBitmapRaw(w, h int, upload UploadBitmapFunction, data unsafe.Pointer) *Bitmap {
  104. fn := (C.function_pointer)(unsafe.Pointer(C.go_upload_bitmap_cb))
  105. return wrapBitmapRaw(C.al_create_custom_bitmap(C.int(w), C.int(h), fn, data))
  106. }
  107. // Puts a pixel to the currently active bitmap
  108. func PutPixel(x, y int, color Color) {
  109. C.al_put_pixel(C.int(x), C.int(y), color.toC())
  110. }
  111. // Blends a pixel to the currently active bitmap
  112. func PutBlendedPixel(x, y int, color Color) {
  113. C.al_put_blended_pixel(C.int(x), C.int(y), color.toC())
  114. }
  115. // Gets a pixel from the bitmap
  116. func (self *Bitmap) GetPixel(x, y int) (color Color) {
  117. return wrapColor(C.al_get_pixel(self.handle, C.int(x), C.int(y)))
  118. }
  119. // Converts pixels of the mask color to transparent pixels (with an alpha channel)
  120. // for the given bitmap. Useful for, say "magic pink" backgrounds.
  121. func (self *Bitmap) ConvertMaskToAlpha(mask_color Color) {
  122. C.al_convert_mask_to_alpha(self.handle, mask_color.toC())
  123. }
  124. // Sets the clipping rectangle for the currently active bitmap. Anything drawn outside
  125. // this rectangle will be cut off or "clipped".
  126. func SetClippingRectangle(x, y, w, h int) {
  127. C.al_set_clipping_rectangle(C.int(x), C.int(y), C.int(w), C.int(h))
  128. }
  129. // Resets the clipping rectangle for the currently active bitmap to the full size.
  130. func ResetClippingRectangle() {
  131. C.al_reset_clipping_rectangle()
  132. }
  133. // Gets the clipping rectangle for the currently active bitmap.
  134. func ClippingRectangle() (x, y, w, h int) {
  135. var cx, cy, cw, ch C.int
  136. C.al_get_clipping_rectangle(&cx, &cy, &cw, &ch)
  137. return int(cx), int(cy), int(cw), int(ch)
  138. }
  139. // Creates a RAW sub bitmap of the given bitmap that must be manually destoyed with
  140. // Destroy()
  141. func (self *Bitmap) CreateSubBitmapRaw(x, y, w, h int) *Bitmap {
  142. return wrapBitmapRaw(C.al_create_sub_bitmap(self.handle,
  143. C.int(x), C.int(y), C.int(w), C.int(h)))
  144. }
  145. // Creates a sub bitmap of the given bitmap that will automatically be destoyed
  146. // through a finalizer. However, you must ensure that this destroy happens before the
  147. // parent bitmap is disposed of. You may need to call Destroy manyally anyway.
  148. func (self *Bitmap) CreateSubBitmap(x, y, w, h int) *Bitmap {
  149. return wrapBitmap(C.al_create_sub_bitmap(self.handle,
  150. C.int(x), C.int(y), C.int(w), C.int(h)))
  151. }
  152. // Returns whether or not the bitmap is a sub bitmap
  153. func (self *Bitmap) IsSubBitmap() bool {
  154. return cb2b(C.al_is_sub_bitmap(self.handle))
  155. }
  156. // Returns the parent bitmap of this sub bitmap, or nil if there is no parent.
  157. // This is a raw bitmap that has no finaliser set on it since likely
  158. /// this function will only be used for inspection.
  159. func (self *Bitmap) Parent() *Bitmap {
  160. return wrapBitmapRaw(C.al_get_parent_bitmap(self.handle))
  161. }
  162. // Returns a raw clone of the bitmap, that will not be automatically
  163. // destroyed.
  164. func (self *Bitmap) CloneRaw() *Bitmap {
  165. return wrapBitmapRaw(C.al_clone_bitmap(self.handle))
  166. }
  167. // Returns a clone of the bitmap, that will automatically be
  168. // destroyed.
  169. func (self *Bitmap) Clone() *Bitmap {
  170. return wrapBitmap(C.al_clone_bitmap(self.handle))
  171. }
  172. // Converts the bitmap to the current screen format, to ensure the blitting goes fast
  173. func (self *Bitmap) Convert() {
  174. C.al_convert_bitmap(self.handle)
  175. }
  176. // Converts all known unconverted bitmaps to the current screen format,
  177. // to ensure the blitting goes fast.
  178. func ConvertBitmaps() {
  179. C.al_convert_bitmaps()
  180. }
  181. /*
  182. void qsort(void *base, size_t nmemb, size_t size,
  183. int (*compar)(const void *, const void *));
  184. */