Jump to content
  • Advertisement
Sign in to follow this  

SDL Mouse Down

This topic is 4129 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 writing a SDL Tic Tac Toe game and I'm doing mouse events. I have it set so when I press the left mouse button down, it spits out a message ala console. The only thing is the event happens way too many times when I click the mouse. ie
Quote:
Button down: x=257 y=163! Button down: x=257 y=163! Button down: x=257 y=163! Button down: x=257 y=163! Button down: x=257 y=163! Button down: x=257 y=163! Button down: x=257 y=163! Button down: x=257 y=163! Button down: x=257 y=163! Button down: x=257 y=163!
--Small sample-- when it should be only once:
Quote:
Button down: x=257 y=163!
Is this easily accomplished? My poll event code:
    while (SDL_PollEvent(&event)) {
        switch (event.type) {
            case SDL_QUIT:
                exit(0);
                break;

            case SDL_MOUSEBUTTONDOWN:
                if (event.button.button == SDL_BUTTON_LEFT) {
                    printf("Button down: x=%i y=%i!\n", event.button.x, event.button.y);
                }
                break;
        }
    }

-Hatty [Edited by - Hatty on January 30, 2007 11:31:00 PM]

Share this post


Link to post
Share on other sites
Advertisement
Hi

I think you could make something like trigger. So it would output it only once, just some example straight out of the head:


bool triggered = false;

void TriggerMessage(int x, int y)
{
if( triggered == false )
{
printf("Button down: x=%i y=%i!\n", x , y);
triggered = true;
}
}

//------------ some code here

case SDL_MOUSEBUTTONDOWN:

if (event.button.button == SDL_BUTTON_LEFT)
{
TriggerMessage(event.button.x, event.button.y);
}
break;

case SDL_MOUSEBUTTONUP:
if (event.button.button == SDL_BUTTON_LEFT)
{
triggered = false
}
break;




This would trigger the event and show the message only once. When the mouse button is unpressed the trigger is set to false, so when you press mouse button again next time it shows the message again.

There is another much more easier way is to use mouse up event - which can happen only once by default


case SDL_MOUSEBUTTONUP:
if (event.button.button == SDL_BUTTON_LEFT)
{
printf("Button down: x=%i y=%i!\n", event.button.x, event.button.y);
}
break;



P.S. I haven't tried it but it should work, let me know if you have any problems. One more thing you can put your code inside the source tags which makes it much more easier to read.

Share this post


Link to post
Share on other sites
oh sorry. I used code tags, didnt know this forum used "source". Anyway, MOUSEUP does the same thing, it just waits 'til after I've pressed the button. Thanks anyway.

Share this post


Link to post
Share on other sites

while (SDL_PollEvent(&event) >= 0) {



should be


while ( SDL_PollEvent(&event) ) {




here's some (hopefully functional and error-free) sample code:


/*
* DISCLAIMER:
* i will not be held accountable for any damage or data loss
* as a result of the following program. use this code at your
* own risk.
*
*/


#include <cstdlib>
#include <iostream>

#include "SDL/SDL.h"

int main(int argc, char *argv[] ) {
if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
std::cerr << SDL_GetError() << std::endl;
exit( EXIT_FAILURE );
}
if( !SDL_SetVideoMode( 800, 600, 0, SDL_SWSURFACE | SDL_DOUBLEBUF ) ) {
std::cerr << SDL_GetError() << std::endl;
SDL_Quit();
exit( EXIT_FAILURE );
}
bool done = false;
while( !done ) {
SDL_Event e;
while(SDL_PollEvent(&e)) {
switch(e.type) {
case SDL_MOUSEMOTION:
break;
case SDL_KEYDOWN:
if(e.key.keysym.sym == SDLK_ESCAPE) {
done = true;
}
break;
case SDL_QUIT:
done = true;
break;
default:
break;
}
}
SDL_Flip( SDL_GetVideoSurface() );
}
SDL_Quit();
return EXIT_SUCCESS;
}


Share this post


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

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!