worked on engine
This commit is contained in:
parent
8e14399b3f
commit
11e640be23
3
.gitignore
vendored
3
.gitignore
vendored
@ -1 +1,2 @@
|
||||
build/
|
||||
build*
|
||||
*.user
|
@ -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}
|
||||
|
7
cameranode.cpp
Normal file
7
cameranode.cpp
Normal file
@ -0,0 +1,7 @@
|
||||
#include "cameranode.h"
|
||||
|
||||
CameraNode::CameraNode()
|
||||
{
|
||||
|
||||
}
|
||||
|
21
cameranode.h
Normal file
21
cameranode.h
Normal 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
|
74
engine.cpp
74
engine.cpp
@ -1,33 +1,75 @@
|
||||
|
||||
#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"
|
||||
//#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);
|
||||
}
|
||||
|
50
engine.h
50
engine.h
@ -1,30 +1,54 @@
|
||||
#ifndef ENGINE_H
|
||||
#define ENGINE_H
|
||||
|
||||
#include <string>
|
||||
|
||||
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
|
||||
|
13
main.cpp
13
main.cpp
@ -1,13 +1,8 @@
|
||||
|
||||
#include "engine.h"
|
||||
#include <input.h>
|
||||
|
||||
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();
|
||||
}
|
||||
|
17
resourcemanager.cpp
Normal file
17
resourcemanager.cpp
Normal 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
23
resourcemanager.h
Normal 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
|
39
scene.cpp
39
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; 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
41
scene.h
@ -1,46 +1,35 @@
|
||||
#ifndef SCENE_H
|
||||
#define SCENE_H
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
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<SceneNode*> m_children;
|
||||
};
|
||||
|
||||
#endif // SCENE_H
|
||||
|
Loading…
x
Reference in New Issue
Block a user