Beispiel #1
0
int main (int argc, char *argv[])
{    
    srand ( time(NULL) );
    
    sound.LoadSound("blockHit","hit.wav");
    sound.LoadSound("horizontal","horizon2.wav");
    sound.LoadSound("rotate","rotate.wav");
    
    loadHighscore(&hscore);
    
    resetGame();
    
    while (keyboard.Quit == false)
    {   
          if(keyboard.r){

                       keyboard.Clear();
                       resetGame();
        }
        
        if(keyboard.p){

                       SDL_Delay(100);
                       keyboard.Poll();
                       keyboard.Clear();
                       while(!keyboard.p){
                            keyboard.Poll(); 
                            display.Clear();
                            drawBorders();
                            sprintf(TEXT,"Game is paused.");
                            display.DrawString(TEXT,  0, (HEIGHT/2)*BLOCKPIXSIZE,0,0x00FF00);
                            display.Render();
                            SDL_Delay(50);
                       }
        }
        int speed = 0;
        speed = score/2400;
        if(speed > 40){speed = 40;}
        
        if(!(cycle% (60-speed)) || keyboard.Down){fallBlock(); HorizontalStrip(); if(keyboard.Down)keyboard.Clear(); }
        
        keyboard.Poll();
        if(keyboard.Left && InvasiveCheckPass(blockx-1,blocky,blockt,blockr)){ --blockx; keyboard.Clear();}
        if(keyboard.Right && InvasiveCheckPass(blockx+1,blocky,blockt,blockr)){++blockx; keyboard.Clear();}
        
        if(keyboard.Up && InvasiveCheckPass(blockx,blocky,blockt,blockr+1)){ 
             blockr = (++blockr > 3) ? 0 : blockr; updateBlockBoundaries();
             sound.PlaySound("rotate",80,0);
             keyboard.Clear();
             
        }
        
        if(keyboard.Space){
            while(InvasiveCheckPass(blockx,blocky+1,blockt,blockr)){fallBlock(); HorizontalStrip();}
            gridLockBlock();
            keyboard.Clear();
            SDL_Delay(80);
        }
    
        
        blockx = (blockx< -minxcord) ? -minxcord : ((blockx > (WIDTH-1)-maxxcord) ? (WIDTH-1)-maxxcord : blockx);
        
        
        render(); 
        
        char TXT[128];
        if(speed > 30){sprintf(TXT,"EXTREME PENTRIX: %i",hscore);}
        else{sprintf(TXT,"Pentrix Highscore: %i",hscore);}
        display.Caption(TXT);
        
        
        SDL_Delay (4); cycle = (++cycle > 239) ? 1 : cycle;
    }

    return 0;
}
int main(void)
{
	char* error = "";
	ALLEGRO_DISPLAY *display = NULL;
	ALLEGRO_FONT *font = NULL;
	ALLEGRO_TIMER *timer = NULL;
	ALLEGRO_EVENT_QUEUE *event_queue = NULL;

	HANDLE mutex = CreateMutex(NULL, FALSE, "Chapter3_CloseMutex-Mutex");
    if (GetLastError() == ERROR_ALREADY_EXISTS) {
		MessageBoxA(GetForegroundWindow(), "Only one instance of the game can run at a time.", "Chapter3_CloseMutex", MB_OK);
        return 0;
    }

	do
	{
		loadHighscore();
		if(!al_init())
		{
			error = "failed to initialize allegro!";
			break;
		}
		if(!al_init_primitives_addon())
		{
			error = "failed to initialize primitives!";
			break;
		}
		al_init_font_addon();
		if(!al_init_ttf_addon())
		{
			error = "failed to initialize ttfs!";
			break;
		}
		if(!al_install_keyboard())
		{
			error = "failed to initialize keyboard!";
			break;
		}
		if(!(font = al_load_ttf_font("arial.ttf", 18, 0)))
		{
			error = "failed to initialize font!";
			break;
		}
		if(!(timer = al_create_timer(1.0 / 20)))
		{
			error = "failed to initialize timer!";
			break;
		}
		if(!(display = al_create_display(640, 480)))
		{
			error = "failed to initialize display!";
			break;
		}
		if(!(event_queue = al_create_event_queue()))
		{
			error = "failed to initialize event_queue!";
			break;
		}

		al_register_event_source(event_queue, al_get_display_event_source(display));
		al_register_event_source(event_queue, al_get_keyboard_event_source());
		al_register_event_source(event_queue, al_get_timer_event_source(timer));

		al_start_timer(timer);

		while(1)
		{
			ALLEGRO_EVENT ev;
			al_wait_for_event(event_queue, &ev);

			if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
				break;
			else if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
			{
				if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
					break;
				else if (ev.keyboard.keycode <= 255)
					keys[ev.keyboard.keycode] = true;
			}
			else if (ev.type == ALLEGRO_EVENT_KEY_UP)
			{
				if (ev.keyboard.keycode <= 255)
					keys[ev.keyboard.keycode] = false;
			}
			else if (ev.type == ALLEGRO_EVENT_KEY_CHAR)
				keyboardEvent();

			if (al_is_event_queue_empty(event_queue) && ev.type == ALLEGRO_EVENT_TIMER)
				drawFrame(font);
		}
	} while (0);

	if (mutex)
        CloseHandle(mutex);

	if (strlen(error) > 0)
	{
		al_show_native_message_box(NULL, NULL, "error", error, NULL, NULL);
		return -1;
	}

	al_destroy_timer(timer);
	al_destroy_display(display);
	al_destroy_event_queue(event_queue);
	saveHighscore();


	return 0;
}
Beispiel #3
0
int main( int argc, char *argv[] )
{
	struct highscoreItem *highscore = loadHighscore();

	res.width = 1280;
	res.height = 720;

	// Load a font

	if (TTF_Init() != 0)
	{
		printf("TTF_Init() Failed: %s\n", TTF_GetError());
		SDL_Quit();
		exit(1);
	}

	fontBig = TTF_OpenFont("resources/fonts/DejaVuSans.ttf", FONT_SIZE_BIG);
	fontSmall = TTF_OpenFont("resources/fonts/DejaVuSans.ttf", FONT_SIZE_SMALL);
	TTF_SetFontStyle(fontSmall, TTF_STYLE_BOLD);
	font = fontBig;
	if (fontBig == NULL || fontSmall == NULL)
	{
		printf("TTF_OpenFont() Failed: %s\n", TTF_GetError());
		TTF_Quit();
		SDL_Quit();
		exit(1);
	}
	
	// Initialisiere SDL
	if( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_AUDIO) < 0 ) {
		fprintf( stderr, "SDL konnte nicht initialisiert werden:  %s\n", SDL_GetError() );
		return 1;
	}

	// Initialisiere SDL_Image
	IMG_Init(IMG_INIT_PNG);

	// Timer einrichten
	SDL_AddTimer (16, generate_userevent, NULL);

	// Event-System initialisieren
	SDL_Event event;

	SDL_WM_SetCaption("Jump and Run", "Jump and Run");
	SDL_Surface *icon = IMG_Load("resources/images/icon.png");
	if (icon != NULL)
		SDL_WM_SetIcon(icon, NULL);

	// Erstelle die Bildschirmfläche
	SDL_Surface *screen = NULL;
	screen = SDL_SetVideoMode( res.width, res.height, SCREEN_BPP, SDL_HWSURFACE | SDL_DOUBLEBUF);

	

	SDL_ShowCursor(0);

	initializeSounds();
	startMusic();

	initializeClouds();

	int returnValue = drawMenu(screen, event);
	while (1)
	{
		switch (returnValue)
		{
			case MENU:
				returnValue = drawMenu(screen, event);
				break;

			case START_GAME:
				returnValue = LEVEL_OFFSET;
				break;

			case HIGHSCORES:
				drawHighscore(screen, font, event, highscore);
				returnValue = MENU;
				break;

			default:
				if (returnValue >= LEVEL_OFFSET && returnValue < LEVEL_OFFSET + MAX_LEVEL)
				{
					returnValue = addScore(screen, startGame (screen, event, res, returnValue - LEVEL_OFFSET), event, &highscore);
				}
				else if (returnValue <= EXIT_GAME)
				{
					printf("Spiel beenden\n");
					freeHighscore(highscore);
					freeAudio();
					TTF_CloseFont(fontBig);
					TTF_CloseFont(fontSmall);
					TTF_Quit();
					SDL_Quit();
					exit(returnValue - EXIT_GAME);
				}
				else
					returnValue = MENU;
		}
	}
	
	return 0;
}