Exemplo n.º 1
0
int main(int argc, char *argv[])
{
    try
    {
        // Démarrer le crawler
        if(argc == 1)
        {
            logger.initialize();
            database.connect();
            Crawler crawler;
            crawler.start();
        }

        // Effectuer une recherche
        else if(argc == 2)
        {
            logger.initialize("search");
            database.connect();
            Search::getResult(argv[1]);
        }
    }

    catch(exception& e)
    {
        logger.log(Logger::ALERT, e.what());
    }

    return 0;
}
Exemplo n.º 2
0
int main(int argc, char **argv)
{
    bool help     = false;
    bool quiet    = false;
    bool version  = false;
    bool interactive   = false;
    std::string infile = "";

    auto cli = (
        option("-h", "--help").set(help).doc("Print this help message."),
        option("-v", "--version").call([]{std::cout << "Crawler version 0.0.1\n"; exit(0);}).doc("show version"),
        option("-i", "--interactive").set(interactive).doc("Use the interactive mode"),
        option("-q", "--quiet").set(quiet).doc("Silent output"),
        option("-f", "--file") & value("config file", infile)
    );

    // Generate Usage
    if(!parse(argc, argv, cli) || help || !isValid(interactive, infile)) {
        std::cout << make_man_page(cli, argv[0]);
        return (0);
    }

    Crawler *crawler = new Crawler(quiet);
    if (interactive) {
      crawler->setInteractive(interactive);
    } else {
      crawler->setConfig(infile);
    }

    return crawler->run();
}
Exemplo n.º 3
0
void * threadProcess (void * _info) // the process that the threads will use to request, start, and finish crawler jobs
{
    pair<JobManager*,int> * info = (pair<JobManager*,int> *)_info;
    JobManager * jobManager = info->first;
    int threadIndex = info->second;
    queue<JobInfo *> * qP = jobManager->getQueuePointer();
    JobInfo * nextJob;
    
    // acquire a lock on the queue
  while(true)
  {
    pthread_mutex_lock(jobManager->getQueueLock());
    // if jobs in queue 
    if (qP->size() > 0)
    {
      // request next job
      nextJob = qP->front();
      // pop the job from the queue
      qP->pop();
    }
    else
    {
      // set nextJob pointer to 0 so we know we didn't get any jobs
      // note that if the job was cancelled, the pointer will already be set
      // to zero
      nextJob = 0;
    }
    // unlock the queue
    pthread_mutex_unlock(jobManager->getQueueLock());
    // start up a crawler and run the job if we got one
    if (nextJob)
    {
      int prevCancelType;
      // start up the job
      nextJob->setCurrentThreadIndex(threadIndex);
      if (nextJob->getStatus() != CANCELLED)
      {
        pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &prevCancelType);
        nextJob->setStatus(RUNNING);
        Crawler * crawler = new Crawler();
        crawler->crawl(nextJob);
        pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &prevCancelType);
        delete crawler;
      }
      nextJob->setCurrentThreadIndex(-1);
    } 
    // wait a little bit before repeating to ease strain on CPU
    sleep(1);
  } // repeat
}
Exemplo n.º 4
0
int main() {
  Crawler *crawler = new FileCrawler();
  /* obtain a list of documents */
  std::unordered_map<int, Document*> doc_map = crawler->fetch_documents();
  /* index the documents */
  std::unordered_map<std::string, DocIdList> index = Indexer::obtain_docid_index(doc_map);
  /* run search queries */
  std::string query;
  while(1) {
    std::cout << "Enter search terms and press enter" << std::endl;
    std::getline(std::cin, query);
    std::istringstream iss(query);
    std::string term;
    std::list<DocIdList*> found;
    std::list<std::string> not_found;
    /* find the document id list for each search term */
    while(iss >> term) {
      auto res = index.find(term);
      if(res == index.end()) {
        not_found.push_back(term);
      }
      else {
        found.push_back(&res->second);
      }
    }
    /* intersect the document id lists */
    auto it = found.begin();
    DocIdList out(**it);
    while(it != found.end()) {
      
    }
    /* present the results */
    if(found.empty()) {
      std::cout << "No results!" << std::endl; 
    }
    else {
      std::cout << "Found " << out.get_docids().size() << " results" << std::endl;
    }
    std::cout << std::endl;
  }
  return 0;
}
Exemplo n.º 5
0
/**
 * Implementation of run
 */
