2017-10-03 18:38:09 +00:00
|
|
|
package allegro5
|
|
|
|
|
|
|
|
// #include <allegro5/allegro.h>
|
|
|
|
// #include <stdlib.h>
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
2017-10-03 18:41:45 +00:00
|
|
|
"fmt"
|
2017-10-23 09:21:02 +00:00
|
|
|
"image"
|
|
|
|
"image/png"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2017-10-03 18:41:45 +00:00
|
|
|
"unsafe"
|
2017-10-03 18:38:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Bitmap represents an in memory bitmap
|
|
|
|
type Bitmap struct {
|
2017-10-03 18:41:45 +00:00
|
|
|
bitmap *C.ALLEGRO_BITMAP
|
|
|
|
width int
|
|
|
|
height int
|
2017-10-03 18:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type DrawOptions struct {
|
2017-10-23 09:21:02 +00:00
|
|
|
Center bool
|
|
|
|
Scale Scale
|
|
|
|
Tint *Color
|
|
|
|
Rotation *Rotation
|
|
|
|
}
|
|
|
|
|
|
|
|
type Scale interface {
|
|
|
|
Horizontal() float32
|
|
|
|
Vertical() float32
|
|
|
|
}
|
|
|
|
|
|
|
|
type scale struct {
|
|
|
|
horizontal float32
|
|
|
|
vertical float32
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *scale) Horizontal() float32 { return s.horizontal }
|
|
|
|
func (s *scale) Vertical() float32 { return s.vertical }
|
|
|
|
|
|
|
|
func NewScale(horizontal, vertical float32) Scale {
|
|
|
|
return &scale{horizontal, vertical}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUniformScale(s float32) Scale {
|
|
|
|
return &scale{s, s}
|
2017-10-03 18:38:09 +00:00
|
|
|
}
|
|
|
|
|
2017-10-23 09:21:02 +00:00
|
|
|
type Rotation struct {
|
|
|
|
Angle float32
|
|
|
|
Center bool
|
2017-10-03 18:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewBitmap creates a new bitmap of given width and height
|
|
|
|
func NewBitmap(width, height int) (*Bitmap, error) {
|
2017-10-03 18:41:45 +00:00
|
|
|
b := C.al_create_bitmap(C.int(width), C.int(height))
|
|
|
|
if nil == b {
|
|
|
|
return nil, fmt.Errorf("Error creating bitmap")
|
|
|
|
}
|
|
|
|
return &Bitmap{b, width, height}, nil
|
2017-10-03 18:38:09 +00:00
|
|
|
}
|
|
|
|
|
2017-10-23 09:21:02 +00:00
|
|
|
func exportToPng(im image.Image) (string, error) {
|
|
|
|
tmp, err := ioutil.TempFile("", "galleg")
|
|
|
|
if nil != err {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer tmp.Close()
|
|
|
|
err = png.Encode(tmp, im)
|
|
|
|
if nil != err {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return tmp.Name(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBitmapFromImage creates a new bitmap starting from a Go native image (image.Image)
|
|
|
|
func NewBitmapFromImage(im image.Image) (*Bitmap, error) {
|
|
|
|
path, err := exportToPng(im)
|
|
|
|
if nil != err {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer os.Remove(path)
|
|
|
|
return LoadBitmap(path)
|
|
|
|
}
|
|
|
|
|
2017-10-03 18:41:45 +00:00
|
|
|
// LoadBitmap tries to load the image at the specified path as a bitmap
|
2017-10-03 18:38:09 +00:00
|
|
|
func LoadBitmap(path string) (*Bitmap, error) {
|
2017-10-03 18:41:45 +00:00
|
|
|
p := C.CString(path)
|
|
|
|
defer C.free(unsafe.Pointer(p))
|
|
|
|
b := C.al_load_bitmap(p)
|
|
|
|
if nil == b {
|
|
|
|
return nil, fmt.Errorf("Error loading bitmap")
|
|
|
|
}
|
|
|
|
width := int(C.al_get_bitmap_width(b))
|
|
|
|
height := int(C.al_get_bitmap_height(b))
|
|
|
|
return &Bitmap{b, width, height}, nil
|
2017-10-03 18:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Draw draws the bitmap at the given location
|
|
|
|
func (b *Bitmap) Draw(left, top float32) {
|
2017-10-03 18:41:45 +00:00
|
|
|
C.al_draw_bitmap(b.bitmap, C.float(left), C.float(top), 0)
|
2017-10-03 18:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bitmap) DrawOptions(left, top float32, options DrawOptions) {
|
2017-10-03 18:41:45 +00:00
|
|
|
width := float32(b.width)
|
|
|
|
height := float32(b.height)
|
2017-10-03 18:38:09 +00:00
|
|
|
|
2017-10-03 18:41:45 +00:00
|
|
|
scale := nil != options.Scale
|
|
|
|
if scale {
|
2017-10-23 09:21:02 +00:00
|
|
|
width *= options.Scale.Horizontal()
|
|
|
|
height *= options.Scale.Vertical()
|
2017-10-03 18:41:45 +00:00
|
|
|
}
|
|
|
|
if options.Center {
|
|
|
|
left -= width * 0.5
|
|
|
|
top -= height * 0.5
|
|
|
|
}
|
2017-10-23 09:21:02 +00:00
|
|
|
rotated := nil != options.Rotation
|
|
|
|
var centerX C.float
|
|
|
|
var centerY C.float
|
|
|
|
if rotated && options.Rotation.Center {
|
|
|
|
centerX = C.float(b.width) * 0.5
|
|
|
|
centerY = C.float(b.height) * 0.5
|
|
|
|
}
|
2017-10-03 18:41:45 +00:00
|
|
|
|
|
|
|
if scale {
|
2017-10-23 09:21:02 +00:00
|
|
|
if nil == options.Tint { // scaled
|
|
|
|
if rotated { // scaled & rotated
|
|
|
|
C.al_draw_scaled_rotated_bitmap(b.bitmap, centerX, centerY, C.float(left), C.float(top), C.float(options.Scale.Horizontal()), C.float(options.Scale.Vertical()), C.float(options.Rotation.Angle), 0)
|
|
|
|
} else { // scaled
|
|
|
|
C.al_draw_scaled_bitmap(b.bitmap, 0, 0, C.float(b.width), C.float(b.height), C.float(left), C.float(top), C.float(width), C.float(height), 0)
|
|
|
|
}
|
|
|
|
} else { // tinted & scaled
|
|
|
|
if rotated { // scaled, tinted & rotated
|
|
|
|
C.al_draw_tinted_scaled_rotated_bitmap(b.bitmap, options.Tint.color, centerX, centerY, C.float(left), C.float(top), C.float(options.Scale.Horizontal()), C.float(options.Scale.Vertical()), C.float(options.Rotation.Angle), 0)
|
|
|
|
} else { // tinted, scaled
|
|
|
|
C.al_draw_tinted_scaled_bitmap(b.bitmap, options.Tint.color, 0, 0, C.float(b.width), C.float(b.height), C.float(left), C.float(top), C.float(width), C.float(height), 0)
|
|
|
|
}
|
2017-10-03 18:41:45 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if nil == options.Tint {
|
2017-10-23 09:21:02 +00:00
|
|
|
if rotated { // rotated
|
|
|
|
C.al_draw_rotated_bitmap(b.bitmap, centerX, centerY, C.float(left), C.float(top), C.float(options.Rotation.Angle), 0)
|
|
|
|
} else {
|
|
|
|
C.al_draw_bitmap(b.bitmap, C.float(left), C.float(top), 0)
|
|
|
|
}
|
|
|
|
} else { // tinted
|
|
|
|
if rotated { // tinted & rotated
|
|
|
|
C.al_draw_tinted_rotated_bitmap(b.bitmap, options.Tint.color, centerX, centerY, C.float(left), C.float(top), C.float(options.Rotation.Angle), 0)
|
|
|
|
} else {
|
|
|
|
C.al_draw_tinted_bitmap(b.bitmap, options.Tint.color, C.float(left), C.float(top), 0)
|
|
|
|
}
|
2017-10-03 18:41:45 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-03 18:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bitmap) Width() int {
|
2017-10-03 18:41:45 +00:00
|
|
|
return b.width
|
2017-10-03 18:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bitmap) Height() int {
|
2017-10-03 18:41:45 +00:00
|
|
|
return b.height
|
2017-10-03 18:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy destroys the bitmap
|
|
|
|
func (b *Bitmap) Destroy() {
|
2017-10-03 18:41:45 +00:00
|
|
|
C.al_destroy_bitmap(b.bitmap)
|
|
|
|
}
|