Archived

This topic is now archived and is closed to further replies.

RickyPiller

DDraw ddsd.lpSurface and ddsd.lPitch??

Recommended Posts

void Line(int x1, int y1, int x2, int y2) { buffer = (UCHAR*)ddsd.lpSurface; memoffset = ddsd.lPitch; int x = x1, y = y1; for(int i = 0; i <= y2 - y1; i++) { for(int j = 0; j <= x2 - x1; j++) { buffer[x + y*memoffset] = 5;//fills in the pixel in this memory spot x+=1; } x=x1; y+=1; } } I have some questions about how this works. First off I know ddsd.lpSurface is of type (void*) but why do we cast it to (UCHAR*)?? Also why do we use x+y*ddsd.lPitch?? These are kindof confusing to me. Thanks all. DDraw. She has double D''''s

Share this post


Link to post
Share on other sites
Well, I don''t have aclue about why we convert it to uchar, but I can answer question #2.

Ok, the whole surface is one big array. for example, pixel 0,0 is element 0 in the array. pixel 1,0 is element 1 in the array.
pixel 0,1 is element(assuming 640*480 resolution) is element 640 in the array. So, y*lpitch(this is suposed to be 640 but sometimes is not, not sure why) will get you how many pixels down the # is in the array, then adding x to it will add that many elements to it. Let me show this in a simple table of pixels:

1 ,2, 3, 4, 5, 6, 7, 8, 9,10
11,12,13,14,15,16,17,18,19,20
21,22,23,24,25,26,27,28,29,30

a lot more than 10, but you get the idea I hope

Sponge Factory
--Muzlack

Share this post


Link to post
Share on other sites
The reason the width is not equal to pitch all the time is that for speed purposes it is cometimes faster to force the scanlines to align on dword or quadword boundaries. The reason you cast it is because presumably you want to work with it in one byte increments (usually the easiest way) you could do it in 16bit or 32 bit chunks too if you found it to be easier this way.

Share this post


Link to post
Share on other sites
the reason you have to "convert" the void* pointer to something else (UCHAR* or BYTE* or even DWORD* for 32bit screenmodes) is, that... well - lookup void in any dictionary.

void* means: i have a pointer, but i dont know to what kind of data it points.
you have to tell the compiler if you want to write a byte, or 2 or 4, or... maybe 8 (double).

got me?

just try:

BYTE some_array[123];
void* ptr = (void*) &(some_array[0]); // make void-pointer point to the array...

*ptr = 1; // <- you get the compiler error HERE

next thing is - if you have

BYTE* p1 = (BYTE*) &(some_array[0]);
DWORD* p2 = (DWORD*) &(some_array[0]);

then p1[3] and p2[3] does not reference the same address.
get a c++ book, read it and you know why.

bye,

--- foobar
We push more polygons before breakfast than most people do in a day

[edited by - foobar on March 21, 2002 1:53:43 PM]

Share this post


Link to post
Share on other sites