Исходный код:
#define ALLEGRO_STATICLINK
#include <allegro5\allegro.h>
#include <allegro5\allegro_primitives.h>
enum KEYS{UP, DOWN, RIGHT, LEFT};
int main()
{
int width = 640;
int height = 480;
bool user_exit = false;
bool redraw = true;
int pos_x = width / 2;
int pos_y = height / 2;
int FPS = 60;
bool keys[4] = {false, false, false, false};
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_TIMER *timer = NULL;
if(!al_init()) //Инициализация Аллегро
return -1;
display = al_create_display(width, height); //Создание дисплея
if(!display) //Проверка дисплея
return -1;
al_init_primitives_addon();
al_install_keyboard();
timer = al_create_timer(1.0 / FPS); //Инициализация таймера
event_queue = al_create_event_queue(); //Инициализация оч. евентов
al_register_event_source(event_queue, al_get_keyboard_event_source());
al_register_event_source(event_queue, al_get_display_event_source(display));
al_register_event_source(event_queue, al_get_timer_event_source(timer)); //Добавление таймера в очередь
al_start_timer(timer); //Старт таймера
while(!user_exit)
{
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &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_RIGHT:
keys[RIGHT] = true;
break;
case ALLEGRO_KEY_LEFT:
keys[LEFT] = true;
break;
}
}
else 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_RIGHT:
keys[RIGHT] = false;
break;
case ALLEGRO_KEY_LEFT:
keys[LEFT] = false;
break;
}
}
else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
{
user_exit = true;
}
else if(ev.type == ALLEGRO_EVENT_TIMER)
{
pos_y -= keys[UP] * 10;
pos_y += keys[DOWN] * 10;
pos_x += keys[RIGHT] * 10;
pos_x -= keys[LEFT] * 10;
redraw = true;
}
if(redraw && al_is_event_queue_empty(event_queue))
{
redraw = false;
al_draw_filled_circle(pos_x, pos_y, 15, al_map_rgb(0, 255, 0));
al_flip_display();
al_clear_to_color(al_map_rgb(0, 0, 0));
}
}
al_destroy_display(display); //Удаление дисплея, очереди евентов, таймера
al_destroy_event_queue(event_queue);
al_destroy_timer(timer);
return 0;
}
Комментариев нет:
Отправить комментарий