Untitled
raw download clone
CPP
views 18
,
size 4051 b
#include <stdio.h>
#include <stdlib.h>
#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
#include <allegro5/allegro_image.h>

#define GAME_TERMINATE 666

ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_EVENT event;
ALLEGRO_TIMER *timer = NULL;

const int width = 800;
const int height = 600;

int pos_x, pos_y; // The position of rectangle's left-up corner.
int mon1pos_x, mon1pos_y, mon1dir;

void show_err_msg(int msg);
void game_init();
void game_begin();
int process_event();
int game_run();
void game_destroy();

/*
    You can declare some variables here if it is useful for you.
*/

int main(int argc, char *argv[])
{
    int msg = 0;

    game_init();
    game_begin();
    printf("Hello world!!!\n");

    while (msg != GAME_TERMINATE)
    {
        msg = game_run();
        if (msg == GAME_TERMINATE)
            printf("See you, world\n");
    }
    game_destroy();
    return 0;
}

void show_err_msg(int msg)
{
    fprintf(stderr, "unexpected msg: %d", msg);
    game_destroy();
    exit(9);
}

void game_init()
{
    if (!al_init())
    {
        show_err_msg(-1);
    }

    pos_x = width / 2;
    pos_y = height / 2;
    mon1pos_x = width - 50;
    mon1pos_y = (height / 4) * 3 - 50;

    display = al_create_display(width, height);
    event_queue = al_create_event_queue();

    if (display == NULL || event_queue == NULL)
    {
        show_err_msg(-1);
    }

    al_init_primitives_addon();
    al_install_keyboard();
    al_register_event_source(event_queue, al_get_display_event_source(display));
    al_register_event_source(event_queue, al_get_keyboard_event_source());
    timer  = al_create_timer(1.0/15.0);
    al_register_event_source(event_queue, al_get_timer_event_source(timer)) ;
    al_start_timer(timer);
}

void game_begin()
{
    al_clear_to_color(al_map_rgb(0, 0, 0));
    al_draw_filled_rectangle(pos_x, pos_y, pos_x + 30, pos_y + 30, al_map_rgb(255, 0, 255));
    al_draw_filled_rectangle(0, (height / 4) * 3, width, (height / 4) * 3 + 1, al_map_rgb(255, 255, 255));
    al_draw_filled_rectangle(mon1pos_x, mon1pos_y, mon1pos_x + 50, mon1pos_y + 50, al_map_rgb(255, 255, 0));
    al_flip_display();
}

int process_event()
{
    al_wait_for_event(event_queue, &event);
    // Keyboard
    if (event.timer.source == timer)
    {
        if (mon1pos_x + 50 == width)
            mon1dir = 0;
        else if (mon1pos_x == 0)
            mon1dir = 1;
        else if (mon1pos_x % 150 == 0)
            mon1dir = rand() % 2;
        if (mon1dir == 0)
            mon1pos_x -= 10;
        else
            mon1pos_x += 10;
    }

    if (event.type == ALLEGRO_EVENT_KEY_UP)
    {
        switch (event.keyboard.keycode)
        {
        // Control
        case ALLEGRO_KEY_W:
            pos_y -= 30;
            break;
        case ALLEGRO_KEY_S:
            pos_y += 30;
            break;
        case ALLEGRO_KEY_A:
            pos_x -= 30;
            break;
        case ALLEGRO_KEY_D:
            pos_x += 30;
            break;

        // For Start Menu
        case ALLEGRO_KEY_ESCAPE:
            return GAME_TERMINATE;
        }
    }

    al_flip_display();
    al_clear_to_color(al_map_rgb(0, 0, 0));
    return 0;
}

int game_run()
{
    int error = 0;
    if (!al_is_event_queue_empty(event_queue))
    {
        error = process_event();
        al_draw_filled_rectangle(pos_x, pos_y, pos_x + 30, pos_y + 30, al_map_rgb(255, 0, 255));
        al_draw_filled_rectangle(0, (height / 4) * 3, width, (height / 4) * 3 + 1, al_map_rgb(255, 255, 255));
        al_draw_filled_rectangle(mon1pos_x, mon1pos_y, mon1pos_x + 50, mon1pos_y + 50, al_map_rgb(255, 255, 0));
    }

    return error;
}

void game_destroy()
{
    al_destroy_event_queue(event_queue);
    al_destroy_display(display);
    al_destroy_timer(timer);
}
close fullscreen
Login or Register to edit or fork this paste. It's free.