Ok, I'm making a terrain generator. It is very slow due to how I blit. How could I possibly make it faster?
public static void ren ()
{
PerlinNoise perlinNoise = new PerlinNoise (-1);
double widthDivisor = 1 / (double)width;
double heightDivisor = 1 / (double)height;
for (uint i=0; i<width; i++) {
for (uint ii=0; ii<height; ii++) {
uint posx = i;
uint posy = ii;
// Note that the result from the noise function is in the range -1 to 1, but I want it in the range of 0 to 1
// that's the reason of the strange code
double v =
// First octave
(perlinNoise.Noise (2 * i * widthDivisor, 2 * ii * heightDivisor, -0.5) + 1) / 2 * 0.7 +
// Second octave
(perlinNoise.Noise (4 * i * widthDivisor, 4 * ii * heightDivisor, 0) + 1) / 2 * 0.2 +
// Third octave
(perlinNoise.Noise (8 * i * widthDivisor, 8 * ii * heightDivisor, +0.5) + 1) / 2 * 0.1;
v = Math.Min (1, Math.Max (0, v));
byte b = (byte)(v * 255);
byte r = b;
byte bl = b;
byte g = b;
Color col;
Color col2;
if (b < 110) {
while (posx>water.Width-1) {
posx -= water.Width - 1;
}
while (posy>water.Height-1) {
posy -= water.Height - 1;
}
col = water.GetPixel (posx, posy);
col2 = new Color (0, 0, 0);
} else if (b < 112) {
while (posx>desert.Width-1) {
posx -= desert.Width - 1;
}
while (posy>desert.Height-1) {
posy -= desert.Height - 1;
}
col = desert.GetPixel (posx, posy);
col2 = new Color (1, 0, 0);
} else if (b < 140) {
while (posx>grass.Width-1) {
posx -= grass.Width - 1;
}
while (posy>grass.Height-1) {
posy -= grass.Height - 1;
}
col = grass.GetPixel (posx, posy);
col2 = new Color (2, 0, 0);
} else {
while (posx>stone.Width-1) {
posx -= stone.Width - 1;
}
while (posy>stone.Height-1) {
posy -= stone.Height - 1;
}
col = stone.GetPixel (posx, posy);
col2 = new Color (3, 0, 0);
}
bitmap.SetPixel (i, ii, col);
data.SetPixel (i, ii, col2);
}
;
}
background.Image = bitmap;
perlinNoise = new PerlinNoise (-1);
Random gen = new Random ();
for (uint i=0; i<width; i++) {
for (uint ii=0; ii<height; ii++) {
uint posx = i;
uint posy = ii;
// Note that the result from the noise function is in the range -1 to 1, but I want it in the range of 0 to 1
// that's the reason of the strange code
double v =
// First octave
(perlinNoise.Noise (2 * i * widthDivisor, 2 * ii * heightDivisor, -0.5) + 1) / 2 * 0.7 +
// Second octave
(perlinNoise.Noise (4 * i * widthDivisor, 4 * ii * heightDivisor, 0) + 1) / 2 * 0.2 +
// Third octave
(perlinNoise.Noise (8 * i * widthDivisor, 8 * ii * heightDivisor, +0.5) + 1) / 2 * 0.1;
v = Math.Min (1, Math.Max (0, v));
byte b = (byte)(v * 255);
if (b > 130) {
if (data.GetPixel (i, ii).R != 0 && data.GetPixel (i, ii).R != 3 &&
data.GetPixel (i, ii).R != 1) {
if (gen.NextDouble () > 0) {
for (uint i2=0; i2<tree.Width; i2++) {
for (uint ii2=0; ii2<tree.Height; ii2++){
bitmap.SetPixel(i, ii, tree.GetPixel(i2, ii2));
}
}
}
}