com.github.mathiewz.slick.SpriteSheetFont Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of modernized-slick Show documentation
Show all versions of modernized-slick Show documentation
The main purpose of this libraryis to modernize and maintain the slick2D library.
The newest version!
package com.github.mathiewz.slick;
import java.io.UnsupportedEncodingException;
import com.github.mathiewz.slick.util.Log;
/**
* A font implementation that will use the graphics inside a SpriteSheet for its data.
* This is useful when your font has a fixed width and height for each character as
* opposed to the more complex AngelCodeFont that allows different sizes and kerning
* for each character.
*
* @author Onno Scheffers
*/
public class SpriteSheetFont implements Font {
/** The SpriteSheet containing the bitmap font */
private final SpriteSheet font;
/** First character in the SpriteSheet */
private final char startingCharacter;
/** Width of each character in pixels */
private final int charWidth;
/** Height of each character in pixels */
private final int charHeight;
/** Number of characters in SpriteSheet horizontally */
private final int horizontalCount;
/** Total number of characters in SpriteSheet */
private final int numChars;
/**
* Create a new font based on a SpriteSheet. The SpriteSheet should hold your
* fixed-width character set in ASCII order. To only get upper-case characters
* working you would usually set up a SpriteSheet with characters for these values:
*
*
*
* !"#$%&'()*+,-./
* 0123456789:;<=>?
* @ABCDEFGHIJKLMNO
* PQRSTUVWXYZ[\]^_
*
*
*
* In this set, ' ' (SPACE) would be the startingCharacter of your characterSet.
*
* @param font
* The SpriteSheet holding the font data.
* @param startingCharacter
* The first character that is defined in the SpriteSheet.
*/
public SpriteSheetFont(SpriteSheet font, char startingCharacter) {
this.font = font;
this.startingCharacter = startingCharacter;
horizontalCount = font.getHorizontalCount();
int verticalCount = font.getVerticalCount();
charWidth = font.getWidth() / horizontalCount;
charHeight = font.getHeight() / verticalCount;
numChars = horizontalCount * verticalCount;
}
/**
* @see com.github.mathiewz.slick.Font#drawString(float, float, java.lang.String)
*/
@Override
public void drawString(float x, float y, String text) {
drawString(x, y, text, Color.white);
}
/**
* @see com.github.mathiewz.slick.Font#drawString(float, float, java.lang.String, com.github.mathiewz.slick.Color)
*/
@Override
public void drawString(float x, float y, String text, Color col) {
drawString(x, y, text, col, 0, text.length() - 1);
}
/**
* @see Font#drawString(float, float, String, Color, int, int)
*/
@Override
public void drawString(float x, float y, String text, Color col, int startIndex, int endIndex) {
try {
byte[] data = text.getBytes("US-ASCII");
for (int i = 0; i < data.length; i++) {
int index = data[i] - startingCharacter;
if (index < numChars) {
int xPos = index % horizontalCount;
int yPos = index / horizontalCount;
if (i >= startIndex || i <= endIndex) {
font.getSprite(xPos, yPos).draw(x + i * charWidth, y, col);
}
}
}
} catch (UnsupportedEncodingException e) {
// Should never happen, ASCII is supported pretty much anywhere
Log.error(e);
}
}
/**
* @see com.github.mathiewz.slick.Font#getHeight(java.lang.String)
*/
@Override
public int getHeight(String text) {
return charHeight;
}
/**
* @see com.github.mathiewz.slick.Font#getWidth(java.lang.String)
*/
@Override
public int getWidth(String text) {
return charWidth * text.length();
}
/**
* In this case equal to {@link com.github.mathiewz.slick.Font#getLogicalWidth(String)}
*
* @see com.github.mathiewz.slick.Font#getLogicalWidth(String)
*/
@Override
public int getLogicalWidth(String str) {
return getWidth(str);
}
/**
* @see com.github.mathiewz.slick.Font#getLineHeight()
*/
@Override
public int getLineHeight() {
return charHeight;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy