From 11e640be23884b732bdd31f31d2949e769ebba7d Mon Sep 17 00:00:00 2001 From: Anselme Date: Wed, 23 Sep 2015 21:22:04 +0200 Subject: [PATCH] worked on engine --- .gitignore | 3 +- CMakeLists.txt | 23 +++++++++----- cameranode.cpp | 7 +++++ cameranode.h | 21 +++++++++++++ engine.cpp | 74 +++++++++++++++++++++++++++++++++++---------- engine.h | 50 ++++++++++++++++++++++-------- main.cpp | 13 +++----- resourcemanager.cpp | 17 +++++++++++ resourcemanager.h | 23 ++++++++++++++ scene.cpp | 39 ++++++++++++++++++++++-- scene.h | 41 +++++++++---------------- 11 files changed, 236 insertions(+), 75 deletions(-) create mode 100644 cameranode.cpp create mode 100644 cameranode.h create mode 100644 resourcemanager.cpp create mode 100644 resourcemanager.h diff --git a/.gitignore b/.gitignore index 567609b..2ce6cc3 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -build/ +build* +*.user \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 14ec372..1c285d5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,12 @@ else(WIN32) set(SYSTEM_LIB_PATH "linux") 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(EXECUTABLE_NAME "test${PROJECT_NAME}") @@ -24,7 +29,10 @@ add_executable(${EXECUTABLE_NAME} main.cpp) add_definitions(-std=c++11) include_directories( - ${INCLUDE_ROOT} + ${INCLUDE_ROOT} + ${INCLUDE_ROOT}/bullet + ${PROJECT_SOURCE_DIR}/../sparrowinput + ${PROJECT_SOURCE_DIR}/../sparrowrenderer ) find_library(SFML_LIBRARY_WINDOW @@ -43,7 +51,7 @@ find_library(SFML_LIBRARY_SYSTEM find_library(SPARROW_RENDERER_LIBRARY NAMES - sparrowrenderer + SparrowRenderer PATHS ${LIB_ROOT} ) @@ -81,11 +89,12 @@ target_link_libraries( ${SFML_LIBRARY_WINDOW} ${SFML_LIBRARY_SYSTEM} ${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( ${EXECUTABLE_NAME} ${LIBRARY_NAME} diff --git a/cameranode.cpp b/cameranode.cpp new file mode 100644 index 0000000..86cfe4c --- /dev/null +++ b/cameranode.cpp @@ -0,0 +1,7 @@ +#include "cameranode.h" + +CameraNode::CameraNode() +{ + +} + diff --git a/cameranode.h b/cameranode.h new file mode 100644 index 0000000..7cf9ec4 --- /dev/null +++ b/cameranode.h @@ -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 diff --git a/engine.cpp b/engine.cpp index 42faa3a..d4d456d 100644 --- a/engine.cpp +++ b/engine.cpp @@ -1,33 +1,75 @@ - #include "engine.h" -#include "scenemanager.h" +#include +#include +#include +#include +#include +#include +#include "resourcemanager.h" +#include "scene.h" -//#include "bullet/btBulletCollisionCommon.h" -//#include "bullet/btBulletDynamicsCommon.h" - -Engine::Engine(Input* input): m_input(input) +Engine::Engine() : + m_input(NULL), + m_window(NULL) { - 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() { - //update time variable + // update delta time m_lastTimeStamp = m_timeStamp; - m_timeStamp = (unsigned int) m_clock.getElapsedTime().asMilliseconds(); - //update Events + m_timeStamp = (unsigned int) m_clock->getElapsedTime().asMilliseconds(); + // update Events 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() @@ -40,7 +82,7 @@ unsigned int Engine::getDeltaTime() 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); } diff --git a/engine.h b/engine.h index ffdf338..96941e2 100644 --- a/engine.h +++ b/engine.h @@ -1,30 +1,54 @@ #ifndef ENGINE_H #define ENGINE_H +#include + class Input; +class SparrowRenderer; class Scene; +namespace sf +{ + class Clock; + class Window; +} +class btDiscreteDynamicsWorld; -class Engine{ +class Engine +{ public: - Engine(Input* input); - //main function - void update(); - void render(); - void updatePhysics(); + Engine(); + + void createWindow(std::string title = "SparrowEngine", + unsigned int w = 800, + 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 getDeltaTime(); - //scene-related function - void setCurrentScene(std::string scene); - private: - Input* m_input; - sf::Clock m_clock; + sf::Clock* m_clock; unsigned int m_timeStamp = 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 diff --git a/main.cpp b/main.cpp index 2ecdf27..9404c11 100644 --- a/main.cpp +++ b/main.cpp @@ -1,13 +1,8 @@ - #include "engine.h" +#include int main(){ - Input* input; - Engine engine = Engine(input); - while(!input->isCloseRequested()){ - engine.update(); - // game content - engine.updatePhysics(); - engine.render(); - } + Engine engine; + engine.createWindow("test"); + engine.start(); } diff --git a/resourcemanager.cpp b/resourcemanager.cpp new file mode 100644 index 0000000..f1ed023 --- /dev/null +++ b/resourcemanager.cpp @@ -0,0 +1,17 @@ +#include "resourcemanager.h" + +std::unordered_map> 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); +} diff --git a/resourcemanager.h b/resourcemanager.h new file mode 100644 index 0000000..6092a68 --- /dev/null +++ b/resourcemanager.h @@ -0,0 +1,23 @@ +#ifndef RESOURCEMANAGER_H +#define RESOURCEMANAGER_H + +#include +#include + +#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> 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 diff --git a/scene.cpp b/scene.cpp index 01fed41..5e03e37 100644 --- a/scene.cpp +++ b/scene.cpp @@ -1,4 +1,5 @@ #include "scene.h" +#include "resourcemanager.h" Scene::Scene() { @@ -7,10 +8,42 @@ Scene::Scene() 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; im_parent = NULL; + m_children[i] == m_children.back(); + m_children.pop_back(); + break; + } + } + } +} + diff --git a/scene.h b/scene.h index 15d96dc..db2530a 100644 --- a/scene.h +++ b/scene.h @@ -1,46 +1,35 @@ #ifndef SCENE_H #define SCENE_H +#include +#include class SceneNode; +class ContainerNode; class Scene { public: Scene(); void update(); - void render(); private: - SceneNode* m_sceneNodeTree; + ContainerNode* m_root; }; - class SceneNode { public: - SceneNode(); + SceneNode* m_parent; + virtual void update() = 0; +}; + +class ContainerNode : public SceneNode +{ +public: virtual void update(); -private: - SceneNode* m_parentNode; -}; - -class CameraSceneNode : public SceneNode -{ - -}; - -class LightSceneNode : public SceneNode -{ - -}; - -class TerrainSceneNode: public SceneNode -{ - -}; - -class ObjectSceneNode : public SceneNode -{ - + void addChild(const std::string &childName); + void removeChild(const std::string &childName); +protected: + std::vector m_children; }; #endif // SCENE_H