473,385 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
2 4602
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
9,208 Expert Mod 8TB
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

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

Similar topics

242
by: James Cameron | last post by:
Hi I'm developing a program and the client is worried about future reuse of the code. Say 5, 10, 15 years down the road. This will be a major factor in selecting the development language. Any...
0
by: TechBookReport | last post by:
The following is an extract of a review of the book 'Programming C#' by Jesse Libety and published by O'Reilly. The review is from TechBookReport (http://www.techbookreport.com): Jesse Liberty's...
35
by: Markus Dreyer | last post by:
I suggested our Librarian for the University Library to buy Accelerated C++. Practical Programming by Example. by Andrew Koenig and Barbara E. Moo http://www.acceleratedcpp.com/ but she...
23
by: Carter Smith | last post by:
http://www.icarusindie.com/Literature/ebooks/ Rather than advocating wasting money on expensive books for beginners, here's my collection of ebooks that have been made freely available on-line...
4
by: Jesse Liberty | last post by:
-- This is a one time announcement. I apologize in advance if it is an inconvenience -- I am proud to announce that my best selling book Programming C# is now in its 4th Edition, fully updated...
19
by: Swaregirl | last post by:
Hello, I would like to build a website using ASP.NET. I would like website visitors to be able to download code that I would like to make available to them and that would be residing on my...
43
by: ZillionDollarSadist | last post by:
Hello, I'm working at a simple Access '97 + VB4 application, and I ran into a terrible problem: something I never modified now gives me a totally unwanted "Invalid use of null" error. It happens...
7
by: Richard Phillips | last post by:
Hello, Does anyone else out there have a copy of this? I'm working through my copy and have discovered that it has no chapter 11... Pages 341-388 are just not there! Anyone else able to check...
11
by: John Coleman | last post by:
Greetings, My copy of the second edition of Chun's "Core Python Programming" just arrived from Amazon on Friday. What really jumped out at me is an interesting feature about how it sequences its...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.