bitmap.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. // returns low level handle for the bitmap
  17. func (bmp *Bitmap) toC() * C.ALLEGRO_BITMAP {
  18. return bmp.handle
  19. }
  20. // Destroys a bitmap. Use this only when really needed!
  21. func (self *Bitmap) Destroy() {
  22. if self.handle != nil {
  23. C.al_destroy_bitmap(self.handle)
  24. }
  25. self.handle = nil
  26. }
  27. // Wraps a C Allegro bitmap in a Bitmap. Sets no finalizer.
  28. func wrapBitmapRaw(handle *C.ALLEGRO_BITMAP) *Bitmap {
  29. if handle == nil {
  30. return nil
  31. }
  32. return &Bitmap{handle}
  33. }
  34. // Wraps a C Allegro Bitmap in a Bitmap. Sets a finalizer that calls Destroy.
  35. func wrapBitmap(handle *C.ALLEGRO_BITMAP) *Bitmap {
  36. self := wrapBitmapRaw(handle)
  37. if self != nil {
  38. runtime.SetFinalizer(self, func(me *Bitmap) { me.Destroy() })
  39. }
  40. return self
  41. }
  42. const (
  43. MEMORY_BITMAP = C.ALLEGRO_MEMORY_BITMAP
  44. FORCE_LOCKING = C.ALLEGRO_FORCE_LOCKING
  45. NO_PRESERVE_TEXTURE = C.ALLEGRO_NO_PRESERVE_TEXTURE
  46. MIN_LINEAR = C.ALLEGRO_MIN_LINEAR
  47. MAG_LINEAR = C.ALLEGRO_MAG_LINEAR
  48. MIPMAP = C.ALLEGRO_MIPMAP
  49. VIDEO_BITMAP = C.ALLEGRO_VIDEO_BITMAP
  50. CONVERT_BITMAP = C.ALLEGRO_CONVERT_BITMAP
  51. )
  52. // Sets the format for new bitmaps that are created using CreateBitmap
  53. func SetNewBitmapFormat(format int) {
  54. C.al_set_new_bitmap_format(C.int(format))
  55. }
  56. // Sets the flags for new bitmaps that are created using CreateBitmap
  57. func SetNewBitmapFlags(flags int) {
  58. C.al_set_new_bitmap_flags(C.int(flags))
  59. }
  60. // Adds a flags to the flags that will be used for new bitmaps that are created
  61. // using CreateBitmap
  62. func AddNewBitmapFlag(flags int) {
  63. C.al_add_new_bitmap_flag(C.int(flags))
  64. }
  65. // Gets the format for new bitmaps that are created using CreateBitmap
  66. func NewBitmapFormat(format int) int {
  67. return int(C.al_get_new_bitmap_format())
  68. }
  69. // Gets the flags for new bitmaps that are created using CreateBitmap
  70. func NewBitmapFlags(flags int) int {
  71. return int(C.al_get_new_bitmap_flags())
  72. }
  73. // Gets the width of the bitmap.
  74. func (self *Bitmap) Width() int {
  75. return int(C.al_get_bitmap_width(self.handle))
  76. }
  77. // Gets the height of the bitmap.
  78. func (self *Bitmap) Height() int {
  79. return int(C.al_get_bitmap_height(self.handle))
  80. }
  81. // Gets the width of the bitmap as a float32.
  82. func (bmp *Bitmap) Widthf() float32 {
  83. return float32(bmp.Width())
  84. }
  85. // Gets the height of the bitmap as a float32.
  86. func (bmp *Bitmap) Heightf() float32 {
  87. return float32(bmp.Height())
  88. }
  89. // Gets the format of the bitmap.
  90. func (self *Bitmap) Format() int {
  91. return int(C.al_get_bitmap_format(self.handle))
  92. }
  93. // Gets the flags of the bitmap.
  94. func (self *Bitmap) Flags() int {
  95. return int(C.al_get_bitmap_flags(self.handle))
  96. }
  97. // Creates a new RAW bitmap. It will not be automatically destroyed!
  98. func CreateBitmapRaw(w, h int) *Bitmap {
  99. return wrapBitmapRaw(C.al_create_bitmap(C.int(w), C.int(h)))
  100. }
  101. // Creates a new bitmap. It has a finalizer in place that will let it be automatically
  102. // destroyed.
  103. func CreateBitmap(w, h int) *Bitmap {
  104. return wrapBitmap(C.al_create_bitmap(C.int(w), C.int(h)))
  105. }
  106. // Puts a pixel to the currently active bitmap
  107. func PutPixel(x, y int, color Color) {
  108. C.al_put_pixel(C.int(x), C.int(y), color.toC())
  109. }
  110. // Blends a pixel to the currently active bitmap
  111. func PutBlendedPixel(x, y int, color Color) {
  112. C.al_put_blended_pixel(C.int(x), C.int(y), color.toC())
  113. }
  114. // Gets a pixel from the bitmap
  115. func (self *Bitmap) GetPixel(x, y int) (color Color) {
  116. return wrapColor(C.al_get_pixel(self.handle, C.int(x), C.int(y)))
  117. }
  118. // Converts pixels of the mask color to transparent pixels (with an alpha channel)
  119. // for the given bitmap. Useful for, say "magic pink" backgrounds.
  120. func (self *Bitmap) ConvertMaskToAlpha(mask_color Color) {
  121. C.al_convert_mask_to_alpha(self.handle, mask_color.toC())
  122. }
  123. // Sets the clipping rectangle for the currently active bitmap. Anything drawn outside
  124. // this rectangle will be cut off or "clipped".
  125. func SetClippingRectangle(x, y, w, h int) {
  126. C.al_set_clipping_rectangle(C.int(x), C.int(y), C.int(w), C.int(h))
  127. }
  128. // Resets the clipping rectangle for the currently active bitmap to the full size.
  129. func ResetClippingRectangle() {
  130. C.al_reset_clipping_rectangle()
  131. }
  132. // Gets the clipping rectangle for the currently active bitmap.
  133. func ClippingRectangle() (x, y, w, h int) {
  134. var cx, cy, cw, ch C.int
  135. C.al_get_clipping_rectangle(&cx, &cy, &cw, &ch)
  136. return int(cx), int(cy), int(cw), int(ch)
  137. }
  138. // Creates a RAW sub bitmap of the given bitmap that must be manually destoyed with
  139. // Destroy()
  140. func (self *Bitmap) CreateSubBitmapRaw(x, y, w, h int) *Bitmap {
  141. return wrapBitmapRaw(C.al_create_sub_bitmap(self.handle,
  142. C.int(x), C.int(y), C.int(w), C.int(h)))
  143. }
  144. // Creates a sub bitmap of the given bitmap that will automatically be destoyed
  145. // through a finalizer. However, you must ensure that this destroy happens before the
  146. // parent bitmap is disposed of. You may need to call Destroy manyally anyway.
  147. func (self *Bitmap) CreateSubBitmap(x, y, w, h int) *Bitmap {
  148. return wrapBitmap(C.al_create_sub_bitmap(self.handle,
  149. C.int(x), C.int(y), C.int(w), C.int(h)))
  150. }
  151. // Returns whether or not the bitmap is a sub bitmap
  152. func (self *Bitmap) IsSubBitmap() bool {
  153. return cb2b(C.al_is_sub_bitmap(self.handle))
  154. }
  155. // Returns the parent bitmap of this sub bitmap, or nil if there is no parent.
  156. // This is a raw bitmap that has no finaliser set on it since likely
  157. /// this function will only be used for inspection.
  158. func (self *Bitmap) Parent() *Bitmap {
  159. return wrapBitmapRaw(C.al_get_parent_bitmap(self.handle))
  160. }
  161. // Gets the Returns the X position within the parent of a sub bitmap
  162. func (bmp *Bitmap) SubX() int {
  163. return int(C.al_get_bitmap_x(bmp.handle))
  164. }
  165. // Gets the Returns the Y position within the parent of a sub bitmap
  166. func (bmp *Bitmap) SubY() int {
  167. return int(C.al_get_bitmap_y(bmp.handle))
  168. }
  169. // Changes the parent, size and position of a sub bitmap
  170. func (bmp *Bitmap) Reparent(parent * Bitmap, x, y, w, h int) {
  171. C.al_reparent_bitmap(bmp.handle, parent.handle, C.int(x), C.int(y), C.int(w), C.int(h))
  172. }
  173. // Returns a raw clone of the bitmap, that will not be automatically
  174. // destroyed.
  175. func (self *Bitmap) CloneRaw() *Bitmap {
  176. return wrapBitmapRaw(C.al_clone_bitmap(self.handle))
  177. }
  178. // Returns a clone of the bitmap, that will automatically be
  179. // destroyed.
  180. func (self *Bitmap) Clone() *Bitmap {
  181. return wrapBitmap(C.al_clone_bitmap(self.handle))
  182. }
  183. // Converts the bitmap to the current screen format, to ensure the blitting goes fast
  184. func (self *Bitmap) Convert() {
  185. C.al_convert_bitmap(self.handle)
  186. }
  187. // Converts all known unconverted memory bitmaps to the current screen format,
  188. // to ensure the blitting goes fast.
  189. func ConvertMemoryBitmaps() {
  190. C.al_convert_memory_bitmaps()
  191. }
  192. const (
  193. LOCK_READWRITE = C.ALLEGRO_LOCK_READWRITE
  194. LOCK_READONLY = C.ALLEGRO_LOCK_READONLY
  195. LOCK_WRITEONLY = C.ALLEGRO_LOCK_WRITEONLY
  196. )
  197. type LockedRegion = C.ALLEGRO_LOCKED_REGION
  198. // TODO: Provide better access to the data member if needed
  199. func (lk * LockedRegion) dataPointer() unsafe.Pointer {
  200. return unsafe.Pointer(lk.data)
  201. }
  202. func (lk * LockedRegion) Format() int {
  203. return int(lk.format)
  204. }
  205. func (lk * LockedRegion) Pitch() int {
  206. return int(lk.pitch)
  207. }
  208. func (lk * LockedRegion) PixelSize() int {
  209. return int(lk.pixel_size)
  210. }
  211. func (bmp * Bitmap) Lock(format, flags int) *LockedRegion {
  212. return (*LockedRegion)(C.al_lock_bitmap(bmp.handle, C.int(format), C.int(flags)))
  213. }
  214. func (bmp * Bitmap) LockRegion(x, y, width, height, format, flags int) *LockedRegion {
  215. return (*LockedRegion)(C.al_lock_bitmap_region(bmp.handle,
  216. C.int(x), C.int(y), C.int(width), C.int(height), C.int(format), C.int(flags)))
  217. }
  218. func (bmp * Bitmap) LockBlocked(flags int) *LockedRegion {
  219. return (*LockedRegion)(C.al_lock_bitmap_blocked(bmp.handle, C.int(flags)))
  220. }
  221. func (bmp * Bitmap) LockRegionBlocked(x, y, width, height, flags int) *LockedRegion {
  222. return (*LockedRegion)(C.al_lock_bitmap_region_blocked(bmp.handle,
  223. C.int(x), C.int(y), C.int(width), C.int(height), C.int(flags)))
  224. }
  225. func (bmp * Bitmap) Unlock(){
  226. C.al_unlock_bitmap(bmp.handle)
  227. }
  228. func (bmp * Bitmap) Locked() bool {
  229. return bool(C.al_is_bitmap_locked(bmp.handle))
  230. }