bitmap.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. // 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. // Gets the Returns the X position within the parent of a sub bitmap
  161. func (bmp *Bitmap) SubX() int {
  162. return int(C.al_get_bitmap_x(bmp.handle))
  163. }
  164. // Gets the Returns the Y position within the parent of a sub bitmap
  165. func (bmp *Bitmap) SubY() int {
  166. return int(C.al_get_bitmap_y(bmp.handle))
  167. }
  168. // Changes the parent, size and position of a sub bitmap
  169. func (bmp *Bitmap) Reparent(parent * Bitmap, x, y, w, h int) {
  170. C.al_reparent_bitmap(bmp.handle, parent.handle, C.int(x), C.int(y), C.int(w), C.int(h))
  171. }
  172. // Returns a raw clone of the bitmap, that will not be automatically
  173. // destroyed.
  174. func (self *Bitmap) CloneRaw() *Bitmap {
  175. return wrapBitmapRaw(C.al_clone_bitmap(self.handle))
  176. }
  177. // Returns a clone of the bitmap, that will automatically be
  178. // destroyed.
  179. func (self *Bitmap) Clone() *Bitmap {
  180. return wrapBitmap(C.al_clone_bitmap(self.handle))
  181. }
  182. // Converts the bitmap to the current screen format, to ensure the blitting goes fast
  183. func (self *Bitmap) Convert() {
  184. C.al_convert_bitmap(self.handle)
  185. }
  186. // Converts all known unconverted memory bitmaps to the current screen format,
  187. // to ensure the blitting goes fast.
  188. func ConvertMemoryBitmaps() {
  189. C.al_convert_memory_bitmaps()
  190. }
  191. const (
  192. LOCK_READWRITE = C.ALLEGRO_LOCK_READWRITE
  193. LOCK_READONLY = C.ALLEGRO_LOCK_READONLY
  194. LOCK_WRITEONLY = C.ALLEGRO_LOCK_WRITEONLY
  195. )
  196. type LockedRegion = C.ALLEGRO_LOCKED_REGION
  197. // TODO: Provide better access to the data member if needed
  198. func (lk * LockedRegion) dataPointer() unsafe.Pointer {
  199. return unsafe.Pointer(lk.data)
  200. }
  201. func (lk * LockedRegion) Format() int {
  202. return int(lk.format)
  203. }
  204. func (lk * LockedRegion) Pitch() int {
  205. return int(lk.pitch)
  206. }
  207. func (lk * LockedRegion) PixelSize() int {
  208. return int(lk.pixel_size)
  209. }
  210. func (bmp * Bitmap) Lock(format, flags int) *LockedRegion {
  211. return (*LockedRegion)(C.al_lock_bitmap(bmp.handle, C.int(format), C.int(flags)))
  212. }
  213. func (bmp * Bitmap) LockRegion(x, y, width, height, format, flags int) *LockedRegion {
  214. return (*LockedRegion)(C.al_lock_bitmap_region(bmp.handle,
  215. C.int(x), C.int(y), C.int(width), C.int(height), C.int(format), C.int(flags)))
  216. }
  217. func (bmp * Bitmap) LockBlocked(flags int) *LockedRegion {
  218. return (*LockedRegion)(C.al_lock_bitmap_blocked(bmp.handle, C.int(flags)))
  219. }
  220. func (bmp * Bitmap) LockRegionBlocked(x, y, width, height, flags int) *LockedRegion {
  221. return (*LockedRegion)(C.al_lock_bitmap_region_blocked(bmp.handle,
  222. C.int(x), C.int(y), C.int(width), C.int(height), C.int(flags)))
  223. }
  224. func (bmp * Bitmap) Unlock(){
  225. C.al_unlock_bitmap(bmp.handle)
  226. }
  227. func (bmp * Bitmap) Locked() bool {
  228. return bool(C.al_is_bitmap_locked(bmp.handle))
  229. }