added sample functions to BaseGame

This commit is contained in:
DaniTheSkunk 2022-10-15 05:52:41 +00:00
parent f52d6e66e3
commit e398f7a095
4 changed files with 36 additions and 3 deletions

View File

@ -1,18 +1,31 @@
package com.danitheskunk.skunkworks;
import com.danitheskunk.skunkworks.audio.AudioEngine;
import com.danitheskunk.skunkworks.audio.ISample;
import com.danitheskunk.skunkworks.audio.nodes.Mixer;
import com.danitheskunk.skunkworks.audio.nodes.Node;
import com.danitheskunk.skunkworks.audio.nodes.SamplePlayer;
import com.danitheskunk.skunkworks.gfx.IRenderContext;
import com.danitheskunk.skunkworks.gfx.font.IFont;
import com.danitheskunk.skunkworks.nodes.Node;
import com.danitheskunk.skunkworks.nodes.NodeRoot;
import org.lwjgl.glfw.GLFW;
public abstract class BaseGame {
protected Engine engine;
protected AudioEngine audioEngine;
protected Mixer mixer;
protected SamplePlayer samplePlayer;
protected IWindow window;
protected IFont debugFont;
protected NodeRoot rootNode;
public BaseGame(Vec2i windowSize, String windowTitle) {
this.audioEngine = new AudioEngine(44100, 256, 8);
this.mixer = new Mixer(this.audioEngine, 2);
this.samplePlayer = new SamplePlayer(this.audioEngine);
Node.connect(samplePlayer, 0, mixer, 0);
this.audioEngine.setNode(this.mixer);
this.engine = new Engine();
this.window = engine.openWindow(windowSize, windowTitle);
//todo: load from .jar
@ -38,6 +51,7 @@ public abstract class BaseGame {
rootNode.render(rc);
render(rc);
window.renderFinish(rc);
audioEngine.refill();
}
}
@ -58,4 +72,16 @@ public abstract class BaseGame {
protected void update(double delta) {
}
protected ISample loadSample(String path) {
return audioEngine.loadSample(path);
}
protected void playSample(ISample sample) {
samplePlayer.play(sample);
}
protected void playSample(ISample sample, boolean looping) {
samplePlayer.play(sample, looping);
}
}

View File

@ -1,12 +1,18 @@
package com.danitheskunk.skunkworks;
import com.danitheskunk.skunkworks.audio.ISample;
import com.danitheskunk.skunkworks.nodes.NodeSprite;
public class TestNode extends BaseGame {
NodeSprite sprite;
ISample kick;
public TestNode() {
super(new Vec2i(1280, 720), "Skunkworks");
kick = loadSample("demoassets/kick.wav");
playSample(kick);
sprite = new NodeSprite();
sprite.setTexture(window.loadTexture("demoassets/test.png"));
sprite.setPos(new Vec2f(100, 100));
@ -14,6 +20,7 @@ public class TestNode extends BaseGame {
sprite.tweenPos(new Vec2f(800, 400), 120).delay(60).then(() -> {
System.out.println("yay! got there! now lets go home");
sprite.tweenPos(new Vec2f(100, 100), 120);
playSample(kick);
});
}

View File

@ -27,7 +27,7 @@ public class TestSound {
var sin3 = new Sine(engine, 523.25);
engine.setNode(mix);
//Node.connect(sin1, 0, mix, 0);
//Node.connect(sin3, 0, mix, 1);
Node.connect(sin3, 0, mix, 1);
//Node.connect(sin2, 0, mix, 2);
var loop = engine.loadSample("C:\\Users\\dani\\Downloads\\AKWF" +
@ -41,7 +41,7 @@ public class TestSound {
player.play(loop, true);
for(int j = 0; j < 10; ++j) {
//player.play(kick);
player.play(kick);
for(int i = 0; i < 50; ++i) {
engine.refill();
Thread.sleep(20);

BIN
demoassets/kick.wav Normal file

Binary file not shown.