Anonymous avatar Anonymous committed 143b5ea

added silly not yet functional TileGame demo

Comments (0)

Files changed (3)

src/main/java/sandbox/common/TileSet.java

+package sandbox.common;
+
+import org.newdawn.slick.opengl.Texture;
+import org.newdawn.slick.opengl.TextureLoader;
+import org.newdawn.slick.util.ResourceLoader;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+public class TileSet {
+    private Texture texture;
+
+    private int tileWidth;
+    private int tileHeight;
+    private int tileColumns;
+    private int tileRows;
+    private int mapWidth;
+    private int mapHeight;
+
+
+    public TileSet(String path, String type, int tileWidth, int tileHeight,
+                   int tileColumns, int tileRows) throws IOException {
+        setTexture(loadTexture(path, type));
+        setTileWidth(tileWidth);
+        setTileHeight(tileHeight);
+        setTileColumns(tileColumns);
+        setTileRows(tileRows);
+
+        setMapWidth(tileWidth * tileColumns);
+        setMapHeight(tileHeight * tileRows);
+    }
+
+    private Texture loadTexture(String path, String type) throws IOException {
+        return TextureLoader.getTexture(type, ResourceLoader.getResourceAsStream(path), true);
+    }
+
+    /**
+     *
+     * @param x - X Coordinate of tile from lower left
+     * @param y - Y Coordinate of tile from lower left
+     * @return lower left and upper right tile coords, packed in float array
+     */
+    public float[] getTileCoords(int x, int y) {
+        float x1 = (float) x * tileWidth;
+        float y1 = (float) y * tileHeight;
+
+        float[] coord = new float[] {
+                x1 / mapWidth, y1 / mapHeight,
+                (x1 + tileWidth) / mapWidth, (y1 + tileHeight) / mapHeight
+        };
+
+//        System.out.printf("getTileCoords(%d, %d) => %s%n", x, y, Arrays.toString(coord));
+        return coord;
+
+    }
+
+    // ==========================
+    // Boilerplate
+
+    public Texture getTexture() {
+        return texture;
+    }
+
+    public void setTexture(Texture texture) {
+        this.texture = texture;
+    }
+
+    public int getTileWidth() {
+        return tileWidth;
+    }
+
+    public void setTileWidth(int tileWidth) {
+        this.tileWidth = tileWidth;
+    }
+
+    public int getTileHeight() {
+        return tileHeight;
+    }
+
+    public void setTileHeight(int tileHeight) {
+        this.tileHeight = tileHeight;
+    }
+
+    public int getTileColumns() {
+        return tileColumns;
+    }
+
+    public void setTileColumns(int tileColumns) {
+        this.tileColumns = tileColumns;
+    }
+
+    public int getTileRows() {
+        return tileRows;
+    }
+
+    public void setTileRows(int tileRows) {
+        this.tileRows = tileRows;
+    }
+
+    public int getMapWidth() {
+        return mapWidth;
+    }
+
+    public void setMapWidth(int mapWidth) {
+        this.mapWidth = mapWidth;
+    }
+
+    public int getMapHeight() {
+        return mapHeight;
+    }
+
+    public void setMapHeight(int mapHeight) {
+        this.mapHeight = mapHeight;
+    }
+}

src/main/java/sandbox/misc/TileGame.java

