Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <random>
- #include <SFML/Graphics.hpp>
- #include "globals.h"
- using namespace typ;
- const u32 VIDW = 800, VIDH = 600;
- std::random_device rd;
- std::mt19937 gen;
- std::uniform_int_distribution<> distrib(0, 255);
- sf::Image *img_generate(u32 w, u32 h, u32 selection=0, u32 variation=0)
- {
- u32 setalpha=0;
- u8 pixels[(w*h*4)];
- switch(selection)
- {
- case 0: //randomized data, colorized white noise
- for (int 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);
- break;
- 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);
- break;
- }
- return 0;
- }
- int main()
- {
- std::mt19937 gen(rd());
- sf::RenderWindow window(sf::VideoMode({VIDW, VIDH}), "SFML C++ demo!");
- sf::CircleShape shape(100.f);
- shape.setFillColor(sf::Color::Green);
- sf::Font font("anonymous_pro.ttf");
- sf::Text text(font);
- text.setString("truetype font render");
- text.setCharacterSize(48);
- text.setFillColor(sf::Color::Black);
- text.setPosition(sf::Vector2f(64,64));
- sf::Image img = *img_generate(800,600,0);
- //img.saveToFile("rainbow-snow.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(text);
- window.display();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement