|
a5 fade animation with timers |
Phrasz
Member #10,091
August 2008
|
So several months ago I inquired about a fade animation with alpha I finally have figured out the code and how to implement the fade in / fade out with the timer example. The code can be found below as an FYI for others. 1
2#include <stdio.h>
3#include <stdlib.h>
4#include "allegro5/allegro.h"
5#include "allegro5/allegro_image.h"
6
7#include "common.c"
8
9int main(int argc, const char *argv[])
10{
11 const char *filename;
12 ALLEGRO_DISPLAY *display;
13 ALLEGRO_BITMAP *membitmap, *bitmap;
14 ALLEGRO_TIMER *timer;
15 ALLEGRO_EVENT_QUEUE *queue;
16 bool redraw = true;
17 double zoom = 1;
18 double t0;
19 double t1;
20 bool done=0;
21 int fadespeed=0;
22
23 filename = "data/mysha.pcx";
24
25 if (!al_init()) {
26 abort_example("Could not init Allegro.\n");
27 }
28 al_install_mouse();
29 al_install_keyboard();
30 al_init_image_addon();
31
32 display = al_create_display(640, 480);
33 if (!display) {
34 abort_example("Error creating display\n");
35 }
36
37 al_set_window_title(display, filename);
38
39 /* We load the bitmap into a memory bitmap, because creating a
40 * display bitmap could fail if the bitmap is too big to fit into a
41 * single texture.
42 * FIXME: Or should A5 automatically created multiple display bitmaps?
43 */
44 al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP);
45 t0 = al_get_time();
46 membitmap = al_load_bitmap(filename);
47 t1 = al_get_time();
48 if (!membitmap) {
49 abort_example("%s not found or failed to load\n", filename);
50 }
51 al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP);
52
53 printf("Loading took %.4f seconds\n", t1 - t0);
54
55 // FIXME:
56 // Now try to split the memory bitmap into display bitmaps?
57 bitmap = al_clone_bitmap(membitmap);
58 if (!bitmap)
59 bitmap = membitmap;
60
61 timer = al_create_timer(1.0 / 60); //60 FPS
62 queue = al_create_event_queue();
63 al_register_event_source(queue, al_get_keyboard_event_source());
64 al_register_event_source(queue, al_get_display_event_source(display));
65 al_register_event_source(queue, al_get_timer_event_source(timer));
66 al_start_timer(timer);
67
68 while (1) {
69 ALLEGRO_EVENT event;
70 al_wait_for_event(queue, &event);
71
72 if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
73 break;
74 if (event.type == ALLEGRO_EVENT_KEY_CHAR) {
75 if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
76 break;
77 if (event.keyboard.keycode == ALLEGRO_KEY_SPACE)
78 done=0;
79 if (event.keyboard.unichar == '1')
80 zoom = 1;
81 if (event.keyboard.unichar == '+')
82 zoom *= 1.1;
83 if (event.keyboard.unichar == '-')
84 zoom /= 1.1;
85 if (event.keyboard.unichar == 'f')
86 zoom = (double)al_get_display_width(display) /
87 al_get_bitmap_width(bitmap);
88 }
89 if (event.type == ALLEGRO_EVENT_TIMER)
90 redraw = true;
91
92 if (redraw && al_is_event_queue_empty(queue)) {
93 redraw = false;
94
95 if (zoom == 1){
96 printf("\nLoop #%i!", fadespeed); //Debugging
97
98 if(fadespeed<256){
99 al_clear_to_color(al_map_rgb_f(0, 0, 0));
100 al_draw_tinted_bitmap(bitmap,al_map_rgba_f(1*(255-fadespeed),1*(255-fadespeed),1*(255-fadespeed),(255-fadespeed)/255.0),0,0,0);
101 al_flip_display();
102 }
103
104 if(fadespeed>256&&fadespeed<512){
105 al_clear_to_color(al_map_rgb_f(0, 0, 0));
106 al_draw_tinted_bitmap(bitmap,al_map_rgba_f(1*fadespeed,1*fadespeed,1*fadespeed,fadespeed/255.0),0,0,0);
107 al_flip_display();
108 }
109
110 fadespeed+=1;
111
112 }
113 else{
114 al_clear_to_color(al_map_rgb_f(0, 0, 0));
115 }
116
117 done=1;
118 }
119 }
120
121 al_destroy_bitmap(bitmap);
122
123 return 0;
124}
As a note: The draw tinted bitmap has an issue withe the alpha, and it can be seen in this post. Further goals: Ideally the implementation as a function is either:
|
|