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
|
#include <allegro5\allegro.h>
#include <allegro5\allegro_native_dialog.h>
#include <allegro5\allegro_font.h>
#include <allegro5\allegro_ttf.h>
#include <allegro5\allegro_primitives.h>
enum KEYS {UP, DOWN, LEFT, RIGHT};
int main ()
{
int width=640;
int height=480;
bool done = false;
int pos_x= width/2;
int pos_y= height/2;
bool keys[4] = {false, false, false, false};
ALLEGRO_DISPLAY *display =NULL;
ALLEGRO_EVENT_QUEUE *eventqueue =NULL;
if (!al_init())
{
al_show_native_message_box (NULL, NULL, NULL,
"FAILED TO INITIALIZE ALLEGRO", NULL, NULL);
return -1;
}
display = al_create_display (width, height);
if (!display)
{
al_show_native_message_box (NULL, NULL, NULL,
"FAILED TO INITIALIZE display", NULL, NULL);
return -1;
}
al_init_font_addon();
al_init_ttf_addon();
ALLEGRO_FONT *font24 = al_load_font ("arial.ttf", 24, 0);
ALLEGRO_FONT *font36 = al_load_font ("arial.ttf", 36, 0);
ALLEGRO_FONT *font18 = al_load_font ("arial.ttf", 18, 0);
al_init_primitives_addon();
al_install_keyboard();
eventqueue = al_create_event_queue();
al_register_event_source(eventqueue, al_get_keyboard_event_source());
al_register_event_source(eventqueue, al_get_display_event_source(display));
// do not code above this, above this is initializations
al_clear_to_color (al_map_rgb (0,0,0));
do
{
ALLEGRO_EVENT ev;
al_wait_for_event (eventqueue, &ev);
if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
{
switch (ev.keyboard.keycode)
{
case ALLEGRO_KEY_UP:
keys[UP] = true;
break;
case ALLEGRO_KEY_DOWN:
keys[DOWN] = true;
break;
case ALLEGRO_KEY_LEFT:
keys[LEFT] = true;
break;
case ALLEGRO_KEY_RIGHT:
keys[RIGHT] = true;
break;
}
if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
{
if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
done = true;
}
if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
{
done = true;
}
if (ev.type == ALLEGRO_EVENT_KEY_UP)
{ switch (ev.keyboard.keycode)
{
case ALLEGRO_KEY_UP:
keys[UP] = false;
break;
case ALLEGRO_KEY_DOWN:
keys[DOWN] = false;
break;
case ALLEGRO_KEY_LEFT:
keys[LEFT] = false;
break;
case ALLEGRO_KEY_RIGHT:
keys[RIGHT] = false;
break;
}
}
pos_y -= keys[UP] *10;
pos_y += keys[DOWN] *10;
pos_x -= keys[LEFT] *10;
pos_x += keys[RIGHT] *10;
al_draw_filled_rectangle(pos_x,pos_y,pos_x-5, pos_y-5, al_map_rgb (255,0,0));
al_flip_display();
al_clear_to_color(al_map_rgb(0,0,0));
}
}while (done == false);
al_destroy_display(display);
return 0;
}
|