Entire project

This commit is contained in:
Gnarwhal 2024-08-07 04:59:26 +00:00
commit cba41886e5
Signed by: Gnarwhal
GPG key ID: 0989A73D8C421174
69 changed files with 3982 additions and 0 deletions

View file

@ -0,0 +1,39 @@
package com.gnarly.engine.audio;
import static org.lwjgl.openal.ALC10.alcCreateContext;
import static org.lwjgl.openal.ALC10.alcMakeContextCurrent;
import static org.lwjgl.openal.ALC10.alcOpenDevice;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import org.lwjgl.openal.AL;
import org.lwjgl.openal.ALC;
import org.lwjgl.openal.ALC10;
import org.lwjgl.openal.ALCCapabilities;
public class ALManagement {
private long device, context;
private ALCCapabilities deviceCaps;
public ALManagement() {
device = alcOpenDevice((ByteBuffer) null);
if (device == 0)
throw new IllegalStateException("Failed to open the default device.");
deviceCaps = ALC.createCapabilities(device);
context = alcCreateContext(device, (IntBuffer) null);
if (context == 0)
throw new IllegalStateException("Failed to create an OpenAL context.");
alcMakeContextCurrent(context);
AL.createCapabilities(deviceCaps);
}
public void destroy() {
ALC10.alcDestroyContext(context);
ALC10.alcCloseDevice(device);
}
}

View file

@ -0,0 +1,38 @@
package com.gnarly.engine.audio;
import org.lwjgl.openal.AL10;
public class Sound {
private int buffer;
private int sourceId;
public Sound(String path) {
sourceId = AL10.alGenSources();
buffer = AL10.alGenBuffers();
WaveData waveData = WaveData.create(path);
AL10.alBufferData(buffer, waveData.format, waveData.data, waveData.samplerate);
AL10.alSourcei(sourceId, AL10.AL_BUFFER, buffer);
AL10.alSourcef(sourceId, AL10.AL_GAIN, 1);
AL10.alSourcef(sourceId, AL10.AL_PITCH, 1);
}
public void play(boolean loop) {
AL10.alSourcei(sourceId, AL10.AL_LOOPING, loop ? 1 : 0);
AL10.alSource3f(sourceId, AL10.AL_POSITION, 0, 0, 0);
AL10.alSourcePlay(sourceId);
}
public void stop() {
AL10.alSourceStop(sourceId);
}
public void setVolume(float volume) {
AL10.alSourcef(sourceId, AL10.AL_GAIN, volume);
}
public void destroy() {
AL10.alDeleteBuffers(buffer);
AL10.alDeleteSources(sourceId);
}
}

View file

@ -0,0 +1,88 @@
package com.gnarly.engine.audio;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
import org.lwjgl.BufferUtils;
import org.lwjgl.openal.AL10;
public class WaveData {
final int format;
final int samplerate;
final int totalBytes;
final int bytesPerFrame;
final ByteBuffer data;
private final AudioInputStream audioStream;
private final byte[] dataArray;
private WaveData(AudioInputStream stream) {
this.audioStream = stream;
AudioFormat audioFormat = stream.getFormat();
format = getOpenAlFormat(audioFormat.getChannels(), audioFormat.getSampleSizeInBits());
this.samplerate = (int) audioFormat.getSampleRate();
this.bytesPerFrame = audioFormat.getFrameSize();
this.totalBytes = (int) (stream.getFrameLength() * bytesPerFrame);
this.data = BufferUtils.createByteBuffer(totalBytes);
this.dataArray = new byte[totalBytes];
loadData();
}
protected void dispose() {
try {
audioStream.close();
data.clear();
} catch (IOException e) {
e.printStackTrace();
}
}
private ByteBuffer loadData() {
try {
int bytesRead = audioStream.read(dataArray, 0, totalBytes);
data.clear();
data.put(dataArray, 0, bytesRead);
data.flip();
} catch (IOException e) {
e.printStackTrace();
System.err.println("Couldn't read bytes from audio stream!");
}
return data;
}
public static WaveData create(String file) {
WaveData wavStream = null;
try {
InputStream stream = new FileInputStream(new File(file));
InputStream bufferedInput = new BufferedInputStream(stream);
AudioInputStream audioStream = null;
audioStream = AudioSystem.getAudioInputStream(bufferedInput);
wavStream = new WaveData(audioStream);
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return wavStream;
}
private static int getOpenAlFormat(int channels, int bitsPerSample) {
if (channels == 1) {
return bitsPerSample == 8 ? AL10.AL_FORMAT_MONO8 : AL10.AL_FORMAT_MONO16;
} else {
return bitsPerSample == 8 ? AL10.AL_FORMAT_STEREO8 : AL10.AL_FORMAT_STEREO16;
}
}
}

View file

@ -0,0 +1,101 @@
package com.gnarly.engine.display;
import org.joml.Matrix4f;
import org.joml.Vector3f;
public class Camera {
private Matrix4f projection, projView;
private float width, height;
private Vector3f position;
private float rotation;
public Camera(float width, float height) {
setDims(width, height);
position = new Vector3f();
rotation = 0;
projView = new Matrix4f();
}
public void setDims(float width, float height) {
this.width = width;
this.height = height;
projection = new Matrix4f().setOrtho(0, width, height, 0, 0, 1);
}
public void update() {
projection.translate(position.negate(new Vector3f()), projView);
projView.rotateZ(-rotation);
}
public Matrix4f getProjection() {
return new Matrix4f(projection);
}
public Matrix4f getMatrix() {
return new Matrix4f(projView);
}
public float getX() {
return position.x;
}
public float getY() {
return position.y;
}
public Vector3f getPosition() {
return new Vector3f(position);
}
public float getWidth() {
return width;
}
public float getHeight() {
return height;
}
public void setX(float x) {
position.x = x;
}
public void setY(float y) {
position.y = y;
}
public void setPosition(float x, float y) {
position.set(x, y, position.z);
}
public void setPosition(Vector3f position) {
this.position.x = position.x;
this.position.y = position.y;
}
public void setCenter(float x, float y) {
position.set(x - width / 2, y - height / 2, position.z);
}
public void setCenter(Vector3f position) {
this.position.x = position.x - width / 2;
this.position.y = position.y - height / 2;
}
public void translate(float x, float y, float z) {
position.add(x, y, z);
}
public void translate(Vector3f transform) {
position.add(transform);
}
public void setRotation(float angle) {
rotation = angle;
}
public void rotate(float angle) {
rotation += angle;
}
}

View file

@ -0,0 +1,226 @@
package com.gnarly.engine.display;
import static org.lwjgl.glfw.GLFW.GLFW_CONTEXT_VERSION_MAJOR;
import static org.lwjgl.glfw.GLFW.GLFW_CONTEXT_VERSION_MINOR;
import static org.lwjgl.glfw.GLFW.GLFW_DECORATED;
import static org.lwjgl.glfw.GLFW.GLFW_FALSE;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_BACKSPACE;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_DELETE;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_ENTER;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_LAST;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_LEFT;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_RIGHT;
import static org.lwjgl.glfw.GLFW.GLFW_MAXIMIZED;
import static org.lwjgl.glfw.GLFW.GLFW_MOUSE_BUTTON_LAST;
import static org.lwjgl.glfw.GLFW.GLFW_OPENGL_CORE_PROFILE;
import static org.lwjgl.glfw.GLFW.GLFW_OPENGL_FORWARD_COMPAT;
import static org.lwjgl.glfw.GLFW.GLFW_OPENGL_PROFILE;
import static org.lwjgl.glfw.GLFW.GLFW_RELEASE;
import static org.lwjgl.glfw.GLFW.GLFW_RESIZABLE;
import static org.lwjgl.glfw.GLFW.GLFW_TRUE;
import static org.lwjgl.glfw.GLFW.glfwCreateWindow;
import static org.lwjgl.glfw.GLFW.glfwGetCursorPos;
import static org.lwjgl.glfw.GLFW.glfwGetPrimaryMonitor;
import static org.lwjgl.glfw.GLFW.glfwGetVideoMode;
import static org.lwjgl.glfw.GLFW.glfwGetWindowSize;
import static org.lwjgl.glfw.GLFW.glfwInit;
import static org.lwjgl.glfw.GLFW.glfwMakeContextCurrent;
import static org.lwjgl.glfw.GLFW.glfwPollEvents;
import static org.lwjgl.glfw.GLFW.glfwSetCharCallback;
import static org.lwjgl.glfw.GLFW.glfwSetErrorCallback;
import static org.lwjgl.glfw.GLFW.glfwSetKeyCallback;
import static org.lwjgl.glfw.GLFW.glfwSetMouseButtonCallback;
import static org.lwjgl.glfw.GLFW.glfwSetWindowShouldClose;
import static org.lwjgl.glfw.GLFW.glfwSetWindowSizeCallback;
import static org.lwjgl.glfw.GLFW.glfwSwapBuffers;
import static org.lwjgl.glfw.GLFW.glfwSwapInterval;
import static org.lwjgl.glfw.GLFW.glfwTerminate;
import static org.lwjgl.glfw.GLFW.glfwWindowHint;
import static org.lwjgl.glfw.GLFW.glfwWindowShouldClose;
import static org.lwjgl.opengl.GL.createCapabilities;
import static org.lwjgl.opengl.GL11.GL_BLEND;
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_TEST;
import static org.lwjgl.opengl.GL11.GL_ONE_MINUS_SRC_ALPHA;
import static org.lwjgl.opengl.GL11.GL_SRC_ALPHA;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D;
import static org.lwjgl.opengl.GL11.GL_TRUE;
import static org.lwjgl.opengl.GL11.glBlendFunc;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glClearColor;
import static org.lwjgl.opengl.GL11.glEnable;
import static org.lwjgl.opengl.GL11.glViewport;
import org.joml.Vector3f;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.glfw.GLFWVidMode;
public class Window {
public static int
SCREEN_WIDTH,
SCREEN_HEIGHT;
public static float SCALE;
private long window;
private int width, height;
private boolean resized;
private int[] mouseButtons = new int[GLFW_MOUSE_BUTTON_LAST + 1];
private int[] keys = new int[GLFW_KEY_LAST + 1];
private StringBuilder curKeys = new StringBuilder();
public Window(String title, boolean vSync) {
init(0, 0, title, vSync, false, false, false);
}
public Window(String title, boolean vSync, boolean resizable, boolean decorated) {
init(800, 500, title, vSync, resizable, decorated, true);
}
public Window(int width, int height, String title, boolean vSync, boolean resizable, boolean decorated) {
init(width, height, title, vSync, resizable, decorated, false);
}
public void init(int lwidth, int lheight, String title, boolean vSync, boolean resizable, boolean decorated, boolean maximized) {
glfwSetErrorCallback(GLFWErrorCallback.createPrint(System.err));
for (int i = 0; i < mouseButtons.length; i++)
mouseButtons[i] = 0;
if(!glfwInit()) {
System.err.println("GLFW failed to initialize!");
System.exit(-1);
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_RESIZABLE, resizable ? GLFW_TRUE : GLFW_FALSE);
glfwWindowHint(GLFW_DECORATED, decorated ? GLFW_TRUE : GLFW_FALSE);
glfwWindowHint(GLFW_MAXIMIZED, maximized ? GLFW_TRUE : GLFW_FALSE);
GLFWVidMode vidMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
SCREEN_WIDTH = vidMode.width();
SCREEN_HEIGHT = vidMode.height();
SCALE = SCREEN_HEIGHT / 1080f;
if(lwidth == 0 || lheight == 0) {
width = vidMode.width();
height = vidMode.height();
window = glfwCreateWindow(width, height, title, glfwGetPrimaryMonitor(), 0);
}
else {
this.width = lwidth;
this.height = lheight;
window = glfwCreateWindow(width, height, title, 0, 0);
}
glfwMakeContextCurrent(window);
createCapabilities();
glfwSwapInterval(vSync ? 1 : 0);
glfwSetWindowSizeCallback(window, (long window, int w, int h) -> {
width = w;
height = h;
resized = true;
glViewport(0, 0, width, height);
});
glfwSetMouseButtonCallback(window, (long window, int button, int action, int mods) -> {
mouseButtons[button] = action;
});
glfwSetCharCallback(window, (long window, int codepoint) -> {
curKeys.append((char) codepoint);
});
glfwSetKeyCallback(window, (long window, int key, int scancode, int action, int mods) -> {
keys[key] = action;
if(action != GLFW_RELEASE && (key == GLFW_KEY_ENTER || key == GLFW_KEY_DELETE || key == GLFW_KEY_BACKSPACE || key == GLFW_KEY_RIGHT || key == GLFW_KEY_LEFT))
curKeys.append((char) key);
});
glClearColor(0, 0, 0, 1);
glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
int[] awidth = new int[1], aheight = new int[1];
glfwGetWindowSize(window, awidth, aheight);
width = awidth[0];
height = aheight[0];
}
public void update() {
for (int i = 0; i < mouseButtons.length; i++)
if (mouseButtons[i] == 1)
++mouseButtons[i];
for (int i = 0; i < keys.length; i++)
if (keys[i] == 1)
++keys[i];
curKeys.setLength(0);
resized = false;
glfwPollEvents();
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public void clear() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
public void swap() {
glfwSwapBuffers(window);
}
public void close() {
glfwSetWindowShouldClose(window, true);
}
public static void terminate() {
glfwTerminate();
}
public boolean shouldClose() {
return glfwWindowShouldClose(window);
}
public String getKeys() {
return curKeys.toString();
}
public int keyPressed(int keyCode) {
return keys[keyCode];
}
public Vector3f getMouseCoords(Camera camera) {
double[] x = new double[1], y = new double[1];
glfwGetCursorPos(window, x, y);
Vector3f ret = new Vector3f((float) x[0], (float) y[0], 0);
return ret.mul(camera.getWidth() / this.width, camera.getHeight() / this.height, 1);
}
public int mousePressed(int button) {
return mouseButtons[button];
}
public boolean wasResized() {
return resized;
}
}

View file

@ -0,0 +1,56 @@
package com.gnarly.engine.model;
import org.joml.Matrix4f;
import org.joml.Vector3f;
import com.gnarly.engine.display.Camera;
import com.gnarly.engine.shaders.Shader;
import com.gnarly.engine.shaders.Shader2cs;
import com.gnarly.engine.texture.Spritesheet;
public class CSRect extends Rect {
private Spritesheet texture;
private Shader2cs shader = Shader.SHADER2TM;
private int frame = 0;
private float r, g, b, a;
public CSRect(Camera camera, int fWidth, int fHeight, String path, float x, float y, float z, float width, float height, float rotation, boolean gui) {
super(camera, x, y, z, width, height, rotation, gui);
texture = new Spritesheet(fWidth, fHeight, path);
r = 1;
g = 1;
b = 1;
a = 1;
}
public void render() {
texture.bind();
shader.enable();
Matrix4f cmat = gui ? camera.getProjection() : camera.getMatrix();
shader.setMVP(cmat.translate(position.add(dims.x / 2, dims.y / 2, 0, new Vector3f())).rotateZ(rotation * 3.1415927f / 180).scale(dims).translate(-0.5f, -0.5f, 0));
shader.setFrame(frame, texture);
shader.setColor(r, g, b, a);
vao.render();
shader.disable();
texture.unbind();
}
public void setFrame(int frame) {
this.frame = frame;
}
public void setColor(float r, float g, float b) {
this.r = r;
this.g = g;
this.b = b;
}
public void setColor(float[] colors) {
this.r = colors[0];
this.g = colors[1];
this.b = colors[2];
this.a = colors[3];
}
}

View file

@ -0,0 +1,121 @@
package com.gnarly.engine.model;
import org.joml.Vector3f;
import com.gnarly.engine.display.Camera;
import com.gnarly.engine.shaders.Shader;
import com.gnarly.engine.shaders.Shader2c;
public class Circle {
private static Vao vao;
private Camera camera;
private Shader2c shader;
private Vector3f position;
private float radius;
private float r, g, b, a;
public Circle(Camera camera, float x, float y, float z, float radius) {
this.camera = camera;
position = new Vector3f(x, y, z);
this.radius = radius;
shader = Shader.SHADER2C;
r = 1;
g = 0;
b = 0;
a = 1;
if(vao == null)
initVao();
}
private void initVao() {
final int NUM_POINTS = 30;
float[] cVertices = new float[NUM_POINTS * 3];
int[] cIndices = new int[(NUM_POINTS - 2) * 3];
for (int i = 0; i < cVertices.length; i += 3) {
double angle = Math.PI * 2 * i / (NUM_POINTS * 3);
cVertices[i ] = (float) Math.cos(angle);
cVertices[i + 1] = (float) Math.sin(angle);
cVertices[i + 2] = 0;
}
for (int i = 0; i < cIndices.length; i += 3) {
cIndices[i ] = 0;
cIndices[i + 1] = i / 3 + 1;
cIndices[i + 2] = i / 3 + 2;
}
vao = new Vao(cVertices, cIndices);
}
public void render() {
shader.enable();
shader.setMVP(camera.getMatrix().translate(position).scale(radius));
shader.setColor(r, g, b, a);
vao.render();
}
public Vector3f getPosition() {
return position;
}
public void setX(float x) {
position.x = x;
}
public void setY(float y) {
position.y = y;
}
public void setZ(float z) {
position.z = z;
}
public void setPosition(float x, float y) {
position.x = x;
position.y = y;
}
public void setPosition(float x, float y, float z) {
position.x = x;
position.y = y;
position.z = z;
}
public void setPosition(Vector3f position) {
this.position.set(position);
}
public void translate(float x, float y) {
position.x += x;
position.y += y;
}
public void translate(float x, float y, float z) {
position.x += x;
position.y += y;
position.z += z;
}
public void translate(Vector3f position) {
this.position.add(position);
}
public void setRadius(float radius) {
this.radius = radius;
}
public void setDiameter(float diameter) {
this.radius = diameter / 2;
}
public void setColor(float r, float g, float b, float a) {
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
public boolean contains(Vector3f vector) {
return (position.sub(vector, new Vector3f()).lengthSquared() < radius * radius);
}
}

View file

@ -0,0 +1,37 @@
package com.gnarly.engine.model;
import org.joml.Matrix4f;
import org.joml.Vector3f;
import com.gnarly.engine.display.Camera;
import com.gnarly.engine.shaders.Shader;
import com.gnarly.engine.shaders.Shader2c;
public class ColRect extends Rect {
private Shader2c shader;
private float r, g, b, a;
public ColRect(Camera camera, float x, float y, float z, float width, float height, float r, float g, float b, float a, boolean gui) {
super(camera, x, y, z, width, height, 0, gui);
shader = Shader.SHADER2C;
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
public void render() {
shader.enable();
shader.setColor(r, g, b, a);
Matrix4f cmat = gui ? camera.getProjection() : camera.getMatrix();
shader.setMVP(cmat.translate(position.add(dims.x / 2, dims.y / 2, 0, new Vector3f())).rotateZ(rotation * 3.1415927f / 180).scale(dims).translate(-0.5f, -0.5f, 0));
vao.render();
shader.disable();
}
public void setOpacity(float opacity) {
a = opacity;
}
}

View file

@ -0,0 +1,118 @@
package com.gnarly.engine.model;
import org.joml.Vector3f;
import com.gnarly.engine.display.Camera;
import com.gnarly.engine.shaders.Shader;
import com.gnarly.engine.shaders.Shader2c;
public class Line {
private static Vao cap, rect;
private Camera camera;
private Shader2c shader;
private Vector3f position;
private float angle, length, thickness;
private float r, g, b, a;
public Line(Camera camera, float x1, float y1, float x2, float y2, float depth, float thickness) {
this.camera = camera;
shader = Shader.SHADER2C;
if(cap == null)
initVaos();
this.thickness = thickness;
position = new Vector3f(x1, y1, depth);
setPoints(x1, y1, x2, y2);
r = 1;
g = 1;
b = 1;
a = 1;
}
private void initVaos() {
float[] rVertices = {
0, 0.5f, 0,
0, -0.5f, 0,
1, -0.5f, 0,
1, 0.5f, 0
};
int rIndices[] = {
0, 1, 3,
1, 2, 3
};
rect = new Vao(rVertices, rIndices);
final int NUM_POINTS = 10;
float[] cVertices = new float[NUM_POINTS * 3];
int[] cIndices = new int[(NUM_POINTS - 2) * 3];
for (int i = 0; i < cVertices.length; i += 3) {
double angle = Math.PI * i / (NUM_POINTS * 3 - 3) + Math.PI / 2;
cVertices[i ] = (float) Math.cos(angle) / 2;
cVertices[i + 1] = (float) Math.sin(angle) / 2;
cVertices[i + 2] = 0;
}
for (int i = 0; i < cIndices.length; i += 3) {
cIndices[i ] = 0;
cIndices[i + 1] = i / 3 + 1;
cIndices[i + 2] = i / 3 + 2;
}
cap = new Vao(cVertices, cIndices);
}
public void render() {
shader.enable();
shader.setColor(r, g, b, a);
shader.setMVP(camera.getMatrix().translate(position).rotateZ(angle).scale(thickness));
cap.render();
shader.setMVP(camera.getMatrix().translate(position).rotateZ(angle).scale(length, thickness, 1));
rect.render();
shader.setMVP(camera.getMatrix().translate(position.add((float) (Math.cos(angle) * length), (float) (-Math.sin(Math.PI + angle) * length), 0, new Vector3f())).rotateZ((float) Math.PI).rotateZ(angle).scale(thickness));
cap.render();
shader.disable();
}
public void setAngle(float x, float y, float angle, float length) {
position.x = x;
position.y = y;
this.angle = angle;
this.length = length;
}
public void setPoints(float x1, float y1, float x2, float y2) {
float xl = x2 - x1;
float yl = y2 - y1;
length = (float) Math.sqrt(xl * xl + yl * yl);
if(x1 != x2) {
angle = (float) Math.atan(yl / xl);
if(xl < 0)
angle += Math.PI;
setAngle(x1, y1, angle, length);
}
else if(y1 > y2)
setAngle(x1, y1, (float) Math.PI * 1.5f, length);
else if(y1 < y2)
setAngle(x1, y1, (float) Math.PI * 0.5f, length);
else
setAngle(x1, y1, 0, 0);
}
public void setPoints(Vector3f p1, Vector3f p2) {
setPoints(p1.x, p1.y, p2.x, p2.y);
}
public void setThickness(float thickness) {
this.thickness = thickness;
}
public void setDepth(float z) {
position.z = z;
}
public void setColor(float r, float g, float b, float a) {
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
}

View file

@ -0,0 +1,111 @@
package com.gnarly.engine.model;
import org.joml.Vector2f;
import org.joml.Vector3f;
import com.gnarly.engine.display.Camera;
public class Rect {
protected static Vao vao;
protected Camera camera;
protected Vector3f dims;
protected Vector3f position;
protected float rotation;
protected boolean gui;
protected Rect(Camera camera, float x, float y, float z, float width, float height, float rotation, boolean gui) {
this.camera = camera;
dims = new Vector3f(width, height, 1);
position = new Vector3f(x, y, z);
this.rotation = rotation;
this.gui = gui;
if(vao == null) {
float vertices[] = {
1, 0, 0, // Top left
1, 1, 0, // Bottom left
0, 1, 0, // Bottom right
0, 0, 0 // Top right
};
int indices[] = {
0, 1, 3,
1, 2, 3
};
float[] texCoords = {
1, 0,
1, 1,
0, 1,
0, 0
};
vao = new Vao(vertices, indices);
vao.addAttrib(texCoords, 2);
}
}
public float getX() {
return position.x;
}
public float getY() {
return position.y;
}
public Vector3f getPosition() {
return new Vector3f(position);
}
public float getWidth() {
return dims.x;
}
public float getHeight() {
return dims.y;
}
public void set(float x, float y, float width, float height) {
position.x = x;
position.y = y;
dims.x = width;
dims.y = height;
}
public void setWidth(float width) {
dims.x = width;
}
public void setHeight(float height) {
dims.y = height;
}
public void setPosition(float x, float y, float z) {
position.set(x, y, z);
}
public void setPosition(Vector2f position) {
this.position.x = position.x;
this.position.y = position.y;
}
public void setPosition(Vector3f position) {
this.position.set(position);
}
public void translate(float x, float y, float z) {
position.add(x, y, z);
}
public void setRotation(float angle) {
rotation = angle;
}
public void rotate(float angle) {
rotation += angle;
}
public void sync(Vector3f position, Vector3f dims) {
this.position = position;
this.dims = dims;
}
}

View file

@ -0,0 +1,34 @@
package com.gnarly.engine.model;
import org.joml.Matrix4f;
import org.joml.Vector3f;
import com.gnarly.engine.display.Camera;
import com.gnarly.engine.shaders.Shader;
import com.gnarly.engine.shaders.Shader2t;
import com.gnarly.engine.texture.Texture;
public class TexRect extends Rect {
private Texture texture;
private Shader2t shader = Shader.SHADER2T;
public TexRect(Camera camera, String path, float x, float y, float z, float width, float height, float rotation, boolean gui) {
super(camera, x, y, z, width, height, rotation, gui);
texture = new Texture(path);
}
public void render() {
texture.bind();
shader.enable();
Matrix4f cmat = gui ? camera.getProjection() : camera.getMatrix();
shader.setMVP(cmat.translate(position.add(dims.x / 2, dims.y / 2, 0, new Vector3f())).rotateZ(rotation * 3.1415927f / 180).scale(dims).translate(-0.5f, -0.5f, 0));
vao.render();
shader.disable();
texture.unbind();
}
public void setColor(float r, float g, float b) {
shader.setColor(r, g, b, 1);
}
}

View file

@ -0,0 +1,63 @@
package com.gnarly.engine.model;
import static org.lwjgl.opengl.GL11.GL_FLOAT;
import static org.lwjgl.opengl.GL11.GL_TRIANGLES;
import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT;
import static org.lwjgl.opengl.GL11.glDrawElements;
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;
import static org.lwjgl.opengl.GL15.glBindBuffer;
import static org.lwjgl.opengl.GL15.glBufferData;
import static org.lwjgl.opengl.GL15.glDeleteBuffers;
import static org.lwjgl.opengl.GL15.glGenBuffers;
import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray;
import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray;
import static org.lwjgl.opengl.GL20.glVertexAttribPointer;
import static org.lwjgl.opengl.GL30.glBindVertexArray;
import static org.lwjgl.opengl.GL30.glDeleteVertexArrays;
import static org.lwjgl.opengl.GL30.glGenVertexArrays;
public class Vao {
private int numAttribs = 0;
private int vao, ibo, count;
private int[] vbos = new int[15];
public Vao(float[] vertices, int[] indices) {
vao = glGenVertexArrays();
glBindVertexArray(vao);
addAttrib(vertices, 3);
ibo = glGenBuffers();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices, GL_STATIC_DRAW);
count = indices.length;
}
public void addAttrib(float[] data, int size) {
int vbo = glGenBuffers();
vbos[numAttribs] = vbo;
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, data, GL_STATIC_DRAW);
glVertexAttribPointer(numAttribs, size, GL_FLOAT, false, 0, 0);
++numAttribs;
}
public void render() {
glBindVertexArray(vao);
for(int i = 0; i < numAttribs; ++i)
glEnableVertexAttribArray(i);
glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, 0);
for(int i = 0; i < numAttribs; ++i)
glDisableVertexAttribArray(i);
}
public void destroy() {
for(int vbo : vbos)
glDeleteBuffers(vbo);
glDeleteBuffers(ibo);
glDeleteVertexArrays(vao);
}
}

View file

@ -0,0 +1,101 @@
package com.gnarly.engine.shaders;
import static org.lwjgl.opengl.GL20.GL_COMPILE_STATUS;
import static org.lwjgl.opengl.GL20.GL_FRAGMENT_SHADER;
import static org.lwjgl.opengl.GL20.GL_VERTEX_SHADER;
import static org.lwjgl.opengl.GL20.glAttachShader;
import static org.lwjgl.opengl.GL20.glCompileShader;
import static org.lwjgl.opengl.GL20.glCreateProgram;
import static org.lwjgl.opengl.GL20.glCreateShader;
import static org.lwjgl.opengl.GL20.glDeleteProgram;
import static org.lwjgl.opengl.GL20.glDeleteShader;
import static org.lwjgl.opengl.GL20.glDetachShader;
import static org.lwjgl.opengl.GL20.glGetShaderInfoLog;
import static org.lwjgl.opengl.GL20.glGetShaderi;
import static org.lwjgl.opengl.GL20.glGetUniformLocation;
import static org.lwjgl.opengl.GL20.glLinkProgram;
import static org.lwjgl.opengl.GL20.glShaderSource;
import static org.lwjgl.opengl.GL20.glUniformMatrix4fv;
import static org.lwjgl.opengl.GL20.glUseProgram;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import org.joml.Matrix4f;
public abstract class Shader {
public static Shader2c SHADER2C;
public static Shader2t SHADER2T;
public static Shader2cs SHADER2TM;
protected int program;
protected int mvpLoc;
protected Shader(String vertPath, String fragPath) {
program = glCreateProgram();
int vert = loadShader(vertPath, GL_VERTEX_SHADER);
int frag = loadShader(fragPath, GL_FRAGMENT_SHADER);
glAttachShader(program, vert);
glAttachShader(program, frag);
glLinkProgram(program);
glDetachShader(program, vert);
glDetachShader(program, frag);
glDeleteShader(vert);
glDeleteShader(frag);
mvpLoc = glGetUniformLocation(program, "mvp");
}
private int loadShader(String path, int type) {
StringBuilder file = new StringBuilder();
try {
BufferedReader reader = new BufferedReader(new FileReader(new File(path)));
String line;
while((line = reader.readLine()) != null)
file.append(line + '\n');
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
String source = file.toString();
int shader = glCreateShader(type);
glShaderSource(shader, source);
glCompileShader(shader);
if(glGetShaderi(shader, GL_COMPILE_STATUS) != 1)
throw new RuntimeException("Failed to compile shader: " + path + "! " + glGetShaderInfoLog(shader));
return shader;
}
protected abstract void getUniforms();
public void setMVP(Matrix4f matrix) {
glUniformMatrix4fv(mvpLoc, false, matrix.get(new float[16]));
}
public void enable() {
glUseProgram(program);
}
public void disable() {
glUseProgram(0);
}
public void destroy() {
glDeleteProgram(program);
}
public static void init() {
SHADER2C = new Shader2c();
SHADER2T = new Shader2t();
SHADER2TM = new Shader2cs();
}
}

View file

@ -0,0 +1,23 @@
package com.gnarly.engine.shaders;
import static org.lwjgl.opengl.GL20.glGetUniformLocation;
import static org.lwjgl.opengl.GL20.glUniform4f;
public class Shader2c extends Shader {
int colorLoc;
protected Shader2c() {
super("res/shaders/s2c/vert.gls", "res/shaders/s2c/frag.gls");
getUniforms();
}
@Override
protected void getUniforms() {
colorLoc = glGetUniformLocation(program, "iColor");
}
public void setColor(float r, float g, float b, float a) {
glUniform4f(colorLoc, r, g, b, a);
}
}

View file

@ -0,0 +1,37 @@
package com.gnarly.engine.shaders;
import static org.lwjgl.opengl.GL20.glGetUniformLocation;
import static org.lwjgl.opengl.GL20.glUniform2f;
import static org.lwjgl.opengl.GL20.glUniform3f;
import static org.lwjgl.opengl.GL20.glUniform4f;
import static org.lwjgl.opengl.GL40.glUniform4d;
import com.gnarly.engine.texture.Spritesheet;
public class Shader2cs extends Shader {
int offsetLoc, dimsLoc, colorLoc;
protected Shader2cs() {
super("res/shaders/s2cs/vert.gls", "res/shaders/s2cs/frag.gls");
getUniforms();
}
@Override
protected void getUniforms() {
offsetLoc = glGetUniformLocation(program, "offset");
dimsLoc = glGetUniformLocation(program, "dims");
colorLoc = glGetUniformLocation(program, "iColor");
}
public void setFrame(int frame, Spritesheet map) {
int x = frame % 16;
int y = frame / 16;
glUniform2f(offsetLoc, x * map.getFWidth(), y * map.getFHeight());
glUniform2f(dimsLoc, map.getFWidth(), map.getFHeight());
}
public void setColor(float r, float g, float b, float a) {
glUniform4f(colorLoc, r, g, b, a);
}
}

View file

@ -0,0 +1,23 @@
package com.gnarly.engine.shaders;
import static org.lwjgl.opengl.GL20.glGetUniformLocation;
import static org.lwjgl.opengl.GL20.glUniform4f;
public class Shader2t extends Shader {
int colorLoc;
protected Shader2t() {
super("res/shaders/s2t/vert.gls", "res/shaders/s2t/frag.gls");
getUniforms();
}
@Override
protected void getUniforms() {
colorLoc = glGetUniformLocation(program, "iColor");
}
public void setColor(float r, float g, float b, float a) {
glUniform4f(colorLoc, r, g, b, a);
}
}

View file

@ -0,0 +1,57 @@
package com.gnarly.engine.texture;
public class Anim extends Texture {
private final float FRAME_WIDTH;
private final long NANO_PER_FRAME;
private final int NUM_FRAMES;
private int curFrame;
private long startTime;
private boolean playing;
public Anim(String path, int numFrames, int fps) {
super(path);
FRAME_WIDTH = 1f / (float) numFrames;
NANO_PER_FRAME = 1000000000l / fps;
this.curFrame = 0;
this.NUM_FRAMES = numFrames;
startTime = System.nanoTime();
playing = true;
}
@Override
public void bind() {
super.bind();
if(playing) {
int frame = (int) ((System.nanoTime() - startTime) / NANO_PER_FRAME);
curFrame = frame % NUM_FRAMES;
}
}
public void play() {
startTime = System.nanoTime();
playing = true;
}
public void pause() {
playing = false;
}
@Override
public int getWidth() {
return width / NUM_FRAMES;
}
public float getFrameWidth() {
return FRAME_WIDTH;
}
public float getOffset() {
return FRAME_WIDTH * curFrame;
}
public void setFrame(int frame) {
curFrame = frame;
}
}

View file

@ -0,0 +1,30 @@
package com.gnarly.engine.texture;
public class Spritesheet extends Texture {
int fWidth, fHeight;
public Spritesheet(int fWidth, int fHeight, String path) {
super(path);
this.fWidth = fWidth;
this.fHeight = fHeight;
}
@Override
public int getWidth() {
return width / fWidth;
}
@Override
public int getHeight() {
return height / fHeight;
}
public float getFWidth() {
return 1f / (float) fWidth;
}
public float getFHeight() {
return 1f / (float) fHeight;
}
}

View file

@ -0,0 +1,82 @@
package com.gnarly.engine.texture;
import static org.lwjgl.opengl.GL11.GL_CLAMP;
import static org.lwjgl.opengl.GL11.GL_NEAREST;
import static org.lwjgl.opengl.GL11.GL_RGBA;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_MAG_FILTER;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_MIN_FILTER;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_WRAP_S;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_WRAP_T;
import static org.lwjgl.opengl.GL11.GL_UNSIGNED_BYTE;
import static org.lwjgl.opengl.GL11.glBindTexture;
import static org.lwjgl.opengl.GL11.glDeleteTextures;
import static org.lwjgl.opengl.GL11.glGenTextures;
import static org.lwjgl.opengl.GL11.glTexImage2D;
import static org.lwjgl.opengl.GL11.glTexParameterf;
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 {
protected int id, width, height;
public Texture(String path) {
try {
BufferedImage bi = ImageIO.read(new File(path));
width = bi.getWidth();
height = bi.getHeight();
int[] pixels = bi.getRGB(0, 0, width, height, null, 0, width);
ByteBuffer buffer = BufferUtils.createByteBuffer(width * height * 4);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int pixel = pixels[i * width + j];
buffer.put((byte)((pixel >> 16) & 0xFF)); // Red
buffer.put((byte)((pixel >> 8) & 0xFF)); // Green
buffer.put((byte)((pixel ) & 0xFF)); // Blue
buffer.put((byte)((pixel >> 24) & 0xFF)); // Alpha
}
}
buffer.flip();
id = glGenTextures();
bind();
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
unbind();
} catch (IOException e) {
e.printStackTrace();
}
}
public void bind() {
glBindTexture(GL_TEXTURE_2D, id);
}
public void unbind() {
glBindTexture(GL_TEXTURE_2D, 0);
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public void destroy() {
glDeleteTextures(id);
}
}