Skip to main content
GameDev.net gamedev.net
🔒 Locked

SDL Mouse Down

Started by Hatty Jan 29, 2007 at 9:47 PM 2 replies 3.2k views
Original Post
Hatty
Hatty
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]
DMINATOR
DMINATOR
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.
Hatty
Hatty
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.
gumpy
gumpy
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;}
This space for rent.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.