Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Slick2d Loading font images from glyphs  (Read 2093 times)
0 Members and 1 Guest are viewing this topic.
Offline DrewLols

Senior Devvie


Medals: 3
Projects: 1


Noob going through metamorphosis...


« Posted 2012-06-09 01:23:33 »

Hello, I am currently building a game using slick2D.  It's going great so far, but I've hit a snag.  I can't seem extract images from a .ttf or .fnt file.  What I mean is I can't seem to get a glyph's image from a font file.

Given the way that my game engine is built, I never want to draw strings directly from the render() method.  My game works by creating displayable objects containing Slick's built in Image class and putting those objects into containers, then finally, displaying the container.  This simplifies the act of treating a group of objects as one.  I only mention this because I've seen other people draw strings from the render methods directly onto the screen.  I want to avoid using this method of drawing text, you see.

Is there any way, in Slick2D, to easily get a glyph's image from a .ttf or .fnt file?  I see that there is a Glyph class which supplies an image for a particular glyph, but found that it requires a TON of information in order to instantiate it.

If I can't find a way to make this work, I may have to create a .png map of characters myself and read from that file.  This is also something I would like to avoid...  Any thoughts?

Did you know that 90% of statistics are wrong?
Offline davidllanos22

Junior Devvie


Projects: 2


I'm just a learner!


« Reply #1 - Posted 2012-06-09 08:12:43 »

What I walways do is use AngelCodeFont class. Take whatever font you want, open it in BMFont play with the parameters and export. The program will export a png/tga file and a fnt file. And then in java:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
//define image
private Image img;
//define angecodefont
public static AngelCodeFont font;

//init method of the main class
public void init(GameContainer gc){

//load image (file location,flip?,filter)
img= new Image("data/fonts/font.png",false,Image.FILTER_NEAREST);

//load font(fnt location,image)
font= new AngelCodeFont("data/fonts/font.fnt",img);

}


Once you learn to use it is very easy and fast to use it! And for render it just this:

1  
2  
3  
4  
5  
public void render(Graphics g){

font.render(10,10,"Hello World!");

}


And so...where do you want to render the string if not from the render method?o.O

Take a look at the javadoc too: http://slick.cokeandcode.com/javadoc/org/newdawn/slick/AngelCodeFont.html

Hope it helps Smiley

Offline DrewLols

Senior Devvie


Medals: 3
Projects: 1


Noob going through metamorphosis...


« Reply #2 - Posted 2012-06-09 13:59:44 »

I appreciate your input.  So by the post above, if you use AngelCodeFont, specify an image, and draw a string, it will draw to that image?  Also, maybe I should clarify.  I extended the BasicGame class so that it only paints a heirarchy of images.  It really simplifies trying to find a certain visual effect.  In the end, the text would be rendered to the screen, but it would not be done directly.

Did you know that 90% of statistics are wrong?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline davedes
« Reply #3 - Posted 2012-06-10 23:42:03 »

I'd say your design is flawed if you can't simply call "font.draw()" in your game. For example; instead of relying on a list of Image objects, you should write your own Entity or Renderable interface with a draw() method. Subclasses (e.g. ImageEntity, TextEntity) would simply override the draw method to implement the given functionality.

Also keep in mind that it's generally wise to "batch" images that share the same texture (i.e. sprite sheets, bitmap fonts) to improve performance. So, instead of making each of your sprites in the same sheet call Image.draw(), you should look into using startUse/endUse/drawEmbedded.

For bitmap fonts, look into TWL's Font Tool (save as text) or Hiero. Then you simply use AngelCodeFont to load and render the text.

Offline DrewLols

Senior Devvie


Medals: 3
Projects: 1


Noob going through metamorphosis...


« Reply #4 - Posted 2012-06-11 22:56:25 »

I DO plan to do something along the lines of "font.draw()" like you suggested, but until I get to that point, I need to first figure out how to get individual glyph images from fonts...

Did you know that 90% of statistics are wrong?
Offline davedes
« Reply #5 - Posted 2012-06-11 23:06:43 »

Still not sure why you need individual images for this, but anyways, I pushed an enhancement to AngelCodeFont some time ago that lets you get the individual Glyph for a character. There is an example in FontTest that uses "custom rendering" by grabbing each sub-image and rendering it within a startUse/endUse block. Grab the new code from the slick dev branch.

Two things to keep in mind: The glyph image is a sub-image -- i.e. each glyph points to the same PNG texture. Also ensure you use startUse/endUse otherwise your performance will take a hit.

Offline DrewLols

Senior Devvie


Medals: 3
Projects: 1


Noob going through metamorphosis...


« Reply #6 - Posted 2012-06-12 01:04:04 »

Ah, okay then.  The reason I insist on rendering in this fashion is because I come from a Flash background where every object that is displayed is considered a symbol rather than bitmap data that can be painted on.  Transitioning can get a bit confusing...

Did you know that 90% of statistics are wrong?
Offline davedes
« Reply #7 - Posted 2012-06-12 02:15:03 »

You can do the same thing using interfaces in Java.

1  
2  
3  
public interface Sprite {
    public void draw(Graphics g, int x, int y);
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
public class Label implements Sprite {
    private String str;

    public Label(String str) {
        this.str = str;
    }

    public void draw(Graphics g, int x, int y) {
        g.drawString(str, x, y);
    }
}


Apply the same pattern to Image, Animation, etc.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (36 views)
2014-12-15 09:26:44

Mr.CodeIt (29 views)
2014-12-14 19:50:38

BurntPizza (61 views)
2014-12-09 22:41:13

BurntPizza (98 views)
2014-12-08 04:46:31

JscottyBieshaar (58 views)
2014-12-05 12:39:02

SHC (74 views)
2014-12-03 16:27:13

CopyableCougar4 (76 views)
2014-11-29 21:32:03

toopeicgaming1999 (137 views)
2014-11-26 15:22:04

toopeicgaming1999 (127 views)
2014-11-26 15:20:36

toopeicgaming1999 (37 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!