szoltomi

DMinerDrawer

Aug 31st, 2011
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.01 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package demominer.gui.drawer;
  6.  
  7. import demominer.UI.component.Component;
  8. import demominer.UI.component.WorldArea;
  9. import demominer.UI.panels.GamePanel;
  10. import demominer.game.Game;
  11. import demominer.other.General;
  12. import demominer.other.Perlin;
  13. import demominer.resources.Resources;
  14. import demominer.sprite.SpriteImage;
  15. import demominer.world.Block;
  16. import demominer.world.Ores.OreTypes;
  17. import demominer.world.map.Chunk;
  18.  
  19.  
  20. import java.util.Random;
  21. import org.lwjgl.opengl.GL11;
  22.  
  23. /**
  24.  *
  25.  * @author Szoltomi
  26.  */
  27. public class DMinerDrawer extends DrawerMethods {
  28.  
  29.     static int someCounter = 0;
  30.     /**
  31.      *
  32.      */
  33.     public boolean running = true;
  34.     private static Perlin TP = new Perlin();
  35.     private static Random oreDrawerRandom = new Random(0);
  36.     private static RenderBuffer worldBuffer;
  37.     private static SpriteLocation sl = new SpriteLocation();
  38.     private static int[] worldAreaFBO;
  39.     private static int[] windowFBO;
  40.     private static long viewX;
  41.     private static long viewY;
  42.     private static final double PXS = 1;
  43.     private static final double BLOCK_GRID_RATIO = (1d / Block.SIZE) * WorldArea.GRIDSIZE;
  44.     private static double ZOOM = 2f;
  45.  
  46.     /**
  47.      * Initializes drawing. Called once at the start of the drawing thread.
  48.      */
  49.     public static void init() {
  50.         /* Initializes OpenGL, see DrawerMethods. GamePanel is the main interface container
  51.          * which contains every other UI element, and the game world as well.
  52.          * The window is twice the size of GamePanel.
  53.          */
  54.         initDraw(GamePanel.WIDTH * 2, GamePanel.HEIGHT * 2);
  55.         //Zooms everything in, by a magnitude of 2
  56.         viewZoom(ZOOM);
  57.         //used for testing. Ignore.
  58.         TP.perlinRand.setSeed(3031);
  59.         //Creates the FBO which the game world is drawn to.
  60.         worldAreaFBO = initFBOTexture(WorldArea.WIDTH, WorldArea.HEIGHT);
  61.         //Creates the FBO which everything is drawn to, so it can be flipped later.
  62.         windowFBO = initFBOTexture(GamePanel.WIDTH, GamePanel.HEIGHT);
  63.         /* This sets the background of the WorldArea UI component to the world FBO.
  64.          * When this UI component is drawn, it draws the game world.
  65.          */
  66.         WorldArea.updateTexture(worldAreaFBO[1]);
  67.  
  68.     }
  69.  
  70.     /**
  71.      * This is the root drawing method, which gets called in the drawing loop.
  72.      */
  73.     public static void drawEverything() {
  74.         //clear screen
  75.         clear();
  76.         //draws the world to worldFBO. See more below.
  77.         drawWorldToTexture();
  78.         //binds the view-flipping FBO. See DrawerMethods for definition.
  79.         bindFBO(windowFBO);
  80.         //translates the matrix to origo, and sets the zoom to 1 (no zoom)
  81.         viewMatrix(0, 0, 1f);
  82.         /* Draws out the root UI component, in which everything else is contained.
  83.          * It recursively draws out every UI component and container in it, including the worldFBO (the game world).
  84.         */
  85.         drawUiComponent(Game.gamePanel);
  86.         //unbinds the view flipping FBO
  87.         unbindFBO();
  88.         //zooms in 2X
  89.         viewMatrix(0, 0, 2f);
  90.         //draws the view flipping FBO's texture to the screen
  91.         drawFBOTextureToWindow(windowFBO[1]);
  92.         //checks for opengl errors.
  93.         checkErrors();
  94.     }
  95.  
  96.     public static void drawWorldToTexture() {
  97.         /*
  98.          * The threading and order of creation of basic game objects needs to be done faultproofly.
  99.          * Until then, nulltesting is the most faultproof way of avoiding errors.
  100.          * Game.world.renderBuffers.ready means the triple buffer containing the sprites is ready to be read.
  101.          */
  102.         if (Game.world != null && Game.world.renderBuffers.getReadable() != null && Game.world.renderBuffers.ready){
  103.             //binds the world FBO. See DrawerMethods for definition.
  104.             bindFBO(worldAreaFBO);
  105.             //clears the screen (here, the FBO)
  106.             clear();
  107.             //gets the newest readable buffer from the triplebuffer.
  108.             worldBuffer = Game.world.renderBuffers.getReadable();
  109.             //claims (locks) it so it cannot be written to while it's being read.
  110.             worldBuffer.claim();
  111.             //This block contains only translations and quad drawing. Can be ignored.
  112.             {
  113.                 //transforming the view world coordinaltes to screen cordinates, then translating.
  114.                 viewX = (long) ((-worldBuffer.viewX * BLOCK_GRID_RATIO) + WorldArea.WIDTH / 2);
  115.                 viewY = (long) ((-worldBuffer.viewY * BLOCK_GRID_RATIO) + WorldArea.HEIGHT / 2);
  116.                 viewMatrix(viewX, viewY, 1);
  117.                 //goes to the beginning of the buffer
  118.                 worldBuffer.goToFirst();
  119.                 //Initializes openGL for sprite drawing. See DrawerMethods for definition. Not important though.
  120.                 DrawerMethods.initDrawSprites(Resources.fieldSprites);
  121.         //draws out the game terrain
  122.                 int x = worldBuffer.chunksX;
  123.                 int y = worldBuffer.chunksY;
  124.                 for (int i = 0; i <= RenderBuffer.CHUNK_DRAW_DISTANCE * 2; i++) {
  125.                     for (int j = 0; j <= RenderBuffer.CHUNK_DRAW_DISTANCE * 2; j++) {
  126.                         if (worldBuffer.chunks != null) {
  127.                             drawChunk(worldBuffer.chunks[i][j], i + x, j + y);
  128.                         }
  129.                     }
  130.                 }
  131.         //draws out the game world's sprites
  132.                 setColor(1f, 1f, 1f, 1f);
  133.                 worldBuffer.getIntoSt(sl);
  134.                 while (sl.spriteImage != null) {
  135.                     drawSpriteImageCentered((long) (sl.x * BLOCK_GRID_RATIO),
  136.                                             (long) (sl.y * BLOCK_GRID_RATIO),
  137.                                             sl.z / Block.SIZE,
  138.                                             PXS, sl.spriteImage);
  139.                     worldBuffer.getIntoSt(sl);
  140.                 }
  141.                 drawEnd();
  142.             }
  143.             //releases the buffer so it can be written to later.
  144.             worldBuffer.release();
  145.             //unbinds the world FBO
  146.             unbindFBO();
  147.         }
  148.     }
  149.    
  150.     /**
  151.      * This method draws one quad, stretched to the screen.
  152.      * It's texture is from a FBO where everything is drawn to.
  153.      * The texture is flipped on the vertical axis, thus changing OpenGL's bottom-right point of origin to the top.
  154.      */
  155.     private static void drawFBOTextureToWindow(int id) {
  156.         GL11.glBindTexture(GL11.GL_TEXTURE_2D, id);
  157.         GL11.glBegin(GL11.GL_QUADS);
  158.         setColor(1f, 1f, 1f, 1f);
  159.         GL11.glTexCoord2f(0f, 1f);
  160.         GL11.glVertex2d(0f, 0f);
  161.  
  162.         GL11.glTexCoord2f(1f, 1f);
  163.         GL11.glVertex2d(GamePanel.WIDTH, 0f);
  164.  
  165.         GL11.glTexCoord2f(1f, 0f);
  166.         GL11.glVertex2d(GamePanel.WIDTH, GamePanel.HEIGHT);
  167.  
  168.         GL11.glTexCoord2f(0f, 0f);
  169.         GL11.glVertex2d(0f, GamePanel.HEIGHT);
  170.         GL11.glEnd();
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment