com.telinc1.rpjg.util.DrawingUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.telinc1.rpjg.util.DrawingUtils.java

Source

/*
 * Copyright 2015-2016 Telinc1
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.telinc1.rpjg.util;

import org.lwjgl.opengl.GL11;

import com.telinc1.rpjg.map.event.EventTextured;
import com.telinc1.rpjg.texture.Texture;
import com.telinc1.rpjg.texture.TextureLoader;
import com.telinc1.rpjg.texture.TextureMapper;

import static com.telinc1.rpjg.reference.GameOptions.GRID_SIZE;

/**
 * Utility class to draw graphics as efficiently as possible
 * while still using immediate mode (for OpenGL 1.1 support).
 */
public class DrawingUtils {
    /**
     * All of the color bits for OpenGL.
     */
    public static final int GL_COLOR_BITS = GL11.GL_RED_BITS | GL11.GL_GREEN_BITS | GL11.GL_BLUE_BITS
            | GL11.GL_ALPHA_BITS;

    /**
     * The currently bound texture.
     */
    public static int boundTexture = 0;

    /**
     * The drawing mode we're currently in.
     */
    private static int drawMode = -1;

    /**
     * Binds the given texture.
     * 
     * @param texture - The texture to bind.
     */
    public static void bindTexture(Texture texture) {
        DrawingUtils.boundTexture = texture.getID();
        GL11.glBindTexture(texture.getTarget(), texture.getID());
    }

    /**
     * Binds a texture by ID.
     * 
     * @param texture - The ID of the texture to bind.
     */
    public static void bindTexture(int texture) {
        DrawingUtils.boundTexture = texture;
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);
    }

    /**
     * Unbinds any currently bound textures.
     */
    public static void unbindTextures() {
        DrawingUtils.bindTexture(0);
    }

    /**
     * Begins to draw in a certain mode.
     * 
     * @param mode - The mode to draw in.
     */
    public static void startDrawing(int mode) {
        if (DrawingUtils.drawMode == -1) {
            DrawingUtils.drawMode = mode;
            GL11.glBegin(mode);
        }
    }

    /**
     * Stops drawing.
     */
    public static void stopDrawing() {
        if (DrawingUtils.drawMode != -1) {
            GL11.glEnd();
            DrawingUtils.drawMode = -1;
        }
    }

    /**
     * Draws a rectangle with the given texture.
     * 
     * @param texture - The texture of the rectangle.
     * @param x - The X coordinate of the rectangle.
     * @param y - The Y coordinate of the rectangle.
     * @param width - The width of the rectangle.
     * @param height - The height of the rectangle.
     */
    public static void drawTexturedQuad(Texture texture, int x, int y, int width, int height) {
        if (texture.getID() != DrawingUtils.boundTexture) {
            DrawingUtils.bindTexture(texture);
        }

        DrawingUtils.drawTexturedQuad(x, y, width, height);
    }

    /**
     * Draws a rectangle at the given coordinates, but
     * assumes the texture for it has already been bound.
     * 
     * @param x - The X coordinate of the rectangle.
     * @param y - The Y coordinate of the rectangle.
     * @param width - The width of the rectangle.
     * @param height - The height of the rectangle.
     */
    public static void drawTexturedQuad(int x, int y, int width, int height) {
        DrawingUtils.startDrawing(GL11.GL_QUADS);
        GL11.glTexCoord2f(0, 0);
        GL11.glVertex2i(x, y);

        GL11.glTexCoord2f(1, 0);
        GL11.glVertex2i(x + width, y);

        GL11.glTexCoord2f(1, 1);
        GL11.glVertex2i(x + width, y + height);

        GL11.glTexCoord2f(0, 1);
        GL11.glVertex2i(x, y + height);
        DrawingUtils.stopDrawing();
    }

    /**
     * Draws a textured rectangle, but uses a texture mapper
     * to pull its texture from an atlas.
     * 
     * @param mapper - The {@link TextureMapper texture mapper} to use.
     * @param x - The X coordinate of the rectangle.
     * @param y - The Y coordinate of the rectangle.
     * @param width - The width of the rectangle.
     * @param height - The height of the rectangle.
     */
    public static void drawMappedQuad(TextureMapper mapper, int x, int y, int width, int height) {
        if (mapper.getTexture().getID() != DrawingUtils.boundTexture) {
            DrawingUtils.bindTexture(mapper.getTexture());
        }

        DrawingUtils.startDrawing(GL11.GL_QUADS);
        GL11.glTexCoord2f(mapper.getU(), mapper.getV());
        GL11.glVertex2i(x, y);

        GL11.glTexCoord2f(mapper.getU2(), mapper.getV());
        GL11.glVertex2i(x + width, y);

        GL11.glTexCoord2f(mapper.getU2(), mapper.getV2());
        GL11.glVertex2i(x + width, y + height);

        GL11.glTexCoord2f(mapper.getU(), mapper.getV2());
        GL11.glVertex2i(x, y + height);
        DrawingUtils.stopDrawing();
    }

    /**
     * Draws a textured rectangle with a texture mapper,
     * but doesn't start of end drawing.
     * 
     * @param mapper - The {@link TextureMapper texture mapper} to use.
     * @param x - The X coordinate of the rectangle.
     * @param y - The Y coordinate of the rectangle.
     * @param width - The width of the rectangle.
     * @param height - The height of the rectangle.
     */
    public static void drawConsecutiveMappedQuad(TextureMapper mapper, int x, int y, int width, int height) {
        GL11.glTexCoord2f(mapper.getU(), mapper.getV());
        GL11.glVertex2i(x, y);

        GL11.glTexCoord2f(mapper.getU2(), mapper.getV());
        GL11.glVertex2i(x + width, y);

        GL11.glTexCoord2f(mapper.getU2(), mapper.getV2());
        GL11.glVertex2i(x + width, y + height);

        GL11.glTexCoord2f(mapper.getU(), mapper.getV2());
        GL11.glVertex2i(x, y + height);
    }

    /**
     * Draws a tile from the current tileset.
     * 
     * @param tile - The tile index to draw.
     * @param x - The grid-aligned X coordinate.
     * @param y - The grid-aligned Y coordinate.
     */
    public static void drawTexturedTile(int tile, int x, int y) {
        TextureMapper mapper = new TextureMapper(TextureLoader.tilesetTexture, (tile % GRID_SIZE) * GRID_SIZE,
                (tile / GRID_SIZE) * GRID_SIZE, GRID_SIZE, GRID_SIZE);

        x *= GRID_SIZE;
        y *= GRID_SIZE;

        DrawingUtils.drawConsecutiveMappedQuad(mapper, x, y, GRID_SIZE, GRID_SIZE);
    }

    /**
     * Draws the given event.<br />
     * Respects the visibility flag ({@link EventTextured#visible}).
     * 
     * @param event
     */
    public static void drawTexturedEvent(EventTextured event) {
        if (!event.isVisible()) {
            return;
        }

        TextureMapper mapper = new TextureMapper(event.getTexture(), (event.getIconID() % GRID_SIZE) * GRID_SIZE,
                (event.getIconID() / GRID_SIZE) * GRID_SIZE, GRID_SIZE, GRID_SIZE);

        int x = event.getPixelX();
        int y = event.getPixelY();

        DrawingUtils.drawMappedQuad(mapper, x, y, GRID_SIZE, GRID_SIZE);
    }
}