Jump to content
  • Advertisement
Sign in to follow this  
PaladinJohn

Visual C++ 2005 to 2008 Woes

This topic is 3439 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone, I am currently in the process of learning how to program in C++ and DirectX, so I picked up the book "Beginning Game Programming 2nd ed." However, I've run into a roadblock that I was hoping someone could help me with. Most of the code within the book was developed using Visual C++ 2005, but I downloaded 2008 since not only was 2005 no longer available on Microsoft's site, but I figured I might as well begin to get used to some of the newer development software. Unfortunately, the code does not work from 2005 to 2008. For the first few projects I had a work around by using different compilers, but for the one compiler that I found that did work, I don't believe there is any DirectX support for it. I don't want to further complicate the learning process by learning new code to get the old code to work, leaving me with two options: 1) Find some sort of update for 2008 that will allow my 2005 code to work or 2) Snoop around the net for Visual C++ 2005 somewhere. For reference, I will post the first simple "Hello World" program from the book as well as the compile errors it generates:
// Beginning Game Programming
// Chapter 3
// WindowTest program

//header files to include
#include <windows.h>
#include <stdlib.h>
#include <time.h>

//application title
#define APPTITLE "Hello World"

//function prototypes (forward declarations)
BOOL InitInstance(HINSTANCE,int);
ATOM MyRegisterClass(HINSTANCE);
LRESULT CALLBACK WinProc(HWND,UINT,WPARAM,LPARAM);


//the window event callback function
LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;
	HDC hdc;
	char *szHello = "Hello World!";
	RECT rt;
	int x, y, n;
	COLORREF c;

	switch (message)
	{
		case WM_PAINT:
			//get the dimensions of the window
            GetClientRect(hWnd, &rt);

            //start drawing on device context
            hdc = BeginPaint(hWnd, &ps);

			//draw some text
			DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);

			//draw 1000 random pixels
			for (n=0; n<3000; n++)
			{
				x = rand() % (rt.right - rt.left);
				y = rand() % (rt.bottom - rt.top);
				c = RGB(rand()%256, rand()%256, rand()%256);
				SetPixel(hdc, x, y, c);
			}

            //stop drawing
			EndPaint(hWnd, &ps);
			break;

		case WM_DESTROY:
			PostQuitMessage(0);
			break;
    }
    return DefWindowProc(hWnd, message, 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);
}

//helper function to create the window and refresh it
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   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);

   return TRUE;
}


//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
	if (!InitInstance (hInstance, nCmdShow)) 
		return FALSE;

	//set random number seed
	srand(time(NULL));

    // main message loop
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return msg.wParam;
}
1>------ Build started: Project: WindowTest, Configuration: Debug Win32 ------
1>Compiling...
1>main.cpp
1>c:\documents and settings\owner\my documents\visual studio 2008\projects\windowtest\windowtest\main.cpp(39) : error C2664: 'DrawTextW' : cannot convert parameter 2 from 'char *' to 'LPCWSTR'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\documents and settings\owner\my documents\visual studio 2008\projects\windowtest\windowtest\main.cpp(78) : error C2440: '=' : cannot convert from 'const char [12]' to 'LPCWSTR'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\documents and settings\owner\my documents\visual studio 2008\projects\windowtest\windowtest\main.cpp(102) : error C2664: 'CreateWindowExW' : cannot convert parameter 2 from 'const char [12]' to 'LPCWSTR'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\documents and settings\owner\my documents\visual studio 2008\projects\windowtest\windowtest\main.cpp(133) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>Build log was saved at "file://c:\Documents and Settings\Owner\My Documents\Visual Studio 2008\Projects\WindowTest\WindowTest\Debug\BuildLog.htm"
1>WindowTest - 3 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Thank you for your time, and sorry for the very noobish question. [Edited by - PaladinJohn on May 16, 2009 5:13:06 PM]

Share this post


Link to post
Share on other sites
Advertisement
Quotes are nice, but source tags are better [smile]

Having said that, it looks like you're trying to write code that uses the Multi-Byte Character Set, whereas your project is set to compile with the Unicode Character Set. Basically this means that the compiler is interpreting "Hello World!" as a wchar_t*, but in your code you're trying to interpret it as a char*. Either start using wide characters in your code or change your project to compile with the multi-byte character set by going to Project->Properties->General->Character Set and changing it from "Use Unicode Character Set" to "Use Multi-Byte Character Set".

Share this post


Link to post
Share on other sites
Thanks for the tips! I edited my original post to include source tags rather than quote.

The program has successfully compiled.

EDIT: Found out that I need to debug in Visual C++ in order to get the .exe to even appear. Sorry about all of the confusion, I'm used to Dev-C++ so toying around with Visual C++ was a learning experience in and of itself.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!