Skip to main content
GameDev.net gamedev.net
🔒 Locked

Pointer Function Help

Started by tenpoundbear Nov 20, 2009 at 9:36 AM 4 replies 750+ views
Original Post
tenpoundbear
tenpoundbear
Hey guys, Hope you guys can give me some assistance here. What I need to do is 'somehow' reference the function 'WinProc' for 'myWindow.lpfnWndProc' structure. The class 'Direct3DUtility' is separate from my main window class, hence I can't just easily reference the function name. I'm not sure how to do it. My idea is to pass in a 'WinProc' pointer to the function and use it but I'm so confused on this topic I just don't how to implement it. Main class

#include <Windows.h>

/* This function processes messages that the window recieves. */
LRESULT WINAPI WinProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	return DefWindowProc (hWnd, msg, wParam, lParam);
}

/* Equivalent to the 'main(){}' funtion in a console program. */
int WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	return 0;
}


Direct3DUtility interface class

#include <Windows.h>

class Direct3DUtility {
public:
	void init3D( HINSTANCE hInstance );
};



Direct3DUtility implementation class

#include "Direct3DUtility.h"

void Direct3DUtility::init3D( HINSTANCE hInstance ) {
	WNDCLASSEX myWindow;

	ZeroMemory( &myWindow, sizeof( WNDCLASSEX ));

	myWindow.cbSize = sizeof( WNDCLASSEX );
	myWindow.style = CS_CLASSDC;
	myWindow.lpfnWndProc = &WinProc
	myWindow.hInstance = hInstance;
	myWindow.lpszClassName = "Star Fleet";

	HWND hWnd = CreateWindow(
		"Star Fleet",
		"Prototype",
		WS_OVERLAPPEDWINDOW,
		100, 100,
		300, 300,
		GetDesktopWindow(),
		NULL,
		myWindow.hInstance,
		NULL
	);
}



I understand that function pointers follow the syntax like so, 'return type (*functionName) (parameters)' but I'm having hard time understanding how to implement it. I've read heaps of stuff on the web and have a textbook which I've read also but I am still fuzzy. Some are just beyond my knowledge and not what I need and some are just brief. Can you guys give me some suggestions maybe... any help is appreciated guys :)
ToohrVyk
ToohrVyk
Assuming that your "Main Class" is a typo, and that your WinProc is indeed a standalone function as illustrated in your example, then your code is correct—all you need is to declare the function in the files where you use it, which can be done by adding...
LRESULT WINAPI WinProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
...to every file that needs it (or a header file included by them).
tenpoundbear
tenpoundbear
Hmmm... what do you mean exactly?

So instead of having function 'WinProc' in my main class, move it to my Direct3DUtility class instead?

I tried that but have compile errors, "warning C4007: 'WinMain' : must be '__stdcall'" I think that is saying that I have to the WinProc function in same class as my 'WinMain' function?

Either way, still have a compile error :(
nobodynews
nobodynews
Quote:
Original post by tenpoundbear
Hmmm... what do you mean exactly?

So instead of having function 'WinProc' in my main class, move it to my Direct3DUtility class instead?

I tried that but have compile errors, "warning C4007: 'WinMain' : must be '__stdcall'" I think that is saying that I have to the WinProc function in same class as my 'WinMain' function?

Either way, still have a compile error :(


You can't have a non-static class member function for your windows procedure. This article covers a lot of the concepts needed to make a wrapper class like what you seem to be trying to do.

edit: Actually, I'm not even sure this is what you want. You kept saying 'main class' which threw me off. Do you mean 'the file with your application entry point'? Or are you actually using a class to put WinProc and WinMain inside of? I'm not sure if there is a standard term for 'application entry point' or not, but 'main class' probably isn't it. Your usage of 'class' and the code you provided isn't entirely clear.
C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) ,
tenpoundbear
tenpoundbear
Quote:

You kept saying 'main class' which threw me off. Do you mean 'the file with your application entry point'?


Yep that is what I meant, sorry I wasn't clear. I still have heaps to learn :)

To clarify what I am trying to do, I was trying to separate the code that creates and registers the window class from the application entry point.

For example instead of having this...
#include <windows.h>LRESULT WINAPI WinProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ){	return DefWindowProc( hWnd, msg, wParam, lParam );}int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ){	WNDCLASSEX myWindowClass;	memset( &myWindowClass, 0, sizeof(myWindowClass) );	myWindowClass.cbSize = sizeof(WNDCLASSEX);	myWindowClass.style = CS_CLASSDC;	myWindowClass.lpfnWndProc = &WinProc	myWindowClass.hInstance = hInstance;	myWindowClass.lpszClassName = "MyFirstWindowClass";	RegisterClassEx( &myWindowClass );	HWND hWnd = CreateWindow(		"MyFirstWindowClass",		"My Window Title",		WS_OVERLAPPEDWINDOW,		100, 100,		300, 300,		GetDesktopWindow(),		NULL,		myWindowClass.hInstance,		NULL	);	ShowWindow( hWnd, SW_SHOW );	Sleep( 5000 );	DestroyWindow( hWnd );	UnregisterClass( "MyFirstWindowClass", hInstance );	return 0;}


I was trying to have my design my program more along the line of the initial post.

Initially when I was reading stuff and trying to understand other people's code I saw a code example like this...
//////////////////////////////////////////////////////////////////////////////////////////////////// // File: d3dUtility.h// // Author: Frank Luna (C) All Rights Reserved//// System: AMD Athlon 1800+ XP, 512 DDR, Geforce 3, Windows XP, MSVC++ 7.0 //// Desc: Provides utility functions for simplifying common tasks.//          //////////////////////////////////////////////////////////////////////////////////////////////////#ifndef __d3dUtilityH__#define __d3dUtilityH__#include <d3dx9.h>#include <string>namespace d3d{	bool InitD3D(		HINSTANCE hInstance,       // [in] Application instance.		int width, int height,     // [in] Backbuffer dimensions.		bool windowed,             // [in] Windowed (true)or full screen (false).		D3DDEVTYPE deviceType,     // [in] HAL or REF		IDirect3DDevice9** device);// [out]The created device.	int EnterMsgLoop( 		bool (*ptr_display)(float timeDelta));	LRESULT CALLBACK WndProc(		HWND hwnd,		UINT msg, 		WPARAM wParam,		LPARAM lParam);	template<class T> void Release(T t)	{		if( t )		{			t->Release();			t = 0;		}	}			template<class T> void Delete(T t)	{		if( t )		{			delete t;			t = 0;		}	}}#endif // __d3dUtilityH__


Now at first when I saw this I didn't understand what the reserved word 'namespace' was trying to achieve. I do have some understanding of it, and I've use it before but just never in this context.

To me it look like the reserved word 'class' but after reading some stuff I think I understand how it is being used in this context now...

Basically with Frank's code... does that mean that you can use the functions in the file without having to first instantiate an object of that class first?

So just type d3d::blahblah?

Anyways this was basically what I was trying to do myself... I didn't wanted to use Frank's code because I didn't understand it but now that I know I might adapt it to my own code.

So in conclusion...
I am correct about the 'namespace' word?
And this will work for what I am trying to do myself?

So sorry about the long posts guys :)
Thanks again.
tenpoundbear
tenpoundbear
P. S.

I also have a warning when I compiled my code, "warning C4007: 'WinMain' : must be '__stdcall'".

Should I be very concerned about this warning? to be honest I don't have the faintest idea what it is going on about :(

Topic Locked

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

Sign in to reply to this topic.