• Advertisement
Sign in to follow this  

drawImage horribly slow?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm drawing to draw a simple tile map just to test it. Here is the loop:
	while(running)
	{

		SDL_Delay(10);
		
		while( SDL_PollEvent( &event ) )
		{
			if( event.type == SDL_QUIT )
			{
				running = false;
			}

			Uint8 *keyboard = SDL_GetKeyState( NULL );

			if( event.type = SDL_KEYDOWN )
			{
				if( keyboard[SDLK_ESCAPE] )
				{
					running = false;
				}
			}
		}

		video->beginRendering();
			for( int y = 0; y < 24; y++ )
			{
				for( int x = 0; x < 32; x++ )
				{
					video->drawImage( tiles, Rect( 2*32, 32, 32, 32), Coord( x * 32, y * 32), Color(), 0 );
				}
			}

			video->drawImage( img, Rect(0, 0, 24, 48), Coord( 100,100 ), Color( 255,255,255,255 ) );

		video->endRendering();
	}

Its only one layer of 32x32 tiles across a 1024x768 screen. When I lower the number of tiles per x/y to 5 it runs fine...but doing the whole screen runs less then a frame a second. How can I improve the frame rate? I think it might be because I'm drawing a quad instead of two triangles...which I'm not exactly sure how I would align the textures on two triangles.
void VideoDriver::drawImage( Image *image, Rect &src, Coord &dest, Color &color, int skew )
{
	if(image == NULL)
		return;

	float texX1	= src.x / (float)image->getWidth();
	float texY1 = src.y / (float)image->getHeight();
	float texX2 = (src.x + src.width) / (float)image->getWidth();
	float texY2 = (src.y + src.height) / (float)image->getHeight();

	glBindTexture( GL_TEXTURE_2D, image->getTexture() );
	
	glEnable( GL_TEXTURE_2D );

	glBegin( GL_QUADS );
		glColor4ub( color.r, color.g, color.b, color.a );
		
		glTexCoord2f( texX1, texY1 );
        glVertex3f( dest.x + skew, dest.y + (+skew / 2), 0 );

		glTexCoord2f( texX2, texY1 );
        glVertex3f( dest.x + src.width + skew, dest.y + (+skew / 2 ), 0 );

		glTexCoord2f( texX2, texY2 );
        glVertex3f( dest.x + src.width, dest.y + src.height, 0 );

		glTexCoord2f( texX1, texY2 );
        glVertex3f( dest.x, dest.y + src.height, 0 );
    glEnd();

}

Share this post


Link to post
Share on other sites
Advertisement
Hello well i don't really do sdl but i can do some openGL

texturing two triangles is pritty much the same as a quad.
you just pass in a few more texturing points and thats about that or
what you can do that might speed up your drawing is use a traingle strip.

this might be abit harder to incorperate in our code depending on how you have it setup. that is if you wanted to draw all of each row at once

or you can just replace the quads with a triangle strip witch is pritty simple to use. and just draw a single square out of it.


but i do not really think thats your problem first off you make a whole lot of calls to enableing GL_TEXTURE_2D, it is only nessiceray to enable the state machien on or off so you only need to call it once in your program and its turned on glDisable(GL_TEXTURE_2D); turns it off.

now the real problem for you i think is the fact that your calling alot of glBegin() and glEnd(); in your appit might be a wise idea to put those in

video->beginRendering();
and endRendering() functions

and one more thing since this is a 2d game you are making you can cull all of the back faces so there not draw just to improve speed abit.


Share this post


Link to post
Share on other sites
Thanks for your help. What I did was made the size of the textures powers of two, which they weren't before, and now it works perfectly. I'll defiantly take a look at your suggestions as well.

So its ok to just call glBegin( GL_QUADS ) at the beginning of rendering, even if I want to do different stuff such as GL_LINE_LOOP?

Share this post


Link to post
Share on other sites
From what I remember you need to call a glBegin for each shape type. So yeah, if you were to switch to GL_LINE_LOOP it need to be in it's own glBegin/glEnd structure. I really don't think calling these is what slowed it down, it was definitely the power of 2 issue.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement