1#define _USESOUND_ 1
2
3#include <allegro5/allegro.h>
4#include <allegro5/allegro_image.h>
5#include <allegro5/allegro_primitives.h>
6#include <allegro5/allegro_android.h> /* al_android_set_apk_file_interface */
7
8#if _USESOUND_ == 1
9#include <allegro5/allegro_audio.h>
10#include <allegro5/allegro_acodec.h>
11#endif
12
13ALLEGRO_DEBUG_CHANNEL
("main")
14
15#define MAX_TOUCH 10
16
17struct touch
{
18 bool down
;
19 double x, y
;
20} touch
[MAX_TOUCH
];
21
22/* debugging */
23#define print_standard_path(std) \
24 do { \
25 ALLEGRO_PATH *path = al_get_standard_path(std); \
26 ALLEGRO_DEBUG(#std ": %s", al_path_cstr(path, '/')); \
27 } while (0)
28
29static void print_standard_paths
(void)
30{
31 print_standard_path
(ALLEGRO_RESOURCES_PATH
);
32 print_standard_path
(ALLEGRO_TEMP_PATH
);
33 print_standard_path
(ALLEGRO_USER_DATA_PATH
);
34 print_standard_path
(ALLEGRO_USER_HOME_PATH
);
35 print_standard_path
(ALLEGRO_USER_SETTINGS_PATH
);
36 print_standard_path
(ALLEGRO_USER_DOCUMENTS_PATH
);
37 print_standard_path
(ALLEGRO_EXENAME_PATH
);
38}
39
40static void set_transform
(ALLEGRO_DISPLAY *dpy
)
41{
42 ALLEGRO_TRANSFORM t
;
43 int w
= al_get_display_width(dpy
);
44 int h
= al_get_display_height(dpy
);
45
46 // XXX we shouldn't need this in user code
47 // glViewport(0, 0, w, h);
48
49 al_identity_transform(&t
);
50 al_ortho_transform
(&t,
0, w, h,
0,
-1,
1);
51 al_set_projection_transform
(dpy,
&t
);
52}
53
54static void draw_touches
(void)
55{
56 int i
;
57
58 for (i
= 0; i
< MAX_TOUCH
; i
++) {
59 if (touch
[i
].down
) {
60 al_draw_filled_rectangle(
61 touch
[i
].x-40, touch
[i
].y-40,
62 touch
[i
].x
+40, touch
[i
].y
+40,
63 al_map_rgb(100+i
*20,
40+i
*20,
40+i
*20));
64 }
65 }
66}
67
68int main
(int argc,
char **argv
)
69{
70
71#if _USESOUND_ == 1
72 ALLEGRO_SAMPLE *sample
=NULL
;
73#endif
74
75 ALLEGRO_DISPLAY *dpy
;
76 ALLEGRO_EVENT_QUEUE *queue
;
77 ALLEGRO_EVENT event
;
78 ALLEGRO_TIMER *timer
;
79 ALLEGRO_BITMAP *image
;
80
81 (void) argc
;
82 (void) argv
;
83
84 ALLEGRO_DEBUG
("init allegro!");
85 if (!al_init()) {
86 return 1;
87 }
88
89 ALLEGRO_DEBUG
("init primitives");
90 al_init_primitives_addon();
91
92 ALLEGRO_DEBUG
("init image addon");
93 al_init_image_addon();
94
95 ALLEGRO_DEBUG
("init touch input");
96 al_install_touch_input
();
97
98 ALLEGRO_DEBUG
("init keyboard");
99 al_install_keyboard();
100
101#if _USESOUND_ == 1
102
103 ALLEGRO_DEBUG
("init audio");
104 if(!al_install_audio()){
105 ALLEGRO_ERROR
("failed to initialize audio!\n");
106 return -1;
107 }
108
109 ALLEGRO_DEBUG
("init acodec");
110 if(!al_init_acodec_addon()){
111 ALLEGRO_ERROR
("Failed to initialize audio codecs!\n");
112 return -1;
113 }
114
115 if (!al_reserve_samples(1)){
116 ALLEGRO_ERROR
("failed to reserve samples!\n");
117 return -1;
118 }
119
120 sample
= al_load_sample( "sinewave_onecycle.wav" );
121
122 if (!sample
){
123 ALLEGRO_ERROR
("Audio clip sample not loaded!\n" );
124 return -1;
125 }
126
127#endif
128
129 ALLEGRO_DEBUG
("creating display");
130 dpy
= al_create_display(800,
480);
131 if (!dpy
) {
132 ALLEGRO_ERROR
("failed to create display!");
133 return 1;
134 }
135
136 print_standard_paths
();
137
138 /* This is loaded from assets in the apk. */
139 al_android_set_apk_file_interface
();
140 image
= al_load_bitmap("alexlogo.png");
141 if (!image
) {
142 ALLEGRO_DEBUG
("failed to load alexlogo.png");
143 return 1;
144 }
145 al_set_standard_file_interface();
146
147 al_convert_mask_to_alpha(image,
al_map_rgb(255,
0,
255));
148
149 queue
= al_create_event_queue();
150 al_register_event_source(queue,
al_get_display_event_source(dpy
));
151 al_register_event_source(queue, al_get_touch_input_event_source
());
152 al_register_event_source(queue,
al_get_keyboard_event_source());
153
154 timer
= al_create_timer(1/60.
0);
155 al_register_event_source(queue,
al_get_timer_event_source(timer
));
156 al_start_timer(timer
);
157
158 bool draw
= true;
159 bool running
= true;
160 bool paused
= false;
161 int count
= 0;
162
163#if _USESOUND_ == 1
164
165 /* Loop the sample until the display closes. */
166 al_play_sample(sample,
1.
0,
0.
0,
1.
0,ALLEGRO_PLAYMODE_LOOP,NULL
);
167
168 al_rest(10.
0);
169
170#endif
171
172
173 while (running
) {
174 al_wait_for_event(queue,
&event
);
175
176 switch (event.type
) {
177 case ALLEGRO_EVENT_TOUCH_BEGIN:
178 //ALLEGRO_DEBUG("touch %i begin", event.touch.id);
179 touch
[event.touch.id
].down
= true;
180 touch
[event.touch.id
].x
= event.touch.x
;
181 touch
[event.touch.id
].y
= event.touch.y
;
182 break;
183
184 case ALLEGRO_EVENT_TOUCH_END:
185 //ALLEGRO_DEBUG("touch %i end", event.touch.id);
186 touch
[event.touch.id
].down
= false;
187 touch
[event.touch.id
].x
= 0.
0;
188 touch
[event.touch.id
].y
= 0.
0;
189 break;
190
191 case ALLEGRO_EVENT_TOUCH_MOVE:
192 //ALLEGRO_DEBUG("touch %i move: %fx%f", event.touch.id, event.touch.x, event.touch.y);
193 touch
[event.touch.id
].x
= event.touch.x
;
194 touch
[event.touch.id
].y
= event.touch.y
;
195 break;
196
197 case ALLEGRO_EVENT_TOUCH_CANCEL:
198 //ALLEGRO_DEBUG("touch %i canceled", event.touch.id);
199 break;
200
201 case ALLEGRO_EVENT_KEY_UP:
202 if (event.keyboard.keycode
== ALLEGRO_KEY_BACK
) {
203 ALLEGRO_DEBUG
("back key pressed, exit!");
204 running
= false;
205 }
206 else {
207 ALLEGRO_DEBUG
("%i key pressed", event.keyboard.keycode
);
208 }
209 break;
210
211 case ALLEGRO_EVENT_TIMER:
212 draw
= true;
213 if (count
== 60) {
214 ALLEGRO_DEBUG
("tick");
215 count
= 0;
216 }
217 count
++;
218 break;
219
220 case ALLEGRO_EVENT_DISPLAY_CLOSE:
221 ALLEGRO_DEBUG
("display close");
222 running
= false;
223 break;
224
225 case ALLEGRO_EVENT_DISPLAY_HALT_DRAWING:
226 ALLEGRO_DEBUG
("halt drawing");
227 // Stop the timer so we don't run at all while our display isn't
228 // active.
229 al_stop_timer(timer
);
230 //al_set_target_backbuffer(0);
231 ALLEGRO_DEBUG
("after set target");
232 paused
= true;
233 draw
= false;
234 al_acknowledge_drawing_halt
(dpy
);
235 break;
236
237 case ALLEGRO_EVENT_DISPLAY_RESUME_DRAWING:
238 ALLEGRO_DEBUG
("resume drawing");
239
240 al_acknowledge_drawing_resume
(dpy, NULL
);
241 ALLEGRO_DEBUG
("done waiting for surface recreated");
242
243 al_start_timer(timer
);
244 //al_set_target_backbuffer(dpy);
245 //_al_android_setup_opengl_view(dpy);
246 paused
= false;
247 break;
248
249 case ALLEGRO_EVENT_DISPLAY_RESIZE:
250 ALLEGRO_DEBUG
("display resize");
251 al_acknowledge_resize(dpy
);
252 ALLEGRO_DEBUG
("done resize");
253 set_transform
(dpy
);
254 break;
255
256 case ALLEGRO_EVENT_DISPLAY_ORIENTATION:
257 set_transform
(dpy
);
258 break;
259 }
260
261 if (draw
&& al_event_queue_is_empty
(queue
)) {
262 draw
= false;
263 al_clear_to_color(al_map_rgb(255,
255,
255));
264 if (image
) {
265 al_draw_bitmap(image,
266 al_get_display_width(dpy
)/2 - al_get_bitmap_width(image
)/2,
267 al_get_display_height(dpy
)/2 - al_get_bitmap_height(image
)/2,
268 0);
269 }
270 draw_touches
();
271 al_flip_display();
272 }
273 }
274#if _USESOUND_ == 1
275 al_destroy_sample(sample
);
276#endif
277 ALLEGRO_DEBUG
("done");
278 return 0;
279}
280
281/* vim: set sts=3 sw=3 et: */