   Simplest way to find X&Y on a .BMP, depending on ASCII value
  Yes, my math skills suck. Majorly. That's why I'm coming here for assistance. >_>Basically, I have a 128x128 and a 256x256 font bitmap that I'm trying to draw with a simple function renderText(x,y,text). I'm also trying to make these functions as small as possible, so I of course am using math to help minimize these. Problem is, these functions only work so well. Some letters they draw properly, some they just mess up on.The bitmaps have 16 characters each row. We'll deal with "X" first... here we're drawing A to P (16 characters), it can draw A-O properly... but since P is the 16th, my math screws up here. The formula I'm using is ((((int)text-1)%16)*8). text is the letter, -1 helps with offset, %16 finds remainder, and *8 gets it to the proper pixel location (8x8 cells). I use *16 for the larger bitmap.Next with "Y"... this one *seems* to have the same problem, since A-O are rendering just fine, but the "Y" goes to next level at P. My formula for this is (((int) text-((int)text%16)))/2). ((int) text-((int)text%16))) takes off the remainder, and then /2 is basically the same as /16 to find which "row" the letter is on, and then *8 to find its pixel location.As you can see these formulas are pretty simple, but still screwing up on me. Told you my math sucks. :)Also, here's some statistics that might help those that would like to help me calculate where I'm trying to draw. 'A' is drawn from 0,32 to 8,40 on the bitmap, 'P' is drawn from 120,32 to 127,40, and 'Q' is drawn from 0,40 to 8,48.Anywho, any and all help is MUCH appreciated!-Etyrn
  I usually recommend looking at the data in the debugger -- this sort of problem tends to be rather easy to fix if you see what values you are getting for your problem characters.As an example, lets assume text is equal to the problem character 'P'.  This character has an ASCII value of 80, so your (int)text will equal 80.  Using this value, your X calculation would equal:((80 - 1) % 16) * 8((79) % 16) * 8(15) * 8120This appears to be correct for the X value (I would still recommend checking this in the debugger to verify that text really has the character 'P' in it, and that we haven't made any simple math errors).Next, your Y calculation:(80 - (80 % 16)) / 2(80 - (0)) / 2(80) / 240As you can see this is where your mistake is -- the letter P causes your Y value to move down a row.  You could fix the error in your Y calculation, but I would recommend rewriting it to improve readability.  For example:int char_index = text - 'A';int cell_x = char_index % 16;int cell_y = char_index / 16;int pixel_x = cell_x * 8;int pixel_y = cell_y * 8 + 32;This way, the code is easier to understand for yourself or other programmers that might need to maintain your work.;  'A' is drawn from 0,32 to 8,40 on the bitmap, 'P' is drawn from 120,32 to 127,40, 'Q' is drawn from 0,40 to 8,48.Perhaps I don't understand your layout, but I would expect 'A' to end at 0,39 instead of 0,40.  After all, 0,40 is a part of 'Q', right?  Also, I noticed that 'A' is 9 pixels wide but 'P' is only 8 pixels wide ... is that your intention?Oh, and I second FugueInCPP 100% - use the debugger and see what is actually happening.  If your equations aren't producing the right values, it might help to break up your math into several steps so you can see exactly how you are deriving those wrong results.  If your equations *are* producing what you expect and you're letters are messing up then your font map is wrong or your logic is.Good Luck   