Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package demominer.gui.drawer;
- import demominer.UI.component.Component;
- import demominer.UI.component.WorldArea;
- import demominer.UI.panels.GamePanel;
- import demominer.game.Game;
- import demominer.other.General;
- import demominer.other.Perlin;
- import demominer.resources.Resources;
- import demominer.sprite.SpriteImage;
- import demominer.world.Block;
- import demominer.world.Ores.OreTypes;
- import demominer.world.map.Chunk;
- import java.util.Random;
- import org.lwjgl.opengl.GL11;
- /**
- *
- * @author Szoltomi
- */
- public class DMinerDrawer extends DrawerMethods {
- static int someCounter = 0;
- /**
- *
- */
- public boolean running = true;
- private static Perlin TP = new Perlin();
- private static Random oreDrawerRandom = new Random(0);
- private static RenderBuffer worldBuffer;
- private static SpriteLocation sl = new SpriteLocation();
- private static int[] worldAreaFBO;
- private static int[] windowFBO;
- private static long viewX;
- private static long viewY;
- private static final double PXS = 1;
- private static final double BLOCK_GRID_RATIO = (1d / Block.SIZE) * WorldArea.GRIDSIZE;
- private static double ZOOM = 2f;
- /**
- * Initializes drawing. Called once at the start of the drawing thread.
- */
- public static void init() {
- /* Initializes OpenGL, see DrawerMethods. GamePanel is the main interface container
- * which contains every other UI element, and the game world as well.
- * The window is twice the size of GamePanel.
- */
- initDraw(GamePanel.WIDTH * 2, GamePanel.HEIGHT * 2);
- //Zooms everything in, by a magnitude of 2
- viewZoom(ZOOM);
- //used for testing. Ignore.
- TP.perlinRand.setSeed(3031);
- //Creates the FBO which the game world is drawn to.
- worldAreaFBO = initFBOTexture(WorldArea.WIDTH, WorldArea.HEIGHT);
- //Creates the FBO which everything is drawn to, so it can be flipped later.
- windowFBO = initFBOTexture(GamePanel.WIDTH, GamePanel.HEIGHT);
- /* This sets the background of the WorldArea UI component to the world FBO.
- * When this UI component is drawn, it draws the game world.
- */
- WorldArea.updateTexture(worldAreaFBO[1]);
- }
- /**
- * This is the root drawing method, which gets called in the drawing loop.
- */
- public static void drawEverything() {
- //clear screen
- clear();
- //draws the world to worldFBO. See more below.
- drawWorldToTexture();
- //binds the view-flipping FBO. See DrawerMethods for definition.
- bindFBO(windowFBO);
- //translates the matrix to origo, and sets the zoom to 1 (no zoom)
- viewMatrix(0, 0, 1f);
- /* Draws out the root UI component, in which everything else is contained.
- * It recursively draws out every UI component and container in it, including the worldFBO (the game world).
- */
- drawUiComponent(Game.gamePanel);
- //unbinds the view flipping FBO
- unbindFBO();
- //zooms in 2X
- viewMatrix(0, 0, 2f);
- //draws the view flipping FBO's texture to the screen
- drawFBOTextureToWindow(windowFBO[1]);
- //checks for opengl errors.
- checkErrors();
- }
- public static void drawWorldToTexture() {
- /*
- * The threading and order of creation of basic game objects needs to be done faultproofly.
- * Until then, nulltesting is the most faultproof way of avoiding errors.
- * Game.world.renderBuffers.ready means the triple buffer containing the sprites is ready to be read.
- */
- if (Game.world != null && Game.world.renderBuffers.getReadable() != null && Game.world.renderBuffers.ready){
- //binds the world FBO. See DrawerMethods for definition.
- bindFBO(worldAreaFBO);
- //clears the screen (here, the FBO)
- clear();
- //gets the newest readable buffer from the triplebuffer.
- worldBuffer = Game.world.renderBuffers.getReadable();
- //claims (locks) it so it cannot be written to while it's being read.
- worldBuffer.claim();
- //This block contains only translations and quad drawing. Can be ignored.
- {
- //transforming the view world coordinaltes to screen cordinates, then translating.
- viewX = (long) ((-worldBuffer.viewX * BLOCK_GRID_RATIO) + WorldArea.WIDTH / 2);
- viewY = (long) ((-worldBuffer.viewY * BLOCK_GRID_RATIO) + WorldArea.HEIGHT / 2);
- viewMatrix(viewX, viewY, 1);
- //goes to the beginning of the buffer
- worldBuffer.goToFirst();
- //Initializes openGL for sprite drawing. See DrawerMethods for definition. Not important though.
- DrawerMethods.initDrawSprites(Resources.fieldSprites);
- //draws out the game terrain
- int x = worldBuffer.chunksX;
- int y = worldBuffer.chunksY;
- for (int i = 0; i <= RenderBuffer.CHUNK_DRAW_DISTANCE * 2; i++) {
- for (int j = 0; j <= RenderBuffer.CHUNK_DRAW_DISTANCE * 2; j++) {
- if (worldBuffer.chunks != null) {
- drawChunk(worldBuffer.chunks[i][j], i + x, j + y);
- }
- }
- }
- //draws out the game world's sprites
- setColor(1f, 1f, 1f, 1f);
- worldBuffer.getIntoSt(sl);
- while (sl.spriteImage != null) {
- drawSpriteImageCentered((long) (sl.x * BLOCK_GRID_RATIO),
- (long) (sl.y * BLOCK_GRID_RATIO),
- sl.z / Block.SIZE,
- PXS, sl.spriteImage);
- worldBuffer.getIntoSt(sl);
- }
- drawEnd();
- }
- //releases the buffer so it can be written to later.
- worldBuffer.release();
- //unbinds the world FBO
- unbindFBO();
- }
- }
- /**
- * This method draws one quad, stretched to the screen.
- * It's texture is from a FBO where everything is drawn to.
- * The texture is flipped on the vertical axis, thus changing OpenGL's bottom-right point of origin to the top.
- */
- private static void drawFBOTextureToWindow(int id) {
- GL11.glBindTexture(GL11.GL_TEXTURE_2D, id);
- GL11.glBegin(GL11.GL_QUADS);
- setColor(1f, 1f, 1f, 1f);
- GL11.glTexCoord2f(0f, 1f);
- GL11.glVertex2d(0f, 0f);
- GL11.glTexCoord2f(1f, 1f);
- GL11.glVertex2d(GamePanel.WIDTH, 0f);
- GL11.glTexCoord2f(1f, 0f);
- GL11.glVertex2d(GamePanel.WIDTH, GamePanel.HEIGHT);
- GL11.glTexCoord2f(0f, 0f);
- GL11.glVertex2d(0f, GamePanel.HEIGHT);
- GL11.glEnd();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment