Compare commits
17 Commits
c28ec45f8c
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| d3dc397353 | |||
| ad56624b51 | |||
| bb24efb856 | |||
| 6f731c2885 | |||
| dc50ee48c2 | |||
| 0e66f20423 | |||
| 2ed1763b83 | |||
| abd4597f69 | |||
| d2c33b529a | |||
| 9d74dfd613 | |||
| 73c4b507e2 | |||
| 1402cbcc47 | |||
| 1aa9546b73 | |||
| fbb38d1b86 | |||
| 78efce5cb3 | |||
| 5900117194 | |||
| 1002003168 |
5
README.md
Normal file
5
README.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
# Epic game
|
||||||
|
|
||||||
|
The game does NOTHING still
|
||||||
|
|
||||||
|
The setup NEVER ends
|
||||||
10
assets/shaders/fader.frag.glsl
Normal file
10
assets/shaders/fader.frag.glsl
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#version 330 core
|
||||||
|
|
||||||
|
in vec4 fColour;
|
||||||
|
out vec4 colour;
|
||||||
|
|
||||||
|
uniform float u_alpha;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
colour = vec4(fColour.xyz, 1.0 - u_alpha);
|
||||||
|
}
|
||||||
19
assets/shaders/texture.frag.glsl
Normal file
19
assets/shaders/texture.frag.glsl
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
#version 330 core
|
||||||
|
out vec4 FragColour;
|
||||||
|
|
||||||
|
in vec4 ourColour;
|
||||||
|
in vec2 TexCoord;
|
||||||
|
|
||||||
|
uniform sampler2D texture1;
|
||||||
|
uniform sampler2D texture2;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
vec4 tex1Color = texture(texture1, TexCoord);
|
||||||
|
vec4 tex2Color = texture(texture2, TexCoord);
|
||||||
|
|
||||||
|
if (tex2Color.a < 0.1)
|
||||||
|
tex2Color = tex1Color;
|
||||||
|
|
||||||
|
FragColour = mix(tex1Color, tex2Color, 0.4f);
|
||||||
|
}
|
||||||
15
assets/shaders/texture.vert.glsl
Normal file
15
assets/shaders/texture.vert.glsl
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#version 330 core
|
||||||
|
|
||||||
|
layout (location = 0) in vec3 aPos;
|
||||||
|
layout (location = 1) in vec4 aColour;
|
||||||
|
layout (location = 2) in vec2 aTexCoord;
|
||||||
|
|
||||||
|
out vec4 ourColour;
|
||||||
|
out vec2 TexCoord;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
gl_Position = vec4(aPos, 1.0);
|
||||||
|
ourColour = aColour;
|
||||||
|
TexCoord = aTexCoord;
|
||||||
|
}
|
||||||
BIN
assets/textures/flames.png
Normal file
BIN
assets/textures/flames.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.5 MiB |
BIN
assets/textures/plink.jpg
Normal file
BIN
assets/textures/plink.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 5.1 KiB |
BIN
assets/textures/plink.png
Normal file
BIN
assets/textures/plink.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 49 KiB |
1
pom.xml
1
pom.xml
@@ -10,7 +10,6 @@
|
|||||||
|
|
||||||
<name>game</name>
|
<name>game</name>
|
||||||
<description>A simple game.</description>
|
<description>A simple game.</description>
|
||||||
<!-- FIXME change it to the project's website -->
|
|
||||||
<url>http://www.hirw.org</url>
|
<url>http://www.hirw.org</url>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
|
|||||||
58
src/main/java/org/hirw/game/FaderShader.java
Normal file
58
src/main/java/org/hirw/game/FaderShader.java
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
package org.hirw.game;
|
||||||
|
|
||||||
|
import static org.lwjgl.opengl.GL20.*;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import org.hirw.game.util.Log;
|
||||||
|
import org.hirw.game.util.Time;
|
||||||
|
|
||||||
|
public class FaderShader extends Shader {
|
||||||
|
private static final String FADER_SHADER_FRAG_PATH = "assets/shaders/fader.frag.glsl";
|
||||||
|
private static final float DEFAULT_ALPHA_INCREASE_STEP = 1.0f / 3;
|
||||||
|
private final float INITIAL_ALPHA = 0.0f;
|
||||||
|
|
||||||
|
@Getter private final float alphaIncreaseStep;
|
||||||
|
@Getter @Setter private float alpha;
|
||||||
|
@Getter @Setter private int alphaUniformLocation;
|
||||||
|
|
||||||
|
public FaderShader(float alphaIncreaseStep) {
|
||||||
|
super(FADER_SHADER_FRAG_PATH, DEFAULT_VERT_PATH);
|
||||||
|
this.alpha = INITIAL_ALPHA;
|
||||||
|
this.alphaIncreaseStep = alphaIncreaseStep;
|
||||||
|
}
|
||||||
|
|
||||||
|
public FaderShader() {
|
||||||
|
this(DEFAULT_ALPHA_INCREASE_STEP);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void init() {
|
||||||
|
super.init();
|
||||||
|
findAlphaUniformLocation();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void update() {
|
||||||
|
super.update();
|
||||||
|
increaseAlpha();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void findAlphaUniformLocation() {
|
||||||
|
int location = glGetUniformLocation(getShaderProgramID(), "u_alpha");
|
||||||
|
if (location == -1) {
|
||||||
|
Log.warning("FaderShader", "Failed to get 'u_alpha' uniform location, trying to continue...");
|
||||||
|
} else {
|
||||||
|
setAlphaUniformLocation(location);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void increaseAlpha() {
|
||||||
|
if (getAlphaUniformLocation() == -1) return;
|
||||||
|
if (getAlpha() >= 1.0f) return;
|
||||||
|
|
||||||
|
setAlpha(getAlpha() + (getAlphaIncreaseStep() * Time.deltaTime()));
|
||||||
|
|
||||||
|
glUseProgram(getShaderProgramID());
|
||||||
|
glUniform1f(getAlphaUniformLocation(), getAlpha());
|
||||||
|
glUseProgram(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
118
src/main/java/org/hirw/game/Mesh.java
Normal file
118
src/main/java/org/hirw/game/Mesh.java
Normal file
@@ -0,0 +1,118 @@
|
|||||||
|
package org.hirw.game;
|
||||||
|
|
||||||
|
import static org.lwjgl.opengl.ARBVertexArrayObject.glBindVertexArray;
|
||||||
|
import static org.lwjgl.opengl.ARBVertexArrayObject.glGenVertexArrays;
|
||||||
|
import static org.lwjgl.opengl.GL20.*;
|
||||||
|
|
||||||
|
import java.nio.FloatBuffer;
|
||||||
|
import java.nio.IntBuffer;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import org.lwjgl.BufferUtils;
|
||||||
|
|
||||||
|
public class Mesh {
|
||||||
|
@Getter @Setter private int vaoID, vboID, eboID;
|
||||||
|
@Getter protected Shader shader;
|
||||||
|
@Getter float[] vertices;
|
||||||
|
@Getter int[] elements;
|
||||||
|
|
||||||
|
protected final int POSITION_SIZE = 3;
|
||||||
|
protected final int RGBA_SIZE = 4;
|
||||||
|
protected final int FLOAT_SIZE_IN_BYTES = Float.SIZE / Byte.SIZE;
|
||||||
|
|
||||||
|
protected static final float[] DEFAULT_VERTEX_ARRAY = {
|
||||||
|
0.5f, -0.5f, 0.0f, /* */ 1.0f, 0.0f, 0.0f, 1.0f,
|
||||||
|
-0.5f, 0.5f, 0.0f, /* */ 0.0f, 1.0f, 0.0f, 1.0f,
|
||||||
|
0.5f, 0.5f, 0.0f, /* */ 0.0f, 0.0f, 1.0f, 1.0f,
|
||||||
|
-0.5f, -0.5f, 0.0f, /* */ 1.0f, 1.0f, 0.0f, 1.0f,
|
||||||
|
};
|
||||||
|
|
||||||
|
protected static final int[] DEFAULT_ELEMENT_ARRAY = {
|
||||||
|
2, 1, 0,
|
||||||
|
0, 1, 3
|
||||||
|
};
|
||||||
|
|
||||||
|
public Mesh(Shader shader, float[] vertexArray, int[] elementArray) {
|
||||||
|
this.shader = shader;
|
||||||
|
this.vaoID = -1;
|
||||||
|
this.vboID = -1;
|
||||||
|
this.eboID = -1;
|
||||||
|
this.vertices = vertexArray;
|
||||||
|
this.elements = elementArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Mesh(Shader shader) {
|
||||||
|
this(shader, DEFAULT_VERTEX_ARRAY, DEFAULT_ELEMENT_ARRAY);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void init() {
|
||||||
|
initialiseVertexArrayObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void draw() {
|
||||||
|
getShader().use();
|
||||||
|
glDraw();
|
||||||
|
getShader().detach();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void log() {
|
||||||
|
System.out.println(this.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return String.format(
|
||||||
|
"VAO: %d, VBO: %d, EBO: %d, ShaderProgram: %d",
|
||||||
|
getEboID(), getVboID(), getEboID(), getShader().getShaderProgramID());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void glDraw() {
|
||||||
|
glBindVertexArray(this.vaoID);
|
||||||
|
glDrawElements(GL_TRIANGLES, getElements().length, GL_UNSIGNED_INT, 0);
|
||||||
|
glBindVertexArray(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initialiseVertexArrayObject() {
|
||||||
|
setVaoID(glGenVertexArrays());
|
||||||
|
glBindVertexArray(getVaoID());
|
||||||
|
|
||||||
|
initialiseVertexBufferObject();
|
||||||
|
initialiseElementBufferObject();
|
||||||
|
|
||||||
|
initialiseAttribPointers();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected int initialiseAttribPointers() {
|
||||||
|
int vertexSizeInBytes = calculateVertexSizeInBytes();
|
||||||
|
|
||||||
|
glVertexAttribPointer(0, POSITION_SIZE, GL_FLOAT, false, vertexSizeInBytes, 0);
|
||||||
|
glEnableVertexAttribArray(0);
|
||||||
|
|
||||||
|
glVertexAttribPointer(
|
||||||
|
1, RGBA_SIZE, GL_FLOAT, false, vertexSizeInBytes, POSITION_SIZE * FLOAT_SIZE_IN_BYTES);
|
||||||
|
glEnableVertexAttribArray(1);
|
||||||
|
|
||||||
|
return vertexSizeInBytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected int calculateVertexSizeInBytes() {
|
||||||
|
return (POSITION_SIZE + RGBA_SIZE) * FLOAT_SIZE_IN_BYTES;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initialiseVertexBufferObject() {
|
||||||
|
FloatBuffer vertexBuffer = BufferUtils.createFloatBuffer(getVertices().length);
|
||||||
|
vertexBuffer.put(getVertices()).flip();
|
||||||
|
|
||||||
|
setVboID(glGenBuffers());
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, getVboID());
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, vertexBuffer, GL_STATIC_DRAW);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initialiseElementBufferObject() {
|
||||||
|
IntBuffer elementBuffer = BufferUtils.createIntBuffer(getElements().length);
|
||||||
|
elementBuffer.put(getElements()).flip();
|
||||||
|
|
||||||
|
setEboID(glGenBuffers());
|
||||||
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, getEboID());
|
||||||
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, elementBuffer, GL_STATIC_DRAW);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,19 @@
|
|||||||
package org.hirw.game;
|
package org.hirw.game;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
public abstract class Scene {
|
public abstract class Scene {
|
||||||
public Scene() {}
|
@Getter private final SceneType sceneType;
|
||||||
|
|
||||||
abstract void update();
|
public Scene(SceneType sceneType) {
|
||||||
|
this.sceneType = sceneType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract void init();
|
||||||
|
|
||||||
|
public abstract void update();
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return getSceneType().toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,8 +3,11 @@ package org.hirw.game;
|
|||||||
import java.util.EnumMap;
|
import java.util.EnumMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
import org.hirw.game.util.Log;
|
||||||
|
|
||||||
|
public final class SceneManager {
|
||||||
|
private static final SceneType DEFAULT_SCENE_TYPE = SceneType.SPLASH;
|
||||||
|
|
||||||
final class SceneManager {
|
|
||||||
private static final EnumMap<SceneType, Scene> SCENES =
|
private static final EnumMap<SceneType, Scene> SCENES =
|
||||||
new EnumMap<>(
|
new EnumMap<>(
|
||||||
Map.of(
|
Map.of(
|
||||||
@@ -12,9 +15,42 @@ final class SceneManager {
|
|||||||
SceneType.MENU, new SplashScene(),
|
SceneType.MENU, new SplashScene(),
|
||||||
SceneType.GAME, new SplashScene()));
|
SceneType.GAME, new SplashScene()));
|
||||||
|
|
||||||
@Getter private static Scene scene = SCENES.get(SceneType.SPLASH);
|
@Getter private static Scene scene;
|
||||||
|
|
||||||
public static void setScene(SceneType sType) {
|
public static void init() {
|
||||||
scene = SCENES.get(sType);
|
setScene(DEFAULT_SCENE_TYPE);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setScene(SceneType sceneType) {
|
||||||
|
Scene newScene = SCENES.get(sceneType);
|
||||||
|
|
||||||
|
if (scene == newScene) {
|
||||||
|
Log.warning("SceneManager", sameSceneWarningString());
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
scene = newScene;
|
||||||
|
}
|
||||||
|
|
||||||
|
scene.init();
|
||||||
|
logSceneChange();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void update() {
|
||||||
|
if (scene.getSceneType() == null) return;
|
||||||
|
|
||||||
|
getScene().update();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void logSceneChange() {
|
||||||
|
SceneType newSceneType = getScene().getSceneType();
|
||||||
|
String loadedSceneMessage = String.format("Changed to scene '%s'", newSceneType.toString());
|
||||||
|
|
||||||
|
Log.success("SceneManager", loadedSceneMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String sameSceneWarningString() {
|
||||||
|
return String.format(
|
||||||
|
"Tried to switch to scene '%s' but that scene is already loaded",
|
||||||
|
scene.getSceneType().toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,5 +3,5 @@ package org.hirw.game;
|
|||||||
public enum SceneType {
|
public enum SceneType {
|
||||||
SPLASH,
|
SPLASH,
|
||||||
MENU,
|
MENU,
|
||||||
GAME
|
GAME,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,8 +26,8 @@ public class Shader {
|
|||||||
ShaderType.FRAG, GL_FRAGMENT_SHADER,
|
ShaderType.FRAG, GL_FRAGMENT_SHADER,
|
||||||
ShaderType.VERT, GL_VERTEX_SHADER));
|
ShaderType.VERT, GL_VERTEX_SHADER));
|
||||||
|
|
||||||
private static final String DEFAULT_FRAG_PATH = "assets/shaders/default.frag.glsl";
|
protected static final String DEFAULT_FRAG_PATH = "assets/shaders/default.frag.glsl";
|
||||||
private static final String DEFAULT_VERT_PATH = "assets/shaders/default.vert.glsl";
|
protected static final String DEFAULT_VERT_PATH = "assets/shaders/default.vert.glsl";
|
||||||
|
|
||||||
@Getter private String vertexSource;
|
@Getter private String vertexSource;
|
||||||
@Getter private String fragmentSource;
|
@Getter private String fragmentSource;
|
||||||
@@ -40,6 +40,10 @@ public class Shader {
|
|||||||
this.vertexSource = readFromFile(vertPath);
|
this.vertexSource = readFromFile(vertPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Shader(String path) {
|
||||||
|
this(path + ".frag.glsl", path + ".vert.glsl");
|
||||||
|
}
|
||||||
|
|
||||||
public Shader() {
|
public Shader() {
|
||||||
this(DEFAULT_FRAG_PATH, DEFAULT_VERT_PATH);
|
this(DEFAULT_FRAG_PATH, DEFAULT_VERT_PATH);
|
||||||
}
|
}
|
||||||
@@ -50,6 +54,16 @@ public class Shader {
|
|||||||
createProgram();
|
createProgram();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void use() {
|
||||||
|
glUseProgram(getShaderProgramID());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void detach() {
|
||||||
|
glUseProgram(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void update() {}
|
||||||
|
|
||||||
private void compileShader(ShaderType shaderType) {
|
private void compileShader(ShaderType shaderType) {
|
||||||
int shaderID = glCreateShader(SHADERS.get(shaderType));
|
int shaderID = glCreateShader(SHADERS.get(shaderType));
|
||||||
|
|
||||||
@@ -69,7 +83,7 @@ public class Shader {
|
|||||||
if (glGetShaderi(shaderID, GL_COMPILE_STATUS) == GL_FALSE) {
|
if (glGetShaderi(shaderID, GL_COMPILE_STATUS) == GL_FALSE) {
|
||||||
int len = glGetShaderi(shaderID, GL_INFO_LOG_LENGTH);
|
int len = glGetShaderi(shaderID, GL_INFO_LOG_LENGTH);
|
||||||
Log.error(
|
Log.error(
|
||||||
"Shader initialisation",
|
"Shader",
|
||||||
String.format(
|
String.format(
|
||||||
"Failed to compile %s shader: %s",
|
"Failed to compile %s shader: %s",
|
||||||
shaderType.toString(), glGetShaderInfoLog(shaderID, len)));
|
shaderType.toString(), glGetShaderInfoLog(shaderID, len)));
|
||||||
@@ -77,21 +91,23 @@ public class Shader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void createProgram() {
|
private void createProgram() {
|
||||||
setShaderProgramID(glCreateProgram());
|
int id = glCreateProgram();
|
||||||
glAttachShader(shaderProgramID, getVertexID());
|
glAttachShader(id, getVertexID());
|
||||||
glAttachShader(shaderProgramID, getFragmentID());
|
glAttachShader(id, getFragmentID());
|
||||||
glLinkProgram(shaderProgramID);
|
glLinkProgram(id);
|
||||||
|
|
||||||
int success = glGetProgrami(getShaderProgramID(), GL_LINK_STATUS);
|
int success = glGetProgrami(id, GL_LINK_STATUS);
|
||||||
if (success == GL_FALSE) {
|
if (success == GL_FALSE) {
|
||||||
int len = glGetProgrami(getShaderProgramID(), GL_INFO_LOG_LENGTH);
|
int len = glGetProgrami(id, GL_INFO_LOG_LENGTH);
|
||||||
|
|
||||||
Log.error(
|
Log.error(
|
||||||
"Shader initialisation",
|
"Shader",
|
||||||
String.format(
|
String.format("Failed to create Shader Program: %s", glGetShaderInfoLog(id, len)));
|
||||||
"Failed to create Shader Program: %s",
|
} else {
|
||||||
glGetShaderInfoLog(getShaderProgramID(), len)));
|
Log.success("Shader", "Shader Program created.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setShaderProgramID(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String readFromFile(String stringFilePath) {
|
private String readFromFile(String stringFilePath) {
|
||||||
@@ -102,12 +118,9 @@ public class Shader {
|
|||||||
source = Files.readString(filePath);
|
source = Files.readString(filePath);
|
||||||
|
|
||||||
} catch (NoSuchFileException | InvalidPathException e) {
|
} catch (NoSuchFileException | InvalidPathException e) {
|
||||||
Log.error(
|
Log.error("Shader", "Couldn't open file (probably a bad path): " + stringFilePath);
|
||||||
"Shader initialisation", "Couldn't open file (probably a bad path): " + stringFilePath);
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.error(
|
Log.error("Shader", "An IO Exception occured while reading from file: " + stringFilePath);
|
||||||
"Shader initialisation",
|
|
||||||
"An IO Exception occured while reading from file: " + stringFilePath);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return source;
|
return source;
|
||||||
|
|||||||
@@ -1,19 +1,72 @@
|
|||||||
package org.hirw.game;
|
package org.hirw.game;
|
||||||
|
|
||||||
import static org.lwjgl.opengl.GL11.*;
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
import java.util.Objects;
|
|
||||||
import org.hirw.game.util.Time;
|
|
||||||
|
|
||||||
public class SplashScene extends Scene {
|
public class SplashScene extends Scene {
|
||||||
private float ramp = 0.0f;
|
@Getter public final SceneType SCENE_TYPE = SceneType.SPLASH;
|
||||||
|
|
||||||
|
@Getter @Setter private Mesh screenCover;
|
||||||
|
@Getter @Setter private FaderShader screenCoverFaderShader;
|
||||||
|
@Getter @Setter private Mesh logo;
|
||||||
|
|
||||||
|
public SplashScene() {
|
||||||
|
super(SceneType.SPLASH);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void init() {
|
||||||
|
createScreenCover();
|
||||||
|
createLogo();
|
||||||
|
}
|
||||||
|
|
||||||
public void update() {
|
public void update() {
|
||||||
if (Objects.isNull(Window.get().getGlfwWindow())) {
|
getLogo().draw();
|
||||||
return;
|
getScreenCoverFaderShader().update();
|
||||||
}
|
getScreenCover().draw();
|
||||||
|
|
||||||
glClearColor(this.ramp, this.ramp, this.ramp, 0.0f);
|
|
||||||
this.ramp += 0.5f * Time.deltaTime();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void createScreenCover() {
|
||||||
|
FaderShader faderShader = new FaderShader();
|
||||||
|
faderShader.init();
|
||||||
|
Mesh screenCover = new Mesh(faderShader, screenCoverRectVertices, screenCoverRectElements);
|
||||||
|
screenCover.init();
|
||||||
|
setScreenCoverFaderShader(faderShader);
|
||||||
|
setScreenCover(screenCover);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createLogo() {
|
||||||
|
Shader texturedShader = new Shader("assets/shaders/texture");
|
||||||
|
texturedShader.init();
|
||||||
|
Texture logoTexture = new Texture(225, 225, "assets/textures/plink.png");
|
||||||
|
Texture flameTexture = new Texture(2500, 2500, "assets/textures/flames.png");
|
||||||
|
logoTexture.init();
|
||||||
|
flameTexture.init();
|
||||||
|
Mesh logoMesh =
|
||||||
|
new TexturedMesh(
|
||||||
|
texturedShader,
|
||||||
|
new Texture[] {logoTexture, flameTexture},
|
||||||
|
logoRectVertices,
|
||||||
|
screenCoverRectElements);
|
||||||
|
logoMesh.init();
|
||||||
|
setLogo(logoMesh);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final float[] screenCoverRectVertices = {
|
||||||
|
1.0f, 1.0f, 0.0f, /* */ 0.0f, 0.0f, 0.0f, 0.0f, // 0 Top Right
|
||||||
|
1.0f, -1.0f, 0.0f, /* */ 0.0f, 0.0f, 0.0f, 0.0f, // 1 Bottom Right
|
||||||
|
-1.0f, -1.0f, 0.0f, /* */ 0.0f, 0.0f, 0.0f, 0.0f, // 2 Bottom Left
|
||||||
|
-1.0f, 1.0f, 0.0f, /* */ 0.0f, 0.0f, 0.0f, 0.0f, // 3 Top Left
|
||||||
|
};
|
||||||
|
|
||||||
|
private static final int[] screenCoverRectElements = {
|
||||||
|
0, 1, 3,
|
||||||
|
1, 2, 3
|
||||||
|
};
|
||||||
|
|
||||||
|
private static final float[] logoRectVertices = {
|
||||||
|
0.1f, 0.1f, 0.0f, /* */ 0.0f, 0.0f, 0.0f, 0.0f, /* */ 1.0f, 1.0f, // 0 Top Right
|
||||||
|
0.1f, -0.1f, 0.0f, /* */ 0.0f, 0.0f, 0.0f, 0.0f, /* */ 1.0f, 0.0f, // 1 Bottom Right
|
||||||
|
-0.1f, -0.1f, 0.0f, /* */ 0.0f, 0.0f, 0.0f, 0.0f, /* */ 0.0f, 0.0f, // 2 Bottom Left
|
||||||
|
-0.1f, 0.1f, 0.0f, /* */ 0.0f, 0.0f, 0.0f, 0.0f, /* */ 0.0f, 1.0f, // 3 Top Left
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
105
src/main/java/org/hirw/game/Texture.java
Normal file
105
src/main/java/org/hirw/game/Texture.java
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
package org.hirw.game;
|
||||||
|
|
||||||
|
import static org.lwjgl.opengl.GL11.*;
|
||||||
|
import static org.lwjgl.opengl.GL12.*;
|
||||||
|
import static org.lwjgl.stb.STBImage.stbi_image_free;
|
||||||
|
import static org.lwjgl.stb.STBImage.stbi_load;
|
||||||
|
import static org.lwjgl.stb.STBImage.stbi_set_flip_vertically_on_load;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
public class Texture {
|
||||||
|
@Getter private int width;
|
||||||
|
@Getter private int height;
|
||||||
|
@Getter private String texturePath;
|
||||||
|
@Getter @Setter private int textureID;
|
||||||
|
@Getter @Setter private int channelCount;
|
||||||
|
@Getter @Setter private int internalFormat;
|
||||||
|
|
||||||
|
public Texture(int width, int height, String texturePath) {
|
||||||
|
this.width = width;
|
||||||
|
this.height = height;
|
||||||
|
this.texturePath = texturePath;
|
||||||
|
guessChannelCountAndInternalFormat();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void guessChannelCountAndInternalFormat() {
|
||||||
|
int fileExtensionIndex = getTexturePath().lastIndexOf(".");
|
||||||
|
if (fileExtensionIndex == -1) {
|
||||||
|
// No file extension, so just assume 4 channels (.png -> RGBA)
|
||||||
|
setChannelCount(4);
|
||||||
|
setInternalFormat(GL_RGBA);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String fileExtension = getTexturePath().substring(fileExtensionIndex + 1);
|
||||||
|
switch (fileExtension) {
|
||||||
|
case "png":
|
||||||
|
setChannelCount(4);
|
||||||
|
setInternalFormat(GL_RGBA);
|
||||||
|
break;
|
||||||
|
case "jpg":
|
||||||
|
setChannelCount(3);
|
||||||
|
setInternalFormat(GL_RGB);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
// just pray at this point
|
||||||
|
setChannelCount(3);
|
||||||
|
setInternalFormat(GL_RGB);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void init() {
|
||||||
|
ByteBuffer imageBytes = loadImageBytes();
|
||||||
|
createTexture(imageBytes);
|
||||||
|
stbi_image_free(imageBytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ByteBuffer loadImageBytes() {
|
||||||
|
stbi_set_flip_vertically_on_load(true);
|
||||||
|
return stbi_load(
|
||||||
|
getTexturePath(),
|
||||||
|
new int[getWidth()],
|
||||||
|
new int[getHeight()],
|
||||||
|
new int[getChannelCount()],
|
||||||
|
getChannelCount());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createTexture(ByteBuffer imageBytes) {
|
||||||
|
setTextureID(glGenTextures());
|
||||||
|
glBindTexture(GL_TEXTURE_2D, getTextureID());
|
||||||
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||||
|
|
||||||
|
setupFilterParameters();
|
||||||
|
setupWrapParameters();
|
||||||
|
|
||||||
|
glTexImage2D(
|
||||||
|
GL_TEXTURE_2D,
|
||||||
|
0,
|
||||||
|
getInternalFormat(),
|
||||||
|
getWidth(),
|
||||||
|
getHeight(),
|
||||||
|
0,
|
||||||
|
getInternalFormat(),
|
||||||
|
GL_UNSIGNED_BYTE,
|
||||||
|
imageBytes);
|
||||||
|
// glGenerateMipmap(GL_TEXTURE_2D);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupFilterParameters() {
|
||||||
|
// https://github.com/mattdesl/lwjgl-basics/wiki/textures#texture-parameters
|
||||||
|
// Set the minification and Magnifiation filters:
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupWrapParameters() {
|
||||||
|
// https://github.com/mattdesl/lwjgl-basics/wiki/textures#texture-parameters
|
||||||
|
// Each Vertex has many attributes, including Position (x, y) and Texture Coordinates (s, t).
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||||
|
}
|
||||||
|
}
|
||||||
47
src/main/java/org/hirw/game/TexturedMesh.java
Normal file
47
src/main/java/org/hirw/game/TexturedMesh.java
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
package org.hirw.game;
|
||||||
|
|
||||||
|
import static org.lwjgl.opengl.GL11.*;
|
||||||
|
import static org.lwjgl.opengl.GL20.*;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
public class TexturedMesh extends Mesh {
|
||||||
|
@Getter private Texture[] textures;
|
||||||
|
|
||||||
|
private final int TEXTURE_COORDS_SIZE = 2;
|
||||||
|
|
||||||
|
public TexturedMesh(Shader shader, Texture[] textures, float[] vertexArray, int[] elementArray) {
|
||||||
|
super(shader, vertexArray, elementArray);
|
||||||
|
this.textures = textures;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TexturedMesh(Shader shader, Texture[] textures) {
|
||||||
|
this(shader, textures, DEFAULT_VERTEX_ARRAY, DEFAULT_ELEMENT_ARRAY);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected int initialiseAttribPointers() {
|
||||||
|
int vertexSizeInBytes = super.initialiseAttribPointers();
|
||||||
|
|
||||||
|
int offset = (POSITION_SIZE + RGBA_SIZE) * FLOAT_SIZE_IN_BYTES;
|
||||||
|
glVertexAttribPointer(2, TEXTURE_COORDS_SIZE, GL_FLOAT, false, vertexSizeInBytes, offset);
|
||||||
|
glEnableVertexAttribArray(2);
|
||||||
|
|
||||||
|
return vertexSizeInBytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected int calculateVertexSizeInBytes() {
|
||||||
|
return (POSITION_SIZE + RGBA_SIZE + TEXTURE_COORDS_SIZE) * FLOAT_SIZE_IN_BYTES;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void glDraw() {
|
||||||
|
for (int i = 0; i < textures.length; i++) {
|
||||||
|
int textureUnitIndexStart = GL_TEXTURE0;
|
||||||
|
int textureUnitIndex = textureUnitIndexStart + i;
|
||||||
|
glActiveTexture(textureUnitIndex);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, getTextures()[i].getTextureID());
|
||||||
|
String uniformName = "texture" + Integer.toString(i + 1);
|
||||||
|
glUniform1i(glGetUniformLocation(getShader().getShaderProgramID(), uniformName), i);
|
||||||
|
}
|
||||||
|
super.glDraw();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ import static org.lwjgl.opengl.GL11.*;
|
|||||||
import static org.lwjgl.system.MemoryUtil.*;
|
import static org.lwjgl.system.MemoryUtil.*;
|
||||||
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
import org.hirw.game.util.Time;
|
import org.hirw.game.util.Time;
|
||||||
import org.lwjgl.Version;
|
import org.lwjgl.Version;
|
||||||
import org.lwjgl.glfw.*;
|
import org.lwjgl.glfw.*;
|
||||||
@@ -13,11 +14,14 @@ import org.lwjgl.opengl.*;
|
|||||||
|
|
||||||
public class Window {
|
public class Window {
|
||||||
private int width, height;
|
private int width, height;
|
||||||
private final String title;
|
@Getter private String title;
|
||||||
@Getter private long glfwWindow;
|
@Getter private long glfwWindow;
|
||||||
|
|
||||||
private static Window window = null;
|
private static Window window = null;
|
||||||
|
|
||||||
|
@Getter @Setter private Shader shader;
|
||||||
|
@Getter @Setter private Mesh mesh;
|
||||||
|
|
||||||
private Window() {
|
private Window() {
|
||||||
this.width = 1280;
|
this.width = 1280;
|
||||||
this.height = 720;
|
this.height = 720;
|
||||||
@@ -25,26 +29,31 @@ public class Window {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static Window get() {
|
public static Window get() {
|
||||||
if (Window.window == null) {
|
if (window == null) {
|
||||||
Window.window = new Window();
|
window = new Window();
|
||||||
}
|
}
|
||||||
|
return window;
|
||||||
return Window.window;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void blastOff() {
|
public void blastOff() {
|
||||||
setup();
|
init();
|
||||||
loop();
|
loop();
|
||||||
cleanUp();
|
cleanUp();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void init() {
|
||||||
|
logVersion();
|
||||||
|
createWindow();
|
||||||
|
createShader();
|
||||||
|
createMesh();
|
||||||
|
SceneManager.init();
|
||||||
|
}
|
||||||
|
|
||||||
private void logVersion() {
|
private void logVersion() {
|
||||||
System.out.println("LWJGL Version: " + Version.getVersion());
|
System.out.println("LWJGL Version: " + Version.getVersion());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setup() {
|
private void createWindow() {
|
||||||
logVersion();
|
|
||||||
|
|
||||||
GLFWErrorCallback.createPrint(System.err).set();
|
GLFWErrorCallback.createPrint(System.err).set();
|
||||||
if (!glfwInit()) throw new IllegalStateException("Unable to initialize GLFW");
|
if (!glfwInit()) throw new IllegalStateException("Unable to initialize GLFW");
|
||||||
|
|
||||||
@@ -52,23 +61,9 @@ public class Window {
|
|||||||
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
|
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
|
||||||
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
|
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
|
||||||
|
|
||||||
glfwWindow = glfwCreateWindow(this.width, this.height, "Hello World!", NULL, NULL);
|
glfwWindow = glfwCreateWindow(this.width, this.height, getTitle(), NULL, NULL);
|
||||||
if (glfwWindow == NULL) throw new RuntimeException("Failed to create the GLFW window");
|
if (glfwWindow == NULL) throw new RuntimeException("Failed to create the GLFW window");
|
||||||
|
|
||||||
// glfwSetKeyCallback(
|
|
||||||
// glfwWindow,
|
|
||||||
// (glfwWindow, key, scancode, action, mods) -> {
|
|
||||||
// if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE)
|
|
||||||
// glfwSetWindowShouldClose(glfwWindow, true);
|
|
||||||
// });
|
|
||||||
//
|
|
||||||
// try (MemoryStack stack = stackPush()) {
|
|
||||||
// IntBuffer pWidth = stack.mallocInt(1);
|
|
||||||
// IntBuffer pHeight = stack.mallocInt(1);
|
|
||||||
// glfwGetWindowSize(glfwWindow, pWidth, pHeight);
|
|
||||||
// GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
|
|
||||||
// }
|
|
||||||
|
|
||||||
glfwSetCursorPosCallback(glfwWindow, Mouse::cursorPositionCallback);
|
glfwSetCursorPosCallback(glfwWindow, Mouse::cursorPositionCallback);
|
||||||
glfwSetMouseButtonCallback(glfwWindow, Mouse::mouseButtonCallback);
|
glfwSetMouseButtonCallback(glfwWindow, Mouse::mouseButtonCallback);
|
||||||
glfwSetKeyCallback(glfwWindow, Keyboard::keyCallback);
|
glfwSetKeyCallback(glfwWindow, Keyboard::keyCallback);
|
||||||
@@ -79,16 +74,33 @@ public class Window {
|
|||||||
glfwShowWindow(glfwWindow);
|
glfwShowWindow(glfwWindow);
|
||||||
|
|
||||||
GL.createCapabilities();
|
GL.createCapabilities();
|
||||||
glClearColor(0.0f, 0.0f, 2.0f, 0.0f);
|
|
||||||
Shader someShader = new Shader();
|
glEnable(GL_BLEND);
|
||||||
someShader.init();
|
glEnable(GL_TEXTURE_2D);
|
||||||
|
|
||||||
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||||
|
|
||||||
|
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createShader() {
|
||||||
|
Shader shader = new Shader();
|
||||||
|
setShader(shader);
|
||||||
|
shader.init();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createMesh() {
|
||||||
|
Mesh mesh = new Mesh(getShader());
|
||||||
|
setMesh(mesh);
|
||||||
|
mesh.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loop() {
|
private void loop() {
|
||||||
while (!glfwWindowShouldClose(glfwWindow)) {
|
while (!glfwWindowShouldClose(glfwWindow)) {
|
||||||
Time.update();
|
Time.update();
|
||||||
SceneManager.getScene().update();
|
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
getMesh().draw();
|
||||||
|
SceneManager.update();
|
||||||
glfwSwapBuffers(glfwWindow);
|
glfwSwapBuffers(glfwWindow);
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
package org.hirw.game.util;
|
package org.hirw.game.util;
|
||||||
|
|
||||||
|
import java.util.EnumMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public final class Log {
|
public final class Log {
|
||||||
private interface Colours {
|
private interface Colours {
|
||||||
final String BLACK = "\u001B[30m";
|
final String BLACK = "\u001B[30m";
|
||||||
@@ -11,6 +14,8 @@ public final class Log {
|
|||||||
final String YELLOW = "\u001B[33m";
|
final String YELLOW = "\u001B[33m";
|
||||||
final String YELLOW_BG = "\u001B[43m";
|
final String YELLOW_BG = "\u001B[43m";
|
||||||
final String BLUE = "\u001B[34m";
|
final String BLUE = "\u001B[34m";
|
||||||
|
final String BLUE_BG = "\u001B[44m";
|
||||||
|
final String PURPLE = "\u001B[35m";
|
||||||
final String PURPLE_BG = "\u001B[45m";
|
final String PURPLE_BG = "\u001B[45m";
|
||||||
final String CYAN = "\u001B[36m";
|
final String CYAN = "\u001B[36m";
|
||||||
final String CYAN_BG = "\u001B[46m";
|
final String CYAN_BG = "\u001B[46m";
|
||||||
@@ -19,17 +24,45 @@ public final class Log {
|
|||||||
final String ANSI_RESET = "\u001B[0m";
|
final String ANSI_RESET = "\u001B[0m";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static final EnumMap<LogType, String> COLOUR_FROM_LOG_TYPE =
|
||||||
|
new EnumMap<>(
|
||||||
|
Map.of(
|
||||||
|
LogType.SUCCESS, Colours.GREEN,
|
||||||
|
LogType.ERROR, Colours.RED,
|
||||||
|
LogType.WARNING, Colours.PURPLE,
|
||||||
|
LogType.DEBUG, Colours.WHITE));
|
||||||
|
|
||||||
public static void error(String errorStage, String errorDescription) {
|
public static void error(String errorStage, String errorDescription) {
|
||||||
String fancyError = String.format("[%s] ", colourisedString(Colours.RED, "ERROR"));
|
System.out.println(formatString(LogType.ERROR, errorStage, errorDescription));
|
||||||
String fancyErrorStage = colourisedString(Colours.YELLOW, String.format("<%s> ", errorStage));
|
|
||||||
System.err.println(fancyError + fancyErrorStage + errorDescription);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String colourisedString(String colour, String string) {
|
public static void success(String successStage, String successDescription) {
|
||||||
|
System.out.println(formatString(LogType.SUCCESS, successStage, successDescription));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void warning(String warningStage, String warningDescription) {
|
||||||
|
System.out.println(formatString(LogType.WARNING, warningStage, warningDescription));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void debug(String debugMessage) {
|
||||||
|
System.out.println(formatString(LogType.DEBUG, "DEBUG", debugMessage));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String formatString(LogType logType, String stage, String description) {
|
||||||
|
String formattedType = String.format("[%s]", colouriseString(logType, logType.toString()));
|
||||||
|
String formattedStage = colouriseString(Colours.YELLOW, String.format("<%s>", stage));
|
||||||
|
return String.format("%s %s %s", formattedType, formattedStage, description);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String colouriseString(LogType logType, String string) {
|
||||||
|
return COLOUR_FROM_LOG_TYPE.get(logType) + string + Colours.ANSI_RESET;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String colouriseString(String colour, String string) {
|
||||||
return colour + string + Colours.ANSI_RESET;
|
return colour + string + Colours.ANSI_RESET;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String colourisedString(String colour, String otherColour, String string) {
|
private static String colouriseString(String colour, String otherColour, String string) {
|
||||||
return colour + otherColour + string + Colours.ANSI_RESET;
|
return colour + otherColour + string + Colours.ANSI_RESET;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
8
src/main/java/org/hirw/game/util/LogType.java
Normal file
8
src/main/java/org/hirw/game/util/LogType.java
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
package org.hirw.game.util;
|
||||||
|
|
||||||
|
public enum LogType {
|
||||||
|
SUCCESS,
|
||||||
|
WARNING,
|
||||||
|
ERROR,
|
||||||
|
DEBUG,
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user