Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: cjhrules on Sun 19/03/2006 13:23:24

Title: draw thick line slow
Post by: cjhrules on Sun 19/03/2006 13:23:24
Hi!
I'm using the "draw line function" to draw lines slow. Is it possible to modify this script to increase the thicknes of the line.

THis is the script:


/-------------------------------------------------------

//Ã,  Ã,  Ã,  Ã, This is the script for the Red line on the mapsÃ,  Ã,  Ã, 
//-------------------------------------------------------
function StepsRightDown(int beginX, int beginY, int endX, int endY, int delay, int stepX, int stepY){
Ã,  int flag;Ã,  //flag to leave Y loop
Ã,  while(beginX<=endX){
Ã,  Ã,  beginX++;
Ã,  Ã,  if(beginX%(stepX)==0){Ã,  //the step equasion. If X is the right stepvalue, it will stop and let Y move
Ã,  Ã,  Ã,  while(flag==0){
Ã,  Ã,  Ã,  Ã,  beginY++;
Ã,  Ã,  Ã,  Ã,  if(beginY%(stepY)==0){Ã,  //if Y is done, it goes back to moving X
Ã,  Ã,  Ã,  Ã,  Ã,  flag=1;
Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  Ã,  Ã,  RawDrawLine(beginX/10,beginY/10,beginX/10,beginY/10);Ã,  //deviding everthing back to screen co-ord.
Ã,  Ã,  Ã,  Ã,  if(beginY%delay==0)Wait(1); //delay is anti delay. meaning bigger value makes line go faster. delay can't be 0!
Ã,  Ã,  Ã,  }
Ã,  Ã,  Ã,  flag=0;
Ã,  Ã,  }
Ã,  Ã,  RawDrawLine(beginX/10,beginY/10,beginX/10,beginY/10);
Ã,  Ã,  if(beginX%delay==0)Wait(1);
Ã,  }
}

function StepsLeftDown(int beginX, int beginY, int endX, int endY, int delay, int stepX, int stepY){
Ã,  int flag;
Ã,  while(beginX>=endX){Ã, 
Ã,  Ã,  beginX--;
Ã,  Ã,  if(beginX%(stepX)==0){
Ã,  Ã,  Ã,  while(flag==0){
Ã,  Ã,  Ã,  Ã,  beginY++;
Ã,  Ã,  Ã,  Ã,  if(beginY%(stepY)==0){
Ã,  Ã,  Ã,  Ã,  Ã,  flag=1;
Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  Ã,  Ã,  RawDrawLine(beginX/10,beginY/10,beginX/10,beginY/10);
Ã,  Ã,  Ã,  Ã,  if(beginY%delay==0)Wait(1);
Ã,  Ã,  Ã,  }
Ã,  Ã,  Ã,  flag=0;
Ã,  Ã,  }
Ã,  Ã,  RawDrawLine(beginX/10,beginY/10,beginX/10,beginY/10);
Ã,  Ã,  if(beginX%delay==0)Wait(1);
Ã,  }
}

function StepsRightUp(int beginX, int beginY, int endX, int endY, int delay, int stepX, int stepY){
Ã,  int flag;
Ã,  while(beginX<=endX){Ã, 
Ã,  Ã,  beginX++;
Ã,  Ã,  if(beginX%(stepX)==0){
Ã,  Ã,  Ã,  while(flag==0){
Ã,  Ã,  Ã,  Ã,  beginY--;
Ã,  Ã,  Ã,  Ã,  if(beginY%(stepY)==0){
Ã,  Ã,  Ã,  Ã,  Ã,  flag=1;
Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  Ã,  Ã,  RawDrawLine(beginX/10,beginY/10,beginX/10,beginY/10);
Ã,  Ã,  Ã,  Ã,  if(beginY%delay==0)Wait(1);
Ã,  Ã,  Ã,  }
Ã,  Ã,  Ã,  flag=0;
Ã,  Ã,  }
Ã,  Ã,  RawDrawLine(beginX/10,beginY/10,beginX/10,beginY/10);
Ã,  Ã,  if(beginX%delay==0)Wait(1);
Ã,  }
}

function StepsLeftUp(int beginX, int beginY, int endX, int endY, int delay, int stepX, int stepY){
Ã,  int flag;
Ã,  while(beginX>=endX){Ã, 
Ã,  Ã,  beginX--;
Ã,  Ã,  if(beginX%(stepX)==0){
Ã,  Ã,  Ã,  while(flag==0){
Ã,  Ã,  Ã,  Ã,  beginY--;
Ã,  Ã,  Ã,  Ã,  if(beginY%(stepY)==0){
Ã,  Ã,  Ã,  Ã,  Ã,  flag=1;
Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  Ã,  Ã,  RawDrawLine(beginX/10,beginY/10,beginX/10,beginY/10);
Ã,  Ã,  Ã,  Ã,  if(beginY%delay==0)Wait(1);
Ã,  Ã,  Ã,  }
Ã,  Ã,  Ã,  flag=0;
Ã,  Ã,  }
Ã,  Ã,  RawDrawLine(beginX/10,beginY/10,beginX/10,beginY/10);
Ã,  Ã,  if(beginX%delay==0)Wait(1);
Ã,  }
}

function Steps(int beginX, int beginY, int endX, int endY, int colour, int delay, int steps){Ã,  //X & Y are screen co-ord multiplied by 10. If you test it and there is still a deviation, add exrta 0's
Ã,  int stepX;
Ã,  int stepY;
Ã,  RawSetColor(colour);
Ã,  ifÃ,  Ã,  Ã, (beginX-endX<0&&beginY-endY<0){Ã,  //right down
Ã,  Ã,  ifÃ,  Ã,  Ã, ((endX-beginX)<=(endY-beginY)&&steps==0){stepX=10; stepY=(endY-beginY)*10/(endX-beginX);}Ã,  //if you use steps=0 then the function will draw a straight line
Ã,  Ã,  else if((endX-beginX)> (endY-beginY)&&steps==0){stepX=(endX-beginX)*10/(endY-beginY); stepY=10;}Ã,  //because we use ints, we have to check if hor is bigger then ver first (to avoid value/0)
Ã,  Ã,  elseÃ,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã, {stepX=(endX-beginX)*10/steps; stepY=(endY-beginY)*10/steps;}Ã,  //steps can't be bigger then the maximum steps (endX-beginX) or (endY-beginY) wichever is smaller
Ã,  Ã,  if(steps/10<(endX-beginX)&&steps/10<(endY-beginY))StepsRightDown(beginX,beginY,endX,endY,delay,stepX,stepY);Ã,  //eqasion to prevent value/0 errors. it wil just skip the function
Ã,  }
Ã,  else if(beginX-endX>0&&beginY-endY<0){Ã,  //left down
Ã,  Ã,  ifÃ,  Ã,  Ã, ((beginX-endX)<=(endY-beginY)&&steps==0){stepX=10; stepY=(endY-beginY)*10/(beginX-endX);}Ã,  //the values are multiplied by 10 to make them more acured
Ã,  Ã,  else if((beginX-endX)> (endY-beginY)&&steps==0){stepX=(beginX-endX)*10/(endY-beginY); stepY=10;}Ã,  //this looks all very complicated, but it's only calculating the max amount of steps
Ã,  Ã,  elseÃ,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã, {stepX=(beginX-endX)*10/steps; stepY=(endY-beginY)*10/steps;}Ã,  //begin & end CAN't be the same value! or you'll get value/0!
Ã,  Ã,  if(steps/10<(beginX-endX)&&steps/10<(endY-beginY))StepsLeftDown(beginX,beginY,endX,endY,delay,stepX,stepY);
Ã,  }
Ã,  else if(beginX-endX<0&&beginY-endY>0){Ã,  //right up
Ã,  Ã,  ifÃ,  Ã,  Ã, ((endX-beginX)<=(beginY-endY)&&steps==0){stepX=10; stepY=(beginY-endY)*10/(endX-beginX);}Ã,  //If you test it and there is still a deviation, add exrta 0's to all the 10's.
Ã,  Ã,  else if((endX-beginX)> (beginY-endY)&&steps==0){stepX=(endX-beginX)*10/(beginY-endY); stepY=10;}Ã,  //it will slow down delay because it will draw every point 100 times
Ã,  Ã,  elseÃ,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã, {stepX=(endX-beginX)*10/steps; stepY=(beginY-endY)*10/steps;}
Ã,  Ã,  if(steps/10<(endX-beginX)&&steps/10<(beginY-endY))StepsRightUp(beginX,beginY,endX,endY,delay,stepX,stepY);
Ã,  }
Ã,  else if(beginX-endX>0&&beginY-endY>0){Ã,  //left up
Ã,  Ã,  ifÃ,  Ã,  Ã, ((beginX-endX)<=(beginY-endY)&&steps==0){stepX=10; stepY=(beginY-endY)*10/(beginX-endX);}
Ã,  Ã,  else if((beginX-endX)> (beginY-endY)&&steps==0){stepX=(beginX-endX)*10/(beginY-endY); stepY=10;}
Ã,  Ã,  elseÃ,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã, {stepX=(beginX-endX)*10/steps; stepY=(beginY-endY)*10/steps;}
Ã,  Ã,  if(steps/10<(beginX-endX)&&steps/10<(beginY-endY))StepsLeftUp(beginX,beginY,endX,endY,delay,stepX,stepY);
Ã,  }
}
Title: Re: draw thick line slow
Post by: fovmester on Sun 19/03/2006 14:05:27
Whoa, that a hell lot of code!
Title: Re: draw thick line slow
Post by: cjhrules on Sun 19/03/2006 14:15:22
Yeah, I found it on this forum. I thought I better post it in case someone knows someting about this.
Title: Re: draw thick line slow
Post by: Adrian on Sat 13/03/2010 16:08:44
Hi,
is there a newer version of the code that suits the latest version of AGS?
Title: Re: draw thick line slow
Post by: monkey0506 on Sat 13/03/2010 18:55:15
//-------------------------------------------------------
// This is the script for the Red line on the maps
//-------------------------------------------------------
DrawingSurface *backgroundSurface;
DrawingSurface *backgroundBackup;
int backgroundDrawingColor = 0;

function on_event(EventType event, int data) {
  if (event == eEventEnterRoomAfterFadein) {
    backgroundSurface = Room.GetDrawingSurfaceForBackground();
    backgroundBackup = backgroundSurface.CreateCopy();
    backgroundSurface.Release();
    backgroundSurface = null;
    backgroundDrawingColor = 0;
  }
  else if (event == eEventLeaveRoom) {
    if (backgroundSurface != null) backgroundSurface.Release();
    if (backgroundBackup != null) backgroundBackup.Release();
    backgroundSurface = null;
    backgroundBackup = null;
  }
}

function StepsRightDown(int beginX, int beginY, int endX, int endY, int delay, int stepX, int stepY) {
  backgroundSurface = Room.GetDrawingSurfaceForBackground();
  backgroundSurface.DrawingColor = backgroundDrawingColor;
  int flag; // flag to leave Y loop
  while (beginX <= endX) {
    beginX++;
    if ((beginX % stepX) ==0) { // the step equation. If X is the right stepvalue, it will stop and let Y move
      while (flag == 0) {
        beginY++;
        if ((beginY % stepY) == 0) { // if Y is done, it goes back to moving X
          flag=1;
        }
        backgroundSurface.DrawPixel(beginX / 10, beginY / 10); // dividing everything back to screen co-ord.
        if ((beginY % delay) == 0) Wait(1); // delay is anti delay. meaning bigger value makes line go faster. delay can't be 0!
      }
      flag = 0;
    }
    backgroundSurface.DrawPixel(beginX / 10, beginY / 10);
    if ((beginX % delay) == 0) Wait(1);
  }
  backgroundSurface.Release();
  backgroundSurface = null;
}

function StepsLeftDown(int beginX, int beginY, int endX, int endY, int delay, int stepX, int stepY) {
  backgroundSurface = Room.GetDrawingSurfaceForBackground();
  backgroundSurface.DrawingColor = backgroundDrawingColor;
  int flag;
  while (beginX >= endX) {
    beginX--;
    if ((beginX % stepX) == 0) {
      while (flag == 0) {
        beginY++;
        if ((beginY % stepY) == 0) {
          flag = 1;
        }
        backgroundSurface.DrawPixel(beginX / 10, beginY / 10);
        if ((beginY %delay) == 0) Wait(1);
      }
      flag = 0;
    }
    backgroundSurface.DrawPixel(beginX / 10, beginY / 10);
    if ((beginX % delay) == 0) Wait(1);
  }
  backgroundSurface.Release();
  backgroundSurface = null;
}

function StepsRightUp(int beginX, int beginY, int endX, int endY, int delay, int stepX, int stepY) {
  backgroundSurface = Room.GetDrawingSurfaceForBackground();
  backgroundSurface.DrawingColor = backgroundDrawingColor;
  int flag;
  while (beginX <= endX) {
    beginX++;
    if ((beginX % stepX) == 0) {
      while (flag == 0) {
        beginY--;
        if ((beginY % stepY) == 0) {
          flag = 1;
        }
        backgroundSurface.DrawPixel(beginX / 10, beginY / 10);
        if ((beginY % delay) == 0) Wait(1);
      }
      flag = 0;
    }
    backgroundSurface.DrawPixel(beginX / 10, beginY / 10);
    if ((beginX % delay) == 0) Wait(1);
  }
  backgroundSurface.Release();
  backgroundSurface = null;
}

function StepsLeftUp(int beginX, int beginY, int endX, int endY, int delay, int stepX, int stepY) {
  backgroundSurface = Room.GetDrawingSurfaceForBackground();
  backgroundSurface.DrawingColor = backgroundDrawingColor;
  int flag;
  while (beginX >= endX) {
    beginX--;
    if ((beginX % stepX) == 0) {
      while (flag == 0) {
        beginY--;
        if ((beginY % stepY) == 0) {
          flag = 1;
        }
        backgroundSurface.DrawPixel(beginX / 10, beginY / 10);
        if ((beginY %delay) == 0) Wait(1);
      }
      flag = 0;
    }
    backgroundSurface.DrawPixel(beginX / 10, beginY / 10);
    if ((beginX % delay) == 0) Wait(1);
  }
  backgroundSurface.Release();
  backgroundSurface = null;
}

function Steps(int beginX, int beginY, int endX, int endY, int colour, int delay, int steps) { // X & Y are screen co-ord multiplied by 10. If you test it and there is still a deviation, add extra 0's
  int stepX;
  int stepY;
  backgroundDrawingColor = colour;
  if (((beginX - endX) < 0) && ((beginY - endY) < 0)) { //right down
    if (((endX - beginX) <= (endY - beginY)) && (steps == 0)) {
      // if you use steps = 0 then the function will draw a straight line
      stepX = 10;
      stepY = ((endY - beginY) * 10) / (endX - beginX);
    }
    else if (((endX - beginX) > (endY - beginY)) && (steps == 0)) {
      // because we use ints, we have to check if hor is bigger then ver first (to avoid value / 0)
      stepX = ((endX - beginX) * 10) / (endY - beginY);
      stepY = 10;
    }
    else {
      // steps can't be bigger then the maximum steps (endX - beginX) or (endY - beginY) whichever is smaller
      stepX = ((endX - beginX) * 10) / steps;
      stepY = ((endY-beginY) * 10) / steps;
    }
    if (((steps / 10) < (endX - beginX)) && ((steps / 10 < (endY - beginY))) StepsRightDown(beginX, beginY, endX, endY, delay, stepX, stepY); //equation to prevent value / 0 errors. it wil just skip the function
  }
  else if (((beginX - endX) > 0) && ((beginY - endY) < 0)) { //left down
    if (((beginX - endX) <= (endY - beginY)) && (steps==0)) {
      // the values are multiplied by 10 to make them more acured
      stepX = 10;
      stepY = ((endY-beginY) * 10) / (beginX - endX);
    }
    else if (((beginX - endX) > (endY - beginY)) && (steps == 0)) {
      // this looks all very complicated, but it's only calculating the max amount of steps
      stepX = ((beginX - endX) * 10) / (endY - beginY);
      stepY = 10;
    }
    else {
      // begin & end can't be the same value! or you'll get value / 0!
      stepX = ((beginX - endX) * 10) / steps;
      stepY = ((endY - beginY) * 10) / steps;
    }
    if (((steps / 10) < (beginX - endX)) && ((steps / 10) < (endY - beginY))) StepsLeftDown(beginX, beginY, endX, endY, delay, stepX, stepY);
  }
  else if (((beginX - endX) < 0) && ((beginY - endY) > 0)) { //right up
    if (((endX - beginX) <= (beginY - endY)) && (steps == 0)) {
      // If you test it and there is still a deviation, add extra 0's to all the 10's.
      stepX = 10;
      stepY = ((beginY - endY) * 10) / (endX - beginX);
    }
    else if (((endX - beginX) > (beginY - endY)) && (steps == 0)) {
      // it will slow down delay because it will draw every point 100 times
      stepX = ((endX - beginX) * 10) / (beginY - endY);
      stepY = 10;
    }
    else {
      stepX = ((endX - beginX) * 10) / steps;
      stepY = ((beginY - endY) * 10) / steps;
    }
    if (((steps / 10) < (endX - beginX)) && ((steps / 10) < (beginY - endY))) StepsRightUp(beginX, beginY, endX, endY, delay, stepX, stepY);
  }
  else if (((beginX - endX) > 0) && ((beginY - endY) > 0)){ // left up
    if (((beginX - endX) <= (beginY - endY)) && (steps == 0)) {
      stepX = 10;
      stepY = ((beginY - endY) * 10) / (beginX - endX);
    }
    else if (((beginX - endX) > (beginY - endY)) && (steps == 0)) {
      stepX = ((beginX - endX) * 10) / (beginY - endY);
      stepY=10;
    }
    else {
      stepX = ((beginX - endX) * 10) / steps;
      stepY = ((beginY - endY) * 10) / steps;
    }
    if (((steps / 10) < (beginX - endX)) && ((steps / 10) < (beginY-endY))) StepsLeftUp(beginX, beginY, endX, endY, delay, stepX, stepY);
  }
}

function ClearLines() {
  if (backgroundBackup == null) return;
  if (backgroundSurface == null) backgroundSurface = Room.GetDrawingSurfaceForBackground();
  backgroundSurface.DrawSurface(backgroundBackup);
  backgroundSurface.Release();
  backgroundBackup.Release();
  backgroundSurface = null;
  backgroundBackup = null;
}


Note that I haven't actually tested this, I just switched it over to use the DrawingSurface functions.
Title: Re: draw thick line slow
Post by: Khris on Sat 13/03/2010 20:35:30
Does this seriously use four times almost identical code for every direction...? Wow.
Title: Re: draw thick line slow
Post by: monkey0506 on Sat 13/03/2010 20:59:57
I didn't actually analyze the code or make modifications to it (much) other than just updating it as requested. But you probably have a fair point. Ha!

I recently wrote something to do this myself that I suppose I could release as a module if people are interested in it. Specifically the way I wrote the code was for connecting multiple points with a set number of sub-points in-between. The higher the number of sub-points, the slower the points are connected.
Title: Re: draw thick line slow
Post by: Adrian on Mon 15/03/2010 20:40:59
WOW!
Thaks a lot, monkey! That's more than a fair point to me! :-)

I transfered your code into my global script, which worked fine except two minor problems:

1. Since there is no "eEventAfterFadeIn" for the on_event function (as far as I know), I put the "eEventAfterFadeIn" in the room's room_AfterFadeIn function (as Khris described here: http://www.adventuregamestudio.co.uk/yabb/index.php?topic=32074.0) and declared the variables there too. In this room I want the lines to be drawn.

2. I got an error saying that input ended in the middle of an expression (or something like that). So I changed this line in the function Steps(...):

if (((steps / 10) < (endX - beginX)) && ((steps / 10 < (endY - beginY))) StepsRightDown(beginX, beginY, endX, endY, delay, stepX, stepY); //equation to prevent value / 0 errors. it wil just skip the function

into:

if (((steps / 10) < (endX - beginX)) && ((steps / 10 < (endY - beginY)))) {
      StepsRightDown(beginX, beginY, endX, endY, delay, stepX, stepY);
    } //equation to prevent value / 0 errors. it wil just skip the function

I hope that's right. At least there is no more error comming up! :-)

Now all I need to know is how I can call these functions from within the room script, what parameters do I have to use and so on. Can you give a practical example?

Thanks so much for your help!!

Greetings
adrian
Title: Re: draw thick line slow
Post by: monkey0506 on Mon 15/03/2010 20:47:05
Sadly seeing as I didn't author the code I don't really know what to tell you to use as far as the parameters and such (as I said I didn't really analyze the code much).

But good point about me missing that closing parenthesis there. :D
Title: Re: draw thick line slow
Post by: Adrian on Mon 15/03/2010 20:52:07
Never mind. Maybe I'll figure it out by myself. That was a big step foreward, thaks again!

By the way: I guess I shouldn't be the only one who would be glad about a module for connecting multiple points with a variable number of sub-points. Sounds interesting!

Bye!