25 posts
Edited by Class GenericHuman on
Sorry for printing out the code like this but I think it would make it easier to understand my question. Delete thread if necessary. Anyway, in win32_handmade.cpp, inside the Win32ProcessPendingMessages function, we have a switch statement for handling messages. inside it, there is this code:

 1 2 3 4 5  case WM_SYSKEYDOWN: case WM_SYSKEYUP: case WM_KEYDOWN: case WM_KEYUP: { .. 

Without breaks between any of the cases, wouldn't WM_SYSKEYDOWN, WM_SYSKEYUP or WM_KEYDOWN trigger WM_KEYUP?
27 posts
This isn't a day 25 thing. But anyway, yes, you're right. That's exactly what should happen.

Basically, we want to handle all of these messages with the same block of code.
25 posts
Oh, okay. We are catching all those cases in one case. Thanks. Also, can you tell what the reason is for doing it this way? Is it style choice or does it affect performance/readability etc?
27 posts
That's...the only way to do it that I know of?
The only alternative is to copy and paste the same block of code for each case, or put it in a separate function and call that in each case. Either way, this way is much easier.
If we don't include each of these cases, they will be caught by the default handler.
Mārtiņš Možeiko
2358 posts / 2 projects
Alternative would be to use if statement:
 1 2 3 4 if (Message==WM_SYSKEYDOWN || Message==WM_SYSKEYUP || Message==WM_KEYDOWN || Message==WM_KEYUP) { ... // big block of code } 
Ruy Calderon
26 posts
Edited by Ruy Calderon on Reason: Overkill, explained much more succinctly above
EDIT: Overkill, explained much more succinctly above

http://msdn.microsoft.com/en-us/l...ws/desktop/ff468930(v=vs.85).aspx
Mārtiņš Možeiko
2358 posts / 2 projects
Edited by Mārtiņš Možeiko on
Btw, you could look on switch statement as a series of comparisons and goto's, if that is easier to understand.

So code like this:
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 switch (Var) { case Value1: Code1; break; case Value2: case Value3: case Value4: Code2; break; default: Code3; break; } 

Is equivalent to this code:
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 if (Var == Value1) { goto CaseA; } else if (Var == Value2 || Var == Value4 || Var == Value5) { goto CaseB; } else { goto CaseC; } CaseA: Code1; goto End; CaseB: Code2; goto End; CaseC: Code3; goto End; End: ... 

Of course writing switch and cases is much cleaner, and also in some cases compiler can optimize it better than series of if statements.
Andrew Bromage
183 posts / 1 project
Research engineer, resident maths nerd (Erdős number 3).