Rework of #2267
Reset w index at end of inner loop.
Clean-up of surrounding code to fit coding guidelines.
Fix whitespace in modified section.
Merge pull request #2268 from KonajuGames/texture2d-setdata-rect-fix

Rework of #2267
Pull from develop, reject changes