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 "PerlinNoise-master/PerlinNoise.hpp"
- #include <iostream>
- using namespace std;
- const int screen_width = 800;
- const int screen_height = 800;
- const int a = 800;
- const int b = 800;
- sf::Uint8* pixels = new sf::Uint8[a * b * 4];
- void regen()
- {
- uint8_t * pixel = new uint8_t[a * b * 4];
- //Noise function perameters
- int random = rand();
- const siv::PerlinNoise::seed_type seed = rand();
- const siv::PerlinNoise perlin{ seed };
- //Draw Pixels
- std::vector<int[screen_width]> arr(screen_height);
- std::vector<int[screen_width]> arr1(screen_height);
- std::vector<int[screen_width]> arr2(screen_height);
- for(int32_t i = 0; i < screen_height; ++i)
- {
- for(int32_t j = 0; j < screen_width; ++j)
- {
- int p_value[3];
- const double noise = perlin.octave2D_01((i * 0.01), (j * 0.01), 4);
- if(noise <= 0.5)
- {
- arr[i][j] = {0};
- arr1[i][j] = {66};
- arr2[i][j] = {137};
- }
- else
- {
- arr[i][j] = {66};
- arr1[i][j] = {98};
- arr2[i][j] = {32};
- }
- //Convert 2d representation of each index to 1d
- int CurrentPixelIndex2 = ((i * screen_height) + j);
- pixel[4 * CurrentPixelIndex2] = arr[i][j];
- pixel[4 * CurrentPixelIndex2 +1] = arr1[i][j];
- pixel[4 * CurrentPixelIndex2 +2] = arr2[i][j];
- pixel[4 * CurrentPixelIndex2 +3] = 255;
- }
- }
- for(int32_t i = 0; i < screen_width*screen_height; i+=4)
- {
- pixels[i] = pixel[i];
- pixels[i+1] = pixel[i+1];
- pixels[i+2] = pixel[i+2];
- pixels[i+3] = pixel[i+3];
- }
- }
- int main()
- {
- //window renderer
- sf::RenderWindow window(sf::VideoMode(screen_width, screen_height), "Procedural Generation");
- window.setVerticalSyncEnabled(true);
- //Texture
- sf::Texture Texture;
- Texture.create(screen_width, screen_height);
- //Sprite
- sf::Sprite sprite(Texture);
- //TODO: i need to make a 2d array with some grayscale intensity values in each row
- //R, G, B
- //Next row...
- // then make 1 dimentional pixel and pixels pointer int arrays, one to be used for sfml to store pixel data with width*height*4(for each grayscale value RGBA)
- //CurrentPixelIndex2 int maps the 2D position of the pixel to its linear index in the pixel array.
- //The same value from arr2[x][y] is assigned to all color components (R, G, B) of the pixel at that index.
- 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::Enter)
- regen();
- };
- Texture.update(pixels);
- window.clear();
- window.draw(sprite);
- window.display();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment