/*
 * OpenKropki menus (main, settings, language, about, play setup)
 *
 * Copyright (C) 2014-2026 Mateusz Viste
 *
 * MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <stdio.h>
#include <string.h>           /* memset(), snprintf() */

#include "drv_gra.h"
#include "lang.h"
#include "menu.h"
#include "ui.h"
#include "version.h"
#include "media/data.h"

/* menu caps scale with the (auto-sized) window, but stay bounded */
#define MENU_MAXWIDTH_CAP  1350
#define MENU_MAXHEIGHT_CAP 900


static int get_menu_maxwidth(void) {
  int w;
  gra_getwinsize(&w, NULL);
  /* ~1.5x of a 900px baseline, but never larger than the window itself */
  if (w > MENU_MAXWIDTH_CAP) return(MENU_MAXWIDTH_CAP);
  if (w < 1) return(MENU_MAXWIDTH_CAP);
  return(w);
}


static int get_menu_maxheight(void) {
  int h;
  gra_getwinsize(NULL, &h);
  if (h > MENU_MAXHEIGHT_CAP) return(MENU_MAXHEIGHT_CAP);
  if (h < 1) return(MENU_MAXHEIGHT_CAP);
  return(h);
}


static int getmenuwidth(void) {
  return(get_menu_maxwidth());
}

/* used to compute UI hints */
#define hintgen(a) gra_text2sprite(a, DroidSansBold_ttf, DroidSansBold_ttf_len, 15, 0x505050u, 0xf1f1f1u, 0xf1, getmenuwidth() / 3);


