/* ================== Main ================== */ int IndieLib() { //Sets the working path as the 'exe' directory. All resource paths are relative to this directory if (!WorkingPathSetup::setWorkingPathFromExe(NULL)) { std::cout<<"\nUnable to Set the working path !"; } // ----- IndieLib intialization ----- CIndieLib *mI = CIndieLib::instance(); if (!mI->init()) return 0; // ----- Surface loading ----- // Loading Background IND_Surface *mSurfaceBack = IND_Surface::newSurface(); if (!mI->_surfaceManager->add(mSurfaceBack, "../../resources/twist.jpg", IND_OPAQUE, IND_32)) return 0; // Loading draco IND_Surface *mSurfaceDraco = IND_Surface::newSurface(); if (!mI->_surfaceManager->add(mSurfaceDraco, "../../resources/draco.png", IND_ALPHA, IND_32)) return 0; // Font IND_Font *mFontSmall = IND_Font::newFont(); if (!mI->_fontManager->add(mFontSmall, "../../resources/font_small.png", "../../resources/font_small.xml", IND_ALPHA, IND_32)) return 0; // ----- Font creation ----- IND_Entity2d *mTextSmallWhite = IND_Entity2d::newEntity2d() ; mI->_entity2dManager->add(mTextSmallWhite); // Entity adding mTextSmallWhite->setFont(mFontSmall); // Set the font into the entity mTextSmallWhite->setLineSpacing(18); mTextSmallWhite->setCharSpacing(-8); mTextSmallWhite->setPosition(5, 5, 1); mTextSmallWhite->setAlign(IND_LEFT); // ----- Create a grid for Draco IND_Surface ----- mSurfaceDraco->setGrid(8, 8); // ----- Set the surfaces into 2d entities ----- // Creating 2d entity for the background IND_Entity2d *mBack = IND_Entity2d::newEntity2d(); mI->_entity2dManager->add(mBack); // Entity adding mBack->setSurface(mSurfaceBack); // Set the surface into the entity // Creating 2d entity for the draco IND_Entity2d *mDraco = IND_Entity2d::newEntity2d(); mI->_entity2dManager->add(mDraco); // Entity adding mDraco->setSurface(mSurfaceDraco); // Set the surface into the entity // ----- Changing the attributes of the 2d entities ----- // Background mBack->setHotSpot(0.5f, 0.5f); mBack->setPosition(400, 300, 0); mBack->setScale(1.7f, 1.7f); // Draco mDraco->setPosition(150, 50, 1); // ----- Main Loop ----- int mNumBlocksX = mSurfaceDraco->getBlocksX(); int mNumBlocksY = mSurfaceDraco->getBlocksY(); int mWidthBlock = mSurfaceDraco->getWidthBlock(); int mHeightBlock = mSurfaceDraco->getHeightBlock(); bool mShowGrid = 0; float mAngle = 0; IND_Timer *mTimer = new IND_Timer(); mTimer->start(); float mT; char mText [2048]; mText [0] = 0; while (!mI->_input->onKeyPress(IND_ESCAPE) && !mI->_input->quit()) { // ----- Input update ---- mI->_input->update(); // ----- Text ----- strcpy(mText, "Press space to see the grid in action. This is really cool, isn't it?"); mTextSmallWhite->setText(mText); // ----- Input ---- // Show / Hide the grid pressing "space" if (mI->_input->onKeyPress(IND_SPACE)) { if (mShowGrid){ mShowGrid = 0; }else{ mShowGrid = 1; } } // ----- Updating entities attributes ----- mAngle += 0.1f; mBack->setAngleXYZ(0, 0, mAngle); // Update grid vertices for making a "wave" effect mT = mTimer->getTicks() / 1000.0f; for (int i = 1; i < mNumBlocksX; i++) for (int j = 1; j < mNumBlocksY; j++) mSurfaceDraco->setVertexPos (j, i, (int) ((j * mHeightBlock + cosf (mT * 10 + (i + j) / 2) * 5)), (int) ((i * mWidthBlock + sinf (mT * 10 + (i + j) / 2) * 5))); // ----- Render ----- mI->_render->beginScene(); mI->_render->clearViewPort(60, 60, 60); mI->_entity2dManager->renderEntities2d(); if (mShowGrid) mI->_entity2dManager->renderGridAreas(0, 0, 0, 255); mI->_render->endScene(); } // ----- Free ----- mI->end(); return 0; }
/* ====================================== Listen for possible changes that the selected backdrop (the one where the cursos is over) can suffer ====================================== */ void Listener::ListenHoverBackDrop () { static int mMouseClickX; static int mMouseClickY; IND_Entity2d *mBackDropOver = mBackDropNodeOver->GetEntity(); // -------------------- Translate the selected backdrop image -------------------- static float mInitialPosX, mInitialPosY; float mNewPosX, mNewPosY; if (!mIsRotatingBackDrop && !mIsScalingBackDrop) { if (mI->_input->onMouseButtonPress(IND_MBUTTON_LEFT)) { mMouseClickX = (int) mPosBrushX; mMouseClickY = (int) mPosBrushY; mInitialPosX = mBackDropOver->getPosX(); mInitialPosY = mBackDropOver->getPosY(); } if (mI->_input->isMouseButtonPressed (IND_MBUTTON_LEFT)) { mNewPosX = mInitialPosX + (mPosBrushX - mMouseClickX); mNewPosY = mInitialPosY + (mPosBrushY - mMouseClickY); mBackDropOver->setPosition (mNewPosX, mNewPosY, mBackDropOver->getPosZ()); mIsTranslatingBackDrop = true; } if (mI->_input->onMouseButtonRelease(IND_MBUTTON_LEFT)) { mIsTranslatingBackDrop = false; } } // -------------------- Translate pixel by pixel -------------------- if (mI->_input->onKeyPress (IND_F)) { mBackDropOver->setPosition (mBackDropOver->getPosX() - 1, mBackDropOver->getPosY(), mBackDropOver->getPosZ()); } if (mI->_input->onKeyPress (IND_G)) { mBackDropOver->setPosition (mBackDropOver->getPosX() + 1, mBackDropOver->getPosY(), mBackDropOver->getPosZ()); } if (mI->_input->onKeyPress (IND_C)) { mBackDropOver->setPosition (mBackDropOver->getPosX(), mBackDropOver->getPosY() - 1, mBackDropOver->getPosZ()); } if (mI->_input->onKeyPress (IND_V)) { mBackDropOver->setPosition (mBackDropOver->getPosX(), mBackDropOver->getPosY() + 1, mBackDropOver->getPosZ()); } // -------------------- Scale the selected backdrop image -------------------- static float mInitialScale; float mNewScale; static int mInitialRegionX, mInitialRegionY; int mNewRegionX, mNewRegionY; if (!mIsRotatingBackDrop && !mIsTranslatingBackDrop) { if (mI->_input->onMouseButtonPress (IND_MBUTTON_RIGHT) && mI->_input->isKeyPressed (IND_LSHIFT)) { mMouseClickX = (int) mPosBrushX; if (!mBackDropOver->ifWrap()) { mInitialScale = mBackDropOver->getScaleX(); } else { mInitialRegionX = mBackDropOver->getRegionWidth(); mInitialRegionY = mBackDropOver->getRegionHeight(); } } if (mI->_input->isMouseButtonPressed (IND_MBUTTON_RIGHT) && mI->_input->isKeyPressed(IND_LSHIFT)) { mIsScalingBackDrop = true; if (!mBackDropOver->ifWrap()) { mNewScale = mInitialScale + ((mPosBrushX - mMouseClickX) / 1000); if (mNewScale < 0.05f) mNewScale = 0.1f; mBackDropOver->setScale (mNewScale, mNewScale); } else { mNewRegionX = mInitialRegionX + ((int) mPosBrushX - mMouseClickX); mNewRegionY = mInitialRegionY + ((int) mPosBrushX - mMouseClickX); mBackDropOver->setRegion (0, 0, (int) mNewRegionX, (int) mNewRegionY); } } if (mI->_input->onMouseButtonRelease (IND_MBUTTON_RIGHT)) { mIsScalingBackDrop = false; } } // -------------------- Rotate the selected backdrop image -------------------- static float mInitialAngle; float mNewAngle; if (!mIsTranslatingBackDrop && !mIsScalingBackDrop) { if (mI->_input->onMouseButtonPress (IND_MBUTTON_RIGHT)) { mMouseClickX = (int) mPosBrushX; mInitialAngle = mBackDropOver->getAngleZ(); } if (mI->_input->isMouseButtonPressed (IND_MBUTTON_RIGHT)) { mNewAngle = mInitialAngle + (mPosBrushX - mMouseClickX); mBackDropOver->setAngleXYZ (0, 0, mNewAngle); mIsRotatingBackDrop = true; } if (mI->_input->onMouseButtonRelease (IND_MBUTTON_RIGHT)) { mIsRotatingBackDrop = false; } } // -------------------- Flip backdrop image -------------------- if (mI->_input->onKeyPress(IND_T)) { (mBackDropOver->getMirrorX() == true) ? mBackDropOver->setMirrorX (false) : mBackDropOver->setMirrorX (true); } if (mI->_input->onKeyPress(IND_Y)) { (mBackDropOver->getMirrorY() == true) ? mBackDropOver->setMirrorY (false) : mBackDropOver->setMirrorY (true); } // -------------------- Transparency -------------------- if (mI->_input->isKeyPressed (IND_U, 5)) { if (mBackDropOver->getTransparency() > 0) mBackDropOver->setTransparency (mBackDropOver->getTransparency() - 1); } if (mI->_input->isKeyPressed (IND_I, 5)) { if (mBackDropOver->getTransparency() < 255) mBackDropOver->setTransparency (mBackDropOver->getTransparency() + 1); } // -------------------- Tiling -------------------- if (mI->_input->onKeyPress(IND_L) && !mI->_input->isKeyPressed (IND_LCTRL)) { (mBackDropOver->ifWrap() == true) ? mBackDropOver->toggleWrap (false) : mBackDropOver->toggleWrap (true); mBackDropOver->setRegion (0, 0, mBackDropOver->getSurface()->getWidth(), mBackDropOver->getSurface()->getHeight()); } // -------------------- Tinting -------------------- if (mI->_input->isKeyPressed(IND_SPACE)) { mBackDropOver->setTint ((BYTE) mMouseX, (BYTE) mMouseY, (BYTE) mMouseX); mIsTintingBackDrop = true; } if (mI->_input->onKeyRelease(IND_SPACE)) { mIsTintingBackDrop = false; } // -------------------- Z ordering -------------------- if (mI->_input->onKeyPress(IND_Z)) { mBackDropOver->setPosition (mBackDropOver->getPosX(), mBackDropOver->getPosY(), mBackDropOver->getPosZ() - 1); } if (mI->_input->onKeyPress(IND_X)) { mBackDropOver->setPosition (mBackDropOver->getPosX(), mBackDropOver->getPosY(), mBackDropOver->getPosZ() + 1); } // -------------------- Clone -------------------- if (mI->_input->isKeyPressed(IND_LSHIFT) && mI->_input->onMouseButtonPress (IND_MBUTTON_LEFT)) { mMap->CloneNode (mBackDropNodeOver); // Clone node } // -------------------- Delete -------------------- if (mI->_input->onKeyPress(IND_DELETE)) { mMap->DeleteNode (mBackDropNodeOver); // Erase the node from the map vector } }
int IndieLib() { // ----- IndieLib intialization ----- CIndieLib *mI = CIndieLib::instance(); if (!mI->init()) return 0; // ----- Get Window Dimensions int winWidth = mI->_window->getWidth(); int winHeight = mI->_window->getHeight(); srand(static_cast<unsigned int>(time(0))); // ----- Surface loading ----- IND_Surface *mSurfaceBack = IND_Surface::newSurface(); if (!mI->_surfaceManager->add(mSurfaceBack, "../SpaceGame/resources/Backgrounds/18.jpg", IND_OPAQUE, IND_32)) return 0; /*IND_Animation* mTestA = IND_Animation::newAnimation(); if (!mI->_animationManager->addToSurface(mTestA, "resources/animations/dust.xml", IND_ALPHA, IND_32, 255, 0, 255)) return 0; mTestA->getActualFramePos(0);*/ // Loading 2D Entities // Background IND_Entity2d* mBack = IND_Entity2d::newEntity2d(); mI->_entity2dManager->add(mBack); mBack->setSurface(mSurfaceBack); mBack->setScale((float)winWidth / mSurfaceBack->getWidth(), (float)winHeight / mSurfaceBack->getHeight()); Controls* controls = new Controls(); controls->loadSettings(); ErrorHandler* error = new ErrorHandler(); error->initialize(mI); Hud* mHud = new Hud(); mHud->createHud(mI); Menu* mMenu = new Menu(); mMenu->createMenu(mI); Save* quickSave = new Save(); if (!SoundEngine::initialize()) { error->writeError(200, 100, "Error", "SoundEngine"); } vector<Planet*> mPlanets; Ship* mShip = NULL; bool loadSave = false; float mDelta = 0.0f; IND_Timer* mTimer = new IND_Timer; mTimer->start(); while (!mI->_input->onKeyPress(IND_ESCAPE) && !mI->_input->quit() && !mMenu->isExit()) { // get delta time mDelta = mI->_render->getFrameTime() / 1000.0f; if (mI->_input->isKeyPressed(controls->getMenu())) { mMenu->show(); SoundEngine::getSoundEngine()->setAllSoundsPaused(true); } if (!mMenu->isHidden()) { mMenu->updateMenu(mHud, quickSave, mPlanets, mShip); loadSave = mHud->getLoadingText()->isShow(); } else { if (loadSave) { mDelta = 0.0; loadSave = false; SoundEngine::getSoundEngine()->setAllSoundsPaused(true); mHud->getLoadingText()->setShow(false); quickSave->loadSave(mI, mShip, mPlanets); mHud->showHud(); } if (mShip != NULL) { if (mI->_input->onKeyPress(controls->getQuickSave())) { quickSave->makeSave(mI, mShip, mPlanets); } mHud->updateHud(mShip); if (mI->_input->onKeyPress(controls->getQuickLoad())) { deleteObjects(mHud, mShip, mPlanets); loadSave = true; } if (mShip->isDestroyed()) { SoundEngine::getSoundEngine()->setAllSoundsPaused(true); mHud->updateGameOverText(mShip->getScore()); deleteObjects(mHud, mShip, mPlanets); mHud->getLoadingText()->setShow(false); mHud->getGameOverText()->setShow(true); mMenu->show(); } if(mShip!=NULL) { //----- Collisions ----- checkShipPlanetsCollisions(mI, mPlanets, mShip); checkBulletsPlanetsCollisions(mI, mPlanets, mShip); checkBulletsShipCollisions(mI, mPlanets, mShip); //----- Movement update ----- mShip->updateShip(controls, mDelta); if ((mTimer->getTicks() / 1000.0f) >= 3.0f) { mTimer->start(); mPlanets.at(rand() % mPlanets.size())->addSatellite(); } for (vector<Planet*>::iterator it = mPlanets.begin(); it != mPlanets.end(); ++it) { (*it)->updatePlanet(mDelta, (mShip->getPosX() + 0.25f * mShip->getHeight() * cos(mShip->getAngleZRadian())), (mShip->getPosY() - 0.25f * mShip->getHeight() * sin(mShip->getAngleZRadian()))); } } } } //mI->_render->showFpsInWindowTitle(); mI->_input->update(); mI->_render->beginScene(); mI->_entity2dManager->renderEntities2d(); mI->_render->endScene(); } // ----- Free ----- delete controls; delete error; delete mHud; delete mMenu; delete quickSave; mI->_surfaceManager->remove(mSurfaceBack); mI->_entity2dManager->remove(mBack); deleteObjects(mHud, mShip, mPlanets); mI->end(); return 0; }
/* ================== Main ================== */ int IndieLib() { // ----- IndieLib intialization ----- CIndieLib *mI = CIndieLib::instance(); if (!mI->init()) return 0; // ----- Surface loading ----- // Loading Background IND_Surface *mSurfaceBack = new IND_Surface(); if (!mI->_surfaceManager->add(mSurfaceBack, "../../resources/twist.jpg", IND_OPAQUE, IND_32)) return 0; // Loading Beettleship IND_Surface *mSurfaceBeetleship = new IND_Surface(); if (!mI->_surfaceManager->add(mSurfaceBeetleship, "../../resources/beetleship.png", IND_ALPHA, IND_32)) return 0; // Loading Octopus IND_Surface *mSurfaceOctopus = new IND_Surface(); if (!mI->_surfaceManager->add(mSurfaceOctopus, "../../resources/octopus.png", IND_ALPHA, IND_32)) return 0; // Loading Planet IND_Surface *mSurfacePlanet = new IND_Surface(); if (!mI->_surfaceManager->add(mSurfacePlanet, "../../resources/planet.png", IND_ALPHA, IND_32)) return 0; // Font IND_Font *mFontSmall = new IND_Font() ; if (!mI->_fontManager->add(mFontSmall, "../../resources/font_small.png", "../../resources/font_small.xml", IND_ALPHA, IND_32)) return 0; // ----- Font creation ----- IND_Entity2d *mTextSmallWhite = new IND_Entity2d(); mI->_entity2dManager->add(mTextSmallWhite); // Entity adding mTextSmallWhite->setFont(mFontSmall); // Set the font into the entity mTextSmallWhite->setLineSpacing(18); mTextSmallWhite->setCharSpacing(-8); mTextSmallWhite->setPosition(5, 5, 1); mTextSmallWhite->setAlign(IND_LEFT); // ----- Set the surfaces into 2d entities ----- // Creating 2d entity for the background IND_Entity2d *mBack = new IND_Entity2d(); mI->_entity2dManager->add(mBack); // Entity adding mBack->setSurface(mSurfaceBack); // Set the surface into the entity // Creating 2d entity for the bettleship IND_Entity2d *mBeetleship = new IND_Entity2d(); mI->_entity2dManager->add(mBeetleship); // Entity adding mBeetleship->setSurface(mSurfaceBeetleship); // Set the surface into the entity // Creating 2d entity for the octopus IND_Entity2d *mOctopus = new IND_Entity2d(); mI->_entity2dManager->add(mOctopus); // Entity adding mOctopus->setSurface(mSurfaceOctopus); // Set the surface into the entity // Creating 2d entity for the planet IND_Entity2d *mPlanet = new IND_Entity2d(); mI->_entity2dManager->add(mPlanet); // Entity adding mPlanet->setSurface(mSurfacePlanet); // Set the surface into the entity // ----- Changing the attributes of the 2d entities ----- mBack->setHotSpot(0.5f, 0.5f); mBack->setPosition(400, 300, 0); mBack->setScale(1.7f, 1.7f); mBeetleship->setHotSpot(0.5f, 0.5f); mOctopus->setMirrorX(true); mOctopus->setHotSpot(0.5f, 0.5f); mPlanet->setHotSpot(0.5f, 0.5f); // ----- Main Loop ----- float mAngle = 0; float mPos = 400; int mSpeed = 200; float mDelta; char mText [2048]; mText [0] = 0; while (!mI->_input->onKeyPress (IND_ESCAPE) && !mI->_input->quit()) { // ----- Input update ----- mI->_input->update(); // ----- Text ----- strcpy (mText, "Use the mouse for moving the planet\n"); strcat (mText, "Use left and right arrow keys for moving the ships\n"); strcat (mText, "Press CTRL + X or ESC key to quit"); mTextSmallWhite->setText(mText); // ----- Input ----- mDelta = mI->_render->getFrameTime() / 1000.0f; // Move entities when pressing right if (mI->_input->isKeyPressed(IND_KEYRIGHT)){ mPos += mSpeed * mDelta; } // Move entities when pressing left if (mI->_input->isKeyPressed(IND_KEYLEFT)){ mPos -= mSpeed * mDelta; } // If CTRL + X pressed then exit if (mI->_input->isKeyPressed(IND_LCTRL) && mI->_input->isKeyPressed(IND_X)){ mI->_render->endScene(); mI->end(); exit(0); } mAngle += (mSpeed / 4) * mDelta; // ----- Updating entities attributes ----- // Back mBack->setAngleXYZ(0, 0, mAngle); // Beetle mBeetleship->setPosition((float) mPos, 140, 1); mBeetleship->setAngleXYZ(0, 0, (float) mPos); // Planet mPlanet->setPosition((float) mI->_input->getMouseX(), 300, 2); // Octopus mOctopus->setPosition(800 - (float) mPos, 480, 3); // ----- Render ----- mI->_render->beginScene(); mI->_entity2dManager->renderEntities2d(); mI->_render->endScene(); } // ----- Free ----- mI->end(); return 0; }