【问题标题】:Printing a 2d array in sfml from a text file从文本文件中打印 sfml 中的二维数组
【发布时间】:2018-01-18 18:22:56
【问题描述】:

我正在尝试使用 window.draw() 函数将来自文本文件的二维字符数组打印到渲染窗口中。然而,一切似乎都工作得很好,除了在打印它时恰好跳过一个字符。 例如,如果我有一行 15 个字符,它只会打印 8 个,但是当我在终端上打印它时,它打印得非常好,我根本不知道它为什么会这样,尝试了几件事,比如更改文本文件本身,或更改大小。似乎没有什么对我有用。非常感谢任何想法或帮助。

这里是部分代码:

void Level::printgrid(int level)
{
sf::RenderWindow window(sf::VideoMode(800, 600), "Game On");
sf::Font MyFont;

if (!MyFont.loadFromFile("tnr.ttf"))
{
    // Error...
}

ifstream my_file("nn.txt"); //text that i will be reading from 
char number;   

int i = 0; //to help loop
int j = 0;

while (window.isOpen())
{
    sf::Event event;
    while (window.pollEvent(event))
    {
        if (event.type == sf::Event::Closed)
            window.close();
        else
        {
            window.clear();
            while (i<row) // ive tried 2 for loops that didnt work cause
            {             // it kept re-entering those loops 
                while (j<col)
                {
                    my_file.get(grid[i][j]); //getting the chars one by one
                    cout << grid[i][j];  // printing them on terminal 
                                         //so i can check its correct
                    //changing to text that is drawable to print in window  
                    sf::Text text(grid[i][j], MyFont, 30);
                    text.setPosition(0 + (30 * i), 0 + (30 * j));
                      // drawing it in window
                    window.draw(text);
                    window.display();
                    j++;
                }
                my_file.get(number);
                cout << endl;
                i++;
                j = 0;
            }
        }
    }

  }
 }

【问题讨论】:

    标签: c++ visual-studio sfml


    【解决方案1】:

    您的代码存在一些问题,但最明显的问题是您总是只绘制一个字符,然后呈现结果(这将翻转缓冲区)。

    因此,您基本上是在两个缓冲区之间交替字符(因为 SFML 是双缓冲)。

    此外,您不断地从文件中读取,重新创建字符/文本元素等,这不是很有效,一旦到达文件末尾就会失败。

    对于一些实际的游戏或程序,您应该优化绘图(一次绘制 10 个字符,因为一个 sf::Text 比绘制 10 个单独的 sf::Text 对象要快)。为了简单起见,我暂时忽略它。

    以下只是一个快速(未经测试)的示例,向您展示基本思想。

    // First I'd use some container to store our `sf::Text` objects
    // This will just create all the objects
    std::vector<sf::Text> chars(rows * cols);
    
    // Now let's read the characters and add to our vector
    for (std::size_t y = 0; y < rows; ++y) {
        for (std::size_t x = 0; x < cols; ++x) {
            // Just an alias to simplify the following lines
            sf::Text &text = chars[y * cols + x];
    
            // Now just set up everything
            text.setFont(my_font);
            text.setFillColor(sf::Color::White);
            text.setCharacterSize(30);
            text.setPosition(30 * x, 30 * y);
            text.setString(my_file.get());
        }
    }
    
    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            // Just your usual event handling
        }
    
        window.clear();
        // Iterate over all elements and draw them
        for (const auto &text : chars)
            window.draw(text);
        window.display();
    }
    

    如果您尝试创建某种图形控制台,您应该将所有字符放入一些 sf::VertexArray 中,或者可能使用着色器来渲染它们。

    【讨论】:

    • 您好,谢谢您的回复。我意识到你是对的,SFML 是双缓冲。我试图逐行绘制我的二维数组,但仍然无法工作。我也试过你的建议,但 text.string 函数不起作用。我什至尝试过 sf::VertexArray 但无法让它工作。被困在这几个小时,无法启动我的应用程序。是否有不同的方法可以从给定的文本文件中打印出来?谢谢。
    • @Newdragon 定义“不工作”。还可以尝试一种更简单的方法,渲染静态和硬编码文本,然后替换它。还可以尝试调试/单步调试您的代码,以确保变量获得正确的内容。
    猜你喜欢
    • 2021-04-17
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    • 2014-04-01
    相关资源
    最近更新 更多