Can you please tell me where is my mistake
?
GameObject.cpp:
#include "GameObject.h"
#include <unordered_map>
namespace GameObject {
std::unordered_map<std::string, GameObject> gameobjects;
GameObject::GameObject() {
name = "gameobject";
sprite = sf::Sprite();
}
GameObject::GameObject(std::string gameobject_name, sf::Sprite gameobject_sprite, sf::Texture sprite_texture, int gameobject_position_x, int gameobject_position_y) {
name = gameobject_name;
sprite = gameobject_sprite;
sprite.setTexture(sprite_texture);
sprite.setPosition(gameobject_position_x, gameobject_position_y);
}
void GameObject::setName(std::string gameobject_name) {
name = gameobject_name;
}
void GameObject::setSprite(sf::Sprite gameobject_sprite) {
sprite = gameobject_sprite;
}
void GameObject::setTexture(sf::Texture gameobject_texture) {
sprite.setTexture(gameobject_texture);
}
std::string* GameObject::getName() {
return &name;
}
sf::Sprite* GameObject::getSprite() {
return &sprite;
}
void addGameObject(GameObject gameobject) {
gameobjects[*gameobject.getName()] = gameobject;
}
void removeGameObject(std::string gameobject_name) {
gameobjects.erase(gameobject_name);
}
GameObject* getGameObject(std::string gameobject_name) {
return &gameobjects[gameobject_name];
}
//std::vector<GameObject*> getGameObjects() {
// std::vector<GameObject*> gameobjects_c2;
// gameobjects_c2.reserve(gameobjects.size());
// for (auto gameobject : gameobjects) {
// gameobjects_c2.insert(gameobjects_c2.begin(), &gameobject.second);
// }
// return gameobjects_c2;
//}
}
GameObject.h:
#include <SFML\Graphics.hpp>
namespace GameObject {
class GameObject {
private:
std::string name;
sf::Sprite sprite;
public:
GameObject();
GameObject(std::string gameobject_name, sf::Sprite gameobject_sprite, sf::Texture sprite_texture, int gameobject_position_x, int gameobject_position_y);
void setName(std::string gameobject_name);
void setSprite(sf::Sprite gameobject_sprite);
void setTexture(sf::Texture gameobject_texture);
std::string* getName();
sf::Sprite* getSprite();
};
void addGameObject(GameObject gameobject);
void removeGameObject(std::string gameobject_name);
GameObject* getGameObject(std::string gameobject_name);
/*std::vector<GameObject*> getGameObjects();*/
}
Graphics.cpp:
#include <SFML\Graphics.hpp>
#include "Game.h"
#include "GameObject.h"
#include <iostream>
namespace Graphics {
sf::RenderWindow window;
void openWindow() {
window.create(sf::VideoMode(800, 800), Game::getGameName());
window.setVerticalSyncEnabled(true);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
switch (event.type) {
case sf::Event::Closed:
Game::closeGame();
break;
}
}
window.clear();
GameObject::GameObject* gameobject = GameObject::getGameObject("player_gameobject");
window.draw(*gameobject->getSprite());
window.display();
//for (i = 0; i<drawTable.size(); i++)
//{
// m_app->Draw(*drawTable[i]); //Here is the line causing the crash !
//}
//i = 0;
}
}
void closeWindow() {
window.close();
}
}
Graphics.h:
#include <SFML\Graphics.hpp>
namespace Graphics {
void openWindow();
void closeWindow();
}