Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Drawing line frame by frame

Credits go to Arthur Kalliokoski and SiegeLord for helping out!
This thread is locked; no one can reply to it. rss feed Print
Drawing line frame by frame
CrimsonV
Member #15,634
June 2014

Hello everyone, programming and allegro newbie here. I've been tinkering with this library for a few days, and now I'm trying to create a very small game for an uni project.

The idea I have in mind is a simple version of the atari classic "missile commander". Enemy missiles (represented by slow moving lines) come from above, and you shoot them back with your missiles from below, before they hit the ground.

Problem is, I can't figure out how to implement the missile movement from point A to point B (aka from turret to mouse cursor, when the mouse is clicked). I've been using al_draw_line as a placeholder while testing other functions, but of course that won't cut it, as it draws the entire thing immediately.

If I'm not mistaken, Allegro 4 had a do_line function that when combined with putpixel, did exactly what I'm asking for, but I can't find its equivalent for Allegro 5 (I might easily be missing something huge here).

I've even tried Bresenham's algorithm with al_draw_pixel, and it works like a charm, but the line is drawn from the cursor to the turret, rather than the opposite, and reversing it is proving harder than it actually seemed. In the end, I'm probably overthinking it, so I finally decided to ask for help. In the meantime, I'll try to figure out how to implement threading, which is looking like an even bigger nightmare ::)

Thanks in advance people :)

Arthur Kalliokoski
Second in Command
February 2005
avatar

CrimsonV said:

an uni project.

Quote:

the line is drawn from the cursor to the turret, rather than the opposite, and reversing it is proving harder than it actually seemed.

A university student can't swap a pair of 2d variables? Drink a Pepsi and try again.

Wikipedia entry on Bresenham's line algorithm shows this (among others)

plotLine(x0,y0, x1,y1)
  dx=x1-x0
  dy=y1-y0

  D = 2*dy - dx
  plot(x0,y0)
  y=y0

  for x from x0+1 to x1
    if D > 0
      y = y+1
      plot(x,y)
      D = D + (2*dy-2*dx)
    else
      plot(x,y)
      D = D + (2*dy)

So change the first line from plotLine(x0,y0, x1,y1) to plotLine(x1,y1, x0,y0)?

They all watch too much MSNBC... they get ideas.

CrimsonV
Member #15,634
June 2014

No need to be sassy about it.

I adapted code I found on rosettacode, and of course the first thing I tried was to swap the two points the function takes as input, among other attempts, but it didn't work. Then I realized the entire thing was built from ground up with a "priority" order according to the pixel's location.

Now, the code you posted from wikipedia seems less convoluted, so I will try implementing that. When I posted this thread it was 2 AM and I was very tired and disheartened after many, many consecutive hours spent on my code, so I was only wondering if there was an even simpler way of doing it through an Allegro function before searching for a better way to reverse the algorithm, that's it.

Thanks for your input.

SiegeLord
Member #7,827
October 2006
avatar

CrimsonV said:

No need to be sassy about it.

Arthur is the resident sass-master.

So I'm confused why you don't do something like this (if I understand your task correctly):

auto dx = end_x - start_x;
auto dy = end_y - start_y;
auto ratio = (game_time - start_time) / beam_duration;
al_draw_line(start_x, start_y, ratio * dx + start_x, ratio * dy + start_y, ...);

That will draw a line that slowly increases in length from start to finish. Alternatively (and equivalently), you could do something like this too:

al_draw_line(spawn_x, spawn_y, cur_x, cur_y, ...);

Where each game tick, you update cur_x and cur_y:

cur_x += dt * vel_x;
cur_y += dt * vel_y;

Although I don't think it is the right tool for this task, A5 actually does have a do_line equivalent: al_draw_soft_line.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

CrimsonV
Member #15,634
June 2014

That's even better, thank you ;D

Using a normal line that simply increases each tick is something I really didn't think about. As for the first solution, well, I wasn't even aware that "auto" existed (I had to look it up right now).

I didn't lie when I said I am a programming beginner, I started from absolute zero not even one year ago and I've been given a task where well over half of the requisite knowledge is material that I need to learn by myself, not through lessons. That even included picking an open source library and learning how to use it.

I'm not sure how common that is in other countries and whether it's fair or not, but that's off-topic, I just felt like a little explaination is needed if all this is excessively "banal".

Thanks again, needless to say it works now, I'll be moving onto threading :)

Go to: