I'm on windows 8.1, visual studio 2017.
I'm using this pricedown font in a directx project I'm working on.
I load it with AddFontResourceEx
and create a font for it with D3DXCreateFont
.
When I hit "Local Windows Debugger" everything is fine, font renders. Be it in release or in debug mode. Problem arises when I go through any executable, it never renders said font, be it release or debug.
So I went reading, I read the articles on msdn, this one and others whenever needed.
I don't think I'm doing anything wrong, my Resource View looks like this:
, and IDR_FONT1 looks like this:
The file is automatically loaded into the solution explorer (I didn't add it, VS did from the Resource.rc file), as you can see here:
With these proprieties:
I add it like so:
AddFontResourceEx("pricedown.ttf", FR_PRIVATE, 0);
this->createFont("Pricedown", 60, true, false);
Where createfont is my function to add the font (stripped down, it has arrays):
bool D3D9Render::createFont(char *name, int size, bool bold, bool italic)
{
D3DXCreateFont(m_pD3dDev, size, 0, (bold) ? FW_BOLD : FW_NORMAL, 0, (italic) ? 1 : 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH, name, &m_pFont);
return true;
}
I'm compiling it as x64 release.
As I've said, it works and renders the font when I press "Local Windows Debugger" (in any mode including x64 release), but when I go to project/x64/Release
, it just won't render the font. Even the executable size is adequate.
GetLastError
on the AddFontResource
is 2 (ERROR_FILE_NOT_FOUND)
What am I doing wrong?
(Read the answer until the end, or you'll waste a lot of time.)
I got it. I had read over this blog post.
Here is an example on how to use AddFontMemResourceEx on a font file embedded in the resource.
HINSTANCE hResInstance = AfxGetResourceHandle( ); //Read the edit
HRSRC res = FindResource(
hResInstance,
MAKEINTRESOURCE(IDR_MYFONT),
L"BINARY" //Read The Edit
);
if (res)
{
HGLOBAL mem = LoadResource(hResInstance, res);
void *data = LockResource(mem);
size_t len = SizeofResource(hResInstance, res);
DWORD nFonts;
m_fonthandle = AddFontMemResourceEx(
data, // font resource
len, // number of bytes in font resource
NULL, // Reserved. Must be 0.
&nFonts // number of fonts installed
);
if(m_fonthandle==0)
{
MessageBox(L"Font add fails", L"Error");
}
}
Though you need afxwin.h, and from here:
afxwin.h is MFC and MFC is not included in the free version of VC++ (Express Edition)
EDIT:
You do not need to use AfxGetResourceHandle
(why you would need afxwin.h
), you can simply do:
HINSTANCE hResInstance = (HINSTANCE)GetModuleHandle(NULL);
And in FindResource
, the 3rd parameter should be RT_FONT
, and so you'd get:
HRSRC res = FindResource(hResInstance, MAKEINTRESOURCE(IDR_FONT1), RT_FONT);