Fading day/night

Started by AnasAbdin, Wed 30/03/2016 10:03:53

Previous topic - Next topic

AnasAbdin

Hi all,

I have two backgrounds for a room, (0 = day, 1 = night )
I can change between them using SetBackgroundFrame.

However, I found this code extract in the manual:

Code: ags

DrawingSurface *mainBackground = Room.GetDrawingSurfaceForBackground(0);
DrawingSurface *nightBackground = Room.GetDrawingSurfaceForBackground(1);
mainBackground.DrawSurface(nightBackground, 50);
mainBackground.Release();
nightBackground.Release();


I am trying to make the day background fades into night. The manual says:

Quote from: AGS Manual
TIP: If you want to gradually fade in a second background, create a copy of the original surface and then restore it after each iteration, otherwise the backgrounds will converge too quickly.


I'm not sure what I'm doing wrong, it is working exactly as the manual warned: (otherwise the backgrounds will converge too quickly)

Code: ags
DrawingSurface *mainBackground = Room.GetDrawingSurfaceForBackground(0);
DrawingSurface *nightBackground = Room.GetDrawingSurfaceForBackground(1);
DrawingSurface *copyBG = mainBackground.CreateCopy(); //is this a correct way to create the copy of the original surface?

int i = 90;
while ( i > 0 )
  {
    mainBackground.DrawSurface(nightBackground, i);
    // I'm not sure how to use the copyBG here and how to restore it :/
    copyBG.DrawSurface(mainBackground, 0);
    i --;
    Wait(1);
  }

mainBackground.Release();
nightBackground.Release();
copyBG.Release();


Game stats:
AGS version 3.3.3
Res 640x400 32 bit
Direct 3D 9 hardware acceleration


Thanks in advance  :)

Edit:

I managed to get a gradient effect with this:

Code: ags

DrawingSurface *mainBackground = Room.GetDrawingSurfaceForBackground(0);
DrawingSurface *nightBackground = Room.GetDrawingSurfaceForBackground(1);

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

nightBackground.Release();
mainBackground.Release();

selmiak

make the night BG a roomsized object and use the tween module to tween its transparency in, blocking or nonblocking. easy peasy and even with different curves for the tween.

Snarky

That won't work so well if the background includes e.g. walkbehinds, selmiak.

What you're doing is pretty obviously wrong. What you should do is grab a copy of each background as a surface or dynamicsprite at the beginning (personally I'd do this as soon as the room loads, because then you know that they haven't been messed with), and then never write to them: these are your sources, and they should not be affected by what you do. Then you get a drawingsurface for the background, and on each loop iteration you draw both the original drawingsurfaces/sprites to it (one at 100% first, and then the other at whatever % you're at), and release.

I wrote something like this a while back. There's a version of the code in last year's AGS Awards client.

selmiak

#3
Quote from: Snarky on Wed 30/03/2016 19:34:36
That won't work so well if the background includes e.g. walkbehinds, selmiak.
Alright, this is true, I didn't think of that, I always(!) use PNG objects instead of walkbehinds as walkbehinds are just awful to add and don't have semitransparency for the borders. Done the tweening way it works pretty well, I did it this way in Eternal Chrysalis and by now noone called me out on any crappy transitions.


(!) No, not really always, walkbehinds are good for doors and sideway exits and have their sparse reason to exist but I use overlapping PNG object as often as possible!

Creamy

QuoteDone the tweening way it works pretty well
That's what I did for A Sloth For Both Seasons too. It may not be conventional, but it works if you update the baseline of the background object in "repeatedly execute".
 

SMF spam blocked by CleanTalk