File: ex_reparent.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 (172 lines) | stat: -rw-r--r-- 4,120 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
172
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_primitives.h>
#include <stdlib.h>
#include <math.h>

#include "common.c"

#define FPS 60

struct Example {
   ALLEGRO_DISPLAY *display;
   ALLEGRO_BITMAP *glyphs[2];
   ALLEGRO_BITMAP *sub;
   int i;
   int p;
} example;


/* Draw the parent bitmaps. */
static void draw_glyphs(void)
{
   int x = 0, y = 0;
   al_clear_to_color(al_map_rgb_f(0, 0, 0));
   al_draw_bitmap(example.glyphs[0], 0, 0, 0);
   al_draw_bitmap(example.glyphs[1], 0, 32 * 6, 0);
   if (example.p == 1)
      y = 32 * 6;
   x += al_get_bitmap_x(example.sub);
   y += al_get_bitmap_y(example.sub);
   al_draw_filled_rectangle(x, y,
      x + al_get_bitmap_width(example.sub),
      y + al_get_bitmap_height(example.sub),
      al_map_rgba_f(0.5, 0, 0, 0.5));
}

static void redraw(void)
{
   int i;

   draw_glyphs();

   /* Here we draw example.sub, which is never re-created, just
    * re-parented.
    */
   for (i = 0; i < 8; i++) {
      al_draw_scaled_rotated_bitmap(example.sub, 0, 0,
         i * 100, 32 * 6 + 33 * 5 + 4,
         2.0, 2.0, ALLEGRO_PI / 4 * i / 8, 0);
   }
}

static void update(void)
{
   /* Re-parent out sub bitmap, jumping from glyph to glyph. */
   int i;
   example.i++;
   i = (example.i / 4) % (16 * 6 + 20 * 5);
   if (i < 16 * 6) {
      example.p = 0;
      al_reparent_bitmap(example.sub, example.glyphs[0],
         (i % 16) * 32, (i / 16) * 32, 32, 32);
   }
   else {
      example.p = 1;
      i -= 16 * 6;
      al_reparent_bitmap(example.sub, example.glyphs[1],
         (i % 20) * 37, (i / 20) * 33, 37, 33);
   }
}

static void init(void)
{
   /* We create out sub bitmap once then use it throughout the
    * program, re-parenting as needed.
    */
   example.sub = al_create_sub_bitmap(example.glyphs[0], 0, 0, 32, 32);
}

int main(int argc, char **argv)
{
   ALLEGRO_TIMER *timer;
   ALLEGRO_EVENT_QUEUE *queue;
   int w = 800, h = 600;
   bool done = false;
   bool need_redraw = true;

   (void)argc;
   (void)argv;

   if (!al_init()) {
      abort_example("Failed to init Allegro.\n");
   }

   if (!al_init_image_addon()) {
      abort_example("Failed to init IIO addon.\n");
   }

   al_init_primitives_addon();

   init_platform_specific();

   al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR);
   al_set_new_display_flags(ALLEGRO_RESIZABLE);
   example.display = al_create_display(w, h);
   if (!example.display) {
      abort_example("Error creating display.\n");
   }

   if (!al_install_keyboard()) {
      abort_example("Error installing keyboard.\n");
   }

   example.glyphs[0] = al_load_bitmap("data/font.tga");
   if (!example.glyphs[0]) {
      abort_example("Error loading data/font.tga\n");
   }

   example.glyphs[1] = al_load_bitmap("data/bmpfont.tga");
   if (!example.glyphs[1]) {
      abort_example("Error loading data/bmpfont.tga\n");
   }

   init();

   timer = al_create_timer(1.0 / FPS);

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

   al_register_event_source(queue, al_get_timer_event_source(timer));
   
   al_register_event_source(queue, al_get_display_event_source(
      example.display));

   al_start_timer(timer);

   while (!done) {
      ALLEGRO_EVENT event;
      w = al_get_display_width(example.display);
      h = al_get_display_height(example.display);

      if (need_redraw && al_is_event_queue_empty(queue)) {
         redraw();
         al_flip_display();
         need_redraw = false;
      }

      al_wait_for_event(queue, &event);
      switch (event.type) {
         case ALLEGRO_EVENT_KEY_CHAR:
            if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
               done = true;
            break;

         case ALLEGRO_EVENT_DISPLAY_CLOSE:
            done = true;
            break;
         
         case ALLEGRO_EVENT_DISPLAY_RESIZE:
            al_acknowledge_resize(event.display.source);
            break;
              
         case ALLEGRO_EVENT_TIMER:
            update();
            need_redraw = true;
            break;
      }
   }

   return 0;
}