Beispiel #1
0
Screen* GameScreen::processEvents(bool& quit)
{
	if(confirm_exit_)
	{
		ConfirmDialog::ActionCode code=confirm_exit_->processEvents();
		switch(code)
		{
			case ConfirmDialog::YES:
				if(confirm_exit_->getFlags())
				{
					quit=true;
					return 0;
				}
				else
					return new MenuScreen(getSDLScreen());
			case ConfirmDialog::NO:
				refreshRect(confirm_exit_->getRect());
				removeWidget(confirm_exit_);
				delete confirm_exit_;
				confirm_exit_=0;
				return 0;
			case ConfirmDialog::NOTHING:
				return 0;
		}
	}
	else if(pause_)
	{
		if(pause_->processEvents())
		{
			refreshRect(pause_->getRect());
			removeWidget(pause_);
			delete pause_;
			pause_=0;
		}
		return 0;
	}
	else if(dlg_nick_)
	{
		InputDialog::ActionCode code=dlg_nick_->processEvents();
		switch(code)
		{
			case InputDialog::OK:
				if(!Util::trim(dlg_nick_->getText()).size())
					return 0;
				else
				{
					fstream file;
					try
					{
						file.open(getHomeFile("high_scores").c_str(),std::ios::binary|std::ios::in);
						if(!file.is_open())
							throw runtime_error("no se pudo abrir archivo \"high_scores\"");

						list<HighScore> high_scores;
						while(file.good())
						{
							HighScore hs;
							hs.readFrom(file);
							if(!file.eof())
								high_scores.push_back(hs);
						}
						file.clear();
						file.close();

						HighScore hs(Util::trim(dlg_nick_->getText()),matrix_->getPoints());
						high_scores.push_back(hs);
						high_scores.sort();

						file.open(getHomeFile("high_scores").c_str(),std::ios::binary|std::ios::out);
						if(!file.is_open())
							throw runtime_error("no se pudo abrir archivo \"high_scores\"");
						list<HighScore>::reverse_iterator i;
						int c=0;
						for(i=high_scores.rbegin();i!=high_scores.rend() && c<HS_NUM;i++,c++)
							(*i).writeTo(file);
						file.close();
					}
					catch(const exception& ex)
					{
						if(file.is_open())
							file.close();
						throw runtime_error(string("GameScreen::processEvents(...): ")+ex.what());
					}
					return new HighScoresScreen(getSDLScreen());
				}
			case InputDialog::CANCEL:
				refreshRect(dlg_nick_->getRect());
				removeWidget(dlg_nick_);
				delete dlg_nick_;
				dlg_nick_=0;
				return 0;
			case InputDialog::NOTHING:
				return 0;
		}
	}

	SDL_Event event;
	while(SDL_PollEvent(&event))
		if(event.type==SDL_KEYDOWN)
		{
			switch(event.key.keysym.sym)
			{
				case SDLK_ESCAPE:
					try
					{
						if(game_over_)
							return new MenuScreen(getSDLScreen());
						else
						{
							confirmExit(0);
							return 0;
						}
					}
					catch(const exception& ex)
					{
						throw runtime_error(string("GameScreen::processEvents(...): ")+ex.what());
					}
				case SDLK_PAUSE:
					try
					{
						if(!game_over_)
						{
							pause();
							return 0;
						}
					}
					catch(const exception& ex)
					{
						throw runtime_error(string("GameScreen::processEvents(...): ")+ex.what());
					}
				default:
					if(matrix_ && !matrix_->isDead())
						matrix_->key(event.key.keysym.sym);
					break;
			}
		}
		else if(event.type==SDL_QUIT)
			confirmExit(1);
	return 0;
}
Beispiel #2
0
HighScoresScreen::HighScoresScreen(SDLScreen* sdl_screen):Screen(sdl_screen)
{
	SDL_Surface* bkg=0;
	lb_title_=0;
	lb_totexcl_=0;

	ifstream in;

	Brush* brushs[2];
	brushs[0]=brushs[1]=0;
	list<Window*> rows;
	Label* nick=0,*points=0;
	Window* row=0;

	try
	{
		bkg=Util::loadImage("images/bkg_highscores.png");

		SDL_Color c1={230,230,230,0};
		lb_totexcl_=new Label(0,0,1,"fonts/vera.ttf",12,c1,tge::Label::BLENDED);
		lb_totexcl_->setText("www.totex.cl");

		SDL_Color c2={110,150,230,0};
		lb_title_=new Label(0,0,1,"fonts/polosemi.ttf",40,c2,tge::Label::BLENDED);
		lb_title_->setText("Mejores Puntajes");

		in.open(getHomeFile("high_scores").c_str(),std::ios::binary);
		if(!in.is_open())
			throw runtime_error("no se pudo abrir archivo \"high_scores\"");
		list<HighScore> high_scores;
		while(in.good())
		{
			HighScore hs;
			hs.readFrom(in);
			if(!in.eof())
				high_scores.push_back(hs);
		}
		in.close();

		SDL_Color cr0={27,27,105,0},cr1={92,92,191,0};
		brushs[0]=new Brush(cr0);
		brushs[1]=new Brush(cr1);
		list<HighScore>::iterator i;
		int c=0,row_y=HS_ROW_Y0;
		ostringstream ostr;
		for(i=high_scores.begin();i!=high_scores.end();i++,c++)
		{
			nick=new Label(HS_ROW_MARGIN_X,HS_ROW_MARGIN_Y,1,"fonts/vera.ttf",16,c1,tge::Label::BLENDED);
			points=new Label(0,HS_ROW_MARGIN_Y,1,"fonts/vera.ttf",16,c1,tge::Label::BLENDED);
			nick->setText((*i).getNick());
			ostr<<(*i).getPoints();
			points->setText(ostr.str());
			ostr.str("");
			row=new Window(HS_ROW_X,row_y,getWidth()-2*HS_ROW_X,2*HS_ROW_MARGIN_Y+nick->getHeight(),1);
			row_y+=row->getHeight();
			row->setBackground(*brushs[c%2]);
			row->setBackgroundAlpha(100);
			row->addWidget(nick);
			row->addWidget(points);
			points->setX(row->getWidth()-points->getWidth()-HS_ROW_MARGIN_X);
			rows.push_back(row);
			nick=points=0;
			row=0;
		}
		delete brushs[0];
		delete brushs[1];
	}
	catch(const exception& ex)
	{
		if(bkg)
			SDL_FreeSurface(bkg);
		if(lb_totexcl_)
			delete lb_totexcl_;
		if(lb_title_)
			delete lb_title_;

		if(in.is_open())
			in.close();

		if(nick)
			delete nick;
		if(points)
			delete points;
		if(row)
			delete row;

		if(brushs[0])
			delete brushs[0];
		if(brushs[1])
			delete brushs[1];

		list<Window*>::iterator i;
		for(i=rows.begin();i!=rows.end();i++)
			delete *i;

		throw runtime_error(string("HighScoresScreen::HighScoresScreen(...): ")+ex.what());
	}

	setBackground(bkg);

	lb_totexcl_->setX(5);
	lb_totexcl_->setY(getHeight()-lb_totexcl_->getHeight()-3);
	addWidget(lb_totexcl_);
	lb_title_->setX((getWidth()-lb_title_->getWidth())/2);
	lb_title_->setY(10);
	addWidget(lb_title_);

	list<Window*>::iterator i;
	for(i=rows.begin();i!=rows.end();i++)
		addWidget(*i);
}
Beispiel #3
0
void GameScreen::ticks(int t)
{
	if(game_over_)
		game_over_->ticks(t);
	if(confirm_exit_)
	{
		confirm_exit_->ticks(t);
		return;
	}
	else if(pause_)
	{
		pause_->ticks(t);
		return;
	}
	else if(dlg_nick_)
	{
		dlg_nick_->ticks(t);
		return;
	}
	else if(matrix_)
	{
		if(state_==PLAYING)
		{
			matrix_->ticks(t);
			if(matrix_->isDead())
			{
				state_=GAMEOVER;

				vector<SDL_Surface*>* surfaces=new vector<SDL_Surface*>();
				try
				{
					surfaces->reserve(6);
					ostringstream ostr;
					for(int i=0;i<6;i++)
					{
						ostr<<"images/gameover"<<i<<".png";
						SDL_Surface* tmp=Util::loadImage(ostr.str(),true);
						surfaces->push_back(tmp);
						ostr.str("");
					}

					int w=surfaces->at(0)->w;
					int h=surfaces->at(0)->h;
					Rapidity rapidity(1,5);
					game_over_=new Animation((getWidth()-w)/2,(getHeight()-h)/2,w,h
									,1,Animation::LIFO ,surfaces,rapidity);
					addWidget(game_over_);
				}
				catch(const exception& ex)
				{
					vector<SDL_Surface*>::iterator i;
					for(i=surfaces->begin();i!=surfaces->end();i++)
						SDL_FreeSurface(*i);
					throw runtime_error(string("GameScreen::ticks(...): ")+ex.what());
				}

				ifstream in;
				try
				{
					in.open(getHomeFile("high_scores").c_str(),std::ios::binary);
					if(!in.is_open())
						throw runtime_error("no se pudo abrir archivo \"high_scores\"");

					list<HighScore> high_scores;
					while(in.good())
					{
						HighScore hs;
						hs.readFrom(in);
						if(!in.eof())
							high_scores.push_back(hs);
					}
					in.close();

					if(matrix_->getPoints() && (high_scores.size()<HS_NUM ||
							(*high_scores.rbegin()).getPoints()<matrix_->getPoints()))
						getNick();
				}
				catch(const exception& ex)
				{
					if(in.is_open())
						in.close();
					throw runtime_error(string("GameScreen::ticks(...): ")+ex.what());
				}

			}
		}
		try
		{
			ostringstream ostr;
			ostr<<"Nivel "<<matrix_->getLevel();
			lb_level_->setText(ostr.str());
			ostr.str("");
			ostr<<matrix_->getPoints()<<" pts";
			lb_points_->setText(ostr.str());

			if(!matrix_->isDead())
			{
				SDL_Surface* srf=matrix_->getNextPiece();
				next_piece_->setSurface(srf);
				next_piece_->setPosition(PANEL_X+(PANEL_W-srf->w)/2
						,PANEL_Y+lb_next_piece_->getY()+lb_next_piece_->getHeight()+5);
			}
		}
		catch(const exception& ex)
		{
			throw runtime_error(string("GameScreen::ticks(...): ")+ex.what());
		}
	}
}