Initial attempts

This commit is contained in:
Maximilian Giller 2023-06-11 13:24:27 +02:00
parent b3d793cb9f
commit 7934d623da
16 changed files with 293 additions and 10 deletions

View file

@ -88,7 +88,7 @@ set(SOURCES
src/game/level/level_config.hpp src/game/level/level_config.hpp
src/game/level/level_loader.cpp src/game/level/level_loader.cpp
src/game/level/level_loader.hpp src/game/level/level_loader.hpp
src/levels.hpp) src/levels.hpp src/sprites/tiling/tilemap.cpp src/sprites/tiling/tilemap.hpp src/sprites/tiling/tilemap_config.hpp src/tilemaps.hpp src/sprites/tiling/tileset_config.hpp src/sprites/tiling/tileset.cpp src/sprites/tiling/tileset.hpp)
set(PHYSICS_00_SOURCES set(PHYSICS_00_SOURCES
src/prototypes/physics_00.cpp) src/prototypes/physics_00.cpp)

BIN
assets/grass_plus.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

View file

@ -5,17 +5,22 @@
#include <SFML/System/Vector2.hpp> #include <SFML/System/Vector2.hpp>
#include <utility> #include <utility>
#include <vector> #include <vector>
#include "../../sprites/tiling/tilemap_config.hpp"
struct LevelConfig struct LevelConfig
{ {
std::string name; std::string name;
sf::Vector2f worldMapSize = {}; sf::Vector2f worldMapSize = {};
TileMapConfig tileMapConfig = {};
std::vector<sf::Vector2f> playerSpawnPoints = {}; std::vector<sf::Vector2f> playerSpawnPoints = {};
LevelConfig(std::string name, const sf::Vector2f &worldMapSize, LevelConfig(std::string name, const std::string &tileMapName,
const std::vector<sf::Vector2f> &playerSpawnPoints) const std::vector<sf::Vector2f> &playerSpawnPoints)
: name(std::move(name)), worldMapSize(worldMapSize), playerSpawnPoints(playerSpawnPoints) : name(std::move(name)), playerSpawnPoints(playerSpawnPoints)
{ } {
tileMapConfig = TileMapConfig(tileMapName, {});
worldMapSize = tileMapConfig.getSize();
}
LevelConfig() = default; LevelConfig() = default;

View file

@ -8,7 +8,7 @@
#define INITIAL_LEVEL "default" #define INITIAL_LEVEL "default"
std::map<std::string, LevelConfig> const LEVELS = { std::map<std::string, LevelConfig> const LEVELS = {
{"default", LevelConfig("Default", {25, 25}, {{1, 1}})} {"default", LevelConfig("Default", "grass", {{1, 1}})}
}; };
#endif //HOLESOME_LEVELS_HPP #endif //HOLESOME_LEVELS_HPP

View file

@ -95,3 +95,53 @@ std::shared_ptr<SpriteSheet> SpriteFactory::createSheet(const std::string& name)
LOG(INFO) << "Creating sheet " << name; LOG(INFO) << "Creating sheet " << name;
return std::make_shared<SpriteSheet>(texture, config.columns, config.rows); return std::make_shared<SpriteSheet>(texture, config.columns, config.rows);
} }
std::shared_ptr<TileMap> SpriteFactory::createTileMap(const std::string &name)
{
// Get config
auto tilemap_config = all_tilemaps.find(name);
if (tilemap_config == all_tilemaps.end())
{
LOG(ERROR) << "Tilemap " << name << " not found. Could not create tilemap.";
return nullptr;
}
// Construct tilemap
auto config = tilemap_config->second;
std::shared_ptr<TileSet> tileSet = createTileSet(config.tileSheet);
if (sheet == nullptr)
{
LOG(ERROR) << "Sheet " << config.sheetName << " not found. Could not create tilemap.";
return nullptr;
}
LOG(INFO) << "Creating tilemap " << name;
auto tilemap = std::make_shared<TileMap>(sheet, config.tileSize, config.tileCount);
tilemap->setTilemap(config.tilemap);
return tilemap;
}
std::shared_ptr<TileSet> SpriteFactory::createTileSet(const std::string &name)
{
// Get config
auto tileset_config = all_tilesets.find(name);
if (tileset_config == all_tilesets.end())
{
LOG(ERROR) << "Tileset " << name << " not found. Could not create tileset.";
return nullptr;
}
// Construct tileset
auto config = tileset_config->second;
auto sheet = createSheet(config.sheetName);
if (sheet == nullptr)
{
LOG(ERROR) << "Sheet " << config.sheetName << " not found. Could not create tileset.";
return nullptr;
}
LOG(INFO) << "Creating tileset " << name;
return std::make_shared<TileSet>(sheet, config.tileIndices);
}

View file

@ -8,6 +8,8 @@
#include "animated_sprite.hpp" #include "animated_sprite.hpp"
#include "sprite_sheet.hpp" #include "sprite_sheet.hpp"
#include "../logging/easylogging++.h" #include "../logging/easylogging++.h"
#include "tiling/tilemap.hpp"
#include "tiling/tileset.hpp"
class SpriteFactory class SpriteFactory
{ {
@ -15,6 +17,8 @@ public:
static std::shared_ptr<SingleSprite> createSingleSprite(const std::string& name, sf::Vector2f size = sf::Vector2f(0, 0)); static std::shared_ptr<SingleSprite> createSingleSprite(const std::string& name, sf::Vector2f size = sf::Vector2f(0, 0));
static std::shared_ptr<AnimatedSprite> createAnimatedSprite(const std::string& name, sf::Vector2f size = sf::Vector2f(0, 0)); static std::shared_ptr<AnimatedSprite> createAnimatedSprite(const std::string& name, sf::Vector2f size = sf::Vector2f(0, 0));
static std::shared_ptr<SpriteSheet> createSheet(const std::string& name); static std::shared_ptr<SpriteSheet> createSheet(const std::string& name);
static std::shared_ptr<TileMap> createTileMap(const std::string& name);
std::shared_ptr<TileSet> createTileSet(const std::string &name);
}; };

View file

@ -3,6 +3,10 @@
SpriteSheet::SpriteSheet(const std::shared_ptr<sf::Texture>& texture, int columns, int rows) SpriteSheet::SpriteSheet(const std::shared_ptr<sf::Texture>& texture, int columns, int rows)
{ {
this->texture = texture;
this->columns = columns;
this->rows = rows;
// Extract sprites from texture // Extract sprites from texture
sprites = std::vector<sf::Sprite>(); sprites = std::vector<sf::Sprite>();
int spriteWidth = texture->getSize().x / columns; int spriteWidth = texture->getSize().x / columns;
@ -47,3 +51,18 @@ std::shared_ptr<AnimatedSprite> SpriteSheet::getAnimation(int startingSequenceIn
} }
return std::make_shared<AnimatedSprite>(animation); return std::make_shared<AnimatedSprite>(animation);
} }
int SpriteSheet::getRows() const
{
return rows;
}
int SpriteSheet::getColumns() const
{
return columns;
}
std::shared_ptr<sf::Texture> SpriteSheet::getTexture() const
{
return texture;
}

View file

@ -17,7 +17,15 @@ public:
std::shared_ptr<AnimatedSprite> getAnimation(int startingSequenceIndex, int numberOfFrames) const; std::shared_ptr<AnimatedSprite> getAnimation(int startingSequenceIndex, int numberOfFrames) const;
std::shared_ptr<sf::Texture> getTexture() const;
int getColumns() const;
int getRows() const;
private: private:
int columns;
int rows;
std::shared_ptr<sf::Texture> texture;
std::vector<sf::Sprite> sprites; std::vector<sf::Sprite> sprites;
}; };

View file

@ -0,0 +1,57 @@
#include "tilemap.hpp"
TileMap::TileMap(std::shared_ptr<TileSet> tileSet, std::vector<std::vector<int>> tiles)
{
// resize the vertex array to fit the level size
vertices.setPrimitiveType(sf::Quads);
unsigned int size = tiles.size();
vertices.resize(size * size * 4);
// populate the vertex array, with one quad per tile
for (unsigned int y = 0; y < size; ++y)
for (unsigned int x = 0; x < size; ++x)
{
// get the current tile number
int tileNumber = tiles[size - 1 - y][x];
// TODO
// Follow: https://www.sfml-dev.org/tutorials/2.5/graphics-vertex-array.php#example-tile-map
// find its position in the tileset texture
int tu = tileNumber % (tileset.getSize().y / tileSize.y);
int tv = tileNumber / (tileset.getSize().y / tileSize.y);
// get a pointer to the current tile's quad
sf::Vertex *quad = &vertices[(y + x * width) * 4];
// define its 4 corners
quad[0].position = sf::Vector2f(y * tileSize.y, x * tileSize.y);
quad[1].position = sf::Vector2f((y + 1) * tileSize.y, x * tileSize.y);
quad[2].position = sf::Vector2f((y + 1) * tileSize.y, (x + 1) * tileSize.y);
quad[3].position = sf::Vector2f(y * tileSize.y, (x + 1) * tileSize.y);
// define its 4 texture coordinates
quad[0].texCoords = sf::Vector2f(tu * tileSize.y, tv * tileSize.y);
quad[1].texCoords = sf::Vector2f((tu + 1) * tileSize.y, tv * tileSize.y);
quad[2].texCoords = sf::Vector2f((tu + 1) * tileSize.y, (tv + 1) * tileSize.y);
quad[3].texCoords = sf::Vector2f(tu * tileSize.y, (tv + 1) * tileSize.y);
}
}
void TileMap::draw(sf::RenderWindow *window)
{
window->draw(*this);
}
void TileMap::draw(sf::RenderTarget &target, sf::RenderStates states) const
{
// apply the transform
// TODO? NEEDED?
// states.transform *= getTransform();
// apply the tileset texture
states.texture = &tileMapTexture;
// draw the vertex array
target.draw(vertices, states);
}

View file

@ -0,0 +1,26 @@
#ifndef HOLESOME_TILEMAP_HPP
#define HOLESOME_TILEMAP_HPP
#include <SFML/Graphics/Transformable.hpp>
#include <SFML/Graphics/VertexArray.hpp>
#include <SFML/Graphics/Texture.hpp>
#include "../../game/game_object.h"
#include "tileset.hpp"
class TileMap : public GameObject, public sf::Drawable
{
public:
TileMap(std::shared_ptr<TileSet> tileSet, std::vector<std::vector<int>> tiles);
void draw(sf::RenderWindow *window) override;
void draw(sf::RenderTarget &target, sf::RenderStates states) const override;
private:
sf::VertexArray vertices;
sf::Texture tileMapTexture;
};
#endif //HOLESOME_TILEMAP_HPP

View file

@ -0,0 +1,45 @@
#ifndef HOLESOME_TILEMAP_CONFIG_HPP
#define HOLESOME_TILEMAP_CONFIG_HPP
#include <string>
#include <utility>
#include <vector>
#include <SFML/System/Vector2.hpp>
#include <stdexcept>
struct TileMapConfig
{
std::string tileSheet;
std::vector<int> backgroundTileSelection = {};
std::vector<std::vector<int>> tiles = {};
TileMapConfig(std::string tileSheet, std::vector<std::vector<int>> tiles,
std::vector<int> backgroundTileSelection = {})
{
this->tileSheet = std::move(tileSheet);
this->backgroundTileSelection = std::move(backgroundTileSelection);
this->tiles = std::move(tiles);
// Make sure all rows are the same length and that the map is square
int size = tiles.size();
for (auto &row : tiles)
{
if (row.size() != size)
{
throw std::runtime_error("Tile map is not square");
}
}
}
TileMapConfig() = default;
sf::Vector2f getSize() const
{
int size = tiles.size();
return {static_cast<float>(size), static_cast<float>(size)};
}
};
#endif //HOLESOME_TILEMAP_CONFIG_HPP

