TemplateGame.cpp 2.3 KB

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