window.cpp code cleanup:
Declare variables outside of loops.
Reduced the scope of variables.
Do not assign values to variables if those values are not used.
Always bail out of functions ASAP, do not calculate stuff you might not need.
Use pre- instead of postfix de- and increments.
Declaration and assignment of variables in one line.
Merged if condition checks.
Added and removed brackets.
Added and removed newlines.
Fixed indentation.
Added TODOs (removal of magic numbers).
Approved by PS Jenkins bot, Sam Spilsbury.