Archived

This topic is now archived and is closed to further replies.

PHRICTION

Win32 program structure

Recommended Posts

I just starting to get the hand of win32 coding. But, one small problem I am having is keeping track a variables within my main windows procedure. The problem is that some static variables need to be initialized, so I have to do it in WM_CREATE or else it will reintialize. I end up having some variables that only pertain to a certain message declared up at the begining, and once my program grows I have variabled declared all over the place. Should I just declare every variable at the begining, or is there a trick (eg. naming convention) for keeping track or them, or do I just deal with it? Thanks, Phriction

Share this post


Link to post
Share on other sites

int some_function (int some_argument)
{
static int some_variable = 0;
return 0;
}


If this is what you're talking about, it will only be initialized once.




[edited by - microdot on March 17, 2003 8:52:12 PM]

Share this post


Link to post
Share on other sites
Well not exactly.
I will show you:


      
WinProc(...)
{
//declare some static variables

switch(message)
{
case WM_CREATE:
//initialize the static variables


case WM_COMMAND //for example

static int ForExample[8];
//this will be called every time this procedure recieves

//a WM_COMMAND.

for( i = 0; i < 8; i++ )
{
ForExample[i] = i;
}

}

}


So I either have to declare ForExample outside the switch and intitialize it during WM_CREATE, or I could do this alternatly:


  
WinProc(...)
{
static bool bFirstCall = true;
switch(message)
{
//...

case WM_COMMAND:
static int ForExample[8];

if( bFirstRun )
{
//intialize ForExample

bFirstRun = false;
}
}
}


Im not allowed to do this: static int ForExampe = 5;, inside a case label because it's skipped by other case labels witch I don't understand. You see how confused I am =) ?

EDIT: Typos, and more typos
[edited by - phriction on March 17, 2003 9:07:01 PM]

[edited by - phriction on March 17, 2003 9:09:58 PM]

[edited by - phriction on March 17, 2003 9:11:36 PM]

Share this post


Link to post
Share on other sites
To be honest, I'm starting to get confused myself

Yes, the static variables need to be declared a the beginning of the window procedure. If you don't want variables all over your window procedure you could consider putting some of the message handling into seperate functions called from the window procedure and make the variables local to those functions. Where are the values for these variables coming from?

[edited by - microdot on March 17, 2003 9:28:44 PM]

Share this post


Link to post
Share on other sites
Yes, I think your static variables should go at the top of the function instead of at the case labels, unless those variables will only be used within the case label itself.

For example WM_MOUSEMOVE might use a static x and y variable to track the mouse speed.

Share this post


Link to post
Share on other sites
You can declare variables in a switch statement, you just have to make sure they go out of scope at the end of the case.

case WM_COMMAND:
int bla; //Error
break;

case WM_COMMAND:
{
int bla; //ok
break;
}

If you have so many variables that it becomes confusing then you probably need to rethink your design.



Qui fut tout, et qui ne fut rien
Invader''s Realm

Share this post


Link to post
Share on other sites