SparrowEngine/engine.cpp
2015-09-24 11:41:23 +02:00

107 lines
2.6 KiB
C++

#include "engine.h"
#include <SFML/System/Clock.hpp>
#include <SFML/Window.hpp>
#include <input.h>
#include <sparrowrenderer.h>
#include <btBulletCollisionCommon.h>
#include <btBulletDynamicsCommon.h>
#include "resourcemanager.h"
#include "scene.h"
#include "cameranode.h"
Engine::Engine() :
m_input(NULL),
m_window(NULL),
m_world(NULL)
{
m_clock = new sf::Clock();
m_clock->restart();
m_renderer = new SparrowRenderer();
m_renderer->setCamera(NULL);
}
Engine::~Engine()
{
delete m_clock;
if(m_window != NULL)
m_renderer->destroyGL();
delete m_renderer;
if(m_window != NULL)
{
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::Default : sf::Style::Fullscreen,
sf::ContextSettings(24));
m_window->setFramerateLimit(60);
m_input = new Input(m_window);
m_renderer->initGL(w, h);
}
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, -10, 0));
}
void Engine::update()
{
// update delta time
m_lastTimeStamp = m_timeStamp;
m_timeStamp = (unsigned int) m_clock->getElapsedTime().asMilliseconds();
// update Events
m_input->updateEvents();
// update Scene
m_scene->update();
// update Physics
if(m_world != NULL)
m_world->stepSimulation(1000.f*(float)getDeltaTime());
// update Display
m_renderer->renderGL();
m_window->display();
}
void Engine::start()
{
m_running = true;
while(!m_input->isCloseRequested() && m_running){
update();
}
}
void Engine::stop()
{
m_running = false;
}
unsigned int Engine::getTime()
{
return m_timeStamp;
}
unsigned int Engine::getDeltaTime()
{
return m_timeStamp - m_lastTimeStamp;
}
void Engine::setScene(std::string sceneName)
{
m_scene = RESOURCE_GET(Scene, sceneName);
}