diff options
| author | r4tss <[email protected]> | 2021-03-10 16:14:57 +0100 |
|---|---|---|
| committer | r4tss <[email protected]> | 2021-03-10 16:14:57 +0100 |
| commit | df675d05d9b1e48c0ca95d74ad33b13afa03ffe0 (patch) | |
| tree | 9cf437c1d2131b81d57a7e0fd06604238d66afee /nships.c | |
| parent | 966ea74bc9374015ae468a7e2f930dc6e18c7a66 (diff) | |
Added base logic for menu system
Diffstat (limited to 'nships.c')
| -rw-r--r-- | nships.c | 85 |
1 files changed, 70 insertions, 15 deletions
@@ -1,6 +1,12 @@ #include <ncurses.h> #include <stdlib.h> #include <time.h> +#include <stdio.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <unistd.h> +#include <string.h> #include "server.c" @@ -74,42 +80,91 @@ int menu(int y, int x, int h, int w, int aLen, char it[][15], char *title) wrefresh(menu); } delwin(menu); + clear(); if(o == 1) return c; else return -1; } +void getstrwin(char * ip, char *title) +{ + char str[80]; + echo(); + WINDOW * getip = newwin(3, 20, (LINES - 3) / 2, (COLS - 20) / 2); + box(getip, 0, 0); + refresh(); + mvwprintw(getip, 0, 1, title); + wmove(getip, 1, 1); + wgetnstr(getip, str, 13); + strncpy(ip, str, 128); + delwin(getip); + noecho(); +} + int main() { - struct player p1, p2, hostID; + struct player p1, p2; struct items start = {3, {"Host Game", "Join Game", "Exit"}}; - int state = 0; - char title[15] = "nships"; - bool exit = false; + int state = 0, e = 0, sfd, host; + char title[15] = "nships", nameTitle[20] = "Enter Name", ipTitle[20] = "Enter IP", ip[128]; initscr(); + curs_set(0); noecho(); - while(!exit) + WINDOW * g = newwin(100, 200, (LINES - 100) / 2, (COLS - 200) / 2); + refresh(); + while(e == 0) { switch(state) { case 0: - state = menu((LINES - 15) / 2, (COLS - 10) / 2, 5, 15, start.len, start.items, title); - if(state == 0) - state = 1; - break; - if(state == 1) - state = 2; - break; - if(state == 2) - exit = true; + state = menu((LINES - 5) / 2, (COLS - 15) / 2, 5, 15, start.len, start.items, title) + 1; + if(state == 3) + state = 4; break; case 1: - hostID = fork() + clear(); + printw("Waiting for client to connect"); + refresh(); + sfd = hostGame(); + clear(); + if(sfd == 0) { + printw("Failed to host"); + state = 0; + } + else { + host = 1; + getstrwin(p1.name, "Enter your name"); + state = 4; + } break; case 2: + getstrwin(ip, ipTitle); + sfd = joinGame(ip); + clear(); + if(sfd == 0) { + printw("Failed to connect"); + state = 0; + } + else { + host = 0; + getstrwin(p2.name, "Enter your name"); + state = 4; + } + break; + case 3: + state = 4; + break; + case 4: + e = 1; break; } + refresh(); } + if(sfd != 0) + close(sfd); + + endwin(); + return 0; } |
