1
General / Re: Issue with rendering textures to the screen
« on: December 01, 2018, 09:59:39 pm »
My tilemap only changes when a player places a new tile, which is not happening here, so in this instance, the tilemap is static. None of the tiles are animating or anything either. Here is the code that handles the rendering of the tiles.
BTW tileSize, xOffset, and yOffset are variables declared elsewhere. Tilesize is simply the size of a tile, and x and y offset is the offset from the camera
*EDIT* sorry that the code looks bad in the window. I think it messed up my formatting. Here is a better view of it https://pastebin.com/LNgzCb61
sf::Vector2f v = handler->window->getView().getSize();
//std::cout << v.x << " " << v.y << std::endl;
std::vector<std::vector<sf::RectangleShape>> renderOrder(256,
std::vector<sf::RectangleShape>(0));
for (int x = 0; x < world->getWidth(); x++) {
for (int y = 0; y < world->getHeight(); y++) {
if (!(x * tileSize + tileSize < xOffset + (handler->window->getSize().x / 2 - v.x / 2) ||
x * tileSize > handler->window->getSize().x + xOffset - (handler->window->getSize().x / 2 - v.x / 2) ||
y * tileSize + tileSize < yOffset + (handler->window->getSize().y / 2 - v.y / 2) ||
y * tileSize > handler->window->getSize().y + yOffset - (handler->window->getSize().y / 2 - v.y / 2))) {
sf::Uint8 tileId = world->getTile(x, y);
sf::RectangleShape tile(sf::Vector2f(tileSize, tileSize));
tile.setPosition((x * tileSize - xOffset), (y * tileSize - yOffset));
sf::Uint8 data = 0;
tile.setTexture(handler->assets->getBaseTexture(tileId));
renderOrder[0].push_back(tile); // We always want the base tiles to be drawn first
TileData currentTileData = world->getTileData(x, y);
std::map<sf::Uint8, sf::Uint8> adjInfo = currentTileData.getAdjacentInfo();
for (std::map<sf::Uint8, sf::Uint8>::iterator it = adjInfo.begin(); it != adjInfo.end(); it++) {
sf::Uint8 key = it->first;
sf::Uint8 value = it->second;
sf::Texture* newT = handler->assets->getOuterTexture(key, value, x, y);
if (newT == nullptr) continue;
tile.setTexture(newT);
renderOrder[handler->assets->getTilePriority(key)].push_back(tile);
}
}
}
}
for (int i = 0; i < renderOrder.size(); i++) {
for (int j = 0; j < renderOrder[i].size(); j++) {
handler->window->draw(renderOrder[i][j]);
}
}
//std::cout << v.x << " " << v.y << std::endl;
std::vector<std::vector<sf::RectangleShape>> renderOrder(256,
std::vector<sf::RectangleShape>(0));
for (int x = 0; x < world->getWidth(); x++) {
for (int y = 0; y < world->getHeight(); y++) {
if (!(x * tileSize + tileSize < xOffset + (handler->window->getSize().x / 2 - v.x / 2) ||
x * tileSize > handler->window->getSize().x + xOffset - (handler->window->getSize().x / 2 - v.x / 2) ||
y * tileSize + tileSize < yOffset + (handler->window->getSize().y / 2 - v.y / 2) ||
y * tileSize > handler->window->getSize().y + yOffset - (handler->window->getSize().y / 2 - v.y / 2))) {
sf::Uint8 tileId = world->getTile(x, y);
sf::RectangleShape tile(sf::Vector2f(tileSize, tileSize));
tile.setPosition((x * tileSize - xOffset), (y * tileSize - yOffset));
sf::Uint8 data = 0;
tile.setTexture(handler->assets->getBaseTexture(tileId));
renderOrder[0].push_back(tile); // We always want the base tiles to be drawn first
TileData currentTileData = world->getTileData(x, y);
std::map<sf::Uint8, sf::Uint8> adjInfo = currentTileData.getAdjacentInfo();
for (std::map<sf::Uint8, sf::Uint8>::iterator it = adjInfo.begin(); it != adjInfo.end(); it++) {
sf::Uint8 key = it->first;
sf::Uint8 value = it->second;
sf::Texture* newT = handler->assets->getOuterTexture(key, value, x, y);
if (newT == nullptr) continue;
tile.setTexture(newT);
renderOrder[handler->assets->getTilePriority(key)].push_back(tile);
}
}
}
}
for (int i = 0; i < renderOrder.size(); i++) {
for (int j = 0; j < renderOrder[i].size(); j++) {
handler->window->draw(renderOrder[i][j]);
}
}
BTW tileSize, xOffset, and yOffset are variables declared elsewhere. Tilesize is simply the size of a tile, and x and y offset is the offset from the camera
*EDIT* sorry that the code looks bad in the window. I think it messed up my formatting. Here is a better view of it https://pastebin.com/LNgzCb61