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.
 
 
 

172 lines
4.4 KiB

  1. #include "GL/glew.h"
  2. #include "RenderWindow.h"
  3. #include "RenderDevice.h"
  4. #include "FontManager.h"
  5. #include "MeshManager.h"
  6. #include "TextureManager.h"
  7. #include "ShaderManager.h"
  8. #include "ModelManager.h"
  9. #include "TextureImage.h"
  10. #include "ScriptSystem.h"
  11. #include "ScriptSystem_Font.h"
  12. #include "ScriptSystem_Image.h"
  13. #include "ScriptSystem_Math.h"
  14. #include "ScriptSystem_RigidBody.h"
  15. #include "RigidBodySimulation.h"
  16. #include "Camera.h"
  17. #include "SceneNode.h"
  18. #include "Utilities/CfgParser.h"
  19. #include "Utilities/Log.h"
  20. #include "Utilities/Kernel.h"
  21. #include "physfs.h"
  22. using namespace BlueCore;
  23. void initializePhysfs(char* program)
  24. {
  25. // setup physfs
  26. PHYSFS_init(program);
  27. std::string appdir = PHYSFS_getUserDir();
  28. appdir += ".bluecore";
  29. if ( !PHYSFS_setWriteDir(appdir.c_str()) )
  30. {
  31. if ( (PHYSFS_setWriteDir(PHYSFS_getUserDir()))
  32. && (PHYSFS_mkdir(".bluecore")))
  33. PHYSFS_setWriteDir(appdir.c_str() );
  34. }
  35. PHYSFS_addToSearchPath(appdir.c_str(), 0);
  36. PHYSFS_addToSearchPath("data", 1);
  37. char **rc = PHYSFS_enumerateFiles("");
  38. for (char **i = rc; *i != 0; i++)
  39. {
  40. std::string filename( *i);
  41. if (filename.substr(filename.size() - 4, 4) == ".zip")
  42. {
  43. PHYSFS_addToSearchPath(( "data/" + filename ).c_str(), 1);
  44. clog << ">>> Using addon: "<< filename << endlog;
  45. }
  46. }
  47. PHYSFS_freeList(rc);
  48. }
  49. void shutdownPhysfs()
  50. {
  51. PHYSFS_deinit();
  52. }
  53. int main(int argc, char **argv)
  54. {
  55. initializePhysfs(argv[0]);
  56. CfgParser cfg;
  57. cfg.parseFile("options.cfg");
  58. int width = cfg.get("width", 640);
  59. int height = cfg.get("height", 480);
  60. bool fullscreen = cfg.get("fullscreen", false);
  61. ref_ptr<RenderWindow> window = new RenderWindow();
  62. window->create(width, height, 0, 0, 0, fullscreen);
  63. ref_ptr<RenderDevice> device = new RenderDevice(window);
  64. ref_ptr<FontManager> fontmanager = new FontManager(device);
  65. ref_ptr<MeshManager> meshmanager = new MeshManager(device);
  66. ref_ptr<TextureManager> texturemanager = new TextureManager();
  67. ref_ptr<ScriptSystem> scriptsystem = new ScriptSystem();
  68. ref_ptr<ShaderManager> shadermanager = new ShaderManager(window);
  69. ref_ptr<RigidBodySimulation> simulation = new RigidBodySimulation(scriptsystem);
  70. ref_ptr<ModelManager> modelmanager = new ModelManager (texturemanager, shadermanager, meshmanager);
  71. ref_ptr<Camera> camera = new Camera();
  72. ref_ptr<RenderQueue> queue = new RenderQueue();
  73. /*
  74. ShaderProgram prog =
  75. shadermanager->loadShaderProgram("ambient_color_emissive");
  76. */
  77. setupScriptSystem_Font(scriptsystem, fontmanager);
  78. setupScriptSystem_Image(scriptsystem, texturemanager, device);
  79. setupScriptSystem_Math(scriptsystem);
  80. setupScriptSystem_RigidBody(scriptsystem, simulation);
  81. GLfloat mat_specular[] =
  82. { 1.0, 1.0, 1.0, 1.0 };
  83. GLfloat mat_shininess[] =
  84. { 2.0 };
  85. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_specular);
  86. glMaterialfv(GL_FRONT, GL_AMBIENT, mat_specular);
  87. glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  88. glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  89. if (window.valid() && device.valid())
  90. {
  91. scriptsystem->loadScript("main");
  92. camera->setFoV(90.0);
  93. camera->setAspectRatio((double)width/(double)height);
  94. camera->setNearPlane(1.0);
  95. camera->setFarPlane(100.0);
  96. camera->setPosition(Vector3(0.0, 0.0, 20.0));
  97. device->setAmbientLight(1.0, 1.0, 1.0);
  98. //ref_ptr<SceneNode> rootnode(new SceneNode("root node"));
  99. double _DeltaTime = 0;
  100. double _LastTime = glfwGetTime();
  101. ref_ptr<Model> model = modelmanager->loadModel("combat");
  102. scriptsystem->callFunction("Initialize");
  103. clog << "--- starting main loop..."<< endlog;
  104. while (window->isOpen())
  105. {
  106. double time = glfwGetTime();
  107. _DeltaTime = time - _LastTime;
  108. _LastTime = time;
  109. device->clear();
  110. device->begin3D(camera);
  111. queue->clear();
  112. queue->addOpaqueItem(model, Vector3(10.0, 0.0, 0.0), Quaternion(Vector3(0.0,1.0,0.0), fmod(time, 3)));
  113. queue->render(device);
  114. // device->useShader (program);
  115. // device->setTexture (stage, name, texture)
  116. // device->clearTextures (stage+1);
  117. //glEnable (GL_TEXTURE_2D);
  118. //glEnable (GL_LIGHTING);
  119. //glBindTexture (GL_TEXTURE_2D, texture->getId() );
  120. // device->
  121. //device->
  122. //model->render();
  123. simulation->saveStates();
  124. simulation->updateSteps(_DeltaTime);
  125. while (simulation->step())
  126. ;
  127. scriptsystem->callFunction("OnFrame", _DeltaTime);
  128. device->swap();
  129. }
  130. scriptsystem->callFunction("Shutdown");
  131. clog << "--- main loop finished..."<< endlog;
  132. }
  133. shutdownPhysfs();
  134. }