From f20896ef3b147ac07769eb36c67ba436c6d2ed22 Mon Sep 17 00:00:00 2001 From: Martin Ashby Date: Mon, 28 May 2018 16:29:18 +0100 Subject: New unicorns for displaing animated gifs! Tests pass on both old & new for fakes --- unicorn/FakeUnicorn2.go | 70 ++++++++++++++++++++++--------------------------- 1 file changed, 31 insertions(+), 39 deletions(-) (limited to 'unicorn/FakeUnicorn2.go') diff --git a/unicorn/FakeUnicorn2.go b/unicorn/FakeUnicorn2.go index 73ce8ab..27629d2 100644 --- a/unicorn/FakeUnicorn2.go +++ b/unicorn/FakeUnicorn2.go @@ -8,50 +8,42 @@ import ( type FakeUnicorn2 struct { BaseUnicorn2 + *BaseFakeUnicorn } -func renderImage(im image.Image) { - // b := im.Bounds() - // width := b.Dx() - // height := b.Dy() - // for x := 0; x < width; x++ { - // for y := 0; y < height; y++ { - // r, g, b, _ := im.At(x, y).RGBA() - // un.SetPixel(uint8(x), uint8(y), uint8(r), uint8(g), uint8(b)) - // } - // } - // un.Show() -} - -func render() { - // for !stop { - // for i := 0; i < len(gf.Image); i++ { - // im := gf.Image[i] - // delay := gf.Delay[i] //100ths of a second - // renderImage(un, im) - // time.Sleep(time.Duration(delay * 10000000)) // nanoseconds 10^-9 sec - // } - // } -} - -func (u *FakeUnicorn2) StartRender() { - -} - -func MainLoop() { - running := true - for running { - for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() { - switch event.(type) { - case *sdl.QuitEvent: - println("Quit") - running = false - break +func (u *FakeUnicorn2) renderImage(im image.Image) { + b := im.Bounds() + width, height := b.Dx(), b.Dy() + for x := 0; x < width; x++ { + for y := 0; y < height; y++ { + col := im.At(x, y) + r, g, b, _ := col.RGBA() + // Ignore alpha for now, not worked out how it should work on real unicorn + if err := u.renderer.SetDrawColor(uint8(r), uint8(g), uint8(b), uint8(255)); err != nil { + panic(err) + } + cellWidth := u.displayWidth / int32(width) + cellHeight := u.displayHeight / int32(height) + if err := u.renderer.FillRect(&sdl.Rect{ + X: cellWidth * int32(x), + Y: u.displayHeight - (cellHeight * int32(y)) - cellHeight, // SDL Y coordinate is from the top + W: cellWidth, + H: cellHeight, + }); err != nil { + panic(err) } } } + u.renderer.Present() } -func NewUnicorn2() *FakeUnicorn2 { - return &FakeUnicorn2{} +func NewUnicorn2() (*FakeUnicorn2, error) { + baseFake, err := NewBaseFakeUnicorn(300, 300) + if err != nil { + return nil, err + } + return &FakeUnicorn2{ + BaseUnicorn2{}, + baseFake, + }, nil } -- cgit v1.2.3-ZIG