Sign in to follow this  

Simple, n00b SDL question **SOLVED**

This topic is 4402 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

Heya, I am programming with SDL and I have run into a slight speed bump. When I apply a background to my program, I think it is not being repainted per frame... I see a flash of red (plain background bmp, all red) and then it disappears. How do you force a repaint of your background? [Edited by - KingSatchmo on November 28, 2005 8:01:22 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by KingSatchmo
My new problem: Now the dot leaves a trail behind it on the bitmap. How do I fix this?


You will need to clear the screen (in this example, black) before you draw with a call to SDL_FillRect( screen, 0, 0 ) where screen represents the SDL_Surface of your main screen. You could also use SDL_MapRGB(screen->format, R_Value, G_Value, B_Value) instead of the last 0 as well for easier reading of what's going on.

Share this post


Link to post
Share on other sites
Quote:
Original post by Drew_Benton
Quote:
Original post by KingSatchmo
My new problem: Now the dot leaves a trail behind it on the bitmap. How do I fix this?


You will need to clear the screen (in this example, black) before you draw with a call to SDL_FillRect( screen, 0, 0 ) where screen represents the SDL_Surface of your main screen. You could also use SDL_MapRGB(screen->format, R_Value, G_Value, B_Value) instead of the last 0 as well for easier reading of what's going on.



Okay, I semi get what you are saying... but I still do not know where to stick the FillRect function, and what do you mean by clear the screen?

This is what I have now in int main, with the FillRect in the wrong place (it erases the bitmap after the first frame)
Quote:

int main( int argc, char* args[] )
{
//Make sure the program waits for a quit
bool quit = false;

//Make the dot
Ship myShip;

//The frames rate regulator
Timer fps;

//Do the initialization
if( init() == false )
{
return 1;
}

//Load the files
if( load_files() == false )
{
return 1;
}

//While the user hasn't quit
while( quit == false )
{
//Start the frame timer
fps.start();

//While there's an event to handle
while( SDL_PollEvent( &event ) )
{
//Handle events for the dot
myShip.handle_input( event );

//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
}

//Fill the screen white
SDL_FillRect( screen, 0, SDL_MapRGB(screen->format, 0, 0, 0) );

//Show the ship on the screen
myShip.show( screen );

//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}

//Cap the frame rate
while( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
{
//wait
}
}

//Clean up any unneeded data
clean_up();

return 0;
}

Share this post


Link to post
Share on other sites
Ah yes, sorry, you should clear the screen before you draw anything. And youcan use the [source] [/source] tags instead of the [quote] [/quote] to have your text put in a nice white box syntaxed colored [wink]. BUT, you're code looks correct to me, you clear the screen 1st, then draw the ship, and finally flip the screen, and all that takes place after you update. If you want to clear to white though, you need to use:

//Fill the screen white
SDL_FillRect( screen, 0, SDL_MapRGB(screen->;format, 255, 255, 255) );

Black is 0,0,0 and white is 255,255,255. For other colors you can use a paint program perhaps to test out and get the R, G and B components.

So, I'm not sure what's happening, you will need to check over your drawing code for the ship, but what you are after is this:

<update code here>
1. Clear the screen by using SDL_FillRect - fill the screen with a solid color to prevent 'trails'.
2. Draw your stuff here
3. Finally flip the screen.

This is exactly what Lazy Foo does in his tutorial, the line:
//Fill the screen white SDL_FillRect( screen, &screen->clip_rect, 0xFFFFFF ); 'clears the screen' for you, but you can pass in 0 for the second parameter to clear the entire surface.

So the only thing I have left to say is make sure you do not free your bitmap after you draw it! That process should go like this:

1. Load the BMP to a SDL_Surface*
2. Draw the surface as needed tohughout the program
3. At the end of the program before you quit SDL, free the surface with SDL_FreeSurface.

Share this post


Link to post
Share on other sites

This topic is 4402 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.

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