🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Can you make this code helluva faster?

Started by
1 comment, last by Bigshot 24 years, 5 months ago
I''m writing a game and I have this one function that''s bottlenecking it a bit. The game is pretty simple, 2D, and it runs fast on all my computers, but I''m betting on a really slow Pentium or lower it won''t be so good. The function I have copies a segment of a background image (the background buffer) to the secondary DirectDraw surface (the back buffer) and the copy is a bit slow. The background picture is 1280x480 (that''s not too big) and the back buffer and the screen resolution are 640x480... so i had to make some functions to pan the screen on the x-axis so you can look at the whole background. The part that copies the background buffer to the back buffer is a little slow because it uses ''memcpy'' and it has to copy the buffer line by line. Here''s the exact function I have in my game: inline void Copy_Background() { // lock back surface DDraw_Lock_Back_Surface(); // get a copy of the memory addresses UCHAR *video_ptr = back_buffer; UCHAR *bg_ptr = bg_buffer; // advance pointer to beginning of the pan bg_ptr+=Screen_Pan_X; // copy line by line...very slow for (int y=0; y<480; y++) { // copy 640 bytes from b.g. to back buffer memcpy((void *)video_ptr,(void *)bg_ptr,640); // advance to next line which is ''memory pitch'' bytes away video_ptr+=ddsd.lPitch; // advance to next line in background buffer bg_ptr+=1280; } // end for y // unlock surface DDraw_Unlock_Back_Surface(); } // end Copy_Background Of course this could be helluva faster... so if anyone knows how i can make it faster and more efficient, please help me out. I was thinking assembly language and DWORDs, but I don''t know a damn thing about assembly and I''m not even that good at C/C++, so I''m pretty lost. I was also thinking of putting the background buffer in VRAM so then I would just have to copy vram to vram and that would be helluva fast, but I''m not sure how to do that either. The only other thing I was thinking was making the background buffer another DirectDraw surface, but I never have to blit anything on to it... i only copy it to the screen, so i don''t know if that would be any better. I would appreciate any help. Thanks, Al
Alexbigshot@austin.rr.comFoolish man give wife grand piano. Wise man give wife upright organ.
Advertisement
Assembler is pretty good for loops....

another thing, I don''t know what your stuff does, but if there is a way to track what has changed in the background you only have to repaint the part which has been changed, would be faster......

If you repaint the screen every time (either memcopy or writing a pixel every time) while the biggest part of the screen can remain the same you''ll get an decrease in speed. It''ll be hella lot faster if you don''t make the screen all black again, like I used to.....

Like I said, I don''t know what else you show on the screen.

Dance with me......

http://members.xoom.com/CJdeVos/index.htm

Okay, you''re missing the really big answer to your question. And when I tell you, you''re just gonna say *DUH*.

First thing you have to do (you probably have already done this) is to put your background image into an offscreen DirectDraw surface. This is done with a simple call to (I forgot the actual function name) IDirectDraw::CreateSurface(). Use the OFFSCREEN_PLAIN flag in the dwFlags parameter. You need to have DirectX 5.0 (I think it''s 5.0) or greater to create an offscreen surface bigger than your primary surface, like in your case.

Once you have it in a big offscreen surface, just sculpt out a RECT for the blit. Then call BltFast(). Here''s the code.


/////////////////////////////////////////////////
RECT src;

src.left = x; // this is the starting pixel on your
// offscreen surface
src.top = 0;
src.right = x + (SCREEN_WIDTH - 1);
src.bottom = (SCREEN_HEIGHT - 1);

//Now you have the portion of the background you want to
//blit, so blit it...
lpddsBack->BltFast(0, 0 //this puts it in the upper-left
lpddsBackgroundSurface, //background
&src, DDBLTFAST_WAIT);
/////////////////////////////////////////////////


This just sets up which portion of the background you wish to copy, then let DirectDraw do the blitting. It''s much, MUCH faster. I''m guessing you''re kinda new to DirectDraw, since this is kinda basic.
[email=jperegrine@customcall.com]ColdfireV[/email]

This topic is closed to new replies.

Advertisement