Exposed new bitmap flag manipulation.

This commit is contained in:
Sander Schobers 2018-02-26 07:51:58 +01:00
parent 734d0db4b9
commit 5f12bfe3ef
3 changed files with 54 additions and 33 deletions

View File

@ -60,33 +60,14 @@ func NewBitmap(width, height int) (*Bitmap, error) {
return &Bitmap{b, width, height}, nil 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) // NewBitmapFromImage creates a new bitmap starting from a Go native image (image.Image)
func NewBitmapFromImage(im image.Image, video bool) (*Bitmap, error) { func NewBitmapFromImage(im image.Image, video bool) (*Bitmap, error) {
var newBmpFlags = captureNewBmpFlags() var newBmpFlags = CaptureNewBitmapFlags()
defer newBmpFlags.Revert() defer newBmpFlags.Revert()
newBmpFlags.Mut(func(m *flagMut) { newBmpFlags.Mutate(func(m FlagMutation) {
m.Unset(C.ALLEGRO_VIDEO_BITMAP) m.Unset(NewBitmapFlagVideoBitmap)
m.Set(C.ALLEGRO_MEMORY_BITMAP) m.Set(NewBitmapFlagMemoryBitmap)
m.Set(C.ALLEGRO_MIN_LINEAR) m.Set(NewBitmapFlagMinLinear)
}) })
var bnd = im.Bounds() var bnd = im.Bounds()
width, height := bnd.Dx(), bnd.Dy() width, height := bnd.Dx(), bnd.Dy()
@ -115,10 +96,10 @@ func NewBitmapFromImage(im image.Image, video bool) (*Bitmap, error) {
} }
C.al_unlock_bitmap(b) C.al_unlock_bitmap(b)
if video { if video {
newBmpFlags.Mut(func(m *flagMut) { newBmpFlags.Mutate(func(m FlagMutation) {
m.Unset(C.ALLEGRO_MEMORY_BITMAP) m.Unset(NewBitmapFlagMemoryBitmap)
m.Set(C.ALLEGRO_VIDEO_BITMAP) m.Set(NewBitmapFlagVideoBitmap)
m.Set(C.ALLEGRO_MIN_LINEAR) m.Set(NewBitmapFlagMinLinear)
}) })
C.al_convert_bitmap(b) C.al_convert_bitmap(b)
} }
@ -203,6 +184,10 @@ func (b *Bitmap) Height() int {
return b.height return b.height
} }
func (b *Bitmap) SetAsTarget() {
C.al_set_target_bitmap(b.bitmap)
}
// Destroy destroys the bitmap // Destroy destroys the bitmap
func (b *Bitmap) Destroy() { func (b *Bitmap) Destroy() {
C.al_destroy_bitmap(b.bitmap) C.al_destroy_bitmap(b.bitmap)

View File

@ -3,16 +3,21 @@ package allegro5
// #include <stdlib.h> // #include <stdlib.h>
import "C" import "C"
type FlagMutation interface {
Set(f NewBitmapFlag)
Unset(f NewBitmapFlag)
}
type flagMut struct { type flagMut struct {
flg C.int flg C.int
} }
func (m *flagMut) Set(f C.int) { func (m *flagMut) Set(f NewBitmapFlag) {
m.flg |= f m.flg |= C.int(f)
} }
func (m *flagMut) Unset(f C.int) { func (m *flagMut) Unset(f NewBitmapFlag) {
if m.flg&f == f { if m.flg&C.int(f) == C.int(f) {
m.flg ^= f m.flg ^= C.int(f)
} }
} }

31
allegro5/newbitmapflag.go Normal file
View File

@ -0,0 +1,31 @@
package allegro5
// #include <allegro5/allegro.h>
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)
}