Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: Calin Leafshade on Fri 27/11/2009 09:41:13

Title: A visual effect.
Post by: Calin Leafshade on Fri 27/11/2009 09:41:13
Im not sure if anyone has asked for artistic critique on some code before but i'm trying to create the classic 'Lets go have a turn based battle" visual effect found in most JRPGs but i feel it could use some improvement.

The code should be pasteable straight into a game, it has no dependencies. It must be a 320x240 game or you must alter the values.

I apologise for the messiness.. i havent cleaned it up yet.

'paintovers' and ideas are very welcome


function DoBattle(){
 int Displacement[241]; //displacement for each line
 int Transparancy1[241]; // Transparencies for edges
 int Transparancy2[241];
 
 
 DynamicSprite *ScreenShot = DynamicSprite.CreateFromScreenShot();
 DrawingSurface *ScreenShotSurface = ScreenShot.GetDrawingSurface();
 DynamicSprite *CurrentFrame = DynamicSprite.CreateFromDrawingSurface(ScreenShotSurface, 0, 0, 320, 240);
 DrawingSurface *BG = Room.GetDrawingSurfaceForBackground();
 int i = 0;
   DrawingSurface *CurrentFrameSurface = CurrentFrame.GetDrawingSurface();
 
 while (i < 241){
   Displacement[i] = 0; //reset displacement
   i++;
 }
 
 bool Loop = true;
 Displacement[0] = -5;
 while (Loop){
   i = 1;
    CurrentFrameSurface.Clear(16); //clear surface with black each frame
   while (i < 240){ //for each line
       
   DynamicSprite *Line = DynamicSprite.CreateFromDrawingSurface(ScreenShotSurface, 0, i, 290, 1); //take a line from the screenshot
   DynamicSprite *Line2 = DynamicSprite.CreateFromDrawingSurface(ScreenShotSurface, 270, i, 40, 1); // take 2 more chunks for fading out the lines
   DynamicSprite *Line3 = DynamicSprite.CreateFromDrawingSurface(ScreenShotSurface, 300, i, 20, 1);
   
   if (Displacement[i] > -320) { //if the line has started to move
     Line.Tint(255, 0, 0, Displacement[i] * (-4) / 8, 100); //tint the lines
     Line2.Tint(255, 0, 0, Displacement[i] * (-4) / 8, 100);
     Line3.Tint(255, 0, 0, Displacement[i] * (-4) / 8, 100);
     CurrentFrameSurface.DrawImage(Displacement[i], i, Line.Graphic); //draw line
     
     CurrentFrameSurface.DrawImage(Displacement[i] + 270, i, Line2.Graphic, Transparancy1[i]);
     CurrentFrameSurface.DrawImage(Displacement[i] + 300, i, Line3.Graphic, Transparancy2[i] );
     
   }
   Line.Delete();
   Line2.Delete();
   Line3.Delete();
   i++;
   }
   
   Overlay *myOverlay = Overlay.CreateGraphical(0, 0, CurrentFrame.Graphic,  true);
 
   BG.Clear(); //clear BG.. probably not necessary.
 
   Wait(1);
   i = 1;
   Displacement[0] -= 5;
   while (i < 240){ //update displacement array
     
     if (Displacement[i-1] < -5) {
     Displacement[i] = Displacement[i-1] + 5;
     Transparancy1[i] = 50;
     Transparancy2[i] = 80;
     }
     i ++;
   }
   if (Displacement[239] < -320) Loop = false; //if last line is offscreen
 }

   ScreenShotSurface.Release();
 ScreenShot.Delete();
 CurrentFrameSurface.Release();
 CurrentFrame.Delete();
 BG.Release();


 
 
}


EDIT: I apologise for posting in the wrong forum but I have to question your rationale for putting it in the the Talk and Chat forum.

Surely the Technical forum or Critics lounge are the only two applicable forums?
Title: Re: A visual effect.
Post by: Khris on Fri 27/11/2009 10:48:09
Here's my take on it:

void ScreenEffect() {
 DynamicSprite*line[240];
 
 int i;
 while(i < 240) {
   line[i] = DynamicSprite.CreateFromBackground(0, 0, i, 320, 1);
   i++;
 }
 
 DrawingSurface*ds;
 
 i=0;
 int y;
 int x;
 int end = 300;
 while (i<end) {
   y = 0;
   ds = Room.GetDrawingSurfaceForBackground();
   ds.Clear(Game.GetColorFromRGB(255, 255, 255));
   while (y < 240) {
     x = FloatToInt(Maths.Sin(IntToFloat(i+y)/30.0)*IntToFloat(i));
     
     // additional displacement
     if (y%2) x = -x;

     ds.DrawImage(x, y, line[y].Graphic, (i*100)/end);
     y++;
     if (i == end) line[i].Delete();
   }
   ds.Release();
   Wait(1);
   i++;
 }
}


Edit:
I've just noticed two pretty odd things:

The outer while loop uses (i<end), that's why I put if (i == end - 1) line[i].Delete();BUT, that'll throw an error, since then AGS tries to draw a deleted sprite.
Changing the condition to (i == end) fixes that, but shouldn't it never get called that way?
i is only incremented at the end of the loop after all.

Secondly, I've accidentally used line[i].Delete(); while it's supposed to be line[y], yet AGS doesn't throw an out of bounds error although i goes up to 300... very weird.
Title: Re: A visual effect.
Post by: Andail on Fri 27/11/2009 10:51:38
Yeah sorry meant to send it to the technical forum. Fixed it now.
Title: Re: A visual effect.
Post by: Calin Leafshade on Fri 27/11/2009 10:53:04
Thats very very nice, Khris.

and certainly much more elegantly coded than mine.
Title: Re: A visual effect.
Post by: Calin Leafshade on Fri 27/11/2009 12:15:46
how about this?

a (messy) adaptation of khris's code to add a kind of additive dissolve effect.


void ScreenEffect() {
 DynamicSprite*line[240];
 DynamicSprite*Buffer = DynamicSprite.CreateFromScreenShot();
 DynamicSprite*Black = DynamicSprite.Create(320, 240, true);
 DrawingSurface*ds;
 ds = Buffer.GetDrawingSurface();
  DrawingSurface*BG;
     BG = Black.GetDrawingSurface();
     BG.Clear(16);
 
 int i;
 while(i < 240) {
   line[i] = DynamicSprite.CreateFromDrawingSurface(ds, 0, i, 320, 1);
   i++;
 }
 
 

 
 i=0;
 int y;
 int x;
 int end = 300;
 
   
   

 while (i<end) {
   y = 0;
   
   while (y < 240) {
     x = FloatToInt(Maths.Sin(IntToFloat(i+y)/30.0)*IntToFloat(i));
     
     // additional displacement
     if (y%2) x = -x;
     line[y].Tint(255, 255, 255, (i*100)/end, 100 - ((i*50)/end) + 50);
     ds.DrawImage(x , y + Random(4) - 2, line[y].Graphic, ((i*50)/end) + 50);
     
     y++;
     if (i == end) line[i].Delete();
   }
   if (i > 200) ds.DrawImage(0, 0, Black.Graphic, 100 - (i - 200));
   Overlay *myOverlay = Overlay.CreateGraphical(0, 0, Buffer.Graphic, true);
   int z;
   while (z < 240) {
   line[z] = DynamicSprite.CreateFromDrawingSurface(ds, 0,z, 320, 1);
  z++;
 }
   
   Wait(1);
   i++;
 }
 ds.Release();
 BG.Release();
}


EDIT: hold on the Txt speak thing edited my code.. surely code should be exempt, admins?

Fixed, i think.