Skip to content

Commit

Permalink
Merge pull request #21 from nickd3000/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
nickd3000 authored Jun 11, 2023
2 parents 7237c04 + afa772b commit fa01610
Show file tree
Hide file tree
Showing 30 changed files with 592 additions and 180 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.nickd3000</groupId>
<artifactId>garnet</artifactId>
<version>0.2.1</version>
<version>0.2.2-SNAPSHOT</version>
<packaging>jar</packaging>
<name>${project.groupId}:${project.artifactId}</name>
<url>https://github.com/nickd3000/garnet</url>
Expand Down
38 changes: 10 additions & 28 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Feedback is really useful at this stage, so please feel free to create a small p
<dependency>
<groupId>io.github.nickd3000</groupId>
<artifactId>garnet</artifactId>
<version>0.2.1</version>
<version>0.2.2</version>
</dependency>
```

Expand All @@ -38,67 +38,49 @@ See the [garnet-examples](https://github.com/nickd3000/garnetexamples) project f
A [companion toolkit](https://github.com/nickd3000/garnettoolkit) is also available with lots of useful game features,
like an entity-component system, tilemap drawer, collision system and much more.

### Minimal example with sprites
### Minimal example

``` java
package com.physmo.garnetexamples.graphics;
This small example loads and draws a texture to the display.

``` java
import com.physmo.garnet.Garnet;
import com.physmo.garnet.GarnetApp;
import com.physmo.garnet.Texture;
import com.physmo.garnet.drawablebatch.TileSheet;
import com.physmo.garnet.graphics.Graphics;
import com.physmo.garnet.graphics.Texture;

// NOTE: on MacOS we need to add a vm argument: -XstartOnFirstThread
public class SimpleSpriteExample extends GarnetApp {

TileSheet tileSheet;
Texture texture;
double xPos = 0;
double scale = 4;

public SimpleSpriteExample(Garnet garnet, String name) {
super(garnet, name);
}

public static void main(String[] args) {
Garnet garnet = new Garnet(400, 400);
Garnet garnet = new Garnet(600, 400);
GarnetApp app = new SimpleSpriteExample(garnet, "");

garnet.setApp(app);

garnet.init();
garnet.run();
}

@Override
public void init(Garnet garnet) {
texture = Texture.loadTexture("space.png");
tileSheet = new TileSheet(texture, 16, 16);
Graphics graphics = garnet.getGraphics();
graphics.addTexture(texture);
texture = Texture.loadTexture("garnetCrystal.png");
garnet.getGraphics().addTexture(texture);
}

@Override
public void tick(double delta) {
xPos += delta * 50;
if (xPos > 80) xPos = -16;
}

@Override
public void draw(Graphics g) {
g.setScale(scale);

int[] mousePosition = garnet.getInput().getMousePositionScaled(scale);

g.setColor(0xaaff22ff);
g.drawImage(tileSheet, (int) xPos, 5, 2, 2);

g.setColor(0xaa22ffff);
g.drawImage(tileSheet, mousePosition[0], mousePosition[1], 2, 2);
int[] mousePosition = garnet.getInput().getMousePosition();
g.drawImage(texture, mousePosition[0], mousePosition[1]);
}
}


```

2 changes: 1 addition & 1 deletion src/main/java/com/physmo/garnet/DebugDrawer.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.physmo.garnet;

import com.physmo.garnet.graphics.Graphics;
import com.physmo.garnet.regularfont.RegularFont;
import com.physmo.garnet.text.RegularFont;

import java.util.HashMap;
import java.util.Map;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/physmo/garnet/Display.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,8 @@ public int[] getBufferSize() {

return new int[]{w[0], h[0]};
}

public void setWindowTitle(String title) {
glfwSetWindowTitle(windowHandle, title);
}
}
97 changes: 0 additions & 97 deletions src/main/java/com/physmo/garnet/ParagraphDrawer.java

This file was deleted.

30 changes: 30 additions & 0 deletions src/main/java/com/physmo/garnet/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ public static float[] rgbToFloat(int rgb) {
return f;
}

public static void rgbToFloat(int rgb, float[] outFloats) {
outFloats[0] = ((rgb >> 24) & 0xff) / 255f;
outFloats[1] = ((rgb >> 16) & 0xff) / 255f;
outFloats[2] = ((rgb >> 8) & 0xff) / 255f;
outFloats[3] = ((rgb) & 0xff) / 255f;
}

public static double lerp(double v1, double v2, double pos) {
double span = v2 - v1;
return (v1 + span * pos);
Expand All @@ -121,4 +128,27 @@ public static double lerp(double v1, double v2, double pos) {
// return path;
// }

public static float clampUnit(float v) {
if (v < 0) return 0;
if (v > 1) return 1;
return v;
}

public static double clampUnit(double v) {
if (v < 0) return 0;
if (v > 1) return 1;
return v;
}

public static double remapRange(double value, double inMin, double inMax, double outMin, double outMax) {
if (outMax - outMin == 0) return 0;
value = (value - inMin) / ((inMax - inMin) / (outMax - outMin));
return value + outMin;
}

public static float remapRange(float value, float inMin, float inMax, float outMin, float outMax) {
if (outMax - outMin == 0) return 0;
value = (value - inMin) / ((inMax - inMin) / (outMax - outMin));
return value + outMin;
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/physmo/garnet/audio/AudioFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public Clip createClip() {
audioInputStream = AudioSystem.getAudioInputStream(bis);
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);

// NJD
Line.Info lineInfo = clip.getLineInfo();
Control[] controls = clip.getControls();

return clip;
} catch (UnsupportedAudioFileException | IOException e) {
throw new RuntimeException(e);
Expand Down
70 changes: 68 additions & 2 deletions src/main/java/com/physmo/garnet/audio/Sound.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
package com.physmo.garnet.audio;

import javax.sound.sampled.Clip;
import javax.sound.sampled.FloatControl;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class Sound {

private static final float defaultClipVolume = 1.0f;
private static final float defaultClipPan = 0.0f;
Map<Integer, AudioFile> audioFileMap = new HashMap<>();
int audioFileNumberFountain = 1;
float masterVolume = 1.0f;

public float getMasterVolume() {
return masterVolume;
}

public void setMasterVolume(float masterVolume) {
this.masterVolume = masterVolume;
}

public void init() {

Expand All @@ -27,18 +39,72 @@ public int loadSound(String fileName) {
return audioFileNumberFountain - 1;
}


public void playSound(int id) {
//new Thread(() -> playSound2(id, defaultClipVolume, defaultClipPan)).start();
new Thread(() -> {
Clip clip = playSound2(id, defaultClipVolume, defaultClipPan);
// clip.addLineListener(event -> {
// if (event.getType()== LineEvent.Type.STOP) {
// System.out.println("Stopping thread");
// Thread.currentThread().stop();
// }
// });
}
).start();
}

new Thread(() -> playSound2(id)).start();
public void setClipVolume(Clip clip, float volume) {

float v = masterVolume * volume;
if (v < 0) v = 0;
if (v > 1) v = 1;

FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
gainControl.setValue(20f * (float) Math.log10(v));
}

public void playSound2(int id) {
/**
* -1.0 left - +1.0 Right
* 0 = Center
*/
public void setClipPan(Clip clip, float pan) {
if (!clip.isControlSupported(FloatControl.Type.BALANCE)) {
//System.out.println("pan not supported");
return;
}

FloatControl panControl = (FloatControl) clip.getControl(FloatControl.Type.BALANCE);
panControl.setValue(pan);
}

/**
* @param id
* @param volume
* @param pan -1.0 to 1.0 To set position between left and right speaker.
*/
private Clip playSound2(int id, float volume, float pan) {
Clip clip;
AudioFile audioFile = audioFileMap.get(id);
clip = audioFile.getFreeClip();
setClipVolume(clip, volume);
setClipPan(clip, pan);
clip.setFramePosition(0);
clip.start();
return clip;
}

public void playSound(int id, float volume, float pan) {
new Thread(() -> {
Clip clip = playSound2(id, volume, pan);
// clip.addLineListener(event -> {
// if (event.getType()== LineEvent.Type.STOP) {
// System.out.println("Stopping thread");
// Thread.currentThread().interrupt();
// }
// });
}
).start();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public DrawableBatch() {
elements = new ArrayList<>();
}

public List<DrawableElement> getElements() {
return elements;
}

public void add(DrawableElement batchElement) {
elements.add(batchElement);
}
Expand Down
Loading

0 comments on commit fa01610

Please sign in to comment.