#### Archived

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

# Stupid STUPID newbie question - DDraw buffer error

This topic is 6516 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

The solution to this must be so simple. I feel like an idiot posting this. Anyways, I have primary and backbuffer surfaces in a flipping chain, now it''s time to mess with the surface by drawing pixels all over the place. Here is the code I have:
int
qDXInterface::DrawPixel (POS position)
{
DDSURFACEDESC2	ddsd;
UCHAR			color = rand()%256;

ZeroMemory (&ddsd, sizeof(ddsd));

lpBack->Lock( NULL, &ddsd, DDLOCK_SURFACEMEMORYPTR / DDLOCK_WAIT, NULL );

UCHAR	*buffer	= (UCHAR *)ddsd.lpSurface;
int	lpitch	= (int)ddsd.lPitch;

buffer[position.x + position.y * lpitch] = color; // error occurs here

lpBack->Unlock( NULL );

return 1;
}

The problem with the above code is that when it hits the line marked ''Error occurs here'', it shoves an access violation error in my face. Please tell me if this code snippet looks perfect, because then the problem causing the error is somewhere before this. Thanks guys. Oh yeah, the above code was written using LaMothe''s examples in Tricks of the Windows Game Programming Gurus as reference. The_Minister

##### Share on other sites
1) Don''t lock the surface for each pixel.
Lock surface -> draw all pixels -> unlock surface.

2) Don''t assume your surface lock is successful. Check its return value, and write pixels only when you know the lpSurface value is valid. (DD_OK)

##### Share on other sites
Sorry forgot to mention that this function is just to see if it works.
The final version of it will be far more optimised.

The_Minister

##### Share on other sites
Well I'll be damned.
ddsd.dwSize = sizeof(ddsd)

And it worked *gasp*.

I also added a primitive error handler. Very primitive.
BTW how do you intergrate them smiley faces into these posts?

The_Minister

Edited by - The_Minister on 4/16/00 6:28:39 AM

##### Share on other sites
All you have to do is type(without spaces and quotes) ": )" for a happy smiley, ": (" for a sad smiley, ": P" for a smiley with his tongue sticking out, "; )" for a smiley that's blinking with one eye. And of course there are other smileys, but these are the ones that come to mind right now.

/. Muzzafarath

Edited by - Muzzafarath on 4/16/00 6:59:09 AM

##### Share on other sites
Thats right Minister, DirectX tends to have size fields as the first 32bits of all it''s structs, so it knows what it is getting. It''s good practice, apparently.
If you don''t specify the size field, then it thinks its getting a zero size structure.

-Mezz

Thanks guys.

The_Minister

• 12
• 10
• 11
• 18
• 13