The documentation says:

Detailed Description Utility class for manpulating RGBA colors. sf::Color is a simple color class composed of 4 components: Red Green Blue Alpha (opacity) Each component is a public member, an unsigned integer in the range [0, 255]. Thus, colors can be constructed and manipulated very easily: sf::Color color(255, 0, 0); // red color.r = 0; // make it black color.b = 128; // make it dark blue 

[Link ...] [1] But why then does the compiler point out errors when setting the color BACKGROUND?

 #include <SFML/Graphics.hpp> #include <SFML/System.hpp> #include <SFML/Window.hpp> #include <vector> #include <cmath> #include <iostream> #include <string> #include <set> #include <random> #include <ctime> using namespace std; using namespace sf; constexpr unsigned BALLS_COUNT = 4; const string WINDOW_TITLE = "Moving balls"; static const sf::Color BACKGROUND_COLOR;// = sf::Color(255, 255, 255); BACKGROUND_COLOR.red = 255; BACKGROUND_COLOR.g = 255; BACKGROUND_COLOR.b = 255; constexpr unsigned WINDOW_WIDTH = 800; constexpr unsigned WINDOW_HEIGHT = 600; struct Colr { int red; int green; int blue; bool operator<(const Colr& curr) const { if (red < curr.red) { return true; } else if (red == curr.red) { if (green < curr.green) { return true; } else if (green == curr.green) { return blue < curr.blue; } } return false; } }; struct Ball { CircleShape shape; Vector2f offset; }; struct PRNG { std::mt19937 engine; }; void initGenerator(PRNG& generator) { // Используем время с 1 января 1970 года в секундах как случайное зерно const unsigned seed = unsigned(std::time(nullptr)); generator.engine.seed(seed); cout << "seed == " << seed << endl; } // Генерирует индекс в диапазоне [0, size) size_t random_index(PRNG& generator, int size) { // Создаём распределение std::uniform_int_distribution<int> distribution(0, size - 1); // Вычисляем псевдослучайное число: вызовем распределение как функцию, // передав генератор произвольных целых чисел как аргумент. return distribution(generator.engine); } Colr initColor(PRNG generator, set<Colr>& isColrs) { Colr colr; colr.red = random_index(generator, 256); colr.green = random_index(generator, 256); colr.blue = random_index(generator, 256); if(isColrs.empty() || (isColrs.find(colr) == isColrs.end())) { isColrs.insert(colr); return colr; } else { initColor(generator, isColrs); } } void pollEvents(RenderWindow& window) { Event event{}; while (window.pollEvent(event)) { switch (event.type) { case Event::Closed: window.close(); break; default: break; } } } float length(const Vector2f& value) { return std::hypot(value.x, value.y); } float distance(const Ball& ball, const Ball& curr) { return length(ball.shape.getPosition() - curr.shape.getPosition()); } float dot(const Vector2f& left, const Vector2f& right) { return left.x * right.x + left.y * right.y; } float sqr(float value) { return value * value; } sf::Vector2f getSpeedAfterStrike(const Ball& first, const Ball& second) { const Vector2f deltaSpeed = first.offset - second.offset; const Vector2f deltaPos = first.shape.getPosition() - second.shape.getPosition(); return first.offset - deltaPos * (dot(deltaSpeed, deltaPos) / sqr(length(deltaPos))); } void update(vector<Ball>& balls, float deltaTime) { constexpr float MAX_DELTA_TIME = 0.1f; deltaTime = std::min(deltaTime, MAX_DELTA_TIME); for (auto& ball: balls) { if ((ball.shape.getPosition().x + ball.shape.getRadius() >= WINDOW_WIDTH) || (ball.shape.getPosition().y + ball.shape.getRadius() >= WINDOW_HEIGHT) || (ball.shape.getPosition().x - ball.shape.getRadius() <= 0) || (ball.shape.getPosition().y - ball.shape.getRadius() <= 0)) { ball.offset = -ball.offset; } } for (size_t fi = 0; fi < balls.size(); ++fi) { for (size_t si = fi + 1; si < balls.size(); ++si) { Ball& first = balls[fi]; Ball& second = balls[si]; if (distance(first, second) <= (first.shape.getRadius() + second.shape.getRadius())) { const Vector2f newFirstSpeed = getSpeedAfterStrike(first, second); const Vector2f newSecondSpeed = getSpeedAfterStrike(second, first); first.offset = newFirstSpeed; second.offset = newSecondSpeed; } } } for (auto& ball: balls) { ball.shape.move(ball.offset * deltaTime); } } void redrawFrame(RenderWindow& window, vector<Ball>& balls) { window.clear(BACKGROUND_COLOR); for (auto& ball: balls) { window.draw(ball.shape); } window.display(); } void initBall(PRNG generator, vector<Ball>& balls, Ball& ball, set<Colr>& isColrs) { const vector<float> sizes = { 40, 40, 40, 40 }; const vector<Vector2f> positions = { { 5* sizes.at(0), 5 * sizes.at(0) }, { WINDOW_WIDTH - 3 * sizes.at(1), WINDOW_HEIGHT - 12 * sizes.at(1) }, { WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2 }, { WINDOW_WIDTH - 3 * sizes.at(3), WINDOW_HEIGHT - 3 * sizes.at(3) } }; for (int i = 0; i < BALLS_COUNT; ++i) { balls.at(i).offset.x = random_index(generator, 250); balls.at(i).offset.y = random_index(generator, 250); balls.at(i).shape.setRadius(sizes.at(i)); balls.at(i).shape.setPosition(positions.at(i)); Colr colr = initColor(generator, isColrs); balls.at(i).shape.setFillColor(sf::Color(colr.red, colr.green, colr.blue)); balls.at(i).shape.setOrigin(balls.at(i).shape.getRadius(), balls.at(i).shape.getRadius()); } } void init(PRNG& generator, set<Colr>& isColrs, vector<Ball>& balls, Ball& ball) { initGenerator(generator); initBall(generator, balls, ball, isColrs); } int main() { Clock clock; ContextSettings settings; RenderWindow window(VideoMode({WINDOW_WIDTH, WINDOW_HEIGHT}), WINDOW_TITLE, Style::Default, settings); vector<Ball> balls(BALLS_COUNT); PRNG generator; Ball ball; set<Colr> isColrs; init(generator, isColrs, balls, ball); while (window.isOpen()) { float deltaTime = clock.restart().asSeconds(); pollEvents(window); update(balls, deltaTime); redrawFrame(window, balls); } } 

