Skip to main content
gamedev.net
🔒 Locked

C++ windows programming

Started by namingway Sep 25, 2005 at 7:04 AM 5 replies 950+ views
Original Post
namingway
namingway
I have an annoying problem. I am currently learning C++ programming with the windows coding from 2 different books. My problem is when I copy out the code it never compiles and even the code on the cd doesnt compile. I have tried compiling the code on Dev C++ and visual C++ and neither seem to work if anyone has some suggestions or you'd like me to show the code please help :(
ZQJ
ZQJ
Perhaps you could post a short example of the offending code?
Vopisk
Vopisk
Never copy the code. That is my advice. You don't learn anything from borrowing out from the authors of the books. Use their examples and change them to suit your needs, or perhaps expand a little bit on the larger idea using your newfound knowledge. That being said, if you're working with things like header files and random declarations outside the scope of a main() function, generally the code will not compile(I know that it won't in Dev-C++). This is because you can't make an .exe out of something that has no runtime process, so the file you're left with is instead more of a support file than an actual "source code" in its entirety.

However, the problem could also be that any files you are #include-ing are not properly located so that the compiler can find them, causing it to throw a compile-error when it tries to link or or whatever. So maybe check those aspects out.

Other than that, you'd have to give some examples of what's not compiling and what errors are being thrown at you to give any sort of decent idea as to what exactly is happening.
namingway
namingway
Header file
//Header file

#pragma once

#include

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow);
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

BOOL GameInitialize(HINSTANCE hInstance);
void GameStart(HWND hWindow);
void GameEnd();
void GameActive(HWND hWindow);
void GameDeactive(HWND hWindow);
void GamePaint(HDC hDC);
void GameCycle();

//Game Engine Class
class GameEngine
{
protected:
//variables
static GameEngine* m_pGameEngine; // m_ is used to define member variables
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:
//Constructors, destructors
GameEngine(HINSTANCE hInstance, LPTSTR szWindowClass, LPTSTR szTitle,
WORD wIcon, WORD wSmallIcon, int iwidth = 640, int i Height = 480); //This sets the screen size
virtual ~GameEngine();

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

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_SmallIcon; };
int GetWidth() {return m_iWidth; };
int GetHeight() {return m_iHeight; };
int GetFrameDelay() {return m_iFrameDelay; };
void SetFrameRate(int iFrameRate) {m_iFrameRate = 1000 / iFrameRate; };
BOOL GetSleep() {return m_bsleep; };
void SetSleep(BOOL bSleep) {m_bSleep = bSleep; };
};


C++ source code

#include "GameEngine.h"

GameEngine *GameEngine::m_pGameEngine = NULL;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
MSG msg;
static int iTickTrigger =0;
int iTickCount;
if (GameInitialize(hInstance))
{
//Initialize Game Engine
if GameEngine::GetEngine()->Initialize(iCmdShow))
return FALSE;

while (TRUE)
{
if(PeekMessage(&msg, NULL,0 ,0 PM_REMOVE))
{
if(msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
//Check if engine is sleeping
if (GameEngine::GetEngine()->())
{
iTickCount = GetTickCount():
if (iTickCount > iTickTrigger)
{
iTickTrigger = iTickCount + GameEngine::GetEngine()->GetFrameDelay();
GameCycle();
}
}
}
}
return (int) msg.wParam;
}
//end game engine
GameEnd();

return TRUE;
}

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


// GameEngine Constructor(s)/Destructor

GameEngine::GameEngine(HINSTANCE hInstance, LPTSTR szWindowClass,
LPTSTR szTitle, WORD wIcon, WORD wSmallIcon, int iWidth, int iHeight)
{
//Game engine member variables
m_pGameEngine = this;
m_hInstance = hInstance;
m_hWindow = NULL;
if (lstrlen(szWindowClass) > 0)
lstrcpy(m_sz_WindowClass, 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
m_bsleep = TRUE;
}

GameEngine::-GameEngine()
{
}
//This will set the games size
BOOL GameEngine::Initialize(int iCmdShow)
{
WNDCLASSEX wndclass;
//window class
wndclass.cbsize = size(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_hInstance, MAKEINTRESOURCE(GetSmallIcon()));
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = m_szWindowClass;

if (!RegisterClassEx(&wndclass))
return FALSE;

//Window size and position
int iWindowWidth = m_iWidth + GetSystemMetrics(SM_CXFIXEDFRAME) *2,
iWindowHeight = m_iHeight + GetSystemMetrics(SM_CXFIXEDFRAME)*2 +
GetSystemMetrics(SM_CYCAPTION);
if(wndclass.lpszMenuName !=NULL)
iWindowHeight += GetSystemMetrics(SM_CYMENU);
int iXWindowPos = (GetSystemMetrics(SM_CXMENU) - iWindowWidth) / 2,
iYWindowPos = (GetSystemMetrics(SM_CYMENU) - iWindowHeight) / 2;

//create 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/Update window
ShowWindow(m_hWindow, iCmdShow);
UpdateWindow(m_hInstance);

return TRUE;
}

LRESULT GameEngine::HandleEvent(HWMD hWindow, INT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CREATE:
//set game window and start
SetWindow(hWindow);
GameStart(hWindow);
return 0;

case WM_SETFOCUS:
//activate and sleep status
GameActivate(hWindow);
SetSleep(FALSE);
return 0;

case WM_KILLFOCUS:
//deactivate game and set sleep status
GameDeactivate(hWindow);
SetSleep(TRUE);
return 0;

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

//Paint Game
GamePaint(hDC);

EndPaint(hWindow, %ps);
return 0;

case WM_DESTROY;
//End game and close
GameEnd();
PostQuitMessage(0);
return 0;

}
return DefWindowProc(hWindow, msg, wParam, lParam);
}

theres the start of it.
namingway
namingway
Idont quite fully understand what all that does so if you can point out something wrong it'd be a great help
ZQJ
ZQJ
When I said offending code I was hoping for a small amount. First thing btw, when you post that amount of code on here you should put it inside [ source lang="cpp" ] [ /source ] tags which will highlight it nicely and put it in a scrolling box.

Anyway the first error I can see is GameEngine::GetEngine()->() should be GameEngine::GetEngine()->GetSleep(). And it should be ~GameEngine not -GameEngine(). I can't see anything else obvious.
namingway
namingway
thanks for pointing that out

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.