Skip to content

Commit 631c145

Browse files
committed
Finally got sprites somewhat working :')
1 parent 745308e commit 631c145

File tree

3 files changed

+30
-37
lines changed

3 files changed

+30
-37
lines changed

core/graphics/pixelPipeline/pixelFetcher.ts

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export class PixelFetcher {
4343

4444
static reset(): void {
4545
PixelFetcher.currentStatus = 0;
46+
PixelFetcher.isSprite = false;
4647

4748
// Clear the pixel fifo memory
4849
for (let pixelIndex = 0; pixelIndex < 160; pixelIndex++) {
@@ -96,7 +97,7 @@ export class PixelFetcher {
9697
// Pixel Fetcher won't add more pixels unless there are only 8 pixels left
9798
let pixelsRemainingInFifo = PixelFifo.numberOfPixelsInFifo - PixelFifo.currentIndex;
9899
if (PixelFetcher.currentStatus === 0 || PixelFetcher.currentStatus === 4) {
99-
if (PixelFetcher.currentStatus === 4 && pixelsRemainingInFifo <= 8) {
100+
if (PixelFetcher.currentStatus === 4 && (PixelFetcher.isSprite || pixelsRemainingInFifo <= 8)) {
100101
// Place into the fifo
101102
_storeFetchIntoFifo();
102103

@@ -122,7 +123,7 @@ export class PixelFetcher {
122123
// Read the tile data
123124
_readTileData(1);
124125

125-
if (pixelsRemainingInFifo <= 8) {
126+
if (PixelFetcher.isSprite || pixelsRemainingInFifo <= 8) {
126127
// Place into the fifo
127128
_storeFetchIntoFifo();
128129

@@ -153,10 +154,8 @@ function _readTileIdFromTileMap(): void {
153154
// So just knock off the last bit? :)
154155
spriteTileId -= spriteTileId & 1;
155156

156-
// Check if we wanted to draw the second tile though
157-
if (PixelFetcher.tileLine >= 8) {
158-
spriteTileId += 1;
159-
}
157+
// NOTE: We don't need to do any tile line stuf here, as tall
158+
// sprites will automatically go into the next tile :)
160159
}
161160

162161
PixelFetcher.tileIdFromTileMap = spriteTileId;
@@ -258,6 +257,8 @@ function _readTileData(byteNumber: i32): void {
258257
}
259258
}
260259

260+
// Note: We should only store when we are storing a sprite
261+
// Or if we have <= 8 pixels left in the fifo.
261262
function _storeFetchIntoFifo(): void {
262263
if (PixelFetcher.isSprite) {
263264
// Need to mix the pixel on top of the old data
@@ -322,17 +323,6 @@ function _storeFetchIntoFifo(): void {
322323
// Set that we are a sprite
323324
fifoTypePerPixel = setBitOnByte(i, fifoTypePerPixel);
324325

325-
// Debug / TODO : Firgure our why sprites aren't shoing up...
326-
// fifoTypePerPixel is correct, maybe is is the data bytes?
327-
// log(0x87, fifoTypePerPixel);
328-
// log(0x89, spritePaletteColorId);
329-
// log(0x90, fifoTileDataByteZero);
330-
// log(0x91, fifoTileDataByteOne);
331-
332-
// Debug: Are we writing sprites?
333-
// Yes we are, and what seems like correct scanlines
334-
// log(0x99, Graphics.scanlineRegister);
335-
336326
// Write back to the fifo
337327
storePixelFifoByteForPixelIndexIntoWasmBoyMemory(0, PixelFifo.currentIndex, fifoTileDataByteZero);
338328
storePixelFifoByteForPixelIndexIntoWasmBoyMemory(1, PixelFifo.currentIndex, fifoTileDataByteOne);

core/graphics/pixelPipeline/pixelFifo.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ export class PixelFifo {
3131
}
3232

3333
static stopSpriteIdle(): void {
34-
PixelFifo.currentStatus = 0;
34+
if (PixelFifo.currentStatus === 2) {
35+
PixelFifo.currentStatus = 0;
36+
}
3537
}
3638

3739
static step(): void {

core/graphics/pixelPipeline/pixelPipeline.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ export class PixelPipeline {
2020
// When Cycles == 1, we will step the fetcher.
2121
static cycles: i32 = 0;
2222

23+
// The X index of the previous sprite that was fetched
24+
static previousSpriteIndex: i32 = -1;
25+
2326
// The last recorded scrollX value
2427
// Useful for checking background pixels
2528
static previousScrollX: i32 = 0;
@@ -39,10 +42,6 @@ export class PixelPipeline {
3942
cpuCycles = cpuCycles >> (<i32>Cpu.GBCDoubleSpeed);
4043

4144
for (let pixelPipelineCyclesStepped: i32 = 0; pixelPipelineCyclesStepped < cpuCycles; pixelPipelineCyclesStepped++) {
42-
// Debug:
43-
// The problem with sprites is that we are still pushing pixels, while we are trying to fetch sprites.
44-
// We need to do checks, and then proceed down a certain path
45-
4645
// Push our a pixel
4746
PixelFifo.step();
4847

@@ -64,11 +63,12 @@ export class PixelPipeline {
6463
}
6564
*/
6665

67-
// DEBUG: Just do sprites and BG
66+
// Figure out what we should be fetching next
67+
// First check if we should fetch a sprite
6868
if (_tryToFetchSprite()) {
6969
// We are fetching a sprite!
7070
PixelFifo.startSpriteIdle();
71-
} else {
71+
} else if (!isFetchingSprite()) {
7272
PixelFifo.stopSpriteIdle();
7373
_tryToFetchBackground();
7474
}
@@ -90,9 +90,15 @@ export class PixelPipeline {
9090
PixelFifo.reset();
9191

9292
PixelPipeline.cycles = 0;
93+
PixelPipeline.previousScrollX = 0;
94+
PixelPipeline.previousSpriteIndex = -1;
9395
}
9496
}
9597

98+
function isFetchingSprite(): boolean {
99+
return PixelFetcher.isSprite && PixelFetcher.currentStatus !== 0;
100+
}
101+
96102
// Returns if we started fetching / we are fetching sprites
97103
function _tryToFetchSprite(): boolean {
98104
// Check if sprites are enabled
@@ -107,6 +113,11 @@ function _tryToFetchSprite(): boolean {
107113
return false;
108114
}
109115

116+
// Can only show one sprite per x value
117+
if (PixelFifo.currentIndex <= PixelPipeline.previousSpriteIndex) {
118+
return false;
119+
}
120+
110121
for (let i = 0; i < Sprites.numberOfVisibleSprites; i++) {
111122
// Grab our sprite info from our visible sprites
112123
let spriteIndex: i32 = Sprites.getVisibleSpriteIndex(i);
@@ -138,25 +149,15 @@ function _tryToFetchSprite(): boolean {
138149
if (PixelFifo.currentIndex === spriteXPosition) {
139150
// We need to fetch this sprite!
140151

141-
// Debug: also need to fix tile line in the fetcher!!!
142-
// getting off train
143-
144152
// Get which line of the 8x8 or 8x16 tile.
145-
// Need to sub tract 16 from the total because of the tile height, to get the line
146-
// Debug, may need to put the sprite height here in stead of 16.
147-
let spriteTileLine = Graphics.scanlineRegister - spriteYPosition - 16;
153+
let spriteTileLine = Graphics.scanlineRegister - spriteYPosition;
148154

149155
// Check if we are already fetching the sprite,
150156
// If not, start fetching it
151157
if (!PixelFetcher.isFetchingSpriteTileLine(spriteTileLine, spriteIndex)) {
152-
// Debug: We are fetching this sprite
153-
// But which line and things?
154-
let spriteHeight: i32 = Lcd.tallSpriteSize ? 16 : 8;
155-
log(Graphics.scanlineRegister, spriteYPosition);
156-
log(0x01, spriteHeight);
157-
158-
// Just reset everything, and start fetching the sprite
158+
// Start fetching the sprite
159159
PixelFetcher.startSpriteFetch(spriteTileLine, spriteIndex);
160+
PixelPipeline.previousSpriteIndex = PixelFifo.currentIndex;
160161
}
161162

162163
return true;

0 commit comments

Comments
 (0)