///////////////////////////////////////////////////////////////////////////////////////////////////////// // SimpleGamePlugin // MyGame.cpp // // Code : Anael Seghezzi ///////////////////////////////////////////////////////////////////////////////////////////////////////// #include "MyGame.h" // constructor MyGame::MyGame(void): MGame() {} // destructor MyGame::~MyGame(void) {} // GUI scene and objects MScene * GUIScene = NULL; MOEntity * mouseEntity = NULL; MOEntity * barEntity = NULL; // custom begin scene void MyGame::onBeginScene(void) { MGame::onBeginScene(); // call the default MGame begin scene MEngine * engine = MEngine::getInstance(); // get the engine instance MLevel * level = engine->getLevel(); // get the current level // get GUI scene and objects GUIScene = level->getSceneByName("GUIScene"); mouseEntity = GUIScene->getEntityByName("Mouse"); barEntity = GUIScene->getEntityByName("Bar"); } // custom update void MyGame::update(void) { MEngine * engine = MEngine::getInstance(); // get the engine instance MSystemContext * system = engine->getSystemContext(); // get system context MInputContext * input = engine->getInputContext(); // get input context // custom GUI if(GUIScene) // check if the scene exist { if(GUIScene->getCamerasNumber() > 0) // check if there is a camera in the GUI scene { MOCamera * camera = GUIScene->getCameraByIndex(0); // get the first camera // move the mouse entity if(mouseEntity) { // get screen size unsigned int width, height; system->getScreenSize(&width, &height); float ratio = (float)width/(float)height; // get mouse axis float mouseX = input->getAxis("MOUSE_X") - 0.5f; float mouseY = input->getAxis("MOUSE_Y") - 0.5f; MVector3 position; position.x = camera->getPosition().x + mouseX * camera->getFov() * ratio; // multiply Y by the ortho Fov and the screen ratio position.y = camera->getPosition().y + mouseY * camera->getFov(); // multiply Y by the ortho Fov position.z = -10; mouseEntity->setPosition(position); } // reduce the heart bar when clicking if(barEntity) { if(input->onKeyDown("MOUSE_BUTTON1")) { MVector3 scale = barEntity->getScale(); if(scale.x > 0) { scale.x -= 0.1f; barEntity->setScale(scale); } } } } // update GUI scene GUIScene->update(); GUIScene->updateObjectsMatrices(); } MGame::update(); // call the default MGame update (update current scene physics, animations, script... as done by default) } // custom draw void MyGame::draw(void) { MGame::draw(); // call the default MGame draw (this will draw the current scene as done by default) // custom GUI MEngine * engine = MEngine::getInstance(); // get the engine instance MRenderingContext * render = engine->getRenderingContext(); // get the rendering context if(GUIScene) // check if the scene exist { if(GUIScene->getCamerasNumber() > 0) // check if there is a camera in the GUI scene { MOCamera * camera = GUIScene->getCameraByIndex(0); // get the first camera render->clear(M_BUFFER_DEPTH); // clear depth buffer (to not be affeted by the current scene) // draw GUI on top camera->enable(); GUIScene->draw(camera); } } }