I created a std::shared_ptr for the texture in the spritesheet but it will still just show a white screen. Here's my map and spritesheet files:
Map.hpp:
#pragma once
#include <string>
#include <vector>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include "Spritesheet.hpp"
namespace dte
{
class Map
{
public:
//Constructors and destructor
Map();
Map(std::string spritesheet_filename, std::string map_filename, sf::Vector2u spritesheet_dims, sf::Vector2u sprite_dims, sf::Vector2u map_dimensions);
~Map();
//Getters
Spritesheet GetSpritesheet();
sf::Vector2u GetDimensions();
int GetTile(int position);
//Setters
void SetSpritesheet(Spritesheet& sheet);
private:
//The dimensions of the map
sf::Vector2u m_dimensions;
//The spritesheet for the map
Spritesheet m_spritesheet;
public:
//The map data
std::vector<int> map_array;
};
}
Map.cpp:
#include <fstream>
#include <iostream>
#include <cstdlib>
#include "Map.hpp"
namespace dte
{
//Constructors and destructor
Map::Map() = default;
Map::Map(std::string spritesheet_filename, std::string map_filename, sf::Vector2u spritesheet_dims, sf::Vector2u sprite_dims, sf::Vector2u map_dimensions)
{
m_dimensions = map_dimensions;
m_spritesheet = dte::Spritesheet(spritesheet_filename, spritesheet_dims, sprite_dims);
map_array.resize(m_dimensions.y * m_dimensions.x, 0);
std::ifstream map_file(map_filename);
if(map_file.is_open())
{
for(unsigned int i = 0; i < m_dimensions.x; i++)
{
for(unsigned int j = 0; j < m_dimensions.y; j++)
{
int current_map_piece;
map_file >> current_map_piece;
map_array.at(j * m_dimensions.x + i) = current_map_piece;
}
}
}
else
{
std::cerr << "Could not load map: " << map_filename << std::endl;
exit(EXIT_FAILURE);
}
}
Map::~Map() = default;
//Getters
Spritesheet Map::GetSpritesheet() { return m_spritesheet; }
sf::Vector2u Map::GetDimensions() { return m_dimensions; }
int Map::GetTile(int position) { return map_array.at(position); }
//Setters
void Map::SetSpritesheet(Spritesheet& sheet) { m_spritesheet = sheet; }
}
Spritesheet.hpp:
#pragma once
#include <memory>
#include <vector>
#include <SFML/Graphics.hpp>
namespace dte
{
class Spritesheet
{
public:
//Constructors and destructor
Spritesheet();
Spritesheet(const Spritesheet& source);
Spritesheet(std::string filename, sf::Vector2u image_dims, sf::Vector2u sprite_dims);
~Spritesheet();
//Getters
unsigned int GetSpriteWidth();
unsigned int GetSpriteHeight();
sf::Sprite GetSprite(int sprite_number);
//Overloaded operators
Spritesheet& operator= (const Spritesheet& source);
private:
//Container of all the coordinates of the images
std::vector<sf::Sprite> m_sprites;
//Image dimensions
sf::Vector2u m_texture_dimensions;
//Sprite dimensions
sf::Vector2u m_sprite_dimensions;
//The image of the spritesheet
std::shared_ptr<sf::Texture> m_texture;
//Number of sprites in the image
int m_sprite_count, m_sprites_per_row, m_sprites_per_column;
//The name of the spritesheet for the file
std::string m_filename;
};
}
Spritesheet.cpp:
#include <iostream>
#include <cstdlib>
#include "Spritesheet.hpp"
namespace dte
{
//Spritesheet
//Constructors and destructor
Spritesheet::Spritesheet()
{
//Initalize all sizes to 0
m_texture_dimensions = sf::Vector2u(0, 0); m_sprite_dimensions = sf::Vector2u(0, 0);
m_sprite_count = 0; m_sprites_per_row = 0; m_sprites_per_column = 0;
//The texture and vector don't contain anything
//Set the filename to nothing
m_filename = "";
}
Spritesheet::Spritesheet(const Spritesheet& source)
{
m_texture = source.m_texture;
m_sprites = source.m_sprites;
m_texture_dimensions = source.m_texture_dimensions; m_sprite_dimensions = source.m_sprite_dimensions;
m_sprites_per_row = source.m_sprites_per_row; m_sprites_per_column = source.m_sprites_per_column; m_sprite_count = source.m_sprite_count;
m_filename = source.m_filename;
}
Spritesheet::Spritesheet(std::string filename, sf::Vector2u image_dims, sf::Vector2u sprite_dims)
{
//Initalize the sizes
m_texture_dimensions = image_dims; m_sprite_dimensions = sprite_dims;
m_sprites_per_row = m_texture_dimensions.y/m_sprite_dimensions.y; m_sprites_per_column = m_texture_dimensions.x/m_sprite_dimensions.y; m_sprite_count = m_sprites_per_row*m_sprites_per_column;
//Set the filename
m_filename = filename;
//Initialize the texture
sf::Texture shared_texture;
if(!shared_texture.loadFromFile(m_filename, sf::IntRect(0, 0, m_texture_dimensions.y, m_texture_dimensions.x)))
{
std::cerr << "Could not load texture: " << m_filename << std::endl;
exit(EXIT_FAILURE);
}
m_texture = std::make_shared<sf::Texture>(shared_texture);
//Initialize the vector so that sprites can be accessed
m_sprites.reserve(m_sprites_per_row*m_sprites_per_column);
if(m_sprites_per_column == 1)
{
for(int i = 0; i < m_sprites_per_row; i++)
{
m_sprites.push_back(sf::Sprite(*m_texture, sf::IntRect(i*m_sprite_dimensions.x, 0, m_sprite_dimensions.x, m_sprite_dimensions.y)));
}
}
else if(m_sprites_per_row == 1)
{
for(int i = 0; i < m_sprites_per_column; i++)
{
m_sprites.push_back(sf::Sprite(*m_texture, sf::IntRect(0, i*m_sprite_dimensions.y, m_sprite_dimensions.x, m_sprite_dimensions.y)));
}
}
else
{
for(int i = 0; i < m_sprites_per_column; i++)
{
for(int j = 0; j < m_sprites_per_row; j++)
{
m_sprites.push_back(sf::Sprite(*m_texture, sf::IntRect(j*m_sprite_dimensions.x, i*m_sprite_dimensions.y, m_sprite_dimensions.x, m_sprite_dimensions.y)));
}
}
}
}
Spritesheet::~Spritesheet() = default;
//Getters
unsigned int Spritesheet::GetSpriteWidth() { return m_sprite_dimensions.x; }
unsigned int Spritesheet::GetSpriteHeight() { return m_sprite_dimensions.y; }
sf::Sprite Spritesheet::GetSprite(int sprite_number) { return m_sprites.at(sprite_number); }
//Overloaded operators
Spritesheet& Spritesheet::operator= (const Spritesheet& source)
{
m_texture = source.m_texture;
m_sprites = source.m_sprites;
m_texture_dimensions = source.m_texture_dimensions; m_sprite_dimensions = source.m_sprite_dimensions;
m_sprites_per_row = source.m_sprites_per_row; m_sprites_per_column = source.m_sprites_per_column; m_sprite_count = source.m_sprite_count;
m_filename = source.m_filename;
return *this;
}
}
And here's how I'm creating the instance of my map class:
terrain = dte::Map("resources/Images/Terrain.png", "resources/Maps/World1.map", sf::Vector2u(512, 512), sf::Vector2u(32, 32), sf::Vector2u(window_ptr->getSize().x/32, window->getSize().y/32));