int main(int argc, char **argv) { ALLEGRO_DISPLAY *display; ALLEGRO_TIMER *timer; ALLEGRO_EVENT_QUEUE *queue; bool redraw = true; ALLEGRO_FONT *font; ALLEGRO_BITMAP *spin, *spin2; int current_bitmap = 0; int loaded_bitmap = 0; ALLEGRO_THREAD *thread; (void)argc; (void)argv; if (!al_init()) { abort_example("Could not init Allegro.\n"); } al_init_image_addon(); al_init_font_addon(); al_init_primitives_addon(); init_platform_specific(); open_log(); al_install_mouse(); al_install_keyboard(); spin = al_load_bitmap("data/cursor.tga"); log_printf("default bitmap without display: %p\n", spin); al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP); spin2 = al_load_bitmap("data/cursor.tga"); log_printf("video bitmap without display: %p\n", spin2); log_printf("%p before create_display: ", spin); print_bitmap_flags(spin); log_printf("\n"); display = al_create_display(64, 64); if (!display) { abort_example("Error creating display\n"); } spin2 = al_load_bitmap("data/cursor.tga"); log_printf("video bitmap with display: %p\n", spin2); log_printf("%p after create_display: ", spin); print_bitmap_flags(spin); log_printf("\n"); log_printf("%p after create_display: ", spin2); print_bitmap_flags(spin2); log_printf("\n"); al_destroy_display(display); log_printf("%p after destroy_display: ", spin); print_bitmap_flags(spin); log_printf("\n"); log_printf("%p after destroy_display: ", spin2); print_bitmap_flags(spin2); log_printf("\n"); display = al_create_display(640, 480); log_printf("%p after create_display: ", spin); print_bitmap_flags(spin); log_printf("\n"); log_printf("%p after create_display: ", spin2); print_bitmap_flags(spin2); log_printf("\n"); font = al_load_font("data/fixed_font.tga", 0, 0); mutex = al_create_mutex(); thread = al_create_thread(loading_thread, NULL); al_start_thread(thread); timer = al_create_timer(1.0 / 30); queue = al_create_event_queue(); al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_display_event_source(display)); al_register_event_source(queue, al_get_timer_event_source(timer)); al_start_timer(timer); while (1) { ALLEGRO_EVENT event; al_wait_for_event(queue, &event); if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) break; if (event.type == ALLEGRO_EVENT_KEY_DOWN) { if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) break; } if (event.type == ALLEGRO_EVENT_TIMER) redraw = true; if (redraw && al_is_event_queue_empty(queue)) { float x = 20, y = 320; int i; ALLEGRO_COLOR color = al_map_rgb_f(0, 0, 0); float t = al_current_time(); redraw = false; al_clear_to_color(al_map_rgb_f(0.5, 0.6, 1)); al_draw_textf(font, color, x + 40, y, 0, "Loading %d%%", 100 * load_count / load_total); al_lock_mutex(mutex); if (loaded_bitmap < load_count) { /* This will convert any video bitmaps without a display * (all the bitmaps being loaded in the loading_thread) to * video bitmaps we can use in the main thread. */ al_convert_bitmap(bitmaps[loaded_bitmap]); loaded_bitmap++; } al_unlock_mutex(mutex); if (current_bitmap < loaded_bitmap) { int bw; al_draw_bitmap(bitmaps[current_bitmap], 0, 0, 0); if (current_bitmap + 1 < loaded_bitmap) current_bitmap++; for (i = 0; i <= current_bitmap; i++) { bw = al_get_bitmap_width(bitmaps[i]); al_draw_scaled_rotated_bitmap(bitmaps[i], 0, 0, (i % 20) * 640 / 20, 360 + (i / 20) * 24, 32.0 / bw, 32.0 / bw, 0, 0); } } if (loaded_bitmap < load_total) { al_draw_scaled_rotated_bitmap(spin, 16, 16, x, y, 1.0, 1.0, t * ALLEGRO_PI * 2, 0); } al_flip_display(); } } al_join_thread(thread, NULL); al_destroy_mutex(mutex); al_destroy_font(font); al_destroy_display(display); close_log(true); return 0; }
int main(int argc, char **argv){ srand( time(NULL) ); int bulletLength = 4; Bullet *bullets[ 4 ]; ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_TIMER *timer = NULL; //Bullet *bouncer = new Bullet( SCREEN_W / 2.0 - BOUNCER_SIZE, SCREEN_H / 2.0 - BOUNCER_SIZE / 2.0, -4.0, -4.0 ); bool redraw = true; for( int i = 0; i < bulletLength; i++ ) { bullets[i] = new Bullet( SCREEN_W / 2.0 + ( i * 10 ), SCREEN_H / 2.0 - BOUNCER_SIZE / 2.0, -4.0, -4.0 ); } if(!al_init()) { fprintf(stderr, "failed to initialize allegro!\n"); return -1; } timer = al_create_timer(1.0 / FPS); if(!timer) { fprintf(stderr, "failed to create timer!\n"); return -1; } display = al_create_display(SCREEN_W, SCREEN_H); if(!display) { fprintf(stderr, "failed to create display!\n"); al_destroy_timer(timer); return -1; } for( int i = 0; i < bulletLength; i++ ) { bullets[i]->setBitmap( BOUNCER_SIZE, BOUNCER_SIZE ); if( !bullets[i]->getBitmap() ) { fprintf(stderr, "failed to create bouncer bitmap!\n"); al_destroy_display(display); al_destroy_timer(timer); return -1; } } for( int i = 0; i < bulletLength; i++ ) { al_set_target_bitmap( bullets[i]->getBitmap() ); } // al_set_target_bitmap( bouncer->getBitmap() ); al_clear_to_color(al_map_rgb(255, 0, 255)); al_set_target_bitmap(al_get_backbuffer(display)); event_queue = al_create_event_queue(); if(!event_queue) { fprintf(stderr, "failed to create event_queue!\n"); for( int i = 0; i < bulletLength; i++ ) { al_destroy_bitmap( bullets[i]->getBitmap() ); } //al_destroy_bitmap( bouncer->getBitmap() ); al_destroy_display(display); al_destroy_timer(timer); return -1; } al_register_event_source(event_queue, al_get_display_event_source(display)); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_clear_to_color(al_map_rgb(0,0,0)); al_flip_display(); al_start_timer(timer); while(1) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); for( int i = 0; i < bulletLength; i++ ) { if(ev.type == ALLEGRO_EVENT_TIMER) { if( bullets[i]->getX() < 0 || bullets[i]->getX() > SCREEN_W - BOUNCER_SIZE) { bullets[i]->setDX( - ( bullets[i]->getDX() ) ); } if( bullets[i]->getY() < 0 || bullets[i]->getY() > SCREEN_H - BOUNCER_SIZE) { bullets[i]->setDY( -(bullets[i]->getDY() ) ); } bullets[i]->setX( bullets[i]->getX() + bullets[i]->getDX() ); bullets[i]->setY( bullets[i]->getY() + bullets[i]->getDY() ); redraw = true; } else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } if(redraw && al_is_event_queue_empty(event_queue)) { redraw = false; al_clear_to_color(al_map_rgb(0,0,0)); al_draw_bitmap( bullets[i]->getBitmap(), bullets[i]->getX(), bullets[i]->getY(), 0); al_flip_display(); } } } for( int i = 0; i < bulletLength; i++ ) { al_destroy_bitmap( bullets[i]->getBitmap() ); } al_destroy_timer(timer); al_destroy_display(display); al_destroy_event_queue(event_queue); return 0; }
int main(int argc, const char *argv[]) { const char *font_file = "data/DejaVuSans.ttf"; ALLEGRO_DISPLAY *display; ALLEGRO_TIMER *timer; ALLEGRO_EVENT_QUEUE *queue; int redraw = 0; double t = 0; if (!al_init()) { abort_example("Could not init Allegro.\n"); return 1; } al_init_primitives_addon(); al_install_mouse(); al_init_font_addon(); al_init_ttf_addon(); #ifdef ALLEGRO_IPHONE al_set_new_display_flags(ALLEGRO_FULLSCREEN_WINDOW); #endif display = al_create_display(640, 480); if (!display) { abort_example("Could not create display.\n"); return 1; } al_install_keyboard(); if (argc >= 2) { font_file = argv[1]; } ex.f1 = al_load_font(font_file, 48, 0); ex.f2 = al_load_font(font_file, 48, ALLEGRO_TTF_NO_KERNING); ex.f3 = al_load_font(font_file, 12, 0); /* Specifying negative values means we specify the glyph height * in pixels, not the usual font size. */ ex.f4 = al_load_font(font_file, -140, 0); ex.f5 = al_load_font(font_file, 12, ALLEGRO_TTF_MONOCHROME); if (!ex.f1 || !ex.f2 || !ex.f3 || !ex.f4) { abort_example("Could not load font: %s\n", font_file); return 1; } ex.config = al_load_config_file("data/ex_ttf.ini"); if (!ex.config) { abort_example("Could not data/ex_ttf.ini\n"); return 1; } timer = al_create_timer(1.0 / 60); queue = al_create_event_queue(); al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_display_event_source(display)); al_register_event_source(queue, al_get_timer_event_source(timer)); al_start_timer(timer); while (true) { ALLEGRO_EVENT event; al_wait_for_event(queue, &event); if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) break; if (event.type == ALLEGRO_EVENT_KEY_DOWN && event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { break; } if (event.type == ALLEGRO_EVENT_TIMER) redraw++; while (redraw > 0 && al_is_event_queue_empty(queue)) { double dt; redraw--; dt = al_get_time(); render(); dt = al_get_time() - dt; t = 0.99 * t + 0.01 * dt; ex.fps = 1.0 / t; al_flip_display(); } } al_destroy_font(ex.f1); al_destroy_font(ex.f2); al_destroy_font(ex.f3); al_destroy_font(ex.f4); al_destroy_font(ex.f5); al_destroy_config(ex.config); return 0; }
int main(int argc, char **argv) { ALLEGRO_DISPLAY *display; ALLEGRO_BITMAP *bitmap[2]; ALLEGRO_TIMER *timer; ALLEGRO_EVENT_QUEUE *queue; bool redraw = true; ALLEGRO_SHADER *shader; int t = 0; const char* pixel_file = NULL; (void)argc; (void)argv; if (!al_init()) { abort_example("Could not init Allegro.\n"); } al_install_mouse(); al_install_keyboard(); al_init_image_addon(); init_platform_specific(); al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR | ALLEGRO_MIPMAP); al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST); al_set_new_display_option(ALLEGRO_SAMPLES, 4, ALLEGRO_SUGGEST); al_set_new_display_flags(ALLEGRO_PROGRAMMABLE_PIPELINE); display = al_create_display(640, 480); if (!display) { abort_example("Error creating display\n"); } bitmap[0]= load_bitmap("data/mysha.pcx"); bitmap[1]= load_bitmap("data/obp.jpg"); shader = al_create_shader(ALLEGRO_SHADER_AUTO); if (!shader) abort_example("Error creating shader.\n"); if (al_get_shader_platform(shader) == ALLEGRO_SHADER_GLSL) { #ifdef ALLEGRO_CFG_SHADER_GLSL pixel_file = "data/ex_shader_multitex_pixel.glsl"; #endif } else { #ifdef ALLEGRO_CFG_SHADER_HLSL pixel_file = "data/ex_shader_multitex_pixel.hlsl"; #endif } if (!pixel_file) { abort_example("No shader source\n"); } if (!al_attach_shader_source(shader, ALLEGRO_VERTEX_SHADER, al_get_default_shader_source(ALLEGRO_SHADER_AUTO, ALLEGRO_VERTEX_SHADER))) { abort_example("al_attach_shader_source for vertex shader failed: %s\n", al_get_shader_log(shader)); } if (!al_attach_shader_source_file(shader, ALLEGRO_PIXEL_SHADER, pixel_file)) abort_example("al_attach_shader_source_file for pixel shader failed: %s\n", al_get_shader_log(shader)); if (!al_build_shader(shader)) abort_example("al_build_shader failed: %s\n", al_get_shader_log(shader)); al_use_shader(shader); timer = al_create_timer(1.0 / 60); queue = al_create_event_queue(); al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_display_event_source(display)); al_register_event_source(queue, al_get_timer_event_source(timer)); al_start_timer(timer); while (1) { ALLEGRO_EVENT event; al_wait_for_event(queue, &event); if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) break; if (event.type == ALLEGRO_EVENT_KEY_CHAR) { if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) break; } if (event.type == ALLEGRO_EVENT_TIMER) { redraw = true; t++; } if (redraw && al_is_event_queue_empty(queue)) { int dw, dh; double scale = 1 + 100 * (1 + sin(t * ALLEGRO_PI * 2 / 60 / 10)); double angle = ALLEGRO_PI * 2 * t / 60 / 15; double x = 120 - 20 * cos(ALLEGRO_PI * 2 * t / 60 / 25); double y = 120 - 20 * sin(ALLEGRO_PI * 2 * t / 60 / 25); dw = al_get_display_width(display); dh = al_get_display_height(display); redraw = false; al_clear_to_color(al_map_rgb_f(0, 0, 0)); /* We set a second bitmap for texture unit 1. Unit 0 will have * the normal texture which al_draw_*_bitmap will set up for us. * We then draw the bitmap like normal, except it will use the * custom shader. */ al_set_shader_sampler("tex2", bitmap[1], 1); al_draw_scaled_rotated_bitmap(bitmap[0], x, y, dw / 2, dh / 2, scale, scale, angle, 0); al_flip_display(); } } al_use_shader(NULL); al_destroy_bitmap(bitmap[0]); al_destroy_bitmap(bitmap[1]); al_destroy_shader(shader); return 0; }
int main(int argc, char **argv) { ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_TIMER *timer = NULL; //ALLEGRO_BITMAP *bouncer = NULL; bool redraw = true; float center_w = SCREEN_W/2; float center_h = SCREEN_H/2; bool key[4] = { false, false, false, false }; bool doexit = false; int animation_code; al_init_image_addon(); if(!al_init()) { fprintf(stderr, "failed to initialize allegro!\n"); return -1; } if(!al_install_keyboard()) { fprintf(stderr, "failed to initialize the keyboard!\n"); return -1; } timer = al_create_timer(1.0 / FPS); if(!timer) { fprintf(stderr, "failed to create timer!\n"); return -1; } display = al_create_display(SCREEN_W, SCREEN_H); if(!display) { fprintf(stderr, "failed to create display!\n"); al_destroy_timer(timer); return -1; } player man; tile titi; //al_convert_mask_to_alpha(bouncer, al_map_rgb(255, 0, 255)); //al_set_target_bitmap(bouncer); //al_clear_to_color(al_map_rgb(255, 0, 255)); //al_set_target_bitmap(al_get_backbuffer(display)); event_queue = al_create_event_queue(); if(!event_queue) { fprintf(stderr, "failed to create event_queue!\n"); //al_destroy_bitmap(bouncer); al_destroy_display(display); al_destroy_timer(timer); return -1; } al_register_event_source(event_queue, al_get_display_event_source(display)); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_clear_to_color(al_map_rgb(0,0,0)); al_flip_display(); al_start_timer(timer); while (!doexit) { printf("here!!!"); while(!doexit) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); if(ev.type == ALLEGRO_EVENT_TIMER) { if(key[KEY_UP]) { animation_code=1; } if(key[KEY_DOWN]) { animation_code =2; } if(key[KEY_LEFT]) { animation_code = 3; } if(key[KEY_RIGHT]) { animation_code= 4; } redraw = true; } else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_UP: key[KEY_UP] = true; printf("HERE!!!!!!!!!!!!!!!!!!!!!!!"); break; case ALLEGRO_KEY_DOWN: key[KEY_DOWN] = true; break; case ALLEGRO_KEY_LEFT: key[KEY_LEFT] = true; break; case ALLEGRO_KEY_RIGHT: key[KEY_RIGHT] = true; break; } } else if(ev.type == ALLEGRO_EVENT_KEY_UP) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_UP: key[KEY_UP] = false; printf("FALSE!!!!!!!!!!!!!!!!!!!!!!!!!!!"); break; case ALLEGRO_KEY_DOWN: key[KEY_DOWN] = false; break; case ALLEGRO_KEY_LEFT: key[KEY_LEFT] = false; break; case ALLEGRO_KEY_RIGHT: key[KEY_RIGHT] = false; break; case ALLEGRO_KEY_ESCAPE: doexit = true; break; } } if(redraw && al_is_event_queue_empty(event_queue)) { redraw = false; al_clear_to_color(al_map_rgb(0,0,0)); //al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0); titi.draw_map(center_w, center_h); man.draw(animation_code, ¢er_w, ¢er_h); al_flip_display(); animation_code = 0; } } } //al_destroy_bitmap(bouncer); al_destroy_timer(timer); al_destroy_display(display); al_destroy_event_queue(event_queue); return 0; }
int game() { ALLEGRO_DISPLAY *display = NULL; ALLEGRO_TIMER *timer = NULL; bool exit = false; if (!al_init()) { fprintf(stderr, "Allegro could not be initialized."); return -1; } if (!al_install_keyboard()) { fprintf(stderr, "Keyboard could not be initialized."); return -1; } if (!al_install_audio()){ fprintf(stderr, "Audio could not be initialized."); return -1; } if (!al_reserve_samples(1)){ fprintf(stderr, "Samples could not be reserved."); return -1; } if (!al_init_acodec_addon()) { fprintf(stderr, "Audio codecs could not be initialized."); return -1; } al_init_primitives_addon(); al_init_image_addon(); al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST); al_set_new_display_option(ALLEGRO_SAMPLES, 4, ALLEGRO_SUGGEST); display = al_create_display(SCREEN_W, SCREEN_H); if (!display) { fprintf(stderr, "Display could not be created."); return -1; } timer = al_create_timer(1.0 / FPS); if (!timer) { fprintf(stderr, "Timer could not be created."); return -1; } al_set_target_bitmap(al_get_backbuffer(display)); eventQueue = al_create_event_queue(); if (!eventQueue) { fprintf(stderr, "Event queue could not be created."); return -1; } yaySound = al_load_sample("yay.wav"); naySound = al_load_sample("nay.wav"); hitSound = al_load_sample("hit.wav"); al_register_event_source(eventQueue, al_get_display_event_source(display)); al_register_event_source(eventQueue, al_get_timer_event_source(timer)); al_register_event_source(eventQueue, al_get_keyboard_event_source()); al_clear_to_color(al_map_rgb(10, 10, 10)); al_flip_display(); al_start_timer(timer); // Open menu. titleScreen(); al_destroy_timer(timer); al_destroy_display(display); al_destroy_event_queue(eventQueue); return 0; }
int main(int argc, const char **argv){ //Hacer un programa que muestre un cuadrado y pueda moverse con cuatro teclas. /*RESERVAR DIRECCIONES DE MEMORIA*/ ALLEGRO_DISPLAY *ventana = NULL; ALLEGRO_BITMAP *sprites = NULL; ALLEGRO_EVENT_QUEUE *evento = NULL; ALLEGRO_TIMER *reloj = NULL; //al_set_window_title(dis, "PAC - SON"); bool redraw = true; bool key[4] = {false, false, false, false}; bool doexit = false; struct Nave jugador; /*INICIAR ALLEGRO*/ al_init(); al_install_keyboard(); bool al_init_image_addon(void); /*CREAR*/ ventana = al_create_display(PANTALLA_X, PANTALLA_Y); evento = al_create_event_queue(); reloj = al_create_timer(1/60.); sprites = al_load_bitmap("images/todasposiciones.png"); /*al_convert_mask_to_alpha(sprites, al_map_rgb(0, 0, 0));*/ /*REGISTRAR EVENTOS*/ al_register_event_source(evento, al_get_display_event_source(ventana)); al_register_event_source(evento, al_get_timer_event_source(reloj)); al_register_event_source(evento, al_get_keyboard_event_source()); //al_flip_display(); posicionar(sprites, &jugador); al_set_target_bitmap(al_get_backbuffer(ventana)); al_start_timer(reloj); /*BUCLE*/ while(!doexit){ ALLEGRO_EVENT ev; al_wait_for_event(evento, &ev); if(ev.type == ALLEGRO_EVENT_TIMER){ if(key[KEY_UP] && jugador.y > 0) jugador.y -= VELOCIDAD; if(key[KEY_DOWN] && jugador.y < PANTALLA_Y) jugador.y += VELOCIDAD; if(key[KEY_LEFT] && jugador.x > 0) jugador.x -= VELOCIDAD; if(key[KEY_RIGHT] && jugador.x < PANTALLA_X) jugador.x += VELOCIDAD; redraw = true; } else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE){ break; } else if(ALLEGRO_EVENT_KEY_DOWN){ switch(ev.keyboard.keycode){ case ALLEGRO_KEY_UP: key[KEY_UP] = true; break; case ALLEGRO_KEY_DOWN: key[KEY_DOWN] = true; break; case ALLEGRO_KEY_LEFT: key[KEY_LEFT] = true; break; case ALLEGRO_KEY_RIGHT: key[KEY_RIGHT] = true; break; } } else if(ALLEGRO_EVENT_KEY_UP){ switch(ev.keyboard.keycode){ case ALLEGRO_KEY_UP: key[KEY_UP] = false; break; case ALLEGRO_KEY_DOWN: key[KEY_DOWN] = false; break; case ALLEGRO_KEY_LEFT: key[KEY_LEFT] = false; break; case ALLEGRO_KEY_RIGHT: key[KEY_RIGHT] = false; break; } } if(redraw && al_is_event_queue_empty(evento)) redraw = false; al_draw_bitmap(jugador.img, jugador.x, jugador.y, 0); al_clear_to_color(al_map_rgb(0,30,30)); al_draw_bitmap(jugador.img, jugador.x, jugador.y, 0); al_flip_display(); } /*DESTRUIR*/ al_destroy_timer(reloj); al_destroy_display(ventana); al_destroy_event_queue(evento); /*al_destroy_bitmap(sprites);*/ return 0; }
int main(void) { ALLEGRO_DISPLAY *display; ALLEGRO_BITMAP *cursor; ALLEGRO_EVENT_QUEUE *queue; ALLEGRO_EVENT event; ALLEGRO_FONT *font; int mx = 0; int my = 0; int mz = 0; int mw = 0; int mmx = 0; int mmy = 0; int mmz = 0; int mmw = 0; bool in = true; bool buttons[NUM_BUTTONS] = {false}; int i; float p = 0.0; ALLEGRO_COLOR black; if (!al_init()) { abort_example("Could not init Allegro.\n"); return 1; } al_init_primitives_addon(); al_install_mouse(); al_install_keyboard(); al_init_image_addon(); al_init_font_addon(); actual_buttons = al_get_mouse_num_buttons(); if (actual_buttons > NUM_BUTTONS) actual_buttons = NUM_BUTTONS; al_set_new_display_flags(ALLEGRO_RESIZABLE); display = al_create_display(640, 480); if (!display) { abort_example("Error creating display\n"); return 1; } al_hide_mouse_cursor(display); cursor = al_load_bitmap("data/cursor.tga"); if (!cursor) { abort_example("Error loading cursor.tga\n"); return 1; } font = al_load_font("data/fixed_font.tga", 1, 0); if (!font) { abort_example("data/fixed_font.tga not found\n"); return 1; } black = al_map_rgb_f(0, 0, 0); queue = al_create_event_queue(); al_register_event_source(queue, al_get_mouse_event_source()); al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_display_event_source(display)); while (1) { if (al_is_event_queue_empty(queue)) { al_clear_to_color(al_map_rgb(0xff, 0xff, 0xc0)); for (i = 0; i < actual_buttons; i++) { draw_mouse_button(i, buttons[i]); } al_draw_bitmap(cursor, mx, my, 0); al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA); al_draw_textf(font, black, 5, 5, 0, "dx %i, dy %i, dz %i, dw %i", mmx, mmy, mmz, mmw); al_draw_textf(font, black, 5, 25, 0, "x %i, y %i, z %i, w %i", mx, my, mz, mw); al_draw_textf(font, black, 5, 45, 0, "p = %g", p); al_draw_textf(font, black, 5, 65, 0, "%s", in ? "in" : "out"); al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA); mmx = mmy = mmz = 0; al_flip_display(); } al_wait_for_event(queue, &event); switch (event.type) { case ALLEGRO_EVENT_MOUSE_AXES: mx = event.mouse.x; my = event.mouse.y; mz = event.mouse.z; mw = event.mouse.w; mmx = event.mouse.dx; mmy = event.mouse.dy; mmz = event.mouse.dz; mmw = event.mouse.dw; p = event.mouse.pressure; break; case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN: if (event.mouse.button-1 < NUM_BUTTONS) { buttons[event.mouse.button-1] = true; } p = event.mouse.pressure; break; case ALLEGRO_EVENT_MOUSE_BUTTON_UP: if (event.mouse.button-1 < NUM_BUTTONS) { buttons[event.mouse.button-1] = false; } p = event.mouse.pressure; break; case ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY: in = true; break; case ALLEGRO_EVENT_MOUSE_LEAVE_DISPLAY: in = false; break; case ALLEGRO_EVENT_KEY_DOWN: if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { goto done; } break; case ALLEGRO_EVENT_DISPLAY_RESIZE: al_acknowledge_resize(event.display.source); break; case ALLEGRO_EVENT_DISPLAY_CLOSE: goto done; } } done: al_destroy_event_queue(queue); return 0; }
bool movida::init() { // allegro cout << "allegro ... "; if(!al_init()) { cout << "FAILED" << endl; return 1; }else cout << "OK" << endl; // allegro - KEYBOARD cout << " - install keyboard ... "; if(!al_install_keyboard()) { cout << "FAILED" << endl; return 2; }else cout << "OK" << endl; // allegro - MOUSE cout << " - install mouse ... "; if(!al_install_mouse()) { cout << "FAILED" << endl; return 3; }else cout << "OK" << endl; // allegro - primitives cout << " - init primitives ... "; if(!al_init_primitives_addon()) { cout << "FAILED" << endl; return 4; }else cout << "OK" << endl; // allegro - IMAGE cout << " - init image addon ... "; if(!al_init_image_addon()) { cout << "FAILED" << endl; return 4; }else cout << "OK" << endl; // allegro - FONT al_init_font_addon(); // allegro - FONT -* TTF cout << " - init fonts true type module ... "; if(!al_init_ttf_addon()) { cout << "FAILED" << endl; return 5; }else cout << "OK" << endl; // allegro - TIMER cout << " - create timer ... "; timer = al_create_timer(1.0 / 60); // 60 ticks a second if(!timer) { cout << "FAILED" << endl; return 6; }else cout << "OK" << endl; // allegro - EVENTS cout << " - create event_queue ... "; event_queue = al_create_event_queue(); if(!event_queue) { cout << "FAILED" << endl; return 7; }else cout << "OK" << endl; // allegro - screen cout << " - create screen ... "; al_set_new_display_flags(ALLEGRO_WINDOWED); screen = al_create_display(config::instance()->resX, config::instance()->resY); if(!screen) { cout << "FAILED" << endl; return 8; }else cout << "OK" << endl; // register events al_register_event_source(event_queue, al_get_mouse_event_source()); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_register_event_source(event_queue, al_get_display_event_source(screen)); al_start_timer(timer); gameFont = al_load_ttf_font("data/times.ttf",14,0 ); // load recources gameInterface->load(); gameWorld->load(); // gfx::instance()->init(1, "/home/xuedi/projects/slapEngine/movida/data/gfx/"); // loads sfNone.bmp ad default !! // gfx::instance()->load("sfSlap.bmp"); gameHadInit = 1; return 0; }
void show_options() { int licznik=0; int x,y; x=500; y=100; ALLEGRO_FONT * font_ttf = al_load_ttf_font("cour.ttf",80, 0); ALLEGRO_FONT * font_ttfbolt = al_load_ttf_font("courbd.ttf",80, 0); ALLEGRO_BITMAP *bitmapa = al_load_bitmap( "media/menu.png" ); bool done=false,draw=false; al_draw_bitmap (bitmapa, 0, 0, 0); al_draw_textf(font_ttfbolt,al_map_rgb(255,255,255), x, y,ALLEGRO_ALIGN_CENTRE,"muzyka"); al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y+100,ALLEGRO_ALIGN_CENTRE,"dźwięki"); al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y+200,ALLEGRO_ALIGN_CENTRE,"powrót do menu"); al_flip_display(); ALLEGRO_EVENT_QUEUE *event_queue1 = al_create_event_queue(); ALLEGRO_KEYBOARD_STATE keyState; //al_register_event_source(event_queue, al_get_mouse_event_source()); al_register_event_source(event_queue1, al_get_display_event_source(display)); al_register_event_source(event_queue1, al_get_keyboard_event_source()); while(!done) { ALLEGRO_EVENT events; al_wait_for_event(event_queue1, &events); al_get_keyboard_state(&keyState); if(events.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { done = true; } else if (al_key_down( &keyState, ALLEGRO_KEY_ESCAPE)) { done=true; } else if(al_key_down(&keyState, ALLEGRO_KEY_ENTER)) { if(licznik==2) { done=true; draw=false; } else if(licznik==0) { show_sound(); draw=true; } else if(licznik==1) { show_sound2(); draw=true; } } else if(al_key_down(&keyState, ALLEGRO_KEY_UP)) { licznik--; if(licznik<0)licznik=2; draw=true; } else if(al_key_down(&keyState, ALLEGRO_KEY_DOWN)) { licznik++; if(licznik>2)licznik=0; draw=true; } if(draw) { draw=false; al_draw_bitmap (bitmapa, 0, 0, 0); if(licznik==0) { al_draw_textf(font_ttfbolt,al_map_rgb(255,255,255), x, y,ALLEGRO_ALIGN_CENTRE,"muzyka"); al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y+100,ALLEGRO_ALIGN_CENTRE,"dźwięki"); al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y+200,ALLEGRO_ALIGN_CENTRE,"powrót do menu"); al_rest(0.15); } else if(licznik==1) { al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y,ALLEGRO_ALIGN_CENTRE,"muzyka"); al_draw_textf(font_ttfbolt,al_map_rgb(255,255,255), x, y+100,ALLEGRO_ALIGN_CENTRE,"dźwięki"); al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y+200,ALLEGRO_ALIGN_CENTRE,"powrót do menu"); al_rest(0.15); } else if(licznik==2) { al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y,ALLEGRO_ALIGN_CENTRE,"muzyka"); al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y+100,ALLEGRO_ALIGN_CENTRE,"dźwięki"); al_draw_textf(font_ttfbolt,al_map_rgb(255,255,255), x, y+200,ALLEGRO_ALIGN_CENTRE,"powrót do menu"); al_rest(0.15); } al_flip_display(); } } al_destroy_bitmap(bitmapa); al_destroy_event_queue(event_queue1); al_destroy_font(font_ttf); al_destroy_font(font_ttfbolt); }
void show_menu() { int licznik=0; int x,y; x=500; y=100; ALLEGRO_FONT * font_ttf = al_load_ttf_font("cour.ttf",80, 0); ALLEGRO_FONT * font_ttfbolt = al_load_ttf_font("courbd.ttf",80, 0); ALLEGRO_BITMAP *bitmapa = al_load_bitmap( "media/menu.png" ); bool done=false,draw=false; al_draw_bitmap (bitmapa, 0, 0, 0); al_draw_textf(font_ttfbolt,al_map_rgb(255,255,255), x, y,ALLEGRO_ALIGN_CENTRE,"nowa gra"); al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y+100,ALLEGRO_ALIGN_CENTRE,"kontynuuj"); al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y+200,ALLEGRO_ALIGN_CENTRE,"opcje"); al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y+300,ALLEGRO_ALIGN_CENTRE,"wyjdź"); al_flip_display(); ALLEGRO_EVENT_QUEUE *event_queue1 = al_create_event_queue(); ALLEGRO_KEYBOARD_STATE keyState; //al_register_event_source(event_queue, al_get_mouse_event_source()); al_register_event_source(event_queue1, al_get_display_event_source(display)); al_register_event_source(event_queue1, al_get_keyboard_event_source()); while(!done) { ALLEGRO_EVENT events; al_wait_for_event(event_queue1, &events); al_get_keyboard_state(&keyState); if(events.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { done = true; } else if (al_key_down( &keyState, ALLEGRO_KEY_ESCAPE)) { done=true; } else if(al_key_down(&keyState, ALLEGRO_KEY_ENTER)) { if(licznik==3) { done=true; draw=false; } else if(licznik==0) { roszada_w_long=true; roszada_w_short=true; roszada_b_long=true; roszada_b_short=true; czyjruch=0; create_game(); show_game(); break; done=true; } else if(licznik==1) { // create_game(); load_game(); show_game(); done=true; } else { show_options(); draw=true; } } else if(al_key_down(&keyState, ALLEGRO_KEY_UP)) { licznik--; if(licznik<0)licznik=3; draw=true; } else if(al_key_down(&keyState, ALLEGRO_KEY_DOWN)) { licznik++; if(licznik>3)licznik=0; draw=true; } if(draw) { draw=false; al_draw_bitmap (bitmapa, 0, 0, 0); if(licznik==0) { al_draw_textf(font_ttfbolt,al_map_rgb(255,255,255), x, y,ALLEGRO_ALIGN_CENTRE,"nowa gra"); al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y+100,ALLEGRO_ALIGN_CENTRE,"kontynuuj"); al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y+200,ALLEGRO_ALIGN_CENTRE,"opcje"); al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y+300,ALLEGRO_ALIGN_CENTRE,"wyjdź"); al_rest(0.15); } else if(licznik==1) { al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y,ALLEGRO_ALIGN_CENTRE,"nowa gra"); al_draw_textf(font_ttfbolt,al_map_rgb(255,255,255), x, y+100,ALLEGRO_ALIGN_CENTRE,"kontynuuj"); al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y+200,ALLEGRO_ALIGN_CENTRE,"opcje"); al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y+300,ALLEGRO_ALIGN_CENTRE,"wyjdź"); al_rest(0.15); } else if(licznik==2) { al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y,ALLEGRO_ALIGN_CENTRE,"nowa gra"); al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y+100,ALLEGRO_ALIGN_CENTRE,"kontynuuj"); al_draw_textf(font_ttfbolt,al_map_rgb(255,255,255), x, y+200,ALLEGRO_ALIGN_CENTRE,"opcje"); al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y+300,ALLEGRO_ALIGN_CENTRE,"wyjdź"); al_rest(0.15); } else { al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y,ALLEGRO_ALIGN_CENTRE,"nowa gra"); al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y+100,ALLEGRO_ALIGN_CENTRE,"kontynuuj"); al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y+200,ALLEGRO_ALIGN_CENTRE,"opcje"); al_draw_textf(font_ttfbolt,al_map_rgb(255,255,255), x, y+300,ALLEGRO_ALIGN_CENTRE,"wyjdź"); al_rest(0.15); } al_flip_display(); } } al_destroy_bitmap(bitmapa); al_destroy_event_queue(event_queue1); al_destroy_font(font_ttf); al_destroy_font(font_ttfbolt); }
void show_sound() { int licznik=0; int x,y; x=500; y=100; ALLEGRO_FONT * font_ttf = al_load_ttf_font("cour.ttf",80, 0); ALLEGRO_FONT * font_ttfbolt = al_load_ttf_font("courbd.ttf",80, 0); ALLEGRO_BITMAP *bitmapa = al_load_bitmap( "media/menu.png" ); bool done=false,draw=false; al_draw_bitmap (bitmapa, 0, 0, 0); al_draw_textf(font_ttfbolt,al_map_rgb(255,255,255), x, y,ALLEGRO_ALIGN_CENTRE,"on"); al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y+100,ALLEGRO_ALIGN_CENTRE,"off"); al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y+200,ALLEGRO_ALIGN_CENTRE,"cofnij"); al_flip_display(); ALLEGRO_EVENT_QUEUE *event_queue1 = al_create_event_queue(); ALLEGRO_KEYBOARD_STATE keyState; //al_register_event_source(event_queue, al_get_mouse_event_source()); al_register_event_source(event_queue1, al_get_display_event_source(display)); al_register_event_source(event_queue1, al_get_keyboard_event_source()); while(!done) { ALLEGRO_EVENT events; al_wait_for_event(event_queue1, &events); al_get_keyboard_state(&keyState); if(events.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { done = true; } else if (al_key_down( &keyState, ALLEGRO_KEY_ESCAPE)) { done=true; } else if(al_key_down(&keyState, ALLEGRO_KEY_ENTER)) { if(licznik==2) { done=true; draw=false; } else if(licznik==0) { if(!sample) al_reserve_samples(2); sample= al_load_sample("media/The Wild Hunt.wav"); al_play_sample(sample, 1.0, 0.0,1.0,ALLEGRO_PLAYMODE_LOOP,NULL); FILE *fp; fp=fopen("ustawienia1.txt","w"); fprintf(fp,"%d", 1); fclose(fp); done=true; } else if(licznik==1) { // if(!sample) al_destroy_sample(sample); FILE *fp; fp=fopen("ustawienia1.txt","w"); fprintf(fp,"%d", 0); fclose(fp); done=true; } } else if(al_key_down(&keyState, ALLEGRO_KEY_UP)) { licznik--; if(licznik<0)licznik=2; draw=true; } else if(al_key_down(&keyState, ALLEGRO_KEY_DOWN)) { licznik++; if(licznik>2)licznik=0; draw=true; } if(draw) { draw=false; al_draw_bitmap (bitmapa, 0, 0, 0); if(licznik==0) { al_draw_textf(font_ttfbolt,al_map_rgb(255,255,255), x, y,ALLEGRO_ALIGN_CENTRE,"on"); al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y+100,ALLEGRO_ALIGN_CENTRE,"off"); al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y+200,ALLEGRO_ALIGN_CENTRE,"cofnij"); al_rest(0.15); } else if(licznik==1) { al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y,ALLEGRO_ALIGN_CENTRE,"on"); al_draw_textf(font_ttfbolt,al_map_rgb(255,255,255), x, y+100,ALLEGRO_ALIGN_CENTRE,"off"); al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y+200,ALLEGRO_ALIGN_CENTRE,"cofnij"); al_rest(0.15); } else if(licznik==2) { al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y,ALLEGRO_ALIGN_CENTRE,"on"); al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y+100,ALLEGRO_ALIGN_CENTRE,"off"); al_draw_textf(font_ttfbolt,al_map_rgb(255,255,255), x, y+200,ALLEGRO_ALIGN_CENTRE,"cofnij"); al_rest(0.15); } al_flip_display(); } } al_destroy_bitmap(bitmapa); al_destroy_event_queue(event_queue1); al_destroy_font(font_ttf); al_destroy_font(font_ttfbolt); }
int main(void) { ALLEGRO_DISPLAY *display; ALLEGRO_TIMER *timer; ALLEGRO_EVENT_QUEUE *queue; ALLEGRO_BITMAP *bmp; ALLEGRO_BITMAP *mem_bmp; ALLEGRO_BITMAP *disp_bmp; ALLEGRO_FONT *font; char *text; bool done = false; bool redraw = true; if (!al_init()) { abort_example("Failed to init Allegro.\n"); return 1; } if (!al_init_image_addon()) { abort_example("Failed to init IIO addon.\n"); return 1; } al_init_font_addon(); display = al_create_display(640, 480); if (!display) { abort_example("Error creating display.\n"); return 1; } if (!al_install_keyboard()) { abort_example("Error installing keyboard.\n"); return 1; } font = al_load_font("data/fixed_font.tga", 0, 0); if (!font) { abort_example("Error loading data/fixed_font.tga\n"); return 1; } bmp = disp_bmp = al_load_bitmap("data/mysha.pcx"); if (!bmp) { abort_example("Error loading data/mysha.pcx\n"); return 1; } text = "Display bitmap (space to change)"; al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP); mem_bmp = al_load_bitmap("data/mysha.pcx"); if (!bmp) { abort_example("Error loading data/mysha.pcx\n"); return 1; } timer = al_create_timer(INTERVAL); queue = al_create_event_queue(); al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_timer_event_source(timer)); al_register_event_source(queue, al_get_display_event_source(display)); al_start_timer(timer); al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA); while (!done) { ALLEGRO_EVENT event; if (redraw && al_is_event_queue_empty(queue)) { update(bmp); al_clear_to_color(al_map_rgb_f(0, 0, 0)); al_draw_tinted_bitmap(bmp, al_map_rgba_f(1, 1, 1, 0.5), bmp_x, bmp_y, bmp_flag); al_draw_text(font, al_map_rgba_f(1, 1, 1, 0.5), 0, 0, 0, text); al_flip_display(); redraw = false; } al_wait_for_event(queue, &event); switch (event.type) { case ALLEGRO_EVENT_KEY_DOWN: if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) done = true; else if (event.keyboard.keycode == ALLEGRO_KEY_SPACE) { if (bmp == mem_bmp) { bmp = disp_bmp; text = "Display bitmap (space to change)"; } else { bmp = mem_bmp; text = "Memory bitmap (space to change)"; } } break; case ALLEGRO_EVENT_DISPLAY_CLOSE: done = true; break; case ALLEGRO_EVENT_TIMER: redraw = true; break; } } al_destroy_bitmap(bmp); return 0; }
int main(int argc, char **argv){ if(argc==3){ if(strcmp(argv[1],"rand")==0 && strcmp(argv[1],"map/maps.txt")!=0){ map_aleatoire(argv[2], 1000,100); printf("map cree\n"); return 0; } } /* Creation de l ecran */ ALLEGRO_DISPLAY *display = NULL; /* Creation du timer */ ALLEGRO_TIMER *timer; /* Creation de la liste d'evenement */ ALLEGRO_EVENT_QUEUE *queue; bool redraw = true; /*bool boucle_jeu=true;*/ /* Initialisation d allegro */ if(!al_init()) { fprintf(stderr, "failed to initialize allegro!\n"); return -1; } if(!al_init_image_addon()) { fprintf(stderr, "failed to initialize allegro image addon!\n"); return -1; } if(!al_init_primitives_addon()) { fprintf(stderr, "failed to initialize allegro primitive addon!\n"); return -1; } if(!al_install_mouse()) { fprintf(stderr, "failed to initialize allegro mouse !\n"); return -1; } if(!al_install_keyboard()) { fprintf(stderr, "failed to initialize allegro keyboard!\n"); return -1; } if(!al_install_joystick()){ fprintf(stderr, "failed to initialize allegro joystick!\n"); return -1; } if(!al_install_audio()) { fprintf(stderr, "failed to initialize allegro audio!\n"); /*return -1;*/ } if(!al_init_acodec_addon()){ fprintf(stderr, "failed to initialize allegro audio codec!\n"); /*return -1;*/ } if (!al_reserve_samples(2)){ fprintf(stderr, "failed to initialize allegro reserve sample 1!\n"); /*return -1;*/ } al_init_font_addon(); if(!al_init_ttf_addon()){ fprintf(stderr, "failed to initialize allegro ttf addon!\n"); return -1; } Game* game; /* Instantiation de l ecran */ display = al_create_display(DISPLAY_W,DISPLAY_H); if(!display) { fprintf(stderr, "failed to create display!\n"); return -1; } bool recommencer = true; while(recommencer){ recommencer = false; game = game_init(); al_hide_mouse_cursor(display); /* Instantiation de la liste d evenement */ queue = al_create_event_queue(); /* Instantiation du timer de jeu */ timer = al_create_timer(1.0/FPS); al_start_timer(timer); /* Enregistrement des evenement */ al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_mouse_event_source()); al_register_event_source(queue, al_get_joystick_event_source()); al_register_event_source(queue, al_get_display_event_source(display)); al_register_event_source(queue, al_get_timer_event_source(timer)); /* ECRAN TITRE */ al_clear_to_color(al_map_rgb(0,0,0)); al_draw_bitmap_region(game->jeu->map->background,250,200,game->jeu->map->w*LENGTH_SPRITE,game->jeu->map->h*LENGTH_SPRITE,0,0,0); al_draw_text(game->jeu->font_jeu,al_map_rgb(0,0,0),280,150,ALLEGRO_ALIGN_LEFT,"Titre du jeu"); al_draw_text(game->jeu->font_jeu,al_map_rgb(0,0,0),280,190,ALLEGRO_ALIGN_LEFT,"Press enter"); if(al_is_audio_installed()){ ALLEGRO_SAMPLE* main_musique; main_musique = al_load_sample("musique/musique_titre.ogg"); if (!main_musique){ fprintf(stderr, "Erreur lors du chargement musique d'intro\n"); } al_play_sample(main_musique, 0.1, 0.0,1.0,ALLEGRO_PLAYMODE_LOOP,NULL); } al_flip_display(); bool debut = false; Event* event_titre; ALLEGRO_EVENT event_titre_event; event_titre = event_init(EVENT_PERIPH_XBOX); while(debut != true){ al_wait_for_event(queue, &event_titre_event); if(event_get_ok_court(event_titre)){ debut = true; } event_update(event_titre,&event_titre_event); } if(al_is_audio_installed()){ al_stop_samples(); } /* Ecran de chargement du premier niveau */ char num_niveau[4]; sprintf(num_niveau,"%d",game->current_map_i); al_clear_to_color(al_map_rgb(0,0,0)); if(game->size == game->current_map_i){ al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),280,190,ALLEGRO_ALIGN_LEFT,"Loading final stage"); }else{ al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),300,190,ALLEGRO_ALIGN_LEFT,"Loading level "); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),440,190,ALLEGRO_ALIGN_LEFT,num_niveau); } al_flip_display(); sleep(2); al_clear_to_color(al_map_rgb(0,0,0)); al_flip_display(); /* Début de la boucle de jeu */ game->jeu->ev->son = true; map_set_frame(game->jeu->map,DISPLAY_W/2-200,100,200,DISPLAY_H-300); while(game->jeu->boucle_jeu){ game_next_map_test(game); ALLEGRO_EVENT event; al_wait_for_event(queue, &event); /* On demande a quitter le jeu */ if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE ){ game->jeu->boucle_jeu=false; } event_update(game->jeu->ev,&event); /* Evenement timer */ if(event.type == ALLEGRO_EVENT_TIMER){ redraw=true; } /* if(event.type == ALLEGRO_EVENT_DISPLAY_RESIZE){ al_acknowledge_resize(display); redraw = true; } */ /* affichage */ if(redraw && al_is_event_queue_empty(queue)){ redraw=false; game->jeu->boucle_jeu = jeu_update_event(game->jeu); jeu_draw(game->jeu); } } /* Ecran de fin du jeu */ if(game->success == true){ al_clear_to_color(al_map_rgb(0,0,0)); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),400,100,ALLEGRO_ALIGN_CENTER,"Félicitation, vous avez terminés les premiers niveaux du jeu !"); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),400,120,ALLEGRO_ALIGN_CENTER,"Ce jeu à été créé à l'occasion du premier projet de C de"); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),400,140,ALLEGRO_ALIGN_CENTER,"la promotion 2016 de Télécom Nancy"); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),400,160,ALLEGRO_ALIGN_CENTER,"par"); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),400,180,ALLEGRO_ALIGN_CENTER,"Giannini Valentin et Eric Perlinski"); al_flip_display(); sleep(5); al_clear_to_color(al_map_rgb(0,0,0)); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),400,150,ALLEGRO_ALIGN_CENTER,"Appuyez sur [ESC] pour quitter, ou sur [Entrer] pour recommencer"); al_flip_display(); }else{ al_clear_to_color(al_map_rgb(0,0,0)); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),400,100,ALLEGRO_ALIGN_CENTER,"Vous avez épuisés toutes les vies de votre personnage !"); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),400,120,ALLEGRO_ALIGN_CENTER,"Ce jeu à été créé à l'occasion du premier projet de C de"); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),400,140,ALLEGRO_ALIGN_CENTER,"la promotion 2016 de Télécom Nancy"); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),400,160,ALLEGRO_ALIGN_CENTER,"par"); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),400,180,ALLEGRO_ALIGN_CENTER,"Gianini Valentin et Eric Perlinski"); al_flip_display(); sleep(5); al_clear_to_color(al_map_rgb(0,0,0)); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),400,150,ALLEGRO_ALIGN_CENTER,"Appuyez sur [ESC] pour quitter, ou sur [Entrer] pour recommencer"); al_flip_display(); } /* TODO Chargement écran de fin */ bool action = false; Event* event_credits; ALLEGRO_EVENT event_credits_event; event_credits = event_init(EVENT_PERIPH_XBOX); while(action != true){ al_wait_for_event(queue, &event_credits_event); if(event_get_ok_court(event_credits)){ action = true; recommencer = true; }else if (event_get_menu(event_credits)){ action = true; recommencer = false; } event_update(event_credits,&event_credits_event); } } jeu_dest(game->jeu); al_destroy_display(display); return 0; }
int main(void) { // don't forget to put allegro-5.0.10-monolith-md-debug.lib //primitive variable bool done = false; bool redraw = true; const int FPS = 60; bool isGameOver = false; //object variables spaceShip ship; Bullet bullets[NUM_BULLETS]; Comet comets[NUM_COMETS]; Explosion explosions[NUM_EXPLOSIONS]; //Allegro variables ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_TIMER *timer = NULL; ALLEGRO_FONT *font18 = NULL; ALLEGRO_BITMAP *shipImage; ALLEGRO_BITMAP *cometImage; ALLEGRO_BITMAP *expImage; ALLEGRO_SAMPLE * sample = NULL; ALLEGRO_SAMPLE * sample2 = NULL; ALLEGRO_SAMPLE * sample3 = NULL; ALLEGRO_SAMPLE_INSTANCE * instance1 = NULL; ALLEGRO_SAMPLE_INSTANCE * instance2 = NULL; ALLEGRO_SAMPLE_INSTANCE * instance3 = NULL; //Initialization Functions if(!al_init()) //initialize Allegro return -1; display = al_create_display(WIDTH, HEIGHT); //create our display object if(!display) //test display object return -1; al_init_primitives_addon(); al_install_keyboard(); al_init_font_addon(); al_init_ttf_addon(); al_init_image_addon(); al_install_audio(); // always initialize audio before the codecs al_init_acodec_addon(); al_reserve_samples(10); // reserves numbers of samples/ channels or voices sample = al_load_sample("chirp.ogg"); sample2 = al_load_sample("static.ogg"); sample3 = al_load_sample("JSS - Our Song.ogg"); instance1 = al_create_sample_instance(sample); instance2 = al_create_sample_instance(sample2); instance3 = al_create_sample_instance(sample3); al_set_sample_instance_playmode(instance3, ALLEGRO_PLAYMODE_LOOP); al_attach_sample_instance_to_mixer(instance1, al_get_default_mixer()); al_attach_sample_instance_to_mixer(instance2, al_get_default_mixer()); al_attach_sample_instance_to_mixer(instance3, al_get_default_mixer()); event_queue = al_create_event_queue(); timer = al_create_timer(1.0 / FPS); cometImage = al_load_bitmap("asteroid-1-96.png"); //above does not need convert_mask_to_alpha because it is tranparent background in sprite sheet shipImage = al_load_bitmap("Spaceship_by_arboris.png"); al_convert_mask_to_alpha(shipImage,al_map_rgb(255,0,255)); expImage = al_load_bitmap("explosion_3_40_128.png"); srand(time(NULL)); InitShip(ship, shipImage); InitBullet(bullets, NUM_BULLETS); InitComet(comets, NUM_COMETS, cometImage); InitExplosions(explosions, NUM_EXPLOSIONS, expImage); font18 = al_load_font("arial.ttf", 18, 0); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_register_event_source(event_queue, al_get_display_event_source(display)); al_play_sample_instance(instance3); al_start_timer(timer); while(!done) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); if(ev.type == ALLEGRO_EVENT_TIMER) { redraw = true; int soundX = ship.x; int soundY = ship.y; if(keys[UP]) MoveShipUp(ship); else if(keys[DOWN]) MoveShipDown(ship); else ResetShipAnimation(ship,1); if(keys[LEFT]) MoveShipLeft(ship); else if(keys[RIGHT]) MoveShipRight(ship); else ResetShipAnimation(ship, 2); if(soundX != ship.x || soundY != ship.y){ //al_play_sample(sample, 1,0,1, ALLEGRO_PLAYMODE_ONCE, NULL); al_play_sample_instance(instance1); } if (ship.x -10 < 0 || ship.x + 10 > WIDTH || ship.y - 10 < 0 || ship.y + 10 > HEIGHT){ al_play_sample_instance(instance2); } if(!isGameOver) { UpdateExplosions(explosions, NUM_EXPLOSIONS); UpdateBullet(bullets, NUM_BULLETS); StartComet(comets, NUM_COMETS); UpdateComet(comets, NUM_COMETS); CollideBullet(bullets, NUM_BULLETS, comets, NUM_COMETS, ship, explosions, NUM_EXPLOSIONS); CollideComet(comets, NUM_COMETS, ship, explosions, NUM_BULLETS); if(ship.lives <= 0) isGameOver = true; } } else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { done = true; } else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_ESCAPE: done = true; break; case ALLEGRO_KEY_UP: keys[UP] = true; break; case ALLEGRO_KEY_DOWN: keys[DOWN] = true; break; case ALLEGRO_KEY_LEFT: keys[LEFT] = true; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT] = true; break; case ALLEGRO_KEY_SPACE: keys[SPACE] = true; FireBullet(bullets, NUM_BULLETS, ship); break; } } else if(ev.type == ALLEGRO_EVENT_KEY_UP) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_ESCAPE: done = true; break; case ALLEGRO_KEY_UP: keys[UP] = false; break; case ALLEGRO_KEY_DOWN: keys[DOWN] = false; break; case ALLEGRO_KEY_LEFT: keys[LEFT] = false; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT] = false; break; case ALLEGRO_KEY_SPACE: keys[SPACE] = false; break; } } if(redraw && al_is_event_queue_empty(event_queue)) { redraw = false; if(!isGameOver) { DrawShip(ship); if(al_get_sample_instance_playing(instance1)){ al_draw_text(font18, al_map_rgb(255,255,255),5,30,0, "Instance 1 is playing"); } if(al_get_sample_instance_playing(instance2)){ al_draw_text(font18, al_map_rgb(255,255,255),WIDTH - 5,30,ALLEGRO_ALIGN_RIGHT, "Instance 2 is playing"); } if(al_get_sample_instance_playing(instance3)){ al_draw_textf(font18, al_map_rgb(255,255,255),5,HEIGHT - 30,0, "Instance 3 is playing: %.1f %%", al_get_sample_instance_position(instance3) / (float)al_get_sample_instance_length(instance3) * 100); } DrawBullet(bullets, NUM_BULLETS); DrawComet(comets, NUM_COMETS); DrawExplosions(explosions, NUM_EXPLOSIONS); al_draw_textf(font18, al_map_rgb(255, 0, 255), 5, 5, 0, "Player has %i lives left. Player has destroyed %i objects", ship.lives, ship.score); } else { al_draw_textf(font18, al_map_rgb(0, 255, 255), WIDTH / 2, HEIGHT / 2, ALLEGRO_ALIGN_CENTRE, "Game Over. Final Score: %i", ship.score); } al_flip_display(); al_clear_to_color(al_map_rgb(0,0,0)); } } al_destroy_sample_instance(instance1); al_destroy_sample_instance(instance2); al_destroy_sample_instance(instance3); al_destroy_sample(sample); al_destroy_sample(sample2); al_destroy_sample(sample3); al_destroy_bitmap(expImage); al_destroy_bitmap(shipImage); al_destroy_bitmap(cometImage); al_destroy_event_queue(event_queue); al_destroy_timer(timer); al_destroy_font(font18); al_destroy_display(display); //destroy our display object return 0; }
int main(void) { //primitive variable bool done = false; bool redraw = true; const int FPS = 60; bool isGameOver = false; //object variables spaceShip ship; Bullet bullets[NUM_BULLETS]; Comet comets[NUM_COMETS]; //Allegro variables ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_TIMER *timer = NULL; ALLEGRO_FONT *font18 = NULL; ALLEGRO_BITMAP *shipImage; ALLEGRO_BITMAP *cometImage; //Initialization Functions if(!al_init()) //initialize Allegro return -1; display = al_create_display(WIDTH, HEIGHT); //create our display object if(!display) //test display object return -1; al_init_primitives_addon(); al_install_keyboard(); al_init_font_addon(); al_init_ttf_addon(); al_init_image_addon(); event_queue = al_create_event_queue(); timer = al_create_timer(1.0 / FPS); cometImage = al_load_bitmap("asteroid-1-96.png"); //above does not need convert_mask_to_alpha because it is tranparent background in sprite sheet shipImage = al_load_bitmap("Spaceship_by_arboris.png"); al_convert_mask_to_alpha(shipImage,al_map_rgb(255,0,255)); srand(time(NULL)); InitShip(ship, shipImage); InitBullet(bullets, NUM_BULLETS); InitComet(comets, NUM_COMETS, cometImage); font18 = al_load_font("arial.ttf", 18, 0); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_register_event_source(event_queue, al_get_display_event_source(display)); al_start_timer(timer); while(!done) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); if(ev.type == ALLEGRO_EVENT_TIMER) { redraw = true; if(keys[UP]) MoveShipUp(ship); else if(keys[DOWN]) MoveShipDown(ship); else ResetShipAnimation(ship,1); if(keys[LEFT]) MoveShipLeft(ship); else if(keys[RIGHT]) MoveShipRight(ship); else ResetShipAnimation(ship, 2); if(!isGameOver) { UpdateBullet(bullets, NUM_BULLETS); StartComet(comets, NUM_COMETS); UpdateComet(comets, NUM_COMETS); CollideBullet(bullets, NUM_BULLETS, comets, NUM_COMETS, ship); CollideComet(comets, NUM_COMETS, ship); if(ship.lives <= 0) isGameOver = true; } } else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { done = true; } else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_ESCAPE: done = true; break; case ALLEGRO_KEY_UP: keys[UP] = true; break; case ALLEGRO_KEY_DOWN: keys[DOWN] = true; break; case ALLEGRO_KEY_LEFT: keys[LEFT] = true; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT] = true; break; case ALLEGRO_KEY_SPACE: keys[SPACE] = true; FireBullet(bullets, NUM_BULLETS, ship); break; } } else if(ev.type == ALLEGRO_EVENT_KEY_UP) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_ESCAPE: done = true; break; case ALLEGRO_KEY_UP: keys[UP] = false; break; case ALLEGRO_KEY_DOWN: keys[DOWN] = false; break; case ALLEGRO_KEY_LEFT: keys[LEFT] = false; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT] = false; break; case ALLEGRO_KEY_SPACE: keys[SPACE] = false; break; } } if(redraw && al_is_event_queue_empty(event_queue)) { redraw = false; if(!isGameOver) { DrawShip(ship); DrawBullet(bullets, NUM_BULLETS); DrawComet(comets, NUM_COMETS); al_draw_textf(font18, al_map_rgb(255, 0, 255), 5, 5, 0, "Player has %i lives left. Player has destroyed %i objects", ship.lives, ship.score); } else { al_draw_textf(font18, al_map_rgb(0, 255, 255), WIDTH / 2, HEIGHT / 2, ALLEGRO_ALIGN_CENTRE, "Game Over. Final Score: %i", ship.score); } al_flip_display(); al_clear_to_color(al_map_rgb(0,0,0)); } } al_destroy_bitmap(shipImage); al_destroy_bitmap(cometImage); al_destroy_event_queue(event_queue); al_destroy_timer(timer); al_destroy_font(font18); al_destroy_display(display); //destroy our display object return 0; }
/* dialog_message: * Sends a message to all the objects in a dialog. If any of the objects * return values other than D_O_K, returns the value and sets obj to the * object which produced it. */ int t3gui_dialog_message(T3GUI_ELEMENT *dialog, int msg, int c, int *obj) { int count, res, r, force, try; assert(dialog); force = ((msg == MSG_START) || (msg == MSG_END) || (msg >= MSG_USER)); res = D_O_K; /* If a menu spawned by a d_menu_proc object is active, the dialog engine * has effectively been shutdown for the sake of safety. This means that * we can't send the message to the other objects in the dialog. So try * first to send the message to the d_menu_proc object and, if the menu * is then not active anymore, send it to the other objects as well. */ //if (active_menu_player) { // try = 2; // menu_dialog = active_menu_player->dialog; //} //else try = 1; for (; try > 0; try--) { for (count=0; dialog[count].proc; count++) { //if ((try == 2) && (&dialog[count] != menu_dialog)) // continue; if ((force) || (!(dialog[count].flags & D_HIDDEN))) { r = t3gui_object_message(dialog+count, msg, c); if (r != D_O_K) { res |= r; if (msg != MSG_DRAW && msg != MSG_IDLE && msg != MSG_TIMER && obj) { printf("new object: %d\n", count); *obj = count; } } if ((msg == MSG_IDLE) && (dialog[count].flags & (D_DIRTY | D_HIDDEN)) == D_DIRTY) { dialog[count].flags &= ~D_DIRTY; res |= D_REDRAW; } } } //if (active_menu_player) // break; } return res; } int t3gui_do_dialog(T3GUI_ELEMENT *dialog, int focus) { return t3gui_do_dialog_interval(dialog, 0.0, focus); } int t3gui_do_dialog_interval(T3GUI_ELEMENT *dialog, double speed_sec, int focus) { T3GUI_PLAYER *player = t3gui_init_dialog(dialog, focus, 0, NULL, NULL, NULL); ALLEGRO_TIMER *timer = NULL; t3gui_listen_for_events(player, al_get_keyboard_event_source()); t3gui_listen_for_events(player, al_get_mouse_event_source()); t3gui_listen_for_events(player, al_get_joystick_event_source()); t3gui_listen_for_events(player, al_get_display_event_source(al_get_current_display())); if (speed_sec > 0.0) { timer = al_create_timer(speed_sec); t3gui_listen_for_events(player, al_get_timer_event_source(timer)); al_start_timer(timer); } ALLEGRO_EVENT_QUEUE *menu_queue = al_create_event_queue(); al_register_event_source(menu_queue, t3gui_get_player_event_source(player)); t3gui_start_dialog(player); bool in_dialog = true; bool must_draw = false; while (in_dialog) { if (must_draw) { must_draw = false; t3gui_draw_dialog(player); al_flip_display(); } al_wait_for_event(menu_queue, NULL); while (!al_is_event_queue_empty(menu_queue)) { ALLEGRO_EVENT event; al_get_next_event(menu_queue, &event); switch (event.type) { case T3GUI_EVENT_REDRAW: //printf("Redraw request\n"); must_draw = true; break; case T3GUI_EVENT_CLOSE: focus = event.user.data1; in_dialog = false; break; case T3GUI_EVENT_CANCEL: focus = -1; in_dialog = false; break; } } } t3gui_stop_dialog(player); al_destroy_timer(timer); al_destroy_event_queue(menu_queue); t3gui_shutdown_dialog(player); return focus; }
/* * keyboard driver is called first, so initialize allegro here */ int init_allegro(void){ if(!al_init()) { #if _ANDROID_ __android_log_print(ANDROID_LOG_INFO,"AllegroActivity","Error!, Allegro has failed to initialize."); #else fprintf(stderr,"Error!, Allegro has failed to initialize.\n"); #endif return -1; } else { //fprintf(stderr,"al_init successful\n"); } #if _ANDROID_ //al_set_new_display_flags(ALLEGRO_FULLSCREEN_WINDOW); al_set_new_display_flags(ALLEGRO_WINDOWED); //al_set_new_display_option(ALLEGRO_COLOR_SIZE,32,ALLEGRO_SUGGEST); //al_set_new_display_option(ALLEGRO_CAN_DRAW_INTO_BITMAP,1,ALLEGRO_REQUIRE); #endif //al_set_new_display_option(ALLEGRO_COLOR_SIZE,32,ALLEGRO_SUGGEST); display = al_create_display(SCREEN_WIDTH, SCREEN_HEIGHT); //display = al_create_display(800, 600); if(display == NULL) { #if _ANDROID_ __android_log_print(ANDROID_LOG_INFO,"AllegroActivity","Error!, Failed to create the display."); #else fprintf(stderr,"Error!, Failed to create the display."); #endif return -1; } al_set_system_mouse_cursor(display,ALLEGRO_SYSTEM_MOUSE_CURSOR_DEFAULT); if(!al_install_keyboard()) { fprintf(stderr,"Error!, Failed to install keyboard.\n"); return -1; } else { //fprintf(stderr,"al_install_keyboard successful\n"); } if(!al_install_mouse()) { fprintf(stderr,"Error!, Failed to install mouse."); return -1; } a_event_queue_k = al_create_event_queue(); if(a_event_queue_k == NULL) { fprintf(stderr,"Error!, Failed to create the keyboard event queue."); return -1; } al_register_event_source(a_event_queue_k, al_get_keyboard_event_source()); a_event_queue_m = al_create_event_queue(); if(a_event_queue_m == NULL) { fprintf(stderr,"Error!, Failed to create the mouse event queue."); return -1; } #if _ANDROID_ //do below at touch input #else al_register_event_source(a_event_queue_m, al_get_mouse_event_source()); #endif a_event_queue_d = al_create_event_queue(); if(a_event_queue_d == NULL) { fprintf(stderr,"Error!, Failed to create the display event queue."); return -1; } al_register_event_source(a_event_queue_d, al_get_display_event_source(display)); #if _ANDROID_ if(!al_install_touch_input()) { __android_log_print(ANDROID_LOG_INFO,"AllegroActivity","Error!, Failed to install touch_input."); } else { __android_log_print(ANDROID_LOG_INFO,"AllegroActivity","Installed touch_input successfully."); al_set_mouse_emulation_mode(ALLEGRO_MOUSE_EMULATION_5_0_x); al_register_event_source(a_event_queue_m, al_get_touch_input_mouse_emulation_event_source()); } #endif #if _ANDROID_ //allow to use al_open to read (only) files from the apk store, e.g. fonts al_android_set_apk_file_interface(); #endif return 1; //ok }
void correr(void){ const int CANTIBOTONES=4; ALLEGRO_TIMER *timer=NULL; ALLEGRO_EVENT_QUEUE *queue=NULL; ALLEGRO_BITMAP *main=NULL, *icons[CANTIBOTONES]; bool redraw=false; ALLEGRO_FONT *font=NULL; boton items[CANTIBOTONES]; void (*p2buttonf[CANTIBOTONES])(void); //create display, event queue and timer display = al_create_display(SCREEN_W, SCREEN_H); queue = al_create_event_queue(); timer = al_create_timer(1.0/FPS); //register events al_register_event_source(queue, al_get_display_event_source(display)); al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_mouse_event_source()); al_register_event_source(queue, al_get_timer_event_source(timer)); //load bitmap main=al_load_bitmap("main.jpg"); al_draw_bitmap(main,0,0,0); //mainfondo switch((wallnumber=get_wallp())){ // carga de wallpaper por archivo de configuracion case 1:cambiarfondo1(); break; case 2:cambiarfondo2(); break; case 3:cambiarfondo3(); break; case 4:cambiarfondo4(); break; } exitmenu=false; //wallpaper icon icons[0]=al_load_bitmap("wallpaper-icon.png"); botones(&items[0], 30,OFFSETDOWN-BUTTONSIZE-25, BUTTONSIZE, BUTTONSIZE); p2buttonf[0]=correrwallp; //message icon icons[1]=al_load_bitmap("message-icon.png"); botones(&items[1], 30*2+BUTTONSIZE*1,OFFSETDOWN-BUTTONSIZE-25, BUTTONSIZE, BUTTONSIZE); p2buttonf[1]=corrercontactos; //music icon icons[2]=al_load_bitmap("musica.png"); botones(&items[2], 30*3+BUTTONSIZE*2,OFFSETDOWN-BUTTONSIZE-25, BUTTONSIZE, BUTTONSIZE); p2buttonf[2]=corrermusica; //fractal icon icons[3]=al_load_bitmap("fractal.png"); botones(&items[3], 30*4+BUTTONSIZE*3,OFFSETDOWN-BUTTONSIZE-25, BUTTONSIZE, BUTTONSIZE); p2buttonf[3]=correrfractal; //start timer and font al_start_timer(timer); font=al_load_font("digital.ttf",72,0); while(!doexit) // main loop { ALLEGRO_EVENT ev; al_wait_for_event(queue, &ev); // espero a timer, salida o keyboard if(ev.type == ALLEGRO_EVENT_TIMER) redraw = true; // si es el timer de refresco, redibujo salio(ev);// salio del celular= presionobotones(ev, items, p2buttonf,CANTIBOTONES); // presiono en los icons al_flush_event_queue(queue); if(redraw){ // si hay que redibujar drawfondo();// fondo drawbotones(items, icons,CANTIBOTONES);// dibujo icons hora(font);//horario redraw=false; al_flush_event_queue(queue); al_flip_display();//flipeo } } al_destroy_display(display); al_destroy_timer(timer); al_destroy_bitmap(main); al_destroy_bitmap(mainfondo); al_destroy_event_queue(queue); }
int main(int argc, const char* argv[]) { al_init(); al_init_font_addon(); al_init_ttf_addon(); al_init_image_addon(); al_init_primitives_addon(); ALLEGRO_DISPLAY *display; al_set_new_display_flags(ALLEGRO_WINDOWED); display = al_create_display(800, 600); al_install_keyboard(); al_install_mouse(); ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue(); al_register_event_source(event_queue, (ALLEGRO_EVENT_SOURCE *)display); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_register_event_source(event_queue, al_get_mouse_event_source()); ALLEGRO_FONT* font = al_load_ttf_font("data/times.ttf", 12, 0); Vector2 camera(-400, -300); Platforms platforms; Bitmaps bitmaps; Sprites sprites; File* file = new File(font); file->platforms = &platforms; file->bitmaps = &bitmaps; file->sprites = &sprites; Menu menu; menu.Add_entry(new Create_platform(platforms, camera, font)); menu.Add_entry(new Edit_platform(platforms, camera, font)); menu.Add_entry(file); menu.Add_entry(new Create_sprite(bitmaps, sprites, camera, font)); menu.Add_entry(new Edit_sprite(sprites, camera, font)); if(argc==2) file->Load(argv[1]); while(1) { ALLEGRO_EVENT event; if (al_get_next_event(event_queue, &event)) { if (ALLEGRO_EVENT_DISPLAY_CLOSE == event.type || ALLEGRO_EVENT_KEY_DOWN == event.type && ALLEGRO_KEY_ESCAPE == event.keyboard.keycode) { break; } if(ALLEGRO_EVENT_MOUSE_AXES) { ALLEGRO_MOUSE_STATE mstate; al_get_mouse_state(&mstate); if(al_mouse_button_down(&mstate, 3)) { camera.x -= event.mouse.dx; camera.y -= event.mouse.dy; } } menu.Event(event); } for(Sprites::iterator i = sprites.begin(); i != sprites.end(); ++i) { (*i)->Draw(camera); } for(Platforms::iterator i = platforms.begin(); i != platforms.end(); ++i) { (*i)->Draw(camera, al_map_rgb_f(0, 1, 0)); } al_draw_line(0, -camera.y, 800, -camera.y, al_map_rgba_f(0, 1, 0, 0.5), 0); al_draw_line(-camera.x, 0, -camera.x, 600, al_map_rgba_f(0, 0, 1, 0.5), 0); menu.Draw(); al_flip_display(); al_clear_to_color(al_map_rgb(0, 0, 0)); al_rest(0.001); } al_destroy_event_queue(event_queue); al_destroy_display(display); }
int main (int argc, char *argv[]) { Ship* ship = NULL; ALLEGRO_TIMER *main_timer = NULL; ALLEGRO_DISPLAY *display = NULL; ALLEGRO_COLOR cyan = al_map_rgb(0,255,255); ALLEGRO_BITMAP *space_background = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; bool redraw = true; ALLEGRO_EVENT ev; if(!al_init()) { fprintf(stderr, "Error initializing Allegro"); exit(EXIT_FAILURE); } if(!al_init_image_addon()) { fprintf(stderr, "Error initializing Allegro"); exit(EXIT_FAILURE); } if(!al_install_keyboard()) { fprintf(stderr, "Error initializing keyboard\n"); exit(EXIT_FAILURE); } display=al_create_display(W, H); if(display == NULL) { fprintf(stderr, "Error creating main display"); exit(EXIT_FAILURE); } space_background = al_load_bitmap(BACKGROUND_FILE); if(space_background == NULL) { fprintf(stderr, "Error loading background\n"); exit(EXIT_FAILURE); } ship=create_new_ship("../data/spaceship.png"); ship->current_dir = DOWN; al_set_target_backbuffer(display); al_draw_bitmap(space_background, 0, 0, 0); print_ship_on_screen(ship); al_flip_display(); main_timer=al_create_timer(1.0/FPS); if(main_timer == NULL) { fprintf(stderr, "Error creating main timer\n"); exit(EXIT_FAILURE); } event_queue = al_create_event_queue(); al_register_event_source(event_queue, al_get_timer_event_source(main_timer)); al_register_event_source(event_queue, al_get_display_event_source(display)); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_start_timer(main_timer); while(1) { al_wait_for_event(event_queue, &ev); if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } else { switch (ev.type) { case ALLEGRO_EVENT_TIMER: redraw = true; break; case ALLEGRO_EVENT_KEY_DOWN: switch(ev.keyboard.keycode) { case ALLEGRO_KEY_UP: ship->is_moving = 1; ship->current_dir = UP; break; case ALLEGRO_KEY_DOWN: ship->is_moving = 1; ship->current_dir = DOWN; break; case ALLEGRO_KEY_LEFT: ship->is_moving = 1; ship->current_dir = LEFT; break; case ALLEGRO_KEY_RIGHT: ship->is_moving = 1; ship->current_dir = RIGHT; break; default: break; } break; case ALLEGRO_EVENT_KEY_UP: ship->is_moving = 0; break; default: break; } } if(redraw && al_event_queue_is_empty(event_queue)) { redraw = false; al_clear_to_color(al_map_rgb(0,0,0)); al_draw_bitmap(space_background, 0, 0, 0); move_ship(ship); animate_ship(ship); print_ship_on_screen(ship); al_flip_display(); } } al_destroy_timer(main_timer); al_destroy_display(display); al_destroy_event_queue(event_queue); return 0; }
static void mainloop(void) { ALLEGRO_EVENT_QUEUE *queue; ALLEGRO_TIMER *timer; float *buf; double pitch = 440; int i, si; int n = 0; bool redraw = false; for (i = 0; i < N; i++) { frequency[i] = 22050 * pow(2, i / (double)N); stream[i] = al_create_audio_stream(4, SAMPLES_PER_BUFFER, frequency[i], ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_1); if (!stream[i]) { abort_example("Could not create stream.\n"); } if (!al_attach_audio_stream_to_mixer(stream[i], al_get_default_mixer())) { abort_example("Could not attach stream to mixer.\n"); } } queue = al_create_event_queue(); al_register_event_source(queue, al_get_keyboard_event_source()); for (i = 0; i < N; i++) { al_register_event_source(queue, al_get_audio_stream_event_source(stream[i])); } #ifdef ALLEGRO_POPUP_EXAMPLES if (textlog) { al_register_event_source(queue, al_get_native_text_log_event_source(textlog)); } #endif log_printf("Generating %d sine waves of different sampling quality\n", N); log_printf("If Allegro's resampling is correct there should be little variation\n", N); timer = al_create_timer(1.0 / 60); al_register_event_source(queue, al_get_timer_event_source(timer)); al_register_event_source(queue, al_get_display_event_source(display)); al_start_timer(timer); while (n < 60 * frequency[0] / SAMPLES_PER_BUFFER * N) { ALLEGRO_EVENT event; al_wait_for_event(queue, &event); if (event.type == ALLEGRO_EVENT_AUDIO_STREAM_FRAGMENT) { for (si = 0; si < N; si++) { buf = al_get_audio_stream_fragment(stream[si]); if (!buf) { continue; } for (i = 0; i < SAMPLES_PER_BUFFER; i++) { double t = samplepos[si]++ / (double)frequency[si]; buf[i] = sin(t * pitch * ALLEGRO_PI * 2) / N; } if (!al_set_audio_stream_fragment(stream[si], buf)) { log_printf("Error setting stream fragment.\n"); } n++; log_printf("%d", si); if ((n % 60) == 0) log_printf("\n"); } } if (event.type == ALLEGRO_EVENT_TIMER) { redraw = true; } if (event.type == ALLEGRO_EVENT_KEY_DOWN && event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { break; } if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } #ifdef ALLEGRO_POPUP_EXAMPLES if (event.type == ALLEGRO_EVENT_NATIVE_DIALOG_CLOSE) { break; } #endif if (redraw &&al_is_event_queue_empty(queue)) { ALLEGRO_COLOR c = al_map_rgb(0, 0, 0); int i; al_clear_to_color(al_map_rgb_f(1, 1, 1)); for (i = 0; i < 640; i++) { al_draw_pixel(i, 50 + waveform[i] * 50, c); } al_flip_display(); redraw = false; } } for (si = 0; si < N; si++) { al_drain_audio_stream(stream[si]); } log_printf("\n"); al_destroy_event_queue(queue); }
int main(){ // variables {{{1 int x=10; int y=10; int estado=2; ALLEGRO_DISPLAY *Screen; ALLEGRO_EVENT_QUEUE *qu; ALLEGRO_EVENT Event; ALLEGRO_TIMER *timer; ALLEGRO_BITMAP *Image = NULL; ALLEGRO_BITMAP *background= NULL; bool Exit = false; //}}}1 // Iniciando allegro {{{1 if(!al_init()){ cout<<"eror iniciando allegro"<<endl; return -1; } if(!al_init_image_addon()){ cout<<"error iniciando addon de imagenes"<<endl; return -1; } if(!al_init_primitives_addon()){ cout<<"Couldn't initialize primitives addon!\n"; return -1; } if(!al_install_keyboard()){ cout<<"error iniciando teclado"<<endl; return -1; } //}}}1 // pantalla y resoluciones {{{1 ALLEGRO_DISPLAY_MODE disp_data; int n=al_get_num_display_modes(); bool continuar_resolucion_w=false; bool continuar_resolucion_h=false; for(int i=0;i<n;i++){ al_get_display_mode(i, &disp_data); int w=disp_data.width; int h=disp_data.height; if(w==SWIDTH){ continuar_resolucion_w=true; } if(h==SHEIGHT){ continuar_resolucion_h=true; } } if(continuar_resolucion_w==false && continuar_resolucion_h==false){ cerr<<"resolucion no soportada"<<endl; return -1; } //al_set_new_display_flags(ALLEGRO_FULLSCREEN); Screen = al_create_display(SWIDTH,SHEIGHT); if(!Screen){ cout<<"no se pudo crear el display"<<endl; } //}}}1 // crear queue - timer screen keyboard {{{1 qu= al_create_event_queue(); if(!qu){ cout<<"error creando queue"<<endl; } al_register_event_source(qu, al_get_display_event_source(Screen)); timer = al_create_timer(1.0 / 35); if(!timer){ cout<<"error iniciando timer"<<endl; } al_register_event_source(qu, al_get_timer_event_source(timer)); al_register_event_source(qu, al_get_keyboard_event_source()); //}}}1 // iniciar clases y timer{{{1 // 0 inicio // 1 menu // 2 core Inicio inicio; Menu menu; Core core; inicio.Iniciar(); core.Iniciar_core(Screen); al_start_timer(timer); Image = al_load_bitmap("data/test.png"); ///load the bitmap from a file background= al_load_bitmap("data/background.png"); // ALLEGRO_COLOR clearcol=al_map_rgb(255,255,255); //}}}1 // main loop {{{1 while(Exit == false){ al_wait_for_event(qu, &Event); if(al_is_event_queue_empty(qu)){ if(estado==0){ inicio.Display(Screen); menu.Iniciar_menu(Screen); estado=1; } if(estado==1){ menu.Display(Screen); core.Iniciar_core(Screen); } if(estado==2){ x+=1; core.Animar(); al_draw_bitmap(background,0,0,0); core.Display(Screen); al_draw_bitmap(Image,x,y,0); } al_flip_display(); } if(Event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { Exit = true; } if(Event.type == ALLEGRO_EVENT_TIMER){ } if(Event.type==ALLEGRO_EVENT_KEY_DOWN){ Exit=true; } } return 0; }
int main(){ float* terrain = (float*) malloc(RES*RES*sizeof(float)); genterrain(terrain, RES, 40000, 2.3, true); al_init(); al_install_keyboard(); ALLEGRO_DISPLAY *display; al_set_new_display_flags(ALLEGRO_OPENGL); display = al_create_display(1280, 960); ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue(); al_register_event_source(event_queue, al_get_display_event_source(display)); al_register_event_source(event_queue, al_get_keyboard_event_source()); bool quit = false; glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); float* verts = (float*) malloc(RES*RES*4*4*3); float* colors = (float*) malloc(RES*RES*4*4*3); float* vp = verts; float* cp = colors; for(int x = 0; x < RES-1; x++){ for(int z = 0; z < RES-1; z++){ *(vp++) = (x-RES/2.0+0.5)*1024.0/RES; *(vp++) = terrain[x*RES+z]; *(vp++) = (z-RES/2.0+0.5)*1024.0/RES; *(cp++) = 0.5*terrain[x*RES+z]; *(cp++) = 1-terrain[x*RES+z]; *(cp++) = 0.5*terrain[x*RES+z]; *(vp++) = (x-RES/2.0+1.5)*1024.0/RES; *(vp++) = terrain[(x+1)*RES+z]; *(vp++) = (z-RES/2.0+0.5)*1024.0/RES; *(cp++) = 0.5*terrain[(x+1)*RES+z]; *(cp++) = 1-terrain[(x+1)*RES+z]; *(cp++) = 0.5*terrain[(x+1)*RES+z]; *(vp++) = (x-RES/2.0+1.5)*1024.0/RES; *(vp++) = terrain[(x+1)*RES+z+1]; *(vp++) = (z-RES/2.0+1.5)*1024.0/RES; *(cp++) = 0.5*terrain[(x+1)*RES+z+1]; *(cp++) = 1-terrain[(x+1)*RES+z+1]; *(cp++) = 0.5*terrain[(x+1)*RES+z+1]; *(vp++) = (x-RES/2.0+0.5)*1024.0/RES; *(vp++) = terrain[x*RES+z+1]; *(vp++) = (z-RES/2.0+1.5)*1024.0/RES; *(cp++) = 0.5*terrain[x*RES+z+1]; *(cp++) = 1-terrain[x*RES+z+1]; *(cp++) = 0.5*terrain[x*RES+z+1]; } } glVertexPointer(3, GL_FLOAT, 0, verts); glColorPointer(3, GL_FLOAT, 0, colors); glEnable(GL_CULL_FACE); glCullFace(GL_FRONT); glEnable(GL_DEPTH_TEST); glDepthMask(GL_TRUE); /*glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST); glEnable(GL_POLYGON_SMOOTH);*/ while(!quit){ double ms = al_current_time(); ALLEGRO_EVENT event; if (al_get_next_event(event_queue, &event)){ if (ALLEGRO_EVENT_KEY_DOWN == event.type && ALLEGRO_KEY_Q == event.keyboard.keycode || ALLEGRO_EVENT_DISPLAY_CLOSE == event.type){ break; } } glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); gluPerspective(45, 640.0/480.0,.1,1000); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glRotatef(30, 1, 0, 0); glTranslatef(0, -100, -200); glScalef(1,20,1); glRotatef(ms*12, 0, 1, 0); glPolygonMode(GL_FRONT, GL_LINES); glDrawArrays(GL_QUADS, 0, RES*RES*4); al_flip_display(); } return 0; }
int init(ALLEGRO_DISPLAY ** display, ALLEGRO_EVENT_QUEUE ** event_queue, ALLEGRO_TIMER ** timer) { *display = NULL; *event_queue=NULL; *timer=NULL; if(!al_init()) { fprintf(stderr, "Failed to initialise allegro!\n"); return 0; } *timer=al_create_timer(1.0/FPS); if(!*timer) { fprintf(stderr, "Failed to create timer!\n"); return 0; } *display=al_create_display(SCREEN_W, SCREEN_H); if(!*display) { fprintf(stderr, "Failed to create display\n"); return 0; } if(!al_init_primitives_addon()) { fprintf(stderr, "Failed to initialise primitives!\n"); return 0; } if(!al_init_image_addon()) { fprintf(stderr, "Failed to initialise image addon\n"); return 0; } if(!al_install_mouse()) { fprintf(stderr, "Could not initialise mouse bindings!\n"); return 0; } if(!al_install_keyboard()) { fprintf(stderr, "Could not initilaise keyboard bindings!\n"); return 0; } al_init_font_addon(); al_init_ttf_addon(); *event_queue=al_create_event_queue(); if(!*event_queue) { fprintf(stderr, "Failed to create event queue!\n"); return 0; } al_register_event_source(*event_queue, al_get_display_event_source(*display)); al_register_event_source(*event_queue, al_get_timer_event_source(*timer)); al_register_event_source(*event_queue, al_get_mouse_event_source()); al_register_event_source(*event_queue, al_get_keyboard_event_source()); al_clear_to_color(al_map_rgb(255,255,255)); al_flip_display(); al_start_timer(*timer); //initialise some horrible global constants! wall_img=al_load_bitmap("wall.png"); floor_img=al_load_bitmap("floor.png"); player_img=al_load_bitmap("player.png"); enemy_img=al_load_bitmap("enemy.png"); return 1; }
int mainAdam(void) { //variables const int numSprites = 1; BaseMonster orbs[numSprites]; Player mario; Map map; Menu menu; Keyboard keyboard; bool done = false; bool render = false; settings.init(); //int xOff = 0; //int yOff = 0; //allegro variable ALLEGRO_DISPLAY *display = NULL; ALLEGRO_DISPLAY_MODE disp_data; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_TIMER *timer; ALLEGRO_BITMAP *image; //program init if (!al_init()) //initialize Allegro return -1; int i = al_get_num_display_modes(); al_get_display_mode(1, &disp_data); //al_set_new_display_flags(ALLEGRO_FULLSCREEN); display = al_create_display(disp_data.width, disp_data.height); //create our display object if (!display) //test display object return -1; //addon init keyboard.init(display); al_init_image_addon(); al_install_audio(); al_init_acodec_addon(); al_reserve_samples(1); mario.Init(&map,1); globalText.init(); //menu.init(); if (map.init("Maps/test.fmp")) { return -5; } for (int i = 0; i < numSprites; i++) orbs[i].Init(); event_queue = al_create_event_queue(); timer = al_create_timer(1.0 / 60); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_start_timer(timer); while (!keyboard.keys[Keyboard::ESC]) { /*if (menu.state == END) break;*/ ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); keyboard.update(ev); if (ev.type == ALLEGRO_EVENT_TIMER) { /*if (menu.state == MENU) { menu.update(keyboard.keys); menu.updateBackgrounds(); } else {*/ if (!mario.win) { for (int i = 0; i < numSprites; i++){ orbs[i].Update(); mario.collisionWithOther(&orbs[i]); for (int v = 0; v < bumpingBlockAnimation.size(); v++) bumpingBlockAnimation[v].collisionWithOther(&orbs[i]); } mario.Update(keyboard.keys); for (int v = 0; v < bumpingBlockAnimation.size(); v++) bumpingBlockAnimation[v].Update(); for (int v = 0; v < destroyBrickAnimation.size(); v++) destroyBrickAnimation[v].Update(); for (int v = 0; v < jumpingCoinAnimation.size(); v++) jumpingCoinAnimation[v].Update(); if (map.item->live) { map.item->Update(); map.item->collisionWithOther(&mario); for (int v = 0; v < bumpingBlockAnimation.size(); v++) bumpingBlockAnimation[v].collisionWithOther(map.item); } //} } else { } render = true; } if (render && al_is_event_queue_empty(event_queue)) { render = false; /*if (menu.state == MENU) { menu.drawBackgrounds(); } else {*/ map.draw(); globalText.update(&mario); globalText.draw(); mario.Draw(); for (int v = 0; v < bumpingBlockAnimation.size(); v++) bumpingBlockAnimation[v].Draw(); for (int v = 0; v < destroyBrickAnimation.size(); v++) destroyBrickAnimation[v].Draw(); for (int v = 0; v < jumpingCoinAnimation.size(); v++) jumpingCoinAnimation[v].Draw(); if (map.item->live) map.item->Draw(); for (int i = 0; i < numSprites; i++) orbs[i].Draw(); //} al_flip_display(); al_clear_to_color(al_map_rgb(0, 0, 0)); } } map.del(); keyboard.del(); al_destroy_event_queue(event_queue); al_destroy_display(display); //destroy our display object return 0; }
int main(void) { int num_joysticks; ALLEGRO_EVENT_QUEUE *queue; ALLEGRO_JOYSTICK *curr_joy; ALLEGRO_DISPLAY *display; if (!al_init()) { abort_example("Could not init Allegro.\n"); } if (!al_install_joystick()) { abort_example("Could not init joysticks.\n"); } al_install_keyboard(); al_init_primitives_addon(); open_log(); display = al_create_display(640, 480); if (!display) { abort_example("Could not create display.\n"); } queue = al_create_event_queue(); al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_joystick_event_source()); al_register_event_source(queue, al_get_display_event_source(display)); num_joysticks = al_get_num_joysticks(); log_printf("Num joysticks: %d\n", num_joysticks); if (num_joysticks > 0) { curr_joy = al_get_joystick(0); print_joystick_info(curr_joy); } else { curr_joy = NULL; } draw(curr_joy); while (1) { ALLEGRO_EVENT event; al_wait_for_event(queue, &event); if (event.type == ALLEGRO_EVENT_KEY_DOWN && event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { break; } else if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } else if (event.type == ALLEGRO_EVENT_KEY_CHAR) { int n = event.keyboard.unichar - '0'; if (n >= 0 && n < num_joysticks) { curr_joy = al_get_joystick(n); log_printf("switching to joystick %d\n", n); print_joystick_info(curr_joy); } } else if (event.type == ALLEGRO_EVENT_JOYSTICK_CONFIGURATION) { int old = al_get_num_joysticks(); al_reconfigure_joysticks(); num_joysticks = al_get_num_joysticks(); log_printf("after reconfiguration num joysticks = %d\n", num_joysticks); if (curr_joy) { log_printf("current joystick is: %s\n", al_get_joystick_active(curr_joy) ? "active" : "inactive"); } if (old < num_joysticks) { curr_joy = al_get_joystick(0); } } else if (event.type == ALLEGRO_EVENT_JOYSTICK_AXIS) { log_printf("axis event from %p, stick %d, axis %d\n", event.joystick.id, event.joystick.stick, event.joystick.axis); } else if (event.type == ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN) { log_printf("button down event %d from %p\n", event.joystick.button, event.joystick.id); } draw(curr_joy); } close_log(false); return 0; }
int main(){ // don't forget to put allegro-5.0.10-monolith-md-debug.lib int width = 640; int height = 480; bool done = false; int imageRad = 20; // radius of image //allegro variable ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *eventQueue; ALLEGRO_BITMAP *image = NULL; //check if allegro is initialised <allegro_native_dialog.h> if(!al_init()){ return -1; } display = al_create_display(width,height); //check if display was created if(!display){ return -1; } al_install_keyboard();// will initialize keyboard events al_init_primitives_addon(); image = al_create_bitmap(imageRad * 2,imageRad * 2); al_set_target_bitmap(image); //prepared to draw to screen al_clear_to_color(al_map_rgb(127,127,127)); //turrets al_draw_filled_rectangle(imageRad,imageRad - 9,imageRad + 10,imageRad - 7,al_map_rgb(255,0,0)); al_draw_filled_rectangle(imageRad,imageRad + 9,imageRad + 10,imageRad + 7,al_map_rgb(255,0,0)); //ship body al_draw_filled_triangle(imageRad - 12,imageRad - 17,imageRad + 12,imageRad,imageRad - 12,imageRad + 17,al_map_rgb(0,255,0)); al_draw_filled_rectangle(imageRad - 12, imageRad - 2,imageRad + 15, imageRad + 2, al_map_rgb(0,0,255)); al_set_target_bitmap(al_get_backbuffer(display)); eventQueue = al_create_event_queue(); al_register_event_source(eventQueue, al_get_keyboard_event_source()); while(!done){ ALLEGRO_EVENT ev; al_wait_for_event(eventQueue, &ev); // tells the eventQueue wait for an input if (ev.type==ALLEGRO_EVENT_KEY_DOWN){ switch(ev.keyboard.keycode){ case ALLEGRO_KEY_ESCAPE: done = true; break; } } //make sure when exiting the program exit with esc button else if (ev.type == ALLEGRO_EVENT_KEY_UP){ if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE){ done = true; } } al_draw_bitmap(image, width / 2, height / 2, 0); al_flip_display(); al_clear_to_color(al_map_rgb(0,0,0)); //that prevents the filled rectangle draw imga just like snake style } //al_rest(5.0); al_destroy_display(display); //hey return 0; }
static void *thread_func(ALLEGRO_THREAD *thr, void *arg) { const int INITIAL_WIDTH = 300; const int INITIAL_HEIGHT = 300; const Background *background = (Background *) arg; ALLEGRO_DISPLAY *display; ALLEGRO_EVENT_QUEUE *queue = NULL; ALLEGRO_TIMER *timer = NULL; ALLEGRO_EVENT event; ALLEGRO_STATE state; Square squares[MAX_SQUARES]; double theta = 0.0; bool redraw = true; int i; (void)thr; al_set_new_display_flags(ALLEGRO_RESIZABLE); display = al_create_display(INITIAL_WIDTH, INITIAL_HEIGHT); if (!display) { goto Quit; } queue = al_create_event_queue(); if (!queue) { goto Quit; } timer = al_create_timer(0.1); if (!timer) { goto Quit; } al_register_event_source(queue, al_get_display_event_source(display)); al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_timer_event_source(timer)); for (i = 0; i < MAX_SQUARES; i++) { gen_square(&squares[i], INITIAL_WIDTH, INITIAL_HEIGHT); } al_start_timer(timer); while (true) { if (al_is_event_queue_empty(queue) && redraw) { double r = 0.7 + 0.3 * (sin(theta) + 1.0) / 2.0; ALLEGRO_COLOR c = al_map_rgb_f( background->rmax * r, background->gmax * r, background->bmax * r ); al_clear_to_color(c); al_store_state(&state, ALLEGRO_STATE_BLENDER | ALLEGRO_STATE_TRANSFORM); for (i = 0; i < MAX_SQUARES; i++) { draw_square(&squares[i]); } al_restore_state(&state); al_flip_display(); redraw = false; } al_wait_for_event(queue, &event); if (event.type == ALLEGRO_EVENT_TIMER) { for (i = 0; i < MAX_SQUARES; i++) { animate_square(&squares[i]); } theta += 0.1; redraw = true; } else if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } else if (event.type == ALLEGRO_EVENT_KEY_DOWN && event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { break; } else if (event.type == ALLEGRO_EVENT_DISPLAY_RESIZE) { al_acknowledge_resize(event.display.source); } } Quit: if (timer) { al_destroy_timer(timer); } if (queue) { al_destroy_event_queue(queue); } if (display) { al_destroy_display(display); } return NULL; }
int main(void) { //primitive variable const int FPS = 60; bool done = false; bool redraw = true; bool isGameOver = false; //object variables struct SpaceShip ship; struct Bullet bullets[NUM_BULLETS]; struct Comet comets[NUM_COMETS]; //Allegro variables ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_TIMER *timer = NULL; ALLEGRO_BITMAP *shipImage; ALLEGRO_BITMAP *cometImage; ALLEGRO_BITMAP *bulletImage; ALLEGRO_FONT *font = NULL; ALLEGRO_PATH *font_path; //Initialization Functions if (!al_init()) //initialize Allegro return -1; display = al_create_display(WIDTH, HEIGHT); //create our display object if (!display) //test display object return -1; // inicio inicializacao fontes al_init_image_addon(); font_path = get_resources_path(); al_set_path_filename(font_path, "a4_font.tga"); al_init_font_addon(); al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP); font = al_load_bitmap_font(al_path_cstr(font_path, '/')); if (!font) { return 1; } al_destroy_path(font_path); // termino inicializacao fontes al_init_primitives_addon(); al_install_keyboard(); al_init_image_addon(); event_queue = al_create_event_queue(); timer = al_create_timer(1.0 / FPS); shipImage = al_load_bitmap("spaceship_by_arboris.png"); al_convert_mask_to_alpha(shipImage, al_map_rgb(255, 0, 255)); bulletImage = al_load_bitmap("bullets_by_arboris.png"); al_convert_mask_to_alpha(bulletImage, al_map_rgb(255, 0, 255)); cometImage = al_load_bitmap("asteroid-1-96.png"); srand(time(NULL )); //Game Init ship = InitShip(shipImage); // init bullets int i = 0; while (i < NUM_BULLETS) { bullets[i].ID = BULLET; bullets[i].speed = 10; bullets[i].live = false; bullets[i].maxFrame = 143; bullets[i].curFrame = 0; bullets[i].frameCount = 0; bullets[i].frameDelay = 2; bullets[i].frameWidth = 20; bullets[i].frameHeight = 10; bullets[i].animationColumns = 1; bullets[i].animationRow = 1; bullets[i].image = bulletImage; i++; } // init comets i = 0; while (i < NUM_COMETS) { comets[i].ID = ENEMY; comets[i].live = false; comets[i].speed = 5; comets[i].boundx = 35; comets[i].boundy = 35; comets[i].maxFrame = 143; comets[i].curFrame = 0; comets[i].frameCount = 0; comets[i].frameDelay = 2; comets[i].frameWidth = 96; comets[i].frameHeight = 96; comets[i].animationColumns = 21; if (rand() % 2) comets[i].animationDirection = 1; else comets[i].animationDirection = -1; comets[i].image = cometImage; i++; } al_register_event_source(event_queue, al_get_keyboard_event_source()); al_register_event_source(event_queue, al_get_display_event_source(display)); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_start_timer(timer); while (!done) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { done = true; } else if (ev.type == ALLEGRO_EVENT_TIMER) { redraw = true; if (keys[UP]) ship = MoveShipUp(ship); else if (keys[DOWN]) ship = MoveShipDown(ship); else ship = ResetShipAnimation(ship, 1); if (keys[LEFT]) ship = MoveShipLeft(ship); else if (keys[RIGHT]) ship = MoveShipRight(ship); else ship = ResetShipAnimation(ship, 2); if (!isGameOver) { //UpdateBullet int i = 0; while (i < NUM_BULLETS) { if (bullets[i].live) { bullets[i].x += bullets[i].speed; if (bullets[i].x > WIDTH) bullets[i].live = false; } i++; } // start comet i = 0; while (i < NUM_COMETS) { if (!comets[i].live) { if (rand() % 500 == 0) { comets[i].live = true; comets[i].x = WIDTH; comets[i].y = 30 + rand() % (HEIGHT - 60); break; } } i++; } // UpdateComet(Comet comets[], int size) i = 0; while (i < NUM_COMETS) { if (comets[i].live) { if (++comets[i].frameCount >= comets[i].frameDelay) { comets[i].curFrame += comets[i].animationDirection; if (comets[i].curFrame >= comets[i].maxFrame) comets[i].curFrame = 0; else if (comets[i].curFrame <= 0) comets[i].curFrame = comets[i].maxFrame - 1; comets[i].frameCount = 0; } comets[i].x -= comets[i].speed; } i++; } // collide bullet i = 0; while (i < NUM_BULLETS) { if (bullets[i].live) { int j = 0; while (j < NUM_COMETS) { if (comets[j].live) { if (bullets[i].x > (comets[j].x - comets[j].boundx) && bullets[i].x < (comets[j].x + comets[j].boundx) && bullets[i].y > (comets[j].y - comets[j].boundy) && bullets[i].y < (comets[j].y + comets[j].boundy)) { bullets[i].live = false; comets[j].live = false; ship.score++; } } j++; } } i++; } // collide comet i = 0; while (i < NUM_COMETS) { if (comets[i].live) { if (comets[i].x - comets[i].boundx < ship.x + ship.boundx && comets[i].x + comets[i].boundx > ship.x - ship.boundx && comets[i].y - comets[i].boundy < ship.y + ship.boundy && comets[i].y + comets[i].boundy > ship.y - ship.boundy) { ship.lives--; comets[i].live = false; } else if (comets[i].x < 0) { comets[i].live = false; ship.lives--; } } i++; } if (ship.lives <= 0) isGameOver = true; } } else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch (ev.keyboard.keycode) { case ALLEGRO_KEY_ESCAPE: done = true; break; case ALLEGRO_KEY_UP: keys[UP] = true; break; case ALLEGRO_KEY_DOWN: keys[DOWN] = true; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT] = true; break; case ALLEGRO_KEY_LEFT: keys[LEFT] = true; break; case ALLEGRO_KEY_SPACE: keys[SPACE] = true; //FireBullet int i = 0; while (i < NUM_BULLETS) { if (!bullets[i].live) { bullets[i].x = ship.x + 17; bullets[i].y = ship.y; bullets[i].live = true; break; } i++; } break; } } else if (ev.type == ALLEGRO_EVENT_KEY_UP) { switch (ev.keyboard.keycode) { case ALLEGRO_KEY_UP: keys[UP] = false; break; case ALLEGRO_KEY_DOWN: keys[DOWN] = false; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT] = false; break; case ALLEGRO_KEY_LEFT: keys[LEFT] = false; break; case ALLEGRO_KEY_SPACE: keys[SPACE] = false; break; } } if (redraw && al_is_event_queue_empty(event_queue)) { redraw = false; if (!isGameOver) { DrawShip(ship); DrawBullet(bullets, NUM_BULLETS); DrawComet(comets, NUM_COMETS); al_draw_textf(font, al_map_rgb(255, 0, 255), 5, 5, 0, "Player has %i lives left. Player has destroyed %i objects", ship.lives, ship.score); } else { al_draw_textf(font, al_map_rgb(0, 255, 255), WIDTH / 2, HEIGHT / 2, ALLEGRO_ALIGN_CENTRE, "Game Over. Final Score: %i", ship.score); } al_flip_display(); al_clear_to_color(al_map_rgb(0, 0, 0)); } } al_destroy_bitmap(bulletImage); al_destroy_bitmap(cometImage); al_destroy_bitmap(shipImage); al_destroy_event_queue(event_queue); al_destroy_timer(timer); al_destroy_font(font); al_destroy_display(display); //destroy our display object return 0; }