So, before you even consider just copy/paste the following code - you should know this:
this code is beyond flawed, and is slow as hell!
And with that in mind, I'm here asking for this board's help in achieving a better way to make this tunnel animated...
I have tried using both jagged arrays, and multidimension arrays (which you can clearly see) - but it still gives this "lag" feature no matter what I change :/
I suspect very much that it is due to the pixel manipulation, which seem to be too slow

Oh yes, I have also tried using Parallel.For - but that didn't even work at all (it just crashes due to memory being inaccesible!)...
And yes, I have also considered using every OpenGL and DirectX solution instead (and tried a few, but still slow as shit!)...
So here it is, no more nagging about me failing - hopefully someone in here can aid me in making this possible

Oh by the way, the code is derived from "Lode's Computer Graphics Tutorial" - found here:
http://lodev.org/cgtutor/tunnel.html//Using FastBitmap.cs by boogop which is found on PSC ;)
//http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=5653&lngWId=10
public partial class Form1 : Form
{
BackgroundWorker bw;
Bitmap bmp;
int fHeight;
int fWidth;
Graphics gForm;
Graphics gBmp;
/*Multi-dimension array:*/
int[,] distanceTable;
/*Jagged array:*/
int[][] distanceTable2;
/*Multi-dimension array:*/
int[,] angleTable;
/*Jagged array:*/
int[][] angleTable2;
Bitmap myTexture;
int animation = 0;
int rotation = 0;
public Form1()
{
InitializeComponent();
InitPictureBox();
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer | ControlStyles.Opaque | ControlStyles.ResizeRedraw, true);
}
void InitPictureBox()
{
bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
gForm = pictureBox1.CreateGraphics();
gBmp = Graphics.FromImage(bmp);
fWidth = bmp.Width;
fHeight = bmp.Height;
}
void DoXorTunnel()
{
while (true)
{
animation += 3;
rotation += 1;
Application.DoEvents();
var myBitmap = new Bitmap(bmp.Width, bmp.Height);
var shiftX = myTexture.Width + animation;
var shiftY = myTexture.Height + rotation;
var a = new FastBitmap(myTexture);
var b = new FastBitmap(myBitmap);
for (int y = 0; y < bmp.Height; y++)
{
Application.DoEvents();
for (int x = 0; x < bmp.Width; x++)
{
/*Multi-dimension array:*/
//b.SetPixel(x, y, (a.GetPixel((distanceTable[x, y] + shiftX) % myTexture.Width, (angleTable[x, y] + shiftY) % myTexture.Height)));
/*Jagged array:*/
b.SetPixel(x, y, (a.GetPixel((distanceTable2[x][y] + shiftX) % myTexture.Width, (angleTable2[x][y] + shiftY) % myTexture.Height)));
}
}
a.Release();
b.Release();
gForm.DrawImageUnscaled(myBitmap, 0, 0, fWidth, fHeight);
}
}
private void Form1_Load(object sender, EventArgs e)
{
myTexture = XorTexture(128, 128);
CreateTables();
bw = new BackgroundWorker();
bw.DoWork += Bw_DoWork;
}
private void Bw_DoWork(object sender, DoWorkEventArgs e)
{
InitPictureBox();
DoXorTunnel();
}
private void button1_Click(object sender, EventArgs e)
{
bw.RunWorkerAsync();
}
Bitmap XorTexture(int texHeight, int texWidth)
{
var myB = new Bitmap(texWidth, texHeight);
var b = new FastBitmap(myB);
for (var y = 0; y < texHeight; y++)
{
for (var x = 0; x < texWidth; x++)
{
var c = (x ^ y) % 256;
b.SetPixel(x, y, Color.FromArgb(c, c, c));
}
}
b.Release();
return myB;
}
void CreateTables()
{
distanceTable = new int[bmp.Width, bmp.Height];
distanceTable2 = new int[bmp.Width][];
for (var i = 0; i < bmp.Width; i++)
distanceTable2[i] = new int[bmp.Height];
angleTable = new int[bmp.Width, bmp.Height];
angleTable2 = new int[bmp.Width][];
for (var i = 0; i < bmp.Width; i++)
angleTable2[i] = new int[bmp.Height];
for (var y = 0; y < bmp.Height; y++)
{
for (var x = 0; x < bmp.Width; x++)
{
int angle, distance;
float ratio = 32.0f;
distance = (int)(ratio * myTexture.Height / Math.Sqrt((x - bmp.Width / 2.0) * (x - bmp.Width / 2.0) + (y - bmp.Height / 2.0) * (y - bmp.Height / 2.0))) % myTexture.Height;
angle = (int)(0.5 * myTexture.Width * Math.Atan2(y - bmp.Height / 2.0, x - bmp.Width / 2.0) / Math.PI);
distanceTable[x, y] = distance;
angleTable[x, y] = angle;
distanceTable2[x][y] = distance;
angleTable2[x][y] = angle;
}
}
}
}
Any suggestions to make this pixel manipulation work faster?