TemplateGame.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include "TemplateGame.h"
  2. // Declare our game instance
  3. TemplateGame game;
  4. TemplateGame::TemplateGame()
  5. : _scene(NULL)
  6. {
  7. }
  8. void TemplateGame::initialize()
  9. {
  10. // Load game scene from file
  11. _scene = Scene::load("res/box.gpb");
  12. // Set the aspect ratio for the scene's camera to match the current resolution
  13. _scene->getActiveCamera()->setAspectRatio(getAspectRatio());
  14. // Get light node
  15. Node* lightNode = _scene->findNode("directionalLight");
  16. Light* light = lightNode->getLight();
  17. // Initialize box model
  18. Node* boxNode = _scene->findNode("box");
  19. Model* boxModel = boxNode->getModel();
  20. Material* boxMaterial = boxModel->setMaterial("res/box.material");
  21. boxMaterial->getParameter("u_ambientColor")->setValue(_scene->getAmbientColor());
  22. boxMaterial->getParameter("u_lightColor")->setValue(light->getColor());
  23. boxMaterial->getParameter("u_lightDirection")->setValue(lightNode->getForwardVectorView());
  24. }
  25. void TemplateGame::finalize()
  26. {
  27. SAFE_RELEASE(_scene);
  28. }
  29. void TemplateGame::update(float elapsedTime)
  30. {
  31. // Rotate model
  32. _scene->findNode("box")->rotateY(MATH_DEG_TO_RAD((float)elapsedTime / 1000.0f * 180.0f));
  33. }
  34. void TemplateGame::render(float elapsedTime)
  35. {
  36. // Clear the color and depth buffers
  37. clear(CLEAR_COLOR_DEPTH, Vector4::zero(), 1.0f, 0);
  38. // Visit all the nodes in the scene for drawing
  39. _scene->visit(this, &TemplateGame::drawScene);
  40. }
  41. bool TemplateGame::drawScene(Node* node)
  42. {
  43. // If the node visited contains a model, draw it
  44. Model* model = node->getModel();
  45. if (model)
  46. {
  47. model->draw();
  48. }
  49. return true;
  50. }
  51. void TemplateGame::keyEvent(Keyboard::KeyEvent evt, int key)
  52. {
  53. if (evt == Keyboard::KEY_PRESS)
  54. {
  55. switch (key)
  56. {
  57. case Keyboard::KEY_ESCAPE:
  58. exit();
  59. break;
  60. }
  61. }
  62. }
  63. void TemplateGame::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  64. {
  65. switch (evt)
  66. {
  67. case Touch::TOUCH_PRESS:
  68. break;
  69. case Touch::TOUCH_RELEASE:
  70. break;
  71. case Touch::TOUCH_MOVE:
  72. break;
  73. };
  74. }