Sign in to follow this  

SDL Event Problem...... again

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

Hello everyone. Once again I seem to be having issues with events in my SDL application. I am attempting to determine if a user has pushed the mouse down in a specific location on the screen. The problem I am having is that every time I push the mouse down I get the previous mouse down's event. Here is the relative code

void gameLogic()
{	
	SDL_Event event;
		
	SDL_BlitSurface(background,NULL, screen,&rcBackground);

	while(SDL_PollEvent(&event))
	{
		switch(event.type)
		{
			case SDL_QUIT:	
				quit = true;
				break;
			case SDL_MOUSEBUTTONUP:
				if (event.button.x > 282 && event.button.x < 398 && event.button.y > 230 && event.button.y < 328)
					printf("I'm in Position 1!!! ");
				else if (event.button.x > 420 && event.button.x < 575 && event.button.y > 230 && event.button.y < 328)
					printf("I'm in Position 2!!! ");	
				else if (event.button.x > 600 && event.button.x < 753 && event.button.y > 230 && event.button.y < 328)
					printf("I'm in Position 3!!! ");	
				break;
			case SDL_MOUSEBUTTONDOWN:
				printf("Mouse Down At X: %d, Y: %d\n",event.button.x,event.button.y);
				break;						
			case SDL_KEYDOWN:
				switch(event.key.keysym.sym)
				{
					case SDLK_ESCAPE:
						quit = true;
						break;
				}
		}
	}
}

int main(int argc, char* argv[])
{
	/* initialize video system */
	if (SDL_Init(SDL_INIT_VIDEO) < 0)
	{
		printf("Unable to initalize SDL. Quitting\n");
		return -1;
	}
	
	/* create window */
	screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0,SDL_ANYFORMAT | SDL_DOUBLEBUF);

	loadGraphics();
	
	SDL_ShowCursor(SDL_DISABLE);
	
	while (quit == false)
	{
		gameLogic();	
	}

	cleanUp();
	return 0;                    	
}



