Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 [2]
  ignore  |  Print  
  Java2D drawing colored bitmap fonts  (Read 4235 times)
0 Members and 1 Guest are viewing this topic.
Offline loom_weaver

JGO Coder


Medals: 17



« Reply #30 - Posted 2012-04-27 05:00:48 »

Hrmm.... so you're looking for Win32 PatBlt functionality?  That is where you take a grey-scale bitmapped glyph and you want to stamp it on the canvas in any color you want?

From some serious searching and forums asking I doubt that it's possible to do this in an efficient manner in Java2D.  The problem being that Java2D has to be cross-platform and not all platforms will have a nice native call that does this.

However, it's really easy to do this in OpenGL using textures and alpha channels.
Offline joeyismusic

Junior Duke





« Reply #31 - Posted 2012-04-27 05:01:54 »

Supporting ogl is over kill for this project

perhaps i'll do what minicraft did by just modifying the array's my self
Offline davedes
« Reply #32 - Posted 2012-04-27 07:36:35 »

Here is a little image tint class based on Romain Guy's ColorTintFilter:
http://pastebin.java-gaming.org/54a6b5d74

Hopefully there's a Java2D guru out there who knows of a better solution, but this seems to work OK on my end. Of course, the font image needs to be white (or close to white) to work.

Optimizations:
  • Minimize re-tinting by "batching" your colours. Draw all red text first, then all green text, etc.
  • Use resetTint instead of tinting to white.
  • You might still want to think about rendering the text variations to a transparent buffered image to "cache" it, instead of re-tinting many times per frame.

What's the easiest and best solution? Use LWJGL (or a derivative) to create sprite-based games in Java... Roll Eyes It's harder to muck around with hacks like these in Java2D. And since you'll probably end up enabling OpenGL/D3D acceleration anyways (the JVM flag), you may as well just use OpenGL directly instead of limiting yourself to Java2D's API.

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

Junior Duke





« Reply #33 - Posted 2012-04-27 07:41:09 »

Thanks so much for this. The reason I am really forcing the J2D is because I don't want to go down that spiral of signed jar's for browser applets etc. etc.
If there's a really easy way to get OGL in a Java applet without asking the user anything... and I can use a pixel-level screen format (1 + 1 = 2 pixels) I'M IN!!
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #34 - Posted 2012-04-27 11:31:35 »

The original font image is never modified, so you can simply redraw the white letters with a different color rect later Tongue

Offline joeyismusic

Junior Duke





« Reply #35 - Posted 2012-04-27 18:21:08 »

The original font image is never modified, so you can simply redraw the white letters with a different color rect later Tongue

I was never under the impression or assumption that the original font image is modified.

I am asking if I can do this (and if it is fast, and if its the right way to go about it):

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
...//font class
public void drawString(String s, Color r) {
    blah blah blah
    draw bitmap images / letters to font buffer image
    change composite
    set color, draw color rect
    reset composite back
}
...//gui code
Font.drawString("hello", red); //composite is changed, color is drawn
//later in code
Font.drawString("cool", orange); //composite is changed again, another color is drawn
...
//game loop
Font.clear();
GuiCode();
Font.render(g);//draws the font buffer image to the screen image
...


If the above is slow or not the proper way to do it, then the only other way to do it with one composite change and multiple colors would be some sort of batch buffer.
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #36 - Posted 2012-04-27 20:19:48 »

Of course, that's fine. Why would you think it wouldn't work? The only "slow" part is actually drawing the bitmap images and the color, but that should be much faster than copying each pixel one by one, unmanaging the image in the process.

Offline joeyismusic

Junior Duke





« Reply #37 - Posted 2012-04-27 20:25:23 »

Of course, that's fine. Why would you think it wouldn't work? The only "slow" part is actually drawing the bitmap images and the color, but that should be much faster than copying each pixel one by one, unmanaging the image in the process.
Ok, cool. I think I will experiment a lot with this. This could perhaps lead to dynamic lighting of some sort as well by maybe rendering a light map image to composite over the scene.
Offline BoBear2681

JGO Coder


Medals: 19



« Reply #38 - Posted 2012-04-27 20:43:08 »

Just to make sure - I assume you're rendering text in lots (hundreds maybe?) of different colors, maybe cycling through them for effect?  Because if you're only writing text in a few different colors, why not simply create a Map<Color, Font>, where each value is created as a copy of the original font image with the original color (e.g. white or black) changed to the new one, and lazily create entries per color?
Offline joeyismusic

Junior Duke





« Reply #39 - Posted 2012-04-27 21:14:13 »

Just to make sure - I assume you're rendering text in lots (hundreds maybe?) of different colors, maybe cycling through them for effect?  Because if you're only writing text in a few different colors, why not simply create a Map<Color, Font>, where each value is created as a copy of the original font image with the original color (e.g. white or black) changed to the new one, and lazily create entries per color?
Just wanted a system that was "smart enough" to do any color without having a version of it in memory. Considering we're just putting pixels on the screen, it should be doable via mathematical equation.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BoBear2681

JGO Coder


Medals: 19



« Reply #40 - Posted 2012-04-28 20:41:43 »

Absolutely true, but if performance becomes an issue, you can always fall back on something like I suggested.  It's simple, and having 20 or so tiny BufferedImages for different colored fonts should be pretty negligible. 
Pages: 1 [2]
  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.

Longarmx (38 views)
2014-10-17 03:59:02

Norakomi (28 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (28 views)
2014-10-15 16:18:58

TehJavaDev (53 views)
2014-10-14 00:39:48

TehJavaDev (54 views)
2014-10-14 00:35:47

TehJavaDev (42 views)
2014-10-14 00:32:37

BurntPizza (64 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (78 views)
2014-10-11 22:30:10
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!