Example #1
0
        // The core of the application
        private void step()
        {
            frames++;
            generation++;
            myMap map_alt = new myMap(rule,warp, map.getwidth, map.getheight);
            //build using unsafe block, compile with /unsafe
            //Maximum atainable framerate seems to be 64
            //##############################################
            BitmapData bmData = bitmap.LockBits(
                new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            int stride = bmData.Stride;
            System.IntPtr Scan0 = bmData.Scan0;
            unsafe
            {
                byte* p = (byte*)(void*)Scan0;
                int nOffset = stride - bitmap.Width * 3;
                for (int y = 0; y < bitmap.Height; ++y)
                {
                    for (int x = 0; x < bitmap.Width; ++x)
                    {
                        if (map.getN(x, y))
                        {
                            map_alt.setPixel(x, y, true);
                            p[0] = p[1] = p[2] = (byte)(0); // there must be a better way
                        }
                        else
                        {
                            map_alt.setPixel(x, y, false);
                            p[0] = p[1] = p[2] = (byte)(255);
                        }
                        p += 3;
                    }
                    p += nOffset;
                }
            }

            bitmap.UnlockBits(bmData);
            //###############################################
            //End unsafe
            // Safe block runs at 50 fps
            /*
            for(int x=0; x<map.getwidth; x++)
            {
                for(int y=0; y<map.getheight; y++)
                {
                    if(map.getN(x,y))
                    {
                        bitmap.SetPixel(x, y, alive);
                        map_alt.setPixel(x,y,true);
                    }
                    else
                    {
                        bitmap.SetPixel(x, y, dead);
                        map_alt.setPixel(x,y,false);
                    }
                }
            }
            */
            // End safe block
            map = map_alt;
            Invalidate();
        }