1 /* This example displays a picture on the screen, with support for
2 * command-line parameters, multi-screen, screen-orientation and
3 * zooming.
4 */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include "allegro5/allegro.h"
8 #include "allegro5/allegro_image.h"
9
10 #include "common.c"
11
main(int argc,char ** argv)12 int main(int argc, char **argv)
13 {
14 const char *filename;
15 ALLEGRO_DISPLAY *display;
16 ALLEGRO_BITMAP *bitmap;
17 ALLEGRO_TIMER *timer;
18 ALLEGRO_EVENT_QUEUE *queue;
19 bool redraw = true;
20 double zoom = 1;
21 double t0;
22 double t1;
23
24 /* The first commandline argument can optionally specify an
25 * image to display instead of the default. Allegro's image
26 * addon supports BMP, DDS, PCX, TGA and can be compiled with
27 * PNG and JPG support on all platforms. Additional formats
28 * are supported by platform specific libraries and support for
29 * image formats can also be added at runtime.
30 */
31 if (argc > 1) {
32 filename = argv[1];
33 }
34 else {
35 filename = "data/mysha.pcx";
36 }
37
38 if (!al_init()) {
39 abort_example("Could not init Allegro.\n");
40 }
41
42 // Initializes and displays a log window for debugging purposes.
43 open_log();
44
45 /* The second parameter to the process can optionally specify what
46 * adapter to use.
47 */
48 if (argc > 2) {
49 al_set_new_display_adapter(atoi(argv[2]));
50 }
51
52 /* Allegro requires installing drivers for all input devices before
53 * they can be used.
54 */
55 al_install_mouse();
56 al_install_keyboard();
57
58 /* Initialize the image addon. Requires the allegro_image addon
59 * library.
60 */
61 al_init_image_addon();
62
63 // Helper functions from common.c.
64 init_platform_specific();
65
66 // Create a new display that we can render the image to.
67 display = al_create_display(640, 480);
68 if (!display) {
69 abort_example("Error creating display\n");
70 }
71
72 al_set_window_title(display, filename);
73
74 // Load the image and time how long it took for the log.
75 t0 = al_get_time();
76 bitmap = al_load_bitmap(filename);
77 t1 = al_get_time();
78 if (!bitmap) {
79 abort_example("%s not found or failed to load\n", filename);
80 }
81
82 log_printf("Loading took %.4f seconds\n", t1 - t0);
83
84 // Create a timer that fires 30 times a second.
85 timer = al_create_timer(1.0 / 30);
86 queue = al_create_event_queue();
87 al_register_event_source(queue, al_get_keyboard_event_source());
88 al_register_event_source(queue, al_get_display_event_source(display));
89 al_register_event_source(queue, al_get_timer_event_source(timer));
90 al_start_timer(timer); // Start the timer
91
92 // Primary 'game' loop.
93 while (1) {
94 ALLEGRO_EVENT event;
95 al_wait_for_event(queue, &event); // Wait for and get an event.
96 if (event.type == ALLEGRO_EVENT_DISPLAY_ORIENTATION) {
97 int o = event.display.orientation;
98 if (o == ALLEGRO_DISPLAY_ORIENTATION_0_DEGREES) {
99 log_printf("0 degrees\n");
100 }
101 else if (o == ALLEGRO_DISPLAY_ORIENTATION_90_DEGREES) {
102 log_printf("90 degrees\n");
103 }
104 else if (o == ALLEGRO_DISPLAY_ORIENTATION_180_DEGREES) {
105 log_printf("180 degrees\n");
106 }
107 else if (o == ALLEGRO_DISPLAY_ORIENTATION_270_DEGREES) {
108 log_printf("270 degrees\n");
109 }
110 else if (o == ALLEGRO_DISPLAY_ORIENTATION_FACE_UP) {
111 log_printf("Face up\n");
112 }
113 else if (o == ALLEGRO_DISPLAY_ORIENTATION_FACE_DOWN) {
114 log_printf("Face down\n");
115 }
116 }
117 if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
118 break;
119 /* Use keyboard to zoom image in and out.
120 * 1: Reset zoom.
121 * +: Zoom in 10%
122 * -: Zoom out 10%
123 * f: Zoom to width of window
124 */
125 if (event.type == ALLEGRO_EVENT_KEY_CHAR) {
126 if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
127 break; // Break the loop and quite on escape key.
128 if (event.keyboard.unichar == '1')
129 zoom = 1;
130 if (event.keyboard.unichar == '+')
131 zoom *= 1.1;
132 if (event.keyboard.unichar == '-')
133 zoom /= 1.1;
134 if (event.keyboard.unichar == 'f')
135 zoom = (double)al_get_display_width(display) /
136 al_get_bitmap_width(bitmap);
137 }
138
139 // Trigger a redraw on the timer event
140 if (event.type == ALLEGRO_EVENT_TIMER)
141 redraw = true;
142
143 // Redraw, but only if the event queue is empty
144 if (redraw && al_is_event_queue_empty(queue)) {
145 redraw = false;
146 // Clear so we don't get trippy artifacts left after zoom.
147 al_clear_to_color(al_map_rgb_f(0, 0, 0));
148 if (zoom == 1)
149 al_draw_bitmap(bitmap, 0, 0, 0);
150 else
151 al_draw_scaled_rotated_bitmap(
152 bitmap, 0, 0, 0, 0, zoom, zoom, 0, 0);
153 al_flip_display();
154 }
155 }
156
157 al_destroy_bitmap(bitmap);
158
159 close_log(false);
160
161 return 0;
162 }
163
164 /* vim: set sts=4 sw=4 et: */
165