Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (408)
games submitted by our members
Games in WIP (293)
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  
  Custom Fonts  (Read 1158 times)
0 Members and 1 Guest are viewing this topic.
Online cubemaster21

Senior Member


Medals: 4
Projects: 1



« Posted 2012-04-14 20:07:05 »

I've been working on my own game for a while and think that it's about time that i included my own custom font, like in minecraft, or CatacombSnatch. Does anyone know how to help me out with this?

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline davedes
« Reply #1 - Posted 2012-04-14 20:24:20 »

Look into MatthiasM's Font Tool (which is part of TWL's theme editor). It can export a TTF font as a bitmap image with an XML or text file. Then, you simply read the font file and its kerning information and use it when rendering each glyph. For that, look into TWL's BitmapFont class:
http://hg.picibo.com/twl/file/1055c387f54e/src/de/matthiasmann/twl/renderer/lwjgl/BitmapFont.java

Alternatively, you could simply use TWL or another GUI library, as most of them support text rendering (with line wrapping, styling, etc).

Online ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #2 - Posted 2012-04-14 20:29:51 »

I believe the way Notch does it is he manually draws the characters onto an image and then has a method for drawing the specified characters from that image.

Look into the g.drawImage(Image image, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer) method.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online cubemaster21

Senior Member


Medals: 4
Projects: 1



« Reply #3 - Posted 2012-04-14 22:36:02 »

Thanks, I've looked into that and now the font will render... just not correctly...

Here is the code I'm using:
http://pastebin.java-gaming.org/54a6b9d54

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline _Al3x

JGO Coder


Medals: 7
Projects: 1


Indie Games FTW!


« Reply #4 - Posted 2012-04-14 22:38:48 »

1  
2  
3  
4  
public static void Render(Graphics2D g, String msg, int x, int y){
       msg = msg.toUpperCase();
       int length = msg.length();
       ;


There's a ; that shouldn't be there, but I don't think that's the problem, will you tell us what's not correct? Smiley

Online ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #5 - Posted 2012-04-14 22:49:18 »

Shocked you re-read and re-draw the image every single time you call render! Don't do that! Load the image once and store it as a variable to be reused.

When drawing the letter, your destination x2 should be x+8 not x + (length * 8 ) since that's making the letter as wide as the entire word length Wink

Also, since your letters at 8x8 pixels, you should be doing (c%29) * 8 and (c/29) * 8

Lastly, you should check if c == -1 so it can skip drawing that non-existant letter. This will probably be needed for spaces so don't forget to add 8 to x, or better yet, don't add 8 to x and simply add i * 8 to x when drawing Smiley

Online cubemaster21

Senior Member


Medals: 4
Projects: 1



« Reply #6 - Posted 2012-04-14 23:07:12 »

Shocked you re-read and re-draw the image every single time you call render! Don't do that! Load the image once and store it as a variable to be reused.

When drawing the letter, your destination x2 should be x+8 not x + (length * 8 ) since that's making the letter as wide as the entire word length Wink

Also, since your letters at 8x8 pixels, you should be doing (c%29) * 8 and (c/29) * 8

Lastly, you should check if c == -1 so it can skip drawing that non-existant letter. This will probably be needed for spaces so don't forget to add 8 to x, or better yet, don't add 8 to x and simply add i * 8 to x when drawing Smiley
THIS. Thank you so much, I think that you can tell that I'm relatively new at this. This solved my problems. You, my sir, are awesome.

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Online ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #7 - Posted 2012-04-14 23:16:34 »

Shocked you re-read and re-draw the image every single time you call render! Don't do that! Load the image once and store it as a variable to be reused.

When drawing the letter, your destination x2 should be x+8 not x + (length * 8 ) since that's making the letter as wide as the entire word length Wink

Also, since your letters at 8x8 pixels, you should be doing (c%29) * 8 and (c/29) * 8

Lastly, you should check if c == -1 so it can skip drawing that non-existant letter. This will probably be needed for spaces so don't forget to add 8 to x, or better yet, don't add 8 to x and simply add i * 8 to x when drawing Smiley
THIS. Thank you so much, I think that you can tell that I'm relatively new at this. This solved my problems. You, my sir, are awesome.
Glad to help! Grin

EDIT: One thing I didn't mention, no need to round that last source y2, since you're already doing int division.

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

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (131 views)
2013-05-17 21:29:12

alaslipknot (140 views)
2013-05-16 21:24:48

gouessej (170 views)
2013-05-16 00:53:38

gouessej (164 views)
2013-05-16 00:17:58

theagentd (174 views)
2013-05-15 15:01:13

theagentd (159 views)
2013-05-15 15:00:54

StreetDoggy (203 views)
2013-05-14 15:56:26

kutucuk (227 views)
2013-05-12 17:10:36

kutucuk (227 views)
2013-05-12 15:36:09

UnluckyDevil (230 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.102 seconds with 20 queries.