DirectDraw problems with corrupted sprites

Started by
4 comments, last by Kosh 23 years, 11 months ago
hi everyone I have a little problem using DirectDraw and well, this is a good place to start asking, so.... I have a sprite game, based on space invaders (uh oh, hasbro watching me!!) and well, I have the sprites for the invaders, defense units and player sprite stored in IDirectDrawSurface4 *surfaces''s in video memory But my problem is, that when I run the game, it copies these master images from the video storage to the video memory that is displaying the screen. When I do this, I also have a star field in the background, this is being drawn all on the same buffer (secondary buffer). The order of drawing all this is Starfield Interface elements = score, highscore, lives, level number static sprites = Invaders, player, defense units dynamic sprites = weapons, explosions (static sprites meaning that they are there for the whole game, and either are destroyed for the entire level, or not) (dynamic sprites are fleeting sprites which dont stay onscreen for long, like weapons and explosion effects) Now, When I draw all this to the screen, it appears like the star field is corrupting the master images for the sprites, for some reason, when I blit the sprites to the secondary surface, they pick up all the stars beneath them and hence get corrupted. But I dont understand how this could happen. Does anyone know what I mean, does anyone need more information on this, please reply, cause this is REALLY bugging me kosh
if understanding it made and born with time, then time, is understandings greatest enemy.....
Advertisement
More than likely it is the pixel plotting that you use for drawing the stars. You are probably changing memory outside the area of the back surface and altering the surfaces for the other sprites. If the res is 640x480, for example you can only draw from 639x479...
that one pixel difference is probably causing the problem.
(memory indexes is zero based so it is 0 - 639). And don''t forget to plot pixels using lPitch and NOT dwWidth.
ok, in response to you I post up this source, cause this is the function that I am using to draw the stars ok



int RenderStars(IDirectDrawSurface4 *surface)
{
DDSURFACEDESC2 RenderStarsDesc;
memset(&RenderStarsDesc, 0, sizeof(RenderStarsDesc));
RenderStarsDesc.dwSize = sizeof(RenderStarsDesc);

if((surface->Lock(NULL, &RenderStarsDesc, DDLOCK_SURFACEMEMORYPTR / DDLOCK_WAIT,NULL)) == DD_OK)
{
int bytesperpixel = (int)RenderStarsDesc.lPitch;
unsigned int *buffer = (unsigned int *)RenderStarsDesc.lpSurface;

for (int i = 0; i < 250; i++){
buffer[(star_field.y * (bytesperpixel>>1)) + star_field.x] = star_field.colour;<br> }<br><br> if((surface->Unlock(NULL)) == DD_OK) return 1;<br> }<br> <br> return 0;<br><br>} <br><br></code> </i>
if understanding it made and born with time, then time, is understandings greatest enemy.....
oh, by the way, I''m using 16bit colour, just in case you were wondering what the bytesperpixel>>1 meant, it''s dividing the lpitch by two, which gives me the exact number of pixels, rather than the actual memory used in those pixels

this is correct aint it?

kosh
if understanding it made and born with time, then time, is understandings greatest enemy.....

int bytesperpixel = (int)RenderStarsDesc.lPitch;


A better name for this might be bytesperline ...

unsigned int *buffer = (unsigned int *)RenderStarsDesc.lpSurface;


here''s your problem: you''re indexing an array of INTs which
are 4 bytes long. change to ''short'' (or even better, a ''sized'' typedef: typedef short uint16;)


for (int i = 0; i < 250; i++){
buffer[(star_field.y * (bytesperpixel>>1)) + star_field.x] = star_field.colour;<br>}<br></pre></code><br><br>I assume you mean <code>star_field.y</code>, not <code>star_field.y</code> …<br><br>Good luck.<br><br>–Pryankster </i>
-- Pryankster(Check out my game, a work in progress: DigiBot)
hi

well Pryankster you solved my problem, by the way, it wasnt the array index, I dont know why, but I copied an old version by mistake (version checking anyone ) so it was in the middle of becoming array indexed

BUT !!! the problem with the four byte INT worked, I dont like Win32 typedefs, I think the make code a little less readable, I prefer ansi, so, I just changed it to unsigned short and AWAY !!

thanks to everyone who solved this problem, it''s not a problem anymore, no corruption in any sprite
if understanding it made and born with time, then time, is understandings greatest enemy.....

This topic is closed to new replies.

Advertisement