c++ - SFML 正在做一些与 vector 数组非常相关的事情

标签 c++ sfml

嘿,当我使用此代码时,它似乎将我的 SBOUND 放在同一个位置,而不是我想要的第 2 个位置的任何地方。我不知道问题出在哪里,而且对 SFML 来说大多是新手,无法完全理解正在发生的事情。主要代码是这样的。 `

#include "MainH.h"
int main()
{
int x = 200;
int y = 200;
sf::RenderWindow window(sf::VideoMode(512, 256), "Tilemap");
// create a new vertex
sf::Vertex vertex;

// set its position
vertex.position = sf::Vector2f(10, 50);

// set its color
vertex.color = sf::Color::Red;

// set its texture coordinates
vertex.texCoords = sf::Vector2f(100, 100);






////////////////////////
sf::Sprite A;
sf::Texture AT;
AT.loadFromFile("Red.png");
A.setTexture(AT);
A.setPosition(16.f, 16.f);
sf::Text A_Tex;
sf::Font A_Font;
A_Font.loadFromFile("arial.ttf");
A_Tex.setFont(A_Font);
A_Tex.setColor(sf::Color::Yellow);
A_Tex.setScale(0.35f,0.35f);
/////////////////////////////
sf::Sprite B;
sf::Texture BT;
BT.loadFromFile("Green.png");
B.setTexture(BT);
B.setPosition(30.f, 30.f);
sf::Text B_Tex;
sf::Font B_Font;
B_Font.loadFromFile("arial.ttf");
B_Tex.setFont(A_Font);
B_Tex.setColor(sf::Color::Yellow);
B_Tex.setScale(0.35f, 0.35f);

const int level[] =
{
    5, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0,
    2, 1, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3,
    2, 1, 0, 0, 2, 0, 3, 3, 3, 0, 1, 1, 1, 0, 0, 0,
    2, 1, 1, 0, 3, 3, 3, 0, 0, 0, 1, 1, 1, 2, 0, 0,
    2, 0, 1, 0, 3, 0, 2, 2, 0, 0, 1, 1, 1, 1, 2, 0,
    2, 0, 1, 0, 3, 0, 2, 2, 2, 0, 1, 1, 1, 1, 1, 1,
    2, 0, 1, 0, 3, 2, 2, 2, 0, 0, 0, 0, 1, 1, 1, 1,
};
sf::Texture COL;
COL.loadFromFile("COL.png");


TileMap map;
if (!map.load("Set.png", sf::Vector2u(16, 16), level, 16, 8, COL))
    return -1;

bool Debug = false;
//////////////////////////////
BorderLine C;
sf::Texture BordS;
sf::Texture BordT;
BordS.loadFromFile("Border.png");
BordT.loadFromFile("TopBD.png");
C.Setup(BordS, 16 ,BordT);
////////////////////////////////
window.setKeyRepeatEnabled(false);

while (window.isOpen())
{
    C.Object = A.getPosition();
    sf::Vector2f APos = A.getPosition();
    sf::Vector2f BPos = B.getPosition();

    sf::Event event;
    while (window.pollEvent(event))
    {
        if (event.type == sf::Event::Closed)
            window.close();
        if (event.type == sf::Event::Resized)
        {
            window.setView(sf::View(sf::FloatRect(0, 0, event.size.width, event.size.height)));
        }

    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && !Collision::PixelPerfectTest(C.A_B_L, B, 256))
    {
        // left key is pressed: move our character
        A.move(-.01f, 0);
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && !Collision::PixelPerfectTest(C.A_B_R, B, 256))
    {
        // left key is pressed: move our character
        for (unsigned int i = 0; i < map.SBOUND.size(); ++i)
            map.SBOUND[i].move(.01f, 0);
        for (unsigned int i = 0; i < map.TEX.size(); ++i)
            map.TEX[i].move(.01f, 0);


        A.move(.01f, 0);
    }


    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && !Collision::PixelPerfectTest(C.A_B_T, B, 256))
    {
        // left key is pressed: move our character

        for (unsigned int i = 0; i < map.SBOUND.size(); ++i)
            map.SBOUND[i].move(0, -.01f);
        for (unsigned int i = 0; i < map.TEX.size(); ++i)
            map.TEX[i].move(0, -.01f);

        A.move(0, -.01f);
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) && !Collision::PixelPerfectTest(C.A_B_B, B, 256))

    {
        for (unsigned int i = 0; i < map.SBOUND.size(); ++i)
            map.SBOUND[i].move(0, .01f);
        for (unsigned int i = 0; i < map.TEX.size(); ++i)
            map.TEX[i].move(0, .01f);

        // left key is pressed: move our character
        A.move(0, .01f);
    }
    A_Tex.setPosition(APos.x + 16, APos.y + 16);
    A_Tex.setString(to_string(APos.x) + "," + to_string(APos.y));

    B_Tex.setPosition(BPos.x + 16, BPos.y + 16);
    B_Tex.setString(to_string(BPos.x) + "," + to_string(BPos.y));
    // LEFT         window.draw(C.A_B_L);
    // RIGHT        window.draw(C.A_B_R);
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
    {
        // left key is pressed: move our character
        Debug = true;
    }
    else
    {
        Debug = false;
    }

    window.clear();
    window.draw(map);
    window.draw(A);
    window.draw(B);
    C.A();
    window.draw(C.A_B_R);
    window.draw(C.A_B_L);
    window.draw(C.A_B_T);
    window.draw(C.A_B_B);


    if (Debug) {
        window.draw(A_Tex);
        window.draw(B_Tex);
    }

    for (unsigned int i = 0; i < map.SBOUND.size(); ++i)
        window.draw(map.SBOUND[i]);
    for (unsigned int i = 0; i < map.TEX.size(); ++i)
        window.draw(map.TEX[i]);

    window.display();
}

return 0;
}`

