Handmade Hero»Forums»Code
1 posts
Why does *Row has to be a uint8 instead of a uint32
Hi I was just hoping someone could please explain why *Row has to be a uint8 instead of a uint32 and why then *Pixel is a uint32. Thanks



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
uint8 *Row = Buffer->Memory + MinX * Buffer->BytesPerPixel + MinY * Buffer->Pitch

for(int Y = MinY; Y < MaxY; Y++)
{

uint32 *Pixel = (uint32 *)Row;

for(int X = MinX; X < MaxX; X++)
{
*Pixel++ = Color;
}
Row += Buffer->Pitch;

}
498 posts
Why does *Row has to be a uint8 instead of a uint32
So that Row += Buffer->Pitch; will work.

pointer arithmetic will automatically multiply the size of the thing pointer ad with the offsets you add and subtract to it. uint8 is the same granularity in which Pitch is provided so it makes sense to use that.
Simon Anciaux
1112 posts
Why does *Row has to be a uint8 instead of a uint32
You may find some more informations on this thread.
Why does *Row has to be a uint8 instead of a uint32
1
2
3
4
fake Buffer->Memory
0 1 2 3 4 5 6 7
8 9 a b c d e f
g h i j k l m n

you can pretend that each element of the array above is the size uint8
so from the start of the array, 0, going 4 uint8's in gets you to 4
1
[start here]0, 1, 2, 3, [end here]4

1
uint8 *arraystart + 4 = 4

and from
1
(uint8 *)(j's position) + 4 = n



since a uint32 is equal to 4 uint8's, it represents 4 elements in the array.
from the start of the array going 1 uint32 further gets you to 4

1
(uint32 *)arraystart + 1 = 4


pitch = amount to get to the next row but under where the old position was
pitch = 8
row is a uint8 pointer type
1
uint8 *Row
so from 0's position adding the pitch puts you at 8, at c it puts you to k.

if row were a uint32 * then adding pitch would add pitch * 4 = 32
and would go outside of the array above, the point being it's not working as a pitch then

with the uint32 *Pixel, it's the same logic, you're accessing 4 elements at a time.
1
[0, 1, 2 , 3], [4, 5, 6, 7] ... [g, h, i, j] ...