Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Intro and question about how to show a line being drawn

This thread is locked; no one can reply to it. rss feed Print
Intro and question about how to show a line being drawn
scorliss
Member #15,970
May 2015
avatar

Hi All,

Without being a completely random person asking a random question please allow me to introduce myself. My name is scorliss and I am new member to the Allegro.cc community. I am currently working towards my Game Development Certification.

I am in the process of making a Missile Defense game and I have learnt quite a bit from all of you and your previous posts.

I am currently creating the game in Allegro 5 and the source code that I am porting contains a call to do_line.

#SelectExpand
1void doline(BITMAP *bmp, int x, int y, int d) 2{ 3 //line callback function...fills the points array 4 points[totalpoints].x = x; 5 points[totalpoints].y = y; 6 totalpoints++; 7} 8 9void firenewmissile() 10{ 11 12 //random starting location 13 x1 = rand() % (SCREEN_W-1); 14 y1 = 20; 15 16 //random ending location 17 x2 = rand() % (SCREEN_W-1); 18 y2 = SCREEN_H-50; 19 20 //construct the line point-by-point 21 do_line(buffer,x1,y1,x2,y2,0,&doline); 22}

So reprogramming this call I am now just drawing a line on the display instead of a line being draw segment by segment.

The issue that I am having is that al_draw_soft_line has no examples and I am unsure how and why to use the state parameter for this function. Can someone please point me to or show me how to use this.
Thanks and again,
Scorliss

Dizzy Egg
Member #10,824
March 2009
avatar

"see the implementation of the various shading routines in addons/primitives/line_soft.c"

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

Polybios
Member #12,293
October 2010

Hi,

what exactly are you trying to achieve? I must admit I don't remember Missile Defense clearly. Do you want to track the position for collision detection? What is points[totalpoints]?

Maybe you could do it with simple (floating point) 2D vectors instead of tracking pixels in software as you seem to do?

Peter Hull
Member #1,136
March 2001

Hi scorliss!

When you call al_draw_soft_line you pass in three functions, first, step and draw. Allegro will call your functions:

first(...); // Just once

step(...);
draw(...);
step(...);
draw(...); 
... // for every point on the line

So to do the equivalent your code you need something like:

ALLEGRO_VERTEX v1;
ALLEGRO_VERTEX v2;
v1.x = x1;
v1.y = y1;
v2.x = x2;
v2.y = y2;
al_draw_soft_line(&v1, &v2, NULL, NULL, NULL, doline);

with

void doline(uintptr_t pv, int x, int y)
{
(void) pv;
//line callback function...fills the points array   
points[totalpoints].x = x;   
points[totalpoints].y = y;   
totalpoints++;
}

For your example you don't need state so it's null. In other cases it's useful to have some extra data that gets passed along to each function in turn. This is quite a common pattern for callbacks; it helps to avoid global variables.

Bogus example using state:

#SelectExpand
1struct counter { 2 int minor_steps; 3 int major_steps; 4 int points; 5}; 6 7void first(uintptr_t state, int x0, int y0, ALLEGRO_VERTEX* v1, ALLEGRO_VERTEX* v2) { 8 /* Initialise */ 9 struct counter* pc = (struct counter*) state; 10 pc->minor_steps = pc->major_steps = pc->points = 0; 11} 12 13void step(uintptr_t state, int type) { 14 /* count steps */ 15 struct counter* pc = (struct counter*) state; 16 if (type) { 17 ++pc->minor_steps; 18 } else { 19 ++pc->major_steps; 20 } 21} 22 23void draw(uintptr_t state, int x, int y) { 24 /* count points */ 25 struct counter* pc = (struct counter*) state; 26 ++pc->points; 27} 28 29void countline(ALLEGRO_VERTEX* v1, ALLEGRO_VERTEX* v2) { 30 struct counter mycount; 31 al_draw_soft_line(v1, v2, &mycount, first, step, draw); 32 printf("%d minor steps, %d major steps, %d points\n", mycount.minor_steps, mycount.major_steps, mycount.points); 33}

(typed in by hand so may have mistakes!!)

Go to: