Some problems about the book - Game programming all in one

Started by
5 comments, last by Aardvajk 17 years, 9 months ago
Recently, I have read a book: Game programming all in one. In chapter 7, when I have already finished coding the Monster program, I found that the outputed text is not in English. What can I do to solve this problem? The code is here:

void ConLib::OutputString (char * String)
{
 DWORD Written;

 WriteConsole (m_Screen, String, strlen (String), &Written, NULL);
}
Another problem is the SetTitle() function. When I debug the program, my complier, Visual C++, output the message: Error 1 error C2664: 'SetConsoleTitleW' : cannot convert parameter 1 from 'char *' to 'LPCWSTR' How can I solve it too? The code is here:

void ConLib::SetTitle (char * Title)
{
 SetConsoleTitle (Title);
}
PS: the edition of my book is one [Edited by - icqqq on July 7, 2006 3:11:39 AM]
Advertisement
Sounds like UNICODE related issues to me.

Try including <tchar.h> and replacing all the char with TCHAR, and putting all the string constants in _T(), like this

#include <tchar.h>void f(const TCHAR *c){    SetConsoleTitle(c);}void g(){    f(_T("hello"));}


HTH Paul
Thanks!I have already solved the problem of SetTitle().
But the first problem I still can't solve it.
Here is the error message:
error C2664: 'strlen' : cannot convert parameter 1 from 'const TCHAR *' to 'const char *'
Are there any readers of this book can help me?
I believe that under Windows you can use wstrlen instead to solve this problem.

HTH Paul
Visual Studio 2K5 uses unicode by default, unlike earlier versions.
To disable this, enter the properties of your project, open Configuration Properties, General, and set Character Set to Not Set.
Actually, I've just been doing a bit of research on this and APPARENTLY (according to MSDN), if you include <tchar.h> and then use _tcslen(), it will map to the correct *len function depending on whether UNICODE is enabled or not, so your code is then properly portable, regardless of the compiler settings.

BTW, there is apparently no such function as wstrlen. I meant wcslen, but that will only work for UNICODE, not normal characters.

If you do decide to adopt the universal approach with <tchar.h>, you also need to declare all your char as TCHAR and wrap any string literals in _T().

NON-PORTABLE:
const char *s="hello";
size_t sz=strlen(s); // will fail with UNICODE on

PORTABLE:
#include <tchar.h>

const TCHAR *s=_T("hello");
size_t sz=_tcslen(s); // will work whether UNICODE is on or not

Please note that I have not actually tested any of this as never yet been forced to care about all this myself. I believe it is now a pretty prevalent issue and one we shall all be forced to account for until single-byte characters are deceased.

This topic is closed to new replies.

Advertisement