itsspynex

Untitled

Aug 27th, 2023
1,389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.33 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 "PerlinNoise-master/PerlinNoise.hpp"
  9. #include <iostream>
  10.  
  11. using namespace std;
  12.  
  13.  
  14.     const int screen_width = 800;
  15.     const int screen_height = 800;
  16.     const int a = 800;
  17.     const int b = 800;
  18.     sf::Uint8* pixels = new sf::Uint8[a * b * 4];
  19.  
  20. void regen()
  21. {
  22.     uint8_t * pixel = new uint8_t[a * b * 4];
  23.    
  24.     //Noise function perameters
  25.     int random = rand();
  26.     const siv::PerlinNoise::seed_type seed = rand();
  27.     const siv::PerlinNoise perlin{ seed };
  28.  
  29.     //Draw Pixels
  30.     std::vector<int[screen_width]> arr(screen_height);
  31.     std::vector<int[screen_width]> arr1(screen_height);
  32.     std::vector<int[screen_width]> arr2(screen_height);
  33.     for(int32_t i = 0; i < screen_height; ++i)
  34.     {
  35.         for(int32_t j = 0; j < screen_width; ++j)
  36.         {
  37.             int p_value[3];
  38.             const double noise = perlin.octave2D_01((i * 0.01), (j * 0.01), 4);
  39.             if(noise <= 0.5)
  40.             {
  41.                 arr[i][j] = {0};
  42.                 arr1[i][j] = {66};
  43.                 arr2[i][j] = {137};
  44.             }
  45.             else
  46.             {
  47.                 arr[i][j] = {66};
  48.                 arr1[i][j] = {98};
  49.                 arr2[i][j] = {32};
  50.             }
  51.  
  52.  
  53.             //Convert 2d representation of each index to 1d
  54.             int CurrentPixelIndex2 = ((i * screen_height) + j);
  55.             pixel[4 * CurrentPixelIndex2] = arr[i][j];
  56.             pixel[4 * CurrentPixelIndex2 +1] = arr1[i][j];
  57.             pixel[4 * CurrentPixelIndex2 +2] = arr2[i][j];
  58.             pixel[4 * CurrentPixelIndex2 +3] = 255;
  59.         }
  60.     }
  61.     for(int32_t i = 0; i < screen_width*screen_height; i+=4)
  62.     {
  63.         pixels[i] = pixel[i];
  64.         pixels[i+1] = pixel[i+1];
  65.         pixels[i+2] = pixel[i+2];
  66.         pixels[i+3] = pixel[i+3];
  67.     }
  68.  
  69.  
  70. }
  71.  
  72.  
  73.  
  74. int main()
  75. {
  76.  
  77.  
  78.     //window renderer
  79.     sf::RenderWindow window(sf::VideoMode(screen_width, screen_height), "Procedural Generation");
  80.     window.setVerticalSyncEnabled(true);
  81.  
  82.  
  83.     //Texture
  84.     sf::Texture Texture;
  85.     Texture.create(screen_width, screen_height);
  86.  
  87.  
  88.     //Sprite
  89.     sf::Sprite sprite(Texture);
  90.  
  91.  
  92.     //TODO: i need to make a 2d array with some grayscale intensity values in each row
  93.     //R, G, B
  94.     //Next row...
  95.  
  96.     // 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)
  97.  
  98.     //CurrentPixelIndex2 int maps the 2D position of the pixel to its linear index in the pixel array.
  99.  
  100.     //The same value from arr2[x][y] is assigned to all color components (R, G, B) of the pixel at that index.
  101.  
  102.  
  103.    
  104.     while (window.isOpen()) //Main Game Loop
  105.     {
  106.  
  107.         sf::Event event;
  108.         while (window.pollEvent(event))
  109.         {
  110.  
  111.             if (event.type == sf::Event::Closed)
  112.                 window.close();
  113.            
  114.             if(event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Enter)
  115.                 regen();
  116.  
  117.         };
  118.  
  119.        
  120.         Texture.update(pixels);
  121.         window.clear();
  122.         window.draw(sprite);
  123.         window.display();
  124.  
  125.     }
  126.  
  127.     return 0;
  128. }
  129.  
Advertisement
Add Comment
Please, Sign In to add comment