Guest User

Untitled

a guest
Jun 1st, 2024
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. main.cpp
  2. ```c++
  3. #include <SFML/Graphics.hpp>
  4. #include <iostream>
  5. #include <vector>
  6. #include <cmath>
  7. #include <ctime>
  8.  
  9. const int width = 1080;
  10. const int height = 1080;
  11.  
  12. std::vector<sf::Uint8> pixels(width * height * 4, 0);
  13. double elapsedTime = 0.0;
  14.  
  15. void initializePixelData() {
  16. for (size_t i = 0; i < pixels.size(); i += 4) {
  17. pixels[i + 0] = 0; // R
  18. pixels[i + 1] = 0; // G
  19. pixels[i + 2] = 0; // B
  20. pixels[i + 3] = 255; // A
  21. }
  22. }
  23.  
  24. void generatePixelData() {
  25. // Decrease color values
  26. for (size_t i = 0; i < pixels.size(); i += 4) {
  27. pixels[i + 0] = std::max(0, pixels[i + 0] - 6); // R
  28. pixels[i + 1] = std::max(0, pixels[i + 1] - 6); // G
  29. pixels[i + 2] = std::max(0, pixels[i + 2] - 6); // B
  30. }
  31.  
  32. // Add random pixel data
  33. for (int k = 0; k < 100; ++k) {
  34. double x = static_cast<double>(rand()) / RAND_MAX;
  35. double y = static_cast<double>(rand()) / RAND_MAX;
  36. sf::Uint8 newPixel[3] = {
  37. static_cast<sf::Uint8>(rand() % 256),
  38. static_cast<sf::Uint8>(rand() % 256),
  39. static_cast<sf::Uint8>(rand() % 256)
  40. };
  41.  
  42. for (int j = 0; j < 2000; ++j) {
  43. x = fmod(elapsedTime + x + cos(y * 2.2 + x * 0.1), 1.0);
  44. y = fmod(elapsedTime * 0.3 + y + sin(x * 1.5), 1.0);
  45.  
  46. int px = static_cast<int>(x * width);
  47. int py = static_cast<int>(y * height);
  48. int index = (py * width + px) * 4;
  49.  
  50. pixels[index + 0] = std::min(255, pixels[index + 0] + newPixel[0] / 2);
  51. pixels[index + 1] = std::min(255, pixels[index + 1] + newPixel[1] / 2);
  52. pixels[index + 2] = std::min(255, pixels[index + 2] + newPixel[2] / 1);
  53. }
  54. }
  55. }
  56.  
  57. int main() {
  58. srand(static_cast<unsigned>(time(0)));
  59. sf::RenderWindow window(sf::VideoMode(width, height), "Graphics Demo");
  60.  
  61. sf::Texture texture;
  62. texture.create(width, height);
  63. sf::Sprite sprite(texture);
  64.  
  65. sf::Clock clock;
  66. int frameCount = 0;
  67. float lastTime = 0.0f;
  68.  
  69. initializePixelData();
  70.  
  71. while (window.isOpen()) {
  72. sf::Event event;
  73. while (window.pollEvent(event)) {
  74. if (event.type == sf::Event::Closed) {
  75. window.close();
  76. }
  77. }
  78.  
  79. elapsedTime += 0.0003;
  80. generatePixelData();
  81. texture.update(pixels.data());
  82.  
  83. window.clear();
  84. window.draw(sprite);
  85. window.display();
  86.  
  87. frameCount++;
  88. float currentTime = clock.getElapsedTime().asSeconds();
  89. if (currentTime - lastTime >= 1.0f) {
  90. std::cout << "FPS: " << frameCount << std::endl;
  91. frameCount = 0;
  92. lastTime = currentTime;
  93. }
  94. }
  95.  
  96. return 0;
  97. }
  98. ```c++
  99.  
  100. Makefile
  101. ```Makefile
  102. # Variables
  103. CXX = g++
  104. CXXFLAGS = -std=c++11 -Wall -O2 -I/opt/homebrew/Cellar/sfml/2.6.1/include
  105. LDFLAGS = -L/opt/homebrew/Cellar/sfml/2.6.1/lib
  106. SFML_LIBS = -lsfml-graphics -lsfml-window -lsfml-system
  107. TARGET = graphics_demo
  108. SOURCES = main.cpp
  109.  
  110. # Rules
  111. all: $(TARGET)
  112.  
  113. $(TARGET): $(SOURCES)
  114. $(CXX) $(CXXFLAGS) $(LDFLAGS) -o $(TARGET) $(SOURCES) $(SFML_LIBS)
  115.  
  116. clean:
  117. rm -f $(TARGET)
  118.  
  119. .PHONY: all clean
  120. ```
  121.  
  122. brew install sfml
  123.  
  124. check path
  125. brew info sfml
Advertisement
Add Comment
Please, Sign In to add comment