Sign in to follow this  

need help on sdl cursor

This topic is 2541 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 got little problem when changing mouse cursor inside my game, here is my code,

if i use this code when i enter help page my cursor's image stop at one place since 'if' not inside while.
if i put cursor inside while ( only cursor and flip), the cursor are moving but had trail and those trail won't dissapear.
if i put both help screen and cursor inside while (no if(quit==false){}) cursor move just like what i want but the help screen seems to be rewrite again and again and leave black mark beside help box and my help box aren't transparent anymore, (see picture).

help please :)


void Button::HelpClick()
{

bool endHelp = false;

if( quit == false )
{
//apply the back ground

apply_surface( 0, 0, otherBG,screen );

SDL_GetMouseState(&mouseX, &mouseY);

apply_surface( mouseX , mouseY , cursor , screen , Rcurs );
//update_screen();
SDL_Flip( screen );

// Draw the string 'help' to the center of the screen, position: y
drawString(screen,font1,325,45,helpjudul);
drawString(screen,font2,30,110,helpisi);


// Draw the webspace url to the bottom-right of the screen
drawString(screen,font3,800,730,"Untuk Keluar Tekan ESCAPE");

SDL_GetMouseState(&mouseX, &mouseY);

apply_surface( mouseX , mouseY , cursor , screen , Rcurs );


//update_screen();
SDL_Flip( screen );
}


//while the help is still going
while( ( endHelp == false ) && ( quit == false ) )
{
//if there's events to be handled
if( SDL_PollEvent( &event ) )
{
//if a button was pressed
if( event.type == SDL_KEYDOWN )
{
//if esc was pressed
if( event.key.keysym.sym == SDLK_ESCAPE )
{
//quit the game
endHelp = true;
}

//end the help
}
//if the user Xed out
if( event.type == SDL_QUIT )
{
//quit the game
quit = true;
}
}

}
}



"http://img600.imageshack.us/i/cursorproblem.png/"

Share this post


Link to post
Share on other sites
You must understand that with SDL you are drawing onto surfaces - in your case, a surface called screen. When you call SDL_Flip( screen ) you are putting this surface onto your SDL window for that frame.

Before the next screen you must 'clear' the surface; this is usually accomplished by blitting a background image or a solid color.

So, I looked at your code. It seems your objective is to draw your help, draw your cursor, and then gather inputs. Your while loop handles when the user of the program goes into help i.e. you stay in this loop until help is exited. Also, it is important to note that a mouse cursor can (I assume) go any where with in your SDL window. This implies that any position in the window can go 'dirty' - need to be redrawn so you do not see the trail effect - so you prolly wouldn't be bad off just redrawing everything each frame.

Your while loop should look like:


while( ( endHelp == false ) && ( quit == false ) )
{
HandleEvents();
DrawBackground(); // 'clear' the screen.
DrawHelp(); // draw all help images, text, etc.
DrawMouseCursor(); // draw the mous cursor on top of everything else.
}


So, break things up into these functions to make things clean and easy to see. =)

Your HandleEvent function would be, for example:


//if there's events to be handled
if( SDL_PollEvent( &event ) )
{
//if a button was pressed
if( event.type == SDL_KEYDOWN )
{
//if esc was pressed
if( event.key.keysym.sym == SDLK_ESCAPE )
{
//quit the game
endHelp = true;
}

//end the help
}
//if the user Xed out
if( event.type == SDL_QUIT )
{
//quit the game
quit = true;
}




And so on. You already know how to draw your help so put it inside the DrawHelp() function; clear the screen in the DrawBackground() function. And lastly, your DrawMouseCursor() function would look something like


void DrawMouseCursor()
{
SDL_GetMouseState(&mouseX, &mouseY);
apply_surface( mouseX , mouseY , cursor , screen , Rcurs );
}



So yeah, your while loop is the problem. How's this sound?

Share this post


Link to post
Share on other sites
like this?


void Button::HelpClick()
{
//make sure to start the intro
bool endHelp = false;

while( ( endHelp == false ) && ( quit == false ) )
{
HandleEvents();
DrawBackground(); // 'clear' the screen.
DrawHelp(); // draw all help images, text, etc.
DrawMouseCursor(); // draw the mous cursor on top of everything else.
}
}

void HandleEvents()
{
if( SDL_PollEvent( &event ) )
{
//if a button was pressed
if( event.type == SDL_KEYDOWN )
{
//if esc was pressed
if( event.key.keysym.sym == SDLK_ESCAPE )
{
//quit the game
endHelp = true;
}

}
//if the user Xed out
if( event.type == SDL_QUIT )
{

quit = true;
}
}
}

void DrawBackground()
{
apply_surface( 0, 0, otherBG,screen );
}

void DrawHelp()
{
drawString(screen,font1,325,45,helpjudul);
drawString(screen,font2,30,110,helpisi);
drawString(screen,font3,800,730,"Untuk Keluar Tekan ESCAPE");
}

void DrawMouse()
{
SDL_GetMouseState(&mouseX, &mouseY);

apply_surface( mouseX , mouseY , cursor , screen , Rcurs );

update_screen();
SDL_Flip( screen );
}

Share this post


Link to post
Share on other sites

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