Transition from a Background to another

Started by henrikes, Mon 06/11/2017 21:36:22

Previous topic - Next topic

henrikes

The code bellow is suposed to transition from a BackGround to another.... but its not working like expected

Code: ags

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

  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???

Khris

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?

henrikes

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..
..

dayowlron

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.
Pro is the opposite of Con                       Kids of today are so much different
This fact can clearly be seen,                  Don't you know?
If progress means to move forward         Just ask them where they are from
Then what does congress mean?             And they tell you where you can go.  --Nipsey Russell

Khris

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?

henrikes

#5
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 :(

Khris

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
Code: ags
  // 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)

henrikes

Tried both and the result is still the same.

increased the decrement of the i variable and works much better...
Code: ags
    
  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 :(

Crimson Wizard

#8
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)

Khris

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.

henrikes

#10
A video trying to show what i mean

Video test

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

Snarky

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().

Khris

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.

henrikes

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.

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

SMF spam blocked by CleanTalk