diff --git a/GB_TestResults.md b/GB_TestResults.md index 466bade..ada5636 100644 --- a/GB_TestResults.md +++ b/GB_TestResults.md @@ -157,7 +157,7 @@ Notes: | Test | Result | | --------------- | ------ | -| sprite priority | :x: | +| sprite priority | :+1: | ### Mooneye GB misc tests diff --git a/unity3D_proj/Assets/Scripts/Frontend/GB/LCDDisplay.cs b/unity3D_proj/Assets/Scripts/Frontend/GB/LCDDisplay.cs index d2a7029..cdbae35 100644 --- a/unity3D_proj/Assets/Scripts/Frontend/GB/LCDDisplay.cs +++ b/unity3D_proj/Assets/Scripts/Frontend/GB/LCDDisplay.cs @@ -48,6 +48,8 @@ public class LCDDisplay : MonoBehaviour Color[] m_LCDColor = new Color[4]; Color m_disabledLCDColor; + int[] m_bgRowColors = new int[160]; + public void SetConfigs(EmuCores.GB.ConfigsGB aConfigs) { @@ -184,11 +186,13 @@ public void DrawDisplayLine(PPU aPPU, int aScanline) color |= ((palData >> lo) & 0x1); displayPixels[(i * texWid) + j] = m_LCDColor[color]; + m_bgRowColors[j] = color; } else { displayPixels[(i * texWid) + j] = m_LCDColor[0]; + m_bgRowColors[j] = 0; } } @@ -213,12 +217,17 @@ public void RenderSprites(PPU aPPU, int aScanline) int i = aScanline; int renderedObj = 0; - aOAM.SortByPosX(); + aOAM.SortByPosX(i, objHeight); - for (int objIdx = 0; objIdx < 40; ++objIdx) + for (int objIdx = 10; objIdx >= 0; --objIdx) { OAM.ObjAttributes obj = aOAM.GetObjAttributesSorted(objIdx); + if (obj == null) + { + continue; + } + int yPos = obj.PosY - 16; int xPos = obj.PosX - 8; int tileIdx = obj.TileIdx; @@ -264,7 +273,7 @@ public void RenderSprites(PPU aPPU, int aScanline) // Only appears above BG color0 if (obj.BGPriority == 1) { - if (displayPixels[(i * texWid) + pixel] == m_LCDColor[aPPU.BackgroundPalette & 0x03]) + if (m_bgRowColors[pixel] == 0) { displayPixels[(i * texWid) + pixel] = m_LCDColor[color]; } diff --git a/unity3D_proj/Assets/source/EmuCores/GB/HW/OAM.cs b/unity3D_proj/Assets/source/EmuCores/GB/HW/OAM.cs index a9a460c..4bda751 100644 --- a/unity3D_proj/Assets/source/EmuCores/GB/HW/OAM.cs +++ b/unity3D_proj/Assets/source/EmuCores/GB/HW/OAM.cs @@ -121,16 +121,16 @@ public int CompareTo(object aObj) int v = ((aObj as ObjAttributes).PosX - PosX); if (v > 0) { - return 1; + return -1; } else if (v == 0) { // Same pos, compares idx - return ((aObj as ObjAttributes).ID > ID) ? 1 : -1; + return ((aObj as ObjAttributes).ID > ID) ? -1 : 1; } - return -1; + return 1; } } @@ -160,12 +160,42 @@ public ObjAttributes GetObjAttributes(int aIdx) public ObjAttributes GetObjAttributesSorted(int aIdx) { - return m_objAttrsSorted[aIdx]; + if (aIdx < m_objAttrsSorted.Count) + { + return m_objAttrsSorted[aIdx]; + } + + return null; } - public void SortByPosX( ) + public void SortByPosX(int aScanline, int aObjHeight) { + int totalSelected = 0; + int idx = 0; + m_objAttrsSorted.Clear(); + + // Select the first 10 visible sprites + while (idx < 40) + { + ObjAttributes obj = GetObjAttributes(idx); + int yPos = obj.PosY - 16; + + if (aScanline >= yPos && aScanline < (yPos + aObjHeight)) + { + m_objAttrsSorted.Add(obj); + ++totalSelected; + + if (totalSelected == 10) + { + break; + } + } + + ++idx; + } + + // Sort by pos X m_objAttrsSorted.Sort(); }