Advertisement
szoltomi

DMinerDrawer

Aug 31st, 2011
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.88 KB | None | 0 0
  1. package demominer.gui.drawer;
  2.  
  3. import demominer.UI.component.Component;
  4. import demominer.UI.component.WorldArea;
  5. import demominer.UI.panels.GamePanel;
  6. import demominer.game.Game;
  7. import demominer.other.General;
  8. import demominer.other.Perlin;
  9. import demominer.resources.Resources;
  10. import demominer.sprite.SpriteImage;
  11. import demominer.world.Block;
  12. import demominer.world.Ores.OreTypes;
  13. import demominer.world.map.Chunk;
  14.  
  15.  
  16. import java.util.Random;
  17. import org.lwjgl.opengl.GL11;
  18.  
  19. /**
  20.  *
  21.  * @author Szoltomi
  22.  */
  23. public class DMinerDrawer extends DrawerMethods {
  24.  
  25.    
  26.     static int someCounter = 0;
  27.     public boolean running = true;
  28.     private static Perlin TP = new Perlin();
  29.     private static Random oreDrawerRandom = new Random(0);
  30.     private static RenderBuffer worldBuffer;
  31.     private static SpriteLocation sl = new SpriteLocation();
  32.     private static int[] worldAreaFBO;
  33.     private static int[] windowFBO;
  34.     private static long viewX;
  35.     private static long viewY;
  36.     private static final double PXS = 1;
  37.     private static final double BLOCK_GRID_RATIO = (1d / Block.SIZE) * WorldArea.GRIDSIZE;
  38.     private static double ZOOM = 2f;
  39.  
  40.     public static void init() {
  41.         initDraw(GamePanel.WIDTH * 2, GamePanel.HEIGHT * 2);
  42.         viewZoom(ZOOM);
  43.         TP.perlinRand.setSeed(3031);
  44.         worldAreaFBO = initFBOTexture(WorldArea.WIDTH, WorldArea.HEIGHT);
  45.         windowFBO = initFBOTexture(GamePanel.WIDTH, GamePanel.HEIGHT);
  46.         GL11.glDisable(GL11.GL_CULL_FACE);
  47.         WorldArea.updateTexture(worldAreaFBO[1]);
  48.  
  49.     }
  50.  
  51.     /**
  52.      * This is the root drawing method, which gets called in the drawing loop.
  53.      */
  54.     public static void drawEverything() {
  55.         clear();
  56.         drawWorldToTexture();
  57.         bindFBO(windowFBO);
  58.         viewMatrix(0, 0, 1f);
  59.         drawUiComponent(Game.gameWindow);
  60.         unbindFBO();
  61.         viewMatrix(0, 0, 2f);
  62.         drawFBOTextureToWindow(windowFBO[1]);
  63.         checkErrors();
  64.     }
  65.  
  66.     public static void drawWorldToTexture() {
  67.         //ugly null testing, and testing the triplebuffer I'm using for making the drawing and game loop separarely threaded.
  68.         if (Game.world != null && Game.world.renderBuffers.getReadable() != null && Game.world.renderBuffers.ready) {
  69.             bindFBO(worldAreaFBO);
  70.             clear();
  71.             worldBuffer = Game.world.renderBuffers.getReadable();
  72.             worldBuffer.claim();
  73.             //from now on, only matrix transformations and basic drawing methods:
  74.             //transforming the view world coordinaltes to screen cordinates, then translating.
  75.             viewX = (long) ((-worldBuffer.viewX * BLOCK_GRID_RATIO) + WorldArea.WIDTH / 2);
  76.             viewY = (long) ((-worldBuffer.viewY * BLOCK_GRID_RATIO) + WorldArea.HEIGHT / 2);
  77.             viewMatrix(viewX, viewY, 1);
  78.             //goes to the beginning of the buffer
  79.             worldBuffer.goToFirst();
  80.             //draw the world
  81.             DrawerMethods.initDrawSprites(Resources.fieldSprites);
  82.             int x = worldBuffer.chunksX;
  83.             int y = worldBuffer.chunksY;
  84.             for (int i = 0; i <= RenderBuffer.CHUNK_DRAW_DISTANCE * 2; i++) {
  85.                 for (int j = 0; j <= RenderBuffer.CHUNK_DRAW_DISTANCE * 2; j++) {
  86.                     if (worldBuffer.chunks != null) {
  87.                         drawChunk(worldBuffer.chunks[i][j], i + x, j + y);
  88.                     }
  89.                 }
  90.             }
  91.             setColor(1f,1f,1f,1f);
  92.             worldBuffer.getIntoSt(sl);
  93.             while (sl.spriteImage != null) {                
  94.                 drawSpriteImageCentered((long) (sl.x * BLOCK_GRID_RATIO),
  95.                                         (long) (sl.y * BLOCK_GRID_RATIO),
  96.                                         sl.z / Block.SIZE,
  97.                                         PXS, sl.spriteImage);
  98.                 worldBuffer.getIntoSt(sl);
  99.             }
  100.             drawEnd();
  101.            
  102.             worldBuffer.release();
  103.             unbindFBO();
  104.         }
  105.     }
  106.  
  107.     private static void drawFBOTextureToWindow(int id) {
  108.         GL11.glBindTexture(GL11.GL_TEXTURE_2D, id);
  109.         GL11.glBegin(GL11.GL_QUADS);
  110.         setColor(1f, 1f, 1f, 1f);
  111.         GL11.glTexCoord2f(0f, 1f);
  112.         GL11.glVertex2d(0f, 0f);
  113.  
  114.         GL11.glTexCoord2f(1f, 1f);
  115.         GL11.glVertex2d(GamePanel.WIDTH, 0f);
  116.  
  117.         GL11.glTexCoord2f(1f, 0f);
  118.         GL11.glVertex2d(GamePanel.WIDTH, GamePanel.HEIGHT);
  119.  
  120.         GL11.glTexCoord2f(0f, 0f);
  121.         GL11.glVertex2d(0f, GamePanel.HEIGHT);
  122.         GL11.glEnd();
  123.  
  124.     }
  125.  
  126.     private static void drawChunk(Chunk c, int x, int y) {
  127.         if (c == null) {
  128.             return;
  129.         }
  130.         int s = WorldArea.GRIDSIZE * Chunk.SIZE;
  131.         int cX = x * s;
  132.         int bX = -WorldArea.GRIDSIZE;
  133.         int cY = y * s;
  134.         int bY = -WorldArea.GRIDSIZE;
  135.         for (int i = 0; i < Chunk.SIZE; i++) {
  136.             bX += WorldArea.GRIDSIZE;
  137.             for (int j = 0; j < Chunk.SIZE; j++) {
  138.                 bY += WorldArea.GRIDSIZE;
  139.                 drawWall(cX + bX, cY + bY,
  140.                          c.getBlockHealth(i, j),
  141.                          c.getBlockHardness(i, j),
  142.                          c.getBlockMineralId(i, j));
  143.             }
  144.             bY = -WorldArea.GRIDSIZE;
  145.         }
  146.     }
  147.  
  148.     private static void drawWall(long x, long y, int health, int hardness, int mineralId) {
  149.         if (x < -WorldArea.GRIDSIZE - viewX || x > WorldArea.WIDTH + WorldArea.GRIDSIZE - viewX
  150.             || y < -WorldArea.GRIDSIZE - viewY || y > WorldArea.HEIGHT + WorldArea.GRIDSIZE - viewY) {
  151.             return;
  152.         }
  153.         setColor(1f, 1f, 1f, 1f);
  154.         drawSpriteImage(x, y, 0, PXS, Resources.getWallSprite(health, hardness, mineralId));
  155.         float r, g, b;
  156.         if (mineralId > 0) {
  157.             //draw the ores
  158.             r = OreTypes.ores[mineralId].red;
  159.             g = OreTypes.ores[mineralId].green;
  160.             b = OreTypes.ores[mineralId].blue;
  161.             setColor(r, g, b, 1f);
  162.             long key = General.coordToHash(x, y);
  163.             oreDrawerRandom.setSeed(key);
  164.             for (int i = 0; i < 3; i++) {
  165.                 SpriteImage drawnSprite;
  166.                 if (OreTypes.ores[mineralId].isOre) {
  167.                     int spriteId = oreDrawerRandom.nextInt(Resources.oreSprites.length);
  168.                     drawnSprite = Resources.oreSprites[spriteId];
  169.                 } else {
  170.                     int spriteId = oreDrawerRandom.nextInt(Resources.gemSprites.length);
  171.                     drawnSprite = Resources.gemSprites[spriteId];
  172.                 }
  173.  
  174.                 long offsetX = oreDrawerRandom.nextInt(WorldArea.GRIDSIZE - drawnSprite.getWidth());
  175.                 long offsetY = oreDrawerRandom.nextInt(WorldArea.GRIDSIZE - drawnSprite.getHeight());
  176.                 drawSpriteImage(x + offsetX, y + offsetY, 0, PXS, drawnSprite);
  177.             }
  178.         }
  179.     }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement