SFML и C++ Уроки \ Разработка игр › Форумы › SFML Graphics › При отрисовке текста из класса окно программы сразу закрывается
В этой теме 1 ответ, 1 участник, последнее обновление barbar 7 года/лет, 6 мес. назад.
Просмотр 2 сообщений - с 1 по 2 (из 2 всего)
-
АвторСообщения
-
Когда пишу внизу app.draw(testButton.text); окно открывается и сразу закрывается.
привозу свой код
C++1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889#include <SFML/Graphics.hpp>#include <sstream>#include <string>void loadFont();class Button {bool pressed;int weight, height, posx, posy;std::ostringstream label;public:sf::Text text;sf::RectangleShape button;void newButton(int wh, int hh, int px, int py){weight=wh; height=hh; posx=px; posy=py;button.setSize(sf::Vector2f(weight, height));button.setPosition(posx,posy);button.setFillColor(sf::Color(200, 200, 200));};bool isCursor (float corx, float cory){if ((corx>posx) && (corx<weight+posx) && (cory>posy) && (cory<height+posy)) {button.setFillColor(sf::Color(100, 100, 100));return true; }else { button.setFillColor(sf::Color(200, 200, 200));return false;}}void setLabel (std::string lb, std::string fnt){label <<lb;sf::Font font;font.loadFromFile(fnt);text.setString(label.str());text.setFont(font);text.setCharacterSize(24);text.setColor(sf::Color::Black);text.setPosition(posx,posy);}};int main(){sf::RenderWindow app(sf::VideoMode(800, 600), "SFML window");Button testButton;testButton.newButton(80,30,700,550);testButton.setLabel("exit","CyrilicOld.TTF");while (app.isOpen()){sf::Event event;while (app.pollEvent(event)){if (event.type == sf::Event::Closed)app.close();}sf::Vector2i pixelPos = sf::Mouse::getPosition(app);sf::Vector2f pos = app.mapPixelToCoords(pixelPos);if ((testButton.isCursor(pos.x,pos.y)) && (sf::Mouse::isButtonPressed(sf::Mouse::Left)) )break;app.clear(sf::Color(255,255,255));app.draw(testButton.button);app.draw(testButton.text);app.display();}return EXIT_SUCCESS;}выхлоп терминала
segmentation fault
Я решил проблему, вдруг надо кому нибудь, опишу как решил:
создавать объект класса Font надо было в самом теле класса, а не функции как в примере выше.
-
АвторСообщения
Просмотр 2 сообщений - с 1 по 2 (из 2 всего)
Для ответа в этой теме необходимо авторизоваться.