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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_ttf.h>
#include "common.c"
/* How far has the text been scrolled. */
static float scroll_y;
/* Total length of the scrolling text in pixels. */
static int text_length;
/* The Alex logo. */
static ALLEGRO_BITMAP *logo;
/* The star particle. */
static ALLEGRO_BITMAP *particle;
/* The font we use for everything. */
static ALLEGRO_FONT *font;
/* Local pseudo-random number generator. */
static int rnd(int *seed)
{
*seed = (*seed + 1) * 1103515245 + 12345;
return ((*seed >> 16) & 0xffff);
}
/* Load a bitmap or font and exit with a message if it's missing. */
static void *load(char const *path, char const *type, ...)
{
void *data = NULL;
va_list args;
va_start(args, type);
if (!strcmp(type, "bitmap")) {
data = al_load_bitmap(path);
}
else if (!strcmp(type, "font")) {
int size = va_arg(args, int);
int flags = va_arg(args, int);
data = al_load_font(path, size, flags);
}
va_end(args);
if (!data) {
abort_example("Could not load %s %s", type, path);
}
return data;
}
/* Print fading text. */
static int print(ALLEGRO_FONT *font, float x, float y, float r, float g,
float b, float fade, char const *text)
{
float c = 1 + (y - fade) / 360 / 2.0;
if (c > 1)
c = 1;
if (c < 0)
c = 0;
al_draw_text(font, al_map_rgba_f(c * r, c * g, c * b, c), x, y,
ALLEGRO_ALIGN_CENTER, text);
return y + al_get_font_line_height(font);
}
/* Set up a perspective transform. We make the screen span
* 180 vertical units with square pixel aspect and 90° vertical
* FoV.
*/
static void setup_3d_projection(ALLEGRO_TRANSFORM *projection)
{
ALLEGRO_DISPLAY *display = al_get_current_display();
int dw = al_get_display_width(display);
int dh = al_get_display_height(display);
al_perspective_transform(projection, -180 * dw / dh, -180, 180,
180 * dw / dh, 180, 3000);
al_use_projection_transform(projection);
}
/* 3D transformations make it very easy to draw a starfield. */
static void draw_stars(void)
{
ALLEGRO_TRANSFORM projection;
int seed;
int i;
al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ONE);
seed = 0;
for (i = 0; i < 100; i++) {
const int x = rnd(&seed);
const int y = rnd(&seed);
const int z = rnd(&seed);
al_identity_transform(&projection);
al_translate_transform_3d(&projection, 0, 0,
-2000 + ((int)scroll_y * 1000 / text_length + z) % 2000 - 180);
setup_3d_projection(&projection);
al_draw_bitmap(particle, x % 4000 - 2000, y % 2000 - 1000, 0);
}
}
/* The main part of this example. */
static void draw_scrolling_text(void)
{
ALLEGRO_TRANSFORM projection;
int bw = al_get_bitmap_width(logo);
int bh = al_get_bitmap_height(logo);
float x, y, c;
al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA);
al_identity_transform(&projection);
/* First, we scroll the text in in the y direction (inside the x/z
* plane) and move it away from the camera (in the z direction).
* We move it as far as half the display height to get a vertical
* FOV of 90 degrees.
*/
al_translate_transform_3d(&projection, 0, -scroll_y, -180);
/* Then we tilt it backwards 30 degrees. */
al_rotate_transform_3d(&projection, 1, 0, 0,
30 * ALLEGRO_PI / 180.0);
/* And finally move it down so the 0 position ends up
* at the bottom of the screen.
*/
al_translate_transform_3d(&projection, 0, 180, 0);
setup_3d_projection(&projection);
x = 0;
y = 0;
c = 1 + (y - scroll_y) / 360 / 2.0;
if (c < 0)
c = 0;
al_draw_tinted_bitmap(logo, al_map_rgba_f(c, c, c, c),
x - bw / 2, y, 0);
y += bh;
#define T(str) (y = print(font, x, y, 1, 0.9, 0.3, scroll_y, (str)));
T("Allegro 5")
T("")
T("It is a period of game programming.")
T("Game coders have won their first")
T("victory against the evil")
T("General Protection Fault.")
T("")
T("During the battle, hackers managed")
T("to steal the secret source to the")
T("General's ultimate weapon,")
T("the ACCESS VIOLATION, a kernel")
T("exception with enough power to")
T("destroy an entire program.")
T("")
T("Pursued by sinister bugs the")
T("Allegro developers race home")
T("aboard their library to save")
T("all game programmers and restore")
T("freedom to the open source world.")
#undef T
}
static void draw_intro_text(void)
{
ALLEGRO_TRANSFORM projection;
int fade;
int fh = al_get_font_line_height(font);
if (scroll_y < 50)
fade = (50 - scroll_y) * 12;
else
fade = (scroll_y - 50) * 4;
al_identity_transform(&projection);
al_translate_transform_3d(&projection, 0, -scroll_y / 3, -181);
setup_3d_projection(&projection);
print(font, 0, 0, 0, 0.9, 1, fade, "A long time ago, in a galaxy");
print(font, 0, 0 + fh, 0, 0.9, 1, fade, "not too far away...");
}
int main(int argc, char **argv)
{
ALLEGRO_DISPLAY *display;
ALLEGRO_TIMER *timer;
ALLEGRO_EVENT_QUEUE *queue;
int redraw = 0;
(void)argc;
(void)argv;
if (!al_init()) {
abort_example("Could not init Allegro.\n");
}
al_init_image_addon();
al_init_font_addon();
al_init_ttf_addon();
init_platform_specific();
al_install_keyboard();
al_set_new_display_flags(ALLEGRO_RESIZABLE);
display = al_create_display(640, 360);
if (!display) {
abort_example("Error creating display\n");
}
al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR |
ALLEGRO_MIPMAP);
font = load("data/DejaVuSans.ttf", "font", 40, 0);
logo = load("data/alexlogo.png", "bitmap");
particle = load("data/haiku/air_effect.png", "bitmap");
al_convert_mask_to_alpha(logo, al_map_rgb(255, 0, 255));
text_length = al_get_bitmap_height(logo) +
19 * al_get_font_line_height(font);
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);
while (true) {
ALLEGRO_EVENT event;
al_wait_for_event(queue, &event);
if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
break;
else if (event.type == ALLEGRO_EVENT_DISPLAY_RESIZE) {
al_acknowledge_resize(display);
}
else if (event.type == ALLEGRO_EVENT_KEY_DOWN &&
event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) {
break;
}
else if (event.type == ALLEGRO_EVENT_TIMER) {
scroll_y++;
if (scroll_y > text_length * 2)
scroll_y -= text_length * 2;
redraw = 1;
}
if (redraw && al_is_event_queue_empty(queue)) {
ALLEGRO_COLOR black = al_map_rgba_f(0, 0, 0, 1);
al_clear_to_color(black);
draw_stars();
draw_scrolling_text();
draw_intro_text();
al_flip_display();
redraw = 0;
}
}
return 0;
}
/* vim: set sts=3 sw=3 et: */
|