Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (576)
games submitted by our members
Games in WIP (497)
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  
  Not able to render my font :(  (Read 339 times)
0 Members and 1 Guest are viewing this topic.
Offline Delta517

Senior Member


Medals: 2
Projects: 1



« Posted 2013-05-12 16:10:14 »

Hi again Smiley

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 Sad I tried Googling and even found some videos, but nothing works Sad The problem is that I can't find the right math formula to use, to get the letter I want Sad Here's my code:

Font class:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
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):

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
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 Sad
Offline DazKins
« Reply #1 - Posted 2013-05-12 16:18:35 »

Well for starters, you have different letters placed on different rows and columns so setting the column to always be one seems a bit dodgy to me.

The way i would approach this is something like this: (this is from my entry to last Decembers Ludum Dare so it is not all exactly the same as yours)

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
public class Font {
   private static String alph0 = "abcdefghijklmnopqrstuvwxyz0123456789.,:;'\"!?$%()-=+/ ";
   
   public static void renderText(String t, Bitmap b, int xp, int yp) {
      t = t.toLowerCase();
      for(int x = 0; x < t.length(); x++) {
         int drawIndex = alph0.indexOf(t.charAt(x));
         if (drawIndex != 52) {
            b.blit(Image.font[drawIndex % 26][drawIndex / 26], x * 16 + xp, yp, 1.0f, false, false);            
         }
      }
   }
}


The first deimension of the Image.font array represents the column while the second dimension represents the row.

I hope this helps  Smiley

DazKins

Check out my Dev Blog: http://dazkins.tumblr.com
Offline Delta517

Senior Member


Medals: 2
Projects: 1



« Reply #2 - Posted 2013-05-12 16:35:07 »

I tried like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
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 ="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,:;'\"!?$%()-=/?$%()-=+/";
   
   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((charIndex % 61), (charIndex / 61), 16, 16), x += 16, y, null);
      }
   }
   
   public static int getStringWidth(String message){
      return message.length() * SIZE;
   }
}


But only got a ByteInterleavedTaster error Sad I also fixed the String chars now Tongue
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Delta517

Senior Member


Medals: 2
Projects: 1



« Reply #3 - Posted 2013-05-12 22:40:11 »

Was able to fix it myself with some derp coding ^^

Here's the code if anyone else needs it:

Spritesheet class (had to change a little on the getLetterImage method:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
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), + (row * 16), width, height);
      return img;
   }
}


And here's the font class:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
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      0123456789.,:'\"!?$%()-=+/ ";
   
   public void render(Graphics g, String message, int x, int y){
      message = message.toUpperCase();
     
      for(int i = 0; i < message.length(); i++){
         int row = 14;
         
         int charIndex = chars.indexOf(message.charAt(i));
         
         if(charIndex < 0) continue;
         
         if(charIndex >= 16 && charIndex <= 31){
            charIndex -= 16;
            row = 15;
         }else if(charIndex >= 32 && charIndex <= 47){
            charIndex -= 32;
            row = 16;
         }else if(charIndex >= 48 && charIndex <= 63){
            charIndex -= 48;
            row = 17;
         }
         
         g.drawImage(spriteSheet.grabLetterImage(charIndex, row, 16, 16), x += 16, y, null);
      }
   }
   
   public static int getStringWidth(String message){
      return message.length() * SIZE;
   }
}


Note: There is probably another way to get the row method, but I'm still a newbie so that's how I fixed it Tongue It works though Wink
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.

xsi3rr4x (12 views)
2014-04-15 18:08:23

BurntPizza (10 views)
2014-04-15 03:46:01

UprightPath (24 views)
2014-04-14 17:39:50

UprightPath (10 views)
2014-04-14 17:35:47

Porlus (27 views)
2014-04-14 15:48:38

tom_mai78101 (49 views)
2014-04-10 04:04:31

BurntPizza (107 views)
2014-04-08 23:06:04

tom_mai78101 (207 views)
2014-04-05 13:34:39

trollwarrior1 (176 views)
2014-04-04 12:06:45

CJLetsGame (182 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
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!