#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_audio.h>
#include <allegro5/allegro_acodec.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
#include <allegro5/allegro_primitives.h>
#include <stdio.h>
const int WIDTH = 1000;
const int HEIGHT = 500;
// nomi risorse
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_BITMAP *sprite1 = NULL;
ALLEGRO_BITMAP *sprite2 = NULL;
ALLEGRO_BITMAP *sprite3_a = NULL;
ALLEGRO_BITMAP *sprite3_b = NULL;
ALLEGRO_BITMAP *sprite3_c = NULL;
ALLEGRO_BITMAP *sprite4 = NULL;
ALLEGRO_BITMAP *background = NULL;
ALLEGRO_BITMAP *scroll = NULL;
ALLEGRO_SAMPLE *music = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_FONT *font = NULL;
void destroy() {
// distrugge la risorsa se caricata
if (event_queue) al_destroy_event_queue(event_queue);
if (music) al_destroy_sample(music);
if (music) al_uninstall_keyboard();
if (sprite1) al_destroy_bitmap(sprite1);
if (sprite2) al_destroy_bitmap(sprite2);
if (sprite3_a) al_destroy_bitmap(sprite3_a);
if (sprite3_b) al_destroy_bitmap(sprite3_b);
if (sprite3_c) al_destroy_bitmap(sprite3_c);
if (sprite4) al_destroy_bitmap(sprite4);
if (background) al_destroy_bitmap(background);
if (font) al_destroy_font(font);
if (scroll) al_destroy_bitmap(scroll);
if (display) al_destroy_display(display);
}
int main(int argc, char* argv[]) {
char c;
printf("%s\n\n", argv[0]);
printf("Run da CODE::BLOCK, le risorse devono esistere nella cartella del progetto xxx.cbp\n");
printf("se lanciato dall'eseguibile le risorse devono esistere nella cartella dell'eseguibile.\n\n");
printf("Il programma funzionera' in computer che non hanno MinGW e allegro5 se alla compilazione\n");
printf("si aggiunge -satic (incorpora lib MinGW) e si e' copiato 'allegro_monolith-debug-5.2.dll'\n");
printf("dalla cartella allegro\\bin nella cartella dell'eseguibile.\n\n");
if (!al_init()) {
destroy();
printf("Errore durante l'inizializzazione di Allegro.\nPremi invio");
scanf("%c", &c);
return -1;
}
if (!al_init_image_addon()) {
destroy();
printf("Errore durante l'inizializzazione di image_addon.\nPremi invio");
scanf("%c", &c);
return -1;
}
display = al_create_display(WIDTH, HEIGHT);
if (!display) {
destroy();
printf("Errore durante la creazione di create_display.\nPremi invio");
scanf("%c", &c);
return -1;
}
if (!al_install_audio()) {
destroy();
printf("Errore durante l'inizializzazione di install_audio.\nPremi invio");
scanf("%c", &c);
return -1;
}
if (!al_init_acodec_addon()) {
destroy();
printf("Errore durante l'inizializzazione di acodec_addon.\nPremi invio");
scanf("%c", &c);
return -1;
}
if (!al_install_keyboard()) {
destroy();
printf("Errore durante l'inizializzazione di install_keyboard.\nPremi invio");
scanf("%c", &c);
return -1;
}
if (!al_init_ttf_addon()) {
destroy();
printf("Errore durante l'inizializzazione di ttf_addon.\nPremi invio");
scanf("%c", &c);
return -1;
}
if (!al_init_font_addon()) {
destroy();
printf("Errore durante l'inizializzazione di font_addon.\nPremi invio");
scanf("%c", &c);
return -1;
}
sprite1 = al_load_bitmap("Helicopter.png");
if (!sprite1) {
destroy();
printf("Errore durante il caricamento di Helicopter.png.\nPremi invio");
scanf("%c", &c);
return -1;
}
sprite2 = al_load_bitmap("Helicopter2.png");
if (!sprite2) {
destroy();
printf("Errore durante il caricamento di Helicopter2.png.\nPremi invio");
scanf("%c", &c);
return -1;
}
sprite3_a = al_load_bitmap("carro1_a.png");
if (!sprite3_a) {
destroy();
printf("Errore durante il caricamento del carro1_a.png.\nPremi invio");
scanf("%c", &c);
return -1;
}
sprite3_b = al_load_bitmap("carro1_b.png");
if (!sprite3_b) {
destroy();
printf("Errore durante il caricamento del carro1_b.png.\nPremi invio");
scanf("%c", &c);
return -1;
}
sprite3_c = al_load_bitmap("carro1_c.png");
if (!sprite3_c) {
destroy();
printf("Errore durante il caricamento del carro1_c.png.\nPremi invio");
scanf("%c", &c);
return -1;
}
sprite4 = al_load_bitmap("carro2.png");
if (!sprite4) {
destroy();
printf("Errore durante il caricamento del carro2.png.\nPremi invio");
scanf("%c", &c);
return -1;
}
background = al_load_bitmap("sfondo.jpg");
if (!background) {
destroy();
printf("Errore durante il caricamento dello sfondo.jpg.\nPremi invio");
scanf("%c", &c);
return -1;
}
scroll = al_load_bitmap("scroll3.png");
if (!scroll) {
destroy();
printf("Errore durante il caricamento dell'immagine scroll3.png.\nPremi invio");
scanf("%c", &c);
return -1;
}
font = al_load_font("c:/Windows/Fonts/arial.ttf", 24, 0);
if (!font) {
destroy();
printf("Errore durante il caricamento di c:/Windows/Fonts/arial.ttf.\nPremi invio");
scanf("%c", &c);
return -1;
}
music = al_load_sample("Katy.ogg");
if (!music) {
destroy();
printf("Errore durante il caricamento del file audio Katy.ogg.\nPremi invio");
scanf("%c", &c);
return -1;
}
if (!al_reserve_samples(1)) {
destroy();
printf("Errore durante l'assegnazione del flusso audio 1.\nPremi invio");
scanf("%c", &c);
return -1;
}
event_queue = al_create_event_queue();
if (!event_queue) {
destroy();
printf("Errore durante la creazione di create_event_queue.\nPremi invio");
scanf("%c", &c);
return -1;
}
printf("Allegro5 inizializzato, tutte le risorse caricate.\n");
al_init_primitives_addon();
al_install_mouse(); // installing mouse
al_register_event_source(event_queue, al_get_display_event_source(display)); // eventi schermo
al_register_event_source(event_queue, al_get_keyboard_event_source()); // eventi tastiera
al_register_event_source(event_queue, al_get_mouse_event_source()); // eventi mouse
al_set_window_title(display, "la sequenza di disegno determina chi copre cosa. USA LE FRECCE, SPAZIO e MOUSE!");
al_play_sample(music, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_LOOP, NULL);
ALLEGRO_MOUSE_STATE mouse_state;
int x = -100;
int scroll_x = 0;
int anim = 0;
float velCarro1 = 0;
float xCarro1 = 50;
float rest = 0.006;
bool game_over = false;
bool sun = false;
char *sStamp[40] = {0};
char *sRit[5] = {0};;
// Buffer sporcabile
ALLEGRO_BITMAP *bufferSfondo = al_create_bitmap(al_get_bitmap_width(background), al_get_bitmap_height(background));
// Buffer sporcabile
ALLEGRO_BITMAP *bufferScroll = al_create_bitmap(al_get_bitmap_width(scroll), al_get_bitmap_height(scroll));
al_set_target_bitmap(bufferSfondo); // imposto la destinazione dei comandi grafici sul bufferSfondo
al_draw_bitmap(background, 0, 0, 0); // copio background su bufferSfondo
al_set_target_bitmap(bufferScroll); // imposto la destinazione dei comandi grafici sul bufferScroll
al_draw_bitmap(scroll, 0, 0, 0); // copio scroll su bufferScroll
al_set_target_backbuffer(display); // ripristino la destinazione della grafica sul display
// Ciclo della finestra
while (!game_over) {
ALLEGRO_EVENT event; // coda degli eventi
bool got_event = al_get_next_event(event_queue, &event);
// controllo eventi se ci sono
if (got_event && event.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
game_over = true;
else { // controllo eventi tastiera/mouse
if (event.type == ALLEGRO_EVENT_KEY_DOWN) {
// printf("Kc:%d ", event.keyboard.keycode);
switch (event.keyboard.keycode) {
case ALLEGRO_KEY_ESCAPE:
game_over = true;
break;
case ALLEGRO_KEY_LEFT:
velCarro1 -= .1;
break;
case ALLEGRO_KEY_RIGHT:
velCarro1 += .1;
break;
case ALLEGRO_KEY_UP:
rest += 0.001;
break;
case ALLEGRO_KEY_DOWN:
rest -= 0.001;
if (rest < 0) rest = 0;
break;
case ALLEGRO_KEY_SPACE:
sun = !sun;
if (sun) {
al_set_target_bitmap(bufferSfondo);
al_draw_filled_circle(800,100,40,al_map_rgb(250, 250, 0));
al_set_target_backbuffer(display);
}
else {
al_set_target_bitmap(bufferSfondo);
al_draw_bitmap(background, 0, 0, 0); // ripristino background su bufferSfondo
al_set_target_backbuffer(display);
}
break;
}
}
if (event.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) {
al_get_mouse_state(&mouse_state);
if (mouse_state.buttons & 1) // Left mouse button
{
// l'immagine scroll scorre, alla cordinata mouse.x va tolta la coordinata scroll_x
int calcolo = event.mouse.x - scroll_x;
// l'immagine scroll è duplicata se si clicca sulla seconda...
if (calcolo>1000) calcolo -= 1000;
al_set_target_bitmap(bufferScroll);
al_draw_filled_rectangle(calcolo,event.mouse.y,calcolo+1,event.mouse.y+3,al_map_rgb(120, 250, 120));
al_set_target_backbuffer(display);
}
else if (mouse_state.buttons & 2) // Right mouse button
{
al_set_target_bitmap(bufferScroll);
// cancello con rgba, perchè il png scroll ha il canale alpha
al_clear_to_color(al_map_rgba_f(0.0, 0.0, 0.0, 0.0));
al_draw_bitmap(scroll, 0, 0, 0); // ripristino scroll su bufferScroll
al_set_target_backbuffer(display);
}
}
}
al_draw_bitmap(bufferSfondo, 0, 0, 0); // sfondo fisso, cancella tutta la scena
// Aggiorna la posizione delle immagini
x ++; // coordinata scroll, elicotteri e carro2
xCarro1 += velCarro1; // coordinata carro1_abc
//if (xCarro1 > WIDTH) xCarro1 = -600; // riparte fuori dallo schermo
//if (xCarro1 < -600) xCarro1 = WIDTH; // riparte fuori dallo schermo
// equivalente ternario è meglio?
xCarro1 = (xCarro1 > WIDTH ? -600 : (xCarro1 < -600 ? WIDTH : xCarro1));
if (x > WIDTH) x = -200; // riparte fuori dallo schermo
al_draw_bitmap(sprite2, x + 120, 100, 0); // elicottero2 resta dietro le nuvole
al_draw_bitmap(sprite4, (WIDTH+20) -x * 1.2 , 260, 0); // carro2 resta dietro a carro1_abc
int speed = 19 + velCarro1*15;
int rit = rest * 1000;
snprintf(sRit, sizeof(sRit), "- ritardo %d ms.", rit);
snprintf(sStamp, sizeof(sStamp), "Speed Carro %d Km/h %s", speed, sRit );// creazione stringa per stampa a video
if (speed > 0) { // ruote in avanti a b c
switch (++anim) {
case 3:
anim=0;
al_draw_bitmap(sprite3_a, xCarro1, 230, 1); // carro1_a resta dietro il muretto/cancello
break;
case 1:
al_draw_bitmap(sprite3_b, xCarro1, 230, 1); // carro1_a resta dietro il muretto/cancello
break;
case 2:
al_draw_bitmap(sprite3_c, xCarro1, 230, 1); // carro1_a resta dietro il muretto/cancello
break;
}
}
if (speed == 0) al_draw_bitmap(sprite3_a, xCarro1, 230, 1); // ruote ferme
if (speed < 0){ // ruote indietro c b a
switch (++anim) {
case 3:
anim=0;
al_draw_bitmap(sprite3_c, xCarro1, 230, 1); // carro1_c resta dietro il muretto/cancello
break;
case 1:
al_draw_bitmap(sprite3_b, xCarro1, 230, 1); // carro1_b resta dietro il muretto/cancello
break;
case 2:
al_draw_bitmap(sprite3_a, xCarro1, 230, 1); // carro1_a resta dietro il muretto/cancello
break;
}
}
// Disegna lo scroll con nuvole e cancello due volte,
// la seconda solo per coprire la larghezza dello schermo
al_draw_bitmap_region(bufferScroll, 0, 0, 1000, HEIGHT, scroll_x, 0, 0);
al_draw_bitmap_region(bufferScroll, 0, 0, WIDTH, HEIGHT, scroll_x + 1000, 0, 0);
al_draw_bitmap(sprite1, x*1.4, 70, 0); // elicottero resta avanti le nuvole
// Aggiorna la coordinata dello scroll
scroll_x -= 1;
// Se l'immagine di scroll è uscita dallo schermo, torna all'inizio
if (scroll_x <= -1000) scroll_x = 0;
// stampa stringa preparata centrata avanti a tutto
al_draw_text(font, al_map_rgb(40, 20, 200), WIDTH/2, 230, ALLEGRO_ALIGN_CENTER, sStamp );
al_flip_display(); // scambia lo schermo al V_Sync
al_rest(rest); // attesa in secondi
}
destroy();
return 0;
}