By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,190 Members | 1,503 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,190 IT Pros & Developers. It's quick & easy.

Outdated Code? {Beginning game programming, second edition chapter 4}

P: 58
Hello, i am new to c++ windows and DX programming, i encountered a code in the book stated in the title, which doesn't work for a reason, here is the code

Expand|Select|Wrap|Line Numbers
  1. // Beginning Game Programming
  2. // Chapter 5  #edit: not 4, mistake in the title 
  3. // d3d_windowed program
  4.  
  5. //header files to include
  6. #include <d3d9.h>
  7. #include <time.h>
  8.  
  9. //application title
  10. #define APPTITLE "Direct3D_Windowed"
  11.  
  12. //forward declarations
  13. LRESULT WINAPI WinProc(HWND,UINT,WPARAM,LPARAM);
  14. ATOM MyRegisterClass(HINSTANCE);
  15. int Game_Init(HWND);
  16. void Game_Run(HWND);
  17. void Game_End(HWND);
  18.  
  19. //Direct3D objects
  20. LPDIRECT3D9 d3d = NULL; 
  21. LPDIRECT3DDEVICE9 d3ddev = NULL; 
  22.  
  23.  
  24. //window event callback function
  25. LRESULT WINAPI WinProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
  26. {
  27.     switch( msg )
  28.     {
  29.         case WM_DESTROY:
  30.             Game_End(hWnd);
  31.             PostQuitMessage(0);
  32.             return 0;
  33.     }
  34.  
  35.     return DefWindowProc( hWnd, msg, wParam, lParam );
  36. }
  37.  
  38.  
  39. //helper function to set up the window properties
  40. ATOM MyRegisterClass(HINSTANCE hInstance)
  41. {
  42.     //create the window class structure
  43.     WNDCLASSEX wc;
  44.     wc.cbSize = sizeof(WNDCLASSEX); 
  45.  
  46.     //fill the struct with info
  47.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  48.     wc.lpfnWndProc   = (WNDPROC)WinProc;
  49.     wc.cbClsExtra     = 0;
  50.     wc.cbWndExtra     = 0;
  51.     wc.hInstance     = hInstance;
  52.     wc.hIcon         = NULL;
  53.     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  54.     wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  55.     wc.lpszMenuName  = NULL;
  56.     wc.lpszClassName = APPTITLE;
  57.     wc.hIconSm       = NULL;
  58.  
  59.     //set up the window with the class info
  60.     return RegisterClassEx(&wc);
  61. }
  62.  
  63.  
  64. //entry point for a Windows program
  65. int WINAPI WinMain(HINSTANCE hInstance,
  66.                    HINSTANCE hPrevInstance,
  67.                    LPSTR     lpCmdLine,
  68.                    int       nCmdShow)
  69. {
  70.     // declare variables
  71.     MSG msg;
  72.  
  73.     // register the class
  74.     MyRegisterClass(hInstance);
  75.  
  76.     // initialize application 
  77.     //note--got rid of initinstance
  78.     HWND hWnd;
  79.  
  80.     //create a new window
  81.     hWnd = CreateWindow(
  82.        APPTITLE,              //window class
  83.        APPTITLE,              //title bar
  84.        WS_OVERLAPPEDWINDOW,   //window style
  85.        CW_USEDEFAULT,         //x position of window
  86.        CW_USEDEFAULT,         //y position of window
  87.        500,                   //width of the window
  88.        400,                   //height of the window
  89.        NULL,                  //parent window
  90.        NULL,                  //menu
  91.        hInstance,             //application instance
  92.        NULL);                 //window parameters
  93.  
  94.     //was there an error creating the window?
  95.     if (!hWnd)
  96.       return FALSE;
  97.  
  98.     //display the window
  99.     ShowWindow(hWnd, nCmdShow);
  100.     UpdateWindow(hWnd);
  101.  
  102.     //initialize the game
  103.     if (!Game_Init(hWnd))
  104.         return 0;
  105.  
  106.  
  107.     // main message loop
  108.     int done = 0;
  109.     while (!done)
  110.     {
  111.         if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 
  112.         {
  113.             //look for quit message
  114.             if (msg.message == WM_QUIT)
  115.             {
  116.                 MessageBox(hWnd, "Received WM_QUIT message", "WinMain", MB_OK);
  117.                 done = 1;
  118.             }
  119.  
  120.             //decode and pass messages on to WndProc
  121.             TranslateMessage(&msg);
  122.             DispatchMessage(&msg);
  123.         }
  124.         else
  125.             //process game loop (else prevents running after window is closed)
  126.             Game_Run(hWnd);
  127.     }
  128.  
  129.     return msg.wParam;
  130. }
  131.  
  132.  
  133. int Game_Init(HWND hwnd)
  134. {
  135.     //display init message
  136.     MessageBox(hwnd, "Program is about to start", "Game_Init", MB_OK);
  137.  
  138.     //initialize Direct3D
  139.     d3d = Direct3DCreate9(D3D_SDK_VERSION);
  140.     if (d3d == NULL)
  141.     {
  142.         MessageBox(hwnd, "Error initializing Direct3D", "Error", MB_OK);
  143.         return 0;
  144.     }
  145.  
  146.     //set Direct3D presentation parameters
  147.     D3DPRESENT_PARAMETERS d3dpp; 
  148.     ZeroMemory(&d3dpp, sizeof(d3dpp));
  149.     d3dpp.Windowed = TRUE;
  150.     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  151.     d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
  152.  
  153.     //create Direct3D device
  154.     d3d->CreateDevice(
  155.         D3DADAPTER_DEFAULT, 
  156.         D3DDEVTYPE_HAL, 
  157.         hwnd,
  158.         D3DCREATE_SOFTWARE_VERTEXPROCESSING,
  159.         &d3dpp, 
  160.         &d3ddev);
  161.  
  162.     if (d3ddev == NULL)
  163.     {
  164.         MessageBox(hwnd, "Error creating Direct3D device", "Error", MB_OK);
  165.         return 0;
  166.     }
  167.  
  168.     //set random number seed
  169.     srand(time(NULL));
  170.  
  171.     //return okay
  172.     return 1;
  173. }
  174.  
  175. void Game_Run(HWND hwnd)
  176. {
  177.     //make sure the Direct3D device is valid
  178.     if (d3ddev == NULL)
  179.         return;
  180.  
  181.     //clear the backbuffer to black
  182.     d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,255,0), 1.0f, 0);
  183.  
  184.     //start rendering
  185.     if (d3ddev->BeginScene())
  186.     {
  187.  
  188.  
  189.         //stop rendering
  190.         d3ddev->EndScene();
  191.     }
  192.  
  193.     //display the back buffer on the screen
  194.     d3ddev->Present(NULL, NULL, NULL, NULL);
  195.  
  196. }
  197.  
  198. void Game_End(HWND hwnd)
  199. {
  200.     //display close message
  201.     MessageBox(hwnd, "Program is about to end", "Game_End", MB_OK);
  202.  
  203.     //release the Direct3D device
  204.     if (d3ddev != NULL) 
  205.         d3ddev->Release();
  206.  
  207.     //release the Direct3D object
  208.     if (d3d != NULL)
  209.         d3d->Release();
  210. }
  211.  
