Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- main.cpp
- ```c++
- #include <SFML/Graphics.hpp>
- #include <iostream>
- #include <vector>
- #include <cmath>
- #include <ctime>
- const int width = 1080;
- const int height = 1080;
- std::vector<sf::Uint8> pixels(width * height * 4, 0);
- double elapsedTime = 0.0;
- void initializePixelData() {
- for (size_t i = 0; i < pixels.size(); i += 4) {
- pixels[i + 0] = 0; // R
- pixels[i + 1] = 0; // G
- pixels[i + 2] = 0; // B
- pixels[i + 3] = 255; // A
- }
- }
- void generatePixelData() {
- // Decrease color values
- for (size_t i = 0; i < pixels.size(); i += 4) {
- pixels[i + 0] = std::max(0, pixels[i + 0] - 6); // R
- pixels[i + 1] = std::max(0, pixels[i + 1] - 6); // G
- pixels[i + 2] = std::max(0, pixels[i + 2] - 6); // B
- }
- // Add random pixel data
- for (int k = 0; k < 100; ++k) {
- double x = static_cast<double>(rand()) / RAND_MAX;
- double y = static_cast<double>(rand()) / RAND_MAX;
- sf::Uint8 newPixel[3] = {
- static_cast<sf::Uint8>(rand() % 256),
- static_cast<sf::Uint8>(rand() % 256),
- static_cast<sf::Uint8>(rand() % 256)
- };
- for (int j = 0; j < 2000; ++j) {
- x = fmod(elapsedTime + x + cos(y * 2.2 + x * 0.1), 1.0);
- y = fmod(elapsedTime * 0.3 + y + sin(x * 1.5), 1.0);
- int px = static_cast<int>(x * width);
- int py = static_cast<int>(y * height);
- int index = (py * width + px) * 4;
- pixels[index + 0] = std::min(255, pixels[index + 0] + newPixel[0] / 2);
- pixels[index + 1] = std::min(255, pixels[index + 1] + newPixel[1] / 2);
- pixels[index + 2] = std::min(255, pixels[index + 2] + newPixel[2] / 1);
- }
- }
- }
- int main() {
- srand(static_cast<unsigned>(time(0)));
- sf::RenderWindow window(sf::VideoMode(width, height), "Graphics Demo");
- sf::Texture texture;
- texture.create(width, height);
- sf::Sprite sprite(texture);
- sf::Clock clock;
- int frameCount = 0;
- float lastTime = 0.0f;
- initializePixelData();
- while (window.isOpen()) {
- sf::Event event;
- while (window.pollEvent(event)) {
- if (event.type == sf::Event::Closed) {
- window.close();
- }
- }
- elapsedTime += 0.0003;
- generatePixelData();
- texture.update(pixels.data());
- window.clear();
- window.draw(sprite);
- window.display();
- frameCount++;
- float currentTime = clock.getElapsedTime().asSeconds();
- if (currentTime - lastTime >= 1.0f) {
- std::cout << "FPS: " << frameCount << std::endl;
- frameCount = 0;
- lastTime = currentTime;
- }
- }
- return 0;
- }
- ```c++
- Makefile
- ```Makefile
- # Variables
- CXX = g++
- CXXFLAGS = -std=c++11 -Wall -O2 -I/opt/homebrew/Cellar/sfml/2.6.1/include
- LDFLAGS = -L/opt/homebrew/Cellar/sfml/2.6.1/lib
- SFML_LIBS = -lsfml-graphics -lsfml-window -lsfml-system
- TARGET = graphics_demo
- SOURCES = main.cpp
- # Rules
- all: $(TARGET)
- $(TARGET): $(SOURCES)
- $(CXX) $(CXXFLAGS) $(LDFLAGS) -o $(TARGET) $(SOURCES) $(SFML_LIBS)
- clean:
- rm -f $(TARGET)
- .PHONY: all clean
- ```
- brew install sfml
- check path
- brew info sfml
Advertisement
Add Comment
Please, Sign In to add comment