Question

Hey so I was trying to make a program where you can draw a line from a point to where your mouse is but I am having trouble figuring out how to delete the line after it has been drawn.

#include <allegro.h>
#include <cstdlib>

BITMAP *buffer;

int main()
{
    allegro_init();
    install_mouse();
    install_keyboard();
    set_color_depth(16);
    set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);
    buffer = create_bitmap(640, 480);

    while (!key[KEY_ESC]) {
        if (key[KEY_SPACE]) {
            line(buffer, 30, 450, mouse_x, mouse_y, makecol(255, 0, 0));
        }

        draw_sprite(screen, buffer, 0, 0);
        release_screen();

        rest(10);
    }

    return 0; 
}
END_OF_MAIN();
Was it helpful?

Solution

You will need to store the coordinates of the lines in a data structure of some sort (e.g., an array of structs). When you want to delete a line, remove it from the data structure.

Your drawing code then looks like:

  1. Clear buffer
  2. Iterate through every line, drawing it to buffer
  3. Draw buffer to screen

And don't call acquire/release screen. They generally aren't needed, and you'll give yourself a lot of problems if you misuse them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top