1 2 3 4 5 6 7 | for (int Y = StartY; Y < EndY; ++Y) { for(int X = StartX; X < EndX; ++X) { ((uint32*)(Buffer->Memory))[(Y*Buffer->Width) + X] = Color; } } |
Thinking of the equivalent using pointer arithmetic did not come as quickly to me, but I ended up with something like this:
1 2 3 4 5 6 7 8 9 10 11 | uint8 *Row = (uint8 *)Buffer->Memory; Row += (StartY * Buffer->Pitch); for (int Y = 0; Y < Height; ++Y) { uint32 *Pixel = ((uint32*)(Row)) + StartX; for(int X = 0; X < Width; ++X) { *Pixel++ = Color; } Row += Buffer->Pitch; } |
I think I heard somewhere that [ ] in C is just literally shorthand for pointer arithmetic, but I'm not sure if that's actually true?
I guess I'm wondering if it's worth stumbling through the pointer arithmetic with these kinds of loops for educational purposes if nothing else. Are there any performance / design considerations with this kind of thing?
Curious to hear any thoughts. Thanks ^-^