and i get the following output

Expand|Select|Wrap|Line Numbers
  1. ------- Build started: Project: GameLoop, Configuration: Debug Win32 ------
  2. Compiling...
  3. winmain.cpp
  4. c:\documents and settings\###\my documents\visual studio 2008\projects\gameloop\gameloop\winmain.cpp(56) : error C2440: '=' : cannot convert from 'const char [18]' to 'LPCWSTR'
  5.         Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
  6. c:\documents and settings\###\my documents\visual studio 2008\projects\gameloop\gameloop\winmain.cpp(92) : error C2664: 'CreateWindowExW' : cannot convert parameter 2 from 'const char [18]' to 'LPCWSTR'
  7.         Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
  8. c:\documents and settings\###\my documents\visual studio 2008\projects\gameloop\gameloop\winmain.cpp(116) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [25]' to 'LPCWSTR'
  9.         Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
  10. c:\documents and settings\###\my documents\visual studio 2008\projects\gameloop\gameloop\winmain.cpp(136) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [26]' to 'LPCWSTR'
  11.         Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
  12. c:\documents and settings\###\my documents\visual studio 2008\projects\gameloop\gameloop\winmain.cpp(142) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [28]' to 'LPCWSTR'
  13.         Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
  14. c:\documents and settings\###\my documents\visual studio 2008\projects\gameloop\gameloop\winmain.cpp(164) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [31]' to 'LPCWSTR'
  15.         Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
  16. c:\documents and settings\###\my documents\visual studio 2008\projects\gameloop\gameloop\winmain.cpp(169) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
  17. c:\documents and settings\###\my documents\visual studio 2008\projects\gameloop\gameloop\winmain.cpp(201) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [24]' to 'LPCWSTR'
  18.         Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
  19. Build log was saved at "file://c:\Documents and Settings\###\My Documents\Visual Studio 2008\Projects\GameLoop\GameLoop\Debug\BuildLog.htm"
  20. GameLoop - 7 error(s), 1 warning(s)
  21. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
  22.  
  23.  

The version of Visual C++ used in the book is 2005 express and i'm using 2008
The book states it's a C code.
Any Suggestions?
Jan 10 '09 #1
Share this Question
Share on Google+
2 Replies


P: 58
Ok, nvm i got this, unicode problem, but yet i don't really understand the problem, is unicode set to on or off and how do i change it instead of just writing L in front of quotes("") everywhere
Jan 10 '09 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
You set the project character set property on your Project Properties/Configuration Properties/General dialog.

This means you have to use the TCHAR conversions in your code.
Like here. MessageBox is a macro. It resolves to either MessageBoxA (ASCII)
or MessageBoxW (Unicode). The resolution comes from that character set property
in your project:
Expand|Select|Wrap|Line Numbers
  1. MessageBox(hwnd, TEXT("Program is about to end"), TEXT("Game_End"), MB_OK); 
  2.  
TEXT is the TCHAR conversion macro that creates either an ASCII string (LPSTR)
or a Uniciode string (LPWSTR). Things like LPSTR and LPWSTR are not to appear
in your code. All that shoul be there is the TCHAR equivalent.
Research the TCHAR conversions in MSDN. There are a lot of topics on this subject.
Jan 10 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.