Created
April 13, 2016 11:44
-
-
Save awsumpwner27/5d091141de2171f546b71265792e88aa to your computer and use it in GitHub Desktop.
2015 version of my SFML tilemap implementation.
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 <stdint.h> | |
#include "Tilemap.hpp" | |
Tilemap::Tilemap(const sf::Vector2u& map_dimensions, const sf::Vector2u& tile_dimensions, sf::Texture* texture, uint8_t* tile_data_pointer) : | |
_vertices(sf::Triangles, map_dimensions.x * map_dimensions.y * 6u), | |
_map_dimensions(map_dimensions), | |
_tile_dimensions(tile_dimensions), | |
_texture(texture), | |
_tile_data_pointer(tile_data_pointer) | |
{ | |
this->buildTilemap(); | |
} | |
void Tilemap::setMapDimensions(const sf::Vector2u& map_dimensions) { | |
this->_map_dimensions = map_dimensions; | |
this->_vertices.resize(this->_map_dimensions.x * this->_map_dimensions.y * 6u); | |
} | |
void Tilemap::setMapDimensions(const unsigned int& x, const unsigned int& y) { | |
this->_map_dimensions = sf::Vector2u(x, y); | |
} | |
void Tilemap::setTileDimensions(const sf::Vector2u& tile_dimensions) { | |
this->_tile_dimensions = tile_dimensions; | |
} | |
void Tilemap::setTileDimensions(const unsigned int& x, const unsigned int& y) { | |
this->_tile_dimensions = sf::Vector2u(x, y); | |
} | |
void Tilemap::setTexture(sf::Texture* texture) { | |
this->_texture = texture; | |
} | |
void Tilemap::buildTilemap(void) { | |
const uint8_t vertex_offsets[6][2] = { | |
{0, 0}, | |
{1, 0}, | |
{1, 1}, | |
{1, 1}, | |
{0, 1}, | |
{0, 0} | |
}; | |
unsigned int sheet_width = this->_texture->getSize().x / this->_tile_dimensions.x; | |
for (int y = 0; y < this->_map_dimensions.y; ++y) { | |
for (int x = 0; x < this->_map_dimensions.x; ++x) { | |
for (int index = 0; index < 6; ++index) { | |
unsigned int sheet_index = *( | |
this->_tile_data_pointer + | |
x + | |
y * this->_map_dimensions.x | |
); | |
sf::Vector2u sheet_offset( | |
sheet_index % sheet_width, | |
sheet_index / sheet_width | |
); | |
this->_vertices[ | |
6 * ( | |
x + | |
y * this->_map_dimensions.x | |
) + | |
index | |
] = | |
sf::Vertex( | |
sf::Vector2f( | |
this->_tile_dimensions.x * (x + vertex_offsets[index][0]), | |
this->_tile_dimensions.y * (y + vertex_offsets[index][1]) | |
), | |
sf::Vector2f( | |
this->_tile_dimensions.x * (sheet_offset.x + vertex_offsets[index][0]), | |
this->_tile_dimensions.y * (sheet_offset.y + vertex_offsets[index][1]) | |
) | |
); | |
} | |
} | |
} | |
} |
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
#pragma once | |
#include <stdint.h> | |
#include <SFML/Graphics.hpp> | |
class Tilemap : public sf::Drawable, public sf::Transformable { | |
public: | |
Tilemap(const sf::Vector2u& map_dimensions, const sf::Vector2u& tile_dimensions, sf::Texture* texture, uint8_t* tile_data_pointer); | |
void setMapDimensions(const sf::Vector2u&); | |
void setMapDimensions(const unsigned int& x, const unsigned int& y); | |
void setTileDimensions(const sf::Vector2u&); | |
void setTileDimensions(const unsigned int& x, const unsigned int& y); | |
void setTexture(sf::Texture*); | |
void buildTilemap(void); | |
private: | |
virtual void Tilemap::draw(sf::RenderTarget& render_target, sf::RenderStates render_states) const { | |
render_states.texture = this->_texture; | |
render_states.transform *= this->getTransform(); | |
render_target.draw(this->_vertices, render_states); | |
} | |
sf::VertexArray _vertices; | |
sf::Vector2u _map_dimensions; | |
sf::Vector2u _tile_dimensions; | |
sf::Texture* _texture; | |
uint8_t* _tile_data_pointer; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment