Hi again
So, currently I’m making the font system and trying to render some text on the screen and I’m almost done, except that I can’t get the right character to render I tried Googling and even found some videos, but nothing works
The problem is that I can’t find the right math formula to use, to get the letter I want
Here’s my code:
Font class:
package com.mayogames.zombiecubes.gfx;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import com.mayogames.zombiecubes.Game;
public class Font {
private SpriteSheet spriteSheet;
public Font(SpriteSheet spriteSheet){
this.spriteSheet = spriteSheet;
}
public static final int SIZE = 8 * Game.SCALE;
private static String chars =""+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ "+
"QRSTUVWXYZ "+
"0123456789.,:;'\"!?$%()-=+/"+
"?$%()-=+/ ";
public void render(Graphics g, String message, int x, int y){
message = message.toUpperCase();
for(int i = 0; i < message.length(); i++){
int charIndex = chars.indexOf(message.charAt(i));
if (charIndex < 0) continue;
g.drawImage(spriteSheet.grabLetterImage(1, (charIndex / 1), 16, 16), x += 16, y, null);
}
}
public static int getStringWidth(String message){
return message.length() * SIZE;
}
}
And here’s my SpriteSheet class (to load parts of the spritesheet):
package com.mayogames.zombiecubes.gfx;
import java.awt.image.BufferedImage;
public class SpriteSheet {
private BufferedImage image;
public SpriteSheet(BufferedImage image){
this.image = image;
}
public BufferedImage grabImage(int col, int row, int width, int height){
BufferedImage img = image.getSubimage((col * 32) - 32, (row * 32) - 32, width, height);
return img;
}
public BufferedImage grabLetterImage(int col, int row, int width, int height){
BufferedImage img = image.getSubimage((col * 16) - 16, + (row * 16) - 16, width, height);
return img;
}
}
Here’s the spritesheet itself:
I’m sorry if it feels like I’m asking you guys to do it, but I just can’t figure it out