From 5f12bfe3efb23b7a5d2613c4bebbb8e57f48e4d0 Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Mon, 26 Feb 2018 07:51:58 +0100 Subject: [PATCH] Exposed new bitmap flag manipulation. --- allegro5/bitmap.go | 41 +++++++++++++-------------------------- allegro5/flagmut.go | 15 +++++++++----- allegro5/newbitmapflag.go | 31 +++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 33 deletions(-) create mode 100644 allegro5/newbitmapflag.go diff --git a/allegro5/bitmap.go b/allegro5/bitmap.go index ebb4af2..20b96bc 100644 --- a/allegro5/bitmap.go +++ b/allegro5/bitmap.go @@ -60,33 +60,14 @@ func NewBitmap(width, height int) (*Bitmap, error) { return &Bitmap{b, width, height}, nil } -type newBmpFlagsCapture struct { - cap C.int -} - -func captureNewBmpFlags() *newBmpFlagsCapture { - var cap = C.al_get_new_bitmap_flags() - return &newBmpFlagsCapture{cap} -} - -func (c *newBmpFlagsCapture) Mut(mut func(*flagMut)) { - var m = &flagMut{c.cap} - mut(m) - C.al_set_new_bitmap_flags(m.flg) -} - -func (c *newBmpFlagsCapture) Revert() { - C.al_set_new_bitmap_flags(c.cap) -} - // NewBitmapFromImage creates a new bitmap starting from a Go native image (image.Image) func NewBitmapFromImage(im image.Image, video bool) (*Bitmap, error) { - var newBmpFlags = captureNewBmpFlags() + var newBmpFlags = CaptureNewBitmapFlags() defer newBmpFlags.Revert() - newBmpFlags.Mut(func(m *flagMut) { - m.Unset(C.ALLEGRO_VIDEO_BITMAP) - m.Set(C.ALLEGRO_MEMORY_BITMAP) - m.Set(C.ALLEGRO_MIN_LINEAR) + newBmpFlags.Mutate(func(m FlagMutation) { + m.Unset(NewBitmapFlagVideoBitmap) + m.Set(NewBitmapFlagMemoryBitmap) + m.Set(NewBitmapFlagMinLinear) }) var bnd = im.Bounds() width, height := bnd.Dx(), bnd.Dy() @@ -115,10 +96,10 @@ func NewBitmapFromImage(im image.Image, video bool) (*Bitmap, error) { } C.al_unlock_bitmap(b) if video { - newBmpFlags.Mut(func(m *flagMut) { - m.Unset(C.ALLEGRO_MEMORY_BITMAP) - m.Set(C.ALLEGRO_VIDEO_BITMAP) - m.Set(C.ALLEGRO_MIN_LINEAR) + newBmpFlags.Mutate(func(m FlagMutation) { + m.Unset(NewBitmapFlagMemoryBitmap) + m.Set(NewBitmapFlagVideoBitmap) + m.Set(NewBitmapFlagMinLinear) }) C.al_convert_bitmap(b) } @@ -203,6 +184,10 @@ func (b *Bitmap) Height() int { return b.height } +func (b *Bitmap) SetAsTarget() { + C.al_set_target_bitmap(b.bitmap) +} + // Destroy destroys the bitmap func (b *Bitmap) Destroy() { C.al_destroy_bitmap(b.bitmap) diff --git a/allegro5/flagmut.go b/allegro5/flagmut.go index b4113c3..f4547d0 100644 --- a/allegro5/flagmut.go +++ b/allegro5/flagmut.go @@ -3,16 +3,21 @@ package allegro5 // #include import "C" +type FlagMutation interface { + Set(f NewBitmapFlag) + Unset(f NewBitmapFlag) +} + type flagMut struct { flg C.int } -func (m *flagMut) Set(f C.int) { - m.flg |= f +func (m *flagMut) Set(f NewBitmapFlag) { + m.flg |= C.int(f) } -func (m *flagMut) Unset(f C.int) { - if m.flg&f == f { - m.flg ^= f +func (m *flagMut) Unset(f NewBitmapFlag) { + if m.flg&C.int(f) == C.int(f) { + m.flg ^= C.int(f) } } diff --git a/allegro5/newbitmapflag.go b/allegro5/newbitmapflag.go new file mode 100644 index 0000000..645b135 --- /dev/null +++ b/allegro5/newbitmapflag.go @@ -0,0 +1,31 @@ +package allegro5 + +// #include +import "C" + +type NewBitmapFlag int + +const ( + NewBitmapFlagMemoryBitmap = NewBitmapFlag(C.ALLEGRO_MEMORY_BITMAP) + NewBitmapFlagVideoBitmap = NewBitmapFlag(C.ALLEGRO_VIDEO_BITMAP) + NewBitmapFlagMinLinear = NewBitmapFlag(C.ALLEGRO_MIN_LINEAR) +) + +type NewBitmapFlagsCapture struct { + cap C.int +} + +func CaptureNewBitmapFlags() *NewBitmapFlagsCapture { + var cap = C.al_get_new_bitmap_flags() + return &NewBitmapFlagsCapture{cap} +} + +func (c *NewBitmapFlagsCapture) Mutate(mut func(FlagMutation)) { + var m = &flagMut{c.cap} + mut(m) + C.al_set_new_bitmap_flags(m.flg) +} + +func (c *NewBitmapFlagsCapture) Revert() { + C.al_set_new_bitmap_flags(c.cap) +}