ASCII Table

Dec

Hex

Oct Char

Dec

Hex

Oct

Char

Dec

Hex

Oct

Char

Dec

Hex

Oct

Char

0

a

0

32

20

40

(spacel

64

40

100

■S

96

6D

140

l

l

1

33

21

41

!

65

41

101

A

97

61

141

a

3

2

3

3-«

33

42

66

43

102

a

«1

63

143

b

3

3

3

35

23

43

*

67

43

103

c

99

63

143

c

4

4

4

36

24

44

»

68

44

104

D

100

64

144

d

5

5

5

37

35

45

*

45

105

E

101

65

145

*

<t

6

38

26

46

&

70

46

106

F

102

66

146

t

7

7

7

39

27

47

71

47

107

G

103

67

147

a

8

B

to

40

28

50

(

72

48

110

H

104

6B

150

h

9

9

11

41

29

51

1

73

49

111

1

105

69

151

1

10

A

12

42

2A

52

*

74

4A

112

1

106

6A

152

i

n

B

13

43

IB

53

+

75

4R

113

K

107

6B

153

k

12

C

14

44

2C

54

76

4C

114

L

108

6C

154

1

13

D

IS

45

2D

55

77

4D

115

H

10»

6D

155

m

14

E

16

46

3f

56

78

4E

116

M

110

6E

15«

n

15

F

17

47

2F

57

1

79

4F

117

O

111

6F

157

o

16

10

20

44

30

60

0

80

50

120

p

112

70

160

P

17

11

21

49

31

61

1

81

51

121

<3

113

71

161

IB

12

22

SO

32

62

2

82

52

122

R

114

72

162

r

19

13

23

51

33

63

3

83

53

123

S

115

73

163

s

30

14

34

53

34

64

4

84

54

134

T

116

74

1&4

t

21

15

25

S3

35

65

5

85

55

125

U

117

75

165

u

22

16

26

54

¿6

66

6

86

56

126

V

IIB

76

166

V

23

17

27

55

37

67

7

87

57

137

w

119

77

167

w

24

IB

30

56

38

70

8

88

5S

130

x

120

7B

170

K

21

31

57

39

71

9

89

59

131

Y

121

79

171

y

26

1A

32

58

3A

72

90

5A

132

Z

122

7A

172

i

27

IB

33

59

ifl

73

91

SB

133

I

123

7B

173

i

21

1C

34

60

X

74

«

92

5C

134

\

124

7C

174

1

29

ID

35

61

3D

75

93

5t>

135

1

125

7D

175

>

30

IE

36

62

3E

76

>

94

5E

136

126

7E

176

-

31

IF

37

63

Jf

77

?

95

IF

137

127

7F

177

Figure 9-8. ASCII characters and their decimal, hexadecimal, and octal values

Figure 9-8. ASCII characters and their decimal, hexadecimal, and octal values

Out of those 128 characters, 96 are printable (characters 32 to 127). Our bitmap font only contains printable characters. The first row in the bitmap font contains the characters 32 to 47, the next row contains the characters 48 to 63, and so on. ASCII is only useful if you want to store and display text that uses the standard Latin alphabet. There's an extended ASCII format that uses the values 128 to 255 to encode other common characters of Western languages, such as o or e. More expressive character sets (e.g., for Chinese or Arabic) are represented via Unicode, and can't be encoded via ASCII. For our game, the standard ASCII character set suffices, though.

So how do we render text with a bitmap font? That turns out to be really easy. First we create 96 texture regions, each mapping to a glyph in the bitmap font. We can store those texture regions in an array like this:

TextureRegion[] glyphs = new TextureRegion[96];

Java strings are encoded in 16-bit Unicode. Luckily for us, the ASCII characters we have in our bitmap font have the same values in ASCII and Unicode. To fetch the region for a character in a Java string, we just need to do this:

This gives us a direct index into the texture region array. We just subtract the value for the space character (32) from the current character in the string. If the index is smaller than zero or bigger than 95, we have a Unicode character that is not in our bitmap font. Usually we just ignore such a character.

To render multiple characters in a line, we need to know how much space there should be between characters. The bitmap font in Figure 9-7 is a so-called fixed-width font. That means that each glyph has the same width. Our bitmap font glyphs have a size of 16x20 pixels each. When we advance our rendering position from character to character in a string, we just need to add 20 pixels. The number of pixels we move the drawing position from character to character is called advance. For our bitmap font it is fixed, but in general it is variable depending on the character we draw. A more complex form of advance takes both the current character we are about to draw and the next character into consideration for calculating the advance. This technique is called kerning, if you want to look it up on the Web. We'll only use fixed-width bitmap fonts, as they make our lives considerably easier.

So, how did I generate that ASCII bitmap font? I used one of the many tools available on the Web for generating bitmap fonts. The one I used is called Codehead's Bitmap Font Generator and is freely available. You can select a font file on your hard drive and specify the height of the font, and the generator will produce an image from it for the ASCII character set. The tool has a lot more options I can't discuss here. I recommend checking it out yourself and playing around with it a little.

We'll draw all the remaining strings in our game with this technique. Later you'll see a concrete implementation of a bitmap font class. Let's get on with our assets.

With the bitmap font we now have assets for all our graphical UI elements. We will render them via a SpriteBatcher using a camera that sets up a view frustum that directly maps to our target resolution. This way we can specify all the coordinates in pixel coordinates.

0 0

Post a comment