Original Post
Hey
I have been learning some SDL and have created a very basic app which has a single button which once clicked exit's the program.
But i don't know how to scale it to have multiple buttons and check which button is pressed. So i need some guidance on how i can alter my attempt to handle multiple buttons simplistically.
My current method is as follows [i cut out most of the code to just show the relevant stuff:
[source lang="cpp"]
bool handle_mouse_leftClick(int x, int y, SDL_Surface *button) {
if( ( ( mouseX > x ) && ( mouseX < x + button->w ) ) && ( ( mouseY > y ) && ( mouseY < y + button->h ) ) ) {
return true;
} else {
return false;
}
}
// beyond this line is inside my main function
//menu button
SDL_Surface *button;
button = IMG_Load("button.png");
while(!quit){
//handle events
while( SDL_PollEvent( &event ) ){
switch(event.type){
case SDL_QUIT: quit = true; break;
case SDL_MOUSEMOTION: handle_mouse_position(); break;
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_LEFT) {
if(handle_mouse_leftClick(btnx,btny,button)){
quit = true;
}
}
break;
}
}
[/source]
The problem is im not following how i can make it work to cater for any button i make for the game menu ? Unless some kind of very long if:else statements?
I have been learning some SDL and have created a very basic app which has a single button which once clicked exit's the program.
But i don't know how to scale it to have multiple buttons and check which button is pressed. So i need some guidance on how i can alter my attempt to handle multiple buttons simplistically.
My current method is as follows [i cut out most of the code to just show the relevant stuff:
[source lang="cpp"]
bool handle_mouse_leftClick(int x, int y, SDL_Surface *button) {
if( ( ( mouseX > x ) && ( mouseX < x + button->w ) ) && ( ( mouseY > y ) && ( mouseY < y + button->h ) ) ) {
return true;
} else {
return false;
}
}
// beyond this line is inside my main function
//menu button
SDL_Surface *button;
button = IMG_Load("button.png");
while(!quit){
//handle events
while( SDL_PollEvent( &event ) ){
switch(event.type){
case SDL_QUIT: quit = true; break;
case SDL_MOUSEMOTION: handle_mouse_position(); break;
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_LEFT) {
if(handle_mouse_leftClick(btnx,btny,button)){
quit = true;
}
}
break;
}
}
[/source]
The problem is im not following how i can make it work to cater for any button i make for the game menu ? Unless some kind of very long if:else statements?