Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Audio.hpp>
- #include <SFML/Graphics.hpp>
- #include <SFML/Network.hpp>
- #include <SFML/Main.hpp>
- #include <SFML/System.hpp>
- #include <SFML/Window.hpp>
- #include <SFML/OpenGL.hpp>
- #include <iostream>
- #include <cstdlib>
- #include "lib/FastNoiseLite.h"
- #include <random>
- #include <memory>
- using namespace std;
- const int screen_width = 128;
- const int screen_height = 128;
- const int pixel_channel = 4;
- const int chunkSize = 128;
- FastNoiseLite noiseparams(float oct, FastNoiseLite::NoiseType type, int seed)
- {
- FastNoiseLite noise;
- noise.SetSeed(seed);
- noise.SetNoiseType(type);
- noise.SetFractalLacunarity(3.0f);
- noise.SetFractalGain(0.4f);
- noise.SetFrequency(0.01f);
- noise.SetFractalOctaves(oct);
- noise.SetFractalType(noise.FractalType_FBm);
- return noise;
- };
- struct Chunk
- {
- std::shared_ptr<sf::Texture> texture;
- sf::Sprite sprite;
- sf::Vector2f position; //This will store the cordinate of the chunk
- std::shared_ptr<sf::Uint8[]> pixels;
- Chunk()
- {
- texture = std::make_unique<sf::Texture>();
- texture->create(screen_width, screen_height);
- pixels = std::make_unique<sf::Uint8[]>(screen_width * screen_height * pixel_channel);
- }
- };
- std::vector<Chunk> regen()
- {
- std::vector<Chunk> chunks;
- //Seed creation
- random_device rd; // non-deterministic generator
- mt19937 gen(rd());
- int seed1 = gen();
- srand48(seed1);
- int seed2 = rand();
- srand48(seed2);
- int seed3 = rand();
- //Noise maps
- FastNoiseLite moisture = noiseparams(3.99, FastNoiseLite::NoiseType::NoiseType_Perlin, seed1);
- for(float chunk_y = 0; chunk_y < 16; chunk_y++)
- {
- for(float chunk_x = 0; chunk_x < 8; chunk_x++)
- {
- Chunk& chunk = chunks.emplace_back();
- for(int y = 0; y < screen_height; ++y)
- {
- for(int x = 0; x < screen_width; ++x)
- {
- double moist = moisture.GetNoise(float((chunkSize*chunk_y)+x), float((chunkSize*chunk_x)+y));
- moist = (moist + 1.0) / 2.0;
- moist = int(moist*255);
- //Convert 2d representation of each index to 1d
- int CurrentPixelIndex2 = ((y * screen_height) + x) * 4;
- //Deep Water
- if(moist < 100)
- {
- chunk.pixels[CurrentPixelIndex2] = {0};
- chunk.pixels[CurrentPixelIndex2 +1] = {66};
- chunk.pixels[CurrentPixelIndex2 +2] = {137};
- chunk.pixels[CurrentPixelIndex2 +3] = {255};
- }
- //Deep water
- else if (moist < 120)
- {
- chunk.pixels[CurrentPixelIndex2] = {55};
- chunk.pixels[CurrentPixelIndex2 +1] = {102};
- chunk.pixels[CurrentPixelIndex2 +2] = {200};
- chunk.pixels[CurrentPixelIndex2 +3] = {255};
- }
- //Beaches
- else if (moist < 122)
- {
- chunk.pixels[CurrentPixelIndex2] = {209};
- chunk.pixels[CurrentPixelIndex2 +1] = {189};
- chunk.pixels[CurrentPixelIndex2 +2] = {111};
- chunk.pixels[CurrentPixelIndex2 +3] = {255};
- }
- //Plain
- else if(moist < 160)
- {
- chunk.pixels[CurrentPixelIndex2] = {71};
- chunk.pixels[CurrentPixelIndex2 +1] = {133};
- chunk.pixels[CurrentPixelIndex2 +2] = {47};
- chunk.pixels[CurrentPixelIndex2 +3] = {255};
- }
- //Jungle
- else if(moist < 180)
- {
- chunk.pixels[CurrentPixelIndex2] = {28};
- chunk.pixels[CurrentPixelIndex2 +1] = {101};
- chunk.pixels[CurrentPixelIndex2 +2] = {1};
- chunk.pixels[CurrentPixelIndex2 +3] = {255};
- }
- else if (moist < 200)
- {
- chunk.pixels[CurrentPixelIndex2] = {96};
- chunk.pixels[CurrentPixelIndex2 +1] = {61};
- chunk.pixels[CurrentPixelIndex2 +2] = {0};
- chunk.pixels[CurrentPixelIndex2 +3] = {255};
- }
- else
- {
- chunk.pixels[CurrentPixelIndex2] = {63};
- chunk.pixels[CurrentPixelIndex2 +1] = {37};
- chunk.pixels[CurrentPixelIndex2 +2] = {0};
- chunk.pixels[CurrentPixelIndex2 +3] = {255};
- }
- }
- }
- chunk.position = sf::Vector2f(chunk_x, chunk_y);
- chunk.sprite.setPosition(chunkSize*chunk_y, chunkSize*chunk_x);
- chunk.sprite.setTexture(*chunk.texture);
- chunk.texture->update(chunk.pixels.get());
- chunks.push_back(chunk);
- }
- }
- return chunks;
- }
- int main()
- {
- //window renderer
- sf::RenderWindow window(sf::VideoMode(screen_width, screen_height), "Procedural Generation");
- window.setVerticalSyncEnabled(true);
- std::vector<Chunk> chunks = regen();
- while (window.isOpen()) //Main Game Loop
- {
- sf::Event event;
- while (window.pollEvent(event))
- {
- if (event.type == sf::Event::Closed)
- window.close();
- if(event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Space)
- chunks = regen();
- if(event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Enter)
- chunks = regen();
- if(event.type == sf::Event::Resized)
- {
- sf::FloatRect visibleArea(0.f, 0.f, event.size.width, event.size.height);
- window.setView(sf::View(visibleArea));
- }
- }
- window.clear();
- for (Chunk const& chunk : chunks)
- {
- window.draw(chunk.sprite);
- }
- window.display();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement