SHOW:
|
|
- or go back to the newest paste.
1 | #include <allegro5/allegro.h> | |
2 | #include <allegro5/allegro_image.h> | |
3 | #include <allegro5/allegro_font.h> | |
4 | #include <allegro5/allegro_direct3d.h> | |
5 | #include <allegro5/allegro_primitives.h> | |
6 | #include <stdlib.h> | |
7 | #include <math.h> | |
8 | ||
9 | #include "common.c" | |
10 | ||
11 | #define FPS 60 | |
12 | ||
13 | static struct Example { | |
14 | ALLEGRO_DISPLAY *display; | |
15 | ALLEGRO_FONT *font; | |
16 | ALLEGRO_BITMAP *bitmaps[2][9]; | |
17 | ALLEGRO_COLOR bg, fg, info; | |
18 | int bitmap; | |
19 | int ticks; | |
20 | } example; | |
21 | ||
22 | static int const filter_flags[6] = { | |
23 | 0, | |
24 | ALLEGRO_MIN_LINEAR, | |
25 | ALLEGRO_MIPMAP, | |
26 | ALLEGRO_MIN_LINEAR | ALLEGRO_MIPMAP, | |
27 | 0, | |
28 | ALLEGRO_MAG_LINEAR | |
29 | }; | |
30 | ||
31 | static char const *filter_text[4] = { | |
32 | "nearest", "linear", | |
33 | "nearest mipmap", "linear mipmap" | |
34 | }; | |
35 | ||
36 | static void update(void) | |
37 | { | |
38 | example.ticks++; | |
39 | } | |
40 | ||
41 | static void redraw(void) | |
42 | { | |
43 | int w = al_get_display_width(example.display); | |
44 | int h = al_get_display_height(example.display); | |
45 | int i,j; | |
46 | ALLEGRO_VERTEX v[4]; | |
47 | ||
48 | al_clear_to_color(example.bg); | |
49 | ||
50 | for (i = 0; i < 6; i++) { | |
51 | float x = (i / 2) * w / 3 + w / 6; | |
52 | float y = (i % 2) * h / 2 + h / 4; | |
53 | ALLEGRO_BITMAP *bmp = example.bitmaps[example.bitmap][i]; | |
54 | float bw = al_get_bitmap_width(bmp); | |
55 | float bh = al_get_bitmap_height(bmp); | |
56 | float t = 1 - 2 * fabs((example.ticks % (FPS * 16)) / 16.0 / FPS - 0.5); | |
57 | float scale; | |
58 | int size; | |
59 | float angle = example.ticks * ALLEGRO_PI * 2 / FPS / 8; | |
60 | ||
61 | if (i < 4) | |
62 | scale = 1 - t * 0.9; | |
63 | else | |
64 | scale = 1 + t * 9; | |
65 | ||
66 | al_draw_textf(example.font, example.fg, x, y - 64 - 14, | |
67 | ALLEGRO_ALIGN_CENTRE, "%s", filter_text[i % 4]); | |
68 | ||
69 | al_set_clipping_rectangle(x - 64, y - 64, 128, 128); | |
70 | /* al_draw_scaled_rotated_bitmap(bmp, bw / 2, bh / 2, | |
71 | x, y, scale, scale, angle, 0);*/ | |
72 | ||
73 | for( j = 0; j < 4; j++ ) | |
74 | { | |
75 | v[j].color = al_map_rgba(255,255,255,255); | |
76 | v[j].z = 0; | |
77 | } | |
78 | if( i < 2 ) | |
79 | size = 256; | |
80 | else if( i < 4 ) | |
81 | size = 32; | |
82 | else if ( i < 6 ) | |
83 | size = 1024; | |
84 | v[0].x = x-size; v[0].y = y+size; v[0].u = 0; v[0].v = bh; | |
85 | v[1].x = x-size; v[1].y = y-size; v[1].u = 0; v[1].v = 0; | |
86 | v[2].x = x+size; v[2].y = y+size; v[2].u = bw; v[2].v = bh; | |
87 | v[3].x = x+size; v[3].y = y-size; v[3].u = bw; v[3].v = 0; | |
88 | ||
89 | al_draw_prim( v, NULL, bmp, 0, 4, ALLEGRO_PRIM_TRIANGLE_STRIP ); | |
90 | ||
91 | ||
92 | al_set_clipping_rectangle(0, 0, w, h); | |
93 | } | |
94 | al_draw_textf(example.font, example.info, w / 2, h - 14, | |
95 | ALLEGRO_ALIGN_CENTRE, "press space to change"); | |
96 | } | |
97 | ||
98 | int main(void) | |
99 | { | |
100 | ALLEGRO_TIMER *timer; | |
101 | ALLEGRO_EVENT_QUEUE *queue; | |
102 | int w = 640, h = 480; | |
103 | bool done = false; | |
104 | bool need_redraw = true; | |
105 | int i; | |
106 | ALLEGRO_BITMAP *mysha; | |
107 | ||
108 | if (!al_init()) { | |
109 | abort_example("Failed to init Allegro.\n"); | |
110 | return 1; | |
111 | } | |
112 | ||
113 | if (!al_init_image_addon()) { | |
114 | abort_example("Failed to init IIO addon.\n"); | |
115 | return 1; | |
116 | } | |
117 | ||
118 | al_init_font_addon(); | |
119 | al_init_primitives_addon(); | |
120 | // al_set_new_display_flags( ALLEGRO_OPENGL ); | |
121 | al_set_new_display_flags( ALLEGRO_DIRECT3D ); | |
122 | example.display = al_create_display(w, h); | |
123 | ||
124 | if (!example.display) { | |
125 | abort_example("Error creating display.\n"); | |
126 | return 1; | |
127 | } | |
128 | ||
129 | if (!al_install_keyboard()) { | |
130 | abort_example("Error installing keyboard.\n"); | |
131 | return 1; | |
132 | } | |
133 | ||
134 | if (!al_install_mouse()) { | |
135 | abort_example("Error installing mouse.\n"); | |
136 | return 1; | |
137 | } | |
138 | ||
139 | example.font = al_load_font("data/fixed_font.tga", 0, 0); | |
140 | if (!example.font) { | |
141 | abort_example("Error loading data/fixed_font.tga\n"); | |
142 | return 1; | |
143 | } | |
144 | ||
145 | mysha = al_load_bitmap("data/mysha256x256.png"); | |
146 | if (!mysha) { | |
147 | abort_example("Error loading data/mysha256x256.png\n"); | |
148 | return 1; | |
149 | } | |
150 | ||
151 | for (i = 0; i < 6; i++) { | |
152 | ALLEGRO_LOCKED_REGION *lock; | |
153 | int x, y; | |
154 | /* Only power-of-two bitmaps can have mipmaps. */ | |
155 | al_set_new_bitmap_flags(filter_flags[i]); | |
156 | example.bitmaps[0][i] = al_create_bitmap(1024, 1024); | |
157 | example.bitmaps[1][i] = al_clone_bitmap(mysha); | |
158 | lock = al_lock_bitmap(example.bitmaps[0][i], | |
159 | ALLEGRO_PIXEL_FORMAT_ABGR_8888_LE, ALLEGRO_LOCK_WRITEONLY); | |
160 | for (y = 0; y < 1024; y++) { | |
161 | unsigned char *row = (unsigned char *)lock->data + lock->pitch * y; | |
162 | unsigned char *ptr = row; | |
163 | for (x = 0; x < 1024; x++) { | |
164 | int c = 0; | |
165 | if (((x >> 2) & 1) ^ ((y >> 2) & 1)) c = 255; | |
166 | *(ptr++) = c; | |
167 | *(ptr++) = c; | |
168 | *(ptr++) = c; | |
169 | *(ptr++) = 255; | |
170 | } | |
171 | } | |
172 | al_unlock_bitmap(example.bitmaps[0][i]); | |
173 | ||
174 | } | |
175 | ||
176 | example.bg = al_map_rgb_f(0, 0, 0); | |
177 | example.fg = al_map_rgb_f(1, 1, 1); | |
178 | example.info = al_map_rgb_f(0.5, 0.5, 1); | |
179 | ||
180 | timer = al_create_timer(1.0 / FPS); | |
181 | ||
182 | queue = al_create_event_queue(); | |
183 | al_register_event_source(queue, al_get_keyboard_event_source()); | |
184 | al_register_event_source(queue, al_get_mouse_event_source()); | |
185 | al_register_event_source(queue, al_get_timer_event_source(timer)); | |
186 | al_register_event_source(queue, al_get_display_event_source(example.display)); | |
187 | ||
188 | al_start_timer(timer); | |
189 | ||
190 | while (!done) { | |
191 | ALLEGRO_EVENT event; | |
192 | ||
193 | if (need_redraw && al_is_event_queue_empty(queue)) { | |
194 | redraw(); | |
195 | al_flip_display(); | |
196 | need_redraw = false; | |
197 | } | |
198 | ||
199 | al_wait_for_event(queue, &event); | |
200 | switch (event.type) { | |
201 | case ALLEGRO_EVENT_KEY_DOWN: | |
202 | if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) | |
203 | done = true; | |
204 | if (event.keyboard.keycode == ALLEGRO_KEY_SPACE) | |
205 | example.bitmap = (example.bitmap + 1) % 2; | |
206 | break; | |
207 | ||
208 | case ALLEGRO_EVENT_DISPLAY_CLOSE: | |
209 | done = true; | |
210 | break; | |
211 | ||
212 | case ALLEGRO_EVENT_TIMER: | |
213 | update(); | |
214 | need_redraw = true; | |
215 | break; | |
216 | ||
217 | case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN: | |
218 | example.bitmap = (example.bitmap + 1) % 2; | |
219 | break; | |
220 | } | |
221 | } | |
222 | ||
223 | for (i = 0; i < 6; i++) { | |
224 | al_destroy_bitmap(example.bitmaps[0][i]); | |
225 | al_destroy_bitmap(example.bitmaps[1][i]); | |
226 | } | |
227 | ||
228 | return 0; | |
229 | } | |
230 | ||
231 | /* vim: set sts=3 sw=3 et: */ |