• Advertisement
Sign in to follow this  

SDL CreateCompatible Surfaces performance hit

This topic is 4675 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 am currently working on my first 'game'. I am dabbling through SDL, and I'm using the SDlDotNet library, but all you C++ die-hards should be able to read it(I learned it by look at C++ examples). I have a performance hit in this function, I'm absolutely sure it's this function, because I commented it out and there was no problem. Please see if you can help me find it.
private void Render()
    {
        if(state == GameState.Menu)
	{
	    _Background = (new Surface(resourcepath + "//titlescreen.bmp")).Convert();
	    Button playbutton = new Button(288, 360, 64, 32, GameEngine.resourcepath + "//PlayButton.bmp");
					
	    playbutton.OnClick += new ButtonEventHandler(MouseEvents);

	    _Background.Blit(playbutton.surf, new Rectangle(playbutton.X, playbutton.Y, playbutton.Width, playbutton.Height));

	    _Screen.Blit(_Background, new Rectangle(new Point(0,0), _Background.Size));
	    _Screen.Flip();
	}
	if(state == GameState.Pregame)
	{}
	if(state == GameState.Ingame)
	{
	    _Background = new Surface(resourcepath + "//background.bmp");
	    //_BackBuff = _Screen.CreateCompatibleSurface(640, 480, true);
	    _Background.Blit(_Background, new Point(0,0));
	    _Background.Blit(player.Surf, player.Location);

	    _Screen.Blit(_Background, new Point(0,0));
	    _Screen.Flip();
	}
}


Oh, and I think it's in both the Ingame and Menu parts.

Share this post


Link to post
Share on other sites
Advertisement
Now I am no C# expert, but that is a lot of new's I see in there. I think the problem is that you are allocating memory every frame. This would cause the performance problems you are describing. What you should try is making the variables before hand and loading all the settings so you are not always creaing new ones. Just one suggestion [wink]

Share this post


Link to post
Share on other sites
Yea, I thought of that too, but the program works good for about 15 seconds then, it starts having performance spikes like a heartbeat almost. Maybe it's alive ^_^! I was going to change it, but I couldn't think of a good way, but I just now thought of making an event fire when the game state changes, and I could change the _Background surface in that.

Share this post


Link to post
Share on other sites
You're adding new memory each frame, which mean's C# garbage collection is eventually going to start swapping new memory for old memory each loop. You may even be hitting a hardware memory max, which mean's new memory will be allocated on your harddrive as virtual memory.

Share this post


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

  • Advertisement