#include "engine.h" #include #include #include #include #include #include #include #include "resourcemanager.h" #include "scene/scenetree.h" #include "sparrowshell/sparrowshell.h" #include "scene/physicsdebugnode.h" Engine::Engine() : m_window(nullptr), m_input(nullptr), m_world(nullptr), m_physicsDebugNode(nullptr), m_togglePhysicsDebugAction(NO_ACTION), m_toggleShellAction(NO_ACTION), m_exitGameAction(NO_ACTION) { m_clock = new sf::Clock(); m_clock->restart(); m_renderer = new SparrowRenderer(); } Engine::~Engine() { delete m_clock; delete m_renderer; if(m_window != NULL) { m_window->close(); delete m_window; delete m_input; } if(m_world != NULL) delete m_world; } void Engine::createWindow(std::string title, unsigned int w, unsigned int h, bool isWindowed) { m_window = new sf::Window(sf::VideoMode(w, h), title, isWindowed ? sf::Style::Close : sf::Style::Fullscreen, sf::ContextSettings(24, 8, 0, 3, 3, sf::ContextSettings::Attribute::Core)); m_window->setFramerateLimit(60); m_input = new Input(m_window); m_renderer->initGL(w, h); m_sparrowshell = new SparrowShell(m_window); } void Engine::initPhysics() { btDefaultCollisionConfiguration *collisionConfiguration = new btDefaultCollisionConfiguration(); btBroadphaseInterface *broadPhase = new btAxisSweep3(btVector3(-1000, -1000, -1000), btVector3(1000, 1000, 1000)); btCollisionDispatcher *dispatcher = new btCollisionDispatcher(collisionConfiguration); btSequentialImpulseConstraintSolver *solver = new btSequentialImpulseConstraintSolver(); m_world = new btDiscreteDynamicsWorld(dispatcher, broadPhase, solver, collisionConfiguration); m_world->setGravity(btVector3(0, -9.81f, 0)); } void Engine::update() { // update delta time m_lastTimeStamp = m_timeStamp; m_timeStamp = (unsigned int) m_clock->getElapsedTime().asMilliseconds(); // update Events m_input->updateEvents(); checkSpecialInputs(); // update Physics if(m_world != nullptr) { m_world->stepSimulation(1000.f*(float)getDeltaTime()); if(m_physicsDebugNode != nullptr) { m_physicsDebugNode->clearBuffers(); m_world->debugDrawWorld(); } } // update Scene m_scene->update(); // update Display if(m_input->isResized()) m_renderer->resizeGL(m_window->getSize().x, m_window->getSize().y); m_renderer->renderGL(); m_window->display(); } void Engine::start() { m_running = true; // for(int i = 0;i<5;i++) // m_sparrowshell->scrollUp(); while(!m_input->isCloseRequested() && m_running) update(); } void Engine::stop() { m_running = false; } unsigned int Engine::getTime() const { return m_timeStamp; } unsigned int Engine::getDeltaTime() const { return m_timeStamp - m_lastTimeStamp; } void Engine::setScene(SceneTree *scene) { if(m_physicsDebugNode != nullptr) { m_scene->removeFromIndex(m_physicsDebugNode); scene->addToIndex(m_physicsDebugNode); } scene->getRootObject()->removeChild(m_sparrowshell); m_scene = scene; m_renderer->setScene(m_scene); m_renderer->resizeGL(m_window->getSize().x, m_window->getSize().y); m_sparrowshell->setSceneTree(scene); scene->getRootObject()->addChild(m_sparrowshell); scene->updateShaders(); } void Engine::enablePhysicsDebug() { if(m_world != nullptr && m_physicsDebugNode == nullptr) { m_physicsDebugNode = new PhysicsDebugNode(); m_scene->addToIndex(m_physicsDebugNode); m_world->setDebugDrawer(m_physicsDebugNode); m_world->getDebugDrawer()->setDebugMode(btIDebugDraw::DBG_DrawWireframe); m_scene->updateShaders(); } } void Engine::disablePhysicsDebug() { if(m_world != nullptr && m_physicsDebugNode != nullptr) { m_world->setDebugDrawer(nullptr); m_scene->removeFromIndex(m_physicsDebugNode); delete m_physicsDebugNode; m_physicsDebugNode = nullptr; } } void Engine::setTogglePhysicsDebugAction(int action) { m_togglePhysicsDebugAction = action; } void Engine::setToggleShellAction(int action) { m_toggleShellAction = action; } void Engine::setExitGameAction(int action) { m_exitGameAction = action; } void Engine::outputShell(std::string str) const { m_sparrowshell->out(str); } void Engine::checkSpecialInputs() { for(int action : m_input->getActions()) { if(action == m_togglePhysicsDebugAction) { if(m_physicsDebugNode == nullptr) enablePhysicsDebug(); else disablePhysicsDebug(); } else if(action == m_toggleShellAction) { m_sparrowshell->toggleShell(); } else if(action == m_exitGameAction) { m_running = false; } } } SceneTree* Engine::createScene() { return new SceneTree(*this); }