Reputation: 29
Probably a really simple fix, but I may as well ask anyways. I declared a variable in a header file and tried to use it in a source file, but I got an error saying it wasn't declared in the scope.
My header file:
#ifndef TILEMAP_H
#define TILEMAP_H
#include <SFML/Graphics.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include "mapbuilder.h"
class tileMap : public sf::Drawable, public sf::Transformable
{
public:
tileMap();
virtual ~tileMap();
bool load(const std::string, sf::Vector2u, const int*, unsigned int, unsigned int);
protected:
private:
sf::VertexArray m_vertices;
sf::Texture m_tileset;
};
#endif // TILEMAP_H
My source file:
#include "tilemap.h"
#include <SFML/Graphics.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include "mapbuilder.h"
tileMap::tileMap()
{
//ctor
}
tileMap::~tileMap()
{
//dtor
}
bool load(const std::string& tileset, sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height)
{
// load the tileset texture
if (!m_tileset.loadFromFile(tileset))
return false;
// resize the vertex array to fit the level size
m_vertices.setPrimitiveType(sf::Quads);
m_vertices.resize(width * height * 4);
// populate the vertex array, with one quad per tile
for (unsigned int i = 0; i < width; ++i)
for (unsigned int j = 0; j < height; ++j)
{
// get the current tile number
int tileNumber = tiles[i + j * width];
// find its position in the tileset texture
int tu = tileNumber % (m_tileset.getSize().x / tileSize.x);
int tv = tileNumber / (m_tileset.getSize().x / tileSize.x);
// get a pointer to the current tile's quad
sf::Vertex* quad = &m_vertices[(i + j * width) * 4];
// define its 4 corners
quad[0].position = sf::Vector2f(i * tileSize.x, j * tileSize.y);
quad[1].position = sf::Vector2f((i + 1) * tileSize.x, j * tileSize.y);
quad[2].position = sf::Vector2f((i + 1) * tileSize.x, (j + 1) * tileSize.y);
quad[3].position = sf::Vector2f(i * tileSize.x, (j + 1) * tileSize.y);
// define its 4 texture coordinates
quad[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y);
quad[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);
quad[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);
quad[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);
}
return true;
}
Specifically, m_tileset and m_vertices don't seem to be declared in the load function, even though I defined them in the header file.
By the way, I'm new to C++, so please be gentle.
Upvotes: 0
Views: 42
Reputation: 216
Your load function is outside the proper namespace. Add "tileMap::" before load().
bool tileMap::load([params])
Upvotes: 1