0

I am using the allegro_primitives.h header file, and when I go to draw a rectangle with al_draw_filled_rectangle, and I move the rectangle with the keys, the outline of the rectangle in the direction that the rectangle is going is changing colors. Here's the code:

#include <allegro5\allegro5.h>
#include <allegro5\allegro_primitives.h>
#include <iostream>
#include "Globals.h"
using namespace std;

bool keys[5] = {false, false, false, false, false};
enum KEYS {UP, DOWN, LEFT, RIGHT, SPACE};
const int WINDOW_WIDTH = 600;
const int WINDOW_HEIGHT = 600;
int main()
{
    bool done = false;
    bool redraw = true;

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

    if(!al_init())
        return -1;

    al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST);
    al_set_new_display_option(ALLEGRO_SAMPLES, 8, ALLEGRO_SUGGEST);

    display = al_create_display(WINDOW_WIDTH,WINDOW_HEIGHT);

    if(!display)
        return -1;

    al_init_primitives_addon();
    al_install_keyboard();

    event_queue = al_create_event_queue();
    timer = al_create_timer(1.0 / 60);

    if(!timer)
        return -1;
    if(!event_queue)
        return -1;

    al_register_event_source(event_queue,al_get_keyboard_event_source());
    al_register_event_source(event_queue,al_get_timer_event_source(timer));
    al_register_event_source(event_queue,al_get_display_event_source(display));

    Character player;
    Character lover;

    player.x1 = 100;
    player.x2 = player.x1 + 40;
    player.y1 = (WINDOW_HEIGHT / 2) - 20;
    player.y2 = player.y1 + 40;

    lover.x1 = WINDOW_WIDTH - 140;
    lover.x2 = lover.x1 + 40;
    lover.y1 = (WINDOW_HEIGHT / 2) - 20;
    lover.y2 = lover.y1 + 40;

    al_start_timer(timer);

    while(!done)
    {
        ALLEGRO_EVENT ev;
        al_wait_for_event(event_queue,&ev);
        if(ev.timer.source == timer)
        {
            if(keys[UP])
            {
                player.y1 -= 5;
                player.y2 -= 5;
            }
            if(keys[DOWN])
            {
                player.y1 += 5;
                player.y2 += 5;
            }
            if(keys[LEFT])
            {
                player.x1 -= 5;
                player.x2 -= 5;
            }
            if(keys[RIGHT])
            {
                player.x1 += 5;
                player.x2 += 5;
            }
            if(player.x1 <= 0)
            {
                player.x1 = 0;
                player.x2 = 40;
            }
            if(player.x2 >= WINDOW_WIDTH)
            {
                player.x1 = WINDOW_WIDTH - 40;
                player.x2 = WINDOW_WIDTH;
            }
            if(player.y1 <= 0)
            {
                player.y1 = 0;
                player.y2 = player.y1 + 40;
            }
            if(player.y2 >= WINDOW_HEIGHT)
            {
                player.y1 = WINDOW_HEIGHT - 40;
                player.y2 = WINDOW_HEIGHT;
            }
        }
        if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
        {
            done = true;
        }
        if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
        {
            switch(ev.keyboard.keycode)
            {
            case ALLEGRO_KEY_ESCAPE:
                done = true;
                break;
            case ALLEGRO_KEY_LEFT:
                keys[LEFT] = true;
                break;
            case ALLEGRO_KEY_RIGHT:
                keys[RIGHT] = true;
                break;
            case ALLEGRO_KEY_DOWN:
                keys[DOWN] = true;
                break;
            case ALLEGRO_KEY_UP:
                keys[UP] = true;
                break;
            }
        }
        if(ev.type == ALLEGRO_EVENT_KEY_UP)
        {
            switch(ev.keyboard.keycode)
            {
            case ALLEGRO_KEY_ESCAPE:
                done = true;
                break;
            case ALLEGRO_KEY_LEFT:
                keys[LEFT] = false;
                break;
            case ALLEGRO_KEY_RIGHT:
                keys[RIGHT] = false;
                break;
            case ALLEGRO_KEY_UP:
                keys[UP] = false;
                break;
            case ALLEGRO_KEY_DOWN:
                keys[DOWN] = false;
                break;
            }
        }
        if(redraw && al_is_event_queue_empty(event_queue))
        {
            al_draw_filled_rectangle(player.x1,player.y1,player.x2,player.y2,al_map_rgb(0,0,127));
            al_draw_filled_rectangle(lover.x1,lover.y1,lover.x2,lover.y2,al_map_rgb(127,0,0));
            al_flip_display();
            al_clear_to_color(al_map_rgb(255,255,255));
        }
    }
    al_destroy_timer(timer);
    al_destroy_display(display);
    al_destroy_event_queue(event_queue);
    return 0;
}

Can anybody help me out here? "Globals.h" is just a small header file which contains a struct for Character defining their x1, x2, y1, and y2 variables. Thanks.

angrytoad
  • 11
  • 1

1 Answers1

0

As your code currently is, your redraw is not being controlled by your timer. Your redraw variable is being initialized to true, but it makes more sense to set it to false, and let your timer set it to true when appropriate. When your timer event fires, set redraw to true.

ALLEGRO_EVENT ev;
al_wait_for_event(event_queue,&ev);
if(ev.timer.source == timer)
{
    redraw = true;
}

Then when you check for redraw, set it back to false.

if(redraw && al_is_event_queue_empty(event_queue))
{
    redraw = false;

    al_draw_filled_rectangle(player.x1,player.y1,player.x2,player.y2,al_map_rgb(0,0,127));
    al_draw_filled_rectangle(lover.x1,lover.y1,lover.x2,lover.y2,al_map_rgb(127,0,0));
    al_flip_display();
    al_clear_to_color(al_map_rgb(255,255,255));
}
131nary
  • 125
  • 9
  • When running your code, the only "color change" I see around the edge of the rectangles is the residual ghost image of the previous frame, which for the blue rectangle, appears a little purplish. Is this what you are referring to, or is something different happening? – 131nary May 03 '13 at 17:00
  • If it is just the ghosting, try enabling vsync on your display. al_set_new_display_option(ALLEGRO_VSYNC, 1, ALLEGRO_SUGGEST); – 131nary May 03 '13 at 17:18