Hi, this time I've borrowed a book called "Windows Game Programming for Dummies", albeit old it is still useful!
However, I came across a problem writing a simple code the author made, namely the TextOut()
The error it gives me at build time is as follows:
1>c:\users\shadow\documents\visual studio 2008\projects\g_app\g_app\main.cpp(75) : error C2664: 'TextOutW' : cannot convert parameter 4 from 'char [80]' to 'LPCWSTR'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
And here is a small snippet of the code:
As you can see, it is a win32 application callback func... xD
LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
PAINTSTRUCT ps;
HDC hdc;
char buffer[80];
static int counter = 0;
switch( message )
{
case WM_PAINT:
{
hdc = BeginPaint( hWnd, &ps );
int length = sprintf( buffer, "The Count is %d ", (int)counter++);
TextOut(hdc, 0, 0, buffer, length);
EndPaint( hWnd, &ps );
}
break;
case WM_MOUSEMOVE:
break;
case WM_DESTROY:
PostQuitMessage( 0 );
break;
default:
return DefWindowProc( hWnd, message, wParam, lParam );
}
return 0;
}
I also know that you should use static_cast<> instead of the old one, but it was just an example.(Oddly enough (LPCWSTR)buffer works, but all it does is show some gibberish symbols, like chinese) The question is obvious:
Why doesn't it work?
I have searched the net as I always do in these situations, and no site came close to answering my question (at least none that I could find)...
Remember that I am quite the novice on the subject so keep it in layman's terms. Now, let me also ask another question, possibly regarding the same subject.
Earlier in the book when the author explained the MessageBox() func, this couldnt compile:
MessageBox( NULL, "Blabla", "Meh", MB_OK);
And, after searching the net, I found that this works:
MessageBox( NULL, L"Blabla", L"Meh", MB_OK);
Why is this? does it have to do with the current problem?
Solution needs to be understandable, and explained!
Help! :icon_sad:
Full code for testing:
#include <windows.h>
#include <string>
#include <stdlib.h>
HINSTANCE g_hInst = NULL;
HWND g_hWnd = NULL;
HRESULT InitWindow( HINSTANCE hInstance, int nCmdShow );
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow )
{
if( FAILED( InitWindow( hInstance, nCmdShow ) ) )
return 0;
// Main message loop
MSG msg = {0};
while( GetMessage( &msg, NULL, 0, 0 ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return ( int )msg.wParam;
}
HRESULT InitWindow( HINSTANCE hInstance, int nCmdShow )
{
// Register class
WNDCLASSEX wcex;
wcex.cbSize = sizeof( WNDCLASSEX );
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon( hInstance, IDI_APPLICATION );
wcex.hCursor = LoadCursor( NULL, IDC_ARROW );
wcex.hbrBackground = ( HBRUSH )( COLOR_WINDOW + 1 );//(HBRUSH)8;
wcex.lpszMenuName = NULL;
wcex.lpszClassName = L"RPG";
wcex.hIconSm = LoadIcon( wcex.hInstance, IDI_APPLICATION);
if( !RegisterClassEx( &wcex ) )
return E_FAIL;
// Create window
g_hInst = hInstance;
RECT rc = { 0, 0, 1024, 768 };
AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, FALSE );
g_hWnd = CreateWindow( L"RPG", L"RPG Infinitus", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, hInstance,
NULL );
if( !g_hWnd )
return E_FAIL;
ShowWindow( g_hWnd, nCmdShow );
return S_OK;
}
LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
PAINTSTRUCT ps;
HDC hdc;
char buffer[80];
static int counter = 0;
switch( message )
{
case WM_PAINT:
{
hdc = BeginPaint( hWnd, &ps );
int length = sprintf( buffer, "The Count is %d ", (int)counter++);
TextOut(hdc, 0, 0, buffer, length);
EndPaint( hWnd, &ps );
}
break;
case WM_MOUSEMOVE:
break;
case WM_DESTROY:
PostQuitMessage( 0 );
break;
default:
return DefWindowProc( hWnd, message, wParam, lParam );
}
return 0;
}