Example #1
0
int main(int argc, char* argv[]){
	

	try {
		setWorkdir();
		
		Tetris tetris;
		tetris.play();
	}
	catch (TetrisException& e) {
		std::cerr << "Tetris exception occured:\n" << e.what();
	}
	catch (std::exception& e){
		std::cerr << "Standard exception occured: \n" << e.what();
	}
}
Example #2
0
int main() {
	stringstream score;
	SDL_Surface * screen = 0;
	bool running = true;
	
	SDL_Init(SDL_INIT_EVERYTHING);
	TTF_Init();
	screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DEPTH, SDL_SWSURFACE | SDL_DOUBLEBUF);
	TTF_Font * font = TTF_OpenFont("Bandal.ttf", 28);
	if(font == NULL){
		cout << "Impossibile caricare il font : "<<SDL_GetError()<<endl;
		exit(1);
	}
	
	Tetris * game = new Tetris();
	
	SDL_Surface * bg = loadbg("black_background.bmp");
	
	SDL_Event * event = new SDL_Event();
     	while(!game->getEnd() && running){
     		while(SDL_PollEvent(event)){
     			if(event->type == SDL_QUIT)
     				running = false;
     			if(event->type == SDL_KEYDOWN){
     				if(event->key.keysym.sym == SDLK_SPACE){
     					game->setDirection(4);
     				}
     				if(event->key.keysym.sym == SDLK_RIGHT){
     					game->setDirection(1);
     				}
     				if(event->key.keysym.sym == SDLK_LEFT){
     					game->setDirection(2);
     				}
     				if(event->key.keysym.sym == SDLK_DOWN){
     					game->setDirection(3);
     				}
     			}
     			if(event->type == SDL_KEYUP){
     				if(event->key.keysym.sym == SDLK_DOWN){
     					game->setDirection(3);
     				}
     			}
     		}
     		SDL_BlitSurface(bg, NULL, screen, NULL);
     		game->play();
     		game->draw(screen);
     		score.str("");
     		score << "Score : "<<game->getPoints();
     		putstring(screen, font, score.str().c_str(), 0,0,0xff,0xff,0xff);
     		
     		if(SDL_Flip(screen) == -1)
     			return 1;
     		SDL_Delay(10);
    	}
    	
    	delete game;
    	TTF_CloseFont(font);
    	TTF_Quit();
    	SDL_FreeSurface(screen);
    	SDL_Quit();
    	return 0;
}