void Crawler::run(SharedQueue *sq,const char* dataDir)
{
    while (true)
    {
        while ( (*sq).sharedQueue.empty() )
        {
            sleep(1); // TODO
        }

        QueueNode qNode = (*sq).sharedQueue.front();

        pthread_mutex_lock(&sq->lock);
        (*sq).sharedQueue.pop();
        pthread_mutex_unlock(&sq->lock);

        Crawler newCrawler;

        newCrawler.addURLStart( qNode.urlstart );
        if (qNode.count != -1)
            newCrawler.addCrawlerCount( qNode.count );
        for(int i=0;i<qNode.dontcrawlnr;++i)
            newCrawler.dontCrawl( qNode.dontcrawl[i] );
        newCrawler.addDataDirectory(dataDir);

        newCrawler.start();
    }
}
Exemplo n.º 6
0
int main()
{

	Crawler* crawler = new Crawler();
	crawler->InitURL();
	//crawler->downloader();
	crawler->parseLink();
        //HOW TO CALL HTML_Text MODULE
        /*
        
        string file = "Search.html";
	HTML_PARSER* hp = new HTML_PARSER(file.c_str());
	hp->loadData();
	hp->parseText();
        
        */
        
        /* usage XmlWriter
        Node* root = new Node("Index");
	root->addAttributesToStartTag();
	

	Node* wordNode1 = new Node("word");
	wordNode1->setAttributes("id", "1");
	wordNode1->setAttributes("url", "www.google.com");
	wordNode1->addAttributesToStartTag();

	Node* wordNode2 = new Node("word");
	wordNode2->setAttributes("id", "2");
	wordNode2->setAttributes("url", "www.facebook.com");
	wordNode2->addAttributesToStartTag();

	Node* wordNode3 = new Node("word");
	wordNode3->setAttributes("id", "3");
	wordNode3->setAttributes("url", "www.cnet.com");
	wordNode3->addAttributesToStartTag();

	Node* wordNode4 = new Node("word");
	wordNode4->setAttributes("id", "4");
	wordNode4->setAttributes("url", "www.wired.com");
	wordNode4->addAttributesToStartTag();

	Node* wordNode5 = new Node("word");
	wordNode5->setAttributes("id", "5");
	wordNode5->setAttributes("url", "www.news.com");
	wordNode5->addAttributesToStartTag();

	Node* wordSubNode1 = new Node("sub-word");
	wordSubNode1->setAttributes("id", "1");
	wordSubNode1->setAttributes("url", "www.speakingtree.com");
	wordSubNode1->addAttributesToStartTag();

	wordNode5->addChild(wordSubNode1);


	root->addChild(wordNode1);
	root->addChild(wordNode2);
	root->addChild(wordNode3);
	root->addChild(wordNode4);
	root->addChild(wordNode5);

	XmlWriter* xw = new XmlWriter("data.xml");
	xw->dump(root);
        
        */
        
        /*USAGE OF THE XML DATABASE WHICH WILL CONTAIN FIELDS AS ID WORD AND DOC LIST
        
        
       CSVLoader* loader = new CSVLoader("C:\\Users\\PRIYASH_11\\Downloads\\data\\Word.csv");
	loader->Load();
	vector<string>v = loader->getData();
	vector<Word>w;
	for (int i = 0; i < v.size()-2; i++)
	{
		Word word;
		docFreq df;
		word.id = atoi(v[i].c_str());
		word.word = v[i + 1];
		int j = i + 2;
		while(v[j] != "EOL")
		{
			string s = v[j];
			df.freq = atoi(s.substr(0, s.find(":")).c_str());
			df.docID = s.substr(s.find(":") + 1);
			word.docIDs.push_back(df);
			j++;
		}
		w.push_back(word);
		i = j;
	}



	for (auto i : w)
	{
		cout << i.id << " " << i.word<<" ";
		for (auto j : i.docIDs)
		{
			cout << j.freq << " "<<j.docID<<" ";
		}
		cout << endl;
		
	}
	*/
	
	
	
	
	/*
	USAGE OF JSON PARSER
	
	Json* json = new Json("json_data.json");
	json->read();
	json->addWord("Diamond", 2, 16);
	
	
	*/
	return 0;
}
Exemplo n.º 7
0
/*
================================================================================

Game::Draw

    Gets called once per frame to draw the current snapshot of the game.

================================================================================
*/
void Game::Draw()
{
    // clear the screen
    SDL_SetRenderDrawColor(mRenderer, 0, 0, 0, 255);
    SDL_RenderClear(mRenderer);
	
	if (mBackground)
	{
		Render(mBackground->GetRenderable(), &mBackground->GetRect(), SDL_FLIP_NONE);
	}
	if (mForeground)
	{
		//Render(mForeground->GetRenderable(), &mForeground->GetRect(), SDL_FLIP_NONE);
	}
	if (mFlagPole)
	{
		Render(mFlagPole->GetRenderable(), &mFlagPole->GetRect(), SDL_FLIP_NONE);
	}

    //
    // draw the grid
    //
    if (mGrid)
	{
        int tileWidth = mGrid->TileWidth();
        int tileHeight = mGrid->TileHeight();
        GG::Rect tileRect(0, 0, tileWidth, tileHeight);
        for (int y = 0; y < mGrid->NumRows(); y++)
		{
            for (int x = 0; x < mGrid->NumCols(); x++)
			{
                const GG::Renderable* renderable = mGrid->GetTile(y, x)->GetRenderable();
                if (renderable)
				{
                    Render(renderable, &tileRect, SDL_FLIP_NONE);
                }
                tileRect.x += tileWidth;
            }
            tileRect.y += tileHeight;
            tileRect.x = 0;
        }
    }

	// Draw the collision rectangles
	if (rectVisible)
	{
		SDL_SetRenderDrawColor(mRenderer, 255, 0, 0, 255);
		SDL_RenderFillRect(mRenderer, &mRobot->GetBottomTileRect());
		SDL_SetRenderDrawColor(mRenderer, 150, 0, 0, 255);
		SDL_RenderFillRect(mRenderer, &mRobot->GetTopTileRect());
		SDL_SetRenderDrawColor(mRenderer, 255, 255, 0, 255);
		SDL_RenderFillRect(mRenderer, &mRobot->GetCollisonRect());
		for (auto coinIt = mCoins.begin(); coinIt != mCoins.end(); ++coinIt)
		{
			Coin* coin = *coinIt;
			SDL_RenderFillRect(mRenderer, &coin->GetRect());
		}
		for (auto crawlerIt = mCrawlers.begin(); crawlerIt != mCrawlers.end(); ++crawlerIt)
		{
			Crawler* crawler = *crawlerIt;
			SDL_RenderFillRect(mRenderer, &crawler->GetCollisionRect());
		}
		for (auto meteorIt = mMeteors.begin(); meteorIt != mMeteors.end(); ++meteorIt)
		{
			Meteor* meteor = *meteorIt;
			SDL_RenderFillRect(mRenderer, &meteor->GetRect());
		}
		SDL_SetRenderDrawColor(mRenderer, 0, 0, 255, 255);
		for (auto crawlerIt = mCrawlers.begin(); crawlerIt != mCrawlers.end(); ++crawlerIt)
		{
			Crawler* crawler = *crawlerIt;
			SDL_RenderFillRect(mRenderer, &crawler->GetTileRect());
		}
	}

	//
    // draw the robot
    //
	if (mRobot)
	{
		Render(mRobot->GetRenderable(), &mRobot->GetRect(), mRobot->GetDirection()?SDL_FLIP_HORIZONTAL:SDL_FLIP_NONE);
	}

	//
    // draw the coins
    //
	std::list<Coin*>::iterator coinIt = mCoins.begin();
    for ( ; coinIt != mCoins.end(); ++coinIt)
	{
        Coin* coin = *coinIt;
        Render(coin->GetRenderable(), &coin->GetRect(), SDL_FLIP_NONE);
    }

	//
    // draw the mushrooms
    //
	std::list<Layer*>::iterator mushIter = mMushrooms.begin();
    for ( ; mushIter != mMushrooms.end(); ++mushIter)
	{
        Layer* mushroom = *mushIter;
        Render(mushroom->GetRenderable(), &mushroom->GetRect(), SDL_FLIP_NONE);
    }

	//
    // draw the crawlers
    //
    for (auto crawlerIt = mCrawlers.begin(); crawlerIt != mCrawlers.end(); ++crawlerIt)
	{
        Crawler* crawler = *crawlerIt;
		if (crawler->GetDirection() == 1)
		{
			Render(crawler->GetRenderable(), &crawler->GetRect(), SDL_FLIP_HORIZONTAL);
		}
		else
		{
			Render(crawler->GetRenderable(), &crawler->GetRect(), SDL_FLIP_NONE);
		}
    }

    //
    // draw the explosions
    //
    std::list<Explosion*>::iterator it = mExplosions.begin();
    for ( ; it != mExplosions.end(); ++it)
	{
        Explosion* boom = *it;
        Render(boom->GetRenderable(), &boom->GetRect(), SDL_FLIP_NONE);
    }

	//
    // draw the meteors
    //
    std::list<Meteor*>::iterator metIt = mMeteors.begin();
    for ( ; metIt != mMeteors.end(); ++metIt)
	{
        Meteor* meteor = *metIt;
        Render(meteor->GetRenderable(), &meteor->GetRect(), SDL_FLIP_NONE);
    }

	// Draw the points label
	if (mPointsLabel)
	{
		Render(mPointsLabel->GetRenderable(), &mPointsLabel->GetRect(), SDL_FLIP_NONE);
	}

	// Draw the lives label
	if (mLivesLabel)
	{
		Render(mLivesLabel->GetRenderable(), &mLivesLabel->GetRect(), SDL_FLIP_NONE);
	}

    // display everything we just drew
    SDL_RenderPresent(mRenderer);
}
Exemplo n.º 8
0
/*
================================================================================

Game::Update

    Gets called once per frame to perform game logic.
    The parameter dt is the time elapsed since last frame (in seconds).

================================================================================
*/
void Game::Update(float dt)
{
	// Update the robot
	if (mRobot)
	{
		// If the robot has reached the last scene
		// disable its controls and play game over animation
		if (mScene == 6 && !mRobot->GetJumping() && !mRobot->GetFalling())
		{
			mRobot->SetAutoPilot(true);
		}
		mRobot->Update(dt);
	}

	// Update the coins
	std::list<Coin*>::iterator coinIt = mCoins.begin();
	while (coinIt != mCoins.end())
	{
		Coin *coin = *coinIt;
		// Check if the robot collides with the coin
		if (mRobot->GetCollisonRect().y < coin->GetRect().y + coin->GetRect().h)
		{
			if ((mRobot->GetCollisonRect().x < coin->GetRect().x + coin->GetRect().w)
				&& (mRobot->GetCollisonRect().x + mRobot->GetCollisonRect().w > coin->GetRect().x))
			{
				if ((mRobot->GetCollisonRect().y < coin->GetRect().y + coin->GetRect().h)
					&& (mRobot->GetCollisonRect().y + mRobot->GetCollisonRect().h > coin->GetRect().y))
				{
					//I have found that the sound is delayed...So I start it a bit earlier than the actualy delete of the Coin
					if (coin->GetSoundDelay() == 0)
					{
						// You get 5 points!
						mPoints += 5;
						Mix_PlayChannel(-1, mCoinSound, 0);
						coin->SetSoundDelay(1);
					}
					//Once it has run through 4 times then it Deletes the coin
					else if (coin->GetSoundDelay() == 5)
					{
						coinIt = mCoins.erase(coinIt); // remove the entry from the list and advance iterator
						delete coin;
						coin = NULL;
					}
					else
					{
						coin->SetSoundDelay(1);
					}
				}
				else
				{
					coin->Update(dt);
					++coinIt;
				}
			}
			else
			{
				coin->Update(dt);
				++coinIt;
			}
		}
		else
		{
			coin->Update(dt);
			++coinIt;
		}
			
	}

	// update all crawlers
	std::list<Crawler*>::iterator crawlerIt = mCrawlers.begin();
    while (crawlerIt != mCrawlers.end())
	{
		Crawler *crawler =   *crawlerIt;
		if (crawler->GetState() == Crawler::CRAWLER_DEAD)
		{
			crawlerIt = mCrawlers.erase(crawlerIt); // remove the entry from the list and advance iterator
            delete crawler;              // delete the object
		}
		else
		{
			// If the robot is falling from a jump or just falling
			if (mRobot->GetVerticalVelocity() > 0.0 && (mRobot->GetJumping() || mRobot->GetFalling()))
			{
				// Check if the robot has started squashing the poor crawler
				if (mRobot->GetCollisonRect().x + mRobot->GetCollisonRect().w > crawler->GetCollisionRect().x && 
				mRobot->GetCollisonRect().x < crawler->GetCollisionRect().x + crawler->GetCollisionRect().w)
				{
					if (mRobot->GetCollisonRect().y + mRobot->GetCollisonRect().h > crawler->GetCollisionRect().y &&
						mRobot->GetCollisonRect().y < crawler->GetCollisionRect().y)
					{
						if (crawler->GetState() != CrawlerWeak::CRAWLER_DYING)
						{
							// You get 25 points!
							mPoints += 25;
							if (crawler->IsJumpedOn())
							{
								Mix_PlayChannel(-1, mStompSound, 0);
								mRobot->Bounce(-400, false);
								crawler->SetState(Crawler::CRAWLER_DYING);
							}
							else
							{
								Mix_PlayChannel(-1, mStompSoundNoKill, 0);
								mRobot->Bounce(-400, false);
								crawler->SetState(Crawler::CRAWLER_DYING);
							}
						}
					}
				}
			}
			// If the robot runs into a crawler, the robot must die (but it should not falling onto it from above)
			else if (mRobot->GetCollisonRect().x + mRobot->GetCollisonRect().w > crawler->GetCollisionRect().x && 
				mRobot->GetCollisonRect().x < crawler->GetCollisionRect().x + crawler->GetCollisionRect().w)
			{
				if (mRobot->GetCollisonRect().y + mRobot->GetCollisonRect().h > crawler->GetCollisionRect().y && 
				mRobot->GetCollisonRect().y < crawler->GetCollisionRect().y + crawler->GetCollisionRect().h)
				{
					if (!mRobot->IsDead() && mRobot->GetVerticalVelocity() == -850.0f && crawler->GetState() != CrawlerWeak::CRAWLER_DYING)
					{
						// You lose a life:(
						mRobot->SetLives(mRobot->GetLives() - 1);
						Mix_PlayChannel(-1, mDieSound, 0);
						// Stop the background music
						Mix_HaltMusic();
						if (mRobot->GetLives() == 0)
						{
							printf("\nGame over music is being played!");
							Mix_VolumeMusic(32);
							Mix_PlayMusic(mBadGameOverMusic, 0);
							SetEntitiesGrayscale(true);
						}
						mRobot->Bounce(-400, true);             // kill the robot
					}
				}
			}
			crawler->Update(dt);
			++crawlerIt;
		}
    }

    //
    // update the explosions
    //
    std::list<Explosion*>::iterator it = mExplosions.begin();
    while (it != mExplosions.end())
	{

        Explosion* entity = *it;        // get a pointer to this explosion

        if (entity->IsFinished())
		{
            it = mExplosions.erase(it); // remove the entry from the list and advance iterator
            delete entity;              // delete the object
        } 
		else
		{
            entity->Update(dt);     // update the entity
            ++it;                   // advance list iterator
        }
    }

	//
    // update the meteors
    //
    std::list<Meteor*>::iterator metIt = mMeteors.begin();
    while (metIt != mMeteors.end())
	{
        Meteor* entity = *metIt;        // get a pointer to this meteor

		// If the meteor has either reached the ground, destroy it with an explosion
        if (entity->GetRect().y > mScrHeight-32-64) 
		{
			if (!mRobot->IsDead())
			{
				Mix_PlayChannel(-1, mThudSound, 0);
			}
			Explosion* boom = new Explosion(entity->GetRect().x + entity->GetRect().w / 2, entity->GetRect().y + entity->GetRect().h / 2);
            mExplosions.push_back(boom);
            metIt = mMeteors.erase(metIt); // remove the entry from the list and advance iterator
            delete entity;              // delete the object
        }
		// If the meteor has hit the robot from the top, destroy it with an explosion and also kill the robot
		else if (entity->GetRect().y + entity->GetRect().h > mRobot->GetCollisonRect().y &&
				entity->GetRect().y < mRobot->GetCollisonRect().y + mRobot->GetCollisonRect().h &&
				entity->GetRect().x + entity->GetRect().w > mRobot->GetCollisonRect().x &&
				entity->GetRect().x < mRobot->GetCollisonRect().x + mRobot->GetCollisonRect().w && !mRobot->IsDead())
		{
			// You lose a life:(
			mRobot->SetLives(mRobot->GetLives() - 1);
			Mix_PlayChannel(-1, mThudSound, 0);
			Mix_PlayChannel(-1, mDieSound, 0);
			// Stop the background music
			Mix_HaltMusic();
			if (mRobot->GetLives() == 0)
			{
				Mix_VolumeMusic(32);
				Mix_PlayMusic(mBadGameOverMusic, 0);
				SetEntitiesGrayscale(true);
			}
			mRobot->Bounce(-400, true);             // kill the robot
			Explosion* boom = new Explosion(entity->GetRect().x + entity->GetRect().w / 2, entity->GetRect().y + entity->GetRect().h / 2);
			mExplosions.push_back(boom);
			metIt = mMeteors.erase(metIt); // remove the entry from the list and advance iterator
			delete entity;              // delete the object
		}
		else
		{
            entity->Update(dt);     // update the entity
            ++metIt;                   // advance list iterator
        }
    }

	//
    // update the mushrooms
    //
    std::list<Layer*>::iterator mushIt = mMushrooms.begin();
    while (mushIt != mMushrooms.end())
	{
        Layer* entity = *mushIt;
		// If the robot collects the mushroom, it gets an extra life!
		if (entity->GetRect().y + entity->GetRect().h > mRobot->GetCollisonRect().y &&
			entity->GetRect().y < mRobot->GetCollisonRect().y + mRobot->GetCollisonRect().h &&
			entity->GetRect().x + entity->GetRect().w > mRobot->GetCollisonRect().x &&
			entity->GetRect().x < mRobot->GetCollisonRect().x + mRobot->GetCollisonRect().w && !mRobot->IsDead())
		{
			mRobot->SetLives(mRobot->GetLives() + 1);
			SetFlashesNeeded(2);
			Mix_PlayChannel(-1, mOneupSound, 0);
			mushIt = mMushrooms.erase(mushIt); // remove the entry from the list and advance iterator
			delete entity;              // delete the object
		}
		else
		{
            ++mushIt;                   // advance list iterator
        }
    }

	//
    // generates a grayscale flash every 50 ms (if needed)
    //
	if (mFlashesNeeded > 0.0f)
	{
		if (mTime - mFlashTime > 0.1)
		{
			if ((int)mFlashesNeeded % 2)
			{
				SetEntitiesGrayscale(false);
			}
			else
			{
				SetEntitiesGrayscale(true);
			}
			mFlashesNeeded--;
			mFlashTime = mTime;
		}
	}

	//
    // create a new meteor every 0.2 to 1.2 seconds in scene 5
    //
	if (mTime - mMeteorTime > GG::UnitRandom() + 0.2 && mScene == 5)
	{
		int randomX = GG::RandomInt(mScrWidth-64);
		int randomRotation = GG::RandomInt(90) + 180;
		randomRotation = (randomRotation % 2) ? randomRotation : -randomRotation;
		Meteor* meteor = new Meteor(randomX, -64, (double)randomRotation);  
		mMeteors.push_back(meteor);
		// timestamp this meteor!
		mMeteorTime = mTime;
	}

	// Update the points label
	mTexMgr->DeleteTexture("PointsLabel");
	std::stringstream newLabel;
	newLabel << "Points = " << mPoints;
	SDL_Color text_color = {0, 0, 0};
	mTexMgr->LoadTexture("PointsLabel", newLabel.str().c_str(), text_color);
	delete mPointsLabel;
	mPointsLabel = new Label(10.0f, -5.0f, "PointsLabel");

	// Update the lives label
	mTexMgr->DeleteTexture("LivesLabel");
	newLabel.str(std::string());
	newLabel << "Lives = " << mRobot->GetLives();
	text_color.r = 255;
	text_color.g = 50;
	text_color.b = 50;
	mTexMgr->LoadTexture("LivesLabel", newLabel.str().c_str(), text_color);
	delete mLivesLabel;
	mLivesLabel = new Label(140.0f, -5.0f, "LivesLabel");
}
Exemplo n.º 9
0
/*
================================================================================

Game::HandleEvent

    Processes discrete events generated by the keyboard, mouse, window, etc.

================================================================================
*/
void Game::HandleEvent(const SDL_Event& e)
{
    switch (e.type)
	{
    case SDL_QUIT:
        mShouldQuit = true;
        break;

    case SDL_WINDOWEVENT:
        if (e.window.event == SDL_WINDOWEVENT_RESIZED)
		{
            mScrWidth = e.window.data1;
            mScrHeight = e.window.data2;
        }
        break;

    case SDL_KEYDOWN:
        switch (e.key.keysym.sym)
		{
        case SDLK_ESCAPE:
            mShouldQuit = true;
            break;

        case SDLK_k:
            //
            // Removes all the crawlers
            //
			{
				std::list<Crawler*>::iterator crawlerIter = mCrawlers.begin();
				for ( ; crawlerIter != mCrawlers.end(); ++crawlerIter)
				{
					Crawler* crawler = *crawlerIter;
					delete crawler;
				}
				mCrawlers.clear();
			}
            break;

        case SDLK_p:
            //
            // pause or unpause
            //
            if (mTimer.IsPaused())
			{
                mTimer.Unpause();
				Mix_ResumeMusic();
            }
			else
			{
                mTimer.Pause();
				Mix_PauseMusic();
            }
            break;
		case SDLK_v:
			{
				// show/hide collision rectangle
				rectVisible = rectVisible ? 0 : 1;
				break;
			}
		case SDLK_9:
			//If there is no music playing
			if (Mix_PlayingMusic() == 0)
			{
				//Play the music
				if (mRobot->GetLives() == 0)
				{
					Mix_PlayMusic(mBadGameOverMusic, 0);
				}
				else if (mScene < 6)
				{
					Mix_PlayMusic(mMusic, -1);
				}
				else
				{
					Mix_PlayMusic(mGoodGameOverMusic, -1);
				}
			}
			//If music is being played
			else
			{
				//If the music is paused
				if (Mix_PausedMusic() == 1)
				{
					//Resume the music
					Mix_ResumeMusic();
				}
				//If the music is playing
				else
				{
					//Pause the music
					Mix_PauseMusic();
				}
			}
			break;
		case SDLK_x:
			{
				if (!mRobot->IsDead())
				{
					// Add a strong crawler
					float x = GG::RandomFloat(32, mScrWidth - 32.0f);
					Crawler* crawler = new CrawlerStrong(x, mScrHeight - 1.0f - 32.0f, false);
					crawler->SetDirection(GG::RandomSign());
					mCrawlers.push_back(crawler);
				}
				break;		
			}

		case SDLK_c:
			{
				if (!mRobot->IsDead())
				{
					// Add a weak crawler
					float x = GG::RandomFloat(32, mScrWidth - 32.0f);
					Crawler* crawler = new CrawlerWeak(x, mScrHeight - 1.0f - 32.0f, true);
					crawler->SetDirection(GG::RandomSign());
					mCrawlers.push_back(crawler);
				}
				break;
			}
        }
		break;
	}
}
Exemplo n.º 10
0
// Tells the entities to use their grayscale renderables
// instead of their colored ones (applies to all entities)
void Game::SetEntitiesGrayscale(bool grayscale)
{
	if (mRobot) mRobot->SetGrayscale(grayscale);
	if (mBackground) mBackground->SetGrayscale(grayscale);
	if (mForeground) mForeground->SetGrayscale(grayscale);
	if (mFlagPole) mFlagPole->SetGrayscale(grayscale);
	
	if (mGrid)
	{
        int tileWidth = mGrid->TileWidth();
        int tileHeight = mGrid->TileHeight();
        GG::Rect tileRect(0, 0, tileWidth, tileHeight);
        for (int y = 0; y < mGrid->NumRows(); y++)
		{
            for (int x = 0; x < mGrid->NumCols(); x++)
			{
                Tile* tile = mGrid->GetTile(y, x);
                if (tile->GetRenderable())
				{
                    tile->SetGrayscale(grayscale);
                }
                tileRect.x += tileWidth;
            }
            tileRect.y += tileHeight;
            tileRect.x = 0;
        }
    }

	std::list<Explosion*>::iterator it = mExplosions.begin();
    for ( ; it != mExplosions.end(); ++it)
	{
        Explosion* boom = *it;
        boom->SetGrayscale(grayscale);
    }

	std::list<Meteor*>::iterator metIt = mMeteors.begin();
    for ( ; metIt != mMeteors.end(); ++metIt)
	{
        Meteor* meteor = *metIt;
        meteor->SetGrayscale(grayscale);
    }

	std::list<Crawler*>::iterator crawlerIter = mCrawlers.begin();
    for ( ; crawlerIter != mCrawlers.end(); ++crawlerIter)
	{
        Crawler* crawler = *crawlerIter;
        crawler->SetGrayscale(grayscale);
    }

	std::list<Coin*>::iterator coinIter = mCoins.begin();
    for ( ; coinIter != mCoins.end(); ++coinIter)
	{
        Coin* coin = *coinIter;
        coin->SetGrayscale(grayscale);
    }

	std::list<Layer*>::iterator mushIter = mMushrooms.begin();
    for ( ; mushIter != mMushrooms.end(); ++mushIter)
	{
        Layer* mushroom = *mushIter;
        mushroom->SetGrayscale(grayscale);
    }
}