Jump to content
  • Advertisement
Sign in to follow this  
Adam4444

Problem with a menu for a game

This topic is 4196 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. I got some problem with a menu for my new game project. The menu consists of some text stored in SDL surfaces, and when somebody click on one of those options, the function I'm writing on should return the identifier for that option. The function looks like this:
int menu_get_option(void)
{
    SDL_Event event;
    int i;
    
    while (1) {
        if (SDL_PollEvent(&event) != 0) {
            if (event.type == SDL_MOUSEBUTTONDOWN) {
                for (i = 0; i < MENU_NUM_OPTIONS; i++) {
                    if (event.button.y >= menu_options_surfaces.y &&
                        event.button.y <= menu_options_surfaces.y + menu_options_surfaces.surface->h &&
                        event.button.x >= menu_options_surfaces.x &&
                        event.button.x <= menu_options_surfaces.x + menu_options_surfaces.surface->w) {
                        printf("%d\n", i);
                        /*return i;*/
                    }
                }
            }
        }
        else if (event.type == SDL_QUIT) {
                return MENU_OPTION_EXIT;
        }
    }  
    
    return 0;
}

The problem is that when I click on the first option, 1 is printed out. And on the second, 2, the third, 3, the fourth 4 and the fifth nothing happens. It should print 0, when I click on the first option etc. Anybody got an idea what I'm doing wrong? (Gah, I have programmed too many hours in row. So it's probably a silly mistake. :P) Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
OK, here I initialize the menu option surfaces:
void menu_show(void)
{
int i;
TTF_Font *font, *heading_font;
SDL_Surface *menu_bg_surface;
SDL_Surface *menu_heading;
SDL_Color text_color;
SDL_Rect src_rect, dest_rect;

/* Load the fonts. */
font = TTF_OpenFont(MENU_FONT, MENU_FONT_SIZE);
if (font == NULL) {
fprintf(stderr, "Couldn't load font.\n");
exit(1);
}
heading_font = TTF_OpenFont(MENU_HEADING_FONT, MENU_HEADING_FONT_SIZE);
if (heading_font == NULL) {
fprintf(stderr, "Couldn't /* Generate text for the menu options. */load heading font.\n");
exit(1);
}

/* Load and blit the background. */
menu_bg_surface = SDL_DisplayFormat(IMG_Load(MENU_BACKGROUND_IMAGE));
if (menu_bg_surface == NULL) {
fprintf(stderr, "Couldn't load the menu background image.\n");
exit(1);
}
src_rect.x = src_rect.y = dest_rect.x = dest_rect.y = 0;
src_rect.w = dest_rect.w = menu_bg_surface->w;
src_rect.h = dest_rect.h = menu_bg_surface->h;
SDL_BlitSurface(menu_bg_surface, &src_rect, display_surface, &dest_rect);

/* Generate the heading text. */
text_color.r = text_color.g = text_color.b = 0;
menu_heading = SDL_DisplayFormat(TTF_RenderText_Solid(heading_font,
MENU_HEADING,
text_color));
src_rect.w = dest_rect.w = menu_heading->w;
src_rect.h = dest_rect.h = menu_heading->h;
dest_rect.x = (WIDTH - menu_heading->w) / 2;
dest_rect.y = 40;
SDL_BlitSurface(menu_heading, &src_rect, display_surface, &dest_rect);

/* Generate text for the menu options. */
dest_rect.y = 70;
for (i = 0; i < MENU_NUM_OPTIONS; i++) {
menu_options_surfaces.surface = SDL_DisplayFormat(
TTF_RenderText_Solid(font,
menu_options,
text_color));
if (menu_options_surfaces.surface == NULL) {
fprintf(stderr, "Couldn't generate text fot the menu options.\n");
exit(1);
}
src_rect.w = dest_rect.w = menu_options_surfaces.surface->w;
src_rect.h = dest_rect.h = menu_options_surfaces.surface->h;
dest_rect.x = menu_options_surfaces.x = (WIDTH - menu_options_surfaces.surface->w) / 2;
menu_options_surfaces.y = dest_rect.y;
dest_rect.y += menu_options_surfaces.surface->h + 10;
SDL_BlitSurface(menu_options_surfaces.surface, &src_rect, display_surface, &dest_rect);
}
SDL_UpdateRect(display_surface, 0, 0, 0, 0);
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!