Simple game engine with complete export to scripting language
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

189 lines
4.9 KiB

#include "GL/glew.h"
#include "RenderWindow.h"
#include "RenderDevice.h"
#include "FontManager.h"
#include "MeshManager.h"
#include "TextureManager.h"
#include "ShaderManager.h"
#include "ModelManager.h"
#include "TextureImage.h"
#include "ScriptSystem.h"
#include "ScriptSystem_Font.h"
#include "ScriptSystem_Image.h"
#include "ScriptSystem_Math.h"
#include "ScriptSystem_RigidBody.h"
#include "RigidBodySimulation.h"
#include "Camera.h"
#include "SceneNode.h"
#include "Utilities/CfgParser.h"
#include "Utilities/Log.h"
#include "Utilities/Kernel.h"
#include "physfs.h"
using namespace BlueCore;
void initializePhysfs(char* program)
{
// setup physfs
PHYSFS_init(program);
std::string appdir = PHYSFS_getUserDir();
appdir += ".bluecore";
if ( !PHYSFS_setWriteDir(appdir.c_str()) )
{
if ( (PHYSFS_setWriteDir(PHYSFS_getUserDir()))
&& (PHYSFS_mkdir(".bluecore")))
PHYSFS_setWriteDir(appdir.c_str() );
}
PHYSFS_addToSearchPath(appdir.c_str(), 0);
PHYSFS_addToSearchPath("data", 1);
char **rc = PHYSFS_enumerateFiles("");
for (char **i = rc; *i != 0; i++)
{
std::string filename( *i);
if (filename.substr(filename.size() - 4, 4) == ".zip")
{
PHYSFS_addToSearchPath(( "data/" + filename ).c_str(), 1);
clog << ">>> Using addon: "<< filename << endlog;
}
}
PHYSFS_freeList(rc);
}
void shutdownPhysfs()
{
PHYSFS_deinit();
}
int main(int argc, char **argv)
{
initializePhysfs(argv[0]);
CfgParser cfg;
cfg.parseFile("options.cfg");
int width = cfg.get("width", 640);
int height = cfg.get("height", 480);
bool fullscreen = cfg.get("fullscreen", false);
ref_ptr<RenderWindow> window = new RenderWindow();
window->create(width, height, 0, 0, 0, fullscreen);
ref_ptr<RenderDevice> device = new RenderDevice(window);
ref_ptr<FontManager> fontmanager = new FontManager(device);
ref_ptr<MeshManager> meshmanager = new MeshManager(device);
ref_ptr<TextureManager> texturemanager = new TextureManager();
ref_ptr<ScriptSystem> scriptsystem = new ScriptSystem();
ref_ptr<ShaderManager> shadermanager = new ShaderManager(window);
ref_ptr<RigidBodySimulation> simulation = new RigidBodySimulation(scriptsystem);
ref_ptr<ModelManager> modelmanager = new ModelManager (texturemanager, shadermanager, meshmanager);
ref_ptr<Camera> camera = new Camera();
ref_ptr<RenderQueue> queue = new RenderQueue();
setupScriptSystem_Font(scriptsystem, fontmanager);
setupScriptSystem_Image(scriptsystem, texturemanager, device);
setupScriptSystem_Math(scriptsystem);
setupScriptSystem_RigidBody(scriptsystem, simulation);
GLfloat mat_specular[] =
{ 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] =
{ 2.0 };
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_specular);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
ref_ptr<Mesh> starMesh = meshmanager->loadMesh("stars.3ds");
ref_ptr<Texture> starTexture = texturemanager->loadTexture("stars.png", 0);
ref_ptr<Mesh> nebularMesh = meshmanager->loadMesh("nebular.3ds");
ref_ptr<Texture> nebularTexture = texturemanager->loadTexture("nebular.png");
if (window.valid() && device.valid())
{
scriptsystem->loadScript("main");
camera->setFoV(90.0);
camera->setAspectRatio((double)width/(double)height);
camera->setNearPlane(1.0);
camera->setFarPlane(15000.0);
camera->setPosition(Vector3(0.0, 0.0, 20.0));
device->setAmbientLight(1.0, 1.0, 1.0);
//ref_ptr<SceneNode> rootnode(new SceneNode("root node"));
double _DeltaTime = 0;
double _LastTime = glfwGetTime();
ref_ptr<Model> model = modelmanager->loadModel("combat");
scriptsystem->callFunction("Initialize");
clog << "--- starting main loop..."<< endlog;
while (window->isOpen())
{
double time = glfwGetTime();
_DeltaTime = time - _LastTime;
_LastTime = time;
device->clear();
device->begin3D(camera);
queue->clear();
queue->addOpaqueItem(model, Vector3(10.0, 0.0, 0.0), Quaternion(Vector3(0.0,1.0,0.0), fmod(time, 3)));
queue->render(device);
glMatrixMode ( GL_MODELVIEW );
glPushMatrix();
glLoadIdentity();
glEnable ( GL_BLEND );
glBlendFunc ( GL_ONE, GL_ONE );
glEnable ( GL_DEPTH_TEST );
glDepthMask ( GL_FALSE );
glDisable ( GL_LIGHTING );
glColor4d ( 1.0f, 1.0f, 1.0f, 1.0f );
device->setTexture(0, starTexture, true);
starMesh->render();
glDisable ( GL_BLEND );
// device->useShader (program);
// device->setTexture (stage, name, texture)
// device->clearTextures (stage+1);
//glEnable (GL_TEXTURE_2D);
//glEnable (GL_LIGHTING);
//glBindTexture (GL_TEXTURE_2D, texture->getId() );
// device->
//device->
//model->render();
simulation->saveStates();
simulation->updateSteps(_DeltaTime);
while (simulation->step())
;
scriptsystem->callFunction("OnFrame", _DeltaTime);
device->swap();
}
scriptsystem->callFunction("Shutdown");
clog << "--- main loop finished..."<< endlog;
}
shutdownPhysfs();
}