Compiler errors:

  [ 50%] Building CXX object CMakeFiles/workshop2.4.dir/main.obj D:\cpp\Projects\labs\workshop2\workshop2.4\main.cpp:20:1: error: 'BACKGROUND_COLOR' does not name a type BACKGROUND_COLOR.red = 255; ^~~~~~~~~~~~~~~~ D:\cpp\Projects\labs\workshop2\workshop2.4\main.cpp:21:1: error: 'BACKGROUND_COLOR' does not name a type BACKGROUND_COLOR.g = 255; ^~~~~~~~~~~~~~~~ D:\cpp\Projects\labs\workshop2\workshop2.4\main.cpp:22:1: error: 'BACKGROUND_COLOR' does not name a type BACKGROUND_COLOR.b = 255; ^~~~~~~~~~~~~~~~ mingw32-make.exe[2]: *** [CMakeFiles\workshop2.4.dir\build.make:63: CMakeFiles/workshop2.4.dir/main.obj] Error 1 mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:67: CMakeFiles/workshop2.4.dir/all] Error 2 mingw32-make.exe: *** [Makefile:83: all] Error 2 
  • On what line swears and what he writes. Add the full text of the error. - ffk
  • the compiler swears at the lines: BACKGROUND_COLOR.red = 255; BACKGROUND_COLOR.green = 255; BACKGROUND_COLOR.blue = 255; - deekee
  • I suspect that you have .r fields there, not .red , well, and so on. In the description, color.r = 0; . Why did you comment this out - //BACKGROUND_COLOR.r = 255; ? Didn’t work with him? - Harry
  • not. the same error in both cases. - deekee
  • @deekee, you want to help, then copy the error message that the compiler gives here. There is no show jumping with elements of the work, but there are no telepaths. There must be something source_file.cpp(12): error C2039: 'x': is not a member of 'A' source_file.cpp(6): note: see declaration of 'A' or source_file.cpp: In function 'int main()': source_file.cpp:7:5: error: 'X' was not declared in this scope X a; - ffk

1 answer 1

 static const sf::Color BACKGROUND_COLOR = sf::Color(255, 255, 255); //BACKGROUND_COLOR.red = 255; // Так можно писать только внутри функции //BACKGROUND_COLOR.g = 255; // В глобальном пространстве нельзя //BACKGROUND_COLOR.b = 255; constexpr unsigned WINDOW_WIDTH = 800; 

What option did not suit below?

 static const sf::Color BACKGROUND_COLOR = sf::Color(255, 255, 255); 
  • I wanted to use in the program a comparison with each component BACKGROUND_COLOR, but it seems to have bypassed this moment. So the question can be considered closed - deekee