Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (115)
games submitted by our members
Games in WIP (562)
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  
  Bitmap & Font Help  (Read 1210 times)
0 Members and 1 Guest are viewing this topic.
Offline lethalwire

Senior Newbie





« Posted 2011-12-26 19:48:28 »

I'm reading the red book and I'm in the section about bitmaps and fonts.
What I'm trying to do is setup a bitmap for each letter (uppercase/lowercase) and number and store the data into an inner-class I call RasterFontInfo.

The class RasterFont contains RasterFontInfo and sets up the Map.
A bitmap for each char can be called from a Map that is setup like:
Map<Character, RasterFontInfo> map;

RasterFontInfo contains width, height, x0, y0, and a ByteBuffer which holds the bitmap data.

So far, I've been pretty successful but I'm now blocked with the following problem:


Uploaded with ImageShack.us

All of the letters are showing up correctly except for m, G, J, M, O, Q, and 1. I've tried looking for patterns between these but I can't see any.
I'm hoping someone can give insight as to why the letters show up like this.
If you need the code I have the following:

The code I'm using to draw each letter looks like:
1  
2  
3  
4  
5  
6  
7  
8  
glTranslatef(0.0f, 0.0f, zTranslation);
      glRasterPos2f(-3.2f, 0f);
      char[] phrase = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
     
      for(char c : phrase) {
         RasterFontInfo rfi = characterMap.get(c);
         glBitmap(rfi.getWidth(), rfi.getHeight(), (float)rfi.getX0(), (float)rfi.getY0(), (float)rfi.getWidth()+2, 0, rfi.getBuffer());
      }


Code for deriving a RasterFontInfo object:
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  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
   private RasterFontInfo deriveRasterFontInfo(char c) {
      /* keeps track of width and height of font raster */
      int width = 0, height = 0;
     
      String stringValueOfC = String.valueOf(c);

      /* Temporary drawing area for c */
      BufferedImage temporaryBufferedImage = new BufferedImage(1024, 1024,
            BufferedImage.TYPE_INT_RGB);
     
      /* Acquire graphics object to draw char to drawing area */
      Graphics2D g2d = (Graphics2D) temporaryBufferedImage.getGraphics();
      g2d.setFont(getFont());
     
      /* Acquire FontMetrics object to get geometrical data from font */
      FontMetrics fontMetrics = g2d.getFontMetrics();
     
      /* Acquire geometrical font data */
      int charHeight = fontMetrics.getHeight();
      int charWidth = fontMetrics.stringWidth(stringValueOfC);
      int charDescent = fontMetrics.getDescent();
      Dimension charDimension = new Dimension(charWidth, charHeight+charDescent);
     
      g2d.drawString(stringValueOfC, 0, charHeight);
     
      /* Acquire a sub-image that only contains the drawing area of the char */
      BufferedImage bi = temporaryBufferedImage.getSubimage(0, 0, (int)charDimension.getWidth(), (int)charDimension.getHeight());


      char[][] bitmapArray = new char[bi.getHeight()][multipleOfEight(bi.getWidth())];
      for(char[] ch : bitmapArray)
         java.util.Arrays.fill(ch, '0');
     
      /* setup bitmap array based on values read from the subimage's RGB coordinates */
      /* the result is a 2D-array filled with 0's and 1's */
      for(int i = bi.getHeight()-1; i >= 0; --i) {
         for(int j = bi.getWidth()-1; j >= 0; --j) {
            if(bi.getRGB(j,i) == -16777216)
               bitmapArray[i][j] = '0';
            else if (bi.getRGB(j,i) == -1)
               bitmapArray[i][j] = '1';
         }
      }
     
     
      /* Clear unused bits from bitmapArray */
      bitmapArray = clearUnusedBits(bitmapArray);
     
      /* Temporary list to hold chunks of translated data from bitmap */
      ArrayList<Byte> chunkList = new ArrayList<Byte>();
      int numChunks = bitmapArray[0].length / 8;
     
      /* loop through each array of bitmapArray beginning from the end */
      for(int lineNumber = bitmapArray.length - 1; lineNumber >= 0; --lineNumber) {
         /* sets up data to insert into chunk list */
         String currentLine = new String(bitmapArray[lineNumber]);
         String[] chunks = new String[numChunks];
         getChunks(chunks, currentLine);
         
         int[] binaryChunks = new int[numChunks];
         getBinaryChunks(binaryChunks, chunks);
         
         for(int binaryChunk : binaryChunks)
            chunkList.add((byte) binaryChunk);
         
         /* computes height and width of font raster */
         height = bitmapArray.length;
         int x1 = -1, x2 = -1;
         if( currentLine.contains("1") ) {
            /* count the distance from the first 1 to the last 1 */
            /* the greatest distance found instantiates the width of the raster of the font */
            x1 = currentLine.indexOf('1');
            x2 = currentLine.lastIndexOf('1');
            int leadingZeros = currentLine.substring(0, x1).length();
            int distance = x2 - x1 + 1 + leadingZeros;
            width = Math.max(width, distance);
         }
      }
     
      /* Prints each bitmap */
      System.out.println("---");
      for(char[] ch : bitmapArray) {
         System.out.println(new String(ch));
      }
      System.out.println("---");
     
     
      /* determine x0 and y0, used for glBitmap */
      int x0 = 0;
      int y0 = 0;
     
      /* add chunkList elements to ByteBuffer */
      ByteBuffer buffer = BufferUtils.createByteBuffer(chunkList.size());
      for(Byte b : chunkList)
         buffer.put(b);
      buffer.flip();
     
      /* Setup and return RasterFontInfo used for Map */
      RasterFontInfo rfi = new RasterFontInfo(buffer, width, height, x0, y0);
      return rfi;
   }


And if you need all of the files, they're located here: (2 .java files)
https://skydrive.live.com/?cid=f6516fcd92fde78e#cid=F6516FCD92FDE78E&id=F6516FCD92FDE78E!130
Offline theagentd
« Reply #1 - Posted 2011-12-28 07:49:54 »

1  
2  
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_PACK_ALIGNMENT, 1);

This maybe?

See number 7 here: http://www.opengl.org/resources/features/KilgardTechniques/oglpitfall/

Myomyomyo.
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.

BurntPizza (28 views)
2014-09-21 02:42:18

BurntPizza (18 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (27 views)
2014-09-20 20:14:06

BurntPizza (29 views)
2014-09-19 03:14:18

Dwinin (46 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (102 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56

mitcheeb (71 views)
2014-09-08 06:06:29
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!