diff --git a/.classpath b/.classpath index fb50116..dc39965 100644 --- a/.classpath +++ b/.classpath @@ -2,5 +2,11 @@ + + + + + + diff --git a/lib/jars/lwjgl.jar b/lib/jars/lwjgl.jar new file mode 100644 index 0000000..2617a79 Binary files /dev/null and b/lib/jars/lwjgl.jar differ diff --git a/lib/natives/OpenAL.dll b/lib/natives/OpenAL.dll new file mode 100644 index 0000000..66e7426 Binary files /dev/null and b/lib/natives/OpenAL.dll differ diff --git a/lib/natives/OpenAL32.dll b/lib/natives/OpenAL32.dll new file mode 100644 index 0000000..e5ce1b8 Binary files /dev/null and b/lib/natives/OpenAL32.dll differ diff --git a/lib/natives/glfw.dll b/lib/natives/glfw.dll new file mode 100644 index 0000000..22eb25d Binary files /dev/null and b/lib/natives/glfw.dll differ diff --git a/lib/natives/glfw32.dll b/lib/natives/glfw32.dll new file mode 100644 index 0000000..4a87870 Binary files /dev/null and b/lib/natives/glfw32.dll differ diff --git a/lib/natives/jemalloc.dll b/lib/natives/jemalloc.dll new file mode 100644 index 0000000..06d05e9 Binary files /dev/null and b/lib/natives/jemalloc.dll differ diff --git a/lib/natives/jemalloc32.dll b/lib/natives/jemalloc32.dll new file mode 100644 index 0000000..af40c41 Binary files /dev/null and b/lib/natives/jemalloc32.dll differ diff --git a/lib/natives/libglfw.dylib b/lib/natives/libglfw.dylib new file mode 100644 index 0000000..f0e7de3 Binary files /dev/null and b/lib/natives/libglfw.dylib differ diff --git a/lib/natives/libglfw.so b/lib/natives/libglfw.so new file mode 100644 index 0000000..aa293a9 Binary files /dev/null and b/lib/natives/libglfw.so differ diff --git a/lib/natives/libglfw32.so b/lib/natives/libglfw32.so new file mode 100644 index 0000000..6854b3b Binary files /dev/null and b/lib/natives/libglfw32.so differ diff --git a/lib/natives/libjemalloc.dylib b/lib/natives/libjemalloc.dylib new file mode 100644 index 0000000..95546ad Binary files /dev/null and b/lib/natives/libjemalloc.dylib differ diff --git a/lib/natives/libjemalloc.so b/lib/natives/libjemalloc.so new file mode 100644 index 0000000..978bfeb Binary files /dev/null and b/lib/natives/libjemalloc.so differ diff --git a/lib/natives/libjemalloc32.so b/lib/natives/libjemalloc32.so new file mode 100644 index 0000000..5013da6 Binary files /dev/null and b/lib/natives/libjemalloc32.so differ diff --git a/lib/natives/liblwjgl.dylib b/lib/natives/liblwjgl.dylib new file mode 100644 index 0000000..8c742f1 Binary files /dev/null and b/lib/natives/liblwjgl.dylib differ diff --git a/lib/natives/liblwjgl.so b/lib/natives/liblwjgl.so new file mode 100644 index 0000000..369a0ac Binary files /dev/null and b/lib/natives/liblwjgl.so differ diff --git a/lib/natives/liblwjgl32.so b/lib/natives/liblwjgl32.so new file mode 100644 index 0000000..867bcdc Binary files /dev/null and b/lib/natives/liblwjgl32.so differ diff --git a/lib/natives/libopenal.dylib b/lib/natives/libopenal.dylib new file mode 100644 index 0000000..ad0200d Binary files /dev/null and b/lib/natives/libopenal.dylib differ diff --git a/lib/natives/libopenal.so b/lib/natives/libopenal.so new file mode 100644 index 0000000..af7f325 Binary files /dev/null and b/lib/natives/libopenal.so differ diff --git a/lib/natives/libopenal32.so b/lib/natives/libopenal32.so new file mode 100644 index 0000000..569ba77 Binary files /dev/null and b/lib/natives/libopenal32.so differ diff --git a/lib/natives/lwjgl.dll b/lib/natives/lwjgl.dll new file mode 100644 index 0000000..f1b3e18 Binary files /dev/null and b/lib/natives/lwjgl.dll differ diff --git a/lib/natives/lwjgl32.dll b/lib/natives/lwjgl32.dll new file mode 100644 index 0000000..c5e0437 Binary files /dev/null and b/lib/natives/lwjgl32.dll differ diff --git a/lib/src.zip b/lib/src.zip new file mode 100644 index 0000000..72061fb Binary files /dev/null and b/lib/src.zip differ diff --git a/src/entities/Entity.java b/src/entities/Entity.java deleted file mode 100644 index 2c8ff67..0000000 --- a/src/entities/Entity.java +++ /dev/null @@ -1,6 +0,0 @@ -package entities; - -public abstract class Entity { - - public abstract void tick(); -} diff --git a/src/entities/EntityHandler.java b/src/entities/EntityHandler.java deleted file mode 100644 index a6c517b..0000000 --- a/src/entities/EntityHandler.java +++ /dev/null @@ -1,5 +0,0 @@ -package entities; - -public class EntityHandler { - -} diff --git a/src/entities/Player.java b/src/entities/Player.java deleted file mode 100644 index d16797b..0000000 --- a/src/entities/Player.java +++ /dev/null @@ -1,5 +0,0 @@ -package entities; - -public class Player { - -} diff --git a/src/entity/PlayerBall.java b/src/entity/PlayerBall.java new file mode 100644 index 0000000..c574fac --- /dev/null +++ b/src/entity/PlayerBall.java @@ -0,0 +1,5 @@ +package entity; + +public class PlayerBall { + +} diff --git a/src/game/Game.java b/src/game/Game.java deleted file mode 100644 index 690af3c..0000000 --- a/src/game/Game.java +++ /dev/null @@ -1,22 +0,0 @@ -package game; - -import javax.swing.JFrame; -import gfx.Screen; -import level.LevelHandler; - -public class Game extends JFrame{ - - private Screen screen; - - public Game() { - setSize(800, 600); - setTitle(screen.getLevelName()); - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - setLocationRelativeTo(null); - setVisible(true); - } - - public void setLevel(LevelHandler newLevel) { - screen = new Screen(newLevel); - } -} diff --git a/src/game/InputHandler.java b/src/game/InputHandler.java deleted file mode 100644 index 37b854e..0000000 --- a/src/game/InputHandler.java +++ /dev/null @@ -1,4 +0,0 @@ -package game; -public class InputHandler { - -} diff --git a/src/game/Launcher.java b/src/game/Launcher.java deleted file mode 100644 index ac7240f..0000000 --- a/src/game/Launcher.java +++ /dev/null @@ -1,14 +0,0 @@ -package game; - -import level.LevelHandler; - -public class Launcher { - - public static void main(String[] args) { - //new Menu().run(); - - // Testing file loading - LevelHandler test = new LevelHandler("res/levels/test.txt"); - test.printTiles(); - } -} diff --git a/src/game/Main.java b/src/game/Main.java new file mode 100644 index 0000000..35138fd --- /dev/null +++ b/src/game/Main.java @@ -0,0 +1,118 @@ +package game; + +import static org.lwjgl.glfw.GLFW.*; +import static org.lwjgl.opengl.GL11.*; +import static org.lwjgl.opengl.GL13.*; +import static org.lwjgl.system.MemoryUtil.*; + +import org.lwjgl.glfw.GLFWVidMode; +import org.lwjgl.opengl.GL; + +import game.graphics.Shader; +import game.input.Input; +import game.level.Level; +import game.math.Matrix4f; + +public class Main implements Runnable { + + private int width = 1280; + private int height = 720; + private boolean running = false; + private Thread gameThread; + private long gameWindow; + private Level level; + + //Must be explicitly declared to avoid being garbage collected + private Input input = new Input(); + + public void start() { + running = true; + gameThread = new Thread(this, "Game Noodle"); + gameThread.start(); + } + + private void init() { + glfwWindowHint(GLFW_RESIZABLE, GL_TRUE); + + // Windowed mode, don't share resources + gameWindow = glfwCreateWindow(width, height, "Game Noodle", NULL, NULL); + + GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor()); + glfwSetWindowPos(gameWindow, (vidmode.width() - width) / 2, (vidmode.height() - height) / 2); + glfwSetKeyCallback(gameWindow, input); + glfwMakeContextCurrent(gameWindow); + glfwShowWindow(gameWindow); + GL.createCapabilities(); + + glEnable(GL_DEPTH_TEST); + glActiveTexture(GL_TEXTURE1); + System.out.println("Loaded OpenGL Version: " + glGetString(GL_VERSION)); + Shader.loadAll(); + + // Set all shader uniform variables here + Matrix4f projectionMatrix = Matrix4f.orthographic(-10.0f, 10.0f, -10.0f * 9.0f / 16.0f, 10.0f * 9.0f / 16.0f, -1.0f, 1.0f); + + level = new Level(); + } + + public void run() { + init(); + + long prevTime = System.nanoTime(); + double deltaT = 0.0; + double nanoSecConversion = 1000000000.0 / 60.0; + long timer = System.currentTimeMillis(); + int currentUpdates = 0; + int currentFrames = 0; + + while (running) { + // Frames/Updates per second counter and limiter + long currentTime = System.nanoTime(); + deltaT += (currentTime - prevTime) / nanoSecConversion; + prevTime = currentTime; + if (deltaT >= 1.0) { + update(); + currentUpdates++; + deltaT--; + } + render(); + currentFrames++; + // If longer than one second has passed + if (System.currentTimeMillis() - timer > 1000) { + timer += 1000; + System.out.println("Updates Per Second: " + currentUpdates); + System.out.println("Frames Per Second: " + currentFrames); + currentUpdates = 0; + currentFrames = 0; + } + + if (glfwWindowShouldClose(gameWindow) == GL_TRUE) { + running = false; + } + } + + glfwDestroyWindow(gameWindow); + glfwTerminate(); + } + + public void update() { + glfwPollEvents(); + level.update(); + } + + public void render() { + glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); + level.render(); + + int error = glGetError(); + if (error != GL_NO_ERROR) { + System.out.println("OpenGL Error: " + error); + } + + glfwSwapBuffers(gameWindow); + } + + public static void main(String[] args) { + new Main().start(); + } +} diff --git a/src/game/MouseHandler.java b/src/game/MouseHandler.java deleted file mode 100644 index 520cfca..0000000 --- a/src/game/MouseHandler.java +++ /dev/null @@ -1,4 +0,0 @@ -package game; -public class MouseHandler { - -} diff --git a/src/game/WindowHandler.java b/src/game/WindowHandler.java deleted file mode 100644 index 4e24ec3..0000000 --- a/src/game/WindowHandler.java +++ /dev/null @@ -1,4 +0,0 @@ -package game; -public class WindowHandler { - -} diff --git a/src/game/graphics/Shader.java b/src/game/graphics/Shader.java new file mode 100644 index 0000000..3275424 --- /dev/null +++ b/src/game/graphics/Shader.java @@ -0,0 +1,85 @@ +package game.graphics; + +import static org.lwjgl.opengl.GL20.*; + +import java.util.HashMap; +import java.util.Map; + +import game.math.Matrix4f; +import game.math.Vector3f; +import game.utils.ShaderUtils; + + +public class Shader { + + public static final int VERTEX_ATTRIB = 0; + public static final int TCOORD_ATTRIB = 1; + +// public static Shader SHADERHERE; + + private boolean enabled = false; + + private final int ID; + private Map locationCache = new HashMap(); + + public Shader(String vertex, String fragment) { + ID = ShaderUtils.load(vertex, fragment); + } + + public static void loadAll() { +// SHADERHERE = new Shader(vertPathStr, fragPathStr); + } + + public int getUniform(String name) { + if (locationCache.containsKey(name)) { + return locationCache.get(name); + } + int result = glGetUniformLocation(ID, name); + if (result == -1) { + System.err.println("Could not find uniform variable '" + name + "'!"); + } else { + locationCache.put(name, result); + } + return result; + } + + public void setUniform1i(String name, int value) { + if (!enabled) { + enable(); + } + glUniform1i(getUniform(name), value); + } + + public void setUniform1f(String name, float value) { + if (!enabled) { + enable(); + } + glUniform1f(getUniform(name), value); + } + + public void setUniform2f(String name, float x, float y) { + if (!enabled) { + enable(); + } + glUniform2f(getUniform(name), x, y); + } + + public void setUniform3f(String name, Vector3f vector) { + if (!enabled) { + enable(); + } + glUniform3f(getUniform(name), vector.x, vector.y, vector.z); + } + + public void setUniformMat4f(String name, Matrix4f matrix) { + if (!enabled) { + enable(); + } + glUniformMatrix4fv(getUniform(name), false, matrix.toFloatBuffer()); + } + + public void enable() { + glUseProgram(ID); + enabled = true; + } +} diff --git a/src/game/graphics/Texture.java b/src/game/graphics/Texture.java new file mode 100644 index 0000000..52629e8 --- /dev/null +++ b/src/game/graphics/Texture.java @@ -0,0 +1,61 @@ +package game.graphics; + +import static org.lwjgl.opengl.GL11.*; + +import java.awt.image.BufferedImage; +import java.io.FileInputStream; +import java.io.IOException; +import javax.imageio.ImageIO; +import game.utils.BufferUtils; + +public class Texture { + + private int width, height; + private int texture; + + public Texture(String path) { + texture = load(path); + } + + // Loads the image at the given path and binds the texture to the target + private int load(String path) { + int[] pixels = null; + try { + BufferedImage image = ImageIO.read(new FileInputStream(path)); + width = image.getWidth(); + height = image.getHeight(); + pixels = new int[width * height]; + image.getRGB(0, 0, width, height, pixels, 0, width); + } catch (IOException e) { + e.printStackTrace(); + } + + int[] data = new int[width * height]; + + //Bit shifting in order to get the correct rgba values + for (int i = 0; i < width * height; i++) { + int a = (pixels[i] & 0xff000000) >> 24; + int r = (pixels[i] & 0xff0000) >> 16; + int g = (pixels[i] & 0xff00) >> 8; + int b = (pixels[i] & 0xff); + + data[i] = a << 24 | b << 16 | g << 8 | r; + } + + int result = glGenTextures(); + glBindTexture(GL_TEXTURE_2D, result); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, BufferUtils.createIntBuffer(data)); + glBindTexture(result, 0); + return result; + } + + public void bind() { + glBindTexture(GL_TEXTURE_2D, texture); + } + + public void unbind() { + glBindTexture(GL_TEXTURE_2D, 0); + } +} diff --git a/src/game/graphics/VertexArray.java b/src/game/graphics/VertexArray.java new file mode 100644 index 0000000..7764140 --- /dev/null +++ b/src/game/graphics/VertexArray.java @@ -0,0 +1,75 @@ +package game.graphics; + +import static org.lwjgl.opengl.GL11.*; +import static org.lwjgl.opengl.GL15.*; +import static org.lwjgl.opengl.GL20.*; +import static org.lwjgl.opengl.GL30.*; + +import game.utils.BufferUtils; + +public class VertexArray { + + private int vertexArrayObj, vertexBufferObj, indexBufferObj, textureBufferObj; + private int numVertices; + + public VertexArray(int numVertices) { + this.numVertices = numVertices; + vertexArrayObj = glGenVertexArrays(); + } + + public VertexArray(float[] vertices, byte[] indices, float[] textureCoords) { + numVertices = indices.length; + + vertexArrayObj = glGenVertexArrays(); + glBindVertexArray(vertexArrayObj); + + // Note: pointerOffset parameters must be zero + vertexBufferObj = glGenBuffers(); + glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObj); + glBufferData(GL_ARRAY_BUFFER, BufferUtils.createFloatBuffer(vertices), GL_STATIC_DRAW); + glVertexAttribPointer(Shader.VERTEX_ATTRIB, 3, GL_FLOAT, false, 0, 0); + glEnableVertexAttribArray(Shader.VERTEX_ATTRIB); + + textureBufferObj = glGenBuffers(); + glBindBuffer(GL_ARRAY_BUFFER, textureBufferObj); + glBufferData(GL_ARRAY_BUFFER, BufferUtils.createFloatBuffer(textureCoords), GL_STATIC_DRAW); + glVertexAttribPointer(Shader.VERTEX_ATTRIB, 2, GL_FLOAT, false, 0, 0); + glEnableVertexAttribArray(Shader.TCOORD_ATTRIB); + + indexBufferObj = glGenBuffers(); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferObj); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, BufferUtils.createByteBuffer(indices), GL_STATIC_DRAW); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + glBindVertexArray(0); // Unbind when done + } + + public void bind() { + glBindVertexArray(vertexArrayObj); + if (indexBufferObj > 0) { + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferObj); + } + } + + public void unbind() { + if (indexBufferObj > 0) { + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + } + glBindVertexArray(0); + } + + public void draw() { + if (indexBufferObj > 0) { + glDrawElements(GL_TRIANGLES, numVertices, GL_UNSIGNED_BYTE, 0); + } else { + glDrawArrays(GL_TRIANGLES, 0, numVertices); + } + } + + public void render() { + bind(); + draw(); + } + +} diff --git a/src/game/input/Input.java b/src/game/input/Input.java new file mode 100644 index 0000000..4404fbc --- /dev/null +++ b/src/game/input/Input.java @@ -0,0 +1,17 @@ +package game.input; + +import org.lwjgl.glfw.GLFW; +import org.lwjgl.glfw.GLFWKeyCallback; + +public class Input extends GLFWKeyCallback{ + + public static boolean[] keys = new boolean[65536]; + + public void invoke(long window, int key, int scancode, int action, int mods) { + keys[key] = action != GLFW.GLFW_RELEASE; + } + + public static boolean isKeyDown(int keycode) { + return keys[keycode]; + } +} diff --git a/src/game/level/Level.java b/src/game/level/Level.java new file mode 100644 index 0000000..0e06e68 --- /dev/null +++ b/src/game/level/Level.java @@ -0,0 +1,16 @@ +package game.level; + +public class Level { + + public Level() { + + } + + public void update() { + + } + + public void render() { + + } +} diff --git a/src/game/math/Matrix4f.java b/src/game/math/Matrix4f.java new file mode 100644 index 0000000..729ed2e --- /dev/null +++ b/src/game/math/Matrix4f.java @@ -0,0 +1,84 @@ +package game.math; + +import java.nio.FloatBuffer; +import game.utils.BufferUtils; + +public class Matrix4f { + + public static final int SIZE = 4 * 4; + public float[] elements = new float[SIZE]; + + public Matrix4f() { + + } + + // Returns a 4x4 identity matrix + public static Matrix4f identity() { + Matrix4f result = new Matrix4f(); + for (int i = 0; i < SIZE; i++) { + result.elements[i] = 0.0f; + } + + //Row + Column * Width + result.elements[0 + 0 * 4] = 1.0f; + result.elements[1 + 1 * 4] = 1.0f; + result.elements[2 + 2 * 4] = 1.0f; + result.elements[3 + 3 * 4] = 1.0f; + + return result; + } + + public static Matrix4f orthographic(float left, float right, float bottom, float top, float near, float far) { + Matrix4f result = identity(); + + result.elements[0 + 0 * 4] = 2.0f/(right - left); + result.elements[1 + 1 * 4] = 2.0f/(top - bottom); + result.elements[2 + 2 * 4] = 2.0f/(near - far); + + result.elements[0 + 3 * 4] = (left + right) / (left - right); + result.elements[1 + 3 * 4] = (bottom + top) / (bottom - top); + result.elements[2 + 3 * 4] = (far + near) / (far - near); + + return result; + } + + public static Matrix4f translate(Vector3f vector) { + Matrix4f result = identity(); + result.elements[0 + 3 * 4] = vector.x; + result.elements[1 + 3 * 4] = vector.y; + result.elements[2 + 3 * 4] = vector.z; + return result; + } + + public static Matrix4f rotate(float angle) { + Matrix4f result = identity(); + float r = (float) Math.toRadians(angle); + float cos = (float) Math.cos(r); + float sin = (float) Math.sin(r); + + result.elements[0 + 0 * 4] = cos; + result.elements[1 + 0 * 4] = sin; + result.elements[0 + 1 * 4] = -sin; + result.elements[1 + 1 * 4] = cos; + + return result; + } + + public Matrix4f multiply(Matrix4f matrix) { + Matrix4f result = new Matrix4f(); + for (int y = 0; y < 4; y++) { + for (int x = 0; x < 4; x++) { + float sum = 0.0f; + for (int e = 0; e < 4; e++) { + sum += this.elements[x + e * 4] * matrix.elements[e + y * 4]; + } + result.elements[x + y * 4] = sum; + } + } + return result; + } + + public FloatBuffer toFloatBuffer() { + return BufferUtils.createFloatBuffer(elements); + } +} diff --git a/src/game/math/Vector3f.java b/src/game/math/Vector3f.java new file mode 100644 index 0000000..4b909d5 --- /dev/null +++ b/src/game/math/Vector3f.java @@ -0,0 +1,18 @@ +package game.math; + +public class Vector3f { + + public float x, y, z; + + public Vector3f() { + x = 0.0f; + y = 0.0f; + z = 0.0f; + } + + public Vector3f(float x, float y, float z) { + this.x = x; + this.y = y; + this.z = z; + } +} diff --git a/src/game/utils/BufferUtils.java b/src/game/utils/BufferUtils.java new file mode 100644 index 0000000..65a078e --- /dev/null +++ b/src/game/utils/BufferUtils.java @@ -0,0 +1,31 @@ +package game.utils; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.FloatBuffer; +import java.nio.IntBuffer; + +public class BufferUtils { + + private BufferUtils() { + + } + + public static ByteBuffer createByteBuffer(byte[] array) { + ByteBuffer result = ByteBuffer.allocateDirect(array.length).order(ByteOrder.nativeOrder()); + result.put(array).flip(); + return result; + } + + public static FloatBuffer createFloatBuffer(float[] array) { + FloatBuffer result = ByteBuffer.allocateDirect(array.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer(); + result.put(array).flip(); + return result; + } + + public static IntBuffer createIntBuffer(int[] array) { + IntBuffer result = ByteBuffer.allocateDirect(array.length * 4).order(ByteOrder.nativeOrder()).asIntBuffer(); + result.put(array).flip(); + return result; + } +} diff --git a/src/game/utils/FileUtils.java b/src/game/utils/FileUtils.java new file mode 100644 index 0000000..3e045d4 --- /dev/null +++ b/src/game/utils/FileUtils.java @@ -0,0 +1,27 @@ +package game.utils; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; + +public class FileUtils { + + private FileUtils() { + + } + + public static String loadAsString(String file) { + StringBuilder result = new StringBuilder(); + try { + BufferedReader reader = new BufferedReader(new FileReader(file)); + String buffer = ""; + while ((buffer = reader.readLine()) != null) { + result.append(buffer + '\n'); + } + reader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + return result.toString(); + } +} diff --git a/src/game/utils/ShaderUtils.java b/src/game/utils/ShaderUtils.java new file mode 100644 index 0000000..007c4ea --- /dev/null +++ b/src/game/utils/ShaderUtils.java @@ -0,0 +1,49 @@ +package game.utils; + +import static org.lwjgl.opengl.GL11.*; +import static org.lwjgl.opengl.GL20.*; + +public class ShaderUtils { + + private ShaderUtils() { + + } + + public static int load(String vertPath, String fragPath) { + String vert = FileUtils.loadAsString(vertPath); + String frag = FileUtils.loadAsString(fragPath); + return create(vert, frag); + } + + public static int create(String vert, String frag) { + int program = glCreateProgram(); + int vertID = glCreateShader(GL_VERTEX_SHADER); + int fragID = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(vertID, vert); + glShaderSource(fragID, frag); + + glCompileShader(vertID); + if (glGetShaderi(vertID, GL_COMPILE_STATUS) == GL_FALSE) { + System.err.println("Failed to compile vertex shader!"); + System.err.println(glGetShaderInfoLog(vertID)); + return -1; + } + + glCompileShader(fragID); + if (glGetShaderi(fragID, GL_COMPILE_STATUS) == GL_FALSE) { + System.err.println("Failed to compile fragment shader!"); + System.err.println(glGetShaderInfoLog(fragID)); + return -1; + } + + glAttachShader(program, vertID); + glAttachShader(program, fragID); + glLinkProgram(program); + glValidateProgram(program); + + glDeleteShader(vertID); + glDeleteShader(fragID); + + return program; + } +} diff --git a/src/gfx/Screen.java b/src/gfx/Screen.java deleted file mode 100644 index 45ee275..0000000 --- a/src/gfx/Screen.java +++ /dev/null @@ -1,23 +0,0 @@ -package gfx; - -import java.awt.Dimension; -import javax.swing.JPanel; -import level.LevelHandler; - -// This class will contain and render a specific level/entities -// The game.Game class will contain one screen at a time to display that level. -public class Screen extends JPanel { - - private String levelName; - private LevelHandler level; - - public Screen(LevelHandler level) { - this.level = level; - this.setPreferredSize(new Dimension(700, 600)); - } - - public String getLevelName() { - return levelName; - } - -} diff --git a/src/level/LevelHandler.java b/src/level/LevelHandler.java deleted file mode 100644 index df9a8e8..0000000 --- a/src/level/LevelHandler.java +++ /dev/null @@ -1,73 +0,0 @@ -package level; - -import java.io.BufferedReader; -import java.io.FileReader; -import java.io.IOException; -import java.util.List; -import entities.Entity; -import level.tiles.Tile; - -// Creates and manages the level when the game is running. -public class LevelHandler { - - private Tile[][] tiles = new Tile[30][30]; - private List entities; - private int width; - private int height; - private String levelName; - private String levelPath; - - public LevelHandler(String levelPath) { - if (levelPath != null) { - this.levelPath = levelPath; - this.loadLevelFromFile(); - } - } - - // TODO: Make work, preferably using JSON - private void loadLevelFromFile() { - try { - FileReader fileReader = new FileReader(levelPath); - BufferedReader bufferedReader = new BufferedReader(fileReader); - levelName = bufferedReader.readLine(); - String line = ""; - int yMarker = 0; - while((line = bufferedReader.readLine()) != null) { - int xMarker = 0; - for (String currentID : line.split("\\s")) { - tiles[yMarker][xMarker] = Tile.getTile(Integer.parseInt(currentID)); - xMarker++; - } - yMarker++; - } - bufferedReader.close(); - } catch (IOException e) { - System.err.println(e.getMessage()); - } - } - - public void tick() { - for (Tile[] tileArray : tiles) { - for (Tile t : tileArray) { - t.tick(); - } - } - - for (Entity e : entities) { - e.tick(); - } - } - - public String getLevelName() { - return levelName; - } - - public void printTiles() { - for (Tile[] y : tiles) { - for (Tile x : y) { - System.out.print(x + " "); - } - System.out.println(); - } - } -} diff --git a/src/level/tiles/SmoothFloor.java b/src/level/tiles/SmoothFloor.java deleted file mode 100644 index 9695e8b..0000000 --- a/src/level/tiles/SmoothFloor.java +++ /dev/null @@ -1,25 +0,0 @@ -package level.tiles; - -import java.awt.Color; - -public class SmoothFloor extends Tile{ - - private float friction = 1; - - public SmoothFloor() { - super(1, true, false, Color.gray); - } - - public void tick() { - - } - - public void render() { - - } - - public float getFriction() { - return friction; - } - -} diff --git a/src/level/tiles/Tile.java b/src/level/tiles/Tile.java deleted file mode 100644 index 8ac3a46..0000000 --- a/src/level/tiles/Tile.java +++ /dev/null @@ -1,44 +0,0 @@ -package level.tiles; - -import java.awt.Color; - -public abstract class Tile { - - protected int id; - protected boolean isFloor; - protected boolean isWall; - private Color color; - - // Contains all tiles in the game, where the index is the tile ID. - private static Tile[] gameTiles = new Tile[32]; - - public Tile(int id, boolean isFloor, boolean isWall, Color color) { - this.id = id; - this.isFloor = isFloor; - this.isWall = isWall; - this.color = color; - - // Populate this with all tiles that exist - gameTiles[1] = new SmoothFloor(); - } - - public int getId() { - return id; - } - - public boolean isFloor() { - return isFloor; - } - - public boolean isWall() { - return isWall; - } - - public static Tile getTile(int tileID) { - return gameTiles[tileID]; - } - - public abstract void tick(); - - public abstract void render(); -} diff --git a/src/menu/Menu.java b/src/menu/Menu.java deleted file mode 100644 index 83d9bc9..0000000 --- a/src/menu/Menu.java +++ /dev/null @@ -1,24 +0,0 @@ -package menu; - -import java.io.FileNotFoundException; -import java.io.IOException; - -import game.Game; -import level.LevelHandler; - -public class Menu { - - // Will display options. This is the main menu. - // Currently jumps right to a game. - public void run() throws IOException{ - Game game = new Game(); - // Show menu options here - - // Control flow to select level, game modes, etc. - - // Pretend a test level was selected - LevelHandler chosenLevel = new LevelHandler("/res/test.txt"); - game.setLevel(chosenLevel); - } - -}