View file

@ -0,0 +1,6 @@
#include "tileset.hpp"
TileSet::TileSet(const std::shared_ptr<SpriteSheet>& sheet, const std::vector<int>& tileIndices)
{
}

View file

@ -0,0 +1,14 @@
#ifndef HOLESOME_TILESET_HPP
#define HOLESOME_TILESET_HPP
#include "../sprite_sheet.hpp"
class TileSet
{
public:
TileSet(const std::shared_ptr<SpriteSheet>& sheet, const std::vector<int>& tileIndices);
};
#endif //HOLESOME_TILESET_HPP

View file

@ -0,0 +1,21 @@
#ifndef HOLESOME_TILESET_CONFIG_HPP
#define HOLESOME_TILESET_CONFIG_HPP
#include <string>
#include <vector>
struct TileSetConfig
{
std::string sheetName;
std::vector<int> tileIndices = {};
TileSetConfig() = default;
TileSetConfig(std::string sheetName, std::vector<int> tileIndices)
{
this->sheetName = std::move(sheetName);
this->tileIndices = std::move(tileIndices);
}
};
#endif //HOLESOME_TILESET_CONFIG_HPP

View file

@ -6,6 +6,7 @@
#include "sprites/configs/animation_config.hpp" #include "sprites/configs/animation_config.hpp"
#include "sprites/configs/sheet_config.hpp" #include "sprites/configs/sheet_config.hpp"
#include "sprites/configs/sprite_config.hpp" #include "sprites/configs/sprite_config.hpp"
#include "sprites/tiling/tileset_config.hpp"
#define PLAYER_SKIN "ring" #define PLAYER_SKIN "ring"
@ -17,7 +18,8 @@ std::map<std::string, std::string> const all_textures = {
{"numbers", "assets/numbers.png"}, {"numbers", "assets/numbers.png"},
{"64", "assets/64.png"}, {"64", "assets/64.png"},
{"edge", "assets/edge.png"}, {"edge", "assets/edge.png"},
{"ring", "assets/ring.png"} {"ring", "assets/ring.png"},
{"grasses", "assets/grass_plus.png"}
}; };
/** /**
@ -25,7 +27,8 @@ std::map<std::string, std::string> const all_textures = {
* The key is the name of the sheet, the value is the sheet config. * The key is the name of the sheet, the value is the sheet config.
*/ */
std::map<std::string, SheetConfig> const all_sheets = { std::map<std::string, SheetConfig> const all_sheets = {
{"numbers", SheetConfig("numbers", 4, 2)} {"numbers", SheetConfig("numbers", 4, 2)},
{"grasses", SheetConfig("grasses", 25, 14)}
}; };
/** /**
@ -46,4 +49,8 @@ std::map<std::string, SpriteConfig> const all_sprites = {
{"ring", SpriteConfig("ring")} {"ring", SpriteConfig("ring")}
}; };
std::map<std::string, TileSetConfig> const all_tilesets = {
{"grass", TileSetConfig("grasses", {58})}
};
#endif //HOLESOME_TEXTURE_CONFIG_H #endif //HOLESOME_TEXTURE_CONFIG_H

21
src/tilemaps.hpp Normal file
View file

@ -0,0 +1,21 @@
#ifndef HOLESOME_TILEMAPS_HPP
#define HOLESOME_TILEMAPS_HPP
#include <map>
#include <string>
#include "sprites/tiling/tilemap_config.hpp"
std::map<std::string, TileMapConfig> const all_tilemaps = {
{"grass", TileMapConfig("grass", {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},})}
};
#endif //HOLESOME_TILEMAPS_HPP