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

[SDL] MouseEvent problem?

Started by peterthegreat Aug 4, 2006 at 2:54 PM 3 replies 1.2k views
Original Post
peterthegreat
peterthegreat
This is actually my first message/thread at GameDev so be nice. ;) I'm curently working with a simple "menu" in SDL just to get me started with all the programming again. My "menu" is suppose to work like a standard pop-down menu like in any other program or OS. Problem: I want to be able to press my menu-button so it gets highlighted(two different pictures). The problem is when I first got it highlighted I can't press it again to non-highlight it. Part of the Code: //image is a SDL_Rect. "menu1.jpg" is the highlighted picture while "menu0.jpg" is the opposite. for(int x=image.x; x<image.w; x++) { for(int y=image.y; y<image.h; y++) { if(event.button.x==x && event.button.y==y && event.button.button==1 && event.button.state==SDL_PRESSED && menucheck==0) { DrawImage(screen, "menu1.jpg", image); SDL_Flip(screen); if(event.button.state==SDL_RELEASED)//The problem seems to be here because menucheck refuses to be changed to '1' so the second if-sentence can start. I've tried with "else if" to. menucheck=1; } if(event.button.x==x && event.button.y==y && event.button.button==1 && event.button.state==SDL_PRESSED && menucheck==1) { DrawImage(screen, "menu0.jpg", image); SDL_Flip(screen); if(event.button.state==SDL_RELEASED) menucheck=0; } } }
Japster
Japster
Well... the line if(event.button.state==SDL_RELEASED) {...} block is inside the if(event.button.x==x && event.button.y==y && event.button.button==1 && event.button.state==SDL_PRESSED && menucheck==0) {...} block. That means it gets executed only if event.button.state==SDL_PRESSED.

And also, it's really slow to check every pixel of the button if the cursor would be on it. The correct way to do that is to check that the cursor coordinates are inside the rectangle, like this:

if(event.button.x >= image.x && event.button.y >= image.y && event.button.x < image.x+image.w
&& event.button.y < image.y + image.h)

And it looks like your DrawImage function loads the image every time from the disk when it's drawn. This is slow. You should load it once, store it's SDL_Surface and use it to draw the picture, not read it from the file every time you draw it.
peterthegreat
peterthegreat
Quote:
Original post by Japster
Well... the line if(event.button.state==SDL_RELEASED) {...} block is inside the if(event.button.x==x && event.button.y==y && event.button.button==1 && event.button.state==SDL_PRESSED && menucheck==0) {...} block. That means it gets executed only if event.button.state==SDL_PRESSED.



That I know, the problem is that the picture draws on the screen, but "if(event.button.state==SDL_RELEASED)" that comed right after just won't.

What I wanna do is a regular button which should be able to be pushed on several times without any problems. I'm able to push it ones but because of this tiny little problem i'm not able to push it again.


Thanx for the other tips. They rocked.
Japster
Japster
Well, that's obviously because the menucheck=1; won't never be executed:
Look at that code again, your code has this kind of a problem:
if(... && event.button.state == SDL_RELEASED && ...) { //Because we are inside this IF, event.button.state MUST be SDL_RELEASED if(event.button.state == SDL_PRESSED) {  menucheck=1;  //This will NEVER be executed, how could event.button.state be SDL_PRESSED  //if it just was SDL_RELEASED? }}

Here is some code that should work:
//Check if the mouse is inside the buttonif(event.button.x >= image.x && event.button.y>=image.y && event.button.x <= image.x + image.w && event.button.y <= image.y + image.h){ //Ok, the cursor is inside the button  if(event.button.button==1)  { //And now we know the event is about the correct mouse button     if(event.button.state == SDL_RELEASED && menucheck == 1)     {      //Draw the not-pressed      DrawImage(screen, "menu0.jpg", image);      menucheck = 0;    }    else if(event.button.state == SDL_PRESSED && menucheck == 0)     { //CLICKED!      //Draw the pressed image      DrawImage(screen, "menu1.jpg", image);      menucheck = 1;    }  }}
peterthegreat
peterthegreat
Thx for all the help and tips but I've found out how it was supposed to be done.

I just used "event.button.state=SDL_RELEASED" after if(menucheck==)

Topic Locked

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

Sign in to reply to this topic.