Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved asset loading and simplified rendering #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.joml.Matrix4f;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL30.glBindVertexArray;

public class Renderer {

Expand Down Expand Up @@ -37,6 +38,9 @@ public void init(Window window) throws Exception {
shaderProgram.createUniform("projectionMatrix");
shaderProgram.createUniform("modelViewMatrix");
shaderProgram.createUniform("texture_sampler");

//only one program is ever used we can bind in init()
shaderProgram.bind();
}

public void clear() {
Expand All @@ -45,32 +49,27 @@ public void clear() {

public void render(Window window, Camera camera, GameObject[] gameObjects) {
clear();

if (window.isResized()) {
glViewport(0, 0, window.getWidth(), window.getHeight());
window.setResized(false);
}

shaderProgram.bind();

// Update projection Matrix
Matrix4f projectionMatrix = transformation.getProjectionMatrix(FOV, window.getWidth(), window.getHeight(), Z_NEAR, Z_FAR);
shaderProgram.setUniform("projectionMatrix", projectionMatrix);

// Update view Matrix
Matrix4f viewMatrix = transformation.getViewMatrix(camera);

shaderProgram.setUniform("texture_sampler", 0);
// Render each gameItem
for (GameObject gameObject : gameObjects) {
// Set model view matrix for this item
Matrix4f modelViewMatrix = transformation.getModelViewMatrix(gameObject, viewMatrix);
shaderProgram.setUniform("modelViewMatrix", modelViewMatrix);
// Render the mesh for this game item
gameObject.getMesh().render();
glBindTexture(GL_TEXTURE_2D, gameObject.getMesh().getCurrentTextureId());
glBindVertexArray(gameObject.getMesh().getVaoId());
glDrawElements(GL_TRIANGLES, gameObject.getMesh().getVertexCount(), GL_UNSIGNED_INT, 0);
}

shaderProgram.unbind();
}

public void cleanup() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import java.util.List;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL13.GL_TEXTURE0;
import static org.lwjgl.opengl.GL13.glActiveTexture;
import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER;
import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER;
import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW;
Expand Down Expand Up @@ -172,24 +170,6 @@ public int getVertexCount() {
return vertexCount;
}

public void render() {
// Activate firs texture bank
glActiveTexture(GL_TEXTURE0);
// Bind the texture
if(hasSpriteSheet)
glBindTexture(GL_TEXTURE_2D, spriteSheet.getTextures()[currentFrame].getId());
else
glBindTexture(GL_TEXTURE_2D, texture.getId());

// Draw the mesh
glBindVertexArray(getVaoId());

glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0);

// Restore state
glBindVertexArray(0);
}

public void cleanUp() {
glDisableVertexAttribArray(0);

Expand Down Expand Up @@ -221,6 +201,10 @@ public int getCurrentFrame() {
return currentFrame;
}

public int getCurrentTextureId(){
return hasSpriteSheet ? spriteSheet.getTextures()[currentFrame].getId() : texture.getId();
}

public void setCurrentFrame(int currentFrame) {
this.currentFrame = currentFrame;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,70 +5,59 @@
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class SpriteSheet {

public Texture[] textures;

public SpriteSheet(String filename, int size) {
BufferedImage img = null;
try {
img = ImageIO.read(new File(filename));
} catch(IOException e) {
Log.engine().error(e.getMessage());
}

int rows = img.getHeight() / size;
int cols = img.getWidth() / size;

BufferedImage[] sprites = new BufferedImage[rows * cols];

for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
sprites[(i * cols) + j] = img.getSubimage(
j * size,
i * size,
size,
size
);
BufferedImage img = ImageIO.read(new File(filename));

int rows = img.getHeight() / size;
int cols = img.getWidth() / size;

BufferedImage[] sprites = new BufferedImage[rows * cols];

for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sprites[(i * cols) + j] = img.getSubimage(
j * size,
i * size,
size,
size
);
}
}
}

try {
textures = Texture.loadTexture(sprites);
} catch(Exception e) {
} catch (Exception e) {
Log.engine().error(e.getMessage());
}
}

public SpriteSheet(String filename) {
BufferedImage img = null;
try {
img = ImageIO.read(new File(filename));
} catch(IOException e) {
Log.engine().error(e.getMessage());
}

int rows = img.getHeight();
int cols = img.getWidth();

BufferedImage[] sprites = new BufferedImage[1];

for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
sprites[(i * cols) + j] = img.getSubimage(
j * img.getWidth(),
i * img.getHeight(),
img.getWidth(),
img.getHeight()
);
BufferedImage img = ImageIO.read(new File(filename));

int rows = img.getHeight();
int cols = img.getWidth();

BufferedImage[] sprites = new BufferedImage[1];

for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sprites[(i * cols) + j] = img.getSubimage(
j * img.getWidth(),
i * img.getHeight(),
img.getWidth(),
img.getHeight()
);
}
}
}

try {
textures = Texture.loadTexture(sprites);
} catch(Exception e) {
} catch (Exception e) {
Log.engine().error(e.getMessage());
}
}
Expand All @@ -77,4 +66,4 @@ public Texture[] getTextures() {
return textures;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

public class Layer {

private GameObject layer;
private int[][] layerCoords;
private int tileWidth, tileHeight;
private final int[][] layerCoords;
private final int tileWidth;
private final int tileHeight;

public Layer(int[][] layerCoords, int x, int y, int tileWidth, int tileHeight, ArrayList<TileSet> tileSets) {
this.layerCoords = layerCoords;
Expand All @@ -27,24 +29,32 @@ public Layer(int[][] layerCoords, int x, int y, int tileWidth, int tileHeight, A

// Make the layer
try {
for(int b = 0; b < y; b++) {
for(int a = 0; a < x; a++) {
if(layerCoords[a][b] > 0) {
HashMap<String, BufferedImage> imageCache = new HashMap<>();
String basePath = String.format("src%smain%sresources%smaps%s", File.separator, File.separator, File.separator, File.separator);
for (int b = 0; b < y; b++) {
for (int a = 0; a < x; a++) {
if (layerCoords[a][b] > 0) {
// Get the correct tileSet
TileSet currentTileSet = null;
for (TileSet tileSet : tileSets) {
if (layerCoords[a][b] >= tileSet.getFirstGID())
currentTileSet = tileSet;
}
BufferedImage tileSet = ImageIO.read(new File("src" + File.separator + "main"
+ File.separator + "resources" + File.separator +
"maps" + File.separator + currentTileSet.getSource()));
String path = basePath + currentTileSet.getSource();

BufferedImage tileSet;
if (imageCache.containsKey(path)) {
tileSet = imageCache.get(path);
} else {
tileSet = ImageIO.read(new File(path));
imageCache.put(path, tileSet);
}

// Get subImage from tileset
BufferedImage subImage;
for(int d = 0; d < currentTileSet.getGIDs()[0].length; d++) {
for(int c = 0; c < currentTileSet.getGIDs().length; c++) {
if(currentTileSet.getGIDs()[c][d] == layerCoords[a][b]) {
for (int d = 0; d < currentTileSet.getGIDs()[0].length; d++) {
for (int c = 0; c < currentTileSet.getGIDs().length; c++) {
if (currentTileSet.getGIDs()[c][d] == layerCoords[a][b]) {
subImage = tileSet.getSubimage(c * currentTileSet.getTileWidth(), d * currentTileSet.getTileHeight(), currentTileSet.getTileWidth(), currentTileSet.getTileHeight());
layerTex = joinBufferedImage(layerTex, subImage, b, a);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.File;
import java.util.ArrayList;


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

Expand All @@ -16,7 +17,7 @@ public class MapHost {
private String tmxTarget;
private NodeList rawTileData;
private NodeList rawImageData;
private ArrayList<TileSet> tileSets = new ArrayList<TileSet>();
private ArrayList<TileSet> tileSets = new ArrayList<>();
private static Map map;

public MapHost(String tmxTarget){
Expand Down