Esempio n. 1
0
        public void DrawFrame(int rowBegin, int rowEnd)
        {
            if (!_state.Enabled)
            {
                return;
            }
            if (rowBegin > 143)
            {
                return;
            }

            #region BACKGROUND

            bool drawBackground = _state.LCDCBits[0];
            // We copy the information from the background tile to the effective screen
            for (int y = rowBegin; y < rowEnd; y++)
            {
                // We obtain the correct row
                int bY = (y + _state.SCY) % _disDef.FramePixelCountY;
                DisFuncs.GetRowPixels(ref _tempFrameLineBuffer,
                                      _pixelLookupTable,
                                      _disDef, _memory,
                                      _tempPixelBuffer,
                                      bY, _state.LCDCBits[3], _state.LCDCBits[4]);

                if (_updateDebugTargets[(int)DebugTargets.Background])
                {
                    // TODO(Cristian): Draw the whole debug target
                    DrawFuncs.DrawLine(_disDef, _debugInternalTargets[(int)DebugTargets.Background],
                                       _disDef.FramePixelCountX,
                                       _tempFrameLineBuffer,
                                       0, bY,
                                       0, _disDef.FramePixelCountX);
                }

                if (drawBackground)
                {
                    DrawFuncs.DrawLine(_disDef, _screenInternalBuffer,
                                       _disDef.ScreenPixelCountX,
                                       _tempFrameLineBuffer,
                                       0, y,
                                       _state.SCX, _disDef.FramePixelCountX,
                                       false, true);
                }
            }

            #endregion

            #region WINDOW

            int rWX = _state.WX - 7; // The window pos is (WX - 7, WY)

            // TODO(Cristian): If BG display is off, it actually prints white
            bool drawWindow = _state.LCDCBits[5];
            for (int row = rowBegin; row < rowEnd; row++)
            {
                if ((row >= _state.CurrentWY) && (row < 144))
                {
                    // The offset indexes represent that the window is drawn from it's beggining
                    // at (WX, WY)
                    DisFuncs.GetRowPixels(ref _tempFrameLineBuffer,
                                          _pixelLookupTable,
                                          _disDef, _memory, _tempPixelBuffer,
                                          row - _state.CurrentWY,
                                          _state.LCDCBits[6], _state.LCDCBits[4]);

                    // Independent target
                    if (_updateDebugTargets[(int)DebugTargets.Window])
                    {
                        DrawFuncs.DrawLine(_disDef, _debugInternalTargets[(int)DebugTargets.Window],
                                           _disDef.ScreenPixelCountX,
                                           _tempFrameLineBuffer,
                                           rWX, row,
                                           0, _disDef.ScreenPixelCountX - rWX);
                    }

                    // Screen target
                    if (drawWindow)
                    {
                        DrawFuncs.DrawLine(_disDef, _screenInternalBuffer,
                                           _disDef.ScreenPixelCountX,
                                           _tempFrameLineBuffer,
                                           rWX, row,
                                           0, _disDef.ScreenPixelCountX - rWX);
                    }
                }
            }

            #endregion

            #region SPRITES

            bool drawSprites = _state.LCDCBits[1];
            for (int row = rowBegin; row < rowEnd; row++)
            {
                if (_updateDebugTargets[(int)DebugTargets.SpriteLayer])
                {
                    // Independent target
                    uint[] pixels = new uint[_disDef.ScreenPixelCountX];
                    DisFuncs.GetSpriteRowPixels(_pixelLookupTable,
                                                _disDef, _memory, _spriteOAMs, pixels,
                                                _tempPixelBuffer,
                                                row, _state.LCDCBits[2],
                                                true);
                    DrawFuncs.DrawLine(_disDef, _debugInternalTargets[(int)DebugTargets.SpriteLayer],
                                       _disDef.ScreenPixelCountX,
                                       pixels,
                                       0, row,
                                       0, _disDef.ScreenPixelCountX);
                }

                // Screen Target
                if (drawSprites)
                {
                    DisFuncs.GetPixelRowFromBitmap(ref _tempFrameLineBuffer,
                                                   _disDef, _screenInternalBuffer,
                                                   row, _disDef.ScreenPixelCountX);
                    DisFuncs.GetSpriteRowPixels(_pixelLookupTable,
                                                _disDef, _memory, _spriteOAMs,
                                                _tempFrameLineBuffer, _tempPixelBuffer,
                                                row, _state.LCDCBits[2]);
                    DrawFuncs.DrawLine(_disDef, _screenInternalBuffer, _disDef.ScreenPixelCountX,
                                       _tempFrameLineBuffer,
                                       0, row,
                                       0, _disDef.ScreenPixelCountX);
                }
            }

            #endregion
        }