pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

epitaph64 private pastebin - collaborative debugging tool What's a private pastebin?


Posted by Epitaph64 on Tue 9 Jun 20:05
report abuse | download | new post

  1. package bufferedterrain;
  2.  
  3. import org.newdawn.slick.opengl.renderer.Renderer;
  4. import org.newdawn.slick.AppGameContainer;
  5. import org.newdawn.slick.BasicGame;
  6. import org.newdawn.slick.Color;
  7. import org.newdawn.slick.GameContainer;
  8. import org.newdawn.slick.Graphics;
  9. import org.newdawn.slick.Input;
  10. import org.newdawn.slick.SlickException;
  11.  
  12. public class Main extends BasicGame {
  13.  
  14.     Main()
  15.     {
  16.         super("Terrain Test");
  17.     }
  18.  
  19.     private Input input;
  20.  
  21.     private byte[][] terrain;
  22.     private boolean[][] update;
  23.  
  24.     private short columnToUpdate = 0;
  25.  
  26.     private short brushX, brushY;
  27.     private boolean brushOn;
  28.     private byte brushType = -1;
  29.     private boolean resetTime = true;
  30.     private byte mouseButton = 0;
  31.  
  32.     private short width = 400;
  33.     private short height = 300;
  34.  
  35.     public static void main(String[] args) {
  36.         try
  37.         {
  38.             AppGameContainer app = new AppGameContainer(new Main());
  39.             app.setDisplayMode(800, 600, false);
  40.             app.setFullscreen(false);
  41.             app.setClearEachFrame(false);
  42.             app.setShowFPS(false);
  43.             app.start();
  44.         }
  45.         catch (SlickException e)
  46.         {
  47.             e.printStackTrace();
  48.         }
  49.     }
  50.  
  51.     @Override
  52.     public void init(GameContainer container) throws SlickException
  53.     {
  54.         input = container.getInput();
  55.         Renderer.setRenderer(Renderer.VERTEX_ARRAY_RENDERER);
  56.         start();
  57.     }
  58.  
  59.     @Override
  60.     public void update(GameContainer container, int delta) throws SlickException
  61.     {
  62.         updateInput(container);
  63.         updateTerrain();
  64.     }
  65.  
  66.     public void render(GameContainer container, Graphics g) throws SlickException
  67.     {
  68.         byte tt = 0;
  69.         for (int y = 0; y < height; y++)
  70.         {
  71.             for (int x = 0; x < width; x++)
  72.             {
  73.                 if (update[x][y])
  74.                 {
  75.                     tt = terrain[x][y];
  76.                     switch(tt)
  77.                     {
  78.                         case -1:
  79.                         {
  80.                             g.setColor(new Color(255, 100, 100));
  81.                             break;
  82.                         }
  83.                         case 0:
  84.                         {
  85.                             g.setColor(Color.black);
  86.                             break;
  87.                         }
  88.                         case 1:
  89.                         {
  90.                             g.setColor(Color.white);
  91.                             break;
  92.                         }
  93.                         case 2:
  94.                         {
  95.                             g.setColor(Color.orange);
  96.                             break;
  97.                         }
  98.                         case 3:
  99.                         {
  100.                             g.setColor(Color.blue);
  101.                             break;
  102.                         }
  103.                         case 4:
  104.                         {
  105.                             g.setColor(Color.red);
  106.                             break;
  107.                         }
  108.                     }
  109.                     g.fillRect(x * 2, y * 2, 2, 2);
  110.                     update[x][y] = false;
  111.                 }
  112.             }
  113.         }
  114.     }
  115.  
  116.     private void drawBrush(int brushX, int brushY)
  117.     {
  118.         if (brushX >= 1 && brushX < width - 1 && brushY >= 1 && brushY < height - 1)
  119.         {
  120.             //Scripted brush
  121.             terrain[brushX][brushY] = brushType;
  122.             terrain[brushX + 1][brushY] = brushType;
  123.             terrain[brushX + 1][brushY + 1] = brushType;
  124.             terrain[brushX][brushY + 1] = brushType;
  125.             terrain[brushX - 1][brushY] = brushType;
  126.             terrain[brushX - 1][brushY - 1] = brushType;
  127.             terrain[brushX][brushY - 1] = brushType;
  128.             terrain[brushX + 1][brushY - 1] = brushType;
  129.             terrain[brushX - 1][brushY + 1] = brushType;
  130.             update[brushX][brushY] = true;
  131.             update[brushX + 1][brushY] = true;
  132.             update[brushX + 1][brushY + 1] = true;
  133.             update[brushX][brushY + 1] = true;
  134.             update[brushX - 1][brushY] = true;
  135.             update[brushX - 1][brushY - 1] = true;
  136.             update[brushX + 1][brushY - 1] = true;
  137.             update[brushX - 1][brushY + 1] = true;
  138.         }
  139.     }
  140.  
  141.     private void start()
  142.     {
  143.         terrain = new byte[width][height];
  144.         update = new boolean[width][height];
  145.     }
  146.  
  147.     private void updateInput(GameContainer container)
  148.     {
  149.         mouseButton = 0;
  150.         short mX = (short) (input.getMouseX() / 2);
  151.         short mY = (short) (input.getMouseY() / 2);
  152.         if (input.isKeyPressed(Input.KEY_ESCAPE)) container.exit();
  153.         if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON))
  154.         {
  155.             if (resetTime)
  156.             {
  157.                 brushX = mX;
  158.                 brushY = mY;
  159.                 brushOn = true;
  160.                 brushType = -1;
  161.                 brushOn = true;
  162.                 resetTime = false;
  163.                 drawBrush(brushX, brushY);
  164.             }
  165.         }
  166.         else
  167.         {
  168.             mouseButton ++;
  169.         }
  170.         if (input.isMouseButtonDown(Input.MOUSE_RIGHT_BUTTON))
  171.         {
  172.             if (resetTime)
  173.             {
  174.                 brushX = mX;
  175.                 brushY = mY;
  176.                 brushOn = true;
  177.                 brushType = 0;
  178.                 brushOn = true;
  179.                 resetTime = false;
  180.                 drawBrush(brushX, brushY);
  181.             }
  182.         }
  183.         else
  184.         {
  185.             mouseButton ++;
  186.         }
  187.         if (mouseButton == 2)
  188.         {
  189.             resetTime = true;
  190.             brushOn = false;
  191.         }
  192.         if (brushOn)
  193.         {
  194.             while(brushX != mX && brushY != mY)
  195.             {
  196.                 drawBrush(brushX, brushY);
  197.                 if (Math.abs(brushX - mX) >= (Math.abs(brushY - mY)))
  198.                 {
  199.                     if (brushX < mX)
  200.                     {
  201.                         brushX ++;
  202.                     }
  203.                     if (brushX > mX)
  204.                     {
  205.                         brushX --;
  206.                     }
  207.                 }
  208.                 else
  209.                 {
  210.                     if (brushY < mY)
  211.                     {
  212.                         brushY ++;
  213.                     }
  214.                     if (brushY > mY)
  215.                     {
  216.                         brushY --;
  217.                     }
  218.                 }
  219.             }
  220.         }
  221.     }
  222.  
  223.     private void updateTerrain()
  224.     {
  225.         for (int i = 0; i < width / 32; i++)
  226.         {
  227.             for (int x = columnToUpdate; x < columnToUpdate + 16; x++)
  228.             {
  229.                 for (int y = height - 2; y >= 0; y--)
  230.                 {
  231.                     if (terrain[x][y] > 0)
  232.                     {
  233.                         if (terrain[x][y+1] == 0 && terrain[x][y] != 0)
  234.                         {
  235.                             terrain[x][y+1] = terrain[x][y];
  236.                             terrain[x][y] = 0;
  237.                             update[x][y] = true;
  238.                             update[x][y+1] = true;
  239.                         }
  240.                         if (y + 1 < height - 1)
  241.                         {
  242.                             if (Math.random() * 2 > 1)
  243.                             {
  244.                                 if (x + 1 < width)
  245.                                 {
  246.                                     if (y + 1 < height - 1)
  247.                                     {
  248.                                         if (terrain[x+1][y+1] == 0 || terrain[x][y] > 2)
  249.                                         {
  250.                                             if (terrain[x+1][y] == 0)
  251.                                             {
  252.                                                 terrain[x+1][y] = terrain[x][y];
  253.                                                 terrain[x][y] = 0;
  254.                                                 update[x][y] = true;
  255.                                                 update[x+1][y] = true;
  256.                                             }
  257.                                         }
  258.                                     }
  259.                                 }
  260.                             }
  261.                             else
  262.                             {
  263.                                 if (x - 1 >= 0)
  264.                                 {
  265.                                     if (y + 1 < height - 1)
  266.                                     {
  267.                                         if (terrain[x-1][y+1] == 0 || terrain[x][y] > 2)
  268.                                         {
  269.                                             if (terrain[x-1][y] == 0)
  270.                                             {
  271.                                                 terrain[x-1][y] = terrain[x][y];
  272.                                                 terrain[x][y] = 0;
  273.                                                 update[x][y] = true;
  274.                                                 update[x-1][y] = true;
  275.                                             }
  276.                                         }
  277.                                     }
  278.                                 }
  279.                             }
  280.                         }
  281.                     }
  282.                 }
  283.             }
  284.             columnToUpdate += 16;
  285.             if (columnToUpdate == width)
  286.             {
  287.                 columnToUpdate = 0;
  288.                 terrain[75 + (int)(Math.random() * 32)-16][0] = 1;
  289.                 terrain[150 + (int)(Math.random() * 32)-16][0] = 2;
  290.                 terrain[225 + (int)(Math.random() * 32)-16][0] = 3;
  291.                 terrain[300 + (int)(Math.random() * 32)-16][0] = 4;
  292.             }
  293.         }
  294.     }
  295. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post