Tutorial 7
Here we just use functions for Simplifying key-handling with System-menu (and networking).
- Step 1
At first we show the modifications in the main-program.
- Step 1.1
An enumeration KEYDEFS will be added.
The individual game structure will be changed:
- add a pointer to the keys-structure
- remove gamecontroller-/joystick-ID
- remove keyboard-keys
Differences green-highlighted in main.h:/* abstract key definitions for system-menu */ typedef enum { KEYDEF_KEY_UP, /* move upwards */ KEYDEF_KEY_DOWN, /* move downwards */ } KEYDEFS; /* individual game structure */ struct game { struct vg3_window *wstruct; /* window structure */ int winw, winh; /* size of window */ struct vg3_ofunc *ofstruct; /* common VgaGames3-object structure */ struct vg3_quadtree *qdtr; /* collision-quadtree */ struct vg3_sysmenu *sysm; /* system-menu structure */ struct vg3_keys *skeys; /* keys-struct */ int jid; /* gamecontroller-/joystick-ID or 0 = keyboard */ int key_up, key_down; /* keyboard keys for moving the player-racket */ struct vg3_nwclient *nwclnt; /* network structure */ int endgame; /* flag to end the game */ };
- Step 1.2
In the main-program we
- we receive the gamecontroller-/joystick-ID from the function create_menu() in menu.c
- replace calls to VG3_nw_addkey() with one VG3_keys_nw_addkeys()
- remove the call to change_nw_keys()
- destroy the keys-struct when cleaning up
Differences green-highlighted in main.c:
Receive the gamecontroller-/joystick-ID from the function create_menu()
int master, dowait; int jid; /* program parameter: [...] /* create system-menu */ create_menu(&game); jid = create_menu(&game); if (jid < 0) { fprintf(stderr, "%s\n", VG3_error()); goto endgame; }
Replace calls to VG3_nw_addkey() with one VG3_keys_nw_addkeys()
/* define network-keys */ VG3_nw_addkey(game.nwclnt, game.jid, game.key_up); VG3_nw_addkey(game.nwclnt, game.jid, game.key_down); /* define network-keys from all keys defined in the system-menu */ VG3_keys_nw_addkeys(game.skeys, game.nwclnt, jid);
Remove the call to change_nw_keys()
/* Escape: system-menu */ if (VG3_key_ispressed(game.wstruct, VGAG3_KEY_ESC, VGAG3_IS_NEW_PRESSED)) { if (VG3_sysmenu_exec(game.sysm)) { break; } change_nw_keys(&game); /* eventually actualize network-keys */ }
Destroy the keys-struct when cleaning up
/* close network connection */ VG3_nw_close(game.nwclnt); /* free keys-struct */ VG3_keys_free(game.skeys);
- Step 2
We simplify the file menu.c.
change_nw_keys() is no longer needed because VG3_keys_nw_addkeys() cares for it.
- Step 2.1
menu.h:#ifndef MENU_H_ #define MENU_H_ int create_menu(struct game *); #endif /* MENU_H_ */
- Step 2.2
menu.c:#include <vgagames3.h> #include "main.h" int create_menu(struct game *); /* create system-menu, return joystick-ID or 0 = keyboard or -1 = error */ int create_menu(struct game *game) { struct vg3_text stxt; struct vg3_sysmenu_submenu *inputmenu, *subm_keyb, *subm_gc; int jid, *fjid; game->sysm = VG3_sysmenu_new(game->wstruct, NULL, VG3_color_brightness(VGAG3_COLOR_GREEN, 50), VGAG3_COLOR_GREEN); /* insert exit-menu */ VG3_sysmenu_simple_exitmenu(game->sysm, NULL, "Exit game"); /* insert volume-menu with only main-, sound- and music-volume */ VG3_sysmenu_simple_volumemenu(game->sysm, NULL, "Main volume", "Sound volume", NULL, NULL); /* insert a sub-menu for input-settings */ VGAG3_TEXT_ATTRIBUTES_SET(&stxt, NULL, 0, 0, "Input settings"); inputmenu = VG3_sysmenu_insert_menuentry(game->sysm, NULL, &stxt); /* get number of found gamecontrollers/joysticks */ VG3_gamecontroller_getall(game->wstruct, &fjid); if (fjid[0] == 1) { /* exactly one gamecontroller/joystick found */ jid = fjid[1]; } else { /* no or more than one gamecontrollers/joysticks found: we use keyboard */ jid = 0; } free(fjid); /* insert sub-menu for keyboard or gamecontroller/joystick */ if (jid > 0) { /* use gamecontroller/joystick */ subm_keyb = NULL; subm_gc = VG3_sysmenu_simple_gcmenu(game->sysm, inputmenu); } else { /* use keyboard */ subm_keyb = VG3_sysmenu_simple_keyboardmenu(game->sysm, inputmenu); subm_gc = NULL; } /* initialize keys-struct for simplifying key-handling with system menu (and network) */ game->skeys = VG3_keys_new(game->wstruct, game->sysm); if (game->skeys == NULL) { return -1; } /* insert KEYDEF_KEY_UP into keyboard submenu or gamecontroller submenu */ VG3_keys_menu_insert(game->skeys, NULL, NULL, KEYDEF_KEY_UP, "move upwards", "Upwards moving", subm_keyb, VGAG3_KEY_UCURS, subm_gc, VGAG3_GC_AXIS_RIGHTY_UP, "Axis-1:neg"); /* insert KEYDEF_KEY_DOWN into keyboard submenu or gamecontroller submenu */ VG3_keys_menu_insert(game->skeys, NULL, NULL, KEYDEF_KEY_DOWN, "move downwards", "Downwards moving", subm_keyb, VGAG3_KEY_DCURS, subm_gc, VGAG3_GC_AXIS_RIGHTY_DOWN, "Axis-1:pos"); return jid; }
- Putting together
See the files in example-games/tutorial7/ - Executing the system-menu
Main menuInput menuFound gamecontrollersSetting for gamecontroller
<<Prev | Top | Last>> |