Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

RanBlade

Using the mouse in SDL

This topic is 5180 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 have been working on a tic tac toe clone for awhile now and have been using the SDL. I have browsed the net for help with this but the links i find link me to the SDL documentation. Which isnt very helpful. My problem is I want to use mouse input to click on the screen where they want to mark there move. But I cant get it to draw it to the screen. I played around with general stuff and got it to draw onto a screen by clicking the mouse. But with stuff already drawn to the screen and now trying to draw this as a game move it wont draw. My program flow goes something like this Start progrm -> enter main message pump -> press F1 to start game(place holder for now to start game) -> Initlize game and draw gameboard -> RunGame function called -> enter game message pump -> player move -> draw X to screen(when I click within the defined coordinates it dosent draw anything)... I have also expanded it to click anywhere on the screen to see if it will draw it to the screen and it dosent. I have my SDL_UpdateRect()'s in the right spots... any input would be great... in the meantime iam gonna mess with it and look around some more thanks.

RanBlade "Passion is what drives you to stay up until 4am fixing that bug that hardly anyone would notice...Passion is where great games come from, if you dont live and breathe games you shouldn't be in the games industry." - Dave Pottinger, Ensemble Studios
[GameDev][C++ Page][Game Tutorials][FreeBSD][HawkNL(Hawk Network Library)][NeHe Productions][Gamedev book's] [Virtually Online-Books][Drunken Hyena][MSDN][Beej's Guide to Network Programming][Mage Tower Ent-My Site][ThunderBox PC] [edited by - RanBlade on June 11, 2004 2:43:39 AM] [edited by - RanBlade on June 11, 2004 2:44:03 AM] [edited by - RanBlade on June 11, 2004 2:44:40 AM]

Share this post


Link to post
Share on other sites
Advertisement
Maybe you''re missing a call to SDL_GL_SwapBuffers( ) ?

Without at least the relevant parts of the source, the question is impossible to answer.

Tom

Share this post


Link to post
Share on other sites
im guessing your running in fullscreen mode? in fullscreen mode, theres problems with using the mouse. im not positive, but i think you have to hide the default mouse and draw your own. either that, or hide the mouse while your drawing and un-hide it anytime else... although i could never get the latter to work for me...

Share this post


Link to post
Share on other sites
Yes its in fullscreen but does the same in non full screen.

and here is some code

This is my main program
// this is the main window for the framework

SDL_Surface* g_window = 0;



int main( int argc, char* argv[] )
{
// declare coordinates.

int x, y;

// declare event holder

SDL_Event event;

// initialize the video system.

SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER );
SDL_WM_SetCaption( PROGRAM_NAME, 0);
SDL_WM_SetIcon(SDL_LoadBMP(ICON_NAME), 0);


// set our at exit function

atexit( SDL_Quit );

// set the video mode.

g_window = SDL_SetVideoMode( WIDTH, HEIGHT, 0, SDL_FULLSCREEN );
DrawIntroScreen(g_window);


//Menu Message pump

while( 1 )
{
//look for an event

if( SDL_PollEvent( &event ) )
{
//an event was found

if( event.type == SDL_QUIT )
break;
if( event.type == SDL_MOUSEBUTTONDOWN)
{

// get the mouse state.

SDL_GetMouseState( &x, &y );

}
if( event.type == SDL_MOUSEBUTTONUP )
{
// get the mouse state.

SDL_GetMouseState( &x, &y );
}
if( event.type == SDL_KEYDOWN )
{
// a key was pressed.

if( event.key.keysym.sym == SDLK_F12 )
{

SDL_Event quit;
quit.type = SDL_QUIT;
SDL_PushEvent( &quit );
}
if(event.key.keysym.sym == SDLK_F1)
{
//Initilize the game

SetGameTable(g_window);

//Enter the Game loop

RunGame(g_window);
}

}
} // end event loop.





// update the entire window.

SDL_UpdateRect( g_window, 0, 0, 0, 0 );
}

