Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "globals.h"
- #include <SFML/Graphics.hpp>
- #include <iostream>
- #include <random>
- #include <sstream>
- #include <string>
- #define MAXCOUNT 40
- using namespace typ;
- const uint VIDW = 800, VIDH = 600;
- std::random_device rd;
- std::mt19937 gen;
- std::uniform_int_distribution<> distrib(0, 255);
- static sf::Image* mandelbrot(uint w, uint h)
- {
- float left, top, xside, yside, xscale, yscale, zx, zy, cx, tempx, cy;
- int x, y, i, j;
- int maxx, maxy;
- u8 count;
- left = -1.75;
- top = -0.25;
- xside = 0.25;
- yside = 0.45;
- maxx = w;
- maxy = h;
- xscale = xside / maxx;
- yscale = yside / maxy;
- u8 pixels[(w * h * 4)];
- for (y = 1; y <= maxy - 1; y++) {
- for (x = 1; x <= maxx - 1; x++) {
- cx = x * xscale + left;
- cy = y * yscale + top;
- zx = 0;
- zy = 0;
- count = 0;
- while ((zx * zx + zy * zy < 4) && (count < MAXCOUNT)) {
- tempx = zx * zx - zy * zy + cx;
- zy = 2 * zx * zy + cy;
- zx = tempx;
- count++;
- }
- pixels[(y * w + x) * 4] = count * 10;
- pixels[(y * w + x) * 4 + 1] = count * 30;
- pixels[(y * w + x) * 4 + 2] = count * 20;
- pixels[(y * w + x) * 4 + 3] = 0xff;
- }
- }
- return new sf::Image(sf::Vector2u(w, h), pixels);
- ;
- }
- sf::Image* img_generate(uint w, uint h, uint selection = 0, uint variation = 0)
- {
- uint setalpha = 0;
- u8 pixels[(w * h * 4)];
- switch (selection) {
- case 0: // randomized data, colorized white noise
- for (uint i = 0; i < w * h * 4; i++) {
- if (setalpha == 3) {
- pixels[i] = 0xff;
- setalpha = 0;
- } else {
- pixels[i] = distrib(gen);
- setalpha++;
- }
- }
- return new sf::Image(sf::Vector2u(w, h), pixels);
- case 1: // variation 0 for red, 1 for green, 2 for blue
- for (int i = 0; i < w * h * 4; i++) {
- if (setalpha == 3) {
- pixels[i] = 0xff;
- setalpha = 0;
- } else {
- if (setalpha == variation)
- pixels[i] = 255;
- else
- pixels[i] = 0;
- setalpha++;
- }
- }
- return new sf::Image(sf::Vector2u(w, h), pixels);
- case 2: // mandelbrot fractal
- return mandelbrot(w, h);
- case 3: // pixel test
- for (u32 y = 0; y < h; y++)
- for (u32 x = 0; x < w; x++) {
- u32 i = (y * w + x) * 4;
- pixels[i] = i * 2;
- pixels[i + 1] = i * 2;
- pixels[i + 2] = 255;
- pixels[i + 3] = 255;
- }
- return new sf::Image(sf::Vector2u(w, h), pixels);
- default:
- break;
- }
- return 0;
- }
- int main(int argc, char* argv[])
- {
- std::mt19937 gen(rd());
- sf::RenderWindow window(sf::VideoMode({ VIDW, VIDH }), "SFML C++ demo!");
- sf::CircleShape shape(40.f);
- shape.setFillColor(sf::Color::Green);
- shape.setPosition(sf::Vector2f(140, 140));
- sf::Font font("anonymous_pro.ttf");
- sf::Text text(font);
- text.setString("truetype font render");
- text.setCharacterSize(48);
- text.setFillColor(sf::Color::Yellow);
- text.setPosition(sf::Vector2f(64, 64));
- uint mod = 0, var = 0;
- if (argc > 1)
- mod = strToNum<uint>(std::string(argv[1]));
- if (argc > 2)
- var = strToNum<uint>(std::string(argv[2]));
- sf::Image img = *img_generate(VIDW, VIDH, mod, var);
- // img.saveToFile("demo-image.png");
- sf::Texture tex(img);
- sf::Sprite spr(tex);
- const auto onClose = [&window](const sf::Event::Closed&) {
- window.close();
- };
- const auto onKeyPressed = [&window](const sf::Event::KeyPressed& keyPressed) {
- if (keyPressed.scancode == sf::Keyboard::Scancode::Escape)
- window.close();
- };
- while (window.isOpen()) {
- window.handleEvents(onClose, onKeyPressed);
- while (const std::optional event = window.pollEvent()) {
- if (event->is<sf::Event::Closed>())
- window.close();
- }
- window.clear(sf::Color::Black);
- window.draw(spr);
- // window.draw(shape);
- // window.draw(text);
- window.display();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement