Here is the core of the program. I took out a bunch of code that dealt with things like the opening logo sequence, a little character creation, all events dealing with key presses other than escape. Basically, I use the same window for everything, I'm just updating it with different things. For example, the logo sequence displays a picture, fades it to black, and then I keep that window, but display a "New Game/Load Game" option which takes mouse movement.
I'm not sure if this matters, but I changed the program from console to windows in the properties.
#include <SFML/Graphics.hpp>
#include <SFML/Window/VideoMode.hpp>
#include <sstream>
#include <string>
#include <iostream>
#include <SFML/Window/Keyboard.hpp>
//Tilemap class-----------------------------------------------------------------------------------------------------------------------------------------------
class TileMap : public sf::Drawable, public sf::Transformable
{
public:
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("images/Tilemap-2.png"))
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;
}
private:
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
{
// apply the transform
states.transform *= getTransform();
// apply the tileset texture
states.texture = &m_tileset;
// draw the vertex array
target.draw(m_vertices, states);
}
sf::VertexArray m_vertices;
sf::Texture m_tileset;
};
//Player Class----------------------------------------------------------------P-L-A-Y-E-R---C-L-A-S-S------------------------------------------------------------------------------
class Player
{
public:
bool gender;
bool step = false;
float tileX = 1;
float tileY = 1;
//Walking function--------------------------------------------------------------W-A-L-K-I-N-G---F-U-N-C-T-I-O-N----------------------------------------------------------
void walking(float& x, float& y, int dir, sf::RenderWindow& window, sf::Sprite& playerSprite, TileMap lm)
{
switch (dir)
{
case 0:
for (int n = 0; n < 16; ++n)
{
y -= .0625;
if (step == false)
{
playerSprite.setTextureRect(sf::IntRect(640, 0, 64, 128));
playerSprite.setPosition(x * 64, y * 64);
window.clear();
window.draw(lm);
window.draw(playerSprite);
window.display();
}
else{
playerSprite.setTextureRect(sf::IntRect(704, 0, 64, 128));
playerSprite.setPosition(x * 64, y * 64);
window.clear();
window.draw(lm);
window.draw(playerSprite);
window.display();
}
}
playerSprite.setTextureRect(sf::IntRect(576, 0, 64, 128));
break;
case 1:
for (int n = 0; n < 16; ++n)
{
y += .0625;
if (step == false)
{
playerSprite.setTextureRect(sf::IntRect(64, 0, 64, 128));
playerSprite.setPosition(x * 64, y * 64);
window.clear();
window.draw(lm);
window.draw(playerSprite);
window.display();
}
else{
playerSprite.setTextureRect(sf::IntRect(128, 0, 64, 128));
playerSprite.setPosition(x * 64, y * 64);
window.clear();
window.draw(lm);
window.draw(playerSprite);
window.display();
}
}
playerSprite.setTextureRect(sf::IntRect(0, 0, 64, 128));
break;
case 2:
for (int n = 0; n < 16; ++n)
{
x -= .0625;
if (step == false)
{
playerSprite.setTextureRect(sf::IntRect(256, 0, 64, 128));
playerSprite.setPosition(x * 64, y * 64);
window.clear();
window.draw(lm);
window.draw(playerSprite);
window.display();
}
else{
playerSprite.setTextureRect(sf::IntRect(320, 0, 64, 128));
playerSprite.setPosition(x * 64, y * 64);
window.clear();
window.draw(lm);
window.draw(playerSprite);
window.display();
}
}
playerSprite.setTextureRect(sf::IntRect(192, 0, 64, 128));
break;
case 3:
for (int n = 0; n < 16; ++n)
{
x += .0625;
if (step == false)
{
playerSprite.setTextureRect(sf::IntRect(448, 0, 64, 128));
playerSprite.setPosition(x * 64, y * 64);
window.clear();
window.draw(lm);
window.draw(playerSprite);
window.display();
}
else{
playerSprite.setTextureRect(sf::IntRect(512, 0, 64, 128));
playerSprite.setPosition(x * 64, y * 64);
window.clear();
window.draw(lm);
window.draw(playerSprite);
window.display();
}
}
playerSprite.setTextureRect(sf::IntRect(384, 0, 64, 128));
break;
}
if (step == true)
{
step = false;
}
else{
step = true;
}
}
};
//--------------------------------------------------------------------------M-A-I-N---------------------------------------------------------------------------------------
int main()
{
// create the window
sf::VideoMode desktop = sf::VideoMode::getDesktopMode();
float desktopWidth = desktop.width;
float desktopHeight = desktop.height;
sf::RenderWindow window(sf::VideoMode(desktopWidth, desktopHeight, desktop.bitsPerPixel), "Game", sf::Style::Fullscreen);
window.setFramerateLimit(60);
//load sprites
sf::Sprite playerSprite;
sf::Texture charMapTex;
if (!charMapTex.loadFromFile("images/character map64.png"))
return false;
playerSprite.setTexture(charMapTex);
int colTest;
playerSprite.setTextureRect(sf::IntRect(0, 0, 64, 128));
playerSprite.setPosition(p.tileX * 64, p.tileY * 64);
const int level[] =
{
2, 0, 0, 0, 0, 0, 0, 0, 0, 3,
2, 1, 1, 1, 1, 1, 1, 1, 1, 3,
2, 1, 1, 1, 1, 1, 1, 1, 1, 3,
2, 1, 1, 1, 1, 1, 1, 1, 1, 3,
2, 1, 1, 1, 1, 1, 1, 1, 1, 3,
2, 1, 1, 1, 1, 1, 1, 1, 1, 3,
2, 1, 1, 1, 1, 1, 1, 1, 1, 3,
2, 1, 1, 1, 1, 1, 1, 1, 1, 3,
2, 1, 1, 1, 1, 1, 1, 1, 1, 3,
7, 4, 4, 6, 1, 5, 4, 4, 4, 8,
};
// create the tilemap from the level definition
TileMap map;
if (!map.load("images/Tilemap-2.png", sf::Vector2u(64, 64), level, 10, 10))
return -1;
// run the main loop
while (window.isOpen())
{
// handle events
sf::Event event;
while (window.pollEvent(event))
{
if (event.key.code == sf::Keyboard::Escape)
{
window.close();
break;
}
}
// draw the map
window.clear();
window.draw(map);
window.draw(playerSprite);
window.display();
}
return EXIT_SUCCESS;
}