VG3_keys_nw_addkeys()
[added in version 1.2]
Add all keys from the system-menu for a given device into the network structure,
defining them as network-keys.
SYNTAX
void
VG3_keys_nw_addkeys(struct vg3_keys *skeys,
struct vg3_nwclient *nwclnt,
int jid)
FUNCTION PARAMETERS
skeys | Keys-struct |
nwclnt | Network-struct |
jid | Joystick-ID or 0 = keyboard |
DESCRIPTION
This function is only needed if networking is active.
It replaces VG3_nw_addkey()
and puts all in the system-menu defined keys into the network structure,
defining them as network-keys.
Only the keys of a specific input-device defined by jid are used as network-keys,
so if a keyboard and a gamecontroller are found, only one of them can be used.
This function has to be called once before calling VG3_nw_connect(),
and the keys must be checked with VG3_keys_key_ispressed().
It replaces also automatically VG3_nw_changekey().
EXAMPLE
Network game just showing whether key of the clients is pressed or not:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <unistd.h> #include <vgagames3.h> /* define key reference number, just one indicating pressing the key */ #define KEYREF_PRESSED 1 /* output text centered onto the window */ static void print_text(struct vg3_window *wstruct, const char *text) { struct vg3_rect rect; struct vg3_text stxt; int w0, h0; if (wstruct == NULL || text == NULL || *text == '\0') { return; } VG3_window_getsize(wstruct, &w0, &h0); VG3_draw_clear(wstruct, NULL, VGAG3_COLOR_BLACK); VGAG3_TEXT_ATTRIBUTES_SET(&stxt, NULL, '\n', 0, text); rect.x = rect.y = 0; rect.w = w0; rect.h = h0; rect = VG3_draw_text(wstruct, NULL, &rect, 0, &stxt, VGAG3_COLOR_YELLOW, VGAG3_COLOR_BLACK, 1); rect.x = w0 / 2 - rect.w / 2; rect.y = h0 / 2 - rect.h / 2; VG3_draw_text(wstruct, NULL, &rect, 0, &stxt, VGAG3_COLOR_YELLOW, VGAG3_COLOR_BLACK, 0); rect.x--; rect.y--; rect.w += 2; rect.h += 2; VG3_draw_rect(wstruct, NULL, &rect, 0, VGAG3_COLOR_YELLOW); } /* main program */ int main(int argc, char **argv) { struct vg3_window *wstruct; struct vg3_nwclient *nwptr; int retw, is_master, masterbyte, clnr, clmax, cli, dowait, jid; char remhost[128], *ipsrv, buf[256], *sysm_string; struct vg3_sysmenu *sysm; struct vg3_sysmenu_submenu *subm_keyb, *subm_gc; struct vg3_keys *skeys; FILE *ffp; /* called with parameter "-m" indicates master-client */ is_master = masterbyte = 0; if (argc >= 2 && strcmp(argv[1], "-m") == 0) { is_master = 1, masterbyte = 1; printf("MASTER\n"); } /* master starts multicast/broadcast server and network server */ if (is_master) { VG3_nw_mbcast_start(); VG3_nw_server(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); } retw = 0; /* don't catch mouse */ VG3_mouserelease(wstruct, VGAG3_KEY_NOKEY); /* create system menu * - install exit menu * - install empty keyboard submenu * - install empty gamecontroller submenu, * but only if at least one gamecontroller/joystick is found * variables: * - "sysm" gets system menu * - "subm_keyb" gets keyboard submenu * - "subm_gc" gets gamecontroller submenu or is set to NULL */ sysm = VG3_sysmenu_new(wstruct, NULL, VG3_color_brightness(VGAG3_COLOR_GREEN, 50), VGAG3_COLOR_GREEN); VG3_sysmenu_simple_exitmenu(sysm, NULL, "Exit game"); subm_keyb = VG3_sysmenu_simple_keyboardmenu(sysm, NULL); subm_gc = NULL; if (VG3_gamecontroller_getall(wstruct, NULL) > 0) { subm_gc = VG3_sysmenu_simple_gcmenu(sysm, NULL); } /* read saved key definitions for system menu from file */ if ((ffp = fopen("nwkeys.keysave", "r")) != NULL) { fgets(buf, sizeof(buf), ffp); fclose(ffp); } else { *buf = '\0'; } sysm_string = strdup(buf); /* initialize keys-struct for simplifying key-handling with system menu (and network) */ skeys = VG3_keys_new(wstruct, sysm); if (skeys == NULL) { fprintf(stderr, "%s\n", VG3_error()); retw = 1; goto endgame; } /* insert KEYREF_PRESSED into keyboard submenu and possibly gamecontroller submenu */ VG3_keys_menu_insert(skeys, sysm_string, NULL, KEYREF_PRESSED, "Key pressed", "Key pressed", subm_keyb, VGAG3_KEY_SPACE, subm_gc, VGAG3_GC_BUTTON_A, "Button-1"); /* if a gamecontroller/joystick is found, use it, else use keyboard */ jid = 0; if (subm_gc != NULL) { int *jidf; VG3_gamecontroller_getall(wstruct, &jidf); if (jidf[0] > 0) { jid = jidf[1]; } /* use first found one */ free(jidf); } /* retrieve ip of network server via multicast/broadcast * variables: * - "ipsrv" gets server ip * - "remhost" gets server name */ if (VG3_inputevent_update(wstruct)) { goto endgame; } print_text(wstruct, "Searching for network-server..."); VG3_window_update(wstruct, 0, 0); ipsrv = VG3_nw_mbcast_getip(remhost, sizeof(remhost)); if (ipsrv == NULL) { fprintf(stderr, "%s\n", VG3_error()); retw = 1; goto endgame; } /* show server name and ip */ if (VG3_inputevent_update(wstruct)) { goto endgame; } snprintf(buf, sizeof(buf), "%s: %s", remhost, ipsrv); print_text(wstruct, buf); VG3_window_update(wstruct, 0, 0); sleep(3); /* get initialized network struct */ nwptr = VG3_nw_open(wstruct); /* add keys (here just KEYREF_PRESSED) installed into the system menu to network server */ VG3_keys_nw_addkeys(skeys, nwptr, jid); /* clear window and connect to network server */ if (VG3_inputevent_update(wstruct)) { goto endgame; } VG3_draw_clear(wstruct, NULL, VG3_color_brightness(VGAG3_COLOR_PINK, 70)); VG3_window_update(wstruct, 0, 0); clnr = VG3_nw_connect(nwptr, ipsrv, NULL, NULL, 0, &masterbyte); if (clnr < 0) { fprintf(stderr, "%s\n", VG3_error()); retw = 1; goto endgame; } if (clnr == 0) { goto endgame; } if (VG3_inputevent_update(wstruct)) { goto endgame; } free(ipsrv); /* show own client number */ snprintf(buf, sizeof(buf), "Connected. client no: %d", clnr); print_text(wstruct, buf); VG3_window_update(wstruct, 0, 0); sleep(3); /* master stops multicast/broadcast server */ if (is_master) { VG3_nw_mbcast_stop(); } /* get number of connected clients and show their names * variables: * - "clmax" gets number of connected clients */ *buf = '\0'; clmax = VG3_nw_numberofclients(nwptr); if (VG3_inputevent_update(wstruct)) { goto endgame; } for (cli = 1; cli <= clmax; cli++) { const char *name; if (VG3_nw_getclientinfo(nwptr, cli, &name)) { snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "[%d]: %s\n", cli, name); } } print_text(wstruct, buf); VG3_window_update(wstruct, 0, 0); sleep(3); /* game-loop */ for (;;) { if (VG3_nw_update(nwptr, &dowait)) { break; } /* exit */ if (VG3_key_ispressed(wstruct, VGAG3_KEY_Q, VGAG3_IS_PRESSED)) { break; } /* call system menu */ if (VG3_key_ispressed(wstruct, VGAG3_KEY_ESC, VGAG3_IS_NEW_PRESSED)) { if (VG3_sysmenu_exec(sysm)) { break; } } /* for each client get status whether key is pressed or not */ *buf = '\0'; for (cli = 1; cli <= clmax; cli++) { /* check whether client is still connected */ if (!VG3_nw_getclientinfo(nwptr, cli, NULL)) { continue; } snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "[%d]:", cli); if (VG3_keys_key_ispressed(skeys, nwptr, cli, -1, KEYREF_PRESSED, VGAG3_IS_PRESSED)) { snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " is pressed"); } else { snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " is not pressed"); } snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "\n"); } /* show retrieved status */ print_text(wstruct, buf); VG3_window_update(wstruct, 0, 0); if (dowait) { VG3_wait_time(30); } } /* ending */ /* close network */ VG3_nw_close(nwptr); /* save key definitions for system menu into file */ { char *sysm_string_new = VG3_sysmenu_savestring_insert(sysm, sysm_string); if ((ffp = fopen("nwkeys.keysave", "w")) != NULL) { fprintf(ffp, "%s", sysm_string_new); fclose(ffp); } free(sysm_string_new); } /* freeing */ free(sysm_string); VG3_keys_free(skeys); VG3_sysmenu_free(sysm); endgame: VG3_window_free(wstruct); exit(retw); }
SEE ALSO