added config file loader, changed mouse sensitivity, changed player speed, and objects throwing force
This commit is contained in:
parent
f7c2609da4
commit
1262f6417d
6
config.ini
Normal file
6
config.ini
Normal file
@ -0,0 +1,6 @@
|
||||
# this is the demo's config file
|
||||
width=800
|
||||
height=600
|
||||
mode=windowed
|
||||
scene=none
|
||||
vsync=false
|
@ -1,9 +0,0 @@
|
||||
#
|
||||
# generated by kHED
|
||||
#
|
||||
newmtl steel
|
||||
d 1.0
|
||||
map_Kd steel.jpg
|
||||
newmtl leather
|
||||
d 1.0
|
||||
map_Kd leather.jpg
|
2107
data/bak/sword.obj
2107
data/bak/sword.obj
File diff suppressed because it is too large
Load Diff
@ -45,11 +45,16 @@ Engine::~Engine()
|
||||
void Engine::createWindow(std::string title,
|
||||
unsigned int w,
|
||||
unsigned int h,
|
||||
bool isWindowed)
|
||||
const std::string &mode)
|
||||
{
|
||||
sf::Uint32 style = sf::Style::Close;
|
||||
if(mode == "fullscreen")
|
||||
style = sf::Style::Fullscreen;
|
||||
else if(mode == "borderless")
|
||||
style = sf::Style::None;
|
||||
m_window = new sf::Window(sf::VideoMode(w, h),
|
||||
title,
|
||||
isWindowed ? sf::Style::Close : sf::Style::Fullscreen,
|
||||
style,
|
||||
sf::ContextSettings(24, 8, 0, 3, 3, sf::ContextSettings::Attribute::Core));
|
||||
m_window->setFramerateLimit(60);
|
||||
m_input = new Input(m_window);
|
||||
@ -164,6 +169,7 @@ void Engine::toggleMouseVisibility()
|
||||
{
|
||||
m_mouseVisible = !m_mouseVisible;
|
||||
m_window->setMouseCursorVisible(m_mouseVisible);
|
||||
m_input->setMouseGrabbed(!m_mouseVisible);
|
||||
}
|
||||
|
||||
void Engine::setTogglePhysicsDebugAction(int action)
|
||||
@ -195,6 +201,9 @@ void Engine::checkSpecialInputs()
|
||||
{
|
||||
for(int action : m_input->getActions())
|
||||
{
|
||||
if(action == -1)
|
||||
continue;
|
||||
|
||||
if(action == m_togglePhysicsDebugAction)
|
||||
{
|
||||
if(m_physicsDebugNode == nullptr)
|
||||
|
@ -25,7 +25,7 @@ public:
|
||||
void createWindow(std::string title = "SparrowEngine",
|
||||
unsigned int w = 800,
|
||||
unsigned int h = 600,
|
||||
bool isWindowed = true);
|
||||
const std::string &mode = "windowed");
|
||||
void setScene(SceneTree *scene);
|
||||
void initPhysics();
|
||||
|
||||
|
@ -7,8 +7,10 @@
|
||||
#include <glm/ext.hpp>
|
||||
|
||||
#include "scenetree.h"
|
||||
#include "lightnode.h"
|
||||
#include <SFML/System.hpp>
|
||||
|
||||
#define DEFAULT_ROTATION_SPEED 0.01f
|
||||
#define DEFAULT_ROTATION_SPEED 0.001f
|
||||
|
||||
void FirstPersonCamera::computeView()
|
||||
{
|
||||
@ -52,7 +54,7 @@ void FirstPersonCamera::setUpVector(const glm::vec3 &up)
|
||||
computeView();
|
||||
}
|
||||
|
||||
const float WALK_SPEED = 8.f;
|
||||
const float WALK_SPEED = 5.f;
|
||||
const float PLAYER_RADIUS = 0.30f;
|
||||
const float PLAYER_HEIGHT = 1.75f;
|
||||
const float EYES_OFFSET = 0.775f;
|
||||
@ -63,6 +65,10 @@ PlayerCharacterNode::PlayerCharacterNode(bool noClip) :
|
||||
m_noclipMode(noClip),
|
||||
m_inputActions({NO_ACTION, NO_ACTION, NO_ACTION, NO_ACTION, NO_ACTION})
|
||||
{
|
||||
m_playerLight = new PointLight(glm::vec3(150, 10, 30), 10, glm::vec3(0.18f, 0.16f, 0.096f)*2.f);
|
||||
m_playerLightNode = new LightNode(m_playerLight);
|
||||
m_playerLightNode->m_parent = this;
|
||||
|
||||
// Create the shape
|
||||
btCollisionShape *shape = new btCapsuleShape(PLAYER_RADIUS, PLAYER_HEIGHT);
|
||||
|
||||
@ -78,6 +84,13 @@ PlayerCharacterNode::PlayerCharacterNode(bool noClip) :
|
||||
m_rigidBody->setAngularFactor(0.0);
|
||||
}
|
||||
|
||||
PlayerCharacterNode::~PlayerCharacterNode()
|
||||
{
|
||||
m_playerLightNode->setSceneTree(nullptr);
|
||||
delete m_playerLightNode;
|
||||
delete m_playerLight;
|
||||
}
|
||||
|
||||
void PlayerCharacterNode::setInputs(int forward, int backward, int strafeLeft, int strafeRight, int jump, int toggleNoClip)
|
||||
{
|
||||
m_inputActions[FORWARD] = forward;
|
||||
@ -127,6 +140,7 @@ void PlayerCharacterNode::update()
|
||||
|
||||
// update camera position
|
||||
btVector3 pos = m_rigidBody->getCenterOfMassPosition();
|
||||
m_playerLight->setPos(glm::vec3(pos.x(), pos.y()+PLAYER_HEIGHT/2.f, pos.z()));
|
||||
m_fpsCamera.moveTo(glm::vec3(pos.x(), pos.y()+EYES_OFFSET, pos.z()));
|
||||
|
||||
// update body movement
|
||||
@ -179,6 +193,7 @@ void PlayerCharacterNode::update()
|
||||
}
|
||||
m_rigidBody->setLinearVelocity(newVelocity);
|
||||
}
|
||||
m_playerLightNode->update();
|
||||
}
|
||||
|
||||
void PlayerCharacterNode::toggleNoClip()
|
||||
@ -186,3 +201,9 @@ void PlayerCharacterNode::toggleNoClip()
|
||||
m_noclipMode = !m_noclipMode;
|
||||
m_noclip_pos = m_rigidBody->getCenterOfMassPosition();
|
||||
}
|
||||
|
||||
void PlayerCharacterNode::setSceneTree(SceneTree* tree)
|
||||
{
|
||||
m_scene = tree;
|
||||
m_playerLightNode->setSceneTree(tree);
|
||||
}
|
||||
|
@ -7,6 +7,8 @@
|
||||
|
||||
class btRigidBody;
|
||||
class btDefaultMotionState;
|
||||
class PointLight;
|
||||
class LightNode;
|
||||
|
||||
class FirstPersonCamera : public BasicCamera
|
||||
{
|
||||
@ -39,12 +41,16 @@ class PlayerCharacterNode : public CameraNode
|
||||
bool m_noclipMode;
|
||||
btVector3 m_noclip_pos;
|
||||
|
||||
PointLight* m_playerLight;
|
||||
LightNode* m_playerLightNode;
|
||||
|
||||
std::vector<int> m_inputActions;
|
||||
|
||||
enum PlayerAction {FORWARD, BACKWARD, STRAFE_LEFT, STRAFE_RIGHT, JUMP, TOGGLE_NOCLIP};
|
||||
|
||||
public:
|
||||
PlayerCharacterNode(bool noClip = true);
|
||||
~PlayerCharacterNode();
|
||||
|
||||
void setInputs(int forward, int backward, int strafe_left, int strafe_right, int jump = NO_ACTION, int toggleNoClip = NO_ACTION);
|
||||
|
||||
@ -60,6 +66,8 @@ public:
|
||||
btRigidBody* getRigidbody() { return m_rigidBody; }
|
||||
|
||||
virtual Camera *getCamera() { return &m_fpsCamera; }
|
||||
|
||||
virtual void setSceneTree(SceneTree* tree);
|
||||
};
|
||||
|
||||
#endif // PLAYERCHARACTERNODE_H
|
||||
|
@ -124,24 +124,43 @@ void generateSponza(SceneTree *scene, btDiscreteDynamicsWorld *world)
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv){
|
||||
enum Mode { SIMPLEST_TEST, TERRAIN_TEST, SPONZA_TEST, FULLSCREEN_DEMO };
|
||||
Mode mode = SIMPLEST_TEST;
|
||||
if(argc > 1)
|
||||
struct Config
|
||||
{
|
||||
std::string mode; // fullscreen / windowed / borderless
|
||||
std::string scene; // terrain / sponza / none
|
||||
bool vsync;
|
||||
int width;
|
||||
int height;
|
||||
|
||||
Config() :
|
||||
mode("windowed"),
|
||||
scene("none"),
|
||||
vsync(false),
|
||||
width(800),
|
||||
height(600)
|
||||
{}
|
||||
|
||||
void loadFromMap(std::unordered_map<std::string, std::string>* configMap)
|
||||
{
|
||||
std::string modeStr(argv[1]);
|
||||
if(modeStr == "terrain")
|
||||
mode = TERRAIN_TEST;
|
||||
else if(modeStr == "sponza")
|
||||
mode = SPONZA_TEST;
|
||||
else if(modeStr == "demo")
|
||||
mode = FULLSCREEN_DEMO;
|
||||
else if(modeStr == "simple")
|
||||
mode = SIMPLEST_TEST;
|
||||
else
|
||||
std::cout << "AVAILABLE MODES : simple(default) / terrain / sponza / demo" << std::endl;
|
||||
std::unordered_map<std::string, std::string>& conf = *configMap;
|
||||
if(conf.count("width"))
|
||||
width = std::stoi(conf["width"]);
|
||||
if(conf.count("height"))
|
||||
height = std::stoi(conf["height"]);
|
||||
if(conf.count("vsync"))
|
||||
vsync = (conf["vsync"] == "true");
|
||||
if(conf.count("mode"))
|
||||
mode = conf["mode"];
|
||||
if(conf.count("scene"))
|
||||
scene = conf["scene"];
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
Config config;
|
||||
config.loadFromMap(Loader::loadConfigFile("../config.ini"));
|
||||
|
||||
Engine engine;
|
||||
|
||||
Loader::setObjDirectory("../data/");
|
||||
@ -150,13 +169,10 @@ int main(int argc, char** argv){
|
||||
|
||||
// this creates the opengl context
|
||||
// the opengl context must exist before any opengl class is used (texture, pipeline, etc..)
|
||||
if(mode == FULLSCREEN_DEMO || mode == SPONZA_TEST)
|
||||
{
|
||||
engine.createWindow("Sparrow Engine Testing Environment", 1920, 1080, true);
|
||||
engine.toggleMouseVisibility();
|
||||
}
|
||||
else
|
||||
engine.createWindow("Sparrow Engine Testing Environment");
|
||||
engine.createWindow("Sparrow Engine Demo", config.width, config.height, config.mode);
|
||||
engine.getWindow()->setVerticalSyncEnabled(config.vsync);
|
||||
|
||||
engine.toggleMouseVisibility();
|
||||
|
||||
// setting up SparrowEngine
|
||||
engine.initPhysics();
|
||||
@ -201,33 +217,30 @@ int main(int argc, char** argv){
|
||||
scene->getRootObject()->addChild(potator);
|
||||
|
||||
// lighting
|
||||
LightNode *ambientLight = new LightNode(new AmbientLight());
|
||||
LightNode *ambientLight = new LightNode(new AmbientLight(glm::vec3(0.05f)));
|
||||
|
||||
DirectionnalLight* sun = new DirectionnalLight(glm::vec3(5, 8, -2), glm::vec3(0.9f));
|
||||
LightNode *sunLight = new LightNode(sun);
|
||||
|
||||
LightNode *fillLight = new LightNode(new DirectionnalLight(glm::vec3(8, -4, 1), glm::vec3(0.1f)));
|
||||
|
||||
scene->getRootObject()->addChild(ambientLight);
|
||||
scene->getRootObject()->addChild(sunLight);
|
||||
scene->getRootObject()->addChild(fillLight);
|
||||
|
||||
// terrain
|
||||
if(mode != SIMPLEST_TEST)
|
||||
// scene
|
||||
if(config.scene == "sponza")
|
||||
{
|
||||
sun->initShadowMap(4096);
|
||||
if(mode == SPONZA_TEST)
|
||||
{
|
||||
generateSponza(scene, engine.getPhysics());
|
||||
player->setPosition(0.f, 2.f, 0.f);
|
||||
sun->setShadowView(glm::vec3(30, 30, 50));
|
||||
}
|
||||
else
|
||||
{
|
||||
generateTerrain(scene, engine.getPhysics());
|
||||
player->setPosition(0.f, 15.f, 0.f);
|
||||
sun->setShadowView(glm::vec3(130, 130, 70));
|
||||
}
|
||||
generateSponza(scene, engine.getPhysics());
|
||||
scene->getRootObject()->addChild(new LightNode(new PointLight(glm::vec3(-3.5, 2, 1.8), 15, glm::vec3(0.35f))));
|
||||
scene->getRootObject()->addChild(new LightNode(new PointLight(glm::vec3(-5, 6, 2), 15, glm::vec3(0.35f))));
|
||||
player->setPosition(0.f, 2.f, 0.f);
|
||||
sun->setShadowView(glm::vec3(30, 30, 50));
|
||||
}
|
||||
else if(config.scene == "terrain")
|
||||
{
|
||||
sun->initShadowMap(4096);
|
||||
generateTerrain(scene, engine.getPhysics());
|
||||
player->setPosition(0.f, 15.f, 0.f);
|
||||
sun->setShadowView(glm::vec3(130, 130, 70));
|
||||
}
|
||||
|
||||
// shell output tests
|
||||
|
@ -127,7 +127,7 @@ void Potator::throwCube()
|
||||
dir = m_player->getDirection();
|
||||
pos = m_player->getEyePosition() + dir*2.f;
|
||||
}
|
||||
float throwForce = 10.f;
|
||||
float throwForce = 5.f;
|
||||
|
||||
createGib(new MeshNode(m_cubeMesh), m_cubeShape, m_cubeMass, pos, dir*throwForce, 30000);
|
||||
}
|
||||
@ -141,7 +141,7 @@ void Potator::throwSphere()
|
||||
dir = m_player->getDirection();
|
||||
pos = m_player->getEyePosition() + dir*2.f;
|
||||
}
|
||||
float throwForce = 10.f;
|
||||
float throwForce = 5.f;
|
||||
|
||||
createGib(new MeshNode(m_sphereMesh), m_sphereShape, m_sphereMass, pos, dir*throwForce, 30000);
|
||||
}
|
||||
@ -155,12 +155,12 @@ void Potator::throwSword()
|
||||
dir = m_player->getDirection();
|
||||
pos = m_player->getEyePosition() + dir*2.f;
|
||||
}
|
||||
float throwForce = 10.f;
|
||||
float throwForce = 5.f;
|
||||
|
||||
GraphicalContainerNode *node = new GraphicalContainerNode();
|
||||
for(Mesh * m : m_swordMeshes)
|
||||
node->addChild(new MeshNode(m));
|
||||
createGib(node, m_swordShape, m_swordMass, pos, dir*throwForce, 300000);
|
||||
createGib(node, m_swordShape, m_swordMass, pos, dir*throwForce, 30000);
|
||||
}
|
||||
|
||||
void Potator::update()
|
||||
|
@ -31,6 +31,39 @@ std::string* Loader::loadTextFile(const std::string &filename)
|
||||
return str;
|
||||
}
|
||||
|
||||
std::unordered_map<std::string, std::string>* Loader::loadConfigFile(const std::string &filename)
|
||||
{
|
||||
std::unordered_map<std::string, std::string>* configPtr = new std::unordered_map<std::string, std::string>();
|
||||
std::unordered_map<std::string, std::string>& config = *configPtr;
|
||||
|
||||
std::ifstream t(filename);
|
||||
|
||||
if(!t.is_open())
|
||||
return configPtr;
|
||||
|
||||
char line[256];
|
||||
std::string currentLine;
|
||||
|
||||
while(!t.eof())
|
||||
{
|
||||
currentLine = "";
|
||||
t.getline(line, 256);
|
||||
currentLine += std::string(line);
|
||||
|
||||
if(t.eof() || t.fail() || currentLine[0] == '#')
|
||||
continue;
|
||||
|
||||
unsigned int pos = currentLine.find_first_of('=');
|
||||
if(pos == std::string::npos)
|
||||
continue;
|
||||
|
||||
std::string key = currentLine.substr(0, pos);
|
||||
std::string val = currentLine.substr(pos+1);
|
||||
config[key] = val;
|
||||
}
|
||||
return configPtr;
|
||||
}
|
||||
|
||||
Image* Loader::loadImage(const std::string &filename, bool hasAlpha, bool reversed)
|
||||
{
|
||||
sf::Image sfImg;
|
||||
|
@ -3,6 +3,7 @@
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
#include <glm/vec3.hpp>
|
||||
|
||||
class Image;
|
||||
@ -17,6 +18,7 @@ class Loader
|
||||
|
||||
public:
|
||||
static std::string* loadTextFile(const std::string &filename);
|
||||
static std::unordered_map<std::string, std::string>* loadConfigFile(const std::string &filename);
|
||||
static Image* loadImage(const std::string &filename, bool hasAlpha = true, bool reversed = true);
|
||||
static std::vector<Mesh*> loadMesh(const std::string &filename);
|
||||
static Font* loadFont(const std::string &texture, const std::string &description);
|
||||
|
Loading…
x
Reference in New Issue
Block a user