aboutsummaryrefslogtreecommitdiff
path: root/unicorn/FakeUnicorn.go
blob: 8db71d59371b54b051846b784e4c8bf6d2496aa9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// +build linux,386 linux,amd64

package unicorn

import (
	"github.com/veandco/go-sdl2/sdl"
)

type FakeUnicorn struct {
	BaseUnicorn
	*BaseFakeUnicorn
}

// NewUnicorn ...
// Constructs a new fake unicorn out of paint and glue
func NewUnicorn() (*FakeUnicorn, error) {
	width := uint8(16)
	height := uint8(16)

	baseFake, err := NewBaseFakeUnicorn(300, 300)
	if err != nil {
		return nil, err
	}

	unicorn := &FakeUnicorn{
		BaseUnicorn{
			pixels: makePixels(width, height),
		},
		baseFake,
	}

	return unicorn, nil
}

func (f *FakeUnicorn) Show() {
	width, height := f.GetWidth(), f.GetHeight()
	for x := uint8(0); x < width; x++ {
		for y := uint8(0); y < height; y++ {
			r, g, b := Rgb(f.pixels[x][y])
			if err := f.renderer.SetDrawColor(r, g, b, uint8(255)); err != nil {
				panic(err)
			}
			cellWidth := f.displayWidth / int32(width)
			cellHeight := f.displayHeight / int32(height)
			if err := f.renderer.FillRect(&sdl.Rect{
				X: cellWidth * int32(x),
				Y: f.displayHeight - (cellHeight * int32(y)) - cellHeight, // SDL Y coordinate is from the top
				W: cellWidth,
				H: cellHeight,
			}); err != nil {
				panic(err)
			}
		}
	}
	f.renderer.Present()
}

func (f *FakeUnicorn) Off() {
	f.Close()
}