Sign in to follow this  
Renzo

SDL - performance question

Recommended Posts

I'm working on a very small 2d library to load and display SFF files (m.u.g.e.n. uses this file format for the gfx, and it's nothing more than a bunch of 8 bit PCXs grouped in a file with some extra info, like axis). My questions are: Do you recommend using SDL_BlitSurface instead of the function dibujar(), considering the conversion SDL should make if I'm using a 16bit surface as the main window? Do you recommend using assembly to do this very fast? (It's worth it?)
class SFF_Images
{
int numimages;
SFF_Image ** imagenes;
//blah blah blah
}

class SFF_Image
{
int group, index
int x,y
char *pal
char *rawdata
//more bla blah and foo
}


This is the code I use for drawing an Image onscreen:
void dibujar(SDL_Surface*pantalla, unsigned char*pixels,unsigned char*paleta, int alto, int ancho, int x, int y, int ex, int ey)
	{
		Uint16 r,g,b;
		Uint16 ix,sy,dx,iy;

    //To avoid of-screen drawing = SDL Parachute deploy!
		ix=-(x-ex<0)*(x-ex);
		sy=-(y-ey<0)*(y-ey);
		
		dx=-(W-(x+ancho-ex)<0)*(W-(x+ancho-ex));
		iy=-(H-(y+alto-ey)<0)*(H-(y+alto-ey));
		
    SDL_LockSurface(pantalla);
    SDL_FillRect(pantalla, NULL, SDL_MapRGB(pantalla->format,0xFF,0,0xFF));
    for(int i=sy;i<alto-iy;++i)
		  {
			for(int j=ix;j<ancho-dx;++j)
			  {
        	r=Uint16(paleta[3*pixels[ancho*i+j]+0])>>(pantalla->format->Rloss);
        	g=Uint16(paleta[3*pixels[ancho*i+j]+1])>>(pantalla->format->Gloss);
        	b=Uint16(paleta[3*pixels[ancho*i+j]+2])>>(pantalla->format->Bloss);

        	r=r<<(pantalla->format->Rshift);
        	g=g<<(pantalla->format->Gshift);
        	b=b<<(pantalla->format->Bshift);

					((Uint16*)(pantalla->pixels))[(i+y-ey)*pantalla->pitch/2+j+x-ex]=r|g|b;
				}
			}
		SDL_UnlockSurface(pantalla);
	}


Share this post


Link to post
Share on other sites
Well what you should do is profile each code and compare the values. Just from looking at it, I think that SDL_BlitSurface will be much faster than what you have. It is already an assembly optimized function, so why reinvent the wheel?

Share this post


Link to post
Share on other sites
Don't blit these manually. Make a surface of the same format as the screen and load the pixel data into the surface manually converting the colors using SDL_MapRGB or load the image in its native format into an SDL_Surface and use SDL_ConvertSurface. SDL surface to video memory surface can be accelerated, but not if you use a function that puts the pixels manually. Your method is also more error prone, don't assume you know the color formats of the destination surface.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this