Conflicts:
	scene.h
This commit is contained in:
Lendemor 2015-09-25 00:30:54 +02:00
commit 7fc3830f9f
12 changed files with 355 additions and 70 deletions

3
.gitignore vendored
View File

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

View File

@ -1,13 +1,20 @@
project(SparrowEngine) project(SparrowEngine)
cmake_minimum_required(VERSION 2.8) cmake_minimum_required(VERSION 2.8)
find_package(OpenGL REQUIRED)
if(WIN32) if(WIN32)
set(SYSTEM_LIB_PATH "win32") set(SYSTEM_LIB_PATH "win32")
else(WIN32) 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 +32,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 +53,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}
) )
@ -76,16 +86,26 @@ find_library(LINEAR_MATH_LIBRARY
${LIB_ROOT} ${LIB_ROOT}
) )
find_library(GLEW_LIBRARY
NAMES
GLEW
PATHS
${LIB_ROOT}
)
target_link_libraries( target_link_libraries(
${LIBRARY_NAME} ${LIBRARY_NAME}
${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}
${GLEW_LIBRARY}
${OPENGL_LIBRARIES}
) )
# ${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}

0
README
View File

50
cameranode.cpp Normal file
View File

@ -0,0 +1,50 @@
#include "cameranode.h"
#include <glm/ext.hpp>
CameraNode::CameraNode(glm::vec3 position, float yFov, float near, float far) :
m_hasMoved(true),
m_hasResized(false),
m_eye(position),
m_target(0, 0, 1),
m_yFov(yFov),
m_near(near),
m_far(far)
{
}
void CameraNode::setPosition(glm::vec3 position)
{
m_hasMoved = true;
m_eye = position;
}
void CameraNode::setTarget(glm::vec3 target)
{
m_hasMoved = true;
m_target = target;
}
void CameraNode::update()
{
if(m_hasMoved)
m_view = glm::lookAt(m_eye, m_target, glm::vec3(0, 1, 0));
if(m_hasResized)
m_projection = glm::perspective(m_yFov, m_ratio, m_near, m_far);
}
glm::mat4 CameraNode::getProjectionMatrix()
{
return m_projection;
}
glm::mat4 CameraNode::getViewMatrix()
{
return m_view;
}
void CameraNode::resize(int width, int height)
{
m_hasResized = false;
m_ratio = ((float)width)/((float)height);
}

38
cameranode.h Normal file
View File

@ -0,0 +1,38 @@
#ifndef CAMERANODE_H
#define CAMERANODE_H
#include "scene.h"
#include "camera.h"
class CameraNode : public SceneNode, public Camera
{
bool m_hasMoved;
bool m_hasResized;
glm::vec3 m_eye;
glm::vec3 m_target;
float m_yFov;
float m_ratio;
float m_near;
float m_far;
glm::mat4 m_projection;
glm::mat4 m_view;
public:
CameraNode(glm::vec3 position, float yFov = 70.f, float near = 0.1f, float far = 100.f);
CameraNode(float yFov = 70.f, float near = 0.1f, float far = 100.f) :
CameraNode(glm::vec3(0), yFov, near, far) {}
void setPosition(glm::vec3 position);
void setTarget(glm::vec3 target);
virtual void update();
virtual glm::mat4 getProjectionMatrix();
virtual glm::mat4 getViewMatrix();
virtual void resize(int width, int height);
};
#endif // CAMERANODE_H

View File

@ -1,33 +1,96 @@
#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 "cameranode.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_world(NULL)
{ {
m_clock.restart(); 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)
{
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::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
if(m_world != NULL)
m_world->stepSimulation(1000.f*(float)getDeltaTime());
// 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::updatePhysics() void Engine::start()
{ {
// work for Bullet m_running = true;
while(!m_input->isCloseRequested() && m_running){
update();
}
} }
void Engine::render() void Engine::stop()
{ {
// work for SparrowRenderer m_running = false;
} }
unsigned int Engine::getTime() unsigned int Engine::getTime()
@ -40,7 +103,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,55 @@
#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 ~Engine();
void update();
void render(); void createWindow(std::string title = "SparrowEngine",
void updatePhysics(); unsigned int w = 800,
unsigned int h = 600,
bool isWindowed = true);
void setScene(std::string sceneName);
void initPhysics();
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 m_running;
sf::Window* m_window;
Input* m_input;
Scene* m_scene;
btDiscreteDynamicsWorld* m_world;
SparrowRenderer* m_renderer;
void update();
}; };
#endif #endif

View File

@ -1,13 +1,20 @@
#include "engine.h" #include "engine.h"
#include <input.h>
#include "scene.h"
#include "cameranode.h"
#include "resourcemanager.h"
#include "sparrowrenderer.h"
int main(){ int main(){
Input* input; CameraNode camera;
Engine engine = Engine(input); RESOURCE_ADD(&camera, SceneNode, "camera");
while(!input->isCloseRequested()){
engine.update(); Scene scene("testScene");
// game content scene.addChild("camera");
engine.updatePhysics();
engine.render(); Engine engine;
} engine.getRenderer()->setCamera(&camera);
engine.createWindow("test");
engine.setScene("testScene");
engine.start();
} }

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,16 +1,52 @@
#include "scene.h" #include "scene.h"
#include "resourcemanager.h"
Scene::Scene() // Scene
Scene::Scene(std::string name) :
m_name(name)
{ {
RESOURCE_ADD(this, Scene, m_name);
} }
void Scene::update() Scene::~Scene()
{ {
//update call for all nodes RESOURCE_ADD(NULL, Scene, m_name);
} }
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,28 +1,34 @@
#ifndef SCENE_H #ifndef SCENE_H
#define SCENE_H #define SCENE_H
#include "SFML/System.hpp" #include <vector>
#include <string>
class SceneNode;
class Scene
{
public:
Scene();
void update();
private:
SceneNode* m_sceneNodeTree;
};
//Node Interface
class SceneNode class SceneNode
{ {
public: public:
SceneNode(); SceneNode* m_parent;
virtual void update() = 0; virtual void update() = 0;
};
class ContainerNode : public SceneNode
{
public:
virtual void update();
void addChild(const std::string &childName);
void removeChild(const std::string &childName);
protected:
std::vector<SceneNode*> m_children;
};
class Scene : public ContainerNode
{
public:
Scene(std::string name);
~Scene();
private: private:
SceneNode* m_parentNode; std::string m_name;
std::vector<SceneNode*> m_childrenNode;
}; };
//general Node //general Node
@ -81,7 +87,6 @@ class ChunkNode: public SceneNode
public: public:
virtual void update(); virtual void update();
// map // map
}; =======
#endif // SCENE_H #endif // SCENE_H