Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: spook1 on Thu 10/01/2008 12:20:48

Title: Draw dotted "moving" line? [SOLVED]
Post by: spook1 on Thu 10/01/2008 12:20:48
In my game the player can create electrical circuits by drawing lines between terminals.
I would like to indicate the current flowing through the wire (drawline) by a dot-pattern that moves along the line.
It is a serious game for learning electricity, thats why I want the current to be "visible" in the lower levels.

Can anyone help me with a suggestion? Or would such a feature be too slow when I draw about 12 lines.

Thanks in advance for thinking along with me,

Martijn
Title: Re: draw dotted "moving" line?
Post by: Khris on Thu 10/01/2008 16:00:23
Sounds like the perfect situation to use color-cycling. Unfortunately, that requires the game to use 256 cols instead of 16bit.
How are the lines drawn by the player? Does he click on the starting point, then click on the ending point? Are they always straight?
Please provide a bit more information.
Title: Re: draw dotted "moving" line?
Post by: spook1 on Thu 10/01/2008 18:47:26
Line are drawn by the using by clicking the start and end point.
For the complete script see:

http://www.adventuregamestudio.co.uk/yabb/index.php?topic=12390.0http://www.adventuregamestudio.co.uk/yabb/index.php?topic=12390.0

Thanks for the quick reply and interest in my question,

Sincerely,