类(class)代码如下:

#include "MainH.h"
class TileMap : public sf::Drawable, public sf::Transformable
{
public:

sf::Texture A;
std::vector<sf::Sprite> SBOUND;
std::vector<sf::Text> TEX;

sf::Font FT;
bool load(const std::string& tileset, sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height , sf::Texture& texture)
{

    FT.loadFromFile("arial.ttf");

    A = texture;
    SBOUND.resize(90);
    for (int a = 0; a < 88; a = a + 1) {
        SBOUND[a].setTexture(A);
    }
    TEX.resize(90);
    for (int a = 0; a < 88; a = a + 1) {
        TEX[a].setFont(FT);
        TEX[a].setString("HERE");
        TEX[a].setColor(sf::Color::Red);
        TEX[a].setScale(0.5f,0.5f);
    }


    // load the tileset texture
    if (!m_tileset.loadFromFile(tileset))
        return false;

    // resize the vertex array to fit the level size
    m_vertices.setPrimitiveType(sf::Quads);
    m_vertices.resize(width * height * 4);

    // populate the vertex array, with one quad per tile
    for (unsigned int i = 0; i < width; ++i)
        for (unsigned int j = 0; j < height; ++j)
        {
            // get the current tile number
            int tileNumber = tiles[i + j * width];
            // find its position in the tileset texture
            int tu = tileNumber % (m_tileset.getSize().x / tileSize.x);
            int tv = tileNumber / (m_tileset.getSize().x / tileSize.x);
            // get a pointer to the current tile's quad
            sf::Vertex* quad = &m_vertices[(i + j * width) * 4];

            // define its 4 corners
            quad[0].position = sf::Vector2f(i * tileSize.x, j * tileSize.y);
            quad[1].position = sf::Vector2f((i + 1) * tileSize.x, j * tileSize.y);
            quad[2].position = sf::Vector2f((i + 1) * tileSize.x, (j + 1) * tileSize.y);
            quad[3].position = sf::Vector2f(i * tileSize.x, (j + 1) * tileSize.y);


            if (tileNumber == 2) {
                std::cout << tileNumber << std::endl;
                SBOUND[j].setPosition(sf::Vector2f((i + 1) * tileSize.x, (j + 1) * tileSize.y));


                TEX[j].setPosition(sf::Vector2f(i * tileSize.x, j * tileSize.y));

                sf::Vector2f V;
                V = SBOUND[j].getPosition();
                std::cout << V.x << " , " << V.y << std::endl;

                std::cout << tileSize.x * i << " Tile_X " << std::endl;
                std::cout << tileSize.y * (j+1) << " Tile_Y " << std::endl;

            }

            // define its 4 texture coordinates
            quad[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y);
            quad[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);
            quad[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);
            quad[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);
        }


    return true;
}

private:

virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
{
    // apply the transform
    states.transform *= getTransform();

    // apply the tileset texture
    states.texture = &m_tileset;

    // draw the vertex array
    target.draw(m_vertices, states);
}

sf::VertexArray m_vertices;
sf::Texture m_tileset;
};

The output

图 block 索引应该在每个 Id 为 2 的图 block 之上。它只覆盖部分而不是全部。

最佳答案

已解决:我只是得到了错误的 Sprite 点数

关于c++ - SFML 正在做一些与 vector 数组非常相关的事情,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44318988/

相关文章:

c++ - 在 C++ 中读取 kinect 深度文件

c++ QVector/Vector 问题...const...丢弃限定符

c++ - 防止显示器开机

c++ - 获取 SFML 窗口的 HWND 和 HInstance?

c++ - 生成多个相同的 Sprite 被破坏

c++ - 如何对 <class*> 类型的 Vector 进行排序?

c++ - 错误 : expected constructor, 析构函数,或 ‘&’ token 之前的类型转换

c++ - Xcode 上的 OpenGL 版本 - glfw 与 sfml

C++程序问题与图片

c++ - SFML(32 位 VS12)-SFML.exe : 0xC0000005: Access violation reading location 0x0526. LoadFromFile 中 0x701ADEF8 (msvcr110.dll) 处未处理的异常