From 27eae9dc8bb29d1df5ce0a24bed36f130d439d84 Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Tue, 3 Oct 2017 20:41:45 +0200 Subject: [PATCH] Fixup - bitmap.go format - c.go cleanup --- allegro5/bitmap.go | 110 ++++++++++++++++++++++----------------------- allegro5/c.go | 8 +--- 2 files changed, 57 insertions(+), 61 deletions(-) diff --git a/allegro5/bitmap.go b/allegro5/bitmap.go index 9f0f494..8460c12 100644 --- a/allegro5/bitmap.go +++ b/allegro5/bitmap.go @@ -5,93 +5,93 @@ package allegro5 import "C" import ( - "fmt" - "unsafe" + "fmt" + "unsafe" ) // Bitmap represents an in memory bitmap type Bitmap struct { - bitmap *C.ALLEGRO_BITMAP - width int - height int + bitmap *C.ALLEGRO_BITMAP + width int + height int } type DrawOptions struct { - Center bool - Scale *Scale - Tint *Color + Center bool + Scale *Scale + Tint *Color } type Scale struct { - Horizontal float32 - Vertical float32 + Horizontal float32 + Vertical float32 } // NewBitmap creates a new bitmap of given width and height func NewBitmap(width, height int) (*Bitmap, error) { - 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 + 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 } -// LoadBitmap tries to load the image at the specified path as a bitmap +// LoadBitmap tries to load the image at the specified path as a bitmap func LoadBitmap(path string) (*Bitmap, error) { - 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 + 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 } // Draw draws the bitmap at the given location func (b *Bitmap) Draw(left, top float32) { - C.al_draw_bitmap(b.bitmap, C.float(left), C.float(top), 0) + C.al_draw_bitmap(b.bitmap, C.float(left), C.float(top), 0) } func (b *Bitmap) DrawOptions(left, top float32, options DrawOptions) { - width := float32(b.width) - height := float32(b.height) + width := float32(b.width) + height := float32(b.height) - scale := nil != options.Scale - if scale { - width *= options.Scale.Horizontal - height *= options.Scale.Vertical - } - if options.Center { - left -= width * 0.5 - top -= height * 0.5 - } - - if scale { - if nil == options.Tint { - 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 { - 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) - } - } else { - if nil == options.Tint { - C.al_draw_bitmap(b.bitmap, C.float(left), C.float(top), 0) - } else { - C.al_draw_tinted_bitmap(b.bitmap, options.Tint.color, C.float(left), C.float(top), 0) - } - } + scale := nil != options.Scale + if scale { + width *= options.Scale.Horizontal + height *= options.Scale.Vertical + } + if options.Center { + left -= width * 0.5 + top -= height * 0.5 + } + + if scale { + if nil == options.Tint { + 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 { + 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) + } + } else { + if nil == options.Tint { + C.al_draw_bitmap(b.bitmap, C.float(left), C.float(top), 0) + } else { + C.al_draw_tinted_bitmap(b.bitmap, options.Tint.color, C.float(left), C.float(top), 0) + } + } } func (b *Bitmap) Width() int { - return b.width + return b.width } func (b *Bitmap) Height() int { - return b.height + return b.height } // Destroy destroys the bitmap func (b *Bitmap) Destroy() { - C.al_destroy_bitmap(b.bitmap) -} \ No newline at end of file + C.al_destroy_bitmap(b.bitmap) +} diff --git a/allegro5/c.go b/allegro5/c.go index 0d2e571..75aee97 100644 --- a/allegro5/c.go +++ b/allegro5/c.go @@ -1,9 +1,5 @@ package allegro5 -// #cgo linux pkg-config: allegro-5 allegro_font-5 allegro_image-5 allegro_primitives-5 allegro_ttf-5 -// #cgo windows LDFLAGS: -lallegro -// #cgo windows LDFLAGS: -lallegro_font -// #cgo windows LDFLAGS: -lallegro_image -// #cgo windows LDFLAGS: -lallegro_primitives -// #cgo windows LDFLAGS: -lallegro_ttf +// #cgo !windows pkg-config: allegro-5 allegro_font-5 allegro_image-5 allegro_primitives-5 allegro_ttf-5 +// #cgo windows LDFLAGS: -lallegro -lallegro_font -lallegro_image -lallegro_primitives -lallegro_ttf import "C"