Advertisement
int8

white noise image generator

Apr 15th, 2025
310
0
176 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 KB | Source Code | 0 0
  1. #include <random>
  2. #include <SFML/Graphics.hpp>
  3. #include "globals.h"
  4.  
  5. using namespace typ;
  6.  
  7. const u32 VIDW = 800, VIDH = 600;
  8.  
  9. std::random_device rd;
  10. std::mt19937 gen;
  11. std::uniform_int_distribution<> distrib(0, 255);
  12.  
  13. sf::Image *img_generate(u32 w, u32 h, u32 selection=0, u32 variation=0)
  14. {
  15.     u32 setalpha=0;
  16.     u8 pixels[(w*h*4)];
  17.     switch(selection)
  18.     {
  19.     case 0: //randomized data, colorized white noise
  20.         for (int i=0;i<w*h*4;i++)
  21.         {
  22.             if(setalpha == 3)
  23.             {
  24.                 pixels[i] = 0xff;
  25.                 setalpha = 0;
  26.             }
  27.             else
  28.             {
  29.                 pixels[i] = distrib(gen);
  30.                 setalpha++;
  31.             }
  32.         }
  33.         return new sf::Image(sf::Vector2u(w,h),pixels);
  34.         break;
  35.     case 1: //variation 0 for red, 1 for green, 2 for blue
  36.  
  37.         for (int i=0;i<w*h*4;i++)
  38.         {
  39.             if(setalpha == 3)
  40.             {
  41.                 pixels[i] = 0xff;
  42.                 setalpha = 0;
  43.             }
  44.             else
  45.             {
  46.                 if(setalpha == variation) pixels[i] = 255;
  47.                 else pixels[i] = 0;            
  48.                 setalpha++;
  49.             }
  50.         }
  51.         return new sf::Image(sf::Vector2u(w,h),pixels);
  52.         break;
  53.     }
  54.     return 0;
  55. }
  56.  
  57.  
  58.  
  59. int main()
  60. {
  61.     std::mt19937 gen(rd());
  62.  
  63.     sf::RenderWindow window(sf::VideoMode({VIDW, VIDH}), "SFML C++ demo!");
  64.     sf::CircleShape shape(100.f);
  65.     shape.setFillColor(sf::Color::Green);
  66.  
  67.     sf::Font font("anonymous_pro.ttf");
  68.     sf::Text text(font);
  69.     text.setString("truetype font render");
  70.     text.setCharacterSize(48);
  71.     text.setFillColor(sf::Color::Black);
  72.     text.setPosition(sf::Vector2f(64,64));
  73.  
  74.     sf::Image img = *img_generate(800,600,0);
  75.     //img.saveToFile("rainbow-snow.png");
  76.    
  77.     sf::Texture tex(img);
  78.     sf::Sprite spr(tex);
  79.     const auto onClose = [&window](const sf::Event::Closed&)
  80.     {
  81.         window.close();
  82.     };
  83.  
  84.     const auto onKeyPressed = [&window](const sf::Event::KeyPressed& keyPressed)
  85.     {
  86.         if (keyPressed.scancode == sf::Keyboard::Scancode::Escape)
  87.             window.close();
  88.     };
  89.  
  90.     while (window.isOpen())
  91.     {
  92.         window.handleEvents(onClose, onKeyPressed);
  93.         while (const std::optional event = window.pollEvent())
  94.         {
  95.             if (event->is<sf::Event::Closed>())
  96.                 window.close();
  97.         }
  98.  
  99.         window.clear(sf::Color::Black);
  100.         window.draw(spr);
  101.         window.draw(text);
  102.         window.display();
  103.     }
  104. }
Tags: c++ sfml
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement