Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于识别页面结尾的函数赢得';我不能正确地拆分文本 我试图在C++和SFML中编写一个简单的任务日志程序,比如RPG游戏中的任务日志。我被困在负责在一次任务中显示所有条目的状态/功能上。我希望它能像电子书阅读器一样工作,这意味着当打开一个任务时,页面(和std::vector)会充满所有条目,并且在任何给定的时间只显示两个相邻的条目。首先,我必须设置一个函数,该函数可以识别何时到达页面的水平端,并且工作正常_C++_Text_Sfml - Fatal编程技术网

用于识别页面结尾的函数赢得';我不能正确地拆分文本 我试图在C++和SFML中编写一个简单的任务日志程序,比如RPG游戏中的任务日志。我被困在负责在一次任务中显示所有条目的状态/功能上。我希望它能像电子书阅读器一样工作,这意味着当打开一个任务时,页面(和std::vector)会充满所有条目,并且在任何给定的时间只显示两个相邻的条目。首先,我必须设置一个函数,该函数可以识别何时到达页面的水平端,并且工作正常

用于识别页面结尾的函数赢得';我不能正确地拆分文本 我试图在C++和SFML中编写一个简单的任务日志程序,比如RPG游戏中的任务日志。我被困在负责在一次任务中显示所有条目的状态/功能上。我希望它能像电子书阅读器一样工作,这意味着当打开一个任务时,页面(和std::vector)会充满所有条目,并且在任何给定的时间只显示两个相邻的条目。首先,我必须设置一个函数,该函数可以识别何时到达页面的水平端,并且工作正常,c++,text,sfml,C++,Text,Sfml,水平断裂的结果: 但是,当我添加了一个函数来识别条目何时到达页面的垂直端时,就会发生这种情况 水平+垂直断裂的结果: 下面是填充页面的函数的代码 if (!mPagesFilled) { //make sure container of pages is not empty if(mPages.empty()) mPages.push_back(Page()); for (auto it : mViewedQuest->questEntries

水平断裂的结果:

但是,当我添加了一个函数来识别条目何时到达页面的垂直端时,就会发生这种情况

水平+垂直断裂的结果:

下面是填充页面的函数的代码

if (!mPagesFilled)
{
    //make sure container of pages is not empty
    if(mPages.empty())
        mPages.push_back(Page());

    for (auto it : mViewedQuest->questEntries) //main loop - iterate through entries belonging to a single, selected quest
    {
        //if there is less than 100px left, add a new Page() to a vector mPages
        if (mActiveTheme->pageLayout[mLeftRight].height - mPages.back().pageSize.y < 100)
        {
            mPages.push_back(Page());
            if (mLeftRight == 0)
                mLeftRight = 1;
            else
                mLeftRight = 0;
        }

        //create and initialize the temporary PageEntry buffer object
        PageEntry entry;
        entry.header.setFont(mActiveTheme->themeFont);
        entry.header.setPosition(mActiveTheme->pageLayout[mLeftRight].left, mActiveTheme->pageLayout[mLeftRight].top + mPages.back().pageSize.y);
        entry.header.setString(it.getEntryDate());
        entry.header.setColor(sf::Color(50, 50, 50, 255));
        entry.entryHeight = entry.header.getLocalBounds().height + 20 ; //update entry height

        //set up the body of entry
        entry.body.setFont(mActiveTheme->themeFont);
        entry.body.setColor(sf::Color::Black);
        entry.body.setPosition(mActiveTheme->pageLayout[mLeftRight].left, mActiveTheme->pageLayout[mLeftRight].top + entry.entryHeight + mPages.back().pageSize.y);

        //clear the stream and the buffer string and put the iterated entry content into the stream
        ss.clear();
        mBuffer.clear();
        ss << it.entryText;

        while (ss) //loop for as long as there is something to be read from the stream
        {
            //check if we have reached the end of stream and break the loop if we did to avoid the garbage char at the end
            mBufferChar = ss.peek(); 
            if (mBufferChar == EOF)
                continue; // i know technically it should be break; but since it's EOF than it doesn't really matter i think

            //append the buffer string with the next char from the stream and update the body
            mBuffer += ss.get();
            entry.body.setString(mBuffer);

            //check if we have hit the right side of the page and add newline if we did
            if (entry.body.getLocalBounds().width > mActiveTheme->pageLayout[mLeftRight].width)
            {
                size_t it = mBuffer.find_last_of(" "); //check where was the last space in the string
                mBuffer.insert(it, "\n"); //put a newline there
                entry.body.setString(mBuffer); //update body string

                //after putting the newline update the height by one line height (represented by header height, always one line) and update pagesize
                entry.entryHeight += entry.header.getLocalBounds().height;
                mPages.back().pageSize.y += entry.entryHeight;


                //check if entry has reached the end of page
                if (mActiveTheme->pageLayout[mLeftRight].height - mPages.back().pageSize.y < 100)
                {
                    //if yes mark entry as unfinished, push it into the page, and reset the height of the buffer entry
                    entry.isFinished = false;
                    mPages.back().page.push_back(entry);
                    entry.entryHeight = 0;

                    //push a new page and toggle the pagemark
                    mPages.push_back(Page());
                    if (mLeftRight == 0)
                        mLeftRight = 1;
                    else
                        mLeftRight = 0;

                    //move the entry to the other page, clear the string buffer and the body string
                    entry.body.setPosition(mActiveTheme->pageLayout[mLeftRight].left, mActiveTheme->pageLayout[mLeftRight].top + entry.entryHeight + mPages.back().pageSize.y);
                    mBuffer.clear();
                    entry.body.setString("");
                    entry.header.setString("");
                }

            } //once entry passes these to if statements go on with the next stream chars until once again right side of the page is reached
        }

        //all stream chars read, mark entry as finished and push it to the page
        entry.isFinished = true;
        mPages.back().page.push_back(entry);

    }
    //all entries parsed, mark filled flag true
    mPagesFilled = true;
}
else
{ }
  • ss是一条小溪
  • pageSize in mPages.back()。pageSize是一个sf::Vector2i,用于存储单个页面中包含的所有PageEntry对象的高度

  • 您是否尝试过通过调试器运行它和/或添加一些调试输出,以查看哪个值在何时何地设置错误?@Lukas很遗憾,我不知道如何使用调试器。每当我遇到问题时,我倾向于使用广泛的调试控制台输出,尽管在这个例子中,我不知道如何应用它。每次迭代后,我都会尝试打印条目大小和页面大小,看看能找到什么。不过我已经发现了一个错误——当条目的最后一行出现垂直中断时,我不会更新新页面的大小,并且下一个条目的标题与该页面重叠it@Lukas我向控制台添加了调试printfs,并注意到另一个bug mPages.back().pageSize.y+=entry.entryHeight;这导致pageSize超出了条目的总和。将其更改为mPages.back().pageSize.y+=entry.header.getLocalBounds().height;条目现在正确地到达页面的末尾(或多或少),但文本在许多地方与另一段文本重叠。很可能是关于何时以及如何将一个新页面推回到mPages向量的问题,不幸的是,我没有时间真正检查所有代码。因为对我来说,这似乎更像是一个“数学”问题,所以在纸上做一次数学,然后用代码或类似的东西检查一下可能会有帮助。
    PageEntry()
    {
    
    };
    
    sf::Text header;   //header containing the date of an Entry
    sf::Text body;     //entry text
    int entryHeight;   //stores the current height of the object 
    bool isFinished;   //indicator if an entry is split among pages or not