1 #include <allegro5/allegro.h>
2 #include <stdio.h>
3 
4 #include "common.c"
5 
6 #define WIDTH 640
7 #define HEIGHT 480
8 #define NUM_STARS 300
9 #define TARGET_FPS 9999
10 
11 
12 typedef struct Point
13 {
14    float x, y;
15 } Point;
16 
17 
main(int argc,char ** argv)18 int main(int argc, char **argv)
19 {
20    ALLEGRO_DISPLAY *display;
21    ALLEGRO_KEYBOARD_STATE key_state;
22    Point stars[3][NUM_STARS/3];
23    float speeds[3] = { 0.0001f, 0.05f, 0.15f };
24    ALLEGRO_COLOR colors[3];
25    long start, now, elapsed, frame_count;
26    int total_frames = 0;
27    double program_start;
28    double length;
29    int layer, star;
30 
31    (void)argc;
32    (void)argv;
33 
34    if (!al_init()) {
35       abort_example("Could not init Allegro.\n");
36    }
37 
38    open_log();
39 
40    al_install_keyboard();
41 
42    display = al_create_display(WIDTH, HEIGHT);
43    if (!display) {
44       abort_example("Could not create display.\n");
45    }
46 
47    colors[0] = al_map_rgba(255, 100, 255, 128);
48    colors[1] = al_map_rgba(255, 100, 100, 255);
49    colors[2] = al_map_rgba(100, 100, 255, 255);
50 
51    for (layer = 0; layer < 3; layer++) {
52       for (star = 0; star < NUM_STARS/3; star++) {
53          Point *p = &stars[layer][star];
54          p->x = rand() % WIDTH;
55          p->y = rand() % HEIGHT;
56       }
57    }
58 
59 
60    start = al_get_time() * 1000;
61    now = start;
62    elapsed = 0;
63    frame_count = 0;
64    program_start = al_get_time();
65 
66 
67    while (1) {
68       if (frame_count < (1000/TARGET_FPS)) {
69          frame_count += elapsed;
70       }
71       else {
72          int X, Y;
73 
74          frame_count -= (1000/TARGET_FPS);
75          al_clear_to_color(al_map_rgb(0, 0, 0));
76          for (star = 0; star < NUM_STARS/3; star++) {
77             Point *p = &stars[0][star];
78             al_draw_pixel(p->x, p->y, colors[0]);
79          }
80          al_lock_bitmap(al_get_backbuffer(display), ALLEGRO_PIXEL_FORMAT_ANY, 0);
81 
82          for (layer = 1; layer < 3; layer++) {
83             for (star = 0; star < NUM_STARS/3; star++) {
84                Point *p = &stars[layer][star];
85                // put_pixel ignores blending
86                al_put_pixel(p->x, p->y, colors[layer]);
87             }
88          }
89 
90          /* Check that dots appear at the window extremes. */
91          X = WIDTH - 1;
92          Y = HEIGHT - 1;
93          al_put_pixel(0, 0, al_map_rgb_f(1, 1, 1));
94          al_put_pixel(X, 0, al_map_rgb_f(1, 1, 1));
95          al_put_pixel(0, Y, al_map_rgb_f(1, 1, 1));
96          al_put_pixel(X, Y, al_map_rgb_f(1, 1, 1));
97 
98          al_unlock_bitmap(al_get_backbuffer(display));
99          al_flip_display();
100          total_frames++;
101       }
102 
103       now = al_get_time() * 1000;
104       elapsed = now - start;
105       start = now;
106 
107       for (layer = 0; layer < 3; layer++) {
108          for (star = 0; star < NUM_STARS/3; star++) {
109             Point *p = &stars[layer][star];
110             p->y -= speeds[layer] * elapsed;
111             if (p->y < 0) {
112                p->x = rand() % WIDTH;
113                p->y = HEIGHT;
114             }
115          }
116       }
117 
118       al_rest(0.001);
119 
120       al_get_keyboard_state(&key_state);
121       if (al_key_down(&key_state, ALLEGRO_KEY_ESCAPE))
122          break;
123    }
124 
125    length = al_get_time() - program_start;
126 
127    if (length != 0) {
128       log_printf("%d FPS\n", (int)(total_frames / length));
129    }
130 
131    al_destroy_display(display);
132 
133    close_log(true);
134 
135    return 0;
136 }
137 
138