Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

My team and I have been working on a game in Java using the Slick2D engine and there are a few things I have questions on. As of right now, the code that I've written basically takes a cosine function and gets a value based on the current game time (which is normalized by some multiple of Pi, like 4*Pi, to give us a period (or day length) of 4*Pi). The value that comes out of the function is used directly as the alpha level of the overlaying light layer so that as the night comes on, the screen darkens until it's completely opaque, then eventually comes back to completely transparent. I've also created a mask around the player, which disallows the alpha level from drawing near the player.

Now, this looks cool and all, but if I want to draw different alpha levels around that, some that may even be non-existent until it begins to get dark like an average alpha transition that goes from the 100% transparent alpha near the player to the 100% opaque alpha outside that mask during the pitch black of night, I run into issues. Screenshot

Basically, Slick provides some tools, like static values in the Graphics class that change the draw mode. Examples are: MODE_ADD, MODE_ADD_ALPHA, etc. I'm wondering if there's a particularly good rundown on using these to get results where I have alpha levels that will vary between the current overall alpha level and the 100% transparency so I can create clean transitions so it doesn't have that clean cutoff seen in the screenshot.

Or should I scratch this and go with a grid/cell based system since the game's tile based when it comes to drawing the background layers, save for the entities. Might make a bit more sense and look a little more natural with the backgrounds, although still look a tad unnatural.

EDIT: I'd also be open to other paradigms.

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

Use MODE_COLOR_MULTIPLY, but without alpha. You are going to end by drawing an image (RGB, but no alpha) over the screen using multiply.

Construct this image (NOT using multiply mode, but normal blending) by drawing pure white during the day, pure black during the night, and a gray color during twilight, as you have done. Then, around the player's location, draw a smooth falloff image using additive blending mode. This falloff image does not have an alpha channel, and is black around the edges and white in the middle. Here is the important part: the image you are drawing to does not have an alpha channel. This is because you are encoding the light level into the actual image, not the alpha channel. As a bonus, you can support colored lights. By using additive blending, you could add lights for other characters, or the environment (but be careful of making any of them too bright).

Once this image is prepared, draw it over the screen using MULTIPLY. This works because light is fundamentally object_color * incoming_light. Basically, what is already on the screen is object_color, and you are calculating an image which represents the incoming_light based on the time of day and where the character is. Even though your game is pixel-art based and 2D, does not mean it can't have cool lighting!

(Hint: you can make it colored, redish-orange at sunset, blue-purple at dusk/night, yellow at sunrise, pale-white during a full moon, and light blue in the morning for a really cool effect. Play with the colors to fit your art style. Just vary the color of the rectangle you draw over the scene; there is no reason it must be white, gray, or black.)

(Hint #2: When drawing the player's light, disable it completely during the day, then have it fade in quickly sometime during the evening, and tint it orange to simulate a lantern (it would make sense to have the character hold one during this time).)

share|improve this answer
    
I'm going to accept this answer because it's scalable. Since I asked this question, I've learned more about OpenGL and the LibGDX libraries (Dropped Slick2D due to it "dying") and this answer is applicable for any system that extends OpenGL. Thanks! –  Radai Jan 29 at 6:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.