Sign in to follow this  
GROUDON

HWND hWnd

Recommended Posts

How does hWnd allow functions used after other functions use variables of those functions as if those variables were part of that function. example:
void function1(HWND hwnd)
{
   int x;
   x=0;
}

void function2(HWND hwnd)
{
   x+=1;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
   MSG msg;
   MyRegisterClass(hInstance);
   if(!InitInstance(hInstance, nCmdShow))
      return FALSE;
   function1(hWnd);
   function2(hWnd);
   while(GetMessage(&msg, NULL, 0, 0))
   {
      TranslateMessage(&msg);
      DispatchMesssage(&msg);
   }
}

if this code is incorrect, please tell me why. I know it's incomplete.

Share this post


Link to post
Share on other sites
I don't follow. HWND is just another type, that's typedef'd to be a void* (A little more involved than that, but essentially just a void*). Adding a HWND as a parameter doesn't let you do anything special.

Can we see some full code to see what you mean?

Share this post


Link to post
Share on other sites
HWND does no such thing, that code, as it stands, will not compile. You will have an error in function2, because x is not declared.

If you do see code that compiles, post all of that code. You probably have a global variable called 'x' someplace.

Share this post


Link to post
Share on other sites
yeah I think jpetrie is right.

I think this is more of a 'scope' question rather than anything to do with HWND.

maybe you could rephrase the question my friend.

From what I see you are delaring 'x' in func1, and trying to use it in func2 ??

If that is your problem, then yeah, you'll need to make 'x' global.

Share this post


Link to post
Share on other sites
The situation you are talking about IS global variable usage. I assume you are not talking about code you thought up and wrote from scratch, but code you see in common projects / examples.

The normal idiom for many old Visual C++ 6.0 people was to put all the really common shared things such as the directDrawDevice, mainWindow, etc as globals so that they would save some typing not having to pass them around (and maybe a very tiny amount of efficiency), and cause they didn't want to write complex OO manager classes they didn't understand (you know the classic C question, why do C++ people put the globals in a class when they know there is only going to be one instance?).

Well versed OO people know that its just as easy to organize everything into its proper containers and then you can mix-and-match pieces (libraries, modules, whatever) in different projects much more safely and easily.

Share this post


Link to post
Share on other sites
What I'm asking is, certain functions have HWND hwnd as a parameter, why so?
I noticed that functions not used in WinMain do not have HWND hwnd as a parameter.

By the way, the reason that i couldn't give you the entire program, was that i was on a computer at school, i didn't have the files with me. I still am not able to give you my code though, so i'll just have to ask questions like this.

I use directx 9.0b and Microsoft Visual Studio .NET 2003. I'm reading the book "Beginning Game Programming" by Jonathan S. Harbour. (Ever heard of him?)
I got to the part where i displayed a Hummer rotating around in the fullscreen mode. I works perfectly, (after i figured out i had to add some directx libraries to the linker command line!) and i was interested in what exactly hWnd does and how to use it.

also...

I got this neat little program called anim8tor. Has anyone heard of it? (It makes 3d models.) and if so, i have two extra questions,
1) How do you make jointed models and use them in your game?
2) How do you export an animated scene into a .X file?

Share this post


Link to post
Share on other sites
An HWND is a handle to a window. Functions take it as a parameter where a reference to a window is required. The reason you tend not to see HWND outside of WinMain in games, is because games don't need to use/manipulate windows beyond creating one at the beginning and destroying it at the end.

Degra

Share this post


Link to post
Share on other sites
all of the things that start with H in Win32 programs are "handles" they are basically like logical pointers (instead of a memory address they represent just an arbitrary id for that window you are using). Almost every win32 function that creates or retrieves an OS object (Window, Device Context, Brush, Pen, etc) returns a handle. Almost every Win32 function that modifies or uses an OS object accepts a handle parameter as an object.

When people make a main window, they often store it in a global variable and just access it from there, rather than pass it around to every function that might need it.

Share this post


Link to post
Share on other sites
From the sound of it, it seems like you do not understand how WIN32 API works. I would highly recommend you learn the basics before using another library that relies on it (like DirectX).

I don't have any Win32 tutorials on me, but MSDN is a great resource for all your Win32 (and even DX) needs.

Share this post


Link to post
Share on other sites
Quote:
Original post by Koobazaur
From the sound of it, it seems like you do not understand how WIN32 API works. I would highly recommend you learn the basics before using another library that relies on it (like DirectX).

I don't have any Win32 tutorials on me, but MSDN is a great resource for all your Win32 (and even DX) needs.


I disagree, you don't need to know a lot about the win32 api to use directx. As degra said, you usually just create a window at the start and destroy it at the end, and there's a gazillion tutorials online showing how to do just that.

Share this post


Link to post
Share on other sites
Quote:
Original post by gharen2
Quote:
Original post by Koobazaur
From the sound of it, it seems like you do not understand how WIN32 API works. I would highly recommend you learn the basics before using another library that relies on it (like DirectX).

I don't have any Win32 tutorials on me, but MSDN is a great resource for all your Win32 (and even DX) needs.


I disagree, you don't need to know a lot about the win32 api to use directx. As degra said, you usually just create a window at the start and destroy it at the end, and there's a gazillion tutorials online showing how to do just that.


i think koobazaur is right, but to be honest you've got a point in that there isn't too much to 'get', as far as the basics go. stuff like drawing sprites/text through GDI to screen isn't important, as long as messaging and window creation is understood.


GROUDON I advise you to look into HWND a little more and understand what it's used for. You won't need to touch it much, but appreciation of the window creation and message handling systems are needed before going onto doing API programming.

Share this post


Link to post
Share on other sites
all windows programmers should at least learn what a WinMain and a WndProc do, how they interact with the OS, and how SendMessage and PostMessage work. Because every other language and windowing system on top of the OS (.NET, etc) still interacts through that system. Beyond those basics, almost nothing else is needed by everyone (although most people could stand to learn some more, depending on their needs ... such as WinSock, or device contexts, etc).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this