| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- #include "TemplateGame.h"
- // Declare our game instance
- TemplateGame game;
- TemplateGame::TemplateGame()
- : _scene(NULL)
- {
- }
- void TemplateGame::initialize()
- {
- // Load game scene from file
- _scene = Scene::load("res/box.gpb");
- // Set the aspect ratio for the scene's camera to match the current resolution
- _scene->getActiveCamera()->setAspectRatio(getAspectRatio());
-
- // Get light node
- Node* lightNode = _scene->findNode("directionalLight");
- Light* light = lightNode->getLight();
- // Initialize box model
- Node* boxNode = _scene->findNode("box");
- Model* boxModel = boxNode->getModel();
- Material* boxMaterial = boxModel->setMaterial("res/box.material");
- boxMaterial->getParameter("u_ambientColor")->setValue(_scene->getAmbientColor());
- boxMaterial->getParameter("u_lightColor")->setValue(light->getColor());
- boxMaterial->getParameter("u_lightDirection")->setValue(lightNode->getForwardVectorView());
- }
- void TemplateGame::finalize()
- {
- SAFE_RELEASE(_scene);
- }
- void TemplateGame::update(float elapsedTime)
- {
- // Rotate model
- _scene->findNode("box")->rotateY(MATH_DEG_TO_RAD((float)elapsedTime / 1000.0f * 180.0f));
- }
- void TemplateGame::render(float elapsedTime)
- {
- // Clear the color and depth buffers
- clear(CLEAR_COLOR_DEPTH, Vector4::zero(), 1.0f, 0);
- // Visit all the nodes in the scene for drawing
- _scene->visit(this, &TemplateGame::drawScene);
- }
- bool TemplateGame::drawScene(Node* node)
- {
- // If the node visited contains a model, draw it
- Model* model = node->getModel();
- if (model)
- {
- model->draw();
- }
- return true;
- }
- void TemplateGame::keyEvent(Keyboard::KeyEvent evt, int key)
- {
- if (evt == Keyboard::KEY_PRESS)
- {
- switch (key)
- {
- case Keyboard::KEY_ESCAPE:
- exit();
- break;
- }
- }
- }
- void TemplateGame::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
- {
- switch (evt)
- {
- case Touch::TOUCH_PRESS:
- break;
- case Touch::TOUCH_RELEASE:
- break;
- case Touch::TOUCH_MOVE:
- break;
- };
- }
|