handmade.network » Forums » question about Day 004: Animating the backbuffer
C_Worm
27 posts
#21050 question about Day 004: Animating the backbuffer
3 months ago Edited by C_Worm on May 16, 2019, 9:26 p.m.

Hey i'm really confused about the "Pixel-loop" casey is implementing and how it works, would anyone be kind to explain it or parts of it?

1. how is the BitmapMemory even changing on this line:

" uint8 *Row = (uint8 *)BitmapMemory; "

i mean the Row variable isn't even attached to anything, or what?....

2. Also, i don't get how the Pixel variable is changing the Bitmap either since its only assigned the value of the Row which i don't see how and thing is attached.

3. Is there some other way to change the BitmapMemory's values with maybe an array[] or somehthing (and not 2 for-loops with pointer arithmetic?), or would that force us to like hard code every value for each pixel on the screen?

everyting was clear and intuitive until the for-loop implementation!
Croepha
David Butler
81 posts / 1 project

I love pretty much anything technical, whether it be programming, networking, hacking or electronics.

#21051 question about Day 004: Animating the backbuffer
3 months ago

Hi C_Worm! Welcome to the Handmade Network forums.


So, you may need to brush up on how pointers work in C/C++. I recommend the K&R C book if you haven't read it yet.


1
BitmapMemory
is a variable that contains the address where the memory for the Pixels starts.

When you do
1
uint8 *Row = (uint8 *)BitmapMemory;

You are setting Row to that same address. Row is a pointer to a uint8. Pointers are numbers that simply hold addresses to memory.
and
1
uint32 *Pixel = (uint32 *)Row;

Is also passing the address to Pixel.

When you do something like
1
*Pixel = something;


You are setting the memory that Pixel point to, to "something". The preceding asterisk is the dereference operator. The dereference means that we follow the pointer and operate on what it points to.

Arrays, and Pointers are functionally the same thing.


Look at this code:

1
2
3
4
5
6
int numbers[10];
int* address_of_numbers = &numbers[0];

// These next two lines, have the exact same result:
numbers[4] = 44; 
*(address_of_numbers + 4) = 44;


The only difference between the last two lines, is that we are using the pointer to get to the memory for numbers[44]. When we add 4 here, we are simply starting at the memory for numbers, then moving forward 4 values.

404 Signature not found
C_Worm
27 posts
#21053 question about Day 004: Animating the backbuffer
3 months ago

Hey and Thanks for the answer!

I have a follow up question:

How is

uint8 * Row = (uint8 *)BitmapMemory

Assigning BitmapMemory’s adress to Row?

Shouldn’t we need the adress of operator ”&”?


Or is (uint8 *)BitmapMemory the same as &Bitmapmemory??
mmozeiko
Mārtiņš Možeiko
1965 posts / 1 project
#21055 question about Day 004: Animating the backbuffer
3 months ago Edited by Mārtiņš Možeiko on May 17, 2019, 8:01 a.m.

"BitmapMemory" is a pointer. It points to pixels of bitmap. "&BitmapMemory" is address of pointer to pixels. It points to 64-bit value (pointer) that points to pixels of bitmap.

This assigns value of BitmapMemory pointer to variable Row:
"uint8* Row = (uint8 *)BitmapMemory".

Accessing Row[0] to Row[n] will access individual bytes of bitmap.

This assigns address of pointer to variable Row:
"uint8* Row = (uint8 *)&BitmapMemory".

Accessing Row[0] to Row[7] will actually access individual bytes of pointer value (BitmapMemory variable).

Two different things.
I suggest you to compile both statements in C and use debugger to step through it. And at the same time watch numbers of variables to understand what exactly gets assign to Row.