Advertisement
Elec0

TextureGenerator.java

Apr 6th, 2013 (edited)
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.25 KB | None | 0 0
  1. package elec0.proceduralCity;
  2.  
  3. import java.util.Random;
  4.  
  5. import org.newdawn.slick.Color;
  6. import org.newdawn.slick.Graphics;
  7. import org.newdawn.slick.Image;
  8. import org.newdawn.slick.SlickException;
  9. import org.newdawn.slick.opengl.Texture;
  10.  
  11. public class TextureGenerator
  12. {
  13.     private Image tTex;
  14.     private Graphics g;
  15.     private Random rand;
  16.     private Color cBaseColor;
  17.     private int iHorizMaxWindows; // Horizontal and vertical maximum windows without a window-sized break
  18.     private int iVertMaxWindows;
  19.    
  20.     public int WIND_WIDTH = 5;
  21.     public int WIND_HEIGHT = 5;
  22.     public int TEX_HEIGHT = 256;
  23.     public int TEX_WIDTH = 256;
  24.     private final int BUFFER_X = WIND_WIDTH * 1;
  25.     private final int BUFFER_Y = WIND_WIDTH * 1;
  26.     private final int BUFFER_MAX_X = TEX_WIDTH - WIND_WIDTH * 2;
  27.     private final int BUFFER_MAX_Y = TEX_HEIGHT - WIND_HEIGHT * 2;
  28.    
  29.     public TextureGenerator()
  30.     { }
  31.  
  32.     public Texture generate()
  33.     {
  34.         rand = new Random();
  35.         try {
  36.             tTex = new Image(TEX_WIDTH, TEX_HEIGHT);
  37.             g = tTex.getGraphics();
  38.         } catch (SlickException e) {
  39.             e.printStackTrace();
  40.         }
  41.        
  42.         iHorizMaxWindows = 10 + rand.nextInt(15);
  43.         iVertMaxWindows = 10 + rand.nextInt(25);
  44.        
  45.        
  46.         int colors = 3;
  47.         int pick = rand.nextInt(colors);
  48.        
  49.         // Pick default color, either blue, white, or orange
  50.         if(pick == 0) // White
  51.         {
  52.             cBaseColor = Color.white;
  53.         }
  54.         else if(pick == 1) // Orange
  55.         {
  56.             cBaseColor = new Color(200 + rand.nextInt(55), 125 + rand.nextInt(155), 0);
  57.            
  58.         }
  59.         else if(pick == 2)
  60.         {
  61.             cBaseColor = new Color(0, 125 + rand.nextInt(75), 125 + rand.nextInt(75));
  62.         }
  63.        
  64.         g.setColor(Color.black);
  65.         g.fillRect(0, 0, TEX_WIDTH, TEX_HEIGHT);
  66.         g.flush();
  67.        
  68.         for(int y = BUFFER_Y; y < BUFFER_MAX_Y; y += WIND_HEIGHT)
  69.         {
  70.             for(int x = BUFFER_X; x < BUFFER_MAX_X; x += WIND_WIDTH)
  71.             {
  72.                 if(y % (iVertMaxWindows*WIND_HEIGHT) == 0 || x % (iHorizMaxWindows*WIND_WIDTH) == 0)
  73.                 {
  74.                     // Skip window for break
  75.                     g.setColor(new Color(0, 0, 5));
  76.                     g.fillRect(x, y, WIND_WIDTH, WIND_HEIGHT);                                     
  77.                     g.flush();
  78.                 }
  79.                 else
  80.                 {
  81.                     float fPercent = randomInRange(0, .1f);
  82.                    
  83.                     if(rand.nextFloat() < fPercent)
  84.                     {
  85.                        
  86.                         genWindow(x, y, true);
  87.                     }
  88.                     else
  89.                         genWindow(x, y, false);
  90.                 }
  91.             }
  92.         }
  93.         return tTex.getTexture();
  94.     }
  95.    
  96.     private void genWindow(int x, int y, boolean lit)
  97.     {
  98.         if(lit)
  99.         {
  100.             float percent = randomInRange(0, 0.3f);
  101.             if(rand.nextBoolean()) // Subtract or add
  102.                 percent *= -1;
  103.            
  104.             float fR = cBaseColor.r*percent + cBaseColor.r;
  105.             float fG = cBaseColor.g*percent + cBaseColor.g;
  106.             float fB = cBaseColor.b*percent + cBaseColor.b;
  107.            
  108.             if(fR > 255)
  109.                 fR = 255;
  110.             if(fG > 255)
  111.                 fG = 255;
  112.             if(fB > 255)
  113.                 fB = 255;
  114.            
  115.             Color col = new Color(fR, fG, fB);
  116.            
  117.             g.setColor(col);
  118.            
  119.             g.fillRect(x, y, WIND_WIDTH, WIND_HEIGHT);
  120.             g.flush();
  121.         }
  122.         else
  123.         {
  124.             int col = 0;
  125.             if(rand.nextBoolean())
  126.                 col = rand.nextInt(10);
  127.             g.setColor(new Color(col, col, col));
  128.            
  129.             g.fillRect(x, y, WIND_WIDTH, WIND_HEIGHT);
  130.             g.flush();
  131.         }
  132.     }
  133.    
  134.     private float randomInRange(float min, float max)
  135.     {
  136.         return (float)Math.random() * (max-min) + min;
  137.     }
  138. //  public Texture getTexture()
  139. //  {
  140. //      return tTex.getTexture();
  141. //  }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement