Przeglądaj źródła

Refactoring and move to InternalFile instead of filename

Tankernn 8 lat temu
rodzic
commit
4d2b022ab9

+ 1 - 1
src/main/java/eu/tankernn/gameEngine/MainLoop.java

@@ -98,7 +98,7 @@ public class MainLoop {
 		ParticleMaster particleMaster = new ParticleMaster(loader, camera.getProjectionMatrix());
 		TextMaster textMaster = new TextMaster(loader);
 
-		FontType font = new FontType(loader.loadTexture("arial.png"), "arial.fnt");
+		FontType font = new FontType(loader.loadTexture("arial.png"), new InternalFile("arial.fnt"));
 		GUIText text = new GUIText("Sample text", 1, font, new Vector2f(0.5f, 0.0f), 0.5f, false).setColor(0, 1, 0);
 		GUIText fpsText = new GUIText("FPS: ", 1, font, new Vector2f(0.0f, 0.0f), 0.5f, false).setColor(1, 1, 1);
 		textMaster.loadText(fpsText);

+ 6 - 2
src/main/java/eu/tankernn/gameEngine/loader/Loader.java

@@ -116,11 +116,15 @@ public class Loader {
 	 * @return The texture ID
 	 * @throws FileNotFoundException
 	 */
-	public Texture loadTexture(String filename) throws FileNotFoundException {
-		Texture texture = Texture.newTexture(new InternalFile(filename)).create();
+	public Texture loadTexture(InternalFile file) {
+		Texture texture = Texture.newTexture(file).create();
 		textures.add(texture);
 		return texture;
 	}
+	
+	public Texture loadTexture(String filename) throws FileNotFoundException {
+		return loadTexture(new InternalFile(filename));
+	}
 
 	/**
 	 * Creates a new cube map from the images specified. File 0: Right face File

+ 2 - 1
src/main/java/eu/tankernn/gameEngine/loader/font/FontType.java

@@ -1,6 +1,7 @@
 package eu.tankernn.gameEngine.loader.font;
 
 import eu.tankernn.gameEngine.loader.textures.Texture;
+import eu.tankernn.gameEngine.util.InternalFile;
 
 /**
  * Represents a font. It holds the font's texture atlas as well as having the
@@ -24,7 +25,7 @@ public class FontType {
 	 *            - the font file containing information about each character in
 	 *            the texture atlas.
 	 */
-	public FontType(Texture texture, String fontFile) {
+	public FontType(Texture texture, InternalFile fontFile) {
 		this.textureAtlas = texture;
 		this.loader = new TextMeshCreator(fontFile);
 	}

+ 8 - 7
src/main/java/eu/tankernn/gameEngine/loader/font/MetaFile.java

@@ -2,12 +2,13 @@ package eu.tankernn.gameEngine.loader.font;
 
 import java.io.BufferedReader;
 import java.io.IOException;
-import java.io.InputStreamReader;
 import java.util.HashMap;
 import java.util.Map;
 
 import org.lwjgl.opengl.Display;
 
+import eu.tankernn.gameEngine.util.InternalFile;
+
 /**
  * Provides functionality for getting the values from a font file.
  * 
@@ -45,12 +46,12 @@ public class MetaFile {
 	/**
 	 * Opens a font file in preparation for reading.
 	 * 
-	 * @param file
+	 * @param fontFile
 	 *            - the font file.
 	 */
-	protected MetaFile(String file) {
+	protected MetaFile(InternalFile fontFile) {
 		this.aspectRatio = (double) Display.getWidth() / (double) Display.getHeight();
-		openFile(file);
+		openFile(fontFile);
 		loadPaddingData();
 		loadLineSizes();
 		int imageWidth = getValueOfVariable("scaleW");
@@ -132,12 +133,12 @@ public class MetaFile {
 	/**
 	 * Opens the font file, ready for reading.
 	 * 
-	 * @param file
+	 * @param fontFile
 	 *            - the font file.
 	 */
-	private void openFile(String file) {
+	private void openFile(InternalFile fontFile) {
 		try {
-			reader = new BufferedReader(new InputStreamReader(MetaFile.class.getResourceAsStream("/" + file)));
+			reader = fontFile.getReader();
 		} catch (Exception e) {
 			e.printStackTrace();
 			System.err.println("Couldn't read font meta file!");

+ 4 - 2
src/main/java/eu/tankernn/gameEngine/loader/font/TextMeshCreator.java

@@ -3,6 +3,8 @@ package eu.tankernn.gameEngine.loader.font;
 import java.util.ArrayList;
 import java.util.List;
 
+import eu.tankernn.gameEngine.util.InternalFile;
+
 public class TextMeshCreator {
 
 	protected static final double LINE_HEIGHT = 0.03f;
@@ -10,8 +12,8 @@ public class TextMeshCreator {
 
 	private MetaFile metaData;
 
-	protected TextMeshCreator(String metaFile) {
-		metaData = new MetaFile(metaFile);
+	protected TextMeshCreator(InternalFile fontFile) {
+		metaData = new MetaFile(fontFile);
 	}
 
 	protected TextMeshData createTextMesh(GUIText text) {

+ 12 - 6
src/main/java/eu/tankernn/gameEngine/renderEngine/shaders/ShaderProgram.java

@@ -1,7 +1,7 @@
 package eu.tankernn.gameEngine.renderEngine.shaders;
 
 import java.io.BufferedReader;
-import java.io.InputStreamReader;
+import java.io.FileNotFoundException;
 import java.util.List;
 
 import org.apache.commons.lang3.ArrayUtils;
@@ -10,6 +10,7 @@ import org.lwjgl.opengl.GL20;
 import org.lwjgl.util.vector.Vector3f;
 
 import eu.tankernn.gameEngine.entities.Light;
+import eu.tankernn.gameEngine.util.InternalFile;
 
 public class ShaderProgram {
 
@@ -23,8 +24,14 @@ public class ShaderProgram {
 	protected UniformVec3[] attenuation;
 
 	public ShaderProgram(String vertexFile, String fragmentFile, String... inVariables) {
-		int vertexShaderID = loadShader(vertexFile, GL20.GL_VERTEX_SHADER);
-		int fragmentShaderID = loadShader(fragmentFile, GL20.GL_FRAGMENT_SHADER);
+		int vertexShaderID, fragmentShaderID;
+		try {
+			vertexShaderID = loadShader(new InternalFile(vertexFile), GL20.GL_VERTEX_SHADER);
+			fragmentShaderID = loadShader(new InternalFile(fragmentFile), GL20.GL_FRAGMENT_SHADER);
+		} catch (FileNotFoundException e) {
+			e.printStackTrace();
+			return;
+		}
 		programID = GL20.glCreateProgram();
 		GL20.glAttachShader(programID, vertexShaderID);
 		GL20.glAttachShader(programID, fragmentShaderID);
@@ -100,11 +107,10 @@ public class ShaderProgram {
 		GL20.glBindAttribLocation(programID, attributeId, variable);
 	}
 
-	private int loadShader(String file, int type) {
+	private int loadShader(InternalFile file, int type) {
 		StringBuilder shaderSource = new StringBuilder();
 		try {
-			BufferedReader reader = new BufferedReader(
-					new InputStreamReader(ShaderProgram.class.getResourceAsStream(file)));
+			BufferedReader reader = file.getReader();
 			String line;
 			while ((line = reader.readLine()) != null) {
 				shaderSource.append(line).append("//\n");

+ 45 - 56
src/main/java/eu/tankernn/gameEngine/util/MousePicker.java

@@ -19,21 +19,21 @@ import eu.tankernn.gameEngine.terrains.TerrainPack;
 public class MousePicker {
 	private static final int RECURSION_COUNT = 200;
 	private static final float RAY_RANGE = 600;
-	
+
 	private Vector3f currentRay = new Vector3f();
-	
+
 	private Matrix4f projectionMatrix;
 	private Matrix4f viewMatrix;
 	private Camera camera;
-	
+
 	private Vector3f currentTerrainPoint;
 	private List<Entity3D> entities;
 	private Entity3D currentEntity;
-	
+
 	private List<GuiTexture> guis;
 	private GuiTexture currentGui;
 	private TerrainPack terrains;
-	
+
 	public MousePicker(Camera cam, Matrix4f projection, List<Entity3D> entities, List<GuiTexture> guis) {
 		camera = cam;
 		projectionMatrix = projection;
@@ -41,23 +41,23 @@ public class MousePicker {
 		this.entities = entities;
 		this.guis = guis;
 	}
-	
+
 	public Entity3D getCurrentEntity() {
 		return currentEntity;
 	}
-	
+
 	public Vector3f getCurrentTerrainPoint() {
 		return currentTerrainPoint;
 	}
-	
+
 	public Vector3f getCurrentRay() {
 		return currentRay;
 	}
-	
+
 	public GuiTexture getCurrentGui() {
 		return currentGui;
 	}
-	
+
 	public void update(TerrainPack terrains) {
 		this.terrains = terrains;
 		viewMatrix = camera.getViewMatrix();
@@ -68,9 +68,9 @@ public class MousePicker {
 		} else {
 			currentTerrainPoint = null;
 		}
-		
+
 		boolean foundTarget = false;
-		for (Entity3D e: entities) {
+		for (Entity3D e : entities) {
 			if (entityInstersect(e) && !foundTarget) {
 				e.setScale(2);
 				foundTarget = true;
@@ -79,7 +79,7 @@ public class MousePicker {
 			}
 		}
 	}
-	
+
 	private Vector3f calculateMouseRay() {
 		float mouseX = Mouse.getX();
 		float mouseY = Mouse.getY();
@@ -89,7 +89,7 @@ public class MousePicker {
 		Vector3f worldRay = toWorldCoords(eyeCoords);
 		return worldRay;
 	}
-	
+
 	private Vector3f toWorldCoords(Vector4f eyeCoords) {
 		Matrix4f invertedView = Matrix4f.invert(viewMatrix, null);
 		Vector4f rayWorld = Matrix4f.transform(invertedView, eyeCoords, null);
@@ -97,95 +97,92 @@ public class MousePicker {
 		mouseRay.normalise();
 		return mouseRay;
 	}
-	
+
 	private Vector4f toEyeCoords(Vector4f clipCoords) {
 		Matrix4f invertedProjection = Matrix4f.invert(projectionMatrix, null);
 		Vector4f eyeCoords = Matrix4f.transform(invertedProjection, clipCoords, null);
 		return new Vector4f(eyeCoords.x, eyeCoords.y, -1f, 0f);
 	}
-	
+
 	private Vector2f getNormalisedDeviceCoordinates(float mouseX, float mouseY) {
 		float x = (2.0f * mouseX) / Display.getWidth() - 1f;
 		float y = (2.0f * mouseY) / Display.getHeight() - 1f;
 		return new Vector2f(x, y);
 	}
-	
+
 	// GUI Intersect
-	
+
 	private GuiTexture calculateGuiTexture() {
 		float mouseX = Mouse.getX();
 		float mouseY = Mouse.getY();
 		Vector2f mouseCoords = getNormalisedDeviceCoordinates(mouseX, mouseY);
-		
+
 		for (GuiTexture gui : guis) {
 			float posX = gui.getPosition().x;
 			float posY = gui.getPosition().y;
 			float scaleX = gui.getScale().x;
 			float scaleY = gui.getScale().y;
-			
-			if (mouseCoords.x > posX - scaleX && mouseCoords.x < posX + scaleX && mouseCoords.y > posY - scaleY && mouseCoords.y < posY + scaleY) {
+
+			if (mouseCoords.x > posX - scaleX && mouseCoords.x < posX + scaleX && mouseCoords.y > posY - scaleY
+					&& mouseCoords.y < posY + scaleY) {
 				return gui;
 			}
 		}
 		return null;
 	}
-	
+
 	// #### Entity intersect ####
-	
+
 	public boolean entityInstersect(Entity3D entity) {
 		AABB box = entity.getBoundingBox();
 		Vector3f dirfrac = new Vector3f();
-		
+
 		dirfrac.x = 1.0f / currentRay.x;
 		dirfrac.y = 1.0f / currentRay.y;
 		dirfrac.z = 1.0f / currentRay.z;
-		// lb is the corner of AABB with minimal coordinates - left bottom, rt is maximal corner
+		// lb is the corner of AABB with minimal coordinates - left bottom, rt
+		// is maximal corner
 		// camera.getPosition() is origin of ray
-		
+
 		float t1 = (box.getLb().x - camera.getPosition().x) * dirfrac.x;
 		float t2 = (box.getRt().x - camera.getPosition().x) * dirfrac.x;
 		float t3 = (box.getLb().y - camera.getPosition().y) * dirfrac.y;
 		float t4 = (box.getRt().y - camera.getPosition().y) * dirfrac.y;
 		float t5 = (box.getLb().z - camera.getPosition().z) * dirfrac.z;
 		float t6 = (box.getRt().z - camera.getPosition().z) * dirfrac.z;
-		
+
 		float tmin = Math.max(Math.max(Math.min(t1, t2), Math.min(t3, t4)), Math.min(t5, t6));
 		float tmax = Math.min(Math.min(Math.max(t1, t2), Math.max(t3, t4)), Math.max(t5, t6));
-		
-		// if tmax < 0, ray (line) is intersecting AABB, but whole AABB is behind us
-		if (tmax < 0)
-		{
+
+		// if tmax < 0, ray (line) is intersecting AABB, but whole AABB is
+		// behind us
+		if (tmax < 0) {
 			return false;
 		}
-		
+
 		// if tmin > tmax, ray doesn't intersect AABB
-		if (tmin > tmax)
-		{
+		if (tmin > tmax) {
 			return false;
 		}
-		
+
 		return true;
 	}
-	
+
 	// #### Terrain intersect ####
-	
+
 	private Vector3f getPointOnRay(Vector3f ray, float distance) {
 		Vector3f camPos = camera.getPosition();
 		Vector3f start = new Vector3f(camPos.x, camPos.y, camPos.z);
 		Vector3f scaledRay = new Vector3f(ray.x * distance, ray.y * distance, ray.z * distance);
 		return Vector3f.add(start, scaledRay, null);
 	}
-	
+
 	private Vector3f binarySearch(int count, float start, float finish, Vector3f ray) {
 		float half = start + ((finish - start) / 2f);
 		if (count >= RECURSION_COUNT) {
 			Vector3f endPoint = getPointOnRay(ray, half);
 			Terrain terrain = getTerrain(endPoint.getX(), endPoint.getZ());
-			if (terrain != null) {
-				return endPoint;
-			} else {
-				return null;
-			}
+			return terrain != null ? endPoint : null;
 		}
 		if (intersectionInRange(start, half, ray)) {
 			return binarySearch(count + 1, start, half, ray);
@@ -193,30 +190,22 @@ public class MousePicker {
 			return binarySearch(count + 1, half, finish, ray);
 		}
 	}
-	
+
 	private boolean intersectionInRange(float start, float finish, Vector3f ray) {
 		Vector3f startPoint = getPointOnRay(ray, start);
 		Vector3f endPoint = getPointOnRay(ray, finish);
-		if (!isUnderGround(startPoint) && isUnderGround(endPoint)) {
-			return true;
-		} else {
-			return false;
-		}
+		return !isUnderGround(startPoint) && isUnderGround(endPoint);
 	}
-	
+
 	private boolean isUnderGround(Vector3f testPoint) {
 		Terrain terrain = getTerrain(testPoint.getX(), testPoint.getZ());
 		float height = 0;
 		if (terrain != null) {
 			height = terrain.getHeightOfTerrain(testPoint.getX(), testPoint.getZ());
 		}
-		if (testPoint.y < height) {
-			return true;
-		} else {
-			return false;
-		}
+		return testPoint.y < height;
 	}
-	
+
 	private Terrain getTerrain(float worldX, float worldZ) {
 		return terrains.getTerrainByWorldPos(worldX, worldZ);
 	}