A 2D tile-based sandbox game.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

perlin-test.cc 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include <swan/log.h>
  2. #include <PerlinNoise/PerlinNoise.hpp>
  3. #include <png++/png.hpp>
  4. static int grassLevel(const siv::PerlinNoise &perlin, int x) {
  5. return (int)(perlin.noise(x / 50.0, 0) * 13);
  6. }
  7. static int stoneLevel(const siv::PerlinNoise &perlin, int x) {
  8. return (int)(perlin.noise(x / 50.0, 10) * 10) + 10;
  9. }
  10. int main() {
  11. siv::PerlinNoise perlin = siv::PerlinNoise(100);
  12. int x1 = -1600;
  13. int x2 = 1600;
  14. int y1 = -800;
  15. int y2 = 800;
  16. png::image<png::gray_pixel> image(x2 - x1 + 1, y2 - y1 + 1);
  17. Swan::info << "perlin-test.png: " << (x2 - x1 + 1) << "x" << (y2 - y1 + 1);
  18. for (int x = x1; x <= x2; ++x) {
  19. int px = x - x1;
  20. int grass_level = grassLevel(perlin, x);
  21. int stone_level = stoneLevel(perlin, x);
  22. for (int y = y1; y <= y2; ++y) {
  23. int py = y - y1;
  24. if (y > grass_level + 10) {
  25. double l = perlin.noise(x / 41.37, y / 16.37);
  26. if (l > 0.2)
  27. image[py][px] = 255;
  28. else
  29. image[py][px] = 0;
  30. } else if (y >= grass_level) {
  31. image[py][px] = 0;
  32. } else {
  33. image[py][px] = 255;
  34. }
  35. if (y == grass_level) {
  36. image[py][px] = 128;
  37. }
  38. }
  39. }
  40. image.write("perlin-test.png");
  41. }