Hello, I'm working on a Perlin noise generator.
The following just draws a completely black window.
void drawNoise2d(int mapWidth, int mapHeight, float noise[], sf::RenderWindow& window)
{
sf::Image image{};
sf::Texture texture{};
sf::Sprite sprite{};
image.create(SCREEN_WIDTH, SCREEN_HEIGHT, sf::Color(0, 0, 0));
sf::Color color{};
for (int x{ 0 }; x < SCREEN_WIDTH; ++x)
{
for (int y{ 0 }; y < SCREEN_HEIGHT; ++y)
{
int pixel_w = std::floor(noise[y * SCREEN_HEIGHT + x] * 4.0f);
switch (pixel_w)
{
case 0:
color.r = 0;
color.g = 0;
color.b = 0;
break;
case 1:
color.r = 105;
color.g = 105;
color.b = 105;
break;
case 2:
color.r = 128;
color.g = 128;
color.b = 128;
break;
case 3:
color.r = 169;
color.g = 169;
color.b = 169;
break;
case 4:
color.r = 192;
color.g = 192;
color.b = 192;
break;
}
image.setPixel(x, y, color);
}
}
texture.update(image);
sprite.setTexture(texture);
window.draw(sprite);
}
The noise[] holds an array of values. For example:
std::cout << "Noise # " << y * mapWidth + x << ": " << noise[y * mapWidth + x] << '\n';
Noise # 0: 0.786747
Noise # 100: 0.770166
Noise # 200: 0.753585
Noise # 300: 0.737004
Noise # 400: 0.720423
Noise # 500: 0.703842
Noise # 600: 0.687262
Noise # 700: 0.67491
Noise # 800: 0.662559
Noise # 900: 0.650208
Noise # 1000: 0.637857
Noise # 1100: 0.625506
Noise # 1200: 0.613155
Noise # 1300: 0.605034
Noise # 1400: 0.596913
Noise # 1500: 0.588791
Noise # 1600: 0.58067
Noise # 1700: 0.572549
Noise # 1800: 0.564428
Noise # 1900: 0.556306
Noise # 2000: 0.548185
Noise # 2100: 0.540064
Noise # 2200: 0.531942
Noise # 2300: 0.523821
Noise # 2400: 0.5157
Noise # 2500: 0.507579
Noise # 2600: 0.503518
Noise # 2700: 0.499457
Noise # 2800: 0.495397
Noise # 2900: 0.491336
Noise # 3000: 0.487276
Noise # 3100: 0.483215
Noise # 3200: 0.479154
Noise # 3300: 0.475094
Noise # 3400: 0.471033
Noise # 3500: 0.466972
Noise # 3600: 0.462912
Noise # 3700: 0.458851
Noise # 3800: 0.454791
Noise # 3900: 0.45073
Noise # 4000: 0.446669
Noise # 4100: 0.442609
Noise # 4200: 0.438548
Noise # 4300: 0.434487
Noise # 4400: 0.430427
Noise # 4500: 0.426366
Noise # 4600: 0.422305
Noise # 4700: 0.418245
Noise # 4800: 0.414184
Noise # 4900: 0.410124
Noise # 5000: 0.406063
Noise # 5100: 0.410124
Noise # 5200: 0.414184
Noise # 5300: 0.418245
Noise # 5400: 0.422305
Noise # 5500: 0.426366
Noise # 5600: 0.430427
Noise # 5700: 0.434487
Noise # 5800: 0.438548
Noise # 5900: 0.442609
Noise # 6000: 0.446669
Noise # 6100: 0.45073
Noise # 6200: 0.454791
Noise # 6300: 0.458851
Noise # 6400: 0.462912
Noise # 6500: 0.466972
Noise # 6600: 0.471033
Noise # 6700: 0.475094
Noise # 6800: 0.479154
Noise # 6900: 0.483215
Noise # 7000: 0.487276
Noise # 7100: 0.491336
Noise # 7200: 0.495397
Noise # 7300: 0.499457
Noise # 7400: 0.503518
Noise # 7500: 0.507579
Noise # 7600: 0.5157
Noise # 7700: 0.523821
Noise # 7800: 0.531942
Noise # 7900: 0.540064
Noise # 8000: 0.548185
Noise # 8100: 0.556306
Noise # 8200: 0.564428
Noise # 8300: 0.572549
Noise # 8400: 0.58067
Noise # 8500: 0.588791
Noise # 8600: 0.596913
Noise # 8700: 0.605034
Noise # 8800: 0.613155
Noise # 8900: 0.621276
Noise # 9000: 0.629398
Noise # 9100: 0.637519
Noise # 9200: 0.64564
Noise # 9300: 0.653761
Noise # 9400: 0.661883
Noise # 9500: 0.670004
Noise # 9600: 0.678125
Noise # 9700: 0.686246
Noise # 9800: 0.694368
Noise # 9900: 0.702489
Noise # 1: 0.777325
Noise # 101: 0.761523
Noise # 201: 0.745722
Noise # 301: 0.72992
Noise # 401: 0.714118
Noise # 501: 0.698317
Noise # 601: 0.682515
Noise # 701: 0.670458
Noise # 801: 0.6584
Noise # 901: 0.646343
Noise # 1001: 0.634286
Noise # 1101: 0.622229
Noise # 1201: 0.610171
...CONTINUED...
The window screen is completely black but running. Where is it wrong or could it be a poor implementation elsewhere in the code. I don't think its the latter, because before this function, I was able to draw randomly picked sample of values of noise[] on a 1D plane, but translating that into 2D isn't working. So I think the error is due to this function.
I'm really new to SFML so I'd really appreciate some help.