Browse Source

Added image io and some bitmap rendering tests.

Beoran 6 years ago
parent
commit
cfc0f91b2c
4 changed files with 74 additions and 2 deletions
  1. 5 0
      al/al.go
  2. 40 2
      al/al_test.go
  3. 29 0
      al/bitmap_io.go
  4. BIN
      al/testdata/gin_feather.png

+ 5 - 0
al/al.go

@@ -851,3 +851,8 @@ func (self *Timer) GetEventSource() *EventSource {
 // Do nothing function for benchmarking only
 func DoNothing() {
 }
+
+// Returns Allehro's error number
+func Errno() int {
+    return int(C.al_get_errno())
+}

+ 40 - 2
al/al_test.go

@@ -3,6 +3,7 @@ package al
 import "testing"
 import "runtime"
 import "flag"
+import "path/filepath"
 import "math/rand"
 
 // some parameters
@@ -25,6 +26,18 @@ const CALLBACK_RESULT = 77
 const BMP_W = 11
 const BMP_H = 23
 
+
+// Helper that loads bitmaps from the testdata folder
+
+func loadBitmap(t *testing.T, name string) * Bitmap {
+    path := filepath.Join("testdata", name) // relative path
+    bmp := LoadBitmap(path)
+    if bmp == nil {
+        t.Fatalf("Could not load bitmap: %s: %d\n", path, Errno())
+    }
+    return bmp
+}
+
 // Test C callbacks, for example create bitmap  
 
 // Test system installation and deinstallation
@@ -252,7 +265,33 @@ func randomColor() Color {
     return CreateColor(rand.Float32(), rand.Float32(), rand.Float32(), 1.0)
 }
 
-// Test some primitive functions 
+// Test some bitmap functions 
+func TestBitmaps(t *testing.T) {
+    InstallSystem()
+    defer UninstallSystem()
+    InitImageAddon()
+    defer ShutdownImageAddon()
+    
+    display := makeDisplay()
+    if display == nil {
+        t.Error("Error creating display.")
+    }
+    
+    bmp := loadBitmap(t, "gin_feather.png")
+    blue := CreateColor(0.0, 0.0, 1.0, 1.0)
+    yellow := CreateColor(1.0, 1.0, 0.0, 1.0)
+    ClearToColor(blue)
+    bmp.DrawTinted(yellow, 20, 30, 0)
+    FlipDisplay()
+    tb := TargetBitmap()
+    if (!tb.Save(filepath.Join("testdata", "TestBitmaps.out.png"))) {
+        t.Errorf("Could not save output file.")
+    }
+    Rest(1.0)
+    display.Destroy()
+}
+
+// Test some primitive functions
 func TestPrimitives(t *testing.T) {
     InstallSystem()
     defer UninstallSystem()
@@ -277,7 +316,6 @@ func TestPrimitives(t *testing.T) {
 }
 
 
-
 // Benchmark basic display function ClearToColor
 func BenchmarkClearToColor(b *testing.B) {
     b.StopTimer()

+ 29 - 0
al/bitmap_io.go

@@ -0,0 +1,29 @@
+// albitmap
+package al
+
+/*
+#include <stdlib.h>
+#include <allegro5/allegro.h>
+#include "helpers.h"
+#include "callbacks.h"
+*/
+import "C"
+
+const KEEP_BITMAP_FORMAT        = C.ALLEGRO_KEEP_BITMAP_FORMAT 
+const NO_PREMULTIPLIED_ALPHA    = C.ALLEGRO_NO_PREMULTIPLIED_ALPHA
+const KEEP_INDEX                = C.ALLEGRO_KEEP_INDEX
+
+func LoadBitmap(name string) * Bitmap {
+    cname := cstr(name)
+    defer cstrFree(cname)
+    return wrapBitmap(C.al_load_bitmap(cname))
+}
+
+func (bmp * Bitmap) Save(name string) bool {
+    cname := cstr(name)
+    defer cstrFree(cname)
+    return bool(C.al_save_bitmap(cname, bmp.toC()))
+}
+
+
+

BIN
al/testdata/gin_feather.png