Hi there,
I use SFML 2.2. MS Visual Studio Express 2013. I'm trying to create a menu for my game.
The problem is when I try to create a text in another class, I'm getting an error that says "Vector erase iterator outside range" and game doesnt want to start... If I use text in the main.cpp everything is fine...
menu.h#pragma once
#include <SFML/Graphics.hpp>
#define MAX_NUMBER_OF_ITEMS 2
using namespace sf;
class menu
{
public:
menu(float width, float height);
~menu();
void draw(RenderWindow &window);
void moveUp();
void moveDown();
private:
int selectedItemIndex;
Font font;
Text startMenu[MAX_NUMBER_OF_ITEMS];
};
menu.cpp#include <iostream>
#include "menu.h"
menu::menu(float width, float height)
{
if (!font.loadFromFile("res/consolab.ttf"))
{
std::cout << "cannot load font: consolab.ttf" << std::endl;
}
startMenu[0].setFont(font);
startMenu[0].setColor(Color::Red);
startMenu[0].setString("Play");
startMenu[0].setPosition(Vector2f(width / 2, (MAX_NUMBER_OF_ITEMS + 1) * 1));
startMenu[1].setFont(font);
startMenu[1].setColor(Color::White);
startMenu[1].setString("Exit");
startMenu[1].setPosition(Vector2f(width / 2, (MAX_NUMBER_OF_ITEMS + 1) * 2));
}
menu::~menu()
{
}
void menu::draw(RenderWindow &window)
{
for (int i = 0; i < MAX_NUMBER_OF_ITEMS; i++)
{
window.draw(startMenu[i]);
}
}
main.cpp#include <SFML/Graphics.hpp>
#include "tank.h"
#include "menu.h"
using namespace sf;
int main()
{
RenderWindow window(VideoMode(800, 600), "Tanks v1.3");
menu startingMenu(window.getSize().x, window.getSize().y);
Tank player1;
player1.addTankAttributes(650, 500, Color(255, 0, 0), "Player 1");
Tank player2;
player2.addTankAttributes(50, 500, Color(0, 0, 255), "Player 2");
while (window.isOpen())
{
Event event;
while (window.pollEvent(event))
{
if (event.type == Event::Closed)
window.close();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) //move up
{
player1.moveRight();
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) //move down
{
player1.moveLeft();
}
window.clear(Color(21,196,202));
startingMenu.draw(window);
player1.drawTank(window);
player2.drawTank(window);
window.display();
}
return 0;
}
One more class that I've got, I tryed to use text there as well, the same problem...
Tank.h#ifndef TANK_H
#define TANK_H
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace sf;
class Tank
{
public:
void addTankAttributes(int x, int y, Color &color, String name);
void drawTank(RenderWindow &window);
void moveLeft();
void moveRight();
Tank();
~Tank();
private:
const float rotation = 0.04;
Texture tankTexture;
Texture turretTexture;
Sprite tankSprite;
Sprite turretSprite;
Font font;
Text text;
};
Tank::Tank()
{
if (!tankTexture.loadFromFile("res/tank.png"))
std::cout << "no tank.png file" << std::endl;
tankSprite.setTexture(tankTexture);
if (!turretTexture.loadFromFile("res/turret.png"))
std::cout << "no turret.png file" << std::endl;
turretSprite.setTexture(turretTexture);
}
void Tank::addTankAttributes(int x, int y, Color &color, String name)
{
tankSprite.setColor(color);
tankSprite.setPosition(x, y);
turretSprite.setColor(color);
turretSprite.setPosition(x+15, y+2);
turretSprite.setOrigin(0, 0);
}
void Tank::drawTank(RenderWindow &window)
{
window.draw(turretSprite);
window.draw(tankSprite);
}
void Tank::moveLeft()
{
if (turretSprite.getRotation() > 152 || turretSprite.getRotation() < 17)
{
turretSprite.rotate(-rotation);
}
}
void Tank::moveRight()
{
if (turretSprite.getRotation() > 150 || turretSprite.getRotation() < 15)
{
turretSprite.rotate(rotation);
}
}
Tank::~Tank()
{
}
#endif TANK_H