File: ex_transform.c

package info (click to toggle)
allegro5 2%3A5.2.8.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 26,560 kB
  • sloc: ansic: 128,223; cpp: 15,902; objc: 4,620; python: 2,898; java: 2,254; javascript: 1,242; sh: 1,010; makefile: 50; perl: 37; xml: 25; pascal: 24
file content (190 lines) | stat: -rw-r--r-- 6,777 bytes parent folder | download | duplicates (6)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <stdio.h>
#include "allegro5/allegro.h"
#include "allegro5/allegro_image.h"
#include "allegro5/allegro_font.h"
#include "allegro5/allegro_primitives.h"
#include <math.h>

#include "common.c"

int main(int argc, char **argv)
{
    const char *filename;
    ALLEGRO_DISPLAY *display;
    ALLEGRO_BITMAP *buffer, *bitmap, *subbitmap, *buffer_subbitmap;
    ALLEGRO_BITMAP *overlay;
    ALLEGRO_TIMER *timer;
    ALLEGRO_EVENT_QUEUE *queue;
    ALLEGRO_TRANSFORM transform;
    bool software = false;
    bool redraw = false;
    bool blend = false;
    bool use_subbitmap = true;
    int w, h;
    ALLEGRO_FONT* font;
    ALLEGRO_FONT* soft_font;

    if (argc > 1) {
       filename = argv[1];
    }
    else {
       filename = "data/mysha.pcx";
    }

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

    al_init_primitives_addon();
    al_install_mouse();
    al_install_keyboard();

    al_init_image_addon();
    al_init_font_addon();
    init_platform_specific();

    display = al_create_display(640, 480);
    if (!display) {
       abort_example("Error creating display\n");
    }
    
    subbitmap = al_create_sub_bitmap(al_get_backbuffer(display), 50, 50, 640 - 50, 480 - 50);
    overlay = al_create_sub_bitmap(al_get_backbuffer(display), 100, 100, 300, 50);
    
    al_set_window_title(display, filename);

    bitmap = al_load_bitmap(filename);
    if (!bitmap) {
       abort_example("%s not found or failed to load\n", filename);
    }
    font = al_load_font("data/bmpfont.tga", 0, 0);
    if (!font) {
       abort_example("data/bmpfont.tga not found or failed to load\n");
    }

    al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP);
    buffer = al_create_bitmap(640, 480);
    buffer_subbitmap = al_create_sub_bitmap(buffer, 50, 50, 640 - 50, 480 - 50);

    soft_font = al_load_font("data/bmpfont.tga", 0, 0);
    if (!soft_font) {
       abort_example("data/bmpfont.tga not found or failed to load\n");
    }

    timer = al_create_timer(1.0 / 60);
    queue = al_create_event_queue();
    al_register_event_source(queue, al_get_keyboard_event_source());
    al_register_event_source(queue, al_get_display_event_source(display));
    al_register_event_source(queue, al_get_timer_event_source(timer));
    al_start_timer(timer);
    
    w = al_get_bitmap_width(bitmap);
    h = al_get_bitmap_height(bitmap);

    al_set_target_bitmap(overlay);
    al_identity_transform(&transform);
    al_rotate_transform(&transform, -0.06);
    al_use_transform(&transform);

    while (1) {
        ALLEGRO_EVENT event;
        al_wait_for_event(queue, &event);
        if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
            break;
        if (event.type == ALLEGRO_EVENT_KEY_DOWN) {
            if (event.keyboard.keycode == ALLEGRO_KEY_S) {
               software = !software;
               if (software) {
                  /* Restore identity transform on display bitmap. */
                  ALLEGRO_TRANSFORM identity;
                  al_identity_transform(&identity);
                  al_use_transform(&identity);
               }
            } else if (event.keyboard.keycode == ALLEGRO_KEY_L) {
               blend = !blend;
            } else if (event.keyboard.keycode == ALLEGRO_KEY_B) {
               use_subbitmap = !use_subbitmap;
            } else if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) {
               break;
            }
        }
        if (event.type == ALLEGRO_EVENT_TIMER)
            redraw = true;
            
        if (redraw && al_is_event_queue_empty(queue)) {
            double t = 3.0 + al_get_time();
            ALLEGRO_COLOR tint;
            redraw = false;
            
            al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ONE);
            if(blend)
               tint = al_map_rgba_f(0.5, 0.5, 0.5, 0.5);
            else
               tint = al_map_rgba_f(1, 1, 1, 1);
            
            if(software) {
               if(use_subbitmap) {
                  al_set_target_bitmap(buffer);
                  al_clear_to_color(al_map_rgb_f(1, 0, 0));
                  al_set_target_bitmap(buffer_subbitmap);
               } else {
                  al_set_target_bitmap(buffer);
               }
            } else {
               if(use_subbitmap) {
                  al_set_target_backbuffer(display);
                  al_clear_to_color(al_map_rgb_f(1, 0, 0));
                  al_set_target_bitmap(subbitmap);
               } else {
                  al_set_target_backbuffer(display);
               }
            }

            /* Set the transformation on the target bitmap. */
            al_identity_transform(&transform);
            al_translate_transform(&transform, -640 / 2, -480 / 2);
            al_scale_transform(&transform, 0.15 + sin(t / 5), 0.15 + cos(t / 5));
            al_rotate_transform(&transform, t / 50);
            al_translate_transform(&transform, 640 / 2, 480 / 2);
            al_use_transform(&transform);

            /* Draw some stuff */
            al_clear_to_color(al_map_rgb_f(0, 0, 0));
            al_draw_tinted_bitmap(bitmap, tint, 0, 0, 0);
            al_draw_tinted_scaled_bitmap(bitmap, tint, w / 4, h / 4, w / 2, h / 2, w, 0, w / 2, h / 4, 0);//ALLEGRO_FLIP_HORIZONTAL);
            al_draw_tinted_bitmap_region(bitmap, tint, w / 4, h / 4, w / 2, h / 2, 0, h, ALLEGRO_FLIP_VERTICAL);
            al_draw_tinted_scaled_rotated_bitmap(bitmap, tint, w / 2, h / 2, w + w / 2, h + h / 2, 0.7, 0.7, 0.3, 0);
            al_draw_pixel(w + w / 2, h + h / 2, al_map_rgb_f(0, 1, 0));
            al_put_pixel(w + w / 2 + 2, h + h / 2 + 2, al_map_rgb_f(0, 1, 1));
            al_draw_circle(w, h, 50, al_map_rgb_f(1, 0.5, 0), 3);

            al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ZERO);
            if(software) {
               al_draw_text(soft_font, al_map_rgba_f(1, 1, 1, 1),
                  640 / 2, 430, ALLEGRO_ALIGN_CENTRE, "Software Rendering");
               al_set_target_backbuffer(display);
               al_draw_bitmap(buffer, 0, 0, 0);
            } else {
               al_draw_text(font, al_map_rgba_f(1, 1, 1, 1),
                  640 / 2, 430, ALLEGRO_ALIGN_CENTRE, "Hardware Rendering");
            }

            /* Each target bitmap has its own transformation matrix, so this
             * overlay is unaffected by the transformations set earlier.
             */
            al_set_target_bitmap(overlay);
            al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ONE);
            al_draw_text(font, al_map_rgba_f(1, 1, 0, 1),
               0, 10, ALLEGRO_ALIGN_LEFT, "hello!");

            al_set_target_backbuffer(display);
            al_flip_display();
        }
    }

    al_destroy_bitmap(bitmap);

    return 0;
}

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