Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <allegro5/allegro.h>
- #include <allegro5/allegro_image.h>
- #include <allegro5/allegro_font.h>
- #include <allegro5/allegro_direct3d.h>
- #include <allegro5/allegro_primitives.h>
- #include <stdlib.h>
- #include <math.h>
- #include "common.c"
- #define FPS 60
- static struct Example {
- ALLEGRO_DISPLAY *display;
- ALLEGRO_FONT *font;
- ALLEGRO_BITMAP *bitmaps[2][9];
- ALLEGRO_COLOR bg, fg, info;
- int bitmap;
- int ticks;
- } example;
- static int const filter_flags[6] = {
- 0,
- ALLEGRO_MIN_LINEAR,
- ALLEGRO_MIPMAP,
- ALLEGRO_MIN_LINEAR | ALLEGRO_MIPMAP,
- 0,
- ALLEGRO_MAG_LINEAR
- };
- static char const *filter_text[4] = {
- "nearest", "linear",
- "nearest mipmap", "linear mipmap"
- };
- static void update(void)
- {
- example.ticks++;
- }
- static void redraw(void)
- {
- int w = al_get_display_width(example.display);
- int h = al_get_display_height(example.display);
- int i,j;
- ALLEGRO_VERTEX v[4];
- al_clear_to_color(example.bg);
- for (i = 0; i < 6; i++) {
- float x = (i / 2) * w / 3 + w / 6;
- float y = (i % 2) * h / 2 + h / 4;
- ALLEGRO_BITMAP *bmp = example.bitmaps[example.bitmap][i];
- float bw = al_get_bitmap_width(bmp);
- float bh = al_get_bitmap_height(bmp);
- float t = 1 - 2 * fabs((example.ticks % (FPS * 16)) / 16.0 / FPS - 0.5);
- float scale;
- int size;
- float angle = example.ticks * ALLEGRO_PI * 2 / FPS / 8;
- if (i < 4)
- scale = 1 - t * 0.9;
- else
- scale = 1 + t * 9;
- al_draw_textf(example.font, example.fg, x, y - 64 - 14,
- ALLEGRO_ALIGN_CENTRE, "%s", filter_text[i % 4]);
- al_set_clipping_rectangle(x - 64, y - 64, 128, 128);
- /* al_draw_scaled_rotated_bitmap(bmp, bw / 2, bh / 2,
- x, y, scale, scale, angle, 0);*/
- for( j = 0; j < 4; j++ )
- {
- v[j].color = al_map_rgba(255,255,255,255);
- v[j].z = 0;
- }
- if( i < 2 )
- size = 256;
- else if( i < 4 )
- size = 32;
- else if ( i < 6 )
- size = 1024;
- v[0].x = x-size; v[0].y = y+size; v[0].u = 0; v[0].v = bh;
- v[1].x = x-size; v[1].y = y-size; v[1].u = 0; v[1].v = 0;
- v[2].x = x+size; v[2].y = y+size; v[2].u = bw; v[2].v = bh;
- v[3].x = x+size; v[3].y = y-size; v[3].u = bw; v[3].v = 0;
- al_draw_prim( v, NULL, bmp, 0, 4, ALLEGRO_PRIM_TRIANGLE_STRIP );
- al_set_clipping_rectangle(0, 0, w, h);
- }
- al_draw_textf(example.font, example.info, w / 2, h - 14,
- ALLEGRO_ALIGN_CENTRE, "press space to change");
- }
- int main(void)
- {
- ALLEGRO_TIMER *timer;
- ALLEGRO_EVENT_QUEUE *queue;
- int w = 640, h = 480;
- bool done = false;
- bool need_redraw = true;
- int i;
- ALLEGRO_BITMAP *mysha;
- 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();
- al_init_primitives_addon();
- // al_set_new_display_flags( ALLEGRO_OPENGL );
- al_set_new_display_flags( ALLEGRO_DIRECT3D );
- example.display = al_create_display(w, h);
- if (!example.display) {
- abort_example("Error creating display.\n");
- return 1;
- }
- if (!al_install_keyboard()) {
- abort_example("Error installing keyboard.\n");
- return 1;
- }
- if (!al_install_mouse()) {
- abort_example("Error installing mouse.\n");
- return 1;
- }
- example.font = al_load_font("data/fixed_font.tga", 0, 0);
- if (!example.font) {
- abort_example("Error loading data/fixed_font.tga\n");
- return 1;
- }
- mysha = al_load_bitmap("data/mysha256x256.png");
- if (!mysha) {
- abort_example("Error loading data/mysha256x256.png\n");
- return 1;
- }
- for (i = 0; i < 6; i++) {
- ALLEGRO_LOCKED_REGION *lock;
- int x, y;
- /* Only power-of-two bitmaps can have mipmaps. */
- al_set_new_bitmap_flags(filter_flags[i]);
- example.bitmaps[0][i] = al_create_bitmap(1024, 1024);
- example.bitmaps[1][i] = al_clone_bitmap(mysha);
- lock = al_lock_bitmap(example.bitmaps[0][i],
- ALLEGRO_PIXEL_FORMAT_ABGR_8888_LE, ALLEGRO_LOCK_WRITEONLY);
- for (y = 0; y < 1024; y++) {
- unsigned char *row = (unsigned char *)lock->data + lock->pitch * y;
- unsigned char *ptr = row;
- for (x = 0; x < 1024; x++) {
- int c = 0;
- if (((x >> 2) & 1) ^ ((y >> 2) & 1)) c = 255;
- *(ptr++) = c;
- *(ptr++) = c;
- *(ptr++) = c;
- *(ptr++) = 255;
- }
- }
- al_unlock_bitmap(example.bitmaps[0][i]);
- }
- example.bg = al_map_rgb_f(0, 0, 0);
- example.fg = al_map_rgb_f(1, 1, 1);
- example.info = al_map_rgb_f(0.5, 0.5, 1);
- timer = al_create_timer(1.0 / FPS);
- queue = al_create_event_queue();
- 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));
- al_register_event_source(queue, al_get_display_event_source(example.display));
- al_start_timer(timer);
- while (!done) {
- ALLEGRO_EVENT event;
- if (need_redraw && al_is_event_queue_empty(queue)) {
- redraw();
- al_flip_display();
- need_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;
- if (event.keyboard.keycode == ALLEGRO_KEY_SPACE)
- example.bitmap = (example.bitmap + 1) % 2;
- break;
- case ALLEGRO_EVENT_DISPLAY_CLOSE:
- done = true;
- break;
- case ALLEGRO_EVENT_TIMER:
- update();
- need_redraw = true;
- break;
- case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN:
- example.bitmap = (example.bitmap + 1) % 2;
- break;
- }
- }
- for (i = 0; i < 6; i++) {
- al_destroy_bitmap(example.bitmaps[0][i]);
- al_destroy_bitmap(example.bitmaps[1][i]);
- }
- return 0;
- }
- /* vim: set sts=3 sw=3 et: */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement