Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (568)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: davedes (posted 2012-04-30 15:48:09, viewed 420 times)

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   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229   230   231  
package slicktests;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

public class LightingTestAlpha extends BasicGame {

   //number of tiles in our simple horizontal sprite sheet
   public static final int TILE_COUNT = 5;
   
   //width/height of tile in pixels
   public static final int TILE_SIZE = 40;
   
   //size of alpha map (for use with sprite sheet)
   public static final int ALPHA_MAP_SIZE = 256;
   
   //space after tile, before next tile
   public static final int TILE_SPACING = 2;
   
   //the "sprite sheet" or texture atlas image
   private Image spriteSheet;
   
   //the sub-images of our sprite sheet
   private Image[] tileSprites;
   
   //our 2D map array
   private Image[][] tileMap;

   //map size in tiles
   private int mapWidth, mapHeight;
   
   //our alpha map image; just a feathered black circle on a transparent background
   private Image alphaMap;
   
   private Random random = new Random();
   
   //our lights
   private List<Light> lights = new ArrayList<Light>();
   
   //a timer used for simple light scaling effect
   private long elapsed;
   
   //a shared instance of Color so we don't need to create a new one each frame
   private Color sharedColor = new Color(1f, 1f, 1f, 1f);
   
   /** Describes a single point light. */
   public static class Light {
      /** The position of the light */
      float x, y;
      /** The RGB tint of the light, alpha is ignored */
      Color tint; 
      /** The alpha value of the light, default 1.0 (100%) */
      float alpha;
      /** The amount to scale the light (use 1.0 for default size). */
      private float scale;
      //original scale
      private float scaleOrig;
      
      public Light(float x, float y, float scale, Color tint) {
         this.x = x;
         this.y = y;
         this.scale = scaleOrig = scale;
         this.alpha = 1f;
         this.tint = tint;
      }
      
      public Light(float x, float y, float scale) {
         this(x, y, scale, Color.white);
      }
      
      public void update(float time) {
         //effect: scale the light slowly using a sin func
         scale = scaleOrig + 1f + .5f*(float)Math.sin(time);
      }
   }
   
   public LightingTestAlpha() {
      super("Alpha Map Lighting");
   }

   public void init(GameContainer container) throws SlickException {
//      container.setVSync(true);
      container.setTargetFrameRate(60);
      
      //To reduce texture binds, our alpha map and tilesheet will be in the same texture
      //Most games will implement their own SpriteSheet class, but for simplicity's sake:
         //map tiles are in a horizontal row starting at (0, 0)
         //alpha map is located below the tiles, at (0, TILE_SIZE+TILE_SPACING)
      spriteSheet = new Image("res/lighting/lighting_sprites.png", false, Image.FILTER_NEAREST);
      
      //grab the tiles
      tileSprites = new Image[TILE_COUNT];
      for (int i = 0; i < tileSprites.length; i++) {
         tileSprites[i] = spriteSheet.getSubImage(i * (TILE_SIZE + TILE_SPACING), 0, TILE_SIZE, TILE_SIZE);
      }
      
      //grab the alpha map
      alphaMap = spriteSheet.getSubImage(0, TILE_SIZE + TILE_SPACING, ALPHA_MAP_SIZE, ALPHA_MAP_SIZE);
      
      //randomize our map
      randomize(container);
      
      //reset the lighting
      resetLights(container);
   }
   
   Image randomTile() {
      int r = random.nextInt(100);
      if (r < 5) 
         return tileSprites[1 + random.nextInt(4) ];
      else
         return tileSprites[0];
   }
   
   void randomize(GameContainer container) {
      // create the map
      mapWidth = container.getWidth() / TILE_SIZE + 1;
      mapHeight = container.getHeight() / TILE_SIZE + 1;
      tileMap = new Image[mapWidth][mapHeight];
      for (int x = 0; x < mapWidth; x++) {
         for (int y = 0; y < mapHeight; y++) {
            tileMap[x][y] = randomTile();
         }
      }
   }

   public void resetLights(GameContainer container) {
      //clear the lights and add a new one with default scale
      lights.clear();
      lights.add(new Light(container.getInput().getMouseX(), container.getInput().getMouseY(), 1f));
   }
   
   public void render(GameContainer container, Graphics g)
         throws SlickException {
      //each light requires a new pass to blend with the previous lights
      //FPS will be affected with too many lights at once
      for (int i=0; i<lights.size(); i++) {
         Light light = lights.get(i);
                  
         //set up our alpha map for the light
         g.setDrawMode(Graphics.MODE_ALPHA_MAP);
         //clear the alpha map before we draw to it...
         g.clearAlphaMap();
         
         int alphaW = (int)(alphaMap.getWidth() * light.scale);
         int alphaH = (int)(alphaMap.getHeight() * light.scale);
         int alphaX = (int)(light.x - alphaW/2f);
         int alphaY = (int)(light.y - alphaH/2f);
         
         //we apply the light alpha here; RGB will be ignored
         sharedColor.a = light.alpha;
         
         //draw the alpha map
         alphaMap.draw(alphaX, alphaY, alphaW, alphaH);
         
         //start blending in our tiles
         g.setDrawMode(Graphics.MODE_ALPHA_BLEND);
         
         //we'll clip to the alpha rectangle, since anything outside of it will be transparent
         g.setClip(alphaX, alphaY, alphaW, alphaH);
         
         //we'll use startUse/drawEmbedded/endUse for efficiency
         spriteSheet.startUse();
         //after startUse, we can apply a new tint like so..
         //since we're in MODE_ALPHA_BLEND, the alpha value will be ignored (hence applying it above)
         light.tint.bind(); 
         for (int x = 0; x < mapWidth; x++) {
            for (int y = 0; y < mapHeight; y++) {
               tileMap[x][y].drawEmbedded(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
            }
         }
         spriteSheet.endUse();
         g.clearClip();
      }
      
      //reset the mode to normal before continuing..
      g.setDrawMode(Graphics.MODE_NORMAL);
      
      g.setColor(Color.white);
      g.drawString("Mouse click to add a light (total count: "+lights.size()+")", 10, 25);
      g.drawString("Press R to randomize the map tiles", 10, 40);
      g.drawString("Press SPACE to reset the lights", 10, 55);
   }

   public void update(GameContainer container, int delta)
         throws SlickException {
      if (container.getInput().isKeyPressed(Input.KEY_R))
         randomize(container);
      if (container.getInput().isKeyPressed(Input.KEY_SPACE)) {
         resetLights(container);
      }
      elapsed += delta;
      
      //update all lights to have them smoothly scale
      for (int i=0; i<lights.size(); i++) {
         lights.get(i).update(elapsed / 1000f);
      }
      
      //the last-added light will be the one under the mouse
      if (lights.size()>0) {
         Light l = lights.get(lights.size()-1);
         l.x = container.getInput().getMouseX();
         l.y = container.getInput().getMouseY();
      }
   }
   
   //adds a new light
   public void mousePressed(int button, int x, int y) {
      float randSize = random.nextInt(15)*.1f+.5f;
      Color randColor = new Color(random.nextFloat(), random.nextFloat(), random.nextFloat());
      lights.add(new Light(x, y, randSize, randColor));
   }

   public static void main(String[] args) {
      try {
         new AppGameContainer(new LightingTestAlpha(), 800, 600, false).start();
      } catch (SlickException e) {
         e.printStackTrace();
      }
   }
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

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

The first screenshot will be displayed as a thumbnail.

Riven (9 views)
2014-10-02 14:36:20

Pippogeek (41 views)
2014-09-24 16:13:29

Pippogeek (32 views)
2014-09-24 16:12:22

Pippogeek (22 views)
2014-09-24 16:12:06

Grunnt (47 views)
2014-09-23 14:38:19

radar3301 (30 views)
2014-09-21 23:33:17

BurntPizza (65 views)
2014-09-21 02:42:18

BurntPizza (37 views)
2014-09-21 01:30:30

moogie (44 views)
2014-09-21 00:26:15

UprightPath (53 views)
2014-09-20 20:14:06
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!