Advertisement
int8

C++ graphic generation with SFML

May 8th, 2025
184
0
303 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.40 KB | Source Code | 0 0
  1. #include "globals.h"
  2. #include <SFML/Graphics.hpp>
  3. #include <iostream>
  4. #include <random>
  5. #include <sstream>
  6. #include <string>
  7.  
  8. #define MAXCOUNT 40
  9. using namespace typ;
  10.  
  11. const uint VIDW = 800, VIDH = 600;
  12.  
  13. std::random_device rd;
  14. std::mt19937 gen;
  15. std::uniform_int_distribution<> distrib(0, 255);
  16.  
  17. static sf::Image* mandelbrot(uint w, uint h)
  18. {
  19.     float left, top, xside, yside, xscale, yscale, zx, zy, cx, tempx, cy;
  20.     int x, y, i, j;
  21.     int maxx, maxy;
  22.     u8 count;
  23.     left = -1.75;
  24.     top = -0.25;
  25.     xside = 0.25;
  26.     yside = 0.45;
  27.     maxx = w;
  28.     maxy = h;
  29.     xscale = xside / maxx;
  30.     yscale = yside / maxy;
  31.     u8 pixels[(w * h * 4)];
  32.  
  33.     for (y = 1; y <= maxy - 1; y++) {
  34.         for (x = 1; x <= maxx - 1; x++) {
  35.             cx = x * xscale + left;
  36.             cy = y * yscale + top;
  37.             zx = 0;
  38.             zy = 0;
  39.             count = 0;
  40.             while ((zx * zx + zy * zy < 4) && (count < MAXCOUNT)) {
  41.                 tempx = zx * zx - zy * zy + cx;
  42.                 zy = 2 * zx * zy + cy;
  43.                 zx = tempx;
  44.                 count++;
  45.             }
  46.             pixels[(y * w + x) * 4] = count * 10;
  47.             pixels[(y * w + x) * 4 + 1] = count * 30;
  48.             pixels[(y * w + x) * 4 + 2] = count * 20;
  49.             pixels[(y * w + x) * 4 + 3] = 0xff;
  50.         }
  51.     }
  52.     return new sf::Image(sf::Vector2u(w, h), pixels);
  53.     ;
  54. }
  55.  
  56. sf::Image* img_generate(uint w, uint h, uint selection = 0, uint variation = 0)
  57. {
  58.     uint setalpha = 0;
  59.     u8 pixels[(w * h * 4)];
  60.     switch (selection) {
  61.     case 0: // randomized data, colorized white noise
  62.         for (uint i = 0; i < w * h * 4; i++) {
  63.             if (setalpha == 3) {
  64.                 pixels[i] = 0xff;
  65.                 setalpha = 0;
  66.             } else {
  67.                 pixels[i] = distrib(gen);
  68.                 setalpha++;
  69.             }
  70.         }
  71.         return new sf::Image(sf::Vector2u(w, h), pixels);
  72.  
  73.     case 1: // variation 0 for red, 1 for green, 2 for blue
  74.  
  75.         for (int i = 0; i < w * h * 4; i++) {
  76.             if (setalpha == 3) {
  77.                 pixels[i] = 0xff;
  78.                 setalpha = 0;
  79.             } else {
  80.                 if (setalpha == variation)
  81.                     pixels[i] = 255;
  82.                 else
  83.                     pixels[i] = 0;
  84.                 setalpha++;
  85.             }
  86.         }
  87.         return new sf::Image(sf::Vector2u(w, h), pixels);
  88.  
  89.     case 2: // mandelbrot fractal
  90.         return mandelbrot(w, h);
  91.  
  92.     case 3: // pixel test
  93.         for (u32 y = 0; y < h; y++)
  94.             for (u32 x = 0; x < w; x++) {
  95.                 u32 i = (y * w + x) * 4;
  96.                 pixels[i] = i * 2;
  97.                 pixels[i + 1] = i * 2;
  98.                 pixels[i + 2] = 255;
  99.                 pixels[i + 3] = 255;
  100.             }
  101.         return new sf::Image(sf::Vector2u(w, h), pixels);
  102.  
  103.     default:
  104.         break;
  105.     }
  106.     return 0;
  107. }
  108.  
  109. int main(int argc, char* argv[])
  110. {
  111.     std::mt19937 gen(rd());
  112.  
  113.     sf::RenderWindow window(sf::VideoMode({ VIDW, VIDH }), "SFML C++ demo!");
  114.  
  115.     sf::CircleShape shape(40.f);
  116.     shape.setFillColor(sf::Color::Green);
  117.     shape.setPosition(sf::Vector2f(140, 140));
  118.  
  119.     sf::Font font("anonymous_pro.ttf");
  120.     sf::Text text(font);
  121.     text.setString("truetype font render");
  122.     text.setCharacterSize(48);
  123.     text.setFillColor(sf::Color::Yellow);
  124.     text.setPosition(sf::Vector2f(64, 64));
  125.     uint mod = 0, var = 0;
  126.     if (argc > 1)
  127.         mod = strToNum<uint>(std::string(argv[1]));
  128.     if (argc > 2)
  129.         var = strToNum<uint>(std::string(argv[2]));
  130.  
  131.     sf::Image img = *img_generate(VIDW, VIDH, mod, var);
  132.     // img.saveToFile("demo-image.png");
  133.  
  134.     sf::Texture tex(img);
  135.     sf::Sprite spr(tex);
  136.     const auto onClose = [&window](const sf::Event::Closed&) {
  137.         window.close();
  138.     };
  139.  
  140.     const auto onKeyPressed = [&window](const sf::Event::KeyPressed& keyPressed) {
  141.         if (keyPressed.scancode == sf::Keyboard::Scancode::Escape)
  142.             window.close();
  143.     };
  144.  
  145.     while (window.isOpen()) {
  146.         window.handleEvents(onClose, onKeyPressed);
  147.         while (const std::optional event = window.pollEvent()) {
  148.             if (event->is<sf::Event::Closed>())
  149.                 window.close();
  150.         }
  151.  
  152.         window.clear(sf::Color::Black);
  153.         window.draw(spr);
  154.         // window.draw(shape);
  155.         // window.draw(text);
  156.         window.display();
  157.     }
  158. }
  159.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement