Beginning Game Programming 4th Edition

hello I am trying to self educate using above mentioned book
and in Visual Studio 2013 it does not seem to be compiling below
code even after I set the character set to be multi byte in the
project properties.

#include <windows.h>
#include <iostream>

using namespace std;

const string ProgramTitle = "Hello Windows";

LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
RECT drawRect;
PAINTSTRUCT ps;
HDC hdc;

switch (message) {
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
for (int n = 0; n < 20; n++) {
int x = n * 20;
int y = n * 20;
drawRect = { x. y. x+100, y+20 }
}
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
ATOM MyRegisterClass(HINSTANCE hInstance) {
WNDCLASSEX wc;
wc.cbsize = sizeof(WNDCLASSEX);
wc.style = S_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WinProc;
wc.cbClsExtra = 0;
wc.WndExtra = 0;
wc.hInstacne = hInstance;
wc.hIcon = NULL;
wc.hCuror = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = ProgramTitle.c_srt();
wc.hIconSm = NULL;

bool InitInstance(HINSTANCE hInstance, int nCmdShow) {
HWND hWnd = CreateWindow(ProgramTitle.c_str(), ProgramTitle.c_str().WS_OVERLAPPEDINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, hInstance, NULL);
if (hWnd == 0) return 0;
UpdateWindow(hWnd);
return 1;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
MyRegisterClass(hInstance);
if (!InitInstance(hInstance, nCmdShow)) return 0;

MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}



I have gotten a small window to compile which was basically a
Hello World but only after I put in <include stdafx.h> which was
mentioned in Visual Studio but not in the book as far as I can tell.

If anyone can help me get things compiling I would be very grateful.
Last edited on
wc.lpszClassName = ProgramTitle.c_srt(); must be
wc.lpszClassName = ProgramTitle.c_str();

wc.hCuror = LoadCursor(NULL, IDC_ARROW); must be
wc.hCursor = LoadCursor(NULL, IDC_ARROW);

HWND hWnd = CreateWindow(ProgramTitle.c_str(), ProgramTitle.c_str().WS_OVERLAPPEDINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, hInstance, NULL); must be

HWND hWnd = CreateWindow(ProgramTitle.c_str(), ProgramTitle.c_str(), WS_OVERLAPPEDINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, hInstance, NULL);

Are there any more error messages ?
OK I seem to have fixed the code so there's no more typos but Visual Studio is still giving a hard time. T.T



the error messages that are being returned are:

1) a ; is expected instead of { after the bool statement in below code

2) class tagWNDCLASSEXa has no member WndExtra or cbSize

3) hInstance local function definitions are illegal while used inside the bool statement

4) WinMain local function definitions are illegal

5) InitInstance local function definitions are illegal

I am at a loss T.T T.T T.T I really want to move onto the next chapter myself T.T





#include <windows.h>
#include <iostream>

using namespace std;

const string ProgramTitle = "Hello Windows";

LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
RECT drawRect;
PAINTSTRUCT ps;
HDC hdc;

switch (message) {
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
for (int n = 0; n < 20; n++) {
int x = n * 20;
int y = n * 20;
drawRect = { x, y, x + 100, y + 20 };
DrawText(hdc, ProgramTitle.c_str(), ProgramTitle.length(), &drawRect, DT_CENTER);
}
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
ATOM MyRegisterClass(HINSTANCE hInstance) {
WNDCLASSEX wc;
wc.cbsize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WinProc;
wc.cbClsExtra = 0;
wc.WndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = ProgramTitle.c_str();
wc.hIconSm = NULL;
return RegisterClassEx(&wc);
bool InitInstance(HINSTANCE hInstance, int nCmdShow) {
HWND hWnd = CreateWindow(ProgramTitle.c_str(), ProgramTitle.c_str(), WS_OVERLAPPEDINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, hInstance, NULL);


if (hWnd == 0) return 0;

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return 1;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
MyRegisterClass(hInstance);
if (!InitInstance(hInstance, nCmdShow)) return 0;

MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
Last edited on
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#if defined (UNICODE) || defined(_UNICODE)
#error Unicode not supported - use Multi-byte character set instead
#endif

#include <windows.h>
#include <iostream>

using namespace std;

const string ProgramTitle = "Hello Windows";

LRESULT CALLBACK WinProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 
{
  RECT drawRect;
  PAINTSTRUCT ps;
  HDC hdc;

  switch (message)
  {
    case WM_PAINT:
    {
      hdc = BeginPaint (hWnd, &ps);
      for (int n = 0; n < 20; n++)
      {
        int x = n * 20;
        int y = n * 20;
        drawRect = { x, y, x + 100, y + 20 };
        DrawText (hdc, ProgramTitle.c_str (), ProgramTitle.length (), &drawRect, DT_CENTER);
      }
      EndPaint (hWnd, &ps);
    }
    break;
  case WM_DESTROY:
    PostQuitMessage (0);
    break;
  }
  return DefWindowProc (hWnd, message, wParam, lParam);
}
ATOM MyRegisterClass (HINSTANCE hInstance)
{
  WNDCLASSEX wc;
  wc.cbSize = sizeof (WNDCLASSEX);
  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 = ProgramTitle.c_str ();
  wc.hIconSm = NULL;

  return RegisterClassEx (&wc);
}

bool InitInstance (HINSTANCE hInstance, int nCmdShow)
{
  HWND hWnd = CreateWindow (ProgramTitle.c_str (), ProgramTitle.c_str (), 
 WS_OVERLAPPED, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, 
NULL, NULL, hInstance, NULL);


  if (hWnd == 0) 
    return 0;

  ShowWindow (hWnd, nCmdShow);
  UpdateWindow (hWnd);

  return true;
}

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 
{
  MyRegisterClass (hInstance);
  if (!InitInstance (hInstance, nCmdShow)) return 0;

  MSG msg;
  while (GetMessage (&msg, NULL, 0, 0))
  {
    TranslateMessage (&msg);
    DispatchMessage (&msg);
  }
  return msg.wParam;
}


You can close the window with alt + F4
it works thank you for tolerating me being such a noob but I got the exe I was supposed to get. XD
You are very welcome. Unfortunately books and their source have quite often typos. If you have nay more problems feel free to ask.
naa the book seems fine so far there was just stuff I wasn't understanding that
was key but too small for the book to clarify.
Topic archived. No new replies allowed.