Skip to content

Commit

Permalink
fix: OSX, No Sound and No Debug Circle (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
Seba244c authored Feb 22, 2024
1 parent d03fd83 commit c026c1b
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 30 deletions.
Binary file added demos/tappy/assets/death.ogg
Binary file not shown.
Binary file added demos/tappy/assets/jump.ogg
Binary file not shown.
10 changes: 8 additions & 2 deletions demos/tappy/src/main/java/dk/sebsa/Tappy.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@
import dk.sebsa.layers.MainMenuScreen;
import dk.sebsa.spellbook.asset.Identifier;
import dk.sebsa.spellbook.asset.loading.FolderAssetProvider;
import dk.sebsa.spellbook.audio.SoundListener;
import dk.sebsa.spellbook.audio.SoundPlayer;
import dk.sebsa.spellbook.core.Application;
import dk.sebsa.spellbook.core.events.*;
import dk.sebsa.spellbook.ecs.Camera;
import dk.sebsa.spellbook.ecs.ECS;
import dk.sebsa.spellbook.ecs.Entity;
import dk.sebsa.spellbook.math.Color;
import dk.sebsa.spellbook.math.Time;
import dk.sebsa.spellbook.graphics.opengl.components.SpriteRenderer;
import dk.sebsa.spellbook.graphics.opengl.stages.SpriteStage;
import dk.sebsa.spellbook.math.Color;
import dk.sebsa.spellbook.math.Time;
import dk.sebsa.spellbook.phys.components.BoxCollider2D;

import java.io.File;
Expand Down Expand Up @@ -46,6 +48,8 @@ public void engineCreateFirstScene(EngineCreateFirstSceneEvent e) {
this.tappy = new Entity(ECS.ROOT);
SpriteRenderer sp = new SpriteRenderer(new Identifier("tappy", "tappy.spr"));
tappy.addComponent(sp);
tappy.addComponent(new SoundListener());
tappy.addComponent(new SoundPlayer(new Identifier("tappy", "jump.ogg")));
sp.scale = 3f;
tappy.tag = "player";

Expand All @@ -55,6 +59,7 @@ public void engineCreateFirstScene(EngineCreateFirstSceneEvent e) {
var collider = new ObstacleComponent();
collider.size.set(960, 48);
floor.addComponent(collider);
floor.addComponent(new SoundPlayer(new Identifier("tappy", "death.ogg")));

// Add celling
var celling = new Entity(ECS.ROOT);
Expand Down Expand Up @@ -101,6 +106,7 @@ public String version() {

public void start() {
tappy.addComponent(new TappyController(tappy.getComponent(SpriteRenderer.class)));
tappy.getComponent(SoundPlayer.class).start();
tappy.transform.setPosition(0, -20, 0);
mainMenuScreen.enabled = false;
deathScreen.enabled = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dk.sebsa.components;

import dk.sebsa.Tappy;
import dk.sebsa.spellbook.audio.SoundPlayer;
import dk.sebsa.spellbook.phys.components.BoxCollider2D;
import dk.sebsa.spellbook.phys.components.Collider2D;

Expand All @@ -9,6 +10,7 @@ public class ObstacleComponent extends BoxCollider2D {
public void onCollide(Collider2D other) {
if (other.getEntity().tag.equals("player")) {
Tappy.instance.death();
getEntity().getComponent(SoundPlayer.class).start();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import dk.sebsa.spellbook.asset.Identifier;
import dk.sebsa.spellbook.ecs.Component;
import dk.sebsa.spellbook.ecs.Entity;
import dk.sebsa.spellbook.math.Time;
import dk.sebsa.spellbook.graphics.opengl.components.SpriteRenderer;
import dk.sebsa.spellbook.math.Time;
import dk.sebsa.spellbook.util.Random;

public class PipesManager extends Component {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package dk.sebsa.components;

import dk.sebsa.spellbook.FrameData;
import dk.sebsa.spellbook.audio.SoundPlayer;
import dk.sebsa.spellbook.graphics.opengl.components.SpriteRenderer;
import dk.sebsa.spellbook.math.Time;
import dk.sebsa.spellbook.math.Vector3f;
import dk.sebsa.spellbook.graphics.opengl.components.SpriteRenderer;
import dk.sebsa.spellbook.phys.components.Collider2D;
import dk.sebsa.spellbook.phys.components.SpriteCollider2D;
import org.lwjgl.glfw.GLFW;
Expand All @@ -26,8 +27,10 @@ public void onCollide(Collider2D other) {

@Override
public void update(FrameData frameData) {
if (frameData.input.isKeyPressed(GLFW.GLFW_KEY_SPACE) || frameData.input.gamePads[0].buttonX)
if (frameData.input.isKeyPressed(GLFW.GLFW_KEY_SPACE) || frameData.input.gamePads[0].buttonX) {
getEntity().getComponent(SoundPlayer.class).start();
velocity = JUMP_ENERGY;
}
else if (velocity <= 0)
velocity = Math.max(velocity - ((VELOCITY_DROPFF * (-velocity + 0.1f)) * Time.getDeltaTime())
, -1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* @since 1.0.0
*/
public class DebugRenderStage extends RenderStage {
private static final float CIRCLE_DETAIL = 32;
private static final int CIRCLE_DETAIL = 32;
private final float[] testPos = {
0.0f, 0.0f,
10, 0.0f,
Expand All @@ -38,6 +38,7 @@ public class DebugRenderStage extends RenderStage {
};
private final DebugVAO linesVAO = new DebugVAO(testPos, 2);
private final DebugVAO pointsVAO = new DebugVAO(testPos, 2);
private final DebugVAO circleVao = new DebugVAO(testPos, 2);
private final GLSLShaderProgram shader;

/**
Expand Down Expand Up @@ -80,34 +81,25 @@ protected void draw(Rect r, FrameData frameData) {
shader.unbind();
}

private void drawCircle(Vector2f point, float radius) {
glBegin(GL_LINE_LOOP);
for (int i = 0; i < CIRCLE_DETAIL; i++) {
float z = i / CIRCLE_DETAIL * 360;
float x = radius * (float) Math.cos(Math.toRadians(z));
float y = radius * (float) Math.sin(Math.toRadians(z));

glVertex2f(x + point.x, y + point.y);
}
glEnd();
}

private void drawColliders(FrameData frameData) {
HashSet<Rect> rects = new HashSet<>();
HashSet<Vector2f> points = new HashSet<>();
HashSet<Circle> circles = new HashSet<>();
shader.setUniform("mode", DebugRenderMode.ModeWorldCamera.value);
shader.setUniform("color", Color.yellow);

for (Collider2D collider2D : frameData.newton2DSolids) {
if (collider2D instanceof BoxCollider2D) rects.add(((BoxCollider2D) collider2D).getWorldPositionRect());
else if (collider2D instanceof CircleCollider2D) {
drawCircle(collider2D.getCenter(), ((CircleCollider2D) collider2D).radius);
}
if (collider2D instanceof BoxCollider2D)
rects.add(((BoxCollider2D) collider2D).getWorldPositionRect());
if (collider2D instanceof CircleCollider2D)
circles.add(new Circle(collider2D.getCenter(), ((CircleCollider2D) collider2D).radius));


points.add(collider2D.getCenter());
}

drawRectList(rects);
drawCircleList(circles);
shader.setUniform("color", Color.red);
drawPointList(points);
}
Expand Down Expand Up @@ -163,6 +155,22 @@ private void drawPointList(Set<Vector2f> points) {
pointsVAO.draw(GL_POINTS);
}

private void drawCircleList(Set<Circle> circles) {
float[] vertices = new float[CIRCLE_DETAIL * 2];
for(Circle c : circles) {
for (int i = 0; i < CIRCLE_DETAIL; i++) {
float z = (float) i / CIRCLE_DETAIL * 360;
float x = c.radius * (float) Math.cos(Math.toRadians(z));
float y = c.radius * (float) Math.sin(Math.toRadians(z));

vertices[i * 2] = c.pos.x + x;
vertices[i * 2 + 1] = c.pos.y + y;
}
circleVao.put(vertices);
circleVao.draw(GL_LINE_LOOP);
}
}

@Override
protected void destroy() {
shader.unreference();
Expand All @@ -182,4 +190,6 @@ enum DebugRenderMode {
}

}

private record Circle(Vector2f pos, float radius) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,20 +188,18 @@ public static void drawText(String text, Color c, Font font, Rect drawRect) {
FloatBuffer y = stack.floats(r.y); // Current y pos

Check warning on line 188 in spellbook/src/main/java/dk/sebsa/spellbook/graphics/opengl/GL2D.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Suspicious variable/parameter name combination

'y' should probably not be passed as parameter 'x'

STBTTAlignedQuad q = STBTTAlignedQuad.malloc(stack);
float lineY = drawRect.y;

for (int i = 0, to = text.length(); i < to; ) { // For all charaters
i += Font.getCP(text, to, i, pCodePoint);
int cp = pCodePoint.get(0); // The charater codepoint

// newline and other weird charaters
if (cp == '\n') {
y.put(0, lineY = y.get(0) + lineHeight);
y.put(0, y.get(0) + lineHeight);
x.put(0, 0.0f);
} else if (cp < 32) continue;

// Render the charater
float cpX = x.get(0); // Current x pos (before the current char is rendered)
stbtt_GetBakedQuad(font.getCdata(), Font.BITMAP_W, Font.BITMAP_H, cp - 32, x, y, q, true);

if (font.isKerningEnabled() && i < to) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL13.*;
import static org.lwjgl.opengl.GL14.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.*;
import static org.lwjgl.opengl.GL32.glDrawElementsBaseVertex;

Expand Down

0 comments on commit c026c1b

Please sign in to comment.