-
Notifications
You must be signed in to change notification settings - Fork 2
/
render.go
executable file
·220 lines (188 loc) · 4.82 KB
/
render.go
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package ppu
import (
"image/color"
)
var (
// Tile bits are counted left-to-right so bit 0 of the tile is bit 7 of the byte
patterns = []uint8{
0b10000000,
0b01000000,
0b00100000,
0b00010000,
0b00001000,
0b00000100,
0b00000010,
0b00000001,
}
grey = []color.RGBA{
{0xff, 0xff, 0xff, 0xff},
{0xaa, 0xaa, 0xaa, 0xff},
{0x77, 0x77, 0x77, 0xff},
{0x33, 0x33, 0x33, 0xff},
}
// red = []color.RGBA{
// {0xff, 0xaa, 0xaa, 0xff},
// {0xdd, 0x77, 0x77, 0xff},
// {0xaa, 0x33, 0x33, 0xff},
// {0x55, 0x00, 0x00, 0xff},
// }
green = []color.RGBA{
{0xaa, 0xff, 0xaa, 0xff},
{0x77, 0xdd, 0x77, 0xff},
{0x33, 0xaa, 0x33, 0xff},
{0x00, 0x55, 0x00, 0xff},
}
blue = []color.RGBA{
{0xaa, 0xaa, 0xff, 0xff},
{0x77, 0x77, 0xdd, 0xff},
{0x33, 0x33, 0xaa, 0xff},
{0x00, 0x00, 0x55, 0xff},
}
)
func (ppu *PPU) renderPixel(x, y uint8) {
colours := grey
var spritePixel uint8
var spriteBehindBackground bool
var useSpritePalette1 bool
// Does this pixel intersect with a sprite?
if ppu.spritesEnabled {
for sprite, overlaps := range ppu.spriteOverlaps {
if !overlaps {
continue
}
spriteAddr := 0xfe00 + uint16(sprite*4)
spriteX := ppu.oam.PPURead(spriteAddr + 1)
if x+8 >= spriteX && x < spriteX {
spriteY := ppu.oam.PPURead(spriteAddr)
tileNumber := ppu.oam.PPURead(spriteAddr + 2)
attributes := ppu.oam.PPURead(spriteAddr + 3)
tileOffsetX := (x - spriteX) % 8
tileOffsetY := (y - spriteY) % 8
spriteBehindBackground = attributes&0x80 > 0
flipY := attributes&0x40 > 0
flipX := attributes&0x20 > 0
useSpritePalette1 = attributes&0x08 > 0
if flipX {
tileOffsetX = 7 - tileOffsetX
}
if flipY {
tileOffsetY = 7 - tileOffsetY
}
spritePixel = ppu.readTilePixel(int(tileNumber), tileOffsetX, tileOffsetY)
if spritePixel > 0 {
break
}
}
}
// Did we intersect with a foreground sprite?
if spritePixel > 0 && !spriteBehindBackground {
if ppu.debug {
colours = blue
}
if useSpritePalette1 {
ppu.frame.SetRGBA(int(x), int(y), colours[ppu.obp1Colour[spritePixel]])
} else {
ppu.frame.SetRGBA(int(x), int(y), colours[ppu.obp0Colour[spritePixel]])
}
return
}
}
var pixel uint8
// Does this pixel intersect with the window?
var intersectedWindow bool
if ppu.windowEnabled {
wx := ppu.ReadWX()
wy := ppu.ReadWY()
if wx <= 166 && wy <= 143 && x >= (wx-7) && y >= wy {
pixel = ppu.findWindowPixel(x-(wx-7), y-wy)
intersectedWindow = true
}
}
// If we didn't find a window pixel then find where this pixel intersects the background
if !intersectedWindow && ppu.bgEnabled {
pixel = ppu.findBackgroundPixel(x, y)
}
// Does this pixel intersect with a background sprite?
if pixel == 0 && spritePixel != 0 && spriteBehindBackground {
if ppu.debug {
colours = blue
}
if useSpritePalette1 {
ppu.frame.SetRGBA(int(x), int(y), colours[ppu.obp1Colour[spritePixel]])
} else {
ppu.frame.SetRGBA(int(x), int(y), colours[ppu.obp0Colour[spritePixel]])
}
} else {
if intersectedWindow {
if ppu.debug {
colours = green
}
ppu.frame.SetRGBA(int(x), int(y), colours[ppu.bgpColour[pixel]])
} else {
ppu.frame.SetRGBA(int(x), int(y), grey[ppu.bgpColour[pixel]])
}
}
}
func (ppu *PPU) findWindowPixel(x, y uint8) uint8 {
tileX := x / 8
tileY := y / 8
tileOffsetX := x % 8
tileOffsetY := y % 8
var offsetAddr uint16
if ppu.highWindowTileMap {
offsetAddr = 0x9c00 - 0x8000
} else {
offsetAddr = 0x9800 - 0x8000
}
var tileNumber int
tileAddr := 32*uint16(tileY) + uint16(tileX)
tileByte := ppu.videoRAM[offsetAddr+tileAddr]
if ppu.lowTileData {
tileNumber = int(tileByte)
} else {
tileNumber = 256 + int(int8(tileByte))
}
return ppu.readTilePixel(tileNumber, tileOffsetX, tileOffsetY)
}
func (ppu *PPU) findBackgroundPixel(x, y uint8) uint8 {
scx := ppu.ReadSCX()
scy := ppu.ReadSCY()
tileX := (x + scx) / 8
tileY := (y + scy) / 8
tileOffsetX := (x + scx) % 8
tileOffsetY := (y + scy) % 8
var offsetAddr uint16
if ppu.highBgTileMap {
offsetAddr = 0x9c00 - 0x8000
} else {
offsetAddr = 0x9800 - 0x8000
}
var tileNumber int
tileAddr := 32*uint16(tileY) + uint16(tileX)
tileByte := ppu.videoRAM[offsetAddr+tileAddr]
if ppu.lowTileData {
tileNumber = int(tileByte)
} else {
tileNumber = 256 + int(int8(tileByte))
}
return ppu.readTilePixel(tileNumber, tileOffsetX, tileOffsetY)
}
func (ppu *PPU) readTilePixel(tileNumber int, tileOffsetX, tileOffsetY uint8) uint8 {
startAddr := tileNumber * 16
a := ppu.videoRAM[startAddr+int(tileOffsetY*2)]
b := ppu.videoRAM[startAddr+int(tileOffsetY*2)+1]
aset := a&patterns[tileOffsetX] > 0
bset := b&patterns[tileOffsetX] > 0
switch {
case !aset && !bset:
return 0
case !aset && bset:
return 1
case aset && !bset:
return 2
case aset && bset:
return 3
default:
panic("error reading tile")
}
}