Exposed new bitmap flag manipulation.
This commit is contained in:
parent
734d0db4b9
commit
5f12bfe3ef
@ -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)
|
||||
|
@ -3,16 +3,21 @@ package allegro5
|
||||
// #include <stdlib.h>
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
31
allegro5/newbitmapflag.go
Normal file
31
allegro5/newbitmapflag.go
Normal 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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user