Hopefully if I can figure this out I will have a better understanding of SDL events. Thanks again. [Edited by - SigmaX on March 2, 2006 7:40:24 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by SigmaX(...)I am attempting to determine if a user has pushed the mouse down in a specific location on the screen.(...)


Hi, I'm not sure if I understand exactly what you meant and what's going on that you didn't expect, but if you want to this you will have to change the SDL_MOUSEBUTTONUP into SDL_MOUSEBUTTONDOWN. The SDL_MOUSEBUTTONUP event will be generated when the user releases the button he /she pressed I believe (as is useful for drag and drop for example).

Share this post


Link to post
Share on other sites

case SDL_MOUSEBUTTONDOWN:
if (event.button.x > 282 && event.button.x < 398 && event.button.y > 230 && event.button.y < 328)
printf("I'm in Position 1!!! ");
else if (event.button.x > 420 && event.button.x < 575 && event.button.y > 230 && event.button.y < 328)
printf("I'm in Position 2!!! ");
else if (event.button.x > 600 && event.button.x < 753 && event.button.y > 230 && event.button.y < 328)
printf("I'm in Position 3!!! ");
break;
case SDL_MOUSEBUTTONUP:
printf("Mouse Up At X: %d, Y: %d\n",event.button.x,event.button.y);
break;




Is this what you mean?

Share this post


Link to post
Share on other sites
Hey, sorry I thought my issue was a little vague. I'm making a Tic Tac Toe game, and my input device is a touch screen. I want the user to be able to drag their finger across the screen then when they decide where they want to move they can lift their finger. That's why I'm using the MouseUp event.

The issue I'm having is that if I touch the first position, nothing is displayed on the console. If I then touch the second position, the text for the first position is displayed. Again if I touch anywhere else on the screen I will recieve the text for the second position.

I hope this clears things up a bit. Thanks.

Share this post


Link to post
Share on other sites
Add a newline character to the end of your printf statements. The strings are likely not being written to the console at the precise time you are calling the functions. If that doesn't fix it, it should at least give you better clues as to what is going and when. A post I just read in general programming about buffered output with printf is what made me think of this. This might be your problem.

[Edited by - nicksterdomus on March 2, 2006 4:06:25 PM]

Share this post


Link to post
Share on other sites
It looks like your 'SDL_KEYDOWN' case is missing the corresponding 'break'. But this does not look like the source of your problem.

I was able to compile a test case with the code you provided and the detection worked perfectly. The missing 'break' didn't matter. I also tested a scenario using the printfs and original code for the SDL_MOUSEBUTTONUP case and it still worked perfectly.

Because I don't have a touch-screen, I cannot really duplicate what is going on. It seems your code is correct but SDL is not:(

The implementation of SDL_MOUSEBUTTONUP/DOWN may be broken for your platform and events are being missed somewhere.Make sure you have the latest SDL libraries.

Here is the test code I used.

//Compiled with Borland C[plus-plus] 5.02
//Window XP
//Uses the SDL_gfx Library for font and rect rendering
//Tests SDL_MOUSEBUTTON*
#include "SDL.h"
#include "sdl_gfxprimitives.h"
#include "sdl_gfxprimitives_font.h"
#include "herrors.h"

static bool quit = false;
static SDL_Surface* screen = NULL;
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
//x,y,w,h
const SDL_Rect pos1={282,230,398-282,328-230};
const SDL_Rect pos2={420,230,575-420,328-230};
const SDL_Rect pos3={600,230,753-600,328-230};
//
bool PtHitRect(const int x,const int y,const SDL_Rect& rc)
{
const int x2 = rc.x + rc.w;
const int y2 = rc.y + rc.h;

if(x < rc.x || x >= x2)return false;
if(y < rc.y || y >= y2)return false;

return true;
}

void GfxPrintString(const char* str,int x,int y)
{
stringRGBA(screen,x,y,str,255,255,255,255);//rgba - pure white
//printf("%s --\n",str);
}

void drawRect(const SDL_Rect& rc)
{
const int x2 = rc.x + rc.w;
const int y2 = rc.y + rc.h;
rectangleRGBA(screen,rc.x,rc.y,x2,y2,255,255,255,255); //rgba-pure white
}

void gameLogic()
{
SDL_Event event;

//SDL_BlitSurface(background,NULL, screen,&rcBackground);

while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
quit = true;
break;
case SDL_MOUSEBUTTONUP:
if (PtHitRect(event.button.x,event.button.y,pos1))
GfxPrintString("I'm in Position 1!!! ",0,0);
else if (PtHitRect(event.button.x,event.button.y,pos2))
GfxPrintString("I'm in Position 2!!! ",0,40);
else if (PtHitRect(event.button.x,event.button.y,pos3))
GfxPrintString("I'm in Position 3!!! ",0,80);
break;
case SDL_MOUSEBUTTONDOWN:
TRACE("Mouse Down At X: %d, Y: %d\n",event.button.x,event.button.y);
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE:
quit = true;
break;
}
break; //ADD MISSING BREAK
} //end switch
} //end while
} //end function

int main(int argc, char* argv[])
{
InitDebugFile();
/* initialize video system */
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("Unable to initalize SDL. Quitting\n");
CloseDebugFile();
return -1;
}

/* create window */
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0,SDL_ANYFORMAT | SDL_DOUBLEBUF);

//loadGraphics();
gfxPrimitivesSetFont(NULL,-1,-1);//set default font
//SDL_ShowCursor(SDL_DISABLE);

while (quit == false)
{
gameLogic();
drawRect(pos1);
drawRect(pos2);
drawRect(pos3);
SDL_Flip(screen);
}

//cleanUp();
gfxPrimitivesSetFont(NULL,-1,-1);//release font surfaces
SDL_Quit();

CloseDebugFile();
return 0;
}







[Edited by - Jack Sotac on March 2, 2006 4:32:41 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by nicksterdomus
Add a newline character to the end of your printf statements. The strings are likely not being written to the console at the precise time you are calling the functions. If that doesn't fix it, it should at least give you better clues as to what is going and when. A post I just read in general programming about buffered output with printf is what made me think of this. This might be your problem.


Well that did it. I can't believe it.
Atleast I don't have to feel like an idiot now. I thought there was something wrong with my logic. And now I know there is a buffered output with printf querk.

Thank you everyone who tried to help out. Now to finish my game!

Share this post


Link to post
Share on other sites

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