/* main menu */
int menu_main(int can_resume, int sound) {
  struct ui_menu_description_t menudesc[16];
  int menucount = 0;
  int menupos;
  const int menustep = 10;
  const int menuystart = 22;

  /* zero out the menudesc array */
  memset(menudesc, 0, sizeof(menudesc));

  /* prepare the menu description */
  /* frontpic goes first, so it is never rendered over a label */
  menudesc[menucount].sprite[0] = gra_loadgzbmp(frontpic_bmp_gz, frontpic_bmp_gz_len);
  menudesc[menucount].propxpos = 0;
  menudesc[menucount].propypos = 100;
  menucount++;
  /* OpenKropki title: ink blue, slight tracking, stronger drop shadow */
  menudesc[menucount].sprite[0] = gra_shadowtext2sprite("OpenKropki", DroidSansBold_ttf, DroidSansBold_ttf_len, 58, 0x1B5FA8u, 0xff, 2);
  menudesc[menucount].propxpos = 50;
  menudesc[menucount].propypos = 16; /* refined below once the first menu item exists */
  menudesc[menucount].flags = UI_MENU_FLAG_XCENTER;
  menucount++;
  /* */
  menudesc[menucount].sprite[0] = gra_text2sprite(lang_msg(MSG_NEW_GAME), DroidSansBold_ttf, DroidSansBold_ttf_len, 30, 0, -1, 0xff, -1);
  menudesc[menucount].sprite[1] = gra_text2sprite(lang_msg(MSG_NEW_GAME), DroidSansBold_ttf, DroidSansBold_ttf_len, 30, 0x0000ffu, -1, 0xff, -1);
  menudesc[menucount].clickid = MAINMENU_NEWGAME;
  menudesc[menucount].propxpos = 50;
  menudesc[menucount].propypos = menuystart + (menustep * menucount);
  menudesc[menucount].flags = UI_MENU_FLAG_XCENTER;
  /* Place the title so top-of-page->title gap equals title->first-item gap. */
  {
    int winw, winh, page_h, item_top_pct, title_pct;
    int item_h = gra_getspriteheight(menudesc[menucount].sprite[0]);
    gra_getwinsize(&winw, &winh);
    /* match ui_menu()'s landscape A4 page height estimate */
    page_h = winh - 80;
    if (page_h > get_menu_maxheight()) page_h = get_menu_maxheight();
    {
      int page_w = winw - 80;
      if (page_w > get_menu_maxwidth()) page_w = get_menu_maxwidth();
      if (page_w * 210 > page_h * 297) page_w = page_h * 297 / 210;
      else page_h = page_w * 210 / 297;
    }
    if (page_h < 1) page_h = 1;
    item_top_pct = menudesc[menucount].propypos - ((item_h * 50) / page_h);
    if (item_top_pct < 1) item_top_pct = 1;
    title_pct = item_top_pct / 2;
    if (title_pct < 1) title_pct = 1;
    if (title_pct > 99) title_pct = 99;
    menudesc[menucount - 1].propypos = (unsigned char)title_pct;
  }
  menucount++;
  /* */
  if (!can_resume) {
    menudesc[menucount].sprite[0] = gra_text2sprite(lang_msg(MSG_RESUME_GAME),   DroidSansBold_ttf, DroidSansBold_ttf_len, 30, 0, -1, 90, -1);
  } else {
    menudesc[menucount].sprite[0] = gra_text2sprite(lang_msg(MSG_RESUME_GAME),   DroidSansBold_ttf, DroidSansBold_ttf_len, 30, 0, -1, 0xff, -1);
    menudesc[menucount].sprite[1] = gra_text2sprite(lang_msg(MSG_RESUME_GAME),   DroidSansBold_ttf, DroidSansBold_ttf_len, 30, 0x0000ffu, -1, 0xff, -1);
  }
  menudesc[menucount].clickid = MAINMENU_RESUME;
  menudesc[menucount].propxpos = 50;
  menudesc[menucount].propypos = menuystart + (menustep * menucount);
  menudesc[menucount].flags = UI_MENU_FLAG_XCENTER;
  menucount++;
  /* */
  menudesc[menucount].sprite[0] = gra_text2sprite(lang_msg(MSG_SETTINGS), DroidSansBold_ttf, DroidSansBold_ttf_len, 30, 0, -1, 0xff, -1);
  menudesc[menucount].sprite[1] = gra_text2sprite(lang_msg(MSG_SETTINGS), DroidSansBold_ttf, DroidSansBold_ttf_len, 30, 0x0000ffu, -1, 0xff, -1);
  menudesc[menucount].clickid = MAINMENU_SETTINGS;
  menudesc[menucount].propxpos = 50;
  menudesc[menucount].propypos = menuystart + (menustep * menucount);
  menudesc[menucount].flags = UI_MENU_FLAG_XCENTER;
  menucount++;
  /* */
  menudesc[menucount].sprite[0] = gra_text2sprite(lang_msg(MSG_ABOUT), DroidSansBold_ttf, DroidSansBold_ttf_len, 30, 0, -1, 0xff, -1);
  menudesc[menucount].sprite[1] = gra_text2sprite(lang_msg(MSG_ABOUT), DroidSansBold_ttf, DroidSansBold_ttf_len, 30, 0x0000ffu, -1, 0xff, -1);
  menudesc[menucount].clickid = MAINMENU_ABOUT;
  menudesc[menucount].propxpos = 50;
  menudesc[menucount].propypos = menuystart + (menustep * menucount);
  menudesc[menucount].flags = UI_MENU_FLAG_XCENTER;
  menucount++;
  /* */
  menudesc[menucount].sprite[0] = gra_text2sprite(lang_msg(MSG_EXIT_GAME), DroidSansBold_ttf, DroidSansBold_ttf_len, 30, 0, -1, 0xff, -1);
  menudesc[menucount].sprite[1] = gra_text2sprite(lang_msg(MSG_EXIT_GAME), DroidSansBold_ttf, DroidSansBold_ttf_len, 30, 0x0000ffu, -1, 0xff, -1);
  menudesc[menucount].clickid = MAINMENU_QUIT;
  menudesc[menucount].propxpos = 50;
  menudesc[menucount].propypos = menuystart + (menustep * menucount);
  menudesc[menucount].flags = UI_MENU_FLAG_XCENTER;
  menucount++;
  /* tiny version / copyright in the bottom-right corner of the page */
  {
    char foot[96];
    snprintf(foot, sizeof(foot), "OpenKropki v%s - Copyright (C) Mateusz Viste 2026", PVER);
    menudesc[menucount].sprite[0] = gra_text2sprite(foot, DroidSans_ttf, DroidSans_ttf_len, 15, 0x808080u, -1, 0xff, -1);
    menudesc[menucount].propxpos = 100;
    menudesc[menucount].propypos = 100;
    menucount++;
  }

  /* compute menu */
  menupos = ui_menu(menudesc, menucount, 1, get_menu_maxwidth(), get_menu_maxheight());
  if (sound) gra_playwavgz(sfx_click_wav_gz, sfx_click_wav_gz_len);

  return(menupos);
}


int menu_lang(int sound) {
  int pos;
  struct ui_menu_description_t desc[16];
  int count = 0;
  int flag_i = 0;
  const int nflags = (int)LANG_ERR;
  const int flag_y0 = 28;  /* first flag (below sub-menu title) */
  const int flag_y1 = 76;  /* last flag -- leave clear gap above "back" at 92 */
  const int back_y = 92;

  /* zero out the menudesc array */
  memset(desc, 0, sizeof(desc));

  /* frontpic goes first, so it is never rendered over a label */
  desc[count].sprite[0] = gra_loadgzbmp(frontpic_bmp_gz, frontpic_bmp_gz_len);
  desc[count].propxpos = 0;
  desc[count].propypos = 100;
  count++;
  /* sub-menu title */
  desc[count].sprite[0] = gra_text2sprite(lang_msg(MSG_LANGUAGE_TITLE), DroidSansBold_ttf, DroidSansBold_ttf_len, 40, 0, -1, 0xff, -1);
  desc[count].propxpos = 50;
  desc[count].propypos = 16;
  desc[count].flags = UI_MENU_FLAG_XCENTER;
  count++;
  /* CZ */
  desc[count].sprite[0] = gra_loadgzbmp(flag_cz_bmp_gz, flag_cz_bmp_gz_len);
  desc[count].sprite[1] = gra_loadgzbmp(flag_mouseover_bmp_gz, flag_mouseover_bmp_gz_len);
  desc[count].clickid = LANG_CZ;
  desc[count].flags = UI_MENU_FLAG_ORSPR | UI_MENU_FLAG_XCENTER;
  desc[count].propxpos = 50;
  desc[count].propypos = flag_y0 + (flag_i * (flag_y1 - flag_y0) / (nflags - 1));
  desc[count].hint = hintgen(lang_name(LANG_CZ));
  count++;
  flag_i++;
  /* DE */
  desc[count].sprite[0] = gra_loadgzbmp(flag_de_bmp_gz, flag_de_bmp_gz_len);
  desc[count].sprite[1] = gra_loadgzbmp(flag_mouseover_bmp_gz, flag_mouseover_bmp_gz_len);
  desc[count].clickid = LANG_DE;
  desc[count].flags = UI_MENU_FLAG_ORSPR | UI_MENU_FLAG_XCENTER;
  desc[count].propxpos = 50;
  desc[count].propypos = flag_y0 + (flag_i * (flag_y1 - flag_y0) / (nflags - 1));
  desc[count].hint = hintgen(lang_name(LANG_DE));
  count++;
  flag_i++;
  /* EN */
  desc[count].sprite[0] = gra_loadgzbmp(flag_en_bmp_gz, flag_en_bmp_gz_len);
  desc[count].sprite[1] = gra_loadgzbmp(flag_mouseover_bmp_gz, flag_mouseover_bmp_gz_len);
  desc[count].clickid = LANG_EN;
  desc[count].flags = UI_MENU_FLAG_ORSPR | UI_MENU_FLAG_XCENTER;
  desc[count].propxpos = 50;
  desc[count].propypos = flag_y0 + (flag_i * (flag_y1 - flag_y0) / (nflags - 1));
  desc[count].hint = hintgen(lang_name(LANG_EN));
  count++;
  flag_i++;
  /* FR */
  desc[count].sprite[0] = gra_loadgzbmp(flag_fr_bmp_gz, flag_fr_bmp_gz_len);
  desc[count].sprite[1] = gra_loadgzbmp(flag_mouseover_bmp_gz, flag_mouseover_bmp_gz_len);
  desc[count].clickid = LANG_FR;
  desc[count].flags = UI_MENU_FLAG_ORSPR | UI_MENU_FLAG_XCENTER;
  desc[count].propxpos = 50;
  desc[count].propypos = flag_y0 + (flag_i * (flag_y1 - flag_y0) / (nflags - 1));
  desc[count].hint = hintgen(lang_name(LANG_FR));
  count++;
  flag_i++;
  /* PL */
  desc[count].sprite[0] = gra_loadgzbmp(flag_pl_bmp_gz, flag_pl_bmp_gz_len);
  desc[count].sprite[1] = gra_loadgzbmp(flag_mouseover_bmp_gz, flag_mouseover_bmp_gz_len);
  desc[count].clickid = LANG_PL;
  desc[count].flags = UI_MENU_FLAG_ORSPR | UI_MENU_FLAG_XCENTER;
  desc[count].propxpos = 50;
  desc[count].propypos = flag_y0 + (flag_i * (flag_y1 - flag_y0) / (nflags - 1));
  desc[count].hint = hintgen(lang_name(LANG_PL));
  count++;
  flag_i++;
  /* RU */
  desc[count].sprite[0] = gra_loadgzbmp(flag_ru_bmp_gz, flag_ru_bmp_gz_len);
  desc[count].sprite[1] = gra_loadgzbmp(flag_mouseover_bmp_gz, flag_mouseover_bmp_gz_len);
  desc[count].clickid = LANG_RU;
  desc[count].flags = UI_MENU_FLAG_ORSPR | UI_MENU_FLAG_XCENTER;
  desc[count].propxpos = 50;
  desc[count].propypos = flag_y0 + (flag_i * (flag_y1 - flag_y0) / (nflags - 1));
  desc[count].hint = hintgen(lang_name(LANG_RU));
  count++;
  flag_i++;
  /* back to main menu */
  desc[count].clickid = 0xff;
  desc[count].propxpos = 50;
  desc[count].propypos = back_y;
  desc[count].flags = UI_MENU_FLAG_XCENTER;
  desc[count].sprite[0] = gra_text2sprite(lang_msg(MSG_BACK_TO_MAIN), DroidSansBold_ttf, DroidSansBold_ttf_len, 22, 0, -1, 0xff, -1);
  desc[count].sprite[1] = gra_text2sprite(lang_msg(MSG_BACK_TO_MAIN), DroidSansBold_ttf, DroidSansBold_ttf_len, 22, 0x0000ffu, -1, 0xff, -1);
  count++;

  /* compute menu */
  pos = ui_menu(desc, count, 1, get_menu_maxwidth(), get_menu_maxheight());
  if (sound) gra_playwavgz(sfx_click_wav_gz, sfx_click_wav_gz_len);

  return(pos);
}