+package sandbox.misc;
+
+import org.lwjgl.BufferUtils;
+import org.lwjgl.LWJGLException;
+import org.lwjgl.opengl.Display;
+import org.lwjgl.opengl.DisplayMode;
+import sandbox.common.BaseApp;
+import sandbox.common.TileSet;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.FloatBuffer;
+import java.util.Random;
+
+import static org.lwjgl.opengl.GL11.*;
+
+public class TileGame extends BaseApp {
+
+    TileSet tileset;
+
+    int width  = 50; // tiles wide to draw
+    int height = 36; // tiles high to draw
+
+    ByteBuffer meshBuf = BufferUtils.createByteBuffer(Float.SIZE * width * height * 8);
+    ByteBuffer texBuf  = BufferUtils.createByteBuffer(Float.SIZE * width * height * 8);
+
+    FloatBuffer mesh = meshBuf.asFloatBuffer();
+    FloatBuffer texc = texBuf.asFloatBuffer();
+
+    Random rand = new Random();
+
+    public TileGame(String title) {
+        super(title, 800, 600);
+    }
+
+    public static void main(String[] args) {
+        TileGame s = new TileGame("Tiles!");
+        s.setPerspectiveProjection(false);
+        s.setOrthoScale(MATCH_VIEWPORT);
+        s.setTargetFrameRate(60);
+        s.doMain(args);
+
+    }
+
+    protected void createDisplay() throws LWJGLException {
+        Display.setTitle(getWindowTitle());
+        Display.setVSyncEnabled(false);
+        Display.setDisplayMode(new DisplayMode(getWindowWidth(), getWindowHeight()));
+//        Display.setFullscreen(true);
+        Display.create(getPixelFormat());
+    }
+
+
+    @Override
+    protected void setup() {
+        glEnable(GL_TEXTURE_2D);
+        int tw = 16;
+        int th = 16;
+        int tx = 16;
+        int ty = 16;
+        try {
+            tileset = new TileSet("textures/dftiles.png", "PNG", tw, th, tx, ty);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+        buildMesh(tw, th);
+        populateTiles(0);
+    }
+
+    private void buildMesh(int tw, int th) {
+        mesh.rewind();
+        // add coordinates from lower-left, counterclockwise
+        for (float y = 0.0f; y < getWindowHeight(); y += th)
+            for (float x = 0.0f; x < getWindowWidth(); x += tw) {
+                mesh.put(x);
+                mesh.put(y);
+                mesh.put(x + tw);
+                mesh.put(y);
+                mesh.put(x + tw);
+                mesh.put(y + tw);
+                mesh.put(x);
+                mesh.put(y + tw);
+            }
+    }
+
+    public void putTile(int x, int y, int tx, int ty) {
+//        System.out.printf("putTile(%d, %d, %d, %d)%n", x, y, tx, ty);
+        texc.position((x + y * width) * 8);
+        float[] c = tileset.getTileCoords(tx,ty);
+        texc.put(c[0]);
+        texc.put(c[1]);
+
+        texc.put(c[2]);
+        texc.put(c[1]);
+
+        texc.put(c[2]);
+        texc.put(c[3]);
+
+        texc.put(c[0]);
+        texc.put(c[3]);
+    }
+
+    private void populateTiles(int offset) {
+        for (int y = 0; y < height; y++)
+            for (int x = 0; x < width; x++) {
+                int tx = (x + offset) % 16;
+                int ty = (y + offset) % 16;
+                putTile(x, y, tx, ty);
+            }
+    }
+
+    int framecount = 0;
+    int offset = 0;
+    @Override
+    protected void render() {
+        framecount++;
+        if (framecount % 6 == 0) offset++;
+        populateTiles(offset);
+        tileset.getTexture().bind();
+        glPushMatrix();
+        glClear(GL_COLOR_BUFFER_BIT);
+        glBegin(GL_QUADS);
+        mesh.rewind();
+        texc.rewind();
+        glColor3f(1,1,1);
+        for (int y = 0; y < height; y++)
+            for (int x = 0; x < width; x++) {
+                glTexCoord2f(texc.get(), texc.get());
+                glVertex2f(mesh.get(), mesh.get());
+
+                glTexCoord2f(texc.get(), texc.get());
+                glVertex2f(mesh.get(), mesh.get());
+
+                glTexCoord2f(texc.get(), texc.get());
+                glVertex2f(mesh.get(), mesh.get());
+
+                glTexCoord2f(texc.get(), texc.get());
+                glVertex2f(mesh.get(), mesh.get());
+            }
+        glEnd();
+        glPopMatrix();
+    }
+}
Add a comment to this file

src/main/resources/textures/dftiles.png

Added
New image
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.