Advertisement

SDL Events

Started by October 26, 2004 06:00 AM
5 comments, last by Narusegawa 19 years, 10 months ago
I'm fairly new to SDL but have been reading the Help file with it and gonig through tutorials at the same time. And starting to experiment.

	while(isRunning == true)
	{
		SDL_PollEvent(&event);
		if (event.type == SDL_KEYDOWN)
		{
			printf("Oh! Key Press.\n");
			if(event.key.keysym.sym == SDLK_ESCAPE)
			{
				printf("Quiting SDL.\n");
				isRunning = false;
			}		

			if(event.key.keysym.sym == SDLK_m)
			{
				printf("Toggling Mouse.\n");
                int isMouse = SDL_ShowCursor(SDL_QUERY);
                printf("%d",isMouse);
                if (isMouse == SDL_ENABLE)
                {
       				printf("Disable Mouse.\n");
                    SDL_ShowCursor(SDL_DISABLE);
                } else {
       				printf("Enable Mouse.\n");
                    SDL_ShowCursor(SDL_ENABLE);
                }
			}

		}


When I press the escape key the program exits. As should. And my log file looks like:

Initializing SDL.
SDL initialized.
Oh! Key Press.
Quiting SDL.
Quiting....
When I press m and then escape my log file looks like:

Initializing SDL.
SDL initialized.
Oh! Key Press.
Toggling Mouse.
isMouse = 1.
...
...
...
Oh! Key Press.
Toggling Mouse.
isMouse = 1.
Quiting SDL.
Quiting....
Only it's actually a 4mb file! Plus the cursor doesn't toggle. I can't see what I'm doing wrong, it looks perfectly logical to me the code. [Edited by - Narusegawa on October 28, 2004 8:56:43 AM]
you might need to put a pause before the SDL_PollEvent, it may be registering multiple key presses because it polled more than one time before the key is released.
Advertisement
no thats not the case, an event is only created when a state is changed, eg. m is pressed, m is released, that is 2 events, not m is still down.

but you may wish to use SDL_WaitEvent(&event); which will only progress onto the rest of your code when an new event is in the event queue

actually that is where your problem lies, you poll for a new event but there is none, so it uses the info from the last event (as the variable event wasn't cleared), and therefore, registers m being pressed until a new event is created.
Thanks Anon! That works.

"SDL_PollEvent -- Polls for currently pending events."

I thought that once you'd done something with an event it'd be taken from the queue.
You should always check the return value of SDL_PollEvent. If it returns 0, the SDL_Event structure you passed to it doesn't get filled in but still contains the last event (which in your case is the key down event of the 'm' key).
the event is removed from the queue when poll event gives it to you, however, it doesn't remove it from your event variable
Advertisement
Thanks, I've now got the following in my main loop.

	while(isRunning == true)	{        SDL_Event event;        while(SDL_PollEvent(&event))        {            if (event.type == SDL_KEYDOWN)            {                printf("Oh! Key Press.\n");    			if(event.key.keysym.sym == SDLK_ESCAPE)    			{    				printf("Quiting SDL.\n");    				isRunning = false;    			}		        			if(event.key.keysym.sym == SDLK_m)    			{    				printf("Toggling Mouse.\n");                    int isMouse = SDL_ShowCursor(SDL_QUERY);                    if (isMouse == SDL_ENABLE)                    {           				printf("Disable Mouse.\n");                        SDL_ShowCursor(SDL_DISABLE);                    } else {           				printf("Enable Mouse.\n");                        SDL_ShowCursor(SDL_ENABLE);                    }    			}    			if(event.key.keysym.sym == SDLK_d)    			{                    DrawTile(0,0);                    DrawTile(0,1);                    DrawTile(1,0);                    DrawTile(1,1);                    SDL_Flip(screen);    			}		     }	    		if (event.type == SDL_QUIT)    		{    			printf("Quiting SDL.\n");    			isRunning = false;    	   	}                        printf("Active for %.2f seconds at %.2f fps.\n", ((float) SDL_GetTicks()/1000), (100/((float) SDL_GetTicks()/1000)));				          }	}

This topic is closed to new replies.

Advertisement