Martijn
Title: Re: draw dotted "moving" line?
Post by: Galen on Thu 10/01/2008 18:51:08
The only method I can think of would involve drawing points are 1px sprites along a line (very possible but extremely slow I'd expect).
Title: Re: draw dotted "moving" line?
Post by: BOYD1981 on Thu 10/01/2008 19:36:25
colour cycling wouldn't be very good for this purpose as it repeatedly cycles the colours with no way to control the speed (it cycles way too fast) other than by setting the actual game speed itself which will slow everything else down, it also gets blocked by other scripts.
i'm no AGS expert but i'd reckon the best way to do it is by using RawDraw.
also, i believe that there was another electronics game that used rawdraw to show wires on a circuit board a few years ago also for educational use, i've never tried it out myself but it might be worth searching for it and contacting the author for advice.
Title: Re: draw dotted "moving" line?
Post by: spook1 on Thu 10/01/2008 22:39:43
Hmm, it might have been meself? I am working (on and off, chaged jobs etc) on my educational game. I 've been around in this forum since a few years ;-)
That's why I referred to the link above...
Title: Re: draw dotted "moving" line?
Post by: Gilbert on Fri 11/01/2008 00:57:25
Quote from: BOYD1981 on Thu 10/01/2008 19:36:25
colour cycling wouldn't be very good for this purpose as it repeatedly cycles the colours with no way to control the speed (it cycles way too fast) other than by setting the actual game speed itself which will slow everything else down, it also gets blocked by other scripts.

False. You can control this via script. However, as 8-bit colour is not a general solution to most people we need other methods.

Edit:
I came up with an idea to implement this "marching ant" broken line effect.
My idea is just raw draw a line in a separate bg frame and erase sections of the line drawn and then grab it as a dynamic sprite (if you're using AGS V3 you don't even need to use the bg frame, as you can draw directly on a dynamic sprite using the drawing surface features).

I have mucked up a quick example:
http://www.2dadventure.com/ags/mant000.zip (http://www.2dadventure.com/ags/mant000.zip)
Use AGS V2.72 to compile, just click click click to see the effect.

As it's rushed and I don't have time to polish it, note that:
1. I never checked your original circuit codes as I can't afford the time for it, so it's just an idea to show how this can be done, if you want to incorporate this into your original codes you need to do some works.
2. It is messy and unoptimised.
3. I just used arrays of size 10 to hold the information, so there can only be at most 10 lines on screen, if your draw more lines the older ones will disappear.
4. The lines are not broken perfectly at some angles, possibly due to precision errors, etc. in calculation, needs tweakings.
5. If you run it in high-res. 640x480, sometimes there are residual of the last lines drawn, changing the last RawDrawRectangle() call to a RawClearScreen() may fix this.
Title: Re: draw dotted "moving" line?
Post by: Radiant on Fri 11/01/2008 08:57:31
No, it would not be too slow.

It takes a bit of coding (not too much) but the easiest way to do it would be to make your own line function. Put the following in your global script:


#define MAX_LINE 10
int frame, linecount;
struct lineinfo { int x1, y1, x2, y2 };
lineinfo line [MAX_LINE];

function my_line (int x1, int y1, int x2, int y2) {
  if (linecount < MAX_LINE) {
    line[linecount].x1 = x1;
    line[linecount].y1 = y1;
    line[linecount].x2 = x2;
    line[linecount].y2 = y2;
    linecount ++;
  }
}

function repeatedly_execute () {
  frame = (frame +1) % 4;
  RawRestoreScreen ();
  int i=0;
  while (i < MAX_LINE) {
    draw_line (line[i].x1, line[i].y1, line[i].x2, line[i].y2, frame);
    i ++l
  }
}


Add add a "RawSaveScreen()" to the entering rooms event.

Now the only thing you need is a draw_line() function that, for every four pixels, only draws the first one. You can find a decent line-drawing function here (http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm).
Title: Re: draw dotted "moving" line?
Post by: Gilbert on Fri 11/01/2008 09:55:30
It's not completely the same thing, but Radiant's suggestion did inspire me a little bit that it can be done the other way round, where you just draw the segments separately instead of drawing the whole line and then break it apart.

So this is a newer version:
http://www.2dadventure.com/ags/mant001.zip (http://www.2dadventure.com/ags/mant001.zip)
So the lines no longer look as odd as the previous one, and you can still view the original method by pressing ESC to get to room 2.

Note for this alternate method:
1. The destination point can be off by a few pixels, I just don't care to fix.
2. Since the segments are now drawn separately instead of just drawing one line, the orientation of each segment could be bit off, maybe if eRoundNearest rounding mode is used for the FloatToInt() calls it will help a bit but I doubt if the results are really that noticiable.
Title: Re: draw dotted "moving" line?
Post by: spook1 on Sun 13/01/2008 13:21:27
Man! You guys are great.
I am really curious to see the example, but I cannot open it in ags 272.
I downloaded AGS3.0 beta but then I ran into a lot of errors,

Can I ask in what versions the example was created?

regards and a many trhanks for the help!
I'll present the result of whole circuit as soon as I have it working

yours sincerely,

Martijn
Title: Re: draw dotted "moving" line?
Post by: Gilbert on Sun 13/01/2008 14:24:23
As I mentioned in my first post, it's V2.72.

How did you open it?

Remember it's just a quickly mucked up game to show how this may be done, NOT a ready-yo-use template or a module, just choose the .dta file in 'Load an existing game' upon starting the editor.

(Well, actually I didn't think of it, in the new method where segments are drawn separately, you don't really need to create multiple sprites, just draw all the lines onto the frame without erasing them and grab them as one sprite, if you don't need the lines to be on top of other stuff, you can even just draw them directly on the current background, and use RawRestoreScreen() to revert the changes between frames.)
Title: Re: draw dotted "moving" line?
Post by: GarageGothic on Sun 13/01/2008 14:27:21
Of course with the new DrawingSurface functions in AGS 3.0 it won't even be necessary to draw to/grab from the background. You can draw everything onto a single sprite and choose to display it as an object, overlay or gui.
Title: Re: draw dotted "moving" line?
Post by: spook1 on Sun 13/01/2008 15:37:47
Quote from: Gilbot V7000a on Sun 13/01/2008 14:24:23
As I mentioned in my first post, it's V2.72.

How did you open it?


Hmm, excuse me for being silly.  I still had a v2.72 beta installed. I upgrade to the stable  version, now I see the ants marching. Great!

I am not sure that I understand the remarks on the RawRestoreScreen.
It has been a while. I'll study the script to rewrite the DrawWires function into an marching ants equivalent:


function DrawWires() {   
   int i;   // Erase the wires first   
   RawRestoreScreen();   /
   // Draw the wires   
   i=0;   
   while (i<MAX_WIRES) {     
      if (wire.active) {         
           RawSetColor(wire.xcolor);         
           RawDrawLine(wire.from_x, wire.from_y, wire.to_x, wire.to_y);     
     }     
   i++;   
   }
}


If I understand it allright, this script will bge very hard to convert to AGS 3.0, because of the unsopported Rawdraw functions.
I am right in that remark?
Title: Re: draw dotted "moving" line?
Post by: Gilbert on Sun 13/01/2008 15:49:02
Yes, or just put the RawRestoreScreen() at the start of the repeatedly execute functions (in case you need to call DrawWires() multiple times, which is not very likely though).

Actually, the DrawingSurface features in V3.00 can do everything RawDraw can, moreover you can drawing directly to a dynamic sprite instead of the backgrounds, which is very useful, just need a bit of time to get accompanied with them.
Title: Re: draw dotted "moving" line? (Solved)
Post by: spook1 on Mon 14/01/2008 16:58:32
The script works fine.
I implemented it with just the rawredraw function. It is great!
As soon as I have the whole room functioning (i.e. speed of ants proportional to current) I'll post the compiled game ;-)

Thanks a lot!