Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
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 Radial Gradient  (Read 1275 times)
0 Members and 1 Guest are viewing this topic.
Offline bcsharp

Senior Newbie


Exp: 6 years



« Posted 2013-06-28 23:19:53 »

Hello,
With apologies, I think that I originally posted this under the wrong section, so I moved my question here. I am having difficulty creating a lighting system for my game. The way I plan to render lighting is like this:
  • Create a lighting map by using additive blending and radial gradients from all of the lightsources
  • Combine the lightmap with the rendered game using multiplicative blending
The issue with my code so far is creating a radial gradient class. I am using Slick2D, which doesn't include a radial gradient class that implements ShapeFill, the interface used to fill shapes. I decided to create one myself, which looks like this:
import org.newdawn.slick.*;
import org.newdawn.slick.geom.*;
import org.newdawn.slick.svg.*;


public class RadialFill implements ShapeFill {
   Color start;
   Color end;
   float centerX;
   float centerY;
   
   public RadialFill(Color s, Color e){
      start = s;
      end = e;
   }

   @Override
   public Color colorAt(Shape shape, float x, float y) {
      float e = (float) (Math.sqrt((x - centerX) * (x - centerX) + (y - centerY) * (y - centerY))) / ((shape.getHeight() > shape.getWidth() ? shape.getHeight() : shape.getWidth()) / 2 );
      float s = 1 - e;
      Color col = new Color(1,1,1,1);
      col.r = (e * end.r) + (s * start.r);
      col.b = (e * end.b) + (s * start.b);
      col.g = (e * end.g) + (s * start.g);
      col.a = (e * end.a) + (s * start.a);
      return col;
   }
   
   public void setCenter(float x, float y){
      centerX = x;
      centerY = y;
   }
   
   public float getCenterX(){
      return centerX;
   }
   
   public float getCenterY(){
      return centerY;
   }
   
   public void setStartColor(Color s){
      start = s;
   }
   
   public Color getStartColor(){
      return start;
   }
   
   public void setEndColor(Color e){
      end = e;
   }
   
   public Color getEndColor(){
      return end;
   }
   
   @Override
   public Vector2f getOffsetAt(Shape shape, float x, float y) {
      // TODO Auto-generated method stub
      return new Vector2f(0,0);
   }

}

Here is the test code that I made:
import org.newdawn.slick.*;
import org.newdawn.slick.fills.*;
import org.newdawn.slick.geom.*;
import org.newdawn.slick.state.*;
import org.newdawn.slick.svg.*;


public class LightTestState extends BasicGameState {
   
   Input input;
   GradientFill grad;
   RadialFill gradFill;
   Ellipse mouseLight;
   int mouseX;
   int mouseY;
   boolean ready = false;

   @Override
   public void init(GameContainer container, StateBasedGame game)
         throws SlickException {
      input = container.getInput();
      grad = new GradientFill(0, 0, Color.yellow, 100, 100, Color.black, false);
      gradFill = new RadialFill(Color.yellow, Color.black);
      ready = true;
   }

   @Override
   public void render(GameContainer container, StateBasedGame game, Graphics g)
         throws SlickException {
      if (ready){
         g.setAntiAlias(true);
         g.setDrawMode(Graphics.MODE_ADD);
         mouseLight = new Ellipse(mouseX, mouseY, 100, 100);
         gradFill.setCenter(mouseX, mouseY);
         g.fill(mouseLight, grad);
      }
   }

   @Override
   public void update(GameContainer container, StateBasedGame game, int delta)
         throws SlickException {
      mouseX = input.getMouseX();
      mouseY = input.getMouseY();
   }

   @Override
   public int getID() {
      // TODO Auto-generated method stub
      return 1;
   }

}

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.tests.*;

public class Noteblock extends StateBasedGame {
   
   public Noteblock(){
      super("Noteblock");
   }

   @Override
   public void initStatesList(GameContainer container) throws SlickException {
      // TODO Auto-generated method stub
      container.setShowFPS(false);
      addState(new LightTestState());
   }

   /**
    * @param args
    */
   public static void main(String[] args) {
      //GradientTest.main(null);
      try {
         AppGameContainer container = new AppGameContainer(new Noteblock());
         container.setDisplayMode(800,600,false);
         container.start();
      } catch (SlickException e) {
         e.printStackTrace();
      }
   }

}

However, when I try to create a radial gradient with this code, it doesn't work. At best, it can only make a fill of one color.  Huh Any ideas? Any help would be appreciated.

-bcsharp
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.

Longarmx (36 views)
2014-10-17 03:59:02

Norakomi (28 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (27 views)
2014-10-15 16:18:58

TehJavaDev (52 views)
2014-10-14 00:39:48

TehJavaDev (54 views)
2014-10-14 00:35:47

TehJavaDev (42 views)
2014-10-14 00:32:37

BurntPizza (63 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (77 views)
2014-10-11 22:30:10
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!