File: ex_rotate.c

package info (click to toggle)
allegro5 2%3A5.2.10.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,820 kB
  • sloc: ansic: 109,795; cpp: 12,976; objc: 4,592; java: 2,845; python: 2,595; javascript: 1,238; sh: 1,008; makefile: 40; xml: 27; pascal: 24
file content (171 lines) | stat: -rw-r--r-- 4,780 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 *    Example program for the Allegro library, by Peter Wang.
 */


#include <allegro5/allegro.h>
#include "allegro5/allegro_image.h"
#include <math.h>

#include "common.c"

int main(int argc, char **argv)
{
   const int display_w = 640;
   const int display_h = 480;

   ALLEGRO_DISPLAY *dpy;
   ALLEGRO_BITMAP *buf;
   ALLEGRO_BITMAP *bmp;
   ALLEGRO_BITMAP *mem_bmp;
   ALLEGRO_BITMAP *src_bmp;
   ALLEGRO_EVENT_QUEUE *queue;
   ALLEGRO_EVENT event;
   double theta = 0;
   double k = 1.0;
   int mode = 0;
   bool wide_mode = false;
   bool mem_src_mode = false;
   bool trans_mode = false;
   int flags = 0;
   bool clip_mode = false;
   ALLEGRO_COLOR trans;

   (void)argc;
   (void)argv;

   if (!al_init()) {
      abort_example("Could not init Allegro.\n");
   }
   al_install_keyboard();
   al_init_image_addon();
   init_platform_specific();

   open_log();
   log_printf("Press 'w' to toggle wide mode.\n");
   log_printf("Press 's' to toggle memory source bitmap.\n");
   log_printf("Press space to toggle drawing to backbuffer or off-screen bitmap.\n");
   log_printf("Press 't' to toggle translucency.\n");
   log_printf("Press 'h' to toggle horizontal flipping.\n");
   log_printf("Press 'v' to toggle vertical flipping.\n");
   log_printf("Press 'c' to toggle clipping.\n");
   log_printf("\n");

   dpy = al_create_display(display_w, display_h);
   if (!dpy) {
      abort_example("Unable to set any graphic mode\n");
   }

   buf = al_create_bitmap(display_w, display_h);
   if (!buf) {
      abort_example("Unable to create buffer\n\n");
   }

   bmp = al_load_bitmap("data/mysha.pcx");
   if (!bmp) {
      abort_example("Unable to load image\n");
   }

   al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP);
   mem_bmp = al_load_bitmap("data/mysha.pcx");
   if (!mem_bmp) {
      abort_example("Unable to load image\n");
   }

   queue = al_create_event_queue();
   al_register_event_source(queue, al_get_keyboard_event_source());

   while (true) {
      if (al_get_next_event(queue, &event)) {
         if (event.type == ALLEGRO_EVENT_KEY_CHAR) {
            if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
               break;
            if (event.keyboard.unichar == ' ') {
               mode = !mode;
               if (mode == 0)
                  log_printf("Drawing to off-screen buffer\n");
               else
                  log_printf("Drawing to display backbuffer\n");
            }
            if (event.keyboard.unichar == 'w')
               wide_mode = !wide_mode;
            if (event.keyboard.unichar == 's') {
               mem_src_mode = !mem_src_mode;
               if (mem_src_mode)
                  log_printf("Source is memory bitmap\n");
               else
                  log_printf("Source is display bitmap\n");
            }
            if (event.keyboard.unichar == 't')
               trans_mode = !trans_mode;
            if (event.keyboard.unichar == 'h')
               flags ^= ALLEGRO_FLIP_HORIZONTAL;
            if (event.keyboard.unichar == 'v')
               flags ^= ALLEGRO_FLIP_VERTICAL;
            if (event.keyboard.unichar == 'c')
               clip_mode = !clip_mode;
         }
      }

      /*
       * mode 0 = draw scaled to off-screen buffer before
       *          blitting to display backbuffer
       * mode 1 = draw scaled to display backbuffer
       */

      if (mode == 0) {
         al_set_target_bitmap(buf);
      }
      else {
         al_set_target_backbuffer(dpy);
      }

      src_bmp = (mem_src_mode) ? mem_bmp : bmp;
      k = (wide_mode) ? 2.0 : 1.0;

      al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ZERO);
      trans = al_map_rgba_f(1, 1, 1, 1);
      if (mode == 0)
         al_clear_to_color(al_map_rgba_f(1, 0, 0, 1));
      else
         al_clear_to_color(al_map_rgba_f(0, 0, 1, 1));

      if (trans_mode) {
         al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA);
         trans = al_map_rgba_f(1, 1, 1, 0.5);
      }

      if (clip_mode) {
         al_set_clipping_rectangle(50, 50, display_w - 100, display_h - 100);
      }
      else {
         al_set_clipping_rectangle(0, 0, display_w, display_h);
      }

      al_draw_tinted_scaled_rotated_bitmap(src_bmp,
         trans,
         50, 50, display_w/2, display_h/2,
         k, k, theta,
         flags);

      if (mode == 0) {
         al_set_target_backbuffer(dpy);
         al_set_clipping_rectangle(0, 0, display_w, display_h);
         al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ZERO);
         al_draw_bitmap(buf, 0, 0, 0);
      }

      al_flip_display();
      al_rest(0.01);
      theta -= 0.01;
   }

   al_destroy_bitmap(bmp);
   al_destroy_bitmap(mem_bmp);
   al_destroy_bitmap(buf);

   close_log(false);
   return 0;
}

/* vim: set sts=3 sw=3 et: */