123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- // albitmap
- package al
- /*
- #include <stdlib.h>
- #include <allegro5/allegro.h>
- #include "helpers.h"
- */
- import "C"
- import "runtime"
- import "unsafe"
- // Type that wraps a Bitmap
- type Bitmap struct {
- handle *C.ALLEGRO_BITMAP
- }
- // returns low level handle for the bitmap
- func (bmp *Bitmap) toC() * C.ALLEGRO_BITMAP {
- return bmp.handle
- }
- // Destroys a bitmap. Use this only when really needed!
- func (self *Bitmap) Destroy() {
- if self.handle != nil {
- C.al_destroy_bitmap(self.handle)
- }
- self.handle = nil
- }
- // Wraps a C Allegro bitmap in a Bitmap. Sets no finalizer.
- func wrapBitmapRaw(handle *C.ALLEGRO_BITMAP) *Bitmap {
- if handle == nil {
- return nil
- }
- return &Bitmap{handle}
- }
- // Wraps a C Allegro Bitmap in a Bitmap. Sets a finalizer that calls Destroy.
- func wrapBitmap(handle *C.ALLEGRO_BITMAP) *Bitmap {
- self := wrapBitmapRaw(handle)
- if self != nil {
- runtime.SetFinalizer(self, func(me *Bitmap) { me.Destroy() })
- }
- return self
- }
- const (
- MEMORY_BITMAP = C.ALLEGRO_MEMORY_BITMAP
- FORCE_LOCKING = C.ALLEGRO_FORCE_LOCKING
- NO_PRESERVE_TEXTURE = C.ALLEGRO_NO_PRESERVE_TEXTURE
- MIN_LINEAR = C.ALLEGRO_MIN_LINEAR
- MAG_LINEAR = C.ALLEGRO_MAG_LINEAR
- MIPMAP = C.ALLEGRO_MIPMAP
- VIDEO_BITMAP = C.ALLEGRO_VIDEO_BITMAP
- CONVERT_BITMAP = C.ALLEGRO_CONVERT_BITMAP
- )
- // Sets the format for new bitmaps that are created using CreateBitmap
- func SetNewBitmapFormat(format int) {
- C.al_set_new_bitmap_format(C.int(format))
- }
- // Sets the flags for new bitmaps that are created using CreateBitmap
- func SetNewBitmapFlags(flags int) {
- C.al_set_new_bitmap_flags(C.int(flags))
- }
- // Adds a flags to the flags that will be used for new bitmaps that are created
- // using CreateBitmap
- func AddNewBitmapFlag(flags int) {
- C.al_add_new_bitmap_flag(C.int(flags))
- }
- // Gets the format for new bitmaps that are created using CreateBitmap
- func NewBitmapFormat(format int) int {
- return int(C.al_get_new_bitmap_format())
- }
- // Gets the flags for new bitmaps that are created using CreateBitmap
- func NewBitmapFlags(flags int) int {
- return int(C.al_get_new_bitmap_flags())
- }
- // Gets the width of the bitmap.
- func (self *Bitmap) Width() int {
- return int(C.al_get_bitmap_width(self.handle))
- }
- // Gets the height of the bitmap.
- func (self *Bitmap) Height() int {
- return int(C.al_get_bitmap_height(self.handle))
- }
- // Gets the width of the bitmap as a float32.
- func (bmp *Bitmap) Widthf() float32 {
- return float32(bmp.Width())
- }
- // Gets the height of the bitmap as a float32.
- func (bmp *Bitmap) Heightf() float32 {
- return float32(bmp.Height())
- }
- // Gets the format of the bitmap.
- func (self *Bitmap) Format() int {
- return int(C.al_get_bitmap_format(self.handle))
- }
- // Gets the flags of the bitmap.
- func (self *Bitmap) Flags() int {
- return int(C.al_get_bitmap_flags(self.handle))
- }
- // Creates a new RAW bitmap. It will not be automatically destroyed!
- func CreateBitmapRaw(w, h int) *Bitmap {
- return wrapBitmapRaw(C.al_create_bitmap(C.int(w), C.int(h)))
- }
- // Creates a new bitmap. It has a finalizer in place that will let it be automatically
- // destroyed.
- func CreateBitmap(w, h int) *Bitmap {
- return wrapBitmap(C.al_create_bitmap(C.int(w), C.int(h)))
- }
- // Puts a pixel to the currently active bitmap
- func PutPixel(x, y int, color Color) {
- C.al_put_pixel(C.int(x), C.int(y), color.toC())
- }
- // Blends a pixel to the currently active bitmap
- func PutBlendedPixel(x, y int, color Color) {
- C.al_put_blended_pixel(C.int(x), C.int(y), color.toC())
- }
- // Gets a pixel from the bitmap
- func (self *Bitmap) GetPixel(x, y int) (color Color) {
- return wrapColor(C.al_get_pixel(self.handle, C.int(x), C.int(y)))
- }
- // Converts pixels of the mask color to transparent pixels (with an alpha channel)
- // for the given bitmap. Useful for, say "magic pink" backgrounds.
- func (self *Bitmap) ConvertMaskToAlpha(mask_color Color) {
- C.al_convert_mask_to_alpha(self.handle, mask_color.toC())
- }
- // Sets the clipping rectangle for the currently active bitmap. Anything drawn outside
- // this rectangle will be cut off or "clipped".
- func SetClippingRectangle(x, y, w, h int) {
- C.al_set_clipping_rectangle(C.int(x), C.int(y), C.int(w), C.int(h))
- }
- // Resets the clipping rectangle for the currently active bitmap to the full size.
- func ResetClippingRectangle() {
- C.al_reset_clipping_rectangle()
- }
- // Gets the clipping rectangle for the currently active bitmap.
- func ClippingRectangle() (x, y, w, h int) {
- var cx, cy, cw, ch C.int
- C.al_get_clipping_rectangle(&cx, &cy, &cw, &ch)
- return int(cx), int(cy), int(cw), int(ch)
- }
- // Creates a RAW sub bitmap of the given bitmap that must be manually destoyed with
- // Destroy()
- func (self *Bitmap) CreateSubBitmapRaw(x, y, w, h int) *Bitmap {
- return wrapBitmapRaw(C.al_create_sub_bitmap(self.handle,
- C.int(x), C.int(y), C.int(w), C.int(h)))
- }
- // Creates a sub bitmap of the given bitmap that will automatically be destoyed
- // through a finalizer. However, you must ensure that this destroy happens before the
- // parent bitmap is disposed of. You may need to call Destroy manyally anyway.
- func (self *Bitmap) CreateSubBitmap(x, y, w, h int) *Bitmap {
- return wrapBitmap(C.al_create_sub_bitmap(self.handle,
- C.int(x), C.int(y), C.int(w), C.int(h)))
- }
- // Returns whether or not the bitmap is a sub bitmap
- func (self *Bitmap) IsSubBitmap() bool {
- return cb2b(C.al_is_sub_bitmap(self.handle))
- }
- // Returns the parent bitmap of this sub bitmap, or nil if there is no parent.
- // This is a raw bitmap that has no finaliser set on it since likely
- /// this function will only be used for inspection.
- func (self *Bitmap) Parent() *Bitmap {
- return wrapBitmapRaw(C.al_get_parent_bitmap(self.handle))
- }
- // Gets the Returns the X position within the parent of a sub bitmap
- func (bmp *Bitmap) SubX() int {
- return int(C.al_get_bitmap_x(bmp.handle))
- }
- // Gets the Returns the Y position within the parent of a sub bitmap
- func (bmp *Bitmap) SubY() int {
- return int(C.al_get_bitmap_y(bmp.handle))
- }
- // Changes the parent, size and position of a sub bitmap
- func (bmp *Bitmap) Reparent(parent * Bitmap, x, y, w, h int) {
- C.al_reparent_bitmap(bmp.handle, parent.handle, C.int(x), C.int(y), C.int(w), C.int(h))
- }
- // Returns a raw clone of the bitmap, that will not be automatically
- // destroyed.
- func (self *Bitmap) CloneRaw() *Bitmap {
- return wrapBitmapRaw(C.al_clone_bitmap(self.handle))
- }
- // Returns a clone of the bitmap, that will automatically be
- // destroyed.
- func (self *Bitmap) Clone() *Bitmap {
- return wrapBitmap(C.al_clone_bitmap(self.handle))
- }
- // Converts the bitmap to the current screen format, to ensure the blitting goes fast
- func (self *Bitmap) Convert() {
- C.al_convert_bitmap(self.handle)
- }
- // Converts all known unconverted memory bitmaps to the current screen format,
- // to ensure the blitting goes fast.
- func ConvertMemoryBitmaps() {
- C.al_convert_memory_bitmaps()
- }
- const (
- LOCK_READWRITE = C.ALLEGRO_LOCK_READWRITE
- LOCK_READONLY = C.ALLEGRO_LOCK_READONLY
- LOCK_WRITEONLY = C.ALLEGRO_LOCK_WRITEONLY
- )
- type LockedRegion = C.ALLEGRO_LOCKED_REGION
- // TODO: Provide better access to the data member if needed
- func (lk * LockedRegion) dataPointer() unsafe.Pointer {
- return unsafe.Pointer(lk.data)
- }
- func (lk * LockedRegion) Format() int {
- return int(lk.format)
- }
- func (lk * LockedRegion) Pitch() int {
- return int(lk.pitch)
- }
- func (lk * LockedRegion) PixelSize() int {
- return int(lk.pixel_size)
- }
- func (bmp * Bitmap) Lock(format, flags int) *LockedRegion {
- return (*LockedRegion)(C.al_lock_bitmap(bmp.handle, C.int(format), C.int(flags)))
- }
- func (bmp * Bitmap) LockRegion(x, y, width, height, format, flags int) *LockedRegion {
- return (*LockedRegion)(C.al_lock_bitmap_region(bmp.handle,
- C.int(x), C.int(y), C.int(width), C.int(height), C.int(format), C.int(flags)))
- }
- func (bmp * Bitmap) LockBlocked(flags int) *LockedRegion {
- return (*LockedRegion)(C.al_lock_bitmap_blocked(bmp.handle, C.int(flags)))
- }
- func (bmp * Bitmap) LockRegionBlocked(x, y, width, height, flags int) *LockedRegion {
- return (*LockedRegion)(C.al_lock_bitmap_region_blocked(bmp.handle,
- C.int(x), C.int(y), C.int(width), C.int(height), C.int(flags)))
- }
- func (bmp * Bitmap) Unlock(){
- C.al_unlock_bitmap(bmp.handle)
- }
- func (bmp * Bitmap) Locked() bool {
- return bool(C.al_is_bitmap_locked(bmp.handle))
- }
|