Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/SpriteSheetFont.java
1456 views
1
package org.newdawn.slick;
2
3
import java.io.UnsupportedEncodingException;
4
5
import org.newdawn.slick.util.Log;
6
7
/**
8
* A font implementation that will use the graphics inside a SpriteSheet for its data.
9
* This is useful when your font has a fixed width and height for each character as
10
* opposed to the more complex AngelCodeFont that allows different sizes and kerning
11
* for each character.
12
*
13
* @author Onno Scheffers
14
*/
15
public class SpriteSheetFont implements Font {
16
/** The SpriteSheet containing the bitmap font */
17
private SpriteSheet font;
18
/** First character in the SpriteSheet */
19
private char startingCharacter;
20
/** Width of each character in pixels */
21
private int charWidth;
22
/** Height of each character in pixels */
23
private int charHeight;
24
/** Number of characters in SpriteSheet horizontally */
25
private int horizontalCount;
26
/** Total number of characters in SpriteSheet */
27
private int numChars;
28
29
/**
30
* Create a new font based on a SpriteSheet. The SpriteSheet should hold your
31
* fixed-width character set in ASCII order. To only get upper-case characters
32
* working you would usually set up a SpriteSheet with characters for these values:
33
* <pre>
34
* !"#$%&'()*+,-./
35
* 0123456789:;<=>?
36
* &#0064;ABCDEFGHIJKLMNO
37
* PQRSTUVWXYZ[\]^_<pre>
38
* In this set, ' ' (SPACE) would be the startingCharacter of your characterSet.
39
*
40
* @param font The SpriteSheet holding the font data.
41
* @param startingCharacter The first character that is defined in the SpriteSheet.
42
*/
43
public SpriteSheetFont(SpriteSheet font, char startingCharacter) {
44
this.font = font;
45
this.startingCharacter = startingCharacter;
46
horizontalCount = font.getHorizontalCount();
47
int verticalCount = font.getVerticalCount();
48
charWidth = font.getWidth() / horizontalCount;
49
charHeight = font.getHeight() / verticalCount;
50
numChars = horizontalCount * verticalCount;
51
}
52
53
/**
54
* @see org.newdawn.slick.Font#drawString(float, float, java.lang.String)
55
*/
56
public void drawString(float x, float y, String text) {
57
drawString(x, y, text, Color.white);
58
}
59
60
/**
61
* @see org.newdawn.slick.Font#drawString(float, float, java.lang.String, org.newdawn.slick.Color)
62
*/
63
public void drawString(float x, float y, String text, Color col) {
64
drawString(x,y,text,col,0,text.length()-1);
65
}
66
67
/**
68
* @see Font#drawString(float, float, String, Color, int, int)
69
*/
70
public void drawString(float x, float y, String text, Color col, int startIndex, int endIndex) {
71
try {
72
byte[] data = text.getBytes("US-ASCII");
73
for (int i = 0; i < data.length; i++) {
74
int index = data[i] - startingCharacter;
75
if (index < numChars) {
76
int xPos = (index % horizontalCount);
77
int yPos = (index / horizontalCount);
78
79
if ((i >= startIndex) || (i <= endIndex)) {
80
font.getSprite(xPos, yPos)
81
.draw(x + (i * charWidth), y, col);
82
}
83
}
84
}
85
} catch (UnsupportedEncodingException e) {
86
// Should never happen, ASCII is supported pretty much anywhere
87
Log.error(e);
88
}
89
}
90
91
/**
92
* @see org.newdawn.slick.Font#getHeight(java.lang.String)
93
*/
94
public int getHeight(String text) {
95
return charHeight;
96
}
97
98
/**
99
* @see org.newdawn.slick.Font#getWidth(java.lang.String)
100
*/
101
public int getWidth(String text) {
102
return charWidth * text.length();
103
}
104
105
/**
106
* @see org.newdawn.slick.Font#getLineHeight()
107
*/
108
public int getLineHeight() {
109
return charHeight;
110
}
111
}
112
113