Fonts

Started by
7 comments, last by NeoFlame 17 years, 6 months ago
Hi this is probably a noob question... but i'm reading Prpgramming RPG with directX 2nd edition, and I'm doing the Font example. I'm using the latest SDK (August 2006) and Visual C++ 2005 Express. The code doesn't compile, even the code in the book doesn't compile. this is the code the book use for initialization and drawing : // Create the font ZeroMemory(&Font, sizeof(Font)); strcpy(Font.lfFaceName, "Arial"); Font.lfHeight = -32; D3DXCreateFontIndirect(g_pD3DDevice, &Font, &g_pFont); *********************************************************************** // Begin scene if(SUCCEEDED(g_pD3DDevice->BeginScene())) { // Draw some text g_pFont->Begin(); g_pFont->DrawText("Programming is Fun!", -1, &Rect, DT_CENTER | DT_VCENTER, 0xFFFFFFFF); g_pFont->End(); // End the scene g_pD3DDevice->EndScene(); } When I compile it, it says : c:\documents and settings\neoflame\my documents\programation\programming rpg with directx\bookcode\chap02\font\winmain.cpp(136) : warning C4996: 'strcpy' was declared deprecated 1> c:\program files\microsoft visual studio 8\vc\include\string.h(73) : see declaration of 'strcpy' 1> Message: 'This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.' 1>c:\documents and settings\neoflame\my documents\programation\programming rpg with directx\bookcode\chap02\font\winmain.cpp(138) : error C2664: 'D3DXCreateFontIndirectA' : cannot convert parameter 2 from 'LOGFONT *' to 'const D3DXFONT_DESCA *' 1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast 1>c:\documents and settings\neoflame\my documents\programation\programming rpg with directx\bookcode\chap02\font\winmain.cpp(171) : error C2039: 'Begin' : is not a member of 'ID3DXFont' 1> c:\program files\microsoft directx sdk (august 2006)\include\d3dx9core.h(314) : see declaration of 'ID3DXFont' 1>c:\documents and settings\neoflame\my documents\programation\programming rpg with directx\bookcode\chap02\font\winmain.cpp(172) : error C2660: 'ID3DXFont::DrawTextA' : function does not take 5 arguments 1>c:\documents and settings\neoflame\my documents\programation\programming rpg with directx\bookcode\chap02\font\winmain.cpp(173) : error C2039: 'End' : is not a member of 'ID3DXFont' 1> c:\program files\microsoft directx sdk (august 2006)\include\d3dx9core.h(314) : see declaration of 'ID3DXFont' By looking at the SDK I was able to do this : // Create the font strcpy_s(g_strFont, "Arial"); ZeroMemory(&Font, sizeof(Font)); Font->FaceName = g_strFont; Font->Height = 32; D3DXCreateFontIndirect(g_pD3DDevice, Font, &g_pFont); ************************************************************************ // Begin scene if (SUCCEEDED(g_pD3DDevice->BeginScene())) { // Draw some text //g_pFont->Begin(); g_pFont->DrawText(NULL, "WAAAAZZZZZAAAAPPPP", -1, &Rect, DT_CENTER | DT_VCENTER, 0xFFFFFFFF); //g_pFont->End(); // End the scene g_pD3DDevice->EndScene(); } and this is the mistakes it give me : 1>.\WinMain.cpp(130) : error C2106: '=' : left operand must be l-value This is the "Font->FaceName = g_strFont;" line. and g_strFont is declared as : CHAR g_strFont[LF_FACESIZE]; but i'm still blocked, I wonder if this is the right forum to post it or I should post it in the beginner forum. thanks for answers :-)
Advertisement
I think (but not positively)that it should be:

Font.FaceName = g_strFont;
Font.Height = 32;

ie., your 1st code used 'Font' as a struct; your 2nd code uses it as a class
http://blog.protonovus.com/
You should be able to just do
Font->FaceName = "Arial";

If not, you can use a regular string (#include <string>):
string fontName = "Arial";
Font->FaceName = fontName.c_str();

I think either way will work.

I don't see what's wrong with your code though, but this removes some extra stuff that might be causing the problems.
EDIT:
swordfish is right
How is your Font declared?
It should be something like
D3DXFONT_DESC Font;

In which case it's just used like a structure like swordfish showed.
Tadd- WarbleWare
The book, the first code, declare Font as a LOGFONT

me, the second code, declare Font as a D3DXFONT_DESC*

and I tryied to put : string fontName = "Arial";
Font->FaceName = fontName.c_str();

with #include <string.h> but it seems that it donesn't exist :-S : \WinMain.cpp(115) : error C2065: 'string' : undeclared identifier
1>.\WinMain.cpp(115) : error C2146: syntax error : missing ';' before identifier 'strFontName'
1>.\WinMain.cpp(115) : error C2065: 'strFontName' : undeclared identifier
1>.\WinMain.cpp(133) : error C2228: left of '.c_str' must have class/struct/union
1> type is ''unknown-type''
Oops sorry, you probably need to say
std::string
Or put
using namespace std;
at the top of the code.

Also, you should use
#include<string>
without the .h
Using .h is the "old" way and often will load different headers than the current C++ standard ones. If you do need the old ones (C ones), they are often renamed as cmath instead of math.h for example.
------------------------------
BUT, I don't think that's your problem. So you can just go back to your old way if you prefer.

As far as your Font variable: If you are using it as a pointer to a D3DXFONT_DESC, then the problem may be that the actual structure doesn't exist anywhere in memory. Are you "new"ing it or do you ever assign Font (the pointer) to point to a real D3DXFONT_DESC structure? The pointer has to point to some real place in memory. By real I mean something you created either through new or by having another line with something like:
D3DXFONT_DESC theFontData;
Font = &theFontData

But you really shouldn't do it that way unless you have to for some other reason. Just make Font the actual structure and send the address in to D3DXCreateFontIndirect with &Font as in the original example.
Tadd- WarbleWare
Thanks

now it's on that line : Font.FaceName = "Arial";

it says that : error C2440: '=' : cannot convert from 'const char [6]' to 'CHAR [32]'
EDIT: see below instead - this still won't work.
Hmm, thought I saw some examples that way, but not sure (probably getting confused with D3DXCreateFont one). Go back to your original way. MSDN says it needs a null terminated TCHAR array (which I think is just a char array, but I'm not clear on all of the special MS type stuff with Unicode).

Your original way will probably work.
strcpy_s(g_strFont, "Arial");
Font.FaceName = g_strFont;

What do you declare g_strFont as?

Hmm, maybe just
Font.FaceName = _T("Arial");
will work. That has something to do with the Unicode conversions, but I try to avoid all that stuff. I would think something like this would work, but not sure.

#include<string>
std::string faceName = "Arial";
Font.FaceName = faceName.c_str();

I can't test things easily now or I'd just try it myself. This may work too:
TCHAR faceName[32];
lstrcpy(faceName, _T("Arial"));
Font.FaceName = faceName;

[Edited by - reana1 on October 2, 2006 6:57:24 PM]
Tadd- WarbleWare
Ok, I got some code I could test it in.
The problem was in the Font.FaceName = ..... lines (which the errors told you, but I didn't understand it till I could get my own code to test).
You'd have to use a strcpy there also, which is why the following works since I just copy directly to the structure member.

D3DXFONT_DESC Font;
strcpy(Font.FaceName, "Arial");
// should need #include <string>, but in my test code
// it worked without it so it must be in windows.h or something

I also saw the following recommended, which has to do with the unicode stuff, but I don't really understand it so can't recommend what's right.
strcpy(Font.FaceName, _T("Arial"));
// needs #include <tchar.h> with the .h here

I couldn't find a strcpy_s on my machine (using VS 2003), so couldn't test that, but I'd think it would work the same as the deprecated version.

This will work too:
std::string strFont = "Arial";
strcpy(Font.FaceName, strFont.c_str());
// needs #include <string>

EDIT:
Oh, and if you do need to use your g_strFont for some reason.
This will work. Not sure if this is how you have g_strFont declared, so I'm guessing based on how I found others using the same variable name online.
char g_strFont[LF_FACESIZE];
strcpy(g_strFont, "Arial");
strcpy(Font.FaceName, g_strFont);
Tadd- WarbleWare
yesssss it worked

just strcpy_s(Font.FaceName, "Arial"); worked (cause it said that strcpy was depreciated, but just strcpy work too no problem)

Thanks so much for your help :-D

This topic is closed to new replies.

Advertisement