-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphics.go
More file actions
108 lines (94 loc) · 2.09 KB
/
Copy pathgraphics.go
File metadata and controls
108 lines (94 loc) · 2.09 KB
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
99
100
101
102
103
104
105
106
107
108
package main
import (
"github.com/veandco/go-sdl2/sdl"
)
const (
colorEmpty = 0x000000
colorOccupied = 0xFFFFFF
colorBorder = 0xD3D3D3
margin = 1
)
// Graphics should be used to conduct screen draws
type Graphics interface {
Render()
BindBuffer([]byte)
}
type graphics struct {
window *sdl.Window
buffer []byte
}
// NewGraphics takes an SDL window and a reference to vidmem
// and renders the screen buffer onto the window through the Render() interface
func NewGraphics(window *sdl.Window) Graphics {
return &graphics{
window: window,
}
}
// Render draws screen buffer onto SDL window
func (g *graphics) Render() {
for i := 0; i < displayRows; i++ {
for j := 0; j < displayColumns; j++ {
ind := displayColumns*i + j
x := int32((ind % displayColumns) * cellSize)
y := int32(i * cellSize)
empty := g.buffer[ind] == 0x0
renderTile(g.window, x, y, empty, Tiled)
}
}
g.window.UpdateSurface()
}
// BindBuffer takes a slice to bind as vidmem to this Graphics renderer
func (g *graphics) BindBuffer(buf []byte) {
g.buffer = buf
}
// initSDLWindow generates window at correct aspect ratio
func initSDLWindow() (*sdl.Window, error) {
if err := sdl.Init(sdl.INIT_EVERYTHING); err != nil {
return nil, err
}
window, err := sdl.CreateWindow(
"chipm8",
sdl.WINDOWPOS_UNDEFINED,
sdl.WINDOWPOS_UNDEFINED,
displayColumns*cellSize,
displayRows*cellSize,
sdl.WINDOW_SHOWN)
if err != nil {
return nil, err
}
return window, nil
}
func renderTile(window *sdl.Window, x, y int32, empty, tiled bool) {
surface, _ := window.GetSurface()
var fillColor uint32
if empty {
fillColor = colorEmpty
} else {
fillColor = colorOccupied
}
if Tiled {
surface.FillRect(
&sdl.Rect{
X: x + margin,
Y: y + margin,
W: cellSize - margin,
H: cellSize - margin},
colorEmpty)
surface.FillRect(
&sdl.Rect{
X: x + margin,
Y: y + margin,
W: cellSize - margin,
H: cellSize - margin},
fillColor)
} else {
// not tiled, use standard rect
surface.FillRect(
&sdl.Rect{
X: x,
Y: y,
W: cellSize,
H: cellSize},
fillColor)
}
}