Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Miori on Sun 16/02/2014 12:36:29

Title: Problem with Lights
Post by: Miori on Sun 16/02/2014 12:36:29
Well, I have a problem with lights. Sometimes it should be night in my game, so the screen gerts dark. So I want my character to have a light circle around itself. Thats not the problem. I could take a big dark pic with a hole at characterposition following it. BUT ... even in the night there are other static light sources like lamps or candles.

So there should be static lights in the background, that can't move + one light following the players position and I have no clue how to do something like this :shocked: Could anyone help me please ???
Title: Re: Problem with Lights
Post by: Slasher on Sun 16/02/2014 13:21:01
You could use a flashlight effect.

Checkout Modules and Plugins.

Latest version:

http://www.adventuregamestudio.co.uk/forums/index.php?topic=30137.0

Older version:
http://www.adventuregamestudio.co.uk/forums/index.php?topic=22732.msg175072#msg175072


Title: Re: Problem with Lights
Post by: Khris on Sun 16/02/2014 14:05:51
If you don't need smooth edges, you could use a DynamicSprite and each loop clear it to black, then draw magenta circles to it. Then set the resulting image to a non-clickable GUI.
Title: Re: Problem with Lights
Post by: Miori on Mon 17/02/2014 13:36:03
Flashlight module is nice :). Tried it and it works finde, but... its only one light. :(

@Khris
Mh, smooth edges would be fine, but I'm not even able to try as you say XD. Could you show me how to do so, please? :)
Title: Re: Problem with Lights
Post by: Khris on Mon 17/02/2014 14:01:03
First you'll need some way of storing the positions and radii of the light sources in each room.
A very basic way would be this:
Code (ags) Select
// global header
import int ls_count;
import void SetLightSource(int i, int x, int y, int r);

// global script
int ls_x[20], ls_y[20], ly_r[20];
int ls_count;
export ls_count;

void SetLightSource(int i, int x, int y, int r) {
  ls_x[i] = x; ls_y[i] = y; ls_r[i] = r;
}


In your room's before fadein event, you can now do this:
Code (ags) Select
  ls_count = 3;
  SetLightSource(0, 130, 67, 25);  // index, room coordinates, radius
  ...


Now you need to update the dynamic sprite:
Code (ags) Select
// global script
DynamicSprite *darkness;
void DrawDarkness() {
  if (darkness == null) darkness = DynamicSprite.Create(System.ViewportWidth, System.ViewportHeight);
  DrawingSurface *ds = darkness.GetDrawingSurface();
  ds.Clear(0); // black
  ds.DrawingColor = Game.GetColorFromRGB(255, 0, 255); // magic pink
  int xo = GetViewportX();
  int yo = GetViewportY();
  int i = 0;
  while (i < ls_count) {
    ds.DrawCircle(ls_x[i] - xo, ls_y[i] - yo, ls_r[i]);
    i++;
  }
  // player
  ds.DrawCircle(player.x - xo, player.y - yo - 20, 20); // adjust y and r
  ds.Release();
  gDarkness.BackgroundGraphic = darkness.Graphic;
}

// in repeatedly_execute_always
  if (it_is_night) DrawDarkness();
  gDarkness.Visible = it_is_night;


This code requires a screen-sized GUI called gDarkness and a bool called it_is_night.
Title: Re: Problem with Lights
Post by: Miori on Tue 18/02/2014 14:26:12
:~( Well, even if I understand how it works, i could never write such a script by myself, I guess there is still much to learn.  :-D It works fine, it is exactly what I was looking for. Thank you so much ;-D 

But I have still some questions. First... I had to downgrade my game to 16-bit to make the magic pink work. But some other graphics, that had smooth transparency effects doesn't look so well anymore. Is it possible to change the bit-rate only for the darkness/light effect and leave 32-bit for the others somehow?

Could we draw another bigger (or smaller if its easier) circle with lower transparency on the position of the light spot, so the contrast between light and dark is not that hard? For an effect like this:

(http://www.bilder-hochladen.net/files/hlba-96-caf1.png)
Title: Re: Problem with Lights
Post by: Khris on Tue 18/02/2014 17:59:45
That's the thing; while AGS can draw semi-transparent stuff, it can only do so if the surface it draws to already has pixels on it. If you draw semi-transparent stuff to an empty/transparent sprite, the result looks like you had drawn on top of magic pink.

There is a plugin called AGSBlend that addresses this, and I believe AGS 3.3.0 has also support for this now. I haven't really used that yet though.
Another problem is that one has to essentially draw holes into a black background. This is easier said than done though, because afaik one can't draw a semi-transparent circle with DrawCircle, one has to use a sprite. But drawing a semi-transparent sprite will not overwrite the pixels below it. One would have to draw sprites like from your post on an empty surface, then fill the area in between with black. But what if two circles overlapped...? etc. etc. ;-D
I'll try a few things and get back to you.
Title: Re: Problem with Lights
Post by: Snarky on Tue 18/02/2014 18:12:21
Quote from: Khris on Tue 18/02/2014 17:59:45
That's the thing; while AGS can draw semi-transparent stuff, it can only do so if the surface it draws to already has pixels on it. If you draw semi-transparent stuff to an empty/transparent sprite, the result looks like you had drawn on top of magic pink.

Isn't that what the latest version fixes, wrt transparency?
Title: Re: Problem with Lights
Post by: Khris on Tue 18/02/2014 18:26:26
Yeah, I did mention this and I'm trying a few things in 3.3.0 right now.

Miori: for now, if you replace Game.GetColorFromRGB(...) with COLOR_TRANSPARENT, you should be able to switch back to 32-bit.


Edit:
[IMGZOOM]http://i.imgur.com/cFXLssl.png[/IMGZOOM]

I put it in module form for now, so remove all the other code and add this: click (https://www.dropbox.com/s/ytuafvh45s1yuvk/Darkness_0.1_AGS_330.scm) (requires AGS 3.3.0!)

Use it like this:
Code (ags) Select
function room_Load()
{
  Darkness.SetDarkness(80); // 0: off, 100: full black
  Darkness.SetCount(2);
  Darkness.SetLightSource(0, 93, 82, 80, 4);  // index, x, y, radius, flickering
  Darkness.SetLightSource(1, 233, 81, 100, 4);
  Darkness.SetPlayerLight(true, 0, -40, 40);  // on/off, x offset, y offset, radius
}
Title: Re: Problem with Lights
Post by: Miori on Wed 19/02/2014 12:17:38
Mh... it looks really well ;-D... on the screenshot :-D. I upgraded to AGS 3.3.0 and added your module. For testing I just copied your code and set the "it_is_night"-variable as true. But nothing happens ??? (except lags, but i guess it depends on my lame notebook, where almost every game is lagging XD). Is there something else I have to do :o?
Title: Re: Problem with Lights
Post by: Khris on Wed 19/02/2014 12:31:33
I've removed a few redundant things: https://www.dropbox.com/s/ytuafvh45s1yuvk/Darkness_0.1_AGS_330.scm

You don't need is_it_night any longer; as long as the darkness is set to something other than 0, it should show up.
Is your game changed back to 32bit?
Did you remove all the previous code? If you didn't, it will interfere.
Title: Re: Problem with Lights
Post by: Miori on Wed 19/02/2014 12:56:36
I use a backup I made before implementing your codes, so there is none of them left and it is still 32bit. But it still does nothing :o The call functions are placed in room_load(), but I guess it doesn't matter where to place them right? Could also call it from elsewhere? (like turning a switch off or something). There are no errors, after all. It's just not getting dark :o

Is there anything I have to consider with the gDarkness GUI? It's Screensized (640x480) and normally off, with no background or border and not clickable of course :).

edit:

Aaaah, found the problem. I had to put sprite alpha rendering style to the new option too :D Now it works. And uh... the static lights are ... ah... missing english vocabulary, sry. I hope you know what I mean. It just looks awesome now :D

But there is still a small problem. I had an similiar bug when I was testing the flashlight script. The Light thats following the player is running away from him D: Maybe its caused by the smooth scrolling script I use:

Click here (http://www.adventuregamestudio.co.uk/forums/index.php?topic=33142.msg636472912#msg636472912)

I solved it in the flashlight script somehow... but I can't remember how :- XD Well I have to leave for now T_T I hate that... when i'm in the middle of bug-research XD
Title: Re: Problem with Lights
Post by: Khris on Wed 19/02/2014 22:49:38
I forgot to subtract the viewport for the player's light. I have updated the module but it's a very quick fix: in lines 70 and 71, change "player.x" to "(player.x - xo)" and "player.y" to "(player.y - yo)".

Btw, if you want a light to not flicker, pass 0 as the last parameter. It's basically the magnitude of the random size change.
Title: Re: Problem with Lights
Post by: Miori on Thu 20/02/2014 11:39:01
Ah thanks, now everything seem to work. Its so cool, and I love the flicker-effect (laugh) But of course its good that I can turn it off for electric lights and stuff, that should not flicker XD Thank you very much for that awesome module and all the work behind it :D You're a genius. :-*
Title: Re: Problem with Lights
Post by: Khris on Thu 20/02/2014 12:56:04
Gern geschehen :)

Couldn't have happened without CW and the other awesome people who fixed alpha blending though. This module was written in ten minutes and it's really simple, but I'm glad everything works :)
How's the lagging? Because one could add a few more layers of semi-black to make the transition much smoother still.
Title: Re: Problem with Lights
Post by: Miori on Fri 21/02/2014 11:25:24
:-D

One more layer for smoother light would be fine, but it is already laggy with this one. My computer is a bit lame and almost every programm and game is lagging here. So it's hard to say :-\ I will test it on the pc of my roomy later ^^.
Title: Re: Problem with Lights
Post by: Miori on Sat 22/02/2014 11:56:22
Seems not to be laggy :D We could try a second semi-black layer :D

And yes you are right. The Team that made AGS 3.3 did very well with the new version ^_^

Edit:

There seem to be a little problem with the script :o I put the call scripts into a room, just for testing. Everything worked fine. But then I deleted the call scripts. I didn't change anything else. Now the room is always dark :O I put "gDarkness.Visible = false;" in the executive_always section. At least I see something again XD But why does it stay black even after closing the game D:
Title: Re: Problem with Lights
Post by: Khris on Sat 22/02/2014 12:28:15
You need to use Darkness.SetDarkness(0); in room_Load to turn it off.

I'll make a few tests regarding more layers. :)

Done:
[IMGZOOM]http://i.imgur.com/AEl1Ug4.png[/IMGZOOM]
Darkness v0.3 (https://www.dropbox.com/s/ntqcab04vgacm4k/Darkness_0.3_AGS_330.scm?dl=1)

Added
Code (ags) Select
  Darkness.SetNumberOfLayers(5); // 0 - 20
It's the number of inbetweens, so a setting of 0 means no gradient.
Title: Re: Problem with Lights
Post by: Miori on Sun 23/02/2014 11:42:21
Ah it looks much greater than before. I also tried the 20 layers limit. That looks really smooth. But I had to wait over a minute for my game to start, and the lags where awfull. I guess for my pc 2 layers are the accepable lag limit XD But well... not everyone has that lame pcs, so I guess I will put it into my option menu, so players can choose the smoothness by themself ^^ Thank you again :D

Besides... is it lagging on your pc too?
Title: Re: Problem with Lights
Post by: Khris on Sun 23/02/2014 12:11:58
You had to wait a minute for the game to start...? I can understand why it would lag, but that shouldn't happen.
Not lagging for me no :) I'm getting a straight 40FPS even with 20 layers :-D
Title: Re: Problem with Lights
Post by: Miori on Sun 23/02/2014 12:47:13
You lucky one ;-D. Now i'm getting jealous ^^. Well it didn't take a minute to start the game exactly. After I press new game-button there is a scene I normally skip with esc, and that skipping took over a minute. ^^ It already takes longer, with only one layer XD And it's already lagging then, but with 20 it was really something I wouldn't do to the player XD Like I said, I'll let the player self decide how smoothy/laggy he wants to play. It's just a bit strange feeling. When darkness is disabled it's fluidly again and everything is running so fast suddenly XD

But now I'm really overthinking if I should really implement a day/night system. One more lag-making thing running in the background :/ I also will have weather running from time to time (snow). I could not spare the snow, but the day/night system. Maybe it could change by specific events... when the player goes to bed or something... Sry, ich denk schon wieder schriftlich XD
Title: Re: Problem with Lights
Post by: Ryan Timothy B on Sun 23/02/2014 14:15:12
Quote from: Miori on Sun 23/02/2014 12:47:13
After I press new game-button there is a scene I normally skip with esc, and that skipping took over a minute.
I haven't looked at this module, but you'll have to take advantage of
Code (ags) Select
if (!Game.SkippingCutscene)
to draw the lighting effects to get around this delay, or anything else that Miori does within this skippable cutscene (like walking, etc).

Miori, when the cutscene is skipped it actually runs the lighting effects every single game loop that the game is trying to skip for the duration of this "skipped" cutscene. Skipping a cutscene doesn't actually skip over everything, it actually just excludes the wait within each game loop and processes the same duration of game loops as you would need if you hadn't skipped the cutscene.

Edit: Miori, you'll need to make your own TL:DR style of programming where you constantly check if the cutscene is currently being skipped, then just have it ignore any commands that are exclusive to this cutscene like the player walking back and forth, and then at the end of the cutscene summarize what happens within a  if (Game.SkippingCutscene)  condition.
Title: Re: Problem with Lights
Post by: Miori on Sun 23/02/2014 15:03:16
It's not the big problem, because I don't want the player to skip my cutscenes. Its just for testing, so I don't have to see it every time I start XD But if it runs every game loop and every single one of it is lagging I understand that it takes so long. It is a scene where a text is written letter by letter and a short walking after it, so every letter is an extra lag ^_^

What is TL:DR style ???
Title: Re: Problem with Lights
Post by: Ryan Timothy B on Sun 23/02/2014 16:06:21
I meant it as:
   Too lazy, didn't read  --> Too lazy, didn't watch cutscene

As for non skippable cutscenes... :-X