Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Player (WIP) #49

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/org/dhsdev/flowerknight/FlowerKnight.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.dhsdev.flowerknight;

import org.dhsdev.flowerknight.game.Player;
import org.dhsdev.flowerknight.gl.Renderable;
import org.dhsdev.flowerknight.gl.Texture;
import org.dhsdev.flowerknight.gl.comp.Label;
Expand Down Expand Up @@ -56,13 +57,13 @@ public static void init() {
Camera.init();

try {
Texture.LOGO = new Texture("res/logo.png");
Texture.LOGO.bind();
Texture.LOGO = new Texture("res/logo.png");
Texture.ARDEN = new Texture("res/arden.png");
} catch (IOException e) {
Logger.log("Could not load logo", Severity.ERROR);
Logger.log("Could not load textures", Severity.ERROR);
}

Renderable.renderables.add(new Label("Test", "Test Text", 0.4f,0.4f, 0f, 0f));
Renderable.renderables.add(new Player(0, 0));

Shader.getSpotlightShader().registerUniform("time");

Expand Down Expand Up @@ -116,6 +117,8 @@ public static void exit() {

Texture.LOGO.unbind();
Texture.LOGO.delete();
Texture.ARDEN.unbind();
Texture.ARDEN.delete();

glfwTerminate();
}
Expand Down
5 changes: 4 additions & 1 deletion src/org/dhsdev/flowerknight/game/GameObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class GameObject {
* The strength of gravity. Until we get an actual game object, we have no
* clue how fast this will make things fall, so keep it zero for now.
*/
public static final float GRAVITY_STRENGTH = 0.0f;
public static final float GRAVITY_STRENGTH = 0.0001f;

/**
* The x-position.
Expand Down Expand Up @@ -75,6 +75,9 @@ protected void update() {
if (hasGravity)
yVelocity -= GameObject.GRAVITY_STRENGTH;

y += yVelocity;
x += xVelocity;

// TODO - collision, etc.

}
Expand Down
43 changes: 43 additions & 0 deletions src/org/dhsdev/flowerknight/game/Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.dhsdev.flowerknight.game;

import org.dhsdev.flowerknight.gl.Mesh;
import org.dhsdev.flowerknight.gl.Renderable;
import org.dhsdev.flowerknight.gl.Texture;

/**
* A player that exists.
*/
public class Player extends GameObject implements Renderable {

private Mesh mesh;

/**
* Adds the object to the list.
* @param xP the x location
* @param yP the y location
*/
public Player(float xP, float yP) {
super(xP, yP);
}

@Override
public void draw() {
mesh = new Mesh(
new float[] {
(float) (x - 0.1), (float) (y - 0.1),
(float) (x - 0.1), (float) (y + 0.1),
(float) (x + 0.1), (float) (y + 0.1),
(float) (x + 0.1), (float) (y - 0.1),
},
new int[] {
0, 1, 3, 3, 1, 2
}
);
mesh.render(Texture.ARDEN);
}

@Override
public void clear() {
// Nothing for now.
}
}
17 changes: 3 additions & 14 deletions src/org/dhsdev/flowerknight/gl/Mesh.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,25 +98,14 @@ private int getVboId(int[] arr) {
* code.
* @param texture the OpenGL textureID to use.
*/
public void render(TextureEnum texture) {
public void render(Texture texture) {

int pos = texture.ordinal();
int texMax = TextureEnum.values().length;

float stride = 1 / (float) texMax;

// We select a square from the large texture atlas image.
var texCoords = new float[] {
pos * stride, 1,
pos * stride, 0,
(pos + 1) * stride, 0,
(pos + 1) * stride, 1,
};
texture.bind();

glBindVertexArray(vaoId);

// Texture coordinates
int texVboId = getVboId(texCoords);
int texVboId = getVboId(new float[] { 0, 1, 0, 0, 1, 0, 1, 1 });
glVertexAttribPointer(1, GAME_DIMENSION, GL_FLOAT, false, 0, 0);

// Draw the mesh
Expand Down
2 changes: 1 addition & 1 deletion src/org/dhsdev/flowerknight/gl/Texture.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void delete() {
/**
* The textures
*/
public static Texture LOGO;
public static Texture LOGO, ARDEN;

/**
* Set this to be used.
Expand Down
11 changes: 0 additions & 11 deletions src/org/dhsdev/flowerknight/gl/TextureEnum.java

This file was deleted.

4 changes: 2 additions & 2 deletions src/org/dhsdev/flowerknight/gl/comp/Label.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class Label extends Component {
private float offsetX;
private float offsetY;
private Mesh mesh;
private TextureEnum tex;
private Texture texture;
private Shader shader;

/**
Expand Down Expand Up @@ -57,7 +57,7 @@ public Label(String name, String text, float x, float y, float offsetX, float of
@Override
public void draw() {
shader.bind();
mesh.render(TextureEnum.LOGO);
mesh.render(texture);
}

@Override
Expand Down