PDA

View Full Version : Visual C++ Sucks


IrishFarmer
12-16-2007, 04:44 PM
Not really, but I'm extremely frustrated right now. Is there anyone who has any experience with VC++ who could help me identify why I would be getting an "unexpected end of file" error "c1004" when I have no hanging brackets, parentheses or needed ";"s?

I just started building a game engine using the windows api, and if you need it here is my code. Though I've gone over it line by line and I can't find a problem with it at all.

--GameEngine.cpp--

//----------------//
//Included Headers//
//----------------//

#include "GameEngine.h"


GameEngine::GameEngine(HINSTANCE hInstance, LPTSTR szWindowClass, LPTSTR szTitle, WORD wIcon,
WORD wSmallIcon, int iWidth, int iHeight)
{
//set the member variables for the game engine
m_pGameEngine = this;
m_hInstance = hInstance;
m_hWindow = NULL;
if(lstrlen(szWindowClass) > 0)
lstrcpy(m_szWindowClass, szWindowClass);
if(lstrlen(szTitle) > 0)
lstrcpy(m_szTitle, szTitle);
m_wIcon = wIcon;
m_wSmallIcon = wSmallIcon;
m_iWidth = iWidth;
m_iHeight = iHeight;
m_iFrameDelay = 50; //20 fps default
m_bSleep = TRUE;
}

GameEngine::~GameEngine()
{
}


BOOL GameEngine::Initialize(int iCmdShow)
{
WNDCLASSEX wndclass;

//create the window class for the main window
wndclass.cbSize = sizeof(wndclass);
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = m_hInstance;
wndclass.hIcon = LoadIcon(m_hInstance, MAKEINTRESOURCE(GetIcon()));
wndclass.hIconSm = LoadIcon(m_hInsatnce, MAKEINTRESOURCE(GetSmallIcon()));
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); // <- default arrow; change later
wndclass.hbrBackground = (BRUSH)(COLOR_WINDOW + 1);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = m_szWindowClass;

//register the window class
if(!RegisterClassEx(&wndclass))
return FALSE;

//calculate the window size and position based upon the game size
int iWindowWidth = m_iWidth + GetSystemMetrics(SM_CXFIXEDFRAME) * 2,
iWindowHeight = m_iHeight + GetSystemMetrics(SM_CYFIXEDFRAME) * 2 +
GetSystemMetrics(SM_CYCAPTION);
if(wndclass.lpszMenuName != NULL)
iWindowHeight += GetSystemMetrics(SM_CYMENU);
int iXWindowPos = (GetSystemMetrics(SM_CXSCREEN) - iWindowWidth) / 2,
iYWindowPos = (GetSystemMetrics(SM_CYSCREEN) - iWindowHeight) / 2;

//create the window
m_hWindow = CreateWindow(m_szWindowClass, m_szTitle, WS_POPUPWINDOW | WS_CAPTION |
WS_MINIMIZEBOX, iXWindowPos, iYWindowPos, iWindowWidth, iWindowHeight, NULL, NULL,
m_hInstance, NULL);
if(!m_hWindow)
return FALSE;

//show and update the window
ShowWindow(m_hWindow, iCmdShow);
UpdateWindow(m_hWindow);

return TRUE;
}

LRESULT GameEngine::HandleEvent(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam)
{
//deliver windows messages to the proper GameEngine member functions
switch(msg)
{
case WM_CREATE:
//set the game window and start the game
SetWindow(hWindow);
GameStart(hWindow);
return 0;

case WM_ACTIVATE:
//activate/deactivate the game and update the sleep status
if(wParam != WA_INACTIVE)
{
GameActivate(hWindow);
SetSleep(FALSE);

}
else
{
GameDeactivate(hWindow);
SetSleep(TRUE);
}
return 0;

case WM_PAINT:
HDC hDC;
PAINTSTRUCT ps;
hDC = BeginPaint(hWindow, &ps);

//paint the view
GamePaint(hDC);

EndPaint(hWindow, &ps);
return 0;

case WM_DESTROY:
//end and exit the app
GameEnd();
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWindow, msg, wParam, lParam);
}


--strategy.cpp--


//--------------//
//Included Files//
//--------------//

#include "strategy.h"

//---------------------//
//Game Engine Functions//
//---------------------//

BOOL GameInitialize(HINSTANCE hInstance)
{
//Create the game engine
_pGame = new GameEngine(hInstance, TEXT("Strategy RPG"), TEXT("Strategy RPG <- Working Title"), IDI_STRATEGY, IDI_STRATEGY_SM);
if(_pGame == NULL)
return FALSE;

//set the frame rate
_pGame->SetFrameRate(60);

return TRUE;
}

void GameStart(HWND hWindow)
{
//seed the random number generator
srand(GetTickCount());
}

void GameEnd()
{
//cleanup memory here
delete _pGame;
}

void GameActivate(HWND hWindow)
{
HDC hDC;
RECT rect;

//draw activation text on the game screen
GetClientRect(hWindow, &rect);
hDC = GetDC(hWindow);
DrawText(hDC, TEXT("Active"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
ReleaseDC(hWindow, hDC);
}

void GameDeactivate(HWND hWindow)
{
HDC hDC;
RECT rect;

//draw deactivation text on the game screen
GetClientRect(hWindow, &rect);
hDC = GetDC(hWindow);
DrawText(hDC, TEXT("Deactivated"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
ReleaseDC(hWindow, hDC);
}

void GamePaint(HDC hDC)
{
}

void GameCycle()
{
HDC hDC;
HWND hWindow = _pGame->GetWindow();

//draw the icon at random positions on the screen for some reason
hDC = GetDC(hWindow);
DrawIcon(hDC, rand() % _pGame->GetWidth(), rand() % _pGame->GetHeight(),
(HICON)(WORD)GetClassLong(hWindow, GCL_HICON));
ReleaseDC(hWindow, hDC);
}

//---------------------//
//Windows Main Function//
//---------------------//

int WINAPI WinMain(HINSTANCE hInsatnce, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
MSG msg;
static int iTickTrigger = 0;
int iTickCount;

if(GameInitialize(hInstance))
{
//initialize the game engine
if(!GameEngine::GetEngine()->Initialize(iCmdShow))
return FALSE;

//enter the main message loop
while (TRUE)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
//process this message
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
//make sure the game engine isn't sleeping
if (!GameEngine::GetEngine()->GetSleep())
{
//check the tick count to see if its time for a new game cycle
iTickCount = GetTickCount();
if(iTickCount > iTickTrigger)
{
iTickTrigger = iTickCount + GameEngine::GetEngine()->GetFrameDelay();
GameCycle();
}
}
}
}
return (int)msg_wParam;
}

//end the game
GameEnd();

return TRUE;
}

LRESULT CALLBACK WndProc(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam)
{
//route all windows message to the game engine
return GameEngine::GetEngine()->HandleEvent(hWindow, msg, wParam, lParam);
}


There's a strategy.rc file which I can't put into here because its not text in VC++.

--GameEngine.h--


//-----------------//
//Game Engine Class//
//-----------------//

#pragma once

#include <windows.h>
#include "Resource.h"

class GameEngine
{
protected:
//member variables
static GameEngine* m_pGameEngine;
HINSTANCE m_hInstance;
HWND m_hWindow;
TCHAR m_szWindowClass[32];
TCHAR m_szTitle[32];
WORD m_wIcon, m_wSmallIcon;
int m_iWidth, m_iHeight;
int m_iFrameDelay;
BOOL m_bSleep;

public:
//constructor(s)/destructor
GameEngine(HINSTANCE hInstance, LPTSTR szWindowClass, LPTSTR szTitle, WORD wIcon,
WORD wSmallIcon, int iWidth = 640, int iHeight = 480);
virtual ~GameEngine();

//General Methods
static GameEngine* GetEngine() { return m_pGameEngine; );
BOOL Initialize(int iCmdShow);
LRESULT HandleEvent(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam);

//accessor methods
HINSTANCE GetInstance() { return m_hInstance; };
HWND GetWindow() { return m_hWindow; };
void SetWindow(HWND hWindow) { m_hWindow = hWindow; };
LPTSTR GetTitle() { return m_szTitle; };
WORD GetIcon() { return m_wIcon; };
WORD GetSmallIcon() { return m_wSmallIcon; };
int GetWidth() { return m_iWidth; };
int GetHeight() { return m_iHeight; };
int GetFrameDelay() { return m_iFrameDelay; };
void SetFrameRate(int iFrameRate) { m_iFrameDelay = 1000 / iFrameRate; };
BOOL GetSleep() { return m_bSleep; };
void SetSleep(BOOL bSleep) { m_bSleep = bSleep; };
};


--Resource.h--


//{{NO_DEPENDENCIES}}
// Microsoft Developer Studio generated include file.
// Used by Strategy.rc
//
#define IDI_STRATEGY 101

// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 103
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1000
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif


--strategy.h--


#pragma once

//-------------//
//Include Files//
//-------------//

#include <windows.h>
#include "Resource.h"
#include "GameEngine.h"

//----------------//
//Global Variables//
//----------------//

GameEngine* _pGame;


I'm pretty much ready to tear my hear out, since I haven't really even done any work yet, and I'd appreciate any help.

I get the error at the end of my GameEngine.cpp and strategy.cpp files. I've gone over their headers and the source files themselves and I can't find jack.

.oisyn
12-16-2007, 05:43 PM
No, your code sucks

static GameEngine* GetEngine() { return m_pGameEngine; );
I see a closing parathesis there instead of a closing brace.

Aside from that, officially you're not allowed to end an in-class function implementation with a semicolon. Also, I think you meant _pGame in strategy.h to be extern, and to define it in strategy.cpp.

IrishFarmer
12-16-2007, 06:50 PM
Yes, well I've been trying to rush the "game engine" of my project (using the piss poor Windows API) just so that I could get to programming the actual game itself. So I was using this book I bought a while ago and copying the code more-or-less verbatim.

After I went through the code line-by-line and rewrote most of it, keeping your advice in mind, I started getting linking errors, and I really didn't feel like taking more time to fix the code. So I finally found the CD that went with the book (thought I lost it), and I'm just going to copy the guy's code straight off of that. Thanks for the smartass help, though. :)

LmT
12-16-2007, 10:19 PM
Plan out your engine mentally and/or plan it out then program what you can. When you get stuck, use books as references. That way you aren't limiting yourself and not using "piss poor" examples.

Jare
12-17-2007, 01:29 AM
So "game engines" are not the only thing you rush... your rush to (wrong) conclusions, like VC sucks, or Win API is piss poor, or people who help you are smartasses...

.oisyn
12-17-2007, 03:11 AM
Well I guess that's something you can expect from an Irish farmer ;)

[P.S. Now I'm being a smartass]