嘿,当我使用这个代码时,它似乎把我的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;
}`
类代码是休闲的:
#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;
};
tile索引应该位于Id 2的每个tile的顶部。它只覆盖一些而不是全部。
答案 0 :(得分:0)
解决:我只是为精灵获得错误的积分