🎉 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!

SDL speed?

Started by
18 comments, last by swiftcoder 19 years, 10 months ago
Hey, I recently tried SDL, so far I like it, it's a lot easier than DirectX and OpenGL and it has a lot of nice stuff.. But now I just implemented a FPS counter in my system, and it seems that my FPS goes no higher than 210. Even when I'm not drawing anything on the screen, just filling the screen surface black.. My computer is quite good ( Athlon 3200+, 2x256mb dual channel ram 400mhz, Radeon 9800 pro 128mb ) and usually, with that kind of game where there is pratically nothing, I get much higher fps ( 1000-2000 ) when the game was using OpenGL or DirectX. Here's my code, I'll only show the important parts so it is quite small, I'd like to know if there's something wrong which is slowing everything down.

//Init
void Init()
{
	SDL_Init(SDL_INIT_VIDEO)
	m_MainSurface=SDL_SetVideoMode( 800, 600, 32, SDL_DOUBLEBUF|SDL_HWACCEL|SDL_HWSURFACE)
}

//..My game loop
while(m_Quit==false)
{
	XTime::OnFrame();
	SDL_Event event;
	while(SDL_PollEvent(&event))
	{
		switch( event.type )
		{
		case SDL_QUIT:
			m_Quit=true; break;
		}
	}
	if(RunGame()==false)
		m_Quit=true;
}
//..

//RunGame() fonction:
bool RunGame()
{
	SDL_Flip( m_MainSurface );
	SDL_FillRect( m_MainSurface, 0, 0 );
	return true;
}






I know 200 fps is not dramatically low, but I am scaried that it could get too low too fast and that it could run slow on weaker computers. Thanks. [Edited by - Hedos on August 7, 2004 11:47:45 AM]
Advertisement
Quote: Original post by Hedos
my FPS goes no higher than 210.
Even when I'm not drawing anything on the screen,
Isn't there a FAQ on this?

Does Not Matter.
First off you're not suppose to flip the screen buffer prior to rendering. You should flip the screen last. Also your video mode may be the primary reason as to why your FPS is so low.

SDL_ANYFORMAT.......Why use this video flag if you already know what format you're using? Also you can't just use hardware acceleration freely in SDL you must check if your hardware supports acceleration before you could actually use it with SDL.

There are more problems with your source code but I don't have time to go over it all :)
C-Junkie: I'm not sure if you were trying to say that there is a FAQ answering my question somewhere, but I have found nothing useful on SDL website.

Trigger9k: Yeah, SDL_ANYFORMAT shouldn't have been there, sorry.
As for the order of flipping the screen, well, it doesn't really matter, that way I have to blit everything I want after the SDL_FillRect call..

Any tip on how to force a surface to be in hardware mode?
I checked the flags of my screen surface after its creation and SDL_HWSURFACE bit is not set :/
*sigh*

void Render( ){   // Clear the screen   SDL_FillRect( m_MainSurface, 0, SDL_MapRGB( m_MainSurface->format, 0, 0, 0 ) );   // Draw all of your crap   m_Level.Render( );   m_Player.Render( );   m_Enemy.Render( );   // Flip the screen buffer   SDL_Flip( m_MainSurface );}// Lord knows why your rendering function returns.....


When you commence actual game coding you'll realize that the order is crucial. For this small example(all you did was clear the screen to black) it doesn't matter.

Quote: Original post by Trigger9k
*sigh*

*** Source Snippet Removed ***

When you commence actual game coding you'll realize that the order is crucial. For this small example(all you did was clear the screen to black) it doesn't matter.



No, his would work... he would just have to draw everything before he flips.
Not giving is not stealing.
Quote: Original post by Trigger9k
*sigh*

*** Source Snippet Removed ***

When you commence actual game coding you'll realize that the order is crucial. For this small example(all you did was clear the screen to black) it doesn't matter.


Alright, what about this exemple?
void OnFrame( ){   // Draw all of your crap   m_Level.Render( );   m_Player.Render( );   m_Enemy.Render( );   // Flip the screen buffer   Graphics::Flip();}void Graphics::Flip(){   SDL_Flip( m_MainSurface );   SDL_FillRect( m_MainSurface, 0, 0 );}


I don't see anything wrong with this, it's the way I do it and in the source code provided in my first post, I removed all the other code in the rendering part of my game, so well I guess it looked like I didn't know what I was doing...
Anyway, yes I have actual game programming experience in OpenGL and DirectX.
Quote:
Original post by thedevdan

No, his would work... he would just have to draw everything before he flips.


That was the point I was trying to make.
Quote: Original post by Trigger9k
Quote:
No, his would work... he would just have to draw everything before he flips.


That was the point I was trying to make.


Anyway, the flipping doesn't really matter since it works fine right now. ( I can blit bitmaps without problems )..

Right now, I'm interested in knowing why wouldn't my screen surface use hardware mode? (SDL_HWSURFACE)
And could it be the cause of my low FPS?

Thanks

edit: It seems that by setting my application to be in fullscreen mode, a hardware surface is used.. This way, I have over 1300 fps.
That's cool, thought I wonder why can't I get hardware surfaces in windowed mode. It is possible in OpenGL to have hardware access in windowed mode and since SDL uses OpenGL, I don't understand what goes wrong :(

Any idea for the windowed mode?
Thanks again

[Edited by - Hedos on August 7, 2004 12:03:29 PM]
Quote: Original post by Hedos
Right now, I'm interested in knowing why wouldn't my screen surface use hardware mode? (SDL_HWSURFACE)
And could it be the cause of my low FPS?


sending this flag does NOT gaurantee that you will get hardware acceleration. in fact, you should just assume that you will never get hardware accel, YOU might, but 80% of your users might not. thats how SDL is, its mostly just a software renderer. personally, i would rely on it not going to hardware mode.


Quote:
edit: It seems that by setting my application to be in fullscreen mode, a hardware surface is used.. This way, I have over 1300 fps.
That's cool, thought I wonder why can't I get hardware surfaces in windowed mode. It is possible in OpenGL to have hardware access in windowed mode and since SDL uses OpenGL, I don't understand what goes wrong :(


actually, on windows, SDL will use Direct Draw to render. i think if your on linux it will use OpenGL, but im not sure. im not sure exactly why fullscreen gave hardware acceleration, or if that was even the case at all. just know that in fullscreen mode your frame-rate should definetly increase. (actually, im not sure, but i could have sworn i read that if your using fullscreen you should use software... go ahead, try it in fullscreen with SOFTWARE and see if you still get the 1300 FPS). this is because your OS gives full power to your game, and doesnt have to draw the background plus your game. theres more to it then that, something about windowed mode not getting full control from the video card, but i dont know exactly. someone here will give the details im sure.

the main thing you have to understand is SDL is primarily a software rendered. if you want lots of speed and hardware acceleration, use OpenGL or d3d. if this is a simple project like pacman or something, which you will only be working on for a month or 2, stick with SDL. if this is a major project you will be working on for the next year, id strongly recommend learning OpenGL. you can still use SDL to take input and create windows, only your rendering code will change. and it shouldnt take very long for you to learn OpenGL, its really not that more complicated to draw a textured quad to the screen rather then an SDL surface.

also, you can check if the user has hardware accel available or not, and then create your window based on that. check the SDL docs for SDL_VideoInfo struct and SDL_GetVideoInfo() function. heres how i do it, this is for use with opengl, but it should be the same way anyway.

if (SDL_Init(SDL_INIT_VIDEO)== -1)		Log("Could not Initialize through SDL_Init!! bailing out!!",FAILURE);	else	{		Log("SDL Initialized Properly!");		atexit(SDL_Quit);	}	 /* this holds some info about our display */     video_info = SDL_GetVideoInfo();  	 if (!video_info)		Log("Could not get video info!!! bailing out!!",FAILURE);	 	 else Log("video info retrieved!!");		/* the flags to pass to SDL_SetVideoMode */    Video_Flags  = SDL_OPENGL;          /* Enable OpenGL in SDL */    Video_Flags |= SDL_GL_DOUBLEBUFFER; /* Enable double buffering */    Video_Flags |= SDL_HWPALETTE;       /* Store the palette in hardware */   // Video_Flags |= SDL_FULLSCREEN;       /* Enable window resizing */    /* This checks to see if surfaces can be stored in memory */    if ( video_info->hw_available )	{		Video_Flags |= SDL_HWSURFACE;		Log("HW Available, surfaces will be stored in memory!!");	}    else	{		Video_Flags |= SDL_SWSURFACE;		Log("Software only available, surfaces wont be stored in video memory!!");	}    /* This checks if hardware blits can be done */    if ( video_info->blit_hw )    {		Video_Flags |= SDL_HWACCEL;		Log("HARDWARE BLITS AVAILABLE!!");    }    else Log("HARDWARE BLITS N/A");	//create the screen	screen = SDL_SetVideoMode(SCREEN_RESW,SCREEN_RESH,video_info->vfmt->BitsPerPixel,Video_Flags); 



EDIT: the main thing you should notice from the code is checking if hardware is available, if so, use hardware, if not, use software.
if ( video_info->hw_available )	{		Video_Flags |= SDL_HWSURFACE;		Log("HW Available, surfaces will be stored in memory!!");	}    else	{		Video_Flags |= SDL_SWSURFACE;		Log("Software only available, surfaces wont be stored in video memory!!");	}
FTA, my 2D futuristic action MMORPG

This topic is closed to new replies.

Advertisement