Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (525)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (594)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Looking for a good way to apply a night effect over a game tile?  (Read 1506 times)
0 Members and 1 Guest are viewing this topic.
Offline Jeremy
« Posted 2013-08-06 12:01:49 »

I want to implement night\day cycles, I was wondering if anyone knows of an optimal way to do this through Java2D. Ideally, I'd need to overlay from dark-blue to white over individual tiles (each image for a tile is stored as an Image).

I haven't been able to think of any good ways of doing this. Thanks.

JevaEngine, Latest Playthrough (This demo is networked with a centralized server model)

http://www.youtube.com/watch?v=rWA8bajpVXg
Online nerb
« Reply #1 - Posted 2013-08-06 12:26:08 »

If the ambient light is evenly applied over all tiles, could you just create a BufferedImage the size of your screen, set it to the desired alpha & colour, and draw it after you draw your tiles???
Offline kpars

JGO Kernel


Medals: 107
Projects: 4
Exp: 4 years


Extreme Typist.


« Reply #2 - Posted 2013-08-06 13:03:02 »

I had to do something VERY similar just yesterday!

This should give you an idea. I'm writing this at work so don't expect this to go perfectly.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
int alpha = 0;

public static void render(Graphics g) {
    for (int x = 0; x < GameClass.display.width; x += YourImage.getWidth()) {
        for (int y = 0; y < GameClass.display.height; y += YourImage.getHeight()) {
            g.drawImage(YourImage, x, y);
        }
    }

    g.setColor(new Color(0, 0, 0, alpha));
    g.fillRect(0, 0, GameClass.display.width, GameClass.display.height);

    alpha++;
}

This basically tiles an image and draws an alpha mask over it that gets darker as the game ticks.

For a day/night cycle you should just set the alpha to go up when it's night time and for it to go down when it's day time.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jeremy
« Reply #3 - Posted 2013-08-06 13:11:45 »

Actually, I can't believe I didn't think of something like that lol.

Okay, so I think I'll do something like that but on a per-tile basis, than I can scan a radius of the tile for light-sources and decay that light sources colour's alpha effect on the tile as it is further away.

Thanks for the responses.

JevaEngine, Latest Playthrough (This demo is networked with a centralized server model)

http://www.youtube.com/watch?v=rWA8bajpVXg
Offline Jeremy
« Reply #4 - Posted 2013-08-06 18:45:47 »

That didn't work (for the reason I feared it would not.) The drawpolygon routine in Java2D's Graphics2D object is way too slow. I know that the overhead is caused by that routine because if I leave my algorithm in and comment out the call to draw polygon - the CPU usage drops dramatically.

The resulting effect is some-what desirable though (I could hack and tweek off of this - but it would need to perform much quicker...):

*edit*
I'm going to try and layer ovals - this would produce a nicer effect I think - and it would definitely reduce overhead.


JevaEngine, Latest Playthrough (This demo is networked with a centralized server model)

http://www.youtube.com/watch?v=rWA8bajpVXg
Offline kpars

JGO Kernel


Medals: 107
Projects: 4
Exp: 4 years


Extreme Typist.


« Reply #5 - Posted 2013-08-06 19:23:58 »

Well darn.

How does it work when you aren't doing it per-tile?

Offline Jeremy
« Reply #6 - Posted 2013-08-06 19:27:04 »

Well darn.

How does it work when you aren't doing it per-tile?

I haven't tried - but I imagine that if I overlayed the entire map it would work fine (and to intend on doing that for ambient light sources)

For point light-sources I think I am going to try layering ovals of increasing radius at the particular point after rendering the tiles - we'll see how that works out - it should be much quicker and I think it'd be smoother but I'm not too sure. I'll have to see.

JevaEngine, Latest Playthrough (This demo is networked with a centralized server model)

http://www.youtube.com/watch?v=rWA8bajpVXg
Offline jonjava
« Reply #7 - Posted 2013-08-06 19:38:13 »

What if you draw a sprite like this directly on top with alpha?


Offline Jeremy
« Reply #8 - Posted 2013-08-06 19:58:08 »

What if you draw a sprite like this directly on top with alpha?



That's a good idea, and it would probably work for baking in a lighting effect - but if I want dynamic lighting - like a moving light source - I would need to find some way to optimally generate the light maps on the fly.

What I was thinking is to have a low-priority background thread do it and present a new light map at intervals of half a second or so - that would probably work without much a of a performance hit, I'll have to toy around a bit with it.

JevaEngine, Latest Playthrough (This demo is networked with a centralized server model)

http://www.youtube.com/watch?v=rWA8bajpVXg
Online wessles

JGO Wizard


Medals: 74
Projects: 4
Exp: 4 years


Radirius Software


« Reply #9 - Posted 2013-08-06 20:04:36 »

Oh, I just use a recursion:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public void lightSource(int x, int y, float brightness) {
      if (tiles[x][y].getBrightness() >= brightness && brightness < 1)
         return;
      tiles[x][y].setBrightness(brightness);
      brightness -= .2f;

      if (x != 0)
         lightSource(x - 1, y, brightness);
      if (x != getGenTilemap().getWidthInTiles() - 1)
         lightSource(x + 1, y, brightness);
      if (y != 0)
         lightSource(x, y - 1, brightness);
      if (y != getGenTilemap().getHeightInTiles() - 1)
         lightSource(x, y + 1, brightness);
   }


I cant remember who to credit, but I got this on these forums.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline kpars

JGO Kernel


Medals: 107
Projects: 4
Exp: 4 years


Extreme Typist.


« Reply #10 - Posted 2013-08-06 20:13:07 »

He knows how to set the brightness. It's drawing it and getting it to render smoothly that he's having a problem with.

Offline Jeremy
« Reply #11 - Posted 2013-08-07 12:24:00 »

I managed to get a solid frame-rate with dynamic rendering using a series of Java2D composition hacks - there are a few bugs I need to work out (and I need to turn this hacky implementation into a proper implementation) but it works well, it looks as I expected it would and it performs relatively quickly:


JevaEngine, Latest Playthrough (This demo is networked with a centralized server model)

http://www.youtube.com/watch?v=rWA8bajpVXg
Offline kpars

JGO Kernel


Medals: 107
Projects: 4
Exp: 4 years


Extreme Typist.


« Reply #12 - Posted 2013-08-07 12:51:04 »

If you are truly wanting a day/night cycle, shouldn't you be setting the background color/image as well?  Tongue

Offline Jeremy
« Reply #13 - Posted 2013-08-07 13:02:18 »

If you are truly wanting a day/night cycle, shouldn't you be setting the background color/image as well?  Tongue

*EDIT*

I'm an idiot, I just figured out you were talking about the black border. Yeah - well normally that wouldn't be visible (I am working with small maps because they're easier to debug etc & my map editor isn't exactly the most stable thing in the world.

The way I do it for anyone interested:
What happens is the ambient colour is layered over the screen (day\night shade) - the point-light sources have an area they're going to apply their lighting effect too as well and I can't cover those areas (otherwise I lose significant quality with my lighting.) So I use two Area objects, one covers the entire area of my map, the other I add my light source's effect area to - then I subtract my light source area from my world map area and draw the ambient light using the world map area.

Then I fill in the spaces I didn't apply the overlay to with the lighting effects by calling the respective pointlight's rendering routine (which is passed an ambient colour to lerp with).

This system allows me to implement all sorts of lights too (like, i.e directional lights)

JevaEngine, Latest Playthrough (This demo is networked with a centralized server model)

http://www.youtube.com/watch?v=rWA8bajpVXg
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

toopeicgaming1999 (69 views)
2014-11-26 15:22:04

toopeicgaming1999 (58 views)
2014-11-26 15:20:36

toopeicgaming1999 (13 views)
2014-11-26 15:20:08

SHC (26 views)
2014-11-25 12:00:59

SHC (24 views)
2014-11-25 11:53:45

Norakomi (30 views)
2014-11-25 11:26:43

Gibbo3771 (24 views)
2014-11-24 19:59:16

trollwarrior1 (37 views)
2014-11-22 12:13:56

xFryIx (76 views)
2014-11-13 12:34:49

digdugdiggy (54 views)
2014-11-12 21:11:50
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!