C ++ / SFML显示毛刺与sprite成员更改mouseclick [英] C++/SFML display glitch with sprite member changed by mouseclick

查看:418
本文介绍了C ++ / SFML显示毛刺与sprite成员更改mouseclick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习C ++ / SFML和练习练习我正在做一个小程序,在窗口中展示一个10x10网格的64x64像素的正方形棕色sprites。此程序允许您使用键盘选择绿色/黄色/蓝色/灰色/棕色方形精灵,并用所选的精灵替换所述网格上的任何瓷砖。下面的游戏循环:

I am learning C++/SFML and as a practice exercise I am making a small program that dispalys a 10x10 grid of 64x64 px square brown sprites in a window. This program allows you to select green/yellow/blue/grey/brown square sprites using the keyboard and replace any tile on said grid with this selected sprite. Game loop below:

while (window.isOpen())
{
    window.clear(sf::Color(sf::Color::Black));
    sf::Event event;
    while (window.pollEvent(event))
    {
        if (event.type == sf::Event::Closed)
            window.close();
    }

    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num1)) s_paintBrush = s_sand;
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num2)) s_paintBrush = s_grass;
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num3)) s_paintBrush = s_dirt;
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num4)) s_paintBrush = s_water;
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num5)) s_paintBrush = s_rock;

    if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
    {
        sf::Vector2i localPosition = sf::Mouse::getPosition(window);
        int i = localPosition.x / x;
        int j = localPosition.y / y;
        if (i < columns && j < rows && i >= 0 && j >=0) grid[j][i].m_terrain = s_paintBrush;;
    }

    for (int i = 0; i < rows; ++i)
    { 
        for (int j = 0; j < columns; ++j)
        {           
                window.draw(grid[i][j].m_terrain);
                grid[i][j].m_terrain.setPosition(x * j, y * i);
        }
    }
    window.display();
}   

grid是类tile的2D向量,包含sf :: Sprite成员变量m_terrain,用于存储要显示在相应网格位置的精灵。整数x& y = 64并且用于确定对应于窗口上的鼠标点击位置的网格的坐标。任何具有前缀s_的是sf :: Sprite。

"grid" is a 2D vector of the class "tile" that contains the sf::Sprite member variable "m_terrain" that stores the sprite to be displayed at the corresponding grid location. Integers x & y = 64 and are used to determine the coordinates of "grid" that correspond to the mouseclick location on the window. Anything with the prefix s_ is a sf::Sprite.

这个程序运行正常,只有一个例外,当我按下鼠标左键,我想改变,上一个精灵消失,留下一个空白的方形区域,替换sprite显示在窗口的左上角,直到鼠标按钮被释放。然后,替换sprite正确显示在mouseclick位置。

This program is working fine with one exception, when I press the left mouse button on a tile I want to change, the previous sprite disappears leaving a blank square area, and the replacement sprite displays in the top left corner of the window until the mouse button is released. The replacement sprite is then correctly displayed at the mouseclick location.

对我来说,这表示新的sprite正在mouseclick上生成,但是没有收到setPosition坐标,直到鼠标按钮被释放。我不确定如何解决这个问题,并没有能够找到一个答案(这很简单,我至少理解,我只是学习!)。

To me this indicates that the new sprite is being generated on mouseclick, but not receiving the setPosition coordinates until the mouse button is released. I am unsure how to fix this, and have not been able to find an answer (that is simple enough for me to understand at least, I am only learning!).

感谢您的关注。

推荐答案

发生的原因是您在之前绘制sprite 设置位置。然后,当下一个事件发生时(按钮向上事件),精灵再次被绘制,但这次使用正确的坐标。

The reason that happens is that you draw the sprite before you set the position. Then when next event occurs (the button up event) the sprite is drawn again but this time with the correct coordinates.

切换绘制和设置位置的线将使它工作:

Switching the lines where you draw and set position will make it work:

grid[i][j].m_terrain.setPosition(x * j, y * i);
window.draw(grid[i][j].m_terrain);

这篇关于C ++ / SFML显示毛刺与sprite成员更改mouseclick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