using System; using sharpallegro5; using ALLEGRO_FONT = System.IntPtr; using ALLEGRO_BITMAP = System.IntPtr; using ALLEGRO_EVENT_QUEUE = System.IntPtr; using ALLEGRO_DISPLAY = System.IntPtr; public class ex_membmp : AllegroExample { static void print(ALLEGRO_FONT myfont, string message, int x, int y) { al_draw_text(myfont, al_map_rgb(0, 0, 0), x + 2, y + 2, 0, message, __arglist()); al_draw_text(myfont, al_map_rgb(255, 255, 255), x, y, 0, message, __arglist()); } static bool test(ALLEGRO_BITMAP bitmap, ALLEGRO_FONT font, string message) { ALLEGRO_EVENT_QUEUE queue; ALLEGRO_EVENT @event = new ALLEGRO_EVENT(); double start_time; long frames = 0; double fps = 0; string second_line; bool quit = false; queue = al_create_event_queue(); al_register_event_source(queue, al_get_keyboard_event_source()); start_time = al_get_time(); for (; ; ) { if (al_get_next_event(queue, ref @event)) { if (@event.type == ALLEGRO_EVENT_KEY_DOWN) { if (@event.keyboard.keycode == ALLEGRO_KEY_SPACE) { break; } if (@event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { quit = true; break; } } } al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ZERO); /* Clear the backbuffer with red so we can tell if the bitmap does not * cover the entire backbuffer. */ al_clear_to_color(al_map_rgb(255, 0, 0)); al_draw_scaled_bitmap(bitmap, 0, 0, al_get_bitmap_width(bitmap), al_get_bitmap_height(bitmap), 0, 0, al_get_bitmap_width(al_get_target_bitmap()), al_get_bitmap_height(al_get_target_bitmap()), 0); al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA); /* Note this makes the memory buffer case much slower due to repeated * locking of the backbuffer. Officially you can't use al_lock_bitmap * to solve the problem either. */ print(font, message, 0, 0); second_line = string.Format("{0:0.0} FPS", fps); print(font, second_line, 0, al_get_font_line_height(font) + 5); al_flip_display(); frames++; fps = (double)frames / (al_get_time() - start_time); } al_destroy_event_queue(queue); return quit; } static int Main() { ALLEGRO_DISPLAY display; ALLEGRO_FONT accelfont; ALLEGRO_FONT memfont; ALLEGRO_BITMAP accelbmp; ALLEGRO_BITMAP membmp; if (!al_init()) { abort_example("Could not init Allegro.\n"); return 1; } al_install_keyboard(); al_init_image_addon(); al_init_font_addon(); display = al_create_display(640, 400); if (display == IntPtr.Zero) { abort_example("Error creating display\n"); return 1; } accelfont = al_load_font("data/font.tga", 0, 0); if (accelfont == IntPtr.Zero) { abort_example("font.tga not found\n"); return 1; } accelbmp = al_load_bitmap("data/mysha.pcx"); if (accelbmp == IntPtr.Zero) { abort_example("mysha.pcx not found\n"); return 1; } al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP); memfont = al_load_font("data/font.tga", 0, 0); membmp = al_load_bitmap("data/mysha.pcx"); for (; ; ) { if (test(membmp, memfont, "Memory bitmap (press SPACE key)")) break; if (test(accelbmp, accelfont, "Accelerated bitmap (press SPACE key)")) break; } return 0; } }