2015-08-15 01:50:14 +00:00
# pragma once
# include "stdafx.h"
# include "PPU.h"
# include "HdNesPack.h"
2015-08-31 01:04:21 +00:00
# include "VideoDecoder.h"
2015-08-15 01:50:14 +00:00
class HdPpu : public PPU
{
private :
2015-08-31 01:04:21 +00:00
HdPpuPixelInfo * _screenTileBuffers [ 2 ] ;
2015-08-15 01:50:14 +00:00
HdPpuPixelInfo * _screenTiles ;
protected :
void DrawPixel ( )
{
uint16_t bufferOffset = ( _scanline < < 8 ) + _cycle - 1 ;
2015-08-31 01:04:21 +00:00
uint16_t & pixel = _currentOutputBuffer [ bufferOffset ] ;
2015-08-15 01:50:14 +00:00
_lastSprite = nullptr ;
if ( IsRenderingEnabled ( ) | | ( ( _state . VideoRamAddr & 0x3F00 ) ! = 0x3F00 ) ) {
uint32_t paletteOffset ;
uint32_t color = GetPixelColor ( paletteOffset ) ;
if ( color = = 0 ) {
pixel = ReadPaletteRAM ( 0 ) | _intensifyColorBits ;
} else {
pixel = ReadPaletteRAM ( paletteOffset + color ) | _intensifyColorBits ;
}
HdPpuPixelInfo & tileInfo = _screenTiles [ bufferOffset ] ;
if ( _lastSprite ) {
tileInfo . Sprite . TileIndex = _memoryManager - > ToAbsoluteChrAddress ( _lastSprite - > TileAddr ) / 16 ;
tileInfo . Sprite . PaletteColors = ReadPaletteRAM ( _lastSprite - > PaletteOffset + 1 ) | ( ReadPaletteRAM ( _lastSprite - > PaletteOffset + 2 ) < < 8 ) | ( ReadPaletteRAM ( _lastSprite - > PaletteOffset + 3 ) < < 16 ) ;
tileInfo . Sprite . OffsetY = _lastSprite - > OffsetY ;
tileInfo . Sprite . OffsetX = _cycle - _lastSprite - > SpriteX - 1 ;
tileInfo . Sprite . HorizontalMirroring = _lastSprite - > HorizontalMirror ;
2015-08-15 14:40:27 +00:00
tileInfo . Sprite . VerticalMirroring = _lastSprite - > VerticalMirror ;
2015-08-15 01:50:14 +00:00
tileInfo . Sprite . BackgroundPriority = _lastSprite - > BackgroundPriority ;
uint32_t backgroundColor = 0 ;
if ( ( _cycle > 8 | | _flags . BackgroundMask ) & & _flags . BackgroundEnabled ) {
//BackgroundMask = false: Hide background in leftmost 8 pixels of screen
backgroundColor = ( ( ( _state . LowBitShift < < _state . XScroll ) & 0x8000 ) > > 15 ) | ( ( ( _state . HighBitShift < < _state . XScroll ) & 0x8000 ) > > 14 ) ;
}
paletteOffset = ( ( _state . XScroll + ( ( _cycle - 1 ) & 0x07 ) < 8 ) ? _previousTile . PaletteOffset : _currentTile . PaletteOffset ) ;
2015-08-15 15:52:10 +00:00
tileInfo . Sprite . BgColorIndex = backgroundColor ;
2015-08-15 01:50:14 +00:00
if ( backgroundColor = = 0 ) {
tileInfo . Sprite . BgColor = ReadPaletteRAM ( 0 ) ;
} else {
tileInfo . Sprite . BgColor = ReadPaletteRAM ( paletteOffset + backgroundColor ) ;
}
} else {
tileInfo . Sprite . TileIndex = HdPpuTileInfo : : NoTile ;
}
TileInfo * lastTile = & ( ( _state . XScroll + ( ( _cycle - 1 ) & 0x07 ) < 8 ) ? _previousTile : _currentTile ) ;
tileInfo . Tile . TileIndex = _memoryManager - > ToAbsoluteChrAddress ( lastTile - > TileAddr ) / 16 ;
tileInfo . Tile . PaletteColors = ReadPaletteRAM ( lastTile - > PaletteOffset + 1 ) | ( ReadPaletteRAM ( lastTile - > PaletteOffset + 2 ) < < 8 ) | ( ReadPaletteRAM ( lastTile - > PaletteOffset + 3 ) < < 16 ) ;
tileInfo . Tile . OffsetY = lastTile - > OffsetY ;
2015-08-15 15:52:10 +00:00
tileInfo . Tile . BackgroundPriority = false ;
2015-08-15 01:50:14 +00:00
tileInfo . Tile . OffsetX = ( _state . XScroll + ( ( _cycle - 1 ) & 0x07 ) ) & 0x07 ;
tileInfo . Tile . HorizontalMirroring = false ;
2015-08-15 14:40:27 +00:00
tileInfo . Tile . VerticalMirroring = false ;
2015-08-15 01:50:14 +00:00
tileInfo . Tile . BgColor = ReadPaletteRAM ( 0 ) ;
} else {
//"If the current VRAM address points in the range $3F00-$3FFF during forced blanking, the color indicated by this palette location will be shown on screen instead of the backdrop color."
pixel = ReadPaletteRAM ( _state . VideoRamAddr ) | _intensifyColorBits ;
_screenTiles [ bufferOffset ] . Tile . TileIndex = HdPpuTileInfo : : NoTile ;
_screenTiles [ bufferOffset ] . Sprite . TileIndex = HdPpuTileInfo : : NoTile ;
}
}
public :
HdPpu ( MemoryManager * memoryManager ) : PPU ( memoryManager )
{
2015-08-31 01:04:21 +00:00
_screenTileBuffers [ 0 ] = new HdPpuPixelInfo [ 256 * 240 ] ;
_screenTileBuffers [ 1 ] = new HdPpuPixelInfo [ 256 * 240 ] ;
_screenTiles = _screenTileBuffers [ 0 ] ;
2015-08-15 01:50:14 +00:00
}
~ HdPpu ( )
{
2015-08-31 01:04:21 +00:00
delete [ ] _screenTileBuffers [ 0 ] ;
delete [ ] _screenTileBuffers [ 1 ] ;
2015-08-15 01:50:14 +00:00
}
void SendFrame ( )
{
2016-01-13 00:42:28 +00:00
VideoDecoder : : GetInstance ( ) - > UpdateFrame ( _currentOutputBuffer , _screenTiles ) ;
_currentOutputBuffer = ( _currentOutputBuffer = = _outputBuffers [ 0 ] ) ? _outputBuffers [ 1 ] : _outputBuffers [ 0 ] ;
_screenTiles = ( _screenTiles = = _screenTileBuffers [ 0 ] ) ? _screenTileBuffers [ 1 ] : _screenTileBuffers [ 0 ] ;
2015-08-15 01:50:14 +00:00
}
} ;