Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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  
  Slick2D alpha map coloured lights  (Read 197 times)
0 Members and 1 Guest are viewing this topic.
Offline benberi

Senior Newbie





« Posted 2014-07-07 01:10:43 »

I am using the Slick2D engine.

I've followed this snippet for creating light sources. I have followed step step, added what is actually important for me, and tested it and the lighting effects worked.

But when I tried to add different colors, it acted the same as if I add 255, 255, 255

I have the Surface class, which implements WorldLayer, Lightable. This class contains the LightContainer class, which should serve the Lightable WorldLayer.

This is my Surface class:


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
public class Surface implements WorldLayer, Lightable {

    private TileMap map = new TileMap(50, 50);
    private LightContainer lights = new LightContainer(new Sprite("light.png"), map);
    private DayLight dayLight = DayLight.AFTER_NOON;

    public Surface() {
        for (int x = 0; x < this.map.getWidth(); x++) {
            for (int y = 0; y < this.map.getHeight(); y++) {
                map.setTile(x, y, new Sprite("water.png"));
            }
        }
    }

    @Override
    public void update(int delta) {
        // TODO Auto-generated method stub

    }

    private int x = 0, y = 0;
    private long t;

    @Override
    public void render(GameContainer c, Graphics g) {
        this.map.setAlpha(this.dayLight.getLight());
        this.map.render(g);
        this.lights.render(c, g);
    }

    @Override
    public TileMap getMap() {
        return this.map;
    }

    @Override
    public LightContainer getLightContainer() {
        return this.lights;
    }
}



And my LightContainer class:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
public class LightContainer {

    private List<Light> lights = new ArrayList<Light>();
    private Image lightSurface;
    private Color base = new Color(1f, 1f, 1f, 1f);
    private TileMap map;

    private Random random = new Random();

    public LightContainer(Sprite lightSource, TileMap map) {
        this.lightSurface = lightSource.getImage();
        this.map = map;
    }

    /**
     * Adds a new light source with a custom scale
     * @param x         X-asis position
     * @param y         Y-asis position
     * @param color     Color of the light
     * @param alpha     Light opacity
     * @param scale     Light scale
     */

    public void add(float x, float y, Color color, float alpha, float scale) {
        lights.add(new Light(x, y, alpha, scale, color));
    }

    /**
     * Adds a new light source with default scale 1.0f
     * @param x         X-asis position
     * @param y         Y-asis position
     * @param color     Color of the light
     * @param alpha     Light opacity
     */

    public void add(float x, float y, Color color, float alpha) {
        lights.add(new Light(x, y, alpha, color));
    }

    /**
     * Generates a new light source with a random scale
     * @param x         X-asis position
     * @param y         Y-asis position
     */

    public void generate(float x, float y) {
        this.generate(x, y, this.random.nextFloat());
    }

    /**
     * Generates a new light source with given coordinates and scale
     * @param x         X-asis position
     * @param y         Y-asis position
     * @param scale     Light scale
     */

    public void generate(float x, float y, float scale) {
        float alpha = 1.0f;
        Color color = new Color(11, 213, 105);
        this.add(x, y, color, alpha, scale);
    }

    /**
     * Removes light index
     * @param index The index of the light
     */

    public void remove(int index) {
        this.lights.remove(index);
    }

    /**
     * Gets light by index
     * @param index Index of the light
     * @return Light object
     */

    public Light get(int index) {
        return this.lights.get(index);
    }

    /**
     * Gets the amount of lights
     * @return Amount of lights
     */

    public int getLength() {
        return this.lights.size();
    }

    /**
     * Gets the width of light source
     * @return Width
     */

    public int getWidth() {
        return this.lightSurface.getWidth();
    }

    /**
     * Gets the height of the light source
     * @return Height
     */

    public int getHeight() {
        return this.lightSurface.getHeight();
    }

    /**
     * Sets temp color base
     * @param f
     */

    public void setBaseAlpha(float f) {
        this.base.a = f;
    }

    /**
     * Gets the light source image
     * @return
     */

    public Image getLightSurface() {
        return this.lightSurface;
    }

    /**
     * Sets a new map
     * @param map   TileMap
     */

    public void setMap(TileMap map) {
        this.map = map;
    }

    /**
     * Renders the lights
     * @param c GameContainer
     * @param g Graphics
     */

    public void render(GameContainer c, Graphics g) {
        for (int i = 0; i < this.lights.size(); i++) {
            Light light = this.lights.get(i);

            g.setDrawMode(Graphics.MODE_ALPHA_MAP);
            g.clearAlphaMap();

            int alphaW = (int) (this.getWidth() * light.getScale());
            int alphaH = (int) (this.getHeight() * light.getScale());
            int alphaX = (int) (light.getX() - alphaW / 2f);
            int alphaY = (int) (light.getX() - alphaH / 2f);

            this.base.a = light.getAlpha();

            this.lightSurface.draw(alphaX, alphaY, alphaW, alphaW, this.base);

            g.setDrawMode(Graphics.MODE_ALPHA_BLEND);
            g.setClip(0, 0, c.getWidth(), c.getHeight());

            light.getColor().bind();
            System.out.println(light.getColor().r);
            this.map.render(g);
            g.clearClip();
        }

        g.setDrawMode(Graphics.MODE_NORMAL);
    }
}



The actual light source drawing happens in LightContainer, at the bottom. If you looked at the Surface class, I first draw the original map, and set the day light amount (this is the alpha value of the tiles in the map).

And then I draw the light sources for this layer using the LightContainer's instance.

As a test, in the main class I called LightContainer#generate(float, float, float) to test if the generating light works, but it seems like it won't render colors of the lights!

1  
2  
3  
    
    this.earth = new Surface();
    this.earth.getLightContainer().generate(150, 150, 1.0f);


Outcome:



As you see, the light is simply white.

What is causing it to look so white? How can I make this support colors?
Offline Hermasetas

Senior Member


Medals: 6
Projects: 2
Exp: 3 years


I do gamez, yes!


« Reply #1 - Posted 2014-07-07 10:23:06 »

When working with float colors the range is from 0f to 1f.
So (0f, 0f, 0f, 1f) is black and (1f, 1f, 1f, 1f) is white.
When you put in (150, 150, 150, 1f) it gets rounded to (1f, 1f, 1f, 1f) Smiley

Try with (0.5f, 0.5f, 0.5f, 1f)  Pointing
Offline benberi

Senior Newbie





« Reply #2 - Posted 2014-07-07 16:32:37 »

I've tried, it doesn't work.
Probably because I dont use spriteSheet.startUse, so color.bind() won't affect the image.
So is there another way of doing this?
Pages: [1]
  ignore  |  Print  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

pw (22 views)
2014-07-24 01:59:36

Riven (20 views)
2014-07-23 21:16:32

Riven (17 views)
2014-07-23 21:07:15

Riven (20 views)
2014-07-23 20:56:16

ctomni231 (48 views)
2014-07-18 06:55:21

Zero Volt (44 views)
2014-07-17 23:47:54

danieldean (35 views)
2014-07-17 23:41:23

MustardPeter (38 views)
2014-07-16 23:30:00

Cero (53 views)
2014-07-16 00:42:17

Riven (52 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!