/* portrait sprite for the currently selected player type */
static struct gra_sprite *playertype_face(enum playertype_t p) {
  switch (p) {
    case PLAYER_DUMBO:
      return(gra_loadgzbmp(face_dumbo_bmp_gz, face_dumbo_bmp_gz_len));
    case PLAYER_TIGRAN:
      return(gra_loadgzbmp(face_tigran_bmp_gz, face_tigran_bmp_gz_len));
    case PLAYER_BENDER:
      return(gra_loadgzbmp(face_bender_bmp_gz, face_bender_bmp_gz_len));
    case PLAYER_VIKTOR:
      return(gra_loadgzbmp(face_viktor_bmp_gz, face_viktor_bmp_gz_len));
    case PLAYER_HUMAN:
    default:
      return(gra_loadgzbmp(face_human_bmp_gz, face_human_bmp_gz_len));
  }
}


/* "playfield size" and other game configurations menu */
int menu_playconf(int sound, int *playsize, enum playertype_t *p) {
  for (;;) {
    struct ui_menu_description_t menu[32];
    int count = 0;
    int menupos;
    int i;

    /* zero out the menudesc array */
    memset(menu, 0, sizeof(menu));

    /* sub-menu title */
    menu[count].propxpos = 50;
    menu[count].propypos = 8;
    menu[count].flags = UI_MENU_FLAG_XCENTER;
    menu[count].sprite[0] = gra_text2sprite(lang_msg(MSG_NEW_GAME), DroidSansBold_ttf, DroidSansBold_ttf_len, 36, 0, -1, 0xff, -1);
    count++;
    /* playfield size */
    menu[count].propxpos = 5;
    menu[count].propypos = 20;
    menu[count].sprite[0] = gra_text2sprite(lang_msg(MSG_PLAYSIZE), DroidSans_ttf, DroidSans_ttf_len, 20, 0x3C3C3Cu, -1, 0xff, -1);
    count++;
    /* A7 */
    menu[count].clickid = 7;
    menu[count].propxpos = 32;
    menu[count].propypos = 20;
    if (*playsize == 7) {
      menu[count].sprite[0] = gra_text2sprite("A7 (20x14)", DroidSansBold_ttf, DroidSansBold_ttf_len, 20, 0xff8400u, -1, 0xff, -1);
      menu[count].sprite[1] = gra_text2sprite("A7 (20x14)", DroidSansBold_ttf, DroidSansBold_ttf_len, 20, 0xff8400u, -1, 0xff, -1);
    } else {
      menu[count].sprite[0] = gra_text2sprite("A7 (20x14)", DroidSans_ttf, DroidSans_ttf_len, 20, 0, -1, 0xff, -1);
      menu[count].sprite[1] = gra_text2sprite("A7 (20x14)", DroidSans_ttf, DroidSans_ttf_len, 20, 0x0000ffu, -1, 0xff, -1);
    }
    count++;
    /* A6 */
    menu[count].clickid = 6;
    menu[count].propxpos = 48;
    menu[count].propypos = 20;
    if (*playsize == 6) {
      menu[count].sprite[0] = gra_text2sprite("A6 (28x20)", DroidSansBold_ttf, DroidSansBold_ttf_len, 20, 0xff8400u, -1, 0xff, -1);
      menu[count].sprite[1] = gra_text2sprite("A6 (28x20)", DroidSansBold_ttf, DroidSansBold_ttf_len, 20, 0xff8400u, -1, 0xff, -1);
    } else {
      menu[count].sprite[0] = gra_text2sprite("A6 (28x20)", DroidSans_ttf, DroidSans_ttf_len, 20, 0, -1, 0xff, -1);
      menu[count].sprite[1] = gra_text2sprite("A6 (28x20)", DroidSans_ttf, DroidSans_ttf_len, 20, 0x0000ffu, -1, 0xff, -1);
    }
    count++;
    /* A5 */
    menu[count].clickid = 5;
    menu[count].propxpos = 64;
    menu[count].propypos = 20;
    if (*playsize == 5) {
      menu[count].sprite[0] = gra_text2sprite("A5 (42x28)", DroidSansBold_ttf, DroidSansBold_ttf_len, 20, 0xff8400u, -1, 0xff, -1);
      menu[count].sprite[1] = gra_text2sprite("A5 (42x28)", DroidSansBold_ttf, DroidSansBold_ttf_len, 20, 0xff8400u, -1, 0xff, -1);
    } else {
      menu[count].sprite[0] = gra_text2sprite("A5 (42x28)", DroidSans_ttf, DroidSans_ttf_len, 20, 0, -1, 0xff, -1);
      menu[count].sprite[1] = gra_text2sprite("A5 (42x28)", DroidSans_ttf, DroidSans_ttf_len, 20, 0x0000ffu, -1, 0xff, -1);
    }
    count++;
    /* A4 */
    menu[count].clickid = 4;
    menu[count].propxpos = 80;
    menu[count].propypos = 20;
    if (*playsize == 4) {
      menu[count].sprite[0] = gra_text2sprite("A4 (58x42)", DroidSansBold_ttf, DroidSansBold_ttf_len, 20, 0xff8400u, -1, 0xff, -1);
      menu[count].sprite[1] = gra_text2sprite("A4 (58x42)", DroidSansBold_ttf, DroidSansBold_ttf_len, 20, 0xff8400u, -1, 0xff, -1);
    } else {
      menu[count].sprite[0] = gra_text2sprite("A4 (58x42)", DroidSans_ttf, DroidSans_ttf_len, 20, 0, -1, 0xff, -1);
      menu[count].sprite[1] = gra_text2sprite("A4 (58x42)", DroidSans_ttf, DroidSans_ttf_len, 20, 0x0000ffu, -1, 0xff, -1);
    }
    count++;
    /* VS */
    menu[count].flags = UI_MENU_FLAG_XCENTER;
    menu[count].propxpos = 50;
    menu[count].propypos = 51;
    menu[count].sprite[0] = gra_text2sprite(lang_msg(MSG_VS), DroidSans_ttf, DroidSans_ttf_len, 27, 0x787878u, -1, 0xff, -1);
    count++;
    /* faces for the current A / B selections (left of A, right of B) */
    menu[count].propxpos = 3;
    menu[count].propypos = 50;
    menu[count].sprite[0] = playertype_face(p[0]);
    count++;
    menu[count].propxpos = 88;
    menu[count].propypos = 50;
    menu[count].sprite[0] = playertype_face(p[1]);
    count++;
    /* */
    for (i = 0; i < 2; i++) {
      int ai_player_pos = 40;
      /* Player A and B */
      menu[count].flags = UI_MENU_FLAG_XCENTER;
      menu[count].propypos = 32;
      if (i == 0) {
        menu[count].sprite[0] = gra_text2sprite(lang_msg(MSG_PLAYER_A), DroidSansBold_ttf, DroidSansBold_ttf_len, 26, 0x282828u, -1, 0xff, -1);
        menu[count].propxpos = 25;
      } else {
        menu[count].sprite[0] = gra_text2sprite(lang_msg(MSG_PLAYER_B), DroidSansBold_ttf, DroidSansBold_ttf_len, 26, 0x282828u, -1, 0xff, -1);
        menu[count].propxpos = 75;
      }
      count++;
      /* Player human */
      menu[count].flags = UI_MENU_FLAG_XCENTER;
      menu[count].propypos = ai_player_pos;
      ai_player_pos += 5;
      if (i == 0) {
        menu[count].clickid = 10;
        menu[count].propxpos = 25;
      } else {
        menu[count].clickid = 20;
        menu[count].propxpos = 75;
      }
      if (p[i] == 0) {
        menu[count].sprite[0] = gra_text2sprite(lang_msg(MSG_HUMAN), DroidSansBold_ttf, DroidSansBold_ttf_len, 26, 0xff8400u, -1, 0xff, -1);
        menu[count].sprite[1] = gra_text2sprite(lang_msg(MSG_HUMAN), DroidSansBold_ttf, DroidSansBold_ttf_len, 26, 0xff8400u, -1, 0xff, -1);
      } else {
        menu[count].sprite[0] = gra_text2sprite(lang_msg(MSG_HUMAN), DroidSans_ttf, DroidSans_ttf_len, 26, 0, -1, 0xff, -1);
        menu[count].sprite[1] = gra_text2sprite(lang_msg(MSG_HUMAN), DroidSans_ttf, DroidSans_ttf_len, 26, 0x0000ffu, -1, 0xff, -1);
      }
      count++;
      /* player Dumbo */
      menu[count].propypos = ai_player_pos;
      ai_player_pos += 5;
      menu[count].flags = UI_MENU_FLAG_XCENTER;
      menu[count].propxpos = 25 + (i * 50);
      menu[count].clickid = 11 + (i * 10);
      menu[count].hint = hintgen(lang_msg(MSG_DUMBO_HINT));
      if (p[i] == PLAYER_DUMBO) {
        menu[count].sprite[0] = gra_text2sprite(lang_msg(MSG_DUMBO), DroidSansBold_ttf, DroidSansBold_ttf_len, 26, 0xff8400u, -1, 0xff, -1);
        menu[count].sprite[1] = gra_text2sprite(lang_msg(MSG_DUMBO), DroidSansBold_ttf, DroidSansBold_ttf_len, 26, 0xff8400u, -1, 0xff, -1);
      } else {
        menu[count].sprite[0] = gra_text2sprite(lang_msg(MSG_DUMBO), DroidSans_ttf, DroidSans_ttf_len, 26, 0, -1, 0xff, -1);
        menu[count].sprite[1] = gra_text2sprite(lang_msg(MSG_DUMBO), DroidSans_ttf, DroidSans_ttf_len, 26, 0x0000ffu, -1, 0xff, -1);
      }
      count++;
      /* player Tigran */
      menu[count].propypos = ai_player_pos;
      ai_player_pos += 5;
      menu[count].flags = UI_MENU_FLAG_XCENTER;
      menu[count].propxpos = 25 + (i * 50);
      menu[count].clickid = 12 + (i * 10);
      menu[count].hint = hintgen(lang_msg(MSG_TIGRAN_HINT));
      if (p[i] == PLAYER_TIGRAN) {
        menu[count].sprite[0] = gra_text2sprite(lang_msg(MSG_TIGRAN), DroidSansBold_ttf, DroidSansBold_ttf_len, 26, 0xff8400u, -1, 0xff, -1);
        menu[count].sprite[1] = gra_text2sprite(lang_msg(MSG_TIGRAN), DroidSansBold_ttf, DroidSansBold_ttf_len, 26, 0xff8400u, -1, 0xff, -1);
      } else {
        menu[count].sprite[0] = gra_text2sprite(lang_msg(MSG_TIGRAN), DroidSans_ttf, DroidSans_ttf_len, 26, 0, -1, 0xff, -1);
        menu[count].sprite[1] = gra_text2sprite(lang_msg(MSG_TIGRAN), DroidSans_ttf, DroidSans_ttf_len, 26, 0x0000ffu, -1, 0xff, -1);
      }
      count++;
      /* player Bender */
      menu[count].propypos = ai_player_pos;
      ai_player_pos += 5;
      menu[count].flags = UI_MENU_FLAG_XCENTER;
      menu[count].propxpos = 25 + (i * 50);
      menu[count].clickid = 13 + (i * 10);
      menu[count].hint = hintgen(lang_msg(MSG_BENDER_HINT));
      if (p[i] == PLAYER_BENDER) {
        menu[count].sprite[0] = gra_text2sprite(lang_msg(MSG_BENDER), DroidSansBold_ttf, DroidSansBold_ttf_len, 26, 0xff8400u, -1, 0xff, -1);
        menu[count].sprite[1] = gra_text2sprite(lang_msg(MSG_BENDER), DroidSansBold_ttf, DroidSansBold_ttf_len, 26, 0xff8400u, -1, 0xff, -1);
      } else {
        menu[count].sprite[0] = gra_text2sprite(lang_msg(MSG_BENDER), DroidSans_ttf, DroidSans_ttf_len, 26, 0, -1, 0xff, -1);
        menu[count].sprite[1] = gra_text2sprite(lang_msg(MSG_BENDER), DroidSans_ttf, DroidSans_ttf_len, 26, 0x0000ffu, -1, 0xff, -1);
      }
      count++;
      /* player Viktor */
      menu[count].propypos = ai_player_pos;
      ai_player_pos += 5;
      menu[count].flags = UI_MENU_FLAG_XCENTER;
      menu[count].propxpos = 25 + (i * 50);
      menu[count].clickid = 14 + (i * 10);
      menu[count].hint = hintgen(lang_msg(MSG_VIKTOR_HINT));
      if (p[i] == PLAYER_VIKTOR) {
        menu[count].sprite[0] = gra_text2sprite(lang_msg(MSG_VIKTOR), DroidSansBold_ttf, DroidSansBold_ttf_len, 26, 0xff8400u, -1, 0xff, -1);
        menu[count].sprite[1] = gra_text2sprite(lang_msg(MSG_VIKTOR), DroidSansBold_ttf, DroidSansBold_ttf_len, 26, 0xff8400u, -1, 0xff, -1);
      } else {
        menu[count].sprite[0] = gra_text2sprite(lang_msg(MSG_VIKTOR), DroidSans_ttf, DroidSans_ttf_len, 26, 0, -1, 0xff, -1);
        menu[count].sprite[1] = gra_text2sprite(lang_msg(MSG_VIKTOR), DroidSans_ttf, DroidSans_ttf_len, 26, 0x0000ffu, -1, 0xff, -1);
      }
      count++;
      /* START GAME */
      if (i == 0) {
        menu[count].propxpos = 25;
        menu[count].clickid = 150;
      } else {
        menu[count].propxpos = 75;
        menu[count].clickid = 151;
      }
      menu[count].propypos = 80;
      menu[count].flags = UI_MENU_FLAG_XCENTER;
      menu[count].sprite[0] = gra_text2sprite(lang_msg(MSG_STARTGAME), DroidSansBold_ttf, DroidSansBold_ttf_len, 22, 0, -1, 0xff, -1);
      menu[count].sprite[1] = gra_text2sprite(lang_msg(MSG_STARTGAME), DroidSansBold_ttf, DroidSansBold_ttf_len, 22, 0x0000ffu, -1, 0xff, -1);
      count++;
    }
    /* back to main menu */
    menu[count].clickid = 200;
    menu[count].propxpos = 50;
    menu[count].propypos = 92;
    menu[count].flags = UI_MENU_FLAG_XCENTER;
    menu[count].sprite[0] = gra_text2sprite(lang_msg(MSG_BACK_TO_MAIN), DroidSansBold_ttf, DroidSansBold_ttf_len, 22, 0, -1, 0xff, -1);
    menu[count].sprite[1] = gra_text2sprite(lang_msg(MSG_BACK_TO_MAIN), DroidSansBold_ttf, DroidSansBold_ttf_len, 22, 0x0000ffu, -1, 0xff, -1);
    count++;
    /* compute menu */
    menupos = ui_menu(menu, count, 1, get_menu_maxwidth(), get_menu_maxheight());
    if (sound) gra_playwavgz(sfx_click_wav_gz, sfx_click_wav_gz_len);

    /* interpret menu choice */
    if ((menupos == 200) || (menupos == UI_MENU_BACK)) return(UI_MENU_BACK);
    if (menupos == UI_MENU_EXIT) return(UI_MENU_EXIT);
    if ((menupos >= 4) && (menupos <= 7)) *playsize = menupos;
    if (menupos == 10) p[0] = PLAYER_HUMAN; /* p1 = human */
    if (menupos == 11) p[0] = PLAYER_DUMBO; /* p1 = dumbo */
    if (menupos == 12) p[0] = PLAYER_TIGRAN;/* p1 = tigran */
    if (menupos == 13) p[0] = PLAYER_BENDER;/* p1 = bender */
    if (menupos == 14) p[0] = PLAYER_VIKTOR;/* p1 = viktor */
    if (menupos == 20) p[1] = PLAYER_HUMAN; /* p2 = human */
    if (menupos == 21) p[1] = PLAYER_DUMBO; /* p2 = dumbo */
    if (menupos == 22) p[1] = PLAYER_TIGRAN;/* p2 = tigran */
    if (menupos == 23) p[1] = PLAYER_BENDER;/* p2 = bender */
    if (menupos == 24) p[1] = PLAYER_VIKTOR;/* p2 = viktor */
    if ((menupos == 150) || (menupos == 151)) return(menupos); /* PLAY GAME */
  }
}


