Хранение пользовательских Drawables в массиве вызывает ошибку сегментации при их рисовании

у меня есть std::vector<Button> называется levelButtonsкакие магазины Buttonвот так:

for (int i = 1; i <= 8; i++) {
for (int j = 0; j < 4; j++) {
Button level(50, 50, std::bind(&Game::continueGame, this, (j * 8) + i), " " + std::to_string((j * 8) + i), &levelTexture);
level.setPosition(50 + (i * 100), 200 + (j * 100));
levelButtons.push_back(level);
}
}

А потом я нарисую их позже так:

for (int i=0; i<levelButtons.size(); i++) {
win->draw(levelButtons[i]);
}

куда win это RenderWindow*, Класс Button выглядит так:

Button::Button(float width, float height, std::function<void()> onclick, String face, Color fillColour) {
arial.loadFromFile("/Users/mmysteriouss/Downloads/arial.ttf");
pointerTexture.loadFromFile("/Users/mmysteriouss/Downloads/pointer.png");
pointer.setTexture(pointerTexture);
pointer.scale(0.04, 0.04);
buttonface.setFont(arial);
buttonface.setString(face);
buttonface.setCharacterSize(30);
buttonface.setFillColor(Color::Black);
outline.setSize(Vector2f(width, height));
outline.setOutlineColor(Color::Black);
outline.setFillColor(fillColour);
click = onclick;
}

Button::Button(float width, float height, std::function<void()> onclick, String face, Texture *fillTexture) {
arial.loadFromFile("/Users/mmysteriouss/Downloads/arial.ttf");
pointerTexture.loadFromFile("/Users/mmysteriouss/Downloads/pointer.png");
pointer.setTexture(pointerTexture);
pointer.scale(0.04, 0.04);
buttonface.setFont(arial);
buttonface.setString(face);
buttonface.setCharacterSize(30);
buttonface.setFillColor(Color::White);
outline.setSize(Vector2f(width, height));
outline.setOutlineColor(Color::Black);
outline.setTexture(fillTexture);
click = onclick;
}

void Button::handleClick(float x, float y) {
if (outline.getGlobalBounds().contains(x, y)) {
click();
}
}

void Button::setPosition(float x, float y) {
outline.setPosition(x, y);
buttonface.setPosition(x, y);
}

void Button::handleMove(float x, float y) {
hovering = outline.getGlobalBounds().contains(x, y);
pointer.setPosition(x - 15, y - 5);
}

И это бросает

Ошибка сегментации: 11;

Отчет выглядит так:

поток 0 Crashed :: очередь отправки: com.apple.main-thread

0 libsfml-graphics.2.4.dylib 0x0000000107e0c6f7 sf :: Font :: setCurrentSize (unsigned int) const + 23

1 libsfml-graphics.2.4.dylib 0x0000000107e0c914 sf :: Font :: getUnderlinePosition (unsigned int) const + 30

2 libsfml-graphics.2.4.dylib 0x0000000107e35c30 sf :: Text :: sureGeometryUpdate () const + 202

3 libsfml-graphics.2.4.dylib 0x0000000107e363aa sf :: Text :: draw (sf :: RenderTarget)&, sf :: RenderStates) const + 38

4 libsfml-graphics.2.4.dylib 0x0000000107e29dfd sf :: RenderTarget :: draw (sf :: Drawable const&, sf :: RenderStates const&) + 41

5 ламп 0x0000000107dcd081 Button :: draw (sf :: RenderTarget)&, sf :: RenderStates) const + 81 (Button.h: 43)

6 libsfml-graphics.2.4.dylib 0x0000000107e29dfd sf :: RenderTarget :: draw (sf :: Drawable const&, sf :: RenderStates const&) + 41

7 light 0x0000000107dd43c2 Game :: draw () + 210
(Game.cpp: 26)

8 light 0x0000000107dd4bc5 AbsGame :: render () + 69 (AbsGame.h: 44)

9 light 0x0000000107dd4910 main + 496 (main.cpp: 27)

10 libdyld.dylib 0x00007fff76f58015 start + 1

Почему возникает ошибка сегментации? Я почти уверен, что моя итерация в порядке, и мой класс Button работает отдельно — я создал основной файл для тестирования, и он сработал …

0

Решение

Я починил это. Я просто добавил этот код для загрузки спрайта:

sf::Sprite Button::loadSprite(std::string source) {
static sf::Texture t;
t.loadFromFile(source);
static sf::Sprite s;
s.setTexture(t);
return s;
}

и использовал это. Спасибо за вашу помощь.

0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector