VgaGames 3 - Miscellaneous man-pages

[.. upper level ..]

VG3_keys_menu_insert()

[added in version 1.2]

Insert an entry into keyboard-menu and gamecontroller-/joystick-menu
of the system-menu for all found devices and respect individual saved values.
SYNTAX
void VG3_keys_menu_insert(struct vg3_keys *skeys, const char *svdata, struct vg3_multilang *mlang, int keyref, const char *keyname, const char *keyinfo, struct vg3_sysmenu_submenu *subm_keyb, int keyidx, struct vg3_sysmenu_submenu *subm_gc, int gcidx, const char *jsname)

FUNCTION PARAMETERS
skeys Keys-struct
svdata Savestring (created by VG3_sysmenu_savestring_insert()), or NULL
mlang Multilanguage structure, or NULL
keyref Arbitrary reference number for this entry, e.g. specifying its action
keyname Description for the key
keyinfo Short description text for menu-window for this entry
subm_keyb Sub-menu for keyboard-entries or NULL = don't insert entry for keyboard
keyidx Default key for keyboard: one of the values of VGAG3_KEYS
subm_gc Sub-menu for gamecontroller-/joystick-entries or NULL = don't insert entry for gamecontroller/joysticks
gcidx Default key for gamecontroller: one of the values of VGAG3_GCS
jsname Default key for joysticks: "Button-<number>" or "Axis-<number>:<pos|neg>"

DESCRIPTION
Insert an entry into the keyboard-menu and the gamecontroller-/joystick-menu of the system-menu for all found devices. This will call VG3_sysmenu_simple_keyboardentry() for the keyboard if subm_keyb is not NULL, and VG3_sysmenu_simple_gcentry() for all found gamecontrollers/joysticks if subm_gc is not NULL. As default value will be used - keyidx for the keyboard-entry - gcidx for the entry for gamecontrollers resp. jsname for joysticks jsname must be a string (but may be NULL if not to be set) - for buttons: "Button-<number>" (e.g. "Button-1") - for axes: "Axis-<number>:<pos|neg>" (e.g. "Axis-1:neg" or "Axis-2:pos") If svdata is not NULL, the actual value will be taken from it instead using the default value.

EXAMPLE

Single-player:
  struct vg3_sysmenu *sysm;
  struct vg3_window *wstruct;
  struct vg3_sysmenu_submenu *subm_keyb, *subm_gc;
  struct vg3_keys *skeys;
  struct vg3_multilang *mlang;
  char *svdata;
  enum { GO_RIGHT, GO_LEFT };

  mlang = NULL;

  /* open window */
  wstruct = VG3_window_new(argv[0], VGAG3_VGAVERSION_LOW, VGAG3_WINSCALE_NOSCALE);
  if (wstruct == NULL) { fprintf(stderr, "%s\n", VG3_error()); exit(1); }

  /* create system-menu */

  sysm = VG3_sysmenu_new(wstruct, NULL, VG3_color_brightness(VGAG3_COLOR_GREEN, 50), VGAG3_COLOR_GREEN);

  /* create keyboard-submenu */
  subm_keyb = VG3_sysmenu_simple_keyboardmenu(sysm, NULL);

  /* create gamecontroller-/joystic-submenu, but only if devices are installed */
  subm_gc = NULL;
  if (VG3_gamecontroller_getall(wstruct, NULL) > 0) { subm_gc = VG3_sysmenu_simple_gcmenu(sysm, NULL); }

  /* read saved key-values from file */
  { char buf[128];
    FILE *ffp = fopen("save.data", "r");
    if (ffp != NULL) { fgets(buf, sizeof(buf), ffp); fclose(ffp); } else { *buf = '\0'; }
    svdata = strdup(buf);
  }

  /* create keys-struct and insert for all found devices two keys */

  skeys = VG3_keys_new(wstruct, sysm);

   /* insert "going to the right" */
  VG3_keys_menu_insert(skeys, svdata, mlang,
                       GO_RIGHT, "Right", "Go to the right",
                       subm_keyb, VGAG3_KEY_RCURS,
                       subm_gc, VGAG3_GC_AXIS_LEFTX_RIGHT, "Axis-1:pos");

   /* insert "going to the left" */
  VG3_keys_menu_insert(skeys, svdata, mlang,
                       GO_LEFT, "Left", "Go to the left",
                       subm_keyb, VGAG3_KEY_LCURS,
                       subm_gc, VGAG3_GC_AXIS_LEFTX_LEFT, "Axis-1:neg");

  /* game-loop */
  VG3_discard_input(wstruct);
  for (;;) {
    if (VG3_inputevent_update(wstruct)) { break; }

    /* quit */
    if (VG3_key_ispressed(wstruct, VGAG3_KEY_Q, VGAG3_IS_NEW_PRESSED)) { break; }

    /* call system-menu */
    if (VG3_key_ispressed(wstruct, VGAG3_KEY_ESC, VGAG3_IS_NEW_PRESSED)) {
      if (VG3_sysmenu_exec(sysm)) { break; }
    }

    /* check for "going right" */
    if (VG3_keys_key_ispressed(skeys, NULL, 0, -1, GO_RIGHT, VGAG3_IS_PRESSED)) {
      [... go to the right ...]
    }

    /* check for "going left" */
    if (VG3_keys_key_ispressed(skeys, NULL, 0, -1, GO_LEFT, VGAG3_IS_PRESSED)) {
      [... go to the left ...]
    }

    [... remaining code of loop ...]
  }
  VG3_discard_input(wstruct);

  /* free keys-struct */
  VG3_keys_free(skeys);

  /* save key-values to file and free system-menu */
  { FILE *ffp = fopen("save.data", "w");
    if (ffp != NULL) {
      char *svdata_new = VG3_sysmenu_savestring_insert(sysm, svdata);
      fprintf(ffp, "%s", svdata_new);
      fclose(ffp);
      free(svdata_new);
    }
    free(svdata);
  }
  VG3_sysmenu_free(sysm);

SEE ALSO
VG3_keys_new() VG3_keys_free() VG3_keys_nw_addkeys() VG3_keys_key_ispressed()