使用SFML从函数绘制文本

时间:2019-05-02 20:18:31

标签: c++ sfml

我正在尝试使用一个函数来根据输入参数设置要在SFML上显示的文本。

该函数的类型为Text,并返回一个文本对象。我添加了cout语句来确定错误发生在哪里。文本如下:

#include <string>
#include <iostream>
#include "functions.h"
using namespace std;
#include <SFML/Graphics.hpp>                    // include the SFML Graphics Library
using namespace sf;

Text showPoints(int& points, bool addPoints, bool isPlayer){
    cout << "Function called " << endl;
    string disPoints;
    Text toDisp;
    Font pointFont;
    if(addPoints){
        points += 1;
    }
    cout << "Points added" << endl;
    if(!pointFont.loadFromFile("data/arial.ttf") ){
        cout << "could not load font" << endl;
    }
    cout << "Loaded fonts" << endl;
    if(isPlayer){
        cout << "isPlayer conditional" << endl;
        disPoints = "Player Points: ";
        toDisp.setPosition(50, 800);
        toDisp.setFont(pointFont);
        toDisp.setString(disPoints);
        toDisp.setFillColor(Color::White);
        toDisp.setCharacterSize(30);
    }
    else if(!isPlayer){
        cout << "isAI conditional" << endl;
        disPoints = "AI Points: ";
        toDisp.setPosition(1000, 200);
        toDisp.setFont(pointFont);
        toDisp.setString(disPoints);
        toDisp.setFillColor(Color::White);
        toDisp.setCharacterSize(30);
    }

    cout << "Conditions passed" << endl;
    return toDisp;
}

//usual SFML stuff... under the while(window.isOpen()), before the 
//window.display() and events check

        cout << "Error on function?" << endl;
        window.draw(showPoints(playerPoints, 0, 1));
        cout << "First function passed" << endl;
        window.draw(showPoints(AIPoints, 0, 0));

我希望文本会显示在SFML窗口的适当位置。但是,终端输出此消息,窗口崩溃:

Error on function?
Function called 
Points added
Loaded fonts
isPlayer conditional
Conditions passed

Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)

window.draw()函数一定有问题,因为未调用第二个绘制函数。那么在window.draw()中调用类型Text的函数合法吗?如果没有,我应该怎么做? Google搜索对该问题没有任何帮助。

0 个答案:

没有答案