All pastes #151178 Raw Edit

Unnamed

public text v1 · immutable
#151178 ·published 2006-08-27 19:52 UTC
rendered paste body
            public static bool DrawSprite(byte X, byte Y, byte N, byte[] Sprite)
            {

                bool bVF = false;

                //
                // Because this is CHIP-8, all sprites are _always_ 8xn.
                // So, apart from the array and the coords we can do most of the op ourselves!
                // ^_^
                //

                // Technically we could work out 'byte N' ourselves, but i cba. :/
                //  The malloc() for one variable would be less cpu power than the mathematical computations
                //   required to work it out. ('mathematical computations' -> read: 'diviszhun 'n shit')
                //

                // Where are we?! WAIL
                    int b = Sprite.GetLowerBound(0);

                // Main loop...
                    for (int iY = Y; iY - Y < N; iY++)
                    {

                        if (iY >= 32)
                            break;

                        // .. Yeah.  If this was a 'proper' emulator, I'd unroll this loop.
                        // No need now.  This way's a tinsy bit prettier, too...
                        for (int iX = X; iX - X < 8 - 1; iX++)
                        {

                            if (iX < 63)
                            {

                                // Do we need to set this pixel?
                                if (GetBit(Sprite[b], (byte)(8 - iX - X)) > 1)
                                {

                                    Pixels[iX, iY] = !Pixels[iX, iY];

                                    if (!Pixels[iX, iY])
                                    {
                                        // Probably quicker to set the var each time,
                                        // than to check if it's already set.
                                        //
                                        // If this was a production emu then at this point I
                                        // would break the loop and transfer control to a
                                        // version of the loop that does _not_ check.
                                        //

                                        // Set the var.
                                        bVF = true;
                                    }

                                }

                            }

                        }

                        // Advance byte.
                            b++;

                    }

                // Return VF val.
                    return bVF;

            }