Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- public static void init() {
- initDraw(GamePanel.WIDTH * 2, GamePanel.HEIGHT * 2);
- viewZoom(ZOOM);
- TP.perlinRand.setSeed(3031);
- worldAreaFBO = initFBOTexture(WorldArea.WIDTH, WorldArea.HEIGHT);
- windowFBO = initFBOTexture(GamePanel.WIDTH, GamePanel.HEIGHT);
- GL11.glDisable(GL11.GL_CULL_FACE);
- WorldArea.updateTexture(worldAreaFBO[1]);
- }
- /**
- * This is the root drawing method, which gets called in the drawing loop.
- */
- public static void drawEverything() {
- clear();
- drawWorldToTexture();
- bindFBO(windowFBO);
- viewMatrix(0, 0, 1f);
- drawUiComponent(Game.gameWindow);
- unbindFBO();
- viewMatrix(0, 0, 2f);
- drawFBOTextureToWindow(windowFBO[1]);
- checkErrors();
- }
- public static void drawWorldToTexture() {
- //ugly null testing, and testing the triplebuffer I'm using for making the drawing and game loop separarely threaded.
- if (Game.world != null && Game.world.renderBuffers.getReadable() != null && Game.world.renderBuffers.ready) {
- bindFBO(worldAreaFBO);
- clear();
- worldBuffer = Game.world.renderBuffers.getReadable();
- worldBuffer.claim();
- //from now on, only matrix transformations and basic drawing methods:
- //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();
- //draw the world
- DrawerMethods.initDrawSprites(Resources.fieldSprites);
- 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);
- }
- }
- }
- 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();
- worldBuffer.release();
- unbindFBO();
- }
- }
- 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();
- }
- private static void drawChunk(Chunk c, int x, int y) {
- if (c == null) {
- return;
- }
- int s = WorldArea.GRIDSIZE * Chunk.SIZE;
- int cX = x * s;
- int bX = -WorldArea.GRIDSIZE;
- int cY = y * s;
- int bY = -WorldArea.GRIDSIZE;
- for (int i = 0; i < Chunk.SIZE; i++) {
- bX += WorldArea.GRIDSIZE;
- for (int j = 0; j < Chunk.SIZE; j++) {
- bY += WorldArea.GRIDSIZE;
- drawWall(cX + bX, cY + bY,
- c.getBlockHealth(i, j),
- c.getBlockHardness(i, j),
- c.getBlockMineralId(i, j));
- }
- bY = -WorldArea.GRIDSIZE;
- }
- }
- private static void drawWall(long x, long y, int health, int hardness, int mineralId) {
- if (x < -WorldArea.GRIDSIZE - viewX || x > WorldArea.WIDTH + WorldArea.GRIDSIZE - viewX
- || y < -WorldArea.GRIDSIZE - viewY || y > WorldArea.HEIGHT + WorldArea.GRIDSIZE - viewY) {
- return;
- }
- setColor(1f, 1f, 1f, 1f);
- drawSpriteImage(x, y, 0, PXS, Resources.getWallSprite(health, hardness, mineralId));
- float r, g, b;
- if (mineralId > 0) {
- //draw the ores
- r = OreTypes.ores[mineralId].red;
- g = OreTypes.ores[mineralId].green;
- b = OreTypes.ores[mineralId].blue;
- setColor(r, g, b, 1f);
- long key = General.coordToHash(x, y);
- oreDrawerRandom.setSeed(key);
- for (int i = 0; i < 3; i++) {
- SpriteImage drawnSprite;
- if (OreTypes.ores[mineralId].isOre) {
- int spriteId = oreDrawerRandom.nextInt(Resources.oreSprites.length);
- drawnSprite = Resources.oreSprites[spriteId];
- } else {
- int spriteId = oreDrawerRandom.nextInt(Resources.gemSprites.length);
- drawnSprite = Resources.gemSprites[spriteId];
- }
- long offsetX = oreDrawerRandom.nextInt(WorldArea.GRIDSIZE - drawnSprite.getWidth());
- long offsetY = oreDrawerRandom.nextInt(WorldArea.GRIDSIZE - drawnSprite.getHeight());
- drawSpriteImage(x + offsetX, y + offsetY, 0, PXS, drawnSprite);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement