I couldn’t come up with a more elegant way of drawing any color font from a single bitmap (png) font loaded in memory.
The only way I could get it to work was to render everything into a buffered image, which is then modified at pixel level by the font drawing.
The only other way I can think of how to do this would be to create the buffered image, then steal its array, and have to do my own pixel copying / color operations from there on instead of using drawImage calls… But I’d really like to avoid going that far if I could!
public final class Font {
private static int trans = 16777215;
public static final int w = 8;
public static final int h = 8;
public static void printString(BufferedImage bimg, String string, int x, int y, int width, int color, int bgcolor) {
int px = 0;
int py = 0;
char c;
int sx;
int sy;
int pixel_x = 0;
int pixel_y = 0;
int ix = 0;
int iy = 0;
int i = 0;
int src_color = 0;
for(i = 0; i < string.length(); i++) {
c = string.charAt(i);
if(c == 32) {
px += w * 2;
if(px / (w * 2) > width) {
px = 0;
py += h * 2;
}
continue;
}
sx = c;
sy = 0;
while(sx > 15) {
sx -= 16;
sy++;
}
sx *= w;
sy *= h;
for(ix = 0; ix < w; ix++) {
for(iy = 0; iy < h; iy++) {
src_color = Art.font.getRGB(sx + ix, sy + iy);
if(src_color != trans) {
pixel_x = x + px + (ix * 2);
pixel_y = y + py + (iy * 2);
if(pixel_x >= 0 && pixel_x + 2 < Game.WIDTH && pixel_y >= 0 && pixel_y + 2 < Game.HEIGHT) {
bimg.setRGB(pixel_x + 1, pixel_y + 1, bgcolor);
bimg.setRGB(pixel_x + 2, pixel_y + 2, bgcolor);
bimg.setRGB(pixel_x + 2, pixel_y + 1, bgcolor);
bimg.setRGB(pixel_x + 1, pixel_y + 2, bgcolor);
}
if(pixel_x >= 0 && pixel_x + 1 < Game.WIDTH && pixel_y >= 0 && pixel_y + 1 < Game.HEIGHT) {
bimg.setRGB(pixel_x + 0, pixel_y + 0, color);
bimg.setRGB(pixel_x + 1, pixel_y + 1, color);
bimg.setRGB(pixel_x + 1, pixel_y + 0, color);
bimg.setRGB(pixel_x + 0, pixel_y + 1, color);
}
}
}
}
px += w * 2;
if(px / (w * 2) > width) {
px = 0;
py += h * 2;
}
}
}
}