Skip to content

Instantly share code, notes, and snippets.

@Furkanzmc
Last active Aug 29, 2015
Embed
What would you like to do?
main.cpp file for the zmcEntitySystem tutorial
#include <iostream>
#include <memory>
#include "EntityManager.h"
#include "Example/ShipEntitySystem.h"
#include "Example/SpriteComponent.h"
#include <SFML/Graphics.hpp>
#include "ComponentManager.h"
using namespace std;
using namespace zmc;
enum GROUPS {
SHIP
};
enum COMPONENTS {
MOVEMENT,
SPRITE
};
int main()
{
std::shared_ptr<sf::RenderWindow> window(new sf::RenderWindow(sf::VideoMode(640, 480), "SD", sf::Style::Default));
//Create a component manager to pass to entity manager
std::unique_ptr<ComponentManager> componentManager(new ComponentManager());
//Our entity manager to create and store entites
EntityManager entityManager(componentManager.get());
//The first parameter is used to determine how many groups will there be
/*----------------------*/
Entity *entity = entityManager.createEntity(1, GROUPS::SHIP);
std::unique_ptr<SpriteComponent> sprite(new SpriteComponent(COMPONENTS::SPRITE));
componentManager->addComponent(entity->getEntityID(), std::move(sprite));
entity->setCustomIdentifier(15);
entity = nullptr;
entity = entityManager.getEntityByCustomIdentifier(15);
/*----------------------*/
//This is our system to process our entities
ShipEntitySystem sSystem(*window.get());
std::vector<int> vectorGroup;
vectorGroup.push_back(COMPONENTS::MOVEMENT);
vectorGroup.push_back(COMPONENTS::SPRITE);
sSystem.setRequiredComponents(vectorGroup);
sSystem.addEntity(*entity);
while (window->isOpen()) {
sf::Event event;
while (window->pollEvent(event)) {
if (event.type == sf::Event::Closed)
window->close();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
window->close();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::C))
entity->setEnabled(true);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::V))
entity->setEnabled(false);
}
window->clear(sf::Color(245, 118, 243));
sSystem.process();
window->display();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment