Advertisement
itsspynex

Untitled

Sep 21st, 2023
911
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.43 KB | None | 0 0
  1. #include <SFML/Audio.hpp>
  2. #include <SFML/Graphics.hpp>
  3. #include <SFML/Network.hpp>
  4. #include <SFML/Main.hpp>
  5. #include <SFML/System.hpp>
  6. #include <SFML/Window.hpp>
  7. #include <SFML/OpenGL.hpp>
  8. #include <iostream>
  9. #include <cstdlib>
  10. #include "lib/FastNoiseLite.h"
  11. #include <random>
  12. #include <memory>
  13.  
  14. using namespace std;
  15.  
  16. const int screen_width = 128;
  17. const int screen_height = 128;
  18. const int pixel_channel = 4;
  19. const int chunkSize = 128;
  20.  
  21.  
  22.  
  23. FastNoiseLite noiseparams(float oct, FastNoiseLite::NoiseType type, int seed)
  24. {
  25.    FastNoiseLite noise;
  26.    noise.SetSeed(seed);
  27.    noise.SetNoiseType(type);
  28.    noise.SetFractalLacunarity(3.0f);
  29.    noise.SetFractalGain(0.4f);
  30.    noise.SetFrequency(0.01f);
  31.    noise.SetFractalOctaves(oct);
  32.    noise.SetFractalType(noise.FractalType_FBm);
  33.    return noise;
  34. };
  35.  
  36.  
  37. struct Chunk
  38. {
  39.     std::shared_ptr<sf::Texture> texture;
  40.     sf::Sprite sprite;
  41.     sf::Vector2f position;  //This will store the cordinate of the chunk
  42.     std::shared_ptr<sf::Uint8[]> pixels;
  43.  
  44.     Chunk()
  45.     {
  46.         texture = std::make_unique<sf::Texture>();
  47.         texture->create(screen_width, screen_height);
  48.         pixels = std::make_unique<sf::Uint8[]>(screen_width * screen_height * pixel_channel);
  49.     }
  50. };
  51.  
  52.  
  53.  
  54. std::vector<Chunk> regen()
  55. {
  56.     std::vector<Chunk> chunks;
  57.  
  58.     //Seed creation
  59.     random_device rd;   // non-deterministic generator
  60.     mt19937 gen(rd());
  61.     int seed1 = gen();
  62.     srand48(seed1);
  63.     int seed2 = rand();
  64.     srand48(seed2);
  65.     int seed3 = rand();
  66.  
  67.     //Noise maps
  68.     FastNoiseLite moisture = noiseparams(3.99, FastNoiseLite::NoiseType::NoiseType_Perlin, seed1);
  69.  
  70.  
  71.     for(float chunk_y = 0; chunk_y < 16; chunk_y++)
  72.     {
  73.         for(float chunk_x = 0; chunk_x < 8; chunk_x++)
  74.         {
  75.  
  76.             Chunk& chunk = chunks.emplace_back();
  77.             for(int y = 0; y < screen_height; ++y)
  78.             {
  79.                 for(int x = 0; x < screen_width; ++x)
  80.                 {
  81.                     double moist = moisture.GetNoise(float((chunkSize*chunk_y)+x), float((chunkSize*chunk_x)+y));
  82.                     moist = (moist + 1.0) / 2.0;
  83.                     moist = int(moist*255);
  84.  
  85.                     //Convert 2d representation of each index to 1d
  86.                     int CurrentPixelIndex2 = ((y * screen_height) + x) * 4;
  87.  
  88.                     //Deep Water
  89.                     if(moist < 100)
  90.                     {
  91.                         chunk.pixels[CurrentPixelIndex2] = {0};
  92.                         chunk.pixels[CurrentPixelIndex2 +1] = {66};
  93.                         chunk.pixels[CurrentPixelIndex2 +2] = {137};
  94.                         chunk.pixels[CurrentPixelIndex2 +3] = {255};
  95.                     }
  96.                     //Deep water
  97.                     else if (moist < 120)
  98.                     {
  99.                         chunk.pixels[CurrentPixelIndex2] = {55};
  100.                         chunk.pixels[CurrentPixelIndex2 +1] = {102};
  101.                         chunk.pixels[CurrentPixelIndex2 +2] = {200};
  102.                         chunk.pixels[CurrentPixelIndex2 +3] = {255};
  103.                     }
  104.                     //Beaches
  105.                     else if (moist < 122)
  106.                     {
  107.                         chunk.pixels[CurrentPixelIndex2] = {209};
  108.                         chunk.pixels[CurrentPixelIndex2 +1] = {189};
  109.                         chunk.pixels[CurrentPixelIndex2 +2] = {111};
  110.                         chunk.pixels[CurrentPixelIndex2 +3] = {255};
  111.                     }
  112.                     //Plain
  113.                     else if(moist < 160)
  114.                     {
  115.                         chunk.pixels[CurrentPixelIndex2] = {71};
  116.                         chunk.pixels[CurrentPixelIndex2 +1] = {133};
  117.                         chunk.pixels[CurrentPixelIndex2 +2] = {47};
  118.                         chunk.pixels[CurrentPixelIndex2 +3] = {255};
  119.                     }
  120.                     //Jungle
  121.                     else if(moist < 180)
  122.                     {
  123.                         chunk.pixels[CurrentPixelIndex2] = {28};
  124.                         chunk.pixels[CurrentPixelIndex2 +1] = {101};
  125.                         chunk.pixels[CurrentPixelIndex2 +2] = {1};
  126.                         chunk.pixels[CurrentPixelIndex2 +3] = {255};
  127.                     }
  128.                     else if (moist < 200)
  129.                     {
  130.                         chunk.pixels[CurrentPixelIndex2] = {96};
  131.                         chunk.pixels[CurrentPixelIndex2 +1] = {61};
  132.                         chunk.pixels[CurrentPixelIndex2 +2] = {0};
  133.                         chunk.pixels[CurrentPixelIndex2 +3] = {255};
  134.                     }
  135.                     else
  136.                     {
  137.                         chunk.pixels[CurrentPixelIndex2] = {63};
  138.                         chunk.pixels[CurrentPixelIndex2 +1] = {37};
  139.                         chunk.pixels[CurrentPixelIndex2 +2] = {0};
  140.                         chunk.pixels[CurrentPixelIndex2 +3] = {255};
  141.                     }
  142.  
  143.                 }
  144.             }
  145.             chunk.position = sf::Vector2f(chunk_x, chunk_y);
  146.             chunk.sprite.setPosition(chunkSize*chunk_y, chunkSize*chunk_x);
  147.             chunk.sprite.setTexture(*chunk.texture);
  148.             chunk.texture->update(chunk.pixels.get());
  149.             chunks.push_back(chunk);
  150.         }
  151.  
  152.     }
  153.     return chunks;
  154. }
  155.  
  156.  
  157.  
  158. int main()
  159. {
  160.     //window renderer
  161.     sf::RenderWindow window(sf::VideoMode(screen_width, screen_height), "Procedural Generation");
  162.     window.setVerticalSyncEnabled(true);
  163.  
  164.     std::vector<Chunk> chunks = regen();
  165.  
  166.     while (window.isOpen()) //Main Game Loop
  167.     {
  168.  
  169.         sf::Event event;
  170.         while (window.pollEvent(event))
  171.         {
  172.  
  173.             if (event.type == sf::Event::Closed)
  174.                 window.close();
  175.            
  176.             if(event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Space)
  177.                 chunks = regen();
  178.            
  179.             if(event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Enter)
  180.                 chunks = regen();
  181.        
  182.             if(event.type == sf::Event::Resized)
  183.             {
  184.                 sf::FloatRect visibleArea(0.f, 0.f, event.size.width, event.size.height);
  185.                 window.setView(sf::View(visibleArea));
  186.             }
  187.  
  188.         }
  189.         window.clear();
  190.         for (Chunk const& chunk : chunks)
  191.         {
  192.             window.draw(chunk.sprite);
  193.         }
  194.         window.display();
  195.        
  196.     }
  197.  
  198.     return 0;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement