Can you make this code helluva faster?

Started by
2 comments, last by Bigshot 24 years, 3 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
Okay, if I am understanding what you are wanting to do, then you are going at it the wrong way. You don''t have to do a memcopy() when you have blitting hardware that will do it for you.

Make your background into a surface, then just blit the portion you want to the backbuffer. This will be much faster than copying it line by line.
One way you can do it is load up the image wich is 1280 x 480 on a of screen surface. Then using the Direct Draw BLT rutin from one surace to the other should speed up stuff alot.

Also what you can do is, since vidoe cards are getting more and more ram, when you create your surface, try to create always in video ram, through a simple is else if the creation failed you create in sys ram. One thing is comuter arent getting slower but only faster so even if the surface is in sys ram I dont think you will loose to much speed!

My say right now is: "If the have a pentium 1 generation computer they should even be playing games no more!"
Hardcore Until The End.
I''ll probably just make it into a surface like you suggested. That was one of the ideas I listed in my first post. When I was first writing the code I thought about making the background a surface, but it never happened. I guess I''m too used to old DOS stuff and I just started learning DirectDraw... actually I just started learning C/C++ last summer. Anyway.. I''ll try your method. I''m going to have to look up some API functions because the graphics library i''m using right now I didn''t write. It''s from the book ''Tricks of the Windows Game Programming Gurus''.

Thanks for your advice,
Al
Alexbigshot@austin.rr.comFoolish man give wife grand piano. Wise man give wife upright organ.

This topic is closed to new replies.

Advertisement