// do game cleanup here

// done

return 0;
}


This is inside RunGame(g_window)

void RunGame(SDL_Surface* GameWindow)
{
//setup a event for the game

SDL_Event game;
int x, y;
int turn = 0;
int gameover = 1;


//The Game loop

while(gameover == 1)
{
if(SDL_PollEvent(&game))
{
if(game.type == SDL_QUIT)
break;
if(game.type == SDL_MOUSEBUTTONDOWN)
{
SDL_GetMouseState(&x, &y);
// if(game.button.button == SDL_BUTTON_LEFT)

// {

if(x > 0 && x < 800 && y > 0 && y < 600)
{
Choose(GameWindow, GameChoice[0], turn);
}
// }

}
if(game.type == SDL_KEYDOWN)
{
if(game.key.keysym.sym == SDLK_F12)
{

SDL_Event quit;
quit.type = SDL_QUIT;
SDL_PushEvent( &quit );
}
}
}
}

}

Choose function

void Choose(SDL_Surface* g_window, int choice, int turn)
{


Draw(Xplayer, g_window, 0, 0);
SDL_UpdateRect(g_window, 0, 0, 0, 0);


}

and finnally inse Draw function

void Draw( SDL_Surface* Asurface, SDL_Surface* pSurface, int x, int y)
{
SDL_Rect destination;

destination.x = x;
destination.y = y;

SDL_BlitSurface( Asurface, NULL, pSurface, &destination );
}


Xplayer is defined as
SDL_Surface* Xplayer = SDL_LoadBMP("images/X.bmp");

Hope that code helps... Iam new to SDL so so I could be over looking soemthing very simple. thanks again
EDIT: Fixing source tags
EDIT2: Fixing sig


RanBlade
"Passion is what drives you to stay up until 4am fixing that bug that hardly anyone would notice...Passion is where great games come from, if you dont live and breathe games you shouldn't be in the games industry." - Dave Pottinger, Ensemble Studios

[GameDev][C++ Page][Game Tutorials][FreeBSD][HawkNL(Hawk Network Library)][NeHe Productions][Gamedev book's]
[Virtually Online-Books][Drunken Hyena][MSDN][Beej's Guide to Network Programming][Mage Tower Ent-My Site][ThunderBox PC]

[edited by - RanBlade on June 11, 2004 3:13:05 AM]

[edited by - RanBlade on June 11, 2004 3:15:10 AM]

Share this post


Link to post
Share on other sites
Did you make sure that you are even recieving the mouse input? Maybe its not the drawing code, do what i do and try adding a messagebox to where the click event should happen to see if it captured correctly. Try using the capture code like this in your event loop:


SDL_Event hEvent;

void HandleEvents()
{
SDL_PollEvent(&hEvent);
if(hEvent.button.button==SDL_MOUSEBUTTONDOWN &&
hEvent.button.x==DESIREDX &&
hEvent.button.y==DESIREDY)
{
// do something with the mouse event

}
}


---------------------------------------------
"If the automobile had followed the same development cycle as the computer, a Rolls-Royce would today cost $100, get a million miles per gallon, and explode once a year, killing everyone inside."

Share this post


Link to post
Share on other sites
I got it to work. Thanks for the replies. Hopefully Ill have something playable soon.



RanBlade
"Passion is what drives you to stay up until 4am fixing that bug that hardly anyone would notice...Passion is where great games come from, if you dont live and breathe games you shouldn''t be in the games industry." - Dave Pottinger, Ensemble Studios

[GameDev][C++ Page][Game Tutorials][FreeBSD][HawkNL(Hawk Network Library)][NeHe Productions][Gamedev book''s]
[Virtually Online-Books][Drunken Hyena][MSDN][Beej''s Guide to Network Programming][Mage Tower Ent-My Site][ThunderBox PC]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!