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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Netwatcher
    New Member
    • Sep 2008
    • 58

    #1

    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

    Code:
    // Beginning Game Programming
    // Chapter 5  #edit: not 4, mistake in the title 
    // d3d_windowed program
    
    //header files to include
    #include <d3d9.h>
    #include <time.h>
    
    //application title
    #define APPTITLE "Direct3D_Windowed"
    
    //forward declarations
    LRESULT WINAPI WinProc(HWND,UINT,WPARAM,LPARAM);
    ATOM MyRegisterClass(HINSTANCE);
    int Game_Init(HWND);
    void Game_Run(HWND);
    void Game_End(HWND);
    
    //Direct3D objects
    LPDIRECT3D9 d3d = NULL; 
    LPDIRECT3DDEVICE9 d3ddev = NULL; 
    
    
    //window event callback function
    LRESULT WINAPI WinProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
    {
        switch( msg )
        {
            case WM_DESTROY:
                Game_End(hWnd);
                PostQuitMessage(0);
                return 0;
        }
    
        return DefWindowProc( hWnd, msg, wParam, lParam );
    }
    
    
    //helper function to set up the window properties
    ATOM MyRegisterClass(HINSTANCE hInstance)
    {
        //create the window class structure
        WNDCLASSEX wc;
        wc.cbSize = sizeof(WNDCLASSEX); 
    
        //fill the struct with info
        wc.style         = CS_HREDRAW | CS_VREDRAW;
        wc.lpfnWndProc   = (WNDPROC)WinProc;
        wc.cbClsExtra	 = 0;
        wc.cbWndExtra	 = 0;
        wc.hInstance     = hInstance;
        wc.hIcon         = NULL;
        wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
        wc.lpszMenuName  = NULL;
        wc.lpszClassName = APPTITLE;
        wc.hIconSm       = NULL;
    
        //set up the window with the class info
        return RegisterClassEx(&wc);
    }
    
    
    //entry point for a Windows program
    int WINAPI WinMain(HINSTANCE hInstance,
                       HINSTANCE hPrevInstance,
                       LPSTR     lpCmdLine,
                       int       nCmdShow)
    {
        // declare variables
    	MSG msg;
    
    	// register the class
    	MyRegisterClass(hInstance);
    
        // initialize application 
        //note--got rid of initinstance
        HWND hWnd;
    
        //create a new window
        hWnd = CreateWindow(
           APPTITLE,              //window class
           APPTITLE,              //title bar
           WS_OVERLAPPEDWINDOW,   //window style
           CW_USEDEFAULT,         //x position of window
           CW_USEDEFAULT,         //y position of window
           500,                   //width of the window
           400,                   //height of the window
           NULL,                  //parent window
           NULL,                  //menu
           hInstance,             //application instance
           NULL);                 //window parameters
    
        //was there an error creating the window?
        if (!hWnd)
          return FALSE;
    
        //display the window
        ShowWindow(hWnd, nCmdShow);
        UpdateWindow(hWnd);
    	
    	//initialize the game
        if (!Game_Init(hWnd))
            return 0;
    
    
        // main message loop
        int done = 0;
    	while (!done)
        {
            if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 
    	    {
                //look for quit message
                if (msg.message == WM_QUIT)
                {
                    MessageBox(hWnd, "Received WM_QUIT message", "WinMain", MB_OK);
                    done = 1;
                }
    
                //decode and pass messages on to WndProc
    		    TranslateMessage(&msg);
    		    DispatchMessage(&msg);
    	    }
            else
                //process game loop (else prevents running after window is closed)
                Game_Run(hWnd);
        }
    
    	return msg.wParam;
    }
    
    
    int Game_Init(HWND hwnd)
    {
        //display init message
        MessageBox(hwnd, "Program is about to start", "Game_Init", MB_OK);
    
        //initialize Direct3D
        d3d = Direct3DCreate9(D3D_SDK_VERSION);
        if (d3d == NULL)
        {
            MessageBox(hwnd, "Error initializing Direct3D", "Error", MB_OK);
            return 0;
        }
    
        //set Direct3D presentation parameters
        D3DPRESENT_PARAMETERS d3dpp; 
        ZeroMemory(&d3dpp, sizeof(d3dpp));
        d3dpp.Windowed = TRUE;
        d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
        d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
    
        //create Direct3D device
        d3d->CreateDevice(
            D3DADAPTER_DEFAULT, 
            D3DDEVTYPE_HAL, 
            hwnd,
            D3DCREATE_SOFTWARE_VERTEXPROCESSING,
            &d3dpp, 
            &d3ddev);
    
        if (d3ddev == NULL)
        {
            MessageBox(hwnd, "Error creating Direct3D device", "Error", MB_OK);
            return 0;
        }
    
    	//set random number seed
    	srand(time(NULL));
    
        //return okay
        return 1;
    }
    
    void Game_Run(HWND hwnd)
    {
        //make sure the Direct3D device is valid
        if (d3ddev == NULL)
            return;
    
        //clear the backbuffer to black
        d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,255,0), 1.0f, 0);
        
        //start rendering
        if (d3ddev->BeginScene())
        {
            
        
            //stop rendering
            d3ddev->EndScene();
        }
    
        //display the back buffer on the screen
        d3ddev->Present(NULL, NULL, NULL, NULL);
    
    }
    
    void Game_End(HWND hwnd)
    {
        //display close message
        MessageBox(hwnd, "Program is about to end", "Game_End", MB_OK);
    
        //release the Direct3D device
        if (d3ddev != NULL) 
            d3ddev->Release();
    
        //release the Direct3D object
        if (d3d != NULL)
            d3d->Release();
    }
    and i get the following output

    Code:
    ------- Build started: Project: GameLoop, Configuration: Debug Win32 ------
    Compiling...
    winmain.cpp
    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'
            Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    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'
            Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    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'
            Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    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'
            Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    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'
            Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    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'
            Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    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
    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'
            Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    Build log was saved at "file://c:\Documents and Settings\###\My Documents\Visual Studio 2008\Projects\GameLoop\GameLoop\Debug\BuildLog.htm"
    GameLoop - 7 error(s), 1 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    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?
  • Netwatcher
    New Member
    • Sep 2008
    • 58

    #2
    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

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      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:
      Code:
      MessageBox(hwnd, TEXT("Program is about to end"), TEXT("Game_End"), MB_OK);
      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.

      Comment

      Working...