Skip to content

Commit

Permalink
[DMG] Properly implemented sprite priority
Browse files Browse the repository at this point in the history
  • Loading branch information
fattard committed Jan 20, 2018
1 parent 9b207a2 commit 95f56f6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
2 changes: 1 addition & 1 deletion GB_TestResults.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ Notes:

| Test | Result |
| --------------- | ------ |
| sprite priority | :x: |
| sprite priority | :+1: |

### Mooneye GB misc tests

Expand Down
15 changes: 12 additions & 3 deletions unity3D_proj/Assets/Scripts/Frontend/GB/LCDDisplay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}
}

Expand All @@ -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;
Expand Down Expand Up @@ -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];
}
Expand Down
40 changes: 35 additions & 5 deletions unity3D_proj/Assets/source/EmuCores/GB/HW/OAM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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();
}

Expand Down

0 comments on commit 95f56f6

Please sign in to comment.