int menu_about(int sound) {
  struct ui_menu_description_t menudesc[8];
  int count = 0;
  int menupos;
  int winw, winh, page_w, page_h;
  int pic_w, text_maxw, gap;
  struct gra_sprite *pic;
  const char *about = lang_msg(MSG_ABOUT_LONG);

  /* zero out the menudesc array */
  memset(menudesc, 0, sizeof(menudesc));

  /* Match ui_menu()'s landscape A4 page size so wrap width matches the real page. */
  gra_getwinsize(&winw, &winh);
  page_w = winw - 80;
  page_h = winh - 80;
  if (page_w > get_menu_maxwidth()) page_w = get_menu_maxwidth();
  if (page_h > get_menu_maxheight()) page_h = get_menu_maxheight();
  if (page_w * 210 > page_h * 297) {
    page_w = page_h * 297 / 210;
  } else {
    page_h = page_w * 210 / 297;
  }
  (void)page_h;
  if (page_w < 200) page_w = 200;

  /* frontpic goes first, so it is never rendered over a label */
  pic = gra_loadgzbmp(frontpic_bmp_gz, frontpic_bmp_gz_len);
  menudesc[count].propxpos = 0;
  menudesc[count].propypos = 100;
  menudesc[count].sprite[0] = pic;
  count++;

  /* About text sits in the column to the right of the picture (never over it). */
  pic_w = gra_getspritewidth(pic);
  gap = 12;
  text_maxw = page_w - pic_w - gap - 8; /* page minus picture, gap and insets */
  if (text_maxw < 160) text_maxw = page_w * 55 / 100; /* very narrow page: keep a readable column */
  if (text_maxw < 120) text_maxw = 120;
  menudesc[count].sprite[0] = gra_text2sprite(about, DroidSans_ttf, DroidSans_ttf_len, 21, 0, -1, 210, text_maxw);
  menudesc[count].flags = UI_MENU_FLAG_XRELPOS;
  /* small gap after the picture's right edge (~gap pixels as % of page) */
  menudesc[count].propxpos = (gap * 100 + page_w - 1) / page_w;
  if (menudesc[count].propxpos < 1) menudesc[count].propxpos = 1;
  menudesc[count].propypos = 58;
  count++;
  /* sub-menu title */
  menudesc[count].sprite[0] = gra_text2sprite(lang_msg(MSG_ABOUT), DroidSansBold_ttf, DroidSansBold_ttf_len, 40, 0, -1, 0xff, -1);
  menudesc[count].propxpos = 50;
  menudesc[count].propypos = 16;
  menudesc[count].flags = UI_MENU_FLAG_XCENTER;
  count++;
  /* */
  menudesc[count].clickid = 1;
  menudesc[count].propxpos = 50;
  menudesc[count].propypos = 92;
  menudesc[count].flags = UI_MENU_FLAG_XCENTER;
  menudesc[count].sprite[0] = gra_text2sprite(lang_msg(MSG_BACK_TO_MAIN), DroidSansBold_ttf, DroidSansBold_ttf_len, 22, 0, -1, 0xff, -1);
  menudesc[count].sprite[1] = gra_text2sprite(lang_msg(MSG_BACK_TO_MAIN), DroidSansBold_ttf, DroidSansBold_ttf_len, 22, 0x0000ffu, -1, 0xff, -1);
  count++;

  /* compute menu */
  menupos = ui_menu(menudesc, count, 1, get_menu_maxwidth(), get_menu_maxheight());
  if (sound) gra_playwavgz(sfx_click_wav_gz, sfx_click_wav_gz_len);

  if (menupos == 1) menupos = UI_MENU_BACK;
  return(menupos);
}


