TemplateGame.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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_lightColor")->setValue(light->getColor());
  25. boxMaterial->getParameter("u_lightDirection")->setValue(lightNode->getForwardVectorView());
  26. }
  27. void TemplateGame::finalize()
  28. {
  29. SAFE_RELEASE(_scene);
  30. }
  31. void TemplateGame::update(float elapsedTime)
  32. {
  33. // Rotate model
  34. _scene->findNode("box")->rotateY(MATH_DEG_TO_RAD((float)elapsedTime / 1000.0f * 180.0f));
  35. }
  36. void TemplateGame::render(float elapsedTime)
  37. {
  38. // Clear the color and depth buffers
  39. clear(CLEAR_COLOR_DEPTH, Vector4::zero(), 1.0f, 0);
  40. // Visit all the nodes in the scene for drawing
  41. _scene->visit(this, &TemplateGame::drawScene);
  42. }
  43. bool TemplateGame::drawScene(Node* node)
  44. {
  45. // If the node visited contains a model, draw it
  46. Model* model = node->getModel();
  47. if (model)
  48. {
  49. model->draw();
  50. }
  51. return true;
  52. }
  53. void TemplateGame::keyEvent(Keyboard::KeyEvent evt, int key)
  54. {
  55. if (evt == Keyboard::KEY_PRESS)
  56. {
  57. switch (key)
  58. {
  59. case Keyboard::KEY_ESCAPE:
  60. exit();
  61. break;
  62. }
  63. }
  64. }
  65. void TemplateGame::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  66. {
  67. switch (evt)
  68. {
  69. case Touch::TOUCH_PRESS:
  70. break;
  71. case Touch::TOUCH_RELEASE:
  72. break;
  73. case Touch::TOUCH_MOVE:
  74. break;
  75. };
  76. }