package com.gradualgames.manager.render;

import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.gradualgames.ggvm.GGVm;
import com.gradualgames.ggvm.Ppu;
import com.gradualgames.manager.rastereffect.RasterEffectManager;

/**
 * Created by derek on 1/7/2017.
 *
 * This class draws the background using nametables at $2000 and $2400. See
 * RenderManager for a high level description of how backgrounds are drawn.
 */
public class VerticalMirroringRenderManager extends RenderManager {

    public VerticalMirroringRenderManager(GGVm ggvm, RasterEffectManager rasterEffectManager) {
        super(ggvm, rasterEffectManager);
    }

    public void drawNametable(GGVm ggvm, SpriteBatch spriteBatch) {
        int patternTableOffset = ggvm.getBackgroundPatternTableAddress() == 0 ? 0 : 1;

        int scrollX = ggvm.getScrollX();
        int scrollY = ggvm.getScrollY();
        int coarseScrollX = scrollX >> 3;
        int coarseScrollY = scrollY >> 3;
        int fineScrollX = scrollX & 7;
        int fineScrollY = scrollY & 7;

        int nameTableX = coarseScrollX;
        int nameTableY = coarseScrollY;
        int actualNameTableX;
        int actualNameTableY;
        int screenX = 0;
        int screenY = 8;
        int nameTableRowCount = fineScrollY == 0 ? 30 : 31;
        int nameTableColumnCount;
        int nameTable;

        while (nameTableRowCount > 0) {
            screenX = 0;
            nameTableColumnCount = fineScrollX == 0 ? 32 : 33;
            nameTableX = coarseScrollX;
            nameTable = (ggvm.getNametableAddress() == Ppu.NAME_TABLE_0_BASE_ADDRESS) ? 0 : 1;
            while (nameTableColumnCount > 0) {
                actualNameTableX = nameTableX % 32;
                actualNameTableY = nameTableY % 30;
                int whichNameTable = nameTable ^ ((nameTableX >> 5) & 1);

                int nameTableAddress = whichNameTable == 1 ? Ppu.NAME_TABLE_1_BASE_ADDRESS : Ppu.NAME_TABLE_0_BASE_ADDRESS;
                int attributeTableAddress = whichNameTable == 1 ? Ppu.ATTRIBUTE_TABLE_1_BASE_ADDRESS : Ppu.ATTRIBUTE_TABLE_0_BASE_ADDRESS;

                int index = ggvm.getNametableTile(nameTableAddress, actualNameTableX, actualNameTableY);
                int attribute = ggvm.getAttributeForNametableTile(attributeTableAddress, actualNameTableX, actualNameTableY);

                int indexRow = index >> 4;
                int indexColumn = index & 0x0f;
                Sprite sprite = patternTableSprites[patternTableOffset * 16 + indexRow][indexColumn];
                sprite.setColor(0, attributes[attribute], 0, 0);
                sprite.setPosition(screenX - fineScrollX, 240 - screenY + fineScrollY);
                sprite.draw(spriteBatch);
                screenX += 8;
                nameTableX++;
                nameTableColumnCount--;
            }
            screenY += 8;
            nameTableY++;
            nameTableRowCount--;
        }
    }
}

@@ -170,16 +170,69 @@
 .GGVm;%0D%0A
+import com.gradualgames.ggvm.GGVmRegisterStatusBar;%0D%0A
 import c
@@ -596,24 +596,136 @@
 anager %7B%0D%0A%0D%0A
+    private com.gradualgames.ggvm.GGVmRegisterStatusBar GGVmRegisterStatusBar = new GGVmRegisterStatusBar();%0D%0A%0D%0A
     public V
@@ -876,61 +876,970 @@
 lic 
-void drawNametable(GGVm ggvm, SpriteBatch spriteBatch
+VerticalMirroringRenderManager(GGVm ggvm, RasterEffectManager rasterEffectManager, boolean statusBarEnabled) %7B%0D%0A        this(ggvm, rasterEffectManager);%0D%0A        if (statusBarEnabled) ggvm.installReadWriteRange(GGVmRegisterStatusBar);%0D%0A    %7D%0D%0A%0D%0A    @Override%0D%0A    public void drawNametable(GGVm ggvm, SpriteBatch spriteBatch) %7B%0D%0A        if (GGVmRegisterStatusBar.isSprite0HitStatusBarEnabled()) %7B%0D%0A            drawNametable(ggvm, spriteBatch, Ppu.NAME_TABLE_0_BASE_ADDRESS, 0, 0, 0, ggvm.getSpriteY(0));%0D%0A            drawNametable(ggvm, spriteBatch, ggvm.getNametableAddress(), ggvm.getScrollX(), ggvm.getScrollY(), ggvm.getSpriteY(0), 240f);%0D%0A        %7D else %7B%0D%0A            drawNametable(ggvm, spriteBatch, ggvm.getNametableAddress(), ggvm.getScrollX(), ggvm.getScrollY(), 0f, 240f);%0D%0A        %7D%0D%0A    %7D%0D%0A%0D%0A    private void drawNametable(GGVm ggvm, SpriteBatch spriteBatch, int startingNametableAddress, int scrollX, int scrollY, float splitYStart, float splitYEnd
 ) %7B%0D
@@ -1933,92 +1933,8 @@
 %0D%0A%0D%0A
-        int scrollX = ggvm.getScrollX();%0D%0A        int scrollY = ggvm.getScrollY();%0D%0A
     
@@ -2612,24 +2612,24 @@
 able = (
-ggvm.get
+starting
 Nametabl
@@ -2632,26 +2632,24 @@
 tableAddress
-()
  == Ppu.NAME
@@ -3672,24 +3672,157 @@
 e%5D, 0, 0);%0D%0A
+                if (!GGVmRegisterStatusBar.isSprite0HitStatusBarEnabled() %7C%7C (screenY %3E= splitYStart && screenY %3C splitYEnd)) %7B%0D%0A    
             
@@ -3910,24 +3910,28 @@
             
+    
 sprite.draw(
@@ -3941,24 +3941,43 @@
 iteBatch);%0D%0A
+                %7D%0D%0A
             

Add GGVm's Sprite 0 Hit based status bar register to the VerticalMirroringRenderManager.