Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop'
Browse files Browse the repository at this point in the history
# Conflicts:
#	readme.md
  • Loading branch information
nickd3000 committed May 31, 2023
2 parents 43c3029 + 513bebe commit 703d4d8
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 17 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.1.2</version>
<version>0.2.0</version>
<packaging>jar</packaging>
<name>${project.groupId}:${project.artifactId}</name>
<url>https://github.com/nickd3000/garnet</url>
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Featuring
<dependency>
<groupId>io.github.nickd3000</groupId>
<artifactId>garnet</artifactId>
<version>0.1.2</version>
<version>0.2.0</version>
</dependency>
```

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/physmo/garnet/DebugDrawer.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public class DebugDrawer {
private final int lineHeight = 10;
private RegularFont regularFont;
private double fps;
private boolean visible = true;
private boolean drawFps = true;
private boolean visible = false;
private boolean drawFps = false;
private double scale = 1;

public double getScale() {
Expand All @@ -39,7 +39,7 @@ public void setDrawFps(boolean drawFps) {
}

public void init() {
visible = false;
//visible = false;
regularFont = new RegularFont(DEBUG_FONT_NAME, 10, 10);
regularFont.setHorizontalPad(-1);
}
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/physmo/garnet/DrawableFont.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.physmo.garnet;

import com.physmo.garnet.graphics.Graphics;

/**
* Interface that both our font classes support
* in order to be used by the paragraph drawer
*/
public interface DrawableFont {
void drawText(Graphics graphics, String text, int x, int y);

int getLineHeight();

int getStringWidth(String text);

int getSpaceWidth();
}
61 changes: 61 additions & 0 deletions src/main/java/com/physmo/garnet/ParagraphDrawer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.physmo.garnet;


import com.physmo.garnet.graphics.Graphics;

import java.util.ArrayList;
import java.util.List;

public class ParagraphDrawer {
DrawableFont font;
int padY = 0;

public ParagraphDrawer(DrawableFont font) {
this.font = font;
}


public void setPadY(int val) {
padY = val;
}

// TODO: cache analyzed strings.
public void drawParagraph(Graphics g, String text, int width, int height, int x, int y) {
List<WordInfo> wordInfos = analyzeText(font, text);

int rx = 0; // rolling x
int ry = 0; // rolling y

for (WordInfo wordInfo : wordInfos) {
if (rx + wordInfo.width > width) {
rx = 0;
ry += font.getLineHeight() + padY;
}

font.drawText(g, wordInfo.text, x + rx, y + ry);
rx += wordInfo.width;
rx += font.getSpaceWidth();
}

}


public List<WordInfo> analyzeText(DrawableFont font, String text) {
List<WordInfo> words = new ArrayList<>();
String[] s = text.split(" ");
for (int i = 0; i < s.length; i++) {
words.add(new WordInfo(s[i], font.getStringWidth(s[i])));
}
return words;
}

class WordInfo {
String text;
int width;

public WordInfo(String _text, int _width) {
text = _text;
width = _width;
}
}
}
3 changes: 2 additions & 1 deletion src/main/java/com/physmo/garnet/Texture.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ public static Texture loadTexture(InputStream inputStream) {
if (!stbi_info_from_memory(byteBuffer, w, h, comp)) {
throw new RuntimeException("Failed to read image information: " + stbi_failure_reason());
} else {
System.out.println("OK with reason: " + stbi_failure_reason());
// Commented this out because it was reporting "Corrupt JPEG" when loading a png file
//System.out.println("OK with reason: " + stbi_failure_reason());
}

// Load image.
Expand Down
17 changes: 15 additions & 2 deletions src/main/java/com/physmo/garnet/bitmapfont/BitmapFont.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.physmo.garnet.bitmapfont;

import com.physmo.garnet.DrawableFont;
import com.physmo.garnet.Texture;
import com.physmo.garnet.Utils;
import com.physmo.garnet.graphics.Graphics;
Expand All @@ -12,7 +13,7 @@
import java.util.HashMap;
import java.util.Map;

public class BitmapFont {
public class BitmapFont implements DrawableFont {

private static final int coordsPerChar = 16;
Map<Integer, GlyphGeometry> geometry;
Expand Down Expand Up @@ -57,7 +58,8 @@ private void parseDefinitionData(String fileData) {
});
}

public void drawString(Graphics graphics, String text, int x, int y) {
@Override
public void drawText(Graphics graphics, String text, int x, int y) {
graphics.addTexture(texture);

float[] floats = generateCoordsForStrings(text, x, y);
Expand All @@ -83,6 +85,12 @@ public void drawString(Graphics graphics, String text, int x, int y) {

}

@Override
public int getLineHeight() {
GlyphGeometry glyphGeometry = geometry.get((int) 'T');
return glyphGeometry.height;
}

private float[] generateCoordsForStrings(String text, int x, int y) {
int xOffset = 0;
float[] coords = new float[text.length() * coordsPerChar];
Expand Down Expand Up @@ -146,4 +154,9 @@ public int getStringWidth(String text) {
return stringWidth;
}

@Override
public int getSpaceWidth() {
return getStringWidth(" ");
}

}
40 changes: 31 additions & 9 deletions src/main/java/com/physmo/garnet/regularfont/RegularFont.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.physmo.garnet.regularfont;

import com.physmo.garnet.DrawableFont;
import com.physmo.garnet.Texture;
import com.physmo.garnet.Utils;
import com.physmo.garnet.drawablebatch.TileSheet;
Expand All @@ -11,7 +12,7 @@
* RegularFont is a simpler font drawer. It requires a font image file arranged in
* a specific grid pattern to match the ascii character system.
*/
public class RegularFont {
public class RegularFont implements DrawableFont {

private final TileSheet tileSheet;
private final Texture texture;
Expand All @@ -38,26 +39,47 @@ public void setHorizontalPad(int horizontalPad) {
this.horizontalPad = horizontalPad;
}

@Override
public void drawText(Graphics graphics, String text, int x, int y) {
if (!graphics.hasTexture(texture.getId()))
graphics.addTexture(texture);
TextObject textObject = new TextObject(text, x, y);
renderTextObject(graphics, textObject);

renderText(graphics, text, x, y);
}

@Override
public int getLineHeight() {
return charHeight;
}

private void renderTextObject(Graphics graphics, TextObject textObject) {
String str = textObject.text;
if (str == null) return;
@Override
public int getStringWidth(String text) {
if (text == null) return 0;

int textLength = text.length();
return textLength * (charWidth + horizontalPad);

}

int textLength = str.length();
int xPos = textObject.x, yPos = textObject.y;
@Override
public int getSpaceWidth() {
return charWidth;
}

private void renderText(Graphics graphics, String text, int x, int y) {

if (text == null) return;

int textLength = text.length();
int xPos = x, yPos = y;
for (int i = 0; i < textLength; i++) {
char c = str.charAt(i);
char c = text.charAt(i);
renderChar(graphics, c, xPos, yPos);
xPos += charWidth + horizontalPad;
}
}


public void renderChar(Graphics graphics, char c, int x, int y) {
int cy = ((int) c) / 16;
int cx = ((int) c) % 16;
Expand Down

0 comments on commit 703d4d8

Please sign in to comment.