Sign in to follow this  
m_power_hax

extended ascii (, ,...)

Recommended Posts

I'm trying to get the letter "é" to show in the application (Win32 c++ app, with some opengl, using Visual Studio 2008).

std::ostringstream output;
output.setf(std::ios::fixed, std::ios::floatfield);
output << std::setprecision(2);
output << "Sensibilité: " << mouse.weightModifier();
g_font.drawText(1, 1, output.str().c_str());

I tried :

output << "Sensibilit" << char(130): " << mouse.weightModifier();
output << "Sensibilit\x82: " << mouse.weightModifier();

No "é" showed, just "Sensibilit".

Anyone have a solution for this?

Share this post


Link to post
Share on other sites
Like the_edd says, it depends entirely on the font/font library you're using.

The first obvious thing to check is whether said font library and font claim to support extended ASCII, and if so if you've enabled it (have set the correspond character set or some such).

I can't help with Win32 API specifics I'm afraid

Share this post


Link to post
Share on other sites
Quote:
Original post by Beyond_Repair
Like the_edd says, it depends entirely on the font/font library you're using.

The first obvious thing to check is whether said font library and font claim to support extended ASCII, and if so if you've enabled it (have set the correspond character set or some such).

I can't help with Win32 API specifics I'm afraid


Where do i find the font library i'm using in the project?

I don't know if it matters, but in Configuration Properties, under general, Character Set is set to Use Multi-Byte Character Set.

Share this post


Link to post
Share on other sites
Quote:
Original post by m_power_hax
Where do i find the font library i'm using in the project?
How do you draw the text? What do you use? You must have some way to map characters to something visible on the screen. E.g. cout, using a bitmap font you created, using some library you downloaded, using the GDI DrawText() function, using SDL_ttf, etc.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
Quote:
Original post by m_power_hax
Where do i find the font library i'm using in the project?
How do you draw the text? What do you use? You must have some way to map characters to something visible on the screen. E.g. cout, using a bitmap font you created, using some library you downloaded, using the GDI DrawText() function, using SDL_ttf, etc.


Ok well here some code :

From main.cpp

std::ostringstream output;
output.setf(std::ios::fixed, std::ios::floatfield);
output &lt;&lt; std::setprecision(2);
output &lt;&lt; " Sensibilit\x82: " &lt;&lt; mouse.weightModifier();

g_font.begin();
g_font.setColor(0.2f, 0.70f, 0.8f);
g_font.drawText(1, 1, output.str().c_str());
g_font.end();


From gl_font.cpp

void GLFont::begin()
{
HWND hWnd = GetForegroundWindow();
RECT rcClient;

GetClientRect(hWnd, &rcClient);

int w = rcClient.right - rcClient.left;
int h = rcClient.bottom - rcClient.top;

glPushAttrib(GL_CURRENT_BIT | GL_LIGHTING_BIT);

glDisable(GL_LIGHTING);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, m_textureObject);

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0.0f, w, h, 0.0f, -1.0f, 1.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

drawTextBegin();
}

void GLFont::drawTextBegin()
{
m_numCharsToDraw = 0;
m_pVertex = m_vertices;
}

void GLFont::drawChar(char c, int x, int y)
{
// 1------4
// | | 1 = (x, y)
// | | 2 = (x, y + charHeight)
// | | 3 = (x + charWidth, y + charHeight)
// | | 4 = (x + charWidth, y)
// | |
// | |
// 2------3
//

const Glyph &glyph = getChar(c);
int charWidth = glyph.width;
int charHeight = m_charHeight;

if (m_drawDropShadows)
{
//bunch of m_pVertex-&gt; calls
//...
//...
if (++m_numCharsToDraw == MAX_CHARS_PER_BATCH)
{
drawTextEnd();
drawBatchOfChars();
drawTextBegin();
}
}

//bunch of m_pVertex-&gt; calls
//...
//...
if (++m_numCharsToDraw == MAX_CHARS_PER_BATCH)
{
drawTextEnd();
drawBatchOfChars();
drawTextBegin();
}
}

void GLFont::drawText(int x, int y, const char *pszText)
{
char prevCh = 0;
char ch = 0;
int dx = x;
int dy = y;
int charHeight = getCellHeight();
int whitespaceWidth = getChar(' ').width;

while (*pszText != '\0')
{
prevCh = ch;
ch = *pszText++;

if (ch == ' ')
{
if (prevCh != '\r')
dx += whitespaceWidth;
}
else if (ch == '\n' || ch == '\r')
{
dx = x;
dy += charHeight;
}
else if (ch == '\t')
{
dx += whitespaceWidth * TAB_SPACES;
}
else if (ch &gt;= CHAR_FIRST && ch &lt;= CHAR_LAST)
{
drawChar(ch, dx, dy);
dx += getChar(ch).width;
}
}
}

void GLFont::drawChar(char c, int x, int y)
{
// 1------4
// | | 1 = (x, y)
// | | 2 = (x, y + charHeight)
// | | 3 = (x + charWidth, y + charHeight)
// | | 4 = (x + charWidth, y)
// | |
// | |
// 2------3
//

const Glyph &glyph = getChar(c);
int charWidth = glyph.width;
int charHeight = m_charHeight;

if (m_drawDropShadows)
{
//bunch of m_pVertex-&gt; calls
//...
//...
if (++m_numCharsToDraw == MAX_CHARS_PER_BATCH)
{
drawTextEnd();
drawBatchOfChars();
drawTextBegin();
}
}

//bunch of m_pVertex-&gt; calls
//...
//...
if (++m_numCharsToDraw == MAX_CHARS_PER_BATCH)
{
drawTextEnd();
drawBatchOfChars();
drawTextBegin();
}
}


Anything else needed?

Share this post


Link to post
Share on other sites
Please use [ source ] tags in future (I've added them to your post this time).

So m_textureObject is a 2D texture containing the characters you need then (A bitmap font) - does it actually have an 'é' character on it?

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
Please use [ source ] tags in future (I've added them to your post this time).

So m_textureObject is a 2D texture containing the characters you need then (A bitmap font) - does it actually have an 'é' character on it?


Would changing every const char to const wchar_t be the solution?

Share this post


Link to post
Share on other sites
Quote:
Original post by m_power_hax
Quote:
Original post by Evil Steve
Please use [ source ] tags in future (I've added them to your post this time).

So m_textureObject is a 2D texture containing the characters you need then (A bitmap font) - does it actually have an 'é' character on it?


Would changing every const char to const wchar_t be the solution?
No - if the texture doesn't have an image for an 'é' character, you can't draw it. Characters are just a bunch of images after all.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
Quote:
Original post by m_power_hax
Quote:
Original post by Evil Steve
Please use [ source ] tags in future (I've added them to your post this time).

So m_textureObject is a 2D texture containing the characters you need then (A bitmap font) - does it actually have an 'é' character on it?


Would changing every const char to const wchar_t be the solution?
No - if the texture doesn't have an image for an 'é' character, you can't draw it. Characters are just a bunch of images after all.


So how could i add an image for an 'é' character in the texture? I mean, i didn't put an image for each letter for the texture to be able to draw it!

Share this post


Link to post
Share on other sites
Quote:
Original post by m_power_hax
So how could i add an image for an 'é' character in the texture? I mean, i didn't put an image for each letter for the texture to be able to draw it!
How is the texture created? At some point, it's filled with image data - and that image data includes the pixels making up the character.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
Quote:
Original post by m_power_hax
So how could i add an image for an 'é' character in the texture? I mean, i didn't put an image for each letter for the texture to be able to draw it!
How is the texture created? At some point, it's filled with image data - and that image data includes the pixels making up the character.


The code is looping from 32 to 127 (é = 130). Even changing the loop to 256 won't make the "é" to draw.


bool GLFont::extractFontMetrics()
{
HWND hWndDesktop = GetDesktopWindow();
HDC hDC = GetDCEx(hWndDesktop, 0, DCX_CACHE | DCX_WINDOW);

if (!hDC)
return false;

HFONT hPrevFont = reinterpret_cast<HFONT>(SelectObject(hDC, m_hFont));
TEXTMETRIC tm;
SIZE charSize = {0};
char szString[2] = {0};
char szName[128] = {0};

GetTextFace(hDC, 128, szName);
m_name = szName;

GetTextMetrics(hDC, &tm);
m_charHeight = m_cellHeight = tm.tmHeight + tm.tmExternalLeading;
m_charMaxWidth = 0;
m_charAvgWidth = 0;

for (int c = 32; c < 127; ++c)
{
szString[0] = c;
GetTextExtentPoint32(hDC, szString, 1, &charSize);

if (charSize.cx > m_charMaxWidth)
m_charMaxWidth = charSize.cx;

m_charAvgWidth += charSize.cx;
m_glyphs[c - 32].width = charSize.cx;
}

m_charAvgWidth /= TOTAL_CHARS;
m_cellWidth = m_charMaxWidth + (m_charAvgWidth / 2);

SelectObject(hDC, hPrevFont);
ReleaseDC(hWndDesktop, hDC);
return true;
}

void GLFont::generateTexCoords(const Bitmap &bitmap)
{
Glyph *pGlyph = 0;
int col = 0;
int row = 0;
int charWidth = 0;
float bmpWidth = static_cast<float>(bitmap.width);
float bmpHeight = static_cast<float>(bitmap.height);

for (int c = 32; c < 127; ++c)
{
col = (c - 32) % 10;
row = (c - 32) / 10;

pGlyph = &m_glyphs[c - 32];
charWidth = pGlyph->width;

pGlyph->upperLeft[0] = (col * m_cellWidth) / bmpWidth;
pGlyph->upperLeft[1] = (row * m_cellHeight) / bmpHeight;

pGlyph->lowerLeft[0] = (col * m_cellWidth) / bmpWidth;
pGlyph->lowerLeft[1] = ((row + 1) * m_cellHeight) / bmpHeight;

pGlyph->lowerRight[0] = ((col * m_cellWidth) + charWidth) / bmpWidth;
pGlyph->lowerRight[1] = ((row + 1) * m_cellHeight) / bmpHeight;

pGlyph->upperRight[0] = ((col * m_cellWidth) + charWidth) / bmpWidth;
pGlyph->upperRight[1] = (row * m_cellHeight) / bmpHeight;
}
}




Share this post


Link to post
Share on other sites
Quote:
é = 130

In which encoding?

First, read this, then familiarize yourself with this stuff (you don't need to read it all now, but you may need to come back to it as a reference).

Now make sure you're using GetTextExtentPoint32W (and the same for the other Windows text-related functions) with the appropriate argument string argument. This will make sense once you've read the previous material.

I *could* answer the question directly for you, but you'd just get stuck again the next time. Besides, every conscientious developer should know this stuff. It will come up again and again throughout your programming career.

Share this post


Link to post
Share on other sites
Quote:
Original post by m_power_hax
Quote:
Original post by Evil Steve
Quote:
Original post by m_power_hax
So how could i add an image for an 'é' character in the texture? I mean, i didn't put an image for each letter for the texture to be able to draw it!
How is the texture created? At some point, it's filled with image data - and that image data includes the pixels making up the character.


The code is looping from 32 to 127 (é = 130). Even changing the loop to 256 won't make the "é" to draw.


No. That wasn't the question.

How is the texture created?

The texture, i.e., the in-memory representation of an image.

It's loaded from an image on disk, right?

Somewhere, there is an actual image file that shows what all the characters look like. OpenGL is copying image data from that file onto surfaces. If there is no part of the picture that looks like 'é', then it can't draw that, because there is nothing to copy.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
How is the texture created?

The texture, i.e., the in-memory representation of an image.

It's loaded from an image on disk, right?
I'd assume that it's a TTF -> texture at load time, given the OP is getting text metrics rather than reading them from a file. I could be wrong of course...

Quote:
Original post by m_power_hax
The code is looping from 32 to 127 (é = 130). Even changing the loop to 256 won't make the "é" to draw.
Why not? Have you debugged it? Are your arrays big enough? Is the texture image large enough? Is there anything that won't try to draw characters > 128?

Share this post


Link to post
Share on other sites
There is also this, which look like creating a bitmap...but im not sure of what it does.


bool GLFont::createFontBitmap()
{
// The font is drawn as a 10 x 10 grid of characters.

if (!extractFontMetrics())
return false;

int w = 10 * m_cellWidth;
int h = 10 * m_cellHeight;
Bitmap bitmap;

if (!bitmap.create(nextPower2(10 * m_cellWidth), nextPower2(10 * m_cellHeight)))
return false;

int x = 0;
int y = 0;
int ch = 32;
HFONT hPrevFont = 0;
COLORREF prevColor = 0;
RECT rc = {0, 0, bitmap.width, bitmap.height};

bitmap.selectObject();
hPrevFont = reinterpret_cast<HFONT>(SelectObject(bitmap.dc, m_hFont));
prevColor = SetTextColor(bitmap.dc, RGB(255,255,255));
SetBkMode(bitmap.dc, TRANSPARENT);
FillRect(bitmap.dc, &rc, reinterpret_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)));

for (int i = 0; i < 10; ++i)
{
y = m_cellHeight * i;

for (int j = 0; j < 10; ++j)
{
x = m_cellWidth * j;

if (ch > 31 && ch < 255)
TextOut(bitmap.dc, x, y, reinterpret_cast<LPCWSTR>(&ch), 1);

++ch;
}
}

SetTextColor(bitmap.dc, prevColor);
SelectObject(bitmap.dc, hPrevFont);
bitmap.deselectObject();

generateTexCoords(bitmap);
return createTexture(bitmap);
}

Share this post


Link to post
Share on other sites
Quote:
Original post by m_power_hax
There is also this, which look like creating a bitmap...but im not sure of what it does.
If you're not sure what it does, you really shouldn't be using it. There's no point copy & pasting code without understanding it.

The code generates a bitmap and uses GDI to print characters onto it. It starts at ASCII code 32 (space), and adds the characters in a 10x10 grid, meaning the last character drawn will be ASCII 132.

It's hard to say what the issue is though; have you tried saving the image to disk to look at to see if the character is on it? You'll need to use another library to do that (Unless you write BMP saving code yourself, which is fairly easy).

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
Quote:
Original post by m_power_hax
There is also this, which look like creating a bitmap...but im not sure of what it does.
If you're not sure what it does, you really shouldn't be using it. There's no point copy & pasting code without understanding it.

The code generates a bitmap and uses GDI to print characters onto it. It starts at ASCII code 32 (space), and adds the characters in a 10x10 grid, meaning the last character drawn will be ASCII 132.

It's hard to say what the issue is though; have you tried saving the image to disk to look at to see if the character is on it? You'll need to use another library to do that (Unless you write BMP saving code yourself, which is fairly easy).


I didn't code all the software, someone else did, i have no choice but to work with this code for now. I'll try changing the grid size to see if it change something.

Share this post


Link to post
Share on other sites
Quote:
Original post by m_power_hax
Nope, didn't work. I was able to remove characters (making the grid be 5x5 as an example). But making the grid (16x16) and making the loops go from 32 to 256 didnt change anything.
What if you save the texture to a BMP file? Does it look correct (I.e. does it have the new characters on it)?

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
Quote:
Original post by m_power_hax
Nope, didn't work. I was able to remove characters (making the grid be 5x5 as an example). But making the grid (16x16) and making the loops go from 32 to 256 didnt change anything.
What if you save the texture to a BMP file? Does it look correct (I.e. does it have the new characters on it)?


I don't know how to save the texture.

Share this post


Link to post
Share on other sites
Quote:
Original post by m_power_hax
Quote:
Original post by Evil Steve
Quote:
Original post by m_power_hax
Nope, didn't work. I was able to remove characters (making the grid be 5x5 as an example). But making the grid (16x16) and making the loops go from 32 to 256 didnt change anything.
What if you save the texture to a BMP file? Does it look correct (I.e. does it have the new characters on it)?


I don't know how to save the texture.


Consider a career change. You seem to lack the understand and drive to research things in order to be a programmer.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this