Skip to content

Commit

Permalink
Basic Tilemap Rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
eef-g committed Oct 24, 2023
1 parent 34a3cdd commit ce8df07
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 9 deletions.
14 changes: 9 additions & 5 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ Game::~Game() {
delete this->window;
}


/*
Public Functions
*/

/// @brief Returns whether the game is running or not
const bool Game::running() const { return this->window->isOpen(); }

/// @brief Runs the game loop
void Game::run() {
sf::Clock clock;
float deltaTime = 0.f;
Expand All @@ -34,7 +36,6 @@ void Game::run() {
}
}


/// @brief Polls events from the window
void Game::pollEvents() {
// Event Polling
Expand All @@ -48,6 +49,8 @@ void Game::pollEvents() {
this->window->close();
}
break;
default:
break;
}
}
}
Expand All @@ -62,26 +65,27 @@ void Game::update(float frameTime) {
void Game::render() {
// Clear the window
this->window->clear(sf::Color(135, 207, 94, 255));
// Render items
// Render items -- Make sure that the bototm-most layer is rendered first
this->tilemap.render(this->window);
this->player.render(this->window);
// Display items
this->window->display();
}


/*
Private Functions
*/


/// @brief Initializes the game's variables
void Game::initVariables() {
this->window = nullptr;
}

/// @brief Initializes the game's window
void Game::initWindow() {
this->videoMode.height = 720;
this->videoMode.width = 1280;
this->videoMode.height = 480;
this->videoMode.width = 854;

window = new sf::RenderWindow(this->videoMode, "The Isle", sf::Style::Titlebar | sf::Style::Close | sf::Style::Resize);
}
2 changes: 2 additions & 0 deletions src/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <SFML/Audio.hpp>
#include <SFML/Network.hpp>
#include "player.hpp"
#include "tilemap.hpp"

// Pro-tip: Press 'CRTL+.' to automatically create the function definition in the cpp file
class Game {
Expand All @@ -22,6 +23,7 @@ class Game {
sf::Event ev;
sf::VideoMode videoMode;
Player player = Player();
Tilemap tilemap = Tilemap();

// Functions
void initVariables();
Expand Down
6 changes: 3 additions & 3 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const sf::Vector2f& Player::getPos() const { return this->shape.getPosition(); }
velocity.x -= 1.f;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
velocity.y += 1.f;
velocity.y +=1.f;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
this->shape.setScale(1.f, 1.f);
Expand All @@ -79,8 +79,8 @@ const sf::Vector2f& Player::getPos() const { return this->shape.getPosition(); }
this->swapAnimation(IDLE);
}
} else {
if (this->currentAnimation != WALK) {
this->swapAnimation(WALK);
if (this->currentAnimation != RUNNING) {
this->swapAnimation(RUNNING);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/player.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef PLAYER_HPP
#define PLAYER_HPP

#include <string>
// #include <string>
#include <iostream>
#include <cmath>

Expand Down
90 changes: 90 additions & 0 deletions src/tilemap.cpp
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
54 changes: 54 additions & 0 deletions src/tilemap.hpp
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

0 comments on commit ce8df07

Please sign in to comment.