/*
         * lcdc.0
         *
         * when 0 => sprites are always displayed on top of the bg
         *
         * bg tile attribute.7
         *
         * when 0 => use oam priority bit
         * when 1 => bg priority
         *
         * sprite attribute.7
         *
         * when 0 => sprite above bg
         * when 1 => sprite above bg color 0
         */

        public void SetOverlay(int[] pixelLine, int offset, TileAttributes spriteAttr, int oamIndex)
        {
            for (var j = offset; j < pixelLine.Length; j++)
            {
                var p = pixelLine[j];
                var i = j - offset;
                if (p == 0)
                {
                    continue; // color 0 is always transparent
                }

                var oldPriority = _priorities.Get(i);

                var put = false;
                if ((oldPriority == -1 || oldPriority == 100) && !_lcdc.IsBgAndWindowDisplay())
                {
                    // this one takes precedence
                    put = true;
                }
                else if (oldPriority == 100)
                {
                    // bg with priority
                    put = _pixels.Get(i) == 0;
                }
                else if (oldPriority == -1 && !spriteAttr.IsPriority())
                {
                    // bg without priority
                    put = true;
                }
                else if (oldPriority == -1 && spriteAttr.IsPriority() && _pixels.Get(i) == 0)
                {
                    // bg without priority
                    put = true;
                }
                else if (oldPriority >= 0 && oldPriority < 10)
                {
                    // other sprite
                    put = oldPriority > oamIndex;
                }

                if (put)
                {
                    _pixels.Set(i, p);
                    _palettes.Set(i, spriteAttr.GetColorPaletteIndex());
                    _priorities.Set(i, oamIndex);
                }
            }
        }