Advertisement

02.03 - The Basic Windows Application

Started by April 09, 2001 12:08 AM
26 comments, last by Teej 21 years, 6 months ago
Boy, aren''t we a curious bunch?

Yes, G is a structure full of global variables that can be accessed from any file in the program. It''s declared in a header file, and uses a little trick to make it truly global without writing externs all over the place.

There''s a reason why all of the variables are in a single structure. Here''s the code:

  //----------------------------------------------------------------------------//                             Global Variables//---------------------------------------------------------------------------- #ifndef GLOBALS_OWNERSHIPextern#endif struct{    HWND      hWnd;         // Handle to the main window    HINSTANCE hInstance;    // Application instance    BOOL      bActive;      // Are we in the foreground?    BOOL      bQuitting;    // Set when the program is terminating     // DirectDraw Variables    LPDIRECTDRAW7        lpDD;    LPDIRECTDRAWSURFACE7 lpDDSPrimary;    LPDIRECTDRAWSURFACE7 lpDDSBack;    LPDIRECTDRAWSURFACE7 lpDDSRes;    LPDIRECTDRAWPALETTE  lpDDPalette;     // DirectInput Variables    LPDIRECTINPUT7       lpDI;    LPDIRECTINPUTDEVICE7 lpDIKeyboard;    UCHAR                KeyState[256];     // DirectSound Variables    LPDIRECTSOUND        lpDS;    LPDIRECTSOUNDBUFFER  lpDSB_Sample; } G;  


One of the source files needs to declare GLOBALS_OWNERSHIP. This file becomes the ''owner'' of the structure G. All other files that include this header get the structure with the word extern slapped onto the beginning of it, which in effect makes the structure available to all source files that include it. If I didn''t do things this way, I''d have to manually type extern before every variable I wanted global, which I consider a pain. Here, I''ve got all of my globals in a convenient structure, thereby enhancing the readability of the code overall.

Don''t get too worried about the code -- it''s all part of the Ladder series. And if you''re one of those people who remembers the code from the last time around, there may be some changes.

Teej


Cool, thanks Teej! I wish I could have gotten into that first tutorial, it seems like I missed a lot

"You won''t get wise with the sleep still in your eyes." - Neil Peart
"You won't get wise with the sleep still in your eyes." - Neil Peart
Advertisement
Not to worry, you won''t be missing anything in this new iteration of the tutorial series.

One of the problem areas with the original setup was that it was hard to follow articles from one to the next, and I didn''t use proper judgement when writing each of the PROJECT articles.

This time around, I have a better idea of what people tend to catch onto, and where I ''lose them''. Most of the original PROJECT material will likely find its way back into the forum, but totally rewritten and improved. This time around we need to ensure that our understanding of certain concepts is rock-solid before doing things that rely on them.

Think of it this way -- I''m determined to do things right this time. The articles will come at a nice steady pace, more of my time will be devoted to their quality, and they will persist for a long time to come. For this reason, I really need to apply some forethought to the next ten articles or so -- they''re going to be laying an all-important foundation to build from.

Teej
er.... i didn''t understand what you guys were talking about do i need to know how to program in c++ to use this totorial i have MSVC++ and 3D studio max, corel draw 9, corel photo paint 9, and a good sound editor i know how to use them all but do i have to be able to understand everything right now or are you going to explane it, just wondering.

Fighterdude (A Baldurs Gate Fan)
Are we meant to understand everything that is going on in this code or are we just meant to get the jist of it? If it''s the former I think I might have to go back to the C books
what''s got two legs and bleeds?Half a dog!
Fighterdude: No, we''re not really using C++ here. There''s some COM usage (with the DirectX components), but we''ll be going over it together. The vast majority of coding will be plain ol'' C.

Shmmee: You don''t need to understand the code right now...I was just offering an overview. In the first main section (after the Ladder Series) we''ll be doing the actual learning.

Teej

Advertisement
Thanks Teej
what''s got two legs and bleeds?Half a dog!
thanks teej for looking after those of us who arent quite C pros just yet!im glad i found this site!
That global stuff was a great idea, well done. I think I understand most of the code so far....

3D MAX and Photoshop???? Are you rich or like me? (you know what I mean)
Thank God, I was worried because I didn''t undestand all of that global stuff. Thank you Teej. (Not God, but close)


Remember to fill this with something clever and original later.
Remember to fill this with something clever and original later.

This topic is closed to new replies.

Advertisement