worked on engine

This commit is contained in:
Anselme 2015-09-23 21:22:04 +02:00
parent 8e14399b3f
commit 11e640be23
11 changed files with 236 additions and 75 deletions

3
.gitignore vendored
View File

@ -1 +1,2 @@
build/ build*
*.user

View File

@ -7,7 +7,12 @@ else(WIN32)
set(SYSTEM_LIB_PATH "linux") set(SYSTEM_LIB_PATH "linux")
endif(WIN32) endif(WIN32)
set(LIB_SRC_LIST engine.cpp scene.cpp) set(LIB_SRC_LIST
engine.cpp
scene.cpp
resourcemanager.cpp
cameranode.cpp
)
set(LIBRARY_NAME ${PROJECT_NAME}) set(LIBRARY_NAME ${PROJECT_NAME})
set(EXECUTABLE_NAME "test${PROJECT_NAME}") set(EXECUTABLE_NAME "test${PROJECT_NAME}")
@ -25,6 +30,9 @@ add_definitions(-std=c++11)
include_directories( include_directories(
${INCLUDE_ROOT} ${INCLUDE_ROOT}
${INCLUDE_ROOT}/bullet
${PROJECT_SOURCE_DIR}/../sparrowinput
${PROJECT_SOURCE_DIR}/../sparrowrenderer
) )
find_library(SFML_LIBRARY_WINDOW find_library(SFML_LIBRARY_WINDOW
@ -43,7 +51,7 @@ find_library(SFML_LIBRARY_SYSTEM
find_library(SPARROW_RENDERER_LIBRARY find_library(SPARROW_RENDERER_LIBRARY
NAMES NAMES
sparrowrenderer SparrowRenderer
PATHS PATHS
${LIB_ROOT} ${LIB_ROOT}
) )
@ -81,11 +89,12 @@ target_link_libraries(
${SFML_LIBRARY_WINDOW} ${SFML_LIBRARY_WINDOW}
${SFML_LIBRARY_SYSTEM} ${SFML_LIBRARY_SYSTEM}
${SPARROW_INPUT_LIBRARY} ${SPARROW_INPUT_LIBRARY}
${SPARROW_RENDERER_LIBRARY}
${BULLET_COLLISION_LIBRARY}
${BULLET_DYNAMICS_LIBRARY}
${LINEAR_MATH_LIBRARY}
) )
# ${SPARROW_RENDERER_LIBRARY}
# ${BULLET_COLLISION_LIBRARY}
# ${}BULLET_DYNAMICS_LIBRARY
# ${LINEAR_MATH_LIBRARY}
target_link_libraries( target_link_libraries(
${EXECUTABLE_NAME} ${EXECUTABLE_NAME}
${LIBRARY_NAME} ${LIBRARY_NAME}

7
cameranode.cpp Normal file
View File

@ -0,0 +1,7 @@
#include "cameranode.h"
CameraNode::CameraNode()
{
}

21
cameranode.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef CAMERANODE_H
#define CAMERANODE_H
#include "scene.h"
#include "camera.h"
class CameraNode : public SceneNode, public Camera
{
public:
CameraNode();
virtual void update();
virtual glm::mat4 getProjectionMatrix();
virtual glm::mat4 getViewMatrix();
virtual void resize(int width, int height);
signals:
public slots:
};
#endif // CAMERANODE_H

View File

@ -1,33 +1,75 @@
#include "engine.h" #include "engine.h"
#include "scenemanager.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 "bullet/btBulletCollisionCommon.h" Engine::Engine() :
//#include "bullet/btBulletDynamicsCommon.h" m_input(NULL),
m_window(NULL)
Engine::Engine(Input* input): m_input(input)
{ {
m_clock.restart(); m_clock = new sf::Clock();
m_clock->restart();
m_renderer = new SparrowRenderer();
m_renderer->setCamera(NULL); // TODO
}
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() void Engine::update()
{ {
//update time variable // update delta time
m_lastTimeStamp = m_timeStamp; m_lastTimeStamp = m_timeStamp;
m_timeStamp = (unsigned int) m_clock.getElapsedTime().asMilliseconds(); m_timeStamp = (unsigned int) m_clock->getElapsedTime().asMilliseconds();
//update Events // update Events
m_input->updateEvents(); m_input->updateEvents();
// update Scene
m_scene->update();
// update Physics
m_world->stepSimulation(1000.f*(float)getDeltaTime());
// update Display
m_renderer->renderGL();
m_window->display();
} }
void Engine::updatePhysics() void Engine::start()
{ {
// work for Bullet running = true;
while(!m_input->isCloseRequested() && running){
update();
}
} }
void Engine::render() void Engine::stop()
{ {
// work for SparrowRenderer running = false;
} }
unsigned int Engine::getTime() unsigned int Engine::getTime()
@ -40,7 +82,7 @@ unsigned int Engine::getDeltaTime()
return m_timeStamp - m_lastTimeStamp; return m_timeStamp - m_lastTimeStamp;
} }
void Engine::setCurrentScene(std::string scene) void Engine::setScene(std::string sceneName)
{ {
p_currentScene = RESOURCE_GET(Scene,scene); m_scene = RESOURCE_GET(Scene,sceneName);
} }

View File

@ -1,30 +1,54 @@
#ifndef ENGINE_H #ifndef ENGINE_H
#define ENGINE_H #define ENGINE_H
#include <string>
class Input; class Input;
class SparrowRenderer;
class Scene; class Scene;
namespace sf
{
class Clock;
class Window;
}
class btDiscreteDynamicsWorld;
class Engine{ class Engine
{
public: public:
Engine(Input* input); Engine();
//main function
void update(); void createWindow(std::string title = "SparrowEngine",
void render(); unsigned int w = 800,
void updatePhysics(); unsigned int h = 600,
bool isWindowed = true);
void setScene(std::string sceneName);
void start();
void stop();
Input* getInput() {return m_input;}
SparrowRenderer* getRenderer() {return m_renderer;}
btDiscreteDynamicsWorld* getPhysics() {return m_world;}
//utils function
unsigned int getTime(); unsigned int getTime();
unsigned int getDeltaTime(); unsigned int getDeltaTime();
//scene-related function
void setCurrentScene(std::string scene);
private: private:
Input* m_input; sf::Clock* m_clock;
sf::Clock m_clock;
unsigned int m_timeStamp = 0; unsigned int m_timeStamp = 0;
unsigned int m_lastTimeStamp = 0; unsigned int m_lastTimeStamp = 0;
Scene* p_currentScene;
bool running;
sf::Window* m_window;
Input* m_input;
Scene* m_scene;
btDiscreteDynamicsWorld* m_world;
SparrowRenderer* m_renderer;
void initPhysics();
void update();
}; };
#endif #endif

View File

@ -1,13 +1,8 @@
#include "engine.h" #include "engine.h"
#include <input.h>
int main(){ int main(){
Input* input; Engine engine;
Engine engine = Engine(input); engine.createWindow("test");
while(!input->isCloseRequested()){ engine.start();
engine.update();
// game content
engine.updatePhysics();
engine.render();
}
} }

17
resourcemanager.cpp Normal file
View File

@ -0,0 +1,17 @@
#include "resourcemanager.h"
std::unordered_map<std::string, std::unordered_map<std::string, void*>> ResourceManager::data;
void ResourceManager::add(void* resource, const std::string &type, const std::string name)
{
data[type][name] = resource;
}
void* ResourceManager::get(const std::string &type, const std::string name)
{
return check(type,name) ? data[type][name] : NULL;
}
bool ResourceManager::check(const std::string &type, const std::string name){
return data.count(type) && data[type].count(name);
}

23
resourcemanager.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef RESOURCEMANAGER_H
#define RESOURCEMANAGER_H
#include <string>
#include <unordered_map>
#define RESOURCE_ADD(ptr, type, name) ResourceManager::add((void*)ptr, #type, name)
#define RESOURCE_GET(type, name) ((type*)(ResourceManager::get(#type, name)))
#define RESOURCE_CHECK(type,name) ResourceManager::check(#type, name)
class ResourceManager
{
private:
static std::unordered_map<std::string, std::unordered_map<std::string, void*>> data;
public:
static void add(void* resource, const std::string &type, const std::string name);
static void* get(const std::string &type, const std::string name);
static bool check(const std::string &type, const std::string name);
};
#endif // RESOURCEMANAGER_H

View File

@ -1,4 +1,5 @@
#include "scene.h" #include "scene.h"
#include "resourcemanager.h"
Scene::Scene() Scene::Scene()
{ {
@ -7,10 +8,42 @@ Scene::Scene()
void Scene::update() void Scene::update()
{ {
//update call for all nodes m_root->update();
} }
void Scene::render() // Container Node
void ContainerNode::update()
{ {
//render call for all nodes for(SceneNode* sn : m_children)
sn->update();
} }
void ContainerNode::addChild(const std::string &childName)
{
SceneNode* node = RESOURCE_GET(SceneNode, childName);
if(node != NULL)
{
m_children.push_back(node);
node->m_parent = this;
}
}
void ContainerNode::removeChild(const std::string &childName)
{
SceneNode* node = RESOURCE_GET(SceneNode, childName);
if(node != NULL)
{
for(unsigned int i=0; i<m_children.size(); ++i)
{
if(m_children[i] == node)
{
m_children[i]->m_parent = NULL;
m_children[i] == m_children.back();
m_children.pop_back();
break;
}
}
}
}

41
scene.h
View File

@ -1,46 +1,35 @@
#ifndef SCENE_H #ifndef SCENE_H
#define SCENE_H #define SCENE_H
#include <vector>
#include <string>
class SceneNode; class SceneNode;
class ContainerNode;
class Scene class Scene
{ {
public: public:
Scene(); Scene();
void update(); void update();
void render();
private: private:
SceneNode* m_sceneNodeTree; ContainerNode* m_root;
}; };
class SceneNode class SceneNode
{ {
public: public:
SceneNode(); SceneNode* m_parent;
virtual void update() = 0;
};
class ContainerNode : public SceneNode
{
public:
virtual void update(); virtual void update();
private: void addChild(const std::string &childName);
SceneNode* m_parentNode; void removeChild(const std::string &childName);
}; protected:
std::vector<SceneNode*> m_children;
class CameraSceneNode : public SceneNode
{
};
class LightSceneNode : public SceneNode
{
};
class TerrainSceneNode: public SceneNode
{
};
class ObjectSceneNode : public SceneNode
{
}; };
#endif // SCENE_H #endif // SCENE_H