-
-
Save jbax86/94ee0b326a30f4dd80efbb33791f6728 to your computer and use it in GitHub Desktop.
carp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <SFML/Graphics.hpp> | |
class TileMap : public sf::Drawable, public sf::Transformable | |
{ | |
public: | |
bool load(const std::string& tileset, sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height); | |
private: | |
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const; | |
sf::VertexArray m_vertices; | |
sf::Texture m_tileset; | |
}; | |
#include "TileMap.h" | |
bool TileMap::load(const std::string& tileset, sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height) | |
{ | |
if (!m_tileset.loadFromFile(tileset)) | |
return false; | |
m_vertices.setPrimitiveType(sf::Quads); | |
m_vertices.resize(width * height * 4); | |
for (unsigned int i = 0; i < width; ++i) | |
for (unsigned int j = 0; j < height; ++j) | |
{ | |
int tileNumber = tiles[i + j * width]; | |
int tu = tileNumber % (m_tileset.getSize().x / tileSize.x); | |
int tv = tileNumber / (m_tileset.getSize().x / tileSize.x); | |
sf::Vertex* quad = &m_vertices[(i + j * width) * 4]; | |
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); | |
quad[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y); | |
quad[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y); | |
quad[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y); | |
quad[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y); | |
} | |
return true; | |
} | |
void TileMap::draw(sf::RenderTarget& target, sf::RenderStates states) const | |
{ | |
states.transform *= getTransform(); | |
states.texture = &m_tileset; | |
target.draw(m_vertices, states); | |
} | |
int level[] = | |
{ | |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 | |
}; | |
int main(void) | |
{ | |
//level[49] = { 2 }; | |
//level[9] = { 2 }; | |
// create the window | |
sf::RenderWindow window(sf::VideoMode(800, 600), "Tilemap"); | |
TileMap map; | |
map.load("tileset.png", sf::Vector2u(32, 32), level, 25, 15); | |
while (window.isOpen()) | |
{ | |
sf::Event event; | |
while (window.pollEvent(event)) | |
{ | |
if (event.type == sf::Event::Closed) window.close(); | |
} | |
window.clear(); | |
window.draw(map); | |
window.display(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment