// Set the FPS limit of the window
m_window.setFramerateLimit(60);
// Activate the window
m_window.setActive(true);
// Disable lighting
glDisable(GL_LIGHTING);
// Configure the viewport (the same size as the window)
glViewport(0, 0, m_window.getSize().x, m_window.getSize().y);
// Enable OpenGL
glEnable(GL_TEXTURE_2D);
// Load all scenes
SceneTest sc_0;
scenes.push_back(&sc_0);
sf::Texture sortOfATex;
sortOfATex.loadFromFile("data/uglie.gif");
// Main game loop
while (m_window.isOpen())
{
// Event
sf::Event event;
while (m_window.pollEvent(event))
{
// Close the game when the [X] button is clicked
if (event.type == sf::Event::Closed)
m_window.close();
}
// Clear the buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Run the scene
scenes[currentScene]->run(m_window);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::B))
{
GameObject obj(sortOfATex);
objects.push_back(obj);
}
int8_t i;
for (const auto& x : objects)
{
objects[i].update();
objects[i].draw(m_window);
i++;
}
i = 0;
// Display the things on the window
m_window.display();
}
--- GameObject#include "GameObject.hpp"
GameObject::GameObject(sf::Texture& text)
: spr(text)
{}
GameObject::~GameObject() {}
void GameObject::update()
{
spr.move(1, 0);
}
void GameObject::draw(sf::RenderWindow& w)
{
w.draw(this->spr);
}
Header#ifndef GAMEOBJECT_HPP
#define GAMEOBJECT_HPP
#include <SFML/Graphics.hpp>
class GameObject
{
public:
GameObject(sf::Texture& text);
virtual ~GameObject();
virtual void update();
virtual void draw(sf::RenderWindow& w);
private:
sf::Sprite spr;
};
#endif // GAMEOBJECT_HPP
std::vector<GameObject*> objects;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::B))
{
GameObject* obj = new GameObject(sortOfATex);
objects.push_back(obj);
delete obj;
}
int8_t i;
for (const auto& x : objects)
{
objects[i]->update();
objects[i]->draw(m_window);
i++;
}
i = 0;
Do you mean that's happening stack overflow?When your code crashes, you usually run it with the debugger, which gives you plenty of information about the error. Among them, one of the most useful is the call stack, which is the sequence of function calls that led to the location of the crash. Knowing what crashed and at which location in source code, is a very good starting point for debugging a crash ;)
the game crashes after the first object when i do thisYou delete the object that you just added to your vector, then you try to use it. Accessing a deleted memory location is undefined behaviour.
Program received signal SIGSEGV, Segmentation fault.
At Core.cpp:73
int8_t i;
for (const auto& x : objects)
{
objects[i].update();
objects[i].draw(m_window);
i++;
}
i = 0;