-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
159 additions
and
9 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#include "tilemap.hpp" | ||
|
||
|
||
#pragma region Tile Code | ||
Tile::Tile() { | ||
// Empty for now | ||
} | ||
|
||
Tile::Tile( | ||
sf::Texture *tileset, | ||
sf::Vector2u tileSize, | ||
sf::Vector2u tilePos, | ||
sf::Vector2u spritePos | ||
) { | ||
this->shape = sf::RectangleShape(sf::Vector2f(tileSize.x, tileSize.y)); | ||
this->shape.setTexture(tileset); | ||
this->shape.setTextureRect(sf::IntRect(spritePos.x, spritePos.y, tileSize.x, tileSize.y)); | ||
this->shape.setPosition(sf::Vector2f(tilePos.x, tilePos.y)); | ||
} | ||
|
||
Tile::~Tile() { | ||
|
||
} | ||
|
||
void Tile::update() { | ||
// Empty for now but we'll do some checking eventually | ||
} | ||
|
||
void Tile::render(sf::RenderTarget *target) { | ||
// Render the tile | ||
target->draw(this->shape); | ||
} | ||
#pragma endregion | ||
|
||
|
||
|
||
#pragma region Tilemap Code | ||
|
||
Tilemap::Tilemap() { | ||
this->initVariables(); | ||
this->initTileset(); | ||
} | ||
|
||
|
||
Tilemap::~Tilemap() { | ||
|
||
} | ||
|
||
void Tilemap::initVariables() { | ||
// Store how large the map is along with the size of each tile | ||
this->tileSize = sf::Vector2u(16, 16); | ||
this->mapSize = sf::Vector2u(10, 10); | ||
|
||
// Load the tileset | ||
this->tileset.loadFromFile("assets/tilemap.png"); | ||
} | ||
|
||
void Tilemap::initTileset() { | ||
// Set up the tilemap | ||
for (int x = 0; x < this->mapSize.x; x++) { | ||
for (int y = 0; y < this->mapSize.y; y++) { | ||
this->addTile(x, y); | ||
} | ||
} | ||
} | ||
|
||
void Tilemap::addTile(int x, int y) { | ||
std::string tileName = "tile_" + std::to_string(x) + "_" + std::to_string(y); | ||
sf::Vector2u tilePos(x * this->tileSize.x, y * this->tileSize.y); | ||
sf::Vector2u spritePos(0, 0); | ||
this->tileMap[tileName] = Tile(&this->tileset, this->tileSize, tilePos, spritePos); | ||
} | ||
|
||
void Tilemap::update() { | ||
// Empty for now but we'll do some checking eventually | ||
} | ||
|
||
void Tilemap::render(sf::RenderTarget *target) { | ||
// Render the tilemap | ||
for (auto &tile : this->tileMap) { | ||
tile.second.render(target); | ||
} | ||
} | ||
|
||
Tile Tilemap::getTile(int x, int y) { | ||
std::string tileName = "tile_" + std::to_string(x) + "_" + std::to_string(y); | ||
return this->tileMap[tileName]; | ||
} | ||
|
||
#pragma endregion |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#ifndef TILEMAP_HPP | ||
#define TILEMAP_HPP | ||
|
||
#include <iostream> | ||
#include <map> | ||
#include <string> | ||
|
||
#include<SFML/Graphics.hpp> | ||
|
||
class Tile { | ||
private: | ||
// Variables | ||
sf::RectangleShape shape; | ||
sf::IntRect uvRect; | ||
sf::Texture *tileset; | ||
sf::Vector2u tileSize; | ||
sf::Vector2u tilePos; | ||
sf::Vector2u spritePos; | ||
|
||
public: | ||
Tile(); | ||
Tile(sf::Texture *tileset, sf::Vector2u tileSize, sf::Vector2u tilePos, sf::Vector2u spritePos); | ||
virtual ~Tile(); | ||
|
||
void update(); | ||
void render(sf::RenderTarget *target); | ||
}; | ||
|
||
|
||
class Tilemap { | ||
private: | ||
//Variables | ||
// Texture handling | ||
sf::Texture tileset; | ||
sf::Vector2u tileSize; | ||
sf::Vector2u mapSize; | ||
|
||
|
||
// Map handling | ||
std::map<std::string, Tile> tileMap; | ||
|
||
void initVariables(); | ||
void initTileset(); | ||
void addTile(int x, int y); | ||
public: | ||
Tilemap(); | ||
virtual ~Tilemap(); | ||
|
||
void update(); | ||
void render(sf::RenderTarget *target); | ||
Tile getTile(int x, int y); | ||
}; | ||
|
||
#endif |