Adapted client to use Ncurses instead of raw terminal output:

Created "slowPrintNcurses", which is a version of "slowPrint" compatible with Ncurses screens.
Ncurses is now used in place of raw-terminal output.
The screen clears after inital start-up messages.
C-d no longer exits, and still doesn't spam.
Added Ncurses to the ld options of client in the Makefile.
Created ld options for server in the Makefile.
This commit is contained in:
Barry Kane 2021-09-03 18:47:11 +01:00
parent 849a80bd37
commit 33bc9bcda0
5 changed files with 35 additions and 9 deletions

View File

@ -5,13 +5,13 @@ clientobj = $(clientsrc:.c=.o)
serversrc = $(wildcard src/misc/*.c) \
src/SilverMUDServer.c
serverobj = $(serversrc:.c=.o)
CLIENTLDFLAGS= -lpthread
CLIENTLDFLAGS= -lpthread -lncurses
SERVERLDFLAGS= -lncurses
SilverMUDClient: $(clientobj)
gcc -o $@ $^ $(CLIENTLDFLAGS)
SilverMUDServer: $(serverobj)
gcc -o $@ $^
gcc -o $@ $^ $(SERVERLDFLAGS)
.PHONY: clean
clean:

View File

@ -4,6 +4,7 @@
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <ncurses.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include "misc/texteffects.h"
@ -20,8 +21,8 @@ void * messageSender(void * sockfd)
while (1)
{
bzero(sendBuffer, MAX);
printf("COMM-LINK> ");
if(fgets(sendBuffer, MAX, stdin) == NULL)
wprintw(stdscr, "COMM-LINK> ");
if(wgetnstr(stdscr, sendBuffer, MAX) == ERR)
{
exit(0);
}
@ -41,9 +42,9 @@ void * messageReceiver(void * sockfd)
while (1)
{
read((long)sockfd, receiveBuffer, MAX);
slowPrint("\nUSER-MESSAGE: ", 8000);
slowPrint(receiveBuffer, 8000);
slowPrint("\nCOMM-LINK (CONT.)> ", 8000);
slowPrintNcurses("\nUSER-MESSAGE: ", 8000);
slowPrintNcurses(receiveBuffer, 8000);
slowPrintNcurses("\nCOMM-LINK (CONT.)> ", 8000);
bzero(receiveBuffer, MAX);
}
}
@ -53,7 +54,6 @@ int main(int argc, char **argv)
int sockfd, connfd;
struct sockaddr_in servaddr, cli;
pthread_t messagingThread;
// Give me a socket, and make sure it's working:
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1)
@ -89,6 +89,11 @@ int main(int argc, char **argv)
{
slowPrint("Connected to the Silverkin Industries Comm-Link Server:\nHave a pleasant day.\n", 8000);
}
usleep(100000);
// Setup Ncurses:
initscr();
scrollok(stdscr, true);
// Run a thread to send messages, and use main to recieve.
pthread_create(&messagingThread, NULL, messageSender, (void *)(long)sockfd);
@ -96,4 +101,7 @@ int main(int argc, char **argv)
// Close the socket.
close(sockfd);
// Unsetup Ncurses:
endwin();
}

View File

@ -7,6 +7,7 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ncurses.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>

View File

@ -2,6 +2,7 @@
// Barry Kane, 2021.
#include <stdio.h>
#include <unistd.h>
#include <ncurses.h>
void slowPrint(char * stringToPrint, int delay)
{
@ -15,3 +16,16 @@ void slowPrint(char * stringToPrint, int delay)
characterIndex++;
}
}
void slowPrintNcurses(char * stringToPrint, int delay)
{
int characterIndex = 0;
while(stringToPrint[characterIndex] != '\0')
{
addch(stringToPrint[characterIndex]);
// Refresh the ncurses screen.
refresh();
usleep(delay);
characterIndex++;
}
}

View File

@ -7,6 +7,9 @@
// A fancy, character by character print. Similar to a serial terminal with lower baud rate.
void slowPrint(char * stringToPrint, int delay);
// The same, altered to work with Ncurses.
void slowPrintNcurses(char * stringToPrint, int delay);
// A string containing an ASCII art version of the Silverkin Industries logo.
char * logostring = " ///////\n //////////////////////////////////////////\n ///////////////////////////////////////////////////////////\n ////////// ////////////////////////////\n ### # # # # ##### ### # # # # # /////////////////\n ### # # # # ## # # ## # ## # //////////////\n ## # # # # # ### # # # # # # /////////\n #### # ### # ##### # # # # # # ## ///////\n # ## # ##### # # ### ### ### # ##### ### ////// \n # # # # # # # # ## # # # # ## ## ////\n # # # # # # # # ## # ### # # ## //\n # # ### ##### ##### ### # # # # #### ### //\n";