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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
// +build linux,386 linux,amd64
package unicorn
import (
"github.com/veandco/go-sdl2/sdl"
)
type FakeUnicorn struct {
BaseUnicorn
displayWidth int32
displayHeight int32
window *sdl.Window
renderer *sdl.Renderer
}
// NewUnicorn ...
// Constructs a new fake unicorn out of paint and glue
func NewUnicorn() (*FakeUnicorn, error) {
width := uint8(16)
height := uint8(16)
if err := sdl.Init(sdl.INIT_EVERYTHING); err != nil {
return nil, err
}
unicorn := &FakeUnicorn{
BaseUnicorn{
pixels: makePixels(width, height),
},
300,
300,
nil,
nil,
}
if err := unicorn.createWindow(); err != nil {
unicorn.Close()
return nil, err
}
if err := unicorn.createRenderer(); err != nil {
unicorn.Close()
return nil, err
}
return unicorn, nil
}
func (f *FakeUnicorn) createWindow() error {
window, err := sdl.CreateWindow("Fake Unicorn",
sdl.WINDOWPOS_UNDEFINED,
sdl.WINDOWPOS_UNDEFINED,
f.displayWidth,
f.displayHeight,
sdl.WINDOW_SHOWN)
f.window = window
return err
}
func (f *FakeUnicorn) createRenderer() error {
renderer, err := sdl.CreateRenderer(f.window, -1, sdl.RENDERER_ACCELERATED)
f.renderer = renderer
return err
}
func (f *FakeUnicorn) Close() error {
if f.window != nil {
f.window.Destroy()
}
if f.renderer != nil {
f.renderer.Destroy()
}
return 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()
}
|