int menu_settings(int sound, int shadebases, int lang) {
  int menupos;
  int count = 0;
  int row = 0;
  struct ui_menu_description_t menu[8];
  const int title_y = 16;
  const int back_y = 92;
  const int nitems = 3; /* language, shadebases, sound */
  const int menustep = 12;
  /* center the item block halfway between the title and "back" */
  const int menuystart = ((title_y + back_y) / 2) - (((nitems - 1) * menustep) / 2);
  char s[256];

  /* zero out the menudesc array */
  memset(menu, 0, sizeof(menu));

  /* frontpic goes first, so it is never rendered over a label */
  menu[count].propxpos = 0;
  menu[count].propypos = 100;
  menu[count].sprite[0] = gra_loadgzbmp(frontpic_bmp_gz, frontpic_bmp_gz_len);
  count++;
  /* sub-menu title */
  menu[count].sprite[0] = gra_text2sprite(lang_msg(MSG_SETTINGS), DroidSansBold_ttf, DroidSansBold_ttf_len, 40, 0, -1, 0xff, -1);
  menu[count].propxpos = 50;
  menu[count].propypos = title_y;
  menu[count].flags = UI_MENU_FLAG_XCENTER;
  count++;
  /* language */
  snprintf(s, sizeof(s), lang_msg(MSG_LANGUAGE), lang_name(lang));
  menu[count].sprite[0] = gra_text2sprite(s, DroidSansBold_ttf, DroidSansBold_ttf_len, 30, 0, -1, 0xff, -1);
  menu[count].sprite[1] = gra_text2sprite(s, DroidSansBold_ttf, DroidSansBold_ttf_len, 30, 0x0000ffu, -1, 0xff, -1);
  menu[count].clickid = SETMENU_LANG;
  menu[count].propxpos = 50;
  menu[count].propypos = menuystart + (menustep * row++);
  menu[count].flags = UI_MENU_FLAG_XCENTER;
  count++;
  /* render shaded bases */
  snprintf(s, sizeof(s), lang_msg(MSG_SHADEBASES), (shadebases)?lang_msg(MSG_YES):lang_msg(MSG_NO));
  menu[count].sprite[0] = gra_text2sprite(s, DroidSansBold_ttf, DroidSansBold_ttf_len, 30, 0, -1, 0xff, -1);
  menu[count].sprite[1] = gra_text2sprite(s, DroidSansBold_ttf, DroidSansBold_ttf_len, 30, 0x0000ffu, -1, 0xff, -1);
  menu[count].clickid = SETMENU_SHADEBASES;
  menu[count].propxpos = 50;
  menu[count].propypos = menuystart + (menustep * row++);
  menu[count].flags = UI_MENU_FLAG_XCENTER;
  count++;
  /* sound */
  snprintf(s, sizeof(s), lang_msg(MSG_SOUND), (sound)?lang_msg(MSG_YES):lang_msg(MSG_NO));
  menu[count].sprite[0] = gra_text2sprite(s, DroidSansBold_ttf, DroidSansBold_ttf_len, 30, 0, -1, 0xff, -1);
  menu[count].sprite[1] = gra_text2sprite(s, DroidSansBold_ttf, DroidSansBold_ttf_len, 30, 0x0000ffu, -1, 0xff, -1);
  menu[count].clickid = SETMENU_SOUND;
  menu[count].propxpos = 50;
  menu[count].propypos = menuystart + (menustep * row++);
  menu[count].flags = UI_MENU_FLAG_XCENTER;
  count++;
  /* back to main menu */
  menu[count].clickid = 0xff;
  menu[count].propxpos = 50;
  menu[count].propypos = back_y;
  menu[count].flags = UI_MENU_FLAG_XCENTER;
  menu[count].sprite[0] = gra_text2sprite(lang_msg(MSG_BACK_TO_MAIN), DroidSansBold_ttf, DroidSansBold_ttf_len, 22, 0, -1, 0xff, -1);
  menu[count].sprite[1] = gra_text2sprite(lang_msg(MSG_BACK_TO_MAIN), DroidSansBold_ttf, DroidSansBold_ttf_len, 22, 0x0000ffu, -1, 0xff, -1);
  count++;

  /* compute menu */
  menupos = ui_menu(menu, count, 1, get_menu_maxwidth(), get_menu_maxheight());
  if (sound) gra_playwavgz(sfx_click_wav_gz, sfx_click_wav_gz_len);

  if (menupos == 1) menupos = UI_MENU_BACK;
  return(menupos);
}


