Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: henrikes on Mon 06/11/2017 21:36:22

Title: Transition from a Background to another
Post by: henrikes on Mon 06/11/2017 21:36:22
The code bellow is suposed to transition from a BackGround to another.... but its not working like expected

Code (ags) Select

function setBlur (bool bBlur)
{
  DrawingSurface* mainBackground  = Room.GetDrawingSurfaceForBackground();
  DrawingSurface* newBackground;

  if (bBlur) {
    newBackground = Room.GetDrawingSurfaceForBackground(2); // Blur
  } else {
    newBackground = Room.GetDrawingSurfaceForBackground(1); // Clear
  }

  int i = 99;
  while ( i > 0 )
  {
    mainBackground = Room.GetDrawingSurfaceForBackground();
    mainBackground.DrawSurface(newBackground, i);
    i --;
    Wait(1);
    mainBackground.Release();
  }
}


To test i added the bellow options to on_key_press
Code (ags) Select

  if (keycode == eKey0) setBlur(true);
  if (keycode == eKey1) setBlur(false);


1st time i hit 0 works OK but from then on seems i have to hit 1 or 0 a number of times before it execute setBlur again :(

What am i doing wrong???
Title: Re: Transition from a Background to another
Post by: Khris on Tue 07/11/2017 11:03:51
Just to get it out of the way: you would need to hit 0, then 1, then 0, then 1, etc to see the transition each time.
Hitting 0 multiple times should block the game though; can you see the wait cursor when you hit a key that supposedly does nothing?
Title: Re: Transition from a Background to another
Post by: henrikes on Tue 07/11/2017 13:46:08
Yes the Logic would be to hit 0 then 1 then 0 then 1.... :)
The problem is that i hit 0 and runs OK, but after that the keys dont work and i have to hit them several Times before ir works..
..
Title: Re: Transition from a Background to another
Post by: dayowlron on Tue 07/11/2017 14:58:37
Just a shot in the dark. Are you waiting a few seconds between hitting the key, because with the counter at 99 the function would process for about 3 seconds before it would allow the key again.
Title: Re: Transition from a Background to another
Post by: Khris on Tue 07/11/2017 15:05:40
Ok, but the most important question in my post went unanswered:
Quote from: Khris on Tue 07/11/2017 11:03:51can you see the wait cursor when you hit a key that supposedly does nothing?

Your code will block the game for 2.5 seconds, assuming you didn't change the default speed of 40 FPS.

After that, keypresses should be processed fine again. So: can you see the wait cursor? Do you have a wait cursor set up?
Title: Re: Transition from a Background to another
Post by: henrikes on Tue 07/11/2017 20:11:03
Quote from: Khris on Tue 07/11/2017 11:03:51
Just to get it out of the way: you would need to hit 0, then 1, then 0, then 1, etc to see the transition each time.
Hitting 0 multiple times should block the game though; can you see the wait cursor when you hit a key that supposedly does nothing?

Sorry didnt see that question.... let me check that...




.... Just checked, and Yes i see the wait cursor during the time that i cant do a thing :(
Title: Re: Transition from a Background to another
Post by: Khris on Tue 07/11/2017 21:01:04
Right; so the keypress does register, and the loop runs, but the screen doesn't change.

The first thing I'd try is adding newBackground.Release(); to the end of the function.

If that doesn't work, try using
  // before loop
  DynamicSprite* sprite = DynamicSprite.CreateFromBackground(1 + bBlur);

    // inside loop
    mainBackground.drawImage(0, 0, sprite.Graphic, i);

(i.e. drawing a sprite to the current background's surface, as opposed to drawing a surface)
Title: Re: Transition from a Background to another
Post by: henrikes on Tue 07/11/2017 21:30:00
Tried both and the result is still the same.

increased the decrement of the i variable and works much better...
Code (ags) Select
   
  while ( i > 0 ) {
    mainBackground = Room.GetDrawingSurfaceForBackground();
    mainBackground.DrawSurface(newBackground, i);
    //i --;
    i = i-5;
    Wait(1);
    mainBackground.Release();
   
    Label1.Text = String.Format("percent :%d", i);
  }


Khris Tried both your options and the result was the same :(
Title: Re: Transition from a Background to another
Post by: Crimson Wizard on Tue 07/11/2017 21:39:29
Frankly I am not sure if that's related to your problem, but I just wanted to say that you must call Release before screen gets updated, in your case that's before Wait(1), otherwise your changes to surface won't be shown in time. (In practice this depends on graphics driver game is run with, for software driver calling Release is not essential, but Direct3D requires it to be called)
Title: Re: Transition from a Background to another
Post by: Khris on Tue 07/11/2017 22:41:07
I tried your code as-is, and it worked perfectly fine for me.
Where .Release() is called doesn't matter apparently, and the background would go blurry and clear multiple times just fine, whenever I pressed the respective button.

When you say "works much better", are you referring to the fact that it's faster? Or does it suddenly work with subsequent keypresses? Please try to be precise, we really want to help, but not stating clearly and exactly what is or isn't happening makes it quite tedious to help you.
Title: Re: Transition from a Background to another
Post by: henrikes on Wed 08/11/2017 22:11:35
A video trying to show what i mean

Video test (https://youtu.be/c7d-e59Lp2M)

You can see what i mean with after i do the transition it takes a long time before i can do it again....

Now a second video with the "i = i-5;", basically its faster

Video test 2 (https://youtu.be/PcXJBNNy-i0)
Title: Re: Transition from a Background to another
Post by: Snarky on Wed 08/11/2017 22:34:20
From these videos, I fail to see the problem. It seems to accept keypresses as soon as the transition is over. It's blocking while the transition is ongoing, but that's the way you've coded it, by putting the Wait(1) call inside the loop.

If you want it to be able to reverse the transition while it's still ongoing (or do other things while the transition is happening), you need to make it non-blocking, which basically means that instead of having the code in a loop, you put it in repeatedly_execute(), without the Wait().
Title: Re: Transition from a Background to another
Post by: Khris on Wed 08/11/2017 22:41:00
I have noticed that the image doesn't seem to change anymore during the last 30 or so frames. The reason is that the previously drawn semi-transparent pixels all add up to the actual color, thus the visual transition is complete long before i reaches 0.

Looks like this has been the issue the entire time? You thought the transition is finished but the game was still blocked and therefore didn't react to your keypress?

A quick fix is to increase the speed, a proper fix is to restore the previous background before drawing the next transparency step. That way the fade is perfectly linear from 100 to 0.
Title: Re: Transition from a Background to another
Post by: henrikes on Thu 09/11/2017 19:42:47
Quote from: Khris on Wed 08/11/2017 22:41:00
Looks like this has been the issue the entire time? You thought the transition is finished but the game was still blocked and therefore didn't react to your keypress?

Yes it was :)

Quote from: Khris on Wed 08/11/2017 22:41:00
A quick fix is to increase the speed, a proper fix is to restore the previous background before drawing the next transparency step. That way the fade is perfectly linear from 100 to 0.

Just tried the "proper fix" (I think) and it looks way better video (https://youtu.be/epuJXXvCaoc).

Thank you all for the help :)
Sorry for not beeing clear at start :)