Jump to content
  • Advertisement
Sign in to follow this  
Sarxous

Windows programming Issues

This topic is 3965 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

I'm reading from the book. "Create RPGs with Direct X" or something along those lines and it has a chapter dedicated to Windows programming which is essential I guess. Anyway, I've uncovered some errors whilst programming. The Code:
#include <windows.h>

int WINAPI WinMain (HINSTANCE hInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil,
                    LPSTR lpCmdLine,
                    int nCmdShow)
{
    typedef struct WNDCLASSEX
    {
        UINT cbSize;
        UINT style;
        WNDPROC lpfnWndProc;
        int cbClsExtra;
        int cbWndExtra;
        HANDLE hInstance;
        HICON hIcon;
        HCURSOR hCursor;
        HBRUSH hbrBackground;
        LPCTSTR lpszMenuName;
        LPCTSTR lpszClassName;
        HICON hIconSm;
    } WNDCLASSEX;

    WNDCLASSEX wcex = { sizeof(WNDCLASSEX), CS_CLASSDC, WindowProc, 0, 0,
                           hInstance, NULL, NULL, NULL, NULL, "GameClass", NULL };

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

    HWND CreateWindow(LPCTSTR lpClassName,
                      LPCTSRT lpWindowName,
                      DWORD dwStyle,
                      int x,
                      int y,
                      int nWidth,
                      int nHeight,
                      HWND hWndParent,
                      HMENU hMenu,
                      HANDLE hInstance,
                      LPVOID lpParam );

    HWND hWnd;
    hWnd = CreateWindow("GameClass", "My Game Title", WS_OVERLAPPEDWINDOW,
                        320, 240, 0, 0, NULL, NULL, hInstance, NULL );

    if(hWnd == NULL)
        return FALSE;

    ShowWindow(hWnd, SW_SHOWNORMAL);
    UpdateWindow(hWnd);

    UnregisterClass("GameClass", hInstance);

    return 0;
}


The Errors: http://www.putfile.com/pic.php?img=6002168 Help please. [Edited by - Sarxous on July 16, 2007 12:06:34 AM]

Share this post


Link to post
Share on other sites
Advertisement
First off (because the image is too small for me too read. Just copy and paste the errors here next time please), you don't need or want to define WNDCLASSEX nor CreateWindow again, especially in your main function. That's why you have headers.

Second, even if you do get the program to compile, it's going to do absolutely nothing (well, in relative terms).

I'd suggest getting the basic programming mistakes taken care of before you worry about Windows specific mistakes.

Share this post


Link to post
Share on other sites
Yes yes I understand, putting definitions in headers is good practice I just didn't feel like the hassle while I was learning. Can you tell me whats wrong with the program, that would be useful.

(forgive the picture size. I forgot that photobucket resizes things)

Share this post


Link to post
Share on other sites
A) Remove typedef struct WNDCLASSEX ...
B) Remove HWND CreateWindow(LPCTSTR lpClass ...

Both of the above are already defined in Windows.H

C) You need to define the WindowProc. It's a callback function. What this means is that when you create a WNDCLASSEX object, you give it a function that will be run when certain messages are received. (Rather than my weak explanation, perhaps another person can give a better explanation).

D) You're not processing any messages. Assuming you take out the glaring errors, the program is going to create a window, display the window for a millisecond or so (give or take), and immeidately exit.

E) Another issue people have, is that if you switch to unicode (which you probably should), you'll need to encode your strings as wide character. This is up to how you have the compiler set up, and what compiler you're using, but for Visual C++, putting an _T("My text here"), or TEXT("My text here") will take care of this issue.

I suggest you start here albeit it's somewhat old, and the Win32 API is dieing. It should at least cover the basics.

Share this post


Link to post
Share on other sites
Previous errors are squashed but more plague me as I attempt to create a state and process manager.

Code:

Manager.cpp
////////////////////////////////////////////////////////////////////////////////
//////////////////////////////STATEMANAGER CLASS////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

class cStateManager
{
typedef struct sState
{
void *State();
sState *Next;
} sState;

protected:
sState *m_StateParent;

public:
cStateManager();
~cStateManager();
void Push(void (*Function)());
BOOL Pop();
BOOL Process();


};

////////////////////////////////////////////////////////////////////////////////
//////////////////////////////PROCESSMANGER CLASS///////////////////////////////
////////////////////////////////////////////////////////////////////////////////

class cProcessManager
{
typedef struct sProcess
{
void *State();
sProcess *Next;
} sProcess;

protected:
sProcess *m_ProcessParent;

public:
cProcessManager();
~cProcessManager();
void Process();
void Add(void *Process());

};


Manager.h
////////////////////////////////////////////////////////////////////////////////
////////////////////////////StateManager Functions//////////////////////////////
////////////////////////////////////////////////////////////////////////////////
cStateManager::cStateManager()
{
m_StateParent = NULL;
}

cStateManager::~cStateManager()
{
sState *StatePtr;

while((StatePtr = m_StateParent) != NULL)
{
m_StateParent = StatePtr->Next;
delete StatePtr;
}
}

void cStateManager::Push(void (*Function)())
{
if(Function != NULL)
{
sState *StatePtr = new sState;
StatePtr->Next = m_StateParent;
m_StateParent = StatePtr;
StatePtr->State = Function;
}
}

BOOL cStateManager::Pop()
{
sState *StatePtr = m_StateParent;

if(StatePtr != NULL)
{
m_StateParent = StatePtr->Next;
delete StatePtr;
}

if(m_StateParent == NULL)
{
return FALSE;
}
return TRUE;
}

BOOL cStateManager::Process()
{
if(m_StateParent == NULL)
return FALSE;

m_StateParent->State();
return TRUE;
}

////////////////////////////////////////////////////////////////////////////////
//////////////////////////ProcessManager Functions//////////////////////////////
////////////////////////////////////////////////////////////////////////////////
cProcessManager::cProcessManager()
{
m_ProcessParent = NULL;
}

cProcessManager::~cProcessManager()
{
sProcess *ProcessPtr;

while((ProcessPtr = m_ProcessParent) != NULL)
{
m_ProcessParent = ProcessPtr->Next;
delete ProcessPtr;
}
}

void cProcessManager::Add(void *Process())
{
if(Process != NULL)
{
sProcess *ProcessPtr = new sProcess;
ProcessPtr->Next = m_ProcessParent;
m_ProcessParent = ProcessPtr;
ProcessPtr->State = Process;
}
}

void cProcessManager::Process()
{
sProcess *ProcessPtr = m_ProcessParent;

while(ProcessPtr != NULL)
{
ProcessPtr->State();
ProcessPtr = ProcessPtr->Next;
}
}



The Errors:
At "StatePtr->State = Function;" // Invalid use of member (did you forget the "&"?)

At "ProcessPtr->State = Process;" // Invalid use of member (did you forget the "&"?)

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!