From 085ca8b7ee7e14c98ed23bd52108783445a3c083 Mon Sep 17 00:00:00 2001 From: Gnarwhal Date: Wed, 7 Aug 2024 04:26:50 +0000 Subject: [PATCH] Project as is --- res/img/Spring 1.png | Bin 0 -> 251 bytes res/img/Spring 2.png | Bin 0 -> 249 bytes res/img/Spring 3.png | Bin 0 -> 244 bytes res/img/Spring 4.png | Bin 0 -> 243 bytes res/img/Stone Block.png | Bin 0 -> 257 bytes res/shaders/frag.fs | 11 ++ res/shaders/vert.vs | 14 ++ src/com/gnarly/engine/GameObject.java | 118 +++++++++++++++++ src/com/gnarly/engine/Window.java | 119 +++++++++++++++++ src/com/gnarly/engine/graphics/Animation.java | 61 +++++++++ src/com/gnarly/engine/graphics/Mesh.java | 124 ++++++++++++++++++ src/com/gnarly/engine/graphics/Shader.java | 120 +++++++++++++++++ .../gnarly/engine/graphics/ShaderLibrary.java | 34 +++++ src/com/gnarly/engine/graphics/Texture.java | 71 ++++++++++ src/com/gnarly/game/Main.java | 72 ++++++++++ 15 files changed, 744 insertions(+) create mode 100755 res/img/Spring 1.png create mode 100755 res/img/Spring 2.png create mode 100755 res/img/Spring 3.png create mode 100755 res/img/Spring 4.png create mode 100755 res/img/Stone Block.png create mode 100755 res/shaders/frag.fs create mode 100755 res/shaders/vert.vs create mode 100755 src/com/gnarly/engine/GameObject.java create mode 100755 src/com/gnarly/engine/Window.java create mode 100755 src/com/gnarly/engine/graphics/Animation.java create mode 100755 src/com/gnarly/engine/graphics/Mesh.java create mode 100755 src/com/gnarly/engine/graphics/Shader.java create mode 100755 src/com/gnarly/engine/graphics/ShaderLibrary.java create mode 100755 src/com/gnarly/engine/graphics/Texture.java create mode 100755 src/com/gnarly/game/Main.java diff --git a/res/img/Spring 1.png b/res/img/Spring 1.png new file mode 100755 index 0000000000000000000000000000000000000000..fd2d6008a92ff82479dc435a6e65c7293d2dfd5b GIT binary patch literal 251 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`Ea{HEjtmSN`?>!lvI6;>1s;*b z3=DjSK$uZf!>a)(C|TkfQ4*Y=R#Ki=l*&+$n3-3imzP?iV4`QBXK3WilKlp#sKV35 zF~q`uY2R(W0}33@fA9VMZ|!V7W4hUGnUkFyxm#yMZamyCdx!gT-p=j2>i;cZGf;4d zO=6SfTmMDit!lvI6;>1s;*b z3=DjSK$uZf!>a)(C|TkfQ4*Y=R#Ki=l*&+$n3-3imzP?iV4`QBXK3WilKlp#sLa#F zF~q`uY2R(W0}33@fA9VMZ!K*7A#4+`r{9_ji(Fm$8g{&zxKOv@MCjS9Umw_aH83*2 zGBnO8u1amG*zT6W_v^AMk95krN2WPC`|K1B^BR>ubn$NbS5kOpkJFy$!;TE1EutMC qr`m5}71+7z=Dq{l>XxQ<-B}p>T=rGknr@&A7(8A5T-G@yGywoMF!lvI6;>1s;*b z3=Dh+L6~vJ#O${~LCF%=h?3y^w370~qEv>0#LT=By}Z;C1rt33JwqdBmh3k`MTMR& zjv*HQOZx)(4k&Oq+rEGI|NN1>uBaZRlc%LJUQgj ku<_*0un*hc1sQq9wcL4fvNY*z9nf+HPgg&ebxsLQ0PSy9#Q*>R literal 0 HcmV?d00001 diff --git a/res/img/Spring 4.png b/res/img/Spring 4.png new file mode 100755 index 0000000000000000000000000000000000000000..6cc9cad37ec1d63c7fbd68cda62ff097bf1168ca GIT binary patch literal 243 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`Ea{HEjtmSN`?>!lvI6;>1s;*b z3=Dh+L6~vJ#O${~LCF%=h?3y^w370~qEv>0#LT=By}Z;C1rt33JwqdBmh3k`MFpNN zjv*HQOZx)(4k&Oq+rEGI|NN1>uFX14D*D+A)@myivBbJ9IL#pt{x>CqRbc1JNMHGX zjba7~2W-;VV*S=fbd<;anK@UwrFcorp2rS-*4y>e*4%X3b9zxT!!b^6fp1ee)-ouW jn_6?-yA~U@>;&%$%NOp=4>T?V?PlbP0l+XkKlMGTp literal 0 HcmV?d00001 diff --git a/res/img/Stone Block.png b/res/img/Stone Block.png new file mode 100755 index 0000000000000000000000000000000000000000..2d0600d84f4f357508083dc2b7dc430f2af723b6 GIT binary patch literal 257 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61SBU+%rFB|jKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCij$3p^r=85sBugD~Uq{1qucLCF%=h?3y^w370~qEv>0#LT=By}Z;C1rt33 zJwt=FTwDi$iV8hl978Pp=T11t+hD-MEU9U1!0epDESA8zO*QOKLFfdZ_o42)qCS=V zS2R|PF%x8bxMg90tJ>;AiOz}J8!x!pWz1L^|3hk}HA|n)7615h4w=Y|M#lvW2i>9) v1j1PRbhaE&2zYS9iJOP1MQ>K+es>$8J^G9Hy((4&TF&6<>gTe~DWM4fuL)1e literal 0 HcmV?d00001 diff --git a/res/shaders/frag.fs b/res/shaders/frag.fs new file mode 100755 index 0000000..e50af71 --- /dev/null +++ b/res/shaders/frag.fs @@ -0,0 +1,11 @@ +#version 330 + +uniform sampler2D sampler; + +in vec2 tex; + +layout (location = 0) out vec4 color; + +void main() { + color = texture(sampler, tex); +} \ No newline at end of file diff --git a/res/shaders/vert.vs b/res/shaders/vert.vs new file mode 100755 index 0000000..1560f4e --- /dev/null +++ b/res/shaders/vert.vs @@ -0,0 +1,14 @@ +# version 330 + +uniform mat4 pr_matrix; +uniform mat4 vw_matrix; + +in vec3 verts; +in vec2 texts; + +out vec2 tex; + +void main() { + tex = texts; + gl_Position = pr_matrix * vw_matrix * vec4(verts, 1.0); +} \ No newline at end of file diff --git a/src/com/gnarly/engine/GameObject.java b/src/com/gnarly/engine/GameObject.java new file mode 100755 index 0000000..db49fdb --- /dev/null +++ b/src/com/gnarly/engine/GameObject.java @@ -0,0 +1,118 @@ +package com.gnarly.engine; + +import org.joml.Matrix4f; +import org.joml.Vector3f; + +import com.gnarly.engine.graphics.*; +import com.gnarly.game.Main; + +public class GameObject { + + float velX = 0.0f; + float velY = 0.0f; + float accelX = 0.0f; + float accelY = 0.0f; + boolean solid; + Vector3f position; + Vector3f size; + float rotation; + float mass; + float friction; + Animation anim; + public static Matrix4f projection; + + public GameObject(String texPath, Shader shader) { + anim = new Animation(texPath, shader, 1, 0, false); + } + + public GameObject(String texPath, Shader shader, int numFrames, int numberUpdates, boolean loop) { + anim = new Animation(texPath, shader, numFrames, numberUpdates, loop); + } + + public GameObject(Animation anim) { + this.anim = anim; + } + + public GameObject newInstanceOf(float x, float y, float z, float rotation, float scale, float mass, float friction, boolean solid) { + GameObject ret = new GameObject(anim); + ret.setVars(x, y, z, rotation, scale, mass, friction, solid); + return ret; + } + + public void setVars(float x, float y, float z, float rotation, float scale, float mass, float friction, boolean solid) { + position = new Vector3f(x, y, z); + this.rotation = rotation; + size = new Vector3f(scale, scale, 0.0f); + this.solid = solid; + this.mass = mass > 0 ? mass : 1.0f; + this.friction = friction; + } + + public void setScale(float scale) { + size.x = scale; + size.y = scale; + } + + public void setX(float x) { + position.x = x; + } + + public void setY(float y) { + position.y = y; + } + + public void setVelocityX(float velX) { + this.velX = velX; + } + + public void setVelocityY(float velY) { + this.velY = velY; + } + + public void setMass(float mass) { + this.mass = mass; + } + + public void setFriction(float friction) { + this.friction = friction; + } + + public void addForce(float angle, float force) { + float xComp = (float)Math.cos(Math.toRadians(angle)) * force; + float yComp = (float)Math.sin(Math.toRadians(angle)) * force; + accelX += xComp / mass; + accelY += yComp / mass; + } + + public void update() { + anim.update(); + if(velX != 0 || velY != 0) { + float zeroStrength = mass * (float) Math.sqrt(accelX * accelX + accelY * accelY); + System.out.println(zeroStrength + ", " + friction); + addForce(getAngle() + 180, (float) Math.min(friction, zeroStrength)); + } + position.x += velX; + position.y += velY; + velX += accelX; + velY += accelY; + } + + public float getAngle() { + float hyp = (float) Math.sqrt(velX * velX + velY * velY); + float angle = (float) ((velY / (float) Math.abs(velY)) * (float) Math.acos(velX / hyp)); + System.out.println((float) Math.toDegrees(angle)); + return (float) Math.toDegrees(angle); + } + + public void render() { + int x = (int) position.x; + int y = (int) position.y; + int z = (int) position.z; + position = new Vector3f(x, y, z); + Matrix4f translate = new Matrix4f().translate(position); + Matrix4f rotate = new Matrix4f().rotateZ((float)Math.toRadians(rotation)); + Matrix4f scale = new Matrix4f().scale(size); + anim.getMesh().getShader().setUniformMat4f("vw_matrix", translate.mul(rotate).mul(scale)); + anim.getMesh().render(); + } +} diff --git a/src/com/gnarly/engine/Window.java b/src/com/gnarly/engine/Window.java new file mode 100755 index 0000000..0966d67 --- /dev/null +++ b/src/com/gnarly/engine/Window.java @@ -0,0 +1,119 @@ +package com.gnarly.engine; + +import static org.lwjgl.glfw.GLFW.*; +import static org.lwjgl.opengl.GL11.*; +import static org.lwjgl.system.MemoryUtil.NULL; +import org.joml.Vector3f; +import org.lwjgl.glfw.*; +import org.lwjgl.opengl.GL; + +public class Window { + + private long window; + + private int width, height, mx, my; + private boolean vSync; + + public Window(int width, int height, boolean vSync) { + this.width = width; + this.height = height; + this.vSync = vSync; + init(); + } + + public void init() { + glfwSetErrorCallback(GLFWErrorCallback.createPrint(System.err)); + + if(!glfwInit()) + throw new IllegalStateException("Could not initalize GLFW!"); + + glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); + glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); + + GLFWVidMode vidMode = glfwGetVideoMode(glfwGetPrimaryMonitor()); + + if(width == NULL || height == NULL) { + window = glfwCreateWindow(vidMode.width(), vidMode.height(), "One Room", glfwGetPrimaryMonitor(), NULL); + width = vidMode.width(); + height = vidMode.height(); + } + else { + window = glfwCreateWindow(width, height, "One Room", NULL, NULL); + + glfwSetWindowPos(window, (vidMode.width() - width) / 2, (vidMode.height() - height) / 2); + } + + glfwSetCursorPosCallback(window, (long window, double xpos, double ypos) -> { + mx = (int)xpos; + my = (int)ypos; + }); + + glfwMakeContextCurrent(window); + GL.createCapabilities(); + + if(vSync) + glfwSwapInterval(1); + + glfwShowWindow(window); + + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + + glEnable(GL_TEXTURE_2D); + + glEnable(GL_DEPTH_TEST); + + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + System.out.println("OpenGL Version: " + glGetString(GL_VERSION)); + } + + public void update() { + glfwPollEvents(); + } + + public void clear() { + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + } + + public void swap() { + glfwSwapBuffers(window); + } + + public void cleanup() { + glfwDestroyWindow(window); + glfwTerminate(); + } + + public Vector3f getMouseCoords() { + return new Vector3f(mx, my, 0); + } + + public boolean isMousePressed(int button) { + return glfwGetMouseButton(window, button) == GLFW_PRESS; + } + + public boolean isKeyPressed(int keyCode) { + return glfwGetKey(window, keyCode) == GLFW_PRESS; + } + + public void close() { + glfwSetWindowShouldClose(window, true); + } + + public boolean shouldClose() { + return glfwWindowShouldClose(window); + } + + public long getWindow() { + return window; + } + + public int getWidth() { + return width; + } + + public int getHeight() { + return height; + } +} diff --git a/src/com/gnarly/engine/graphics/Animation.java b/src/com/gnarly/engine/graphics/Animation.java new file mode 100755 index 0000000..dc1f96f --- /dev/null +++ b/src/com/gnarly/engine/graphics/Animation.java @@ -0,0 +1,61 @@ +package com.gnarly.engine.graphics; + +import com.gnarly.game.Main; + +public class Animation { + + Mesh[] meshes; + int frame = 0; + int max; + int inc = 1; + int curUps; + int needUps; + boolean reverse; + + public Animation(String texPath, Shader shader, int numFrames, int numUpdates, boolean reverse) { + this.reverse = reverse; + max = numFrames; + needUps = numUpdates; + shader.setUniformMat4f("pr_matrix", Main.projection); + meshes = new Mesh[numFrames]; + Texture tex = new Texture(texPath + (numFrames > 1 ? " 1.png" : ".png")); + if(numFrames > 1) + for (int i = 0; i < meshes.length; i++) + meshes[i] = new Mesh(constructVBO(tex.getWidth(), tex.getHeight()), new Texture(texPath + " " + (i+1) + ".png"), shader); + else + meshes[0] = new Mesh(constructVBO(tex.getWidth(), tex.getHeight()), tex, shader); + } + + public void update() { + if(max > 1) { + if(curUps == needUps) { + frame += inc; + if((frame == -1 || frame == max) && reverse) + frame += (inc = -inc); + else if(frame == max) + frame = 0; + curUps = 1; + } + else + ++curUps; + } + } + + public void render() { + meshes[frame].render(); + } + + private float[] constructVBO(float width, float height) { + float[] vbo = new float[] { + 0.0f, 0.0f, 0.0f, //TOP LEFT + 0.0f, -height, 0.0f, //BOTTOM LEFT + width, -height, 0.0f, //BOTTOM RIGHT + width, 0.0f, 0.0f //TOP RIGHT + }; + return vbo; + } + + public Mesh getMesh() { + return meshes[frame]; + } +} diff --git a/src/com/gnarly/engine/graphics/Mesh.java b/src/com/gnarly/engine/graphics/Mesh.java new file mode 100755 index 0000000..a69cf7b --- /dev/null +++ b/src/com/gnarly/engine/graphics/Mesh.java @@ -0,0 +1,124 @@ +package com.gnarly.engine.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 java.nio.ByteBuffer; +import java.nio.FloatBuffer; + +import org.lwjgl.BufferUtils; + +public class Mesh { + + private Shader shader; + private Texture texture; + private int count; + private int vaoID, vboID, iboID, tcboID; + private float[] vbo, tcbo; + private byte[] ibo; + + + public Mesh(float[] vbo, byte[] ibo, float[] tcbo, Texture texture, Shader shader) { + this.vbo = vbo; + this.ibo = ibo; + this.tcbo = tcbo; + this.texture = texture; + this.shader = shader; + count = ibo.length; + init(); + } + + public Mesh(float[] vbo, Texture texture, Shader shader) { + this.vbo = vbo; + this.ibo = defaultIBO(); + this.tcbo = defaultTCBO(); + this.texture = texture; + this.shader = shader; + count = ibo.length; + init(); + } + + public Mesh(float[] vbo, String texPath, String vertPath, String fragPath) { + this.vbo = vbo; + this.ibo = defaultIBO(); + this.tcbo = defaultTCBO(); + this.texture = new Texture(texPath); + this.shader = new Shader(vertPath, fragPath); + count = ibo.length; + init(); + } + + private void init() { + vaoID = glGenVertexArrays(); + glBindVertexArray(vaoID); + + vboID = glGenBuffers(); + glBindBuffer(GL_ARRAY_BUFFER, vboID); + glBufferData(GL_ARRAY_BUFFER, createFloatBuffer(vbo), GL_STATIC_DRAW); + glVertexAttribPointer(Shader.VERT_ATTRIB, 3, GL_FLOAT, false, 0, 0); + glEnableVertexAttribArray(Shader.VERT_ATTRIB); + + iboID = glGenBuffers(); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iboID); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, createByteBuffer(ibo), GL_STATIC_DRAW); + + tcboID = glGenBuffers(); + glBindBuffer(GL_ARRAY_BUFFER, tcboID); + glBufferData(GL_ARRAY_BUFFER, createFloatBuffer(tcbo), GL_STATIC_DRAW); + glVertexAttribPointer(Shader.TEX_COORD_ATTRIB, 2, GL_FLOAT, false, 0, 0); + glEnableVertexAttribArray(Shader.TEX_COORD_ATTRIB); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + glBindVertexArray(0); + } + + public void render() { + shader.enable(); + texture.bind(); + glBindVertexArray(vaoID); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iboID); + glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_BYTE, 0); + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + glBindVertexArray(0); + texture.unbind(); + shader.enable(); + } + + public byte[] defaultIBO() { + return new byte[] { + 0, 1, 2, + 0, 2, 3 + }; + } + + public float[] defaultTCBO() { + return new float[] { + 1, 0, + 1, 1, + 0, 1, + 0, 0, + }; + } + + public Shader getShader() { + return shader; + } + + public FloatBuffer createFloatBuffer(float[] data) { + FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length); + buffer.put(data); + buffer.flip(); + return buffer; + } + + public ByteBuffer createByteBuffer(byte[] data) { + ByteBuffer buffer = BufferUtils.createByteBuffer(data.length); + buffer.put(data); + buffer.flip(); + return buffer; + } +} diff --git a/src/com/gnarly/engine/graphics/Shader.java b/src/com/gnarly/engine/graphics/Shader.java new file mode 100755 index 0000000..e036f12 --- /dev/null +++ b/src/com/gnarly/engine/graphics/Shader.java @@ -0,0 +1,120 @@ +package com.gnarly.engine.graphics; + +import static org.lwjgl.opengl.GL20.*; + +import org.lwjgl.BufferUtils; + +import java.io.*; +import java.nio.FloatBuffer; +import java.util.HashMap; +import java.util.Map; + +import org.joml.Matrix4f; +import org.joml.Vector3f; + +public class Shader { + + private int program, vs, fs; + public static int VERT_ATTRIB = 0, TEX_COORD_ATTRIB = 1; + boolean enabled; + Map uniforms; + + public Shader(String vertPath, String fragPath) { + uniforms = new HashMap<>(); + String vert = load(vertPath); + String frag = load(fragPath); + create(vert, frag); + } + + private String load(String path) { + StringBuilder file = new StringBuilder(); + try { + BufferedReader reader = new BufferedReader(new FileReader(new File("res/shaders/" + path))); + String line; + while((line = reader.readLine()) != null) + file.append(line + '\n'); + reader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + return file.toString(); + } + + public void create(String vert, String frag) { + program = glCreateProgram(); + + vs = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vs, vert); + glCompileShader(vs); + if(glGetShaderi(vs, GL_COMPILE_STATUS) != 1) + throw new RuntimeException("Failed to compile shader! " + glGetShaderInfoLog(vs)); + + fs = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fs, frag); + glCompileShader(fs); + if(glGetShaderi(fs, GL_COMPILE_STATUS) != 1) + throw new RuntimeException("Failed to compile shader! " + glGetShaderInfoLog(fs)); + + glAttachShader(program, vs); + glAttachShader(program, fs); + + glBindAttribLocation(program, VERT_ATTRIB, "verts"); + glBindAttribLocation(program, TEX_COORD_ATTRIB, "texts"); + + glLinkProgram(program); + glValidateProgram(program); + } + + public int getLocation(String name) { + if(uniforms.containsKey(name)) + return uniforms.get(name); + int location = glGetUniformLocation(program, name); + uniforms.put(name, location); + if(location != -1) + return location; + else + throw new RuntimeException("Could not find uniform: " + name); + } + + public void setUniform1i(String name, int value) { + if(!enabled) enable(); + glUniform1i(getLocation(name), value); + disable(); + } + + public void setUniform1f(String name, float value) { + if(!enabled) enable(); + glUniform1f(getLocation(name), value); + disable(); + } + + public void setUniform2f(String name, float x, float y) { + if(!enabled) enable(); + glUniform2f(getLocation(name), x, y); + disable(); + } + + public void setUniform3f(String name, Vector3f vector) { + if(!enabled) enable(); + glUniform3f(getLocation(name), vector.x, vector.y, vector.z); + disable(); + } + + public void setUniformMat4f(String name, Matrix4f matrix) { + if(!enabled) enable(); + FloatBuffer buffer = BufferUtils.createFloatBuffer(16); + matrix.get(buffer); + glUniformMatrix4fv(getLocation(name), false, buffer); + disable(); + } + + public void enable() { + enabled = true; + glUseProgram(program); + } + + public void disable() { + enabled = false; + glUseProgram(0); + } +} diff --git a/src/com/gnarly/engine/graphics/ShaderLibrary.java b/src/com/gnarly/engine/graphics/ShaderLibrary.java new file mode 100755 index 0000000..25a9265 --- /dev/null +++ b/src/com/gnarly/engine/graphics/ShaderLibrary.java @@ -0,0 +1,34 @@ +package com.gnarly.engine.graphics; + +import java.util.HashMap; +import java.util.Map; + +public class ShaderLibrary { + + Map shaders; + + public ShaderLibrary() { + shaders = new HashMap<>(); + init(); + } + + private void init() { + shaders.put("default", new Shader("vert.vs", "frag.fs")); + } + + public void add(String name, Shader shader) { + if(!shaders.containsKey(name)) + shaders.put(name, shader); + } + + public Shader get(String name) { + if(shaders.containsKey(name)) + return shaders.get(name); + else + throw new RuntimeException("Shader of name '" + name + "' has not been create yet!"); + } + + public Shader getDefault() { + return shaders.get("default"); + } +} diff --git a/src/com/gnarly/engine/graphics/Texture.java b/src/com/gnarly/engine/graphics/Texture.java new file mode 100755 index 0000000..e78e215 --- /dev/null +++ b/src/com/gnarly/engine/graphics/Texture.java @@ -0,0 +1,71 @@ +package com.gnarly.engine.graphics; + +import static org.lwjgl.opengl.GL11.*; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import java.nio.ByteBuffer; + +import javax.imageio.ImageIO; + +import org.lwjgl.BufferUtils; + +public class Texture { + + private int width, height; + private int ID; + + public Texture(String path) { + BufferedImage bi; + try { + bi = ImageIO.read(new File("res/img/" + path)); + width = bi.getWidth(); + height = bi.getHeight(); + + int[] pixelsRaw = new int[width * height]; + pixelsRaw = bi.getRGB(0, 0, width, height, null, 0, width); + + ByteBuffer pixels = BufferUtils.createByteBuffer(width * height * 4); + + for (int i = 0; i < height; i++) { + for (int j = 0; j < width; j++) { + int pixel = pixelsRaw[i * width + j]; + pixels.put((byte)((pixel >> 16) & 0xFF)); + pixels.put((byte)((pixel >> 8 ) & 0xFF)); + pixels.put((byte)((pixel ) & 0xFF)); + pixels.put((byte)((pixel >> 24) & 0xFF)); + } + } + + pixels.flip(); + + ID = glGenTextures(); + + glBindTexture(GL_TEXTURE_2D, ID); + + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); + + } catch (IOException e) { + e.printStackTrace(); + } + } + + public int getWidth() { + return width; + } + + public int getHeight() { + return height; + } + + public void bind() { + glBindTexture(GL_TEXTURE_2D, ID); + } + + public void unbind() { + glBindTexture(GL_TEXTURE_2D, 0); + } +} diff --git a/src/com/gnarly/game/Main.java b/src/com/gnarly/game/Main.java new file mode 100755 index 0000000..f7ba67f --- /dev/null +++ b/src/com/gnarly/game/Main.java @@ -0,0 +1,72 @@ +package com.gnarly.game; + +import static org.lwjgl.glfw.GLFW.*; + +import java.util.HashMap; +import java.util.Map; + +import org.joml.Matrix4f; + +import com.gnarly.engine.GameObject; +import com.gnarly.engine.Window; +import com.gnarly.engine.graphics.ShaderLibrary; + +public class Main { + + Window window; + ShaderLibrary shaders; + boolean running = true; + Map objects = new HashMap<>(); + GameObject block1; + GameObject block2; + public static Matrix4f projection; + + public Main() { + window = new Window(1000, 700, true); + shaders = new ShaderLibrary(); + projection = new Matrix4f().ortho2D(-window.getWidth() / 2, window.getWidth() / 2, -window.getHeight() / 2, window.getHeight() / 2); + run(); + } + + public void run() { + objects.put("Stone Block", new GameObject("Stone Block", shaders.getDefault())); + objects.put("Spring", new GameObject("Spring", shaders.getDefault(), 4, 2, true)); + block1 = objects.get("Stone Block").newInstanceOf(0.0f, 0.0f, 0.0f, 0.0f, 8.0f, 1.0f, 0.1f, false); + while(running) { + input(); + update(block1); + render(block1); + running = !window.shouldClose(); + } + } + + public void input() { + float force = 0.05f; + if(window.isKeyPressed(GLFW_KEY_D)) block1.addForce(0, force); + if(window.isKeyPressed(GLFW_KEY_W)) block1.addForce(90, force); + else if(window.isKeyPressed(GLFW_KEY_A)) block1.addForce(180, force); + else if(window.isKeyPressed(GLFW_KEY_S)) block1.addForce(270, force); + if(window.isKeyPressed(GLFW_KEY_RIGHT)) block2.addForce(0, force); + if(window.isKeyPressed(GLFW_KEY_UP)) block2.addForce(90, force); + else if(window.isKeyPressed(GLFW_KEY_LEFT)) block2.addForce(180, force); + else if(window.isKeyPressed(GLFW_KEY_DOWN)) block2.addForce(270, force); + if(window.isKeyPressed(GLFW_KEY_ESCAPE)) window.close(); + } + + public void update(GameObject... gameObjects) { + window.update(); + for (int i = 0; i < gameObjects.length; i++) + gameObjects[i].update(); + } + + public void render(GameObject... gameObjects) { + window.clear(); + for (int i = 0; i < gameObjects.length; i++) + gameObjects[i].render(); + window.swap(); + } + + public static void main(String[] args) { + new Main(); + } +}