Seperate Screen Files #1
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
||||
bbs
|
||||
build/
|
||||
|
43
Makefile
43
Makefile
@@ -1,18 +1,37 @@
|
||||
CC=gcc
|
||||
CFLAGS=-std=c99 -pedantic -Wall -Wextra
|
||||
LIBS=-lncursesw
|
||||
EXECUTABLE=bbs
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
.PHONY: all
|
||||
all: make
|
||||
# Makefile for Amber's BBS Interface
|
||||
# Buildsys by Gale Faraday
|
||||
|
||||
.PHONY: all clean run
|
||||
.SUFFIXES:
|
||||
|
||||
.DEFAULT_GOAL := all
|
||||
|
||||
TARGET := $(shell basename $(CURDIR))
|
||||
SOURCES := src/
|
||||
BUILD := build/
|
||||
OBJS := $(patsubst $(SOURCES)%.c,$(BUILD)%.o,$(wildcard $(SOURCES)*.c))
|
||||
EXECUTABLE := $(TARGET)
|
||||
|
||||
|
||||
make:
|
||||
$(CC) $(CFLAGS) $(LIBS) bbs.c -o $(EXECUTABLE)
|
||||
CC := gcc
|
||||
LD := gcc
|
||||
CFLAGS := -std=c99 -pedantic -Wall -Wextra -O2 -ggdb
|
||||
LDFLAGS := $(CFLAGS) -lncursesw
|
||||
|
||||
.PHONY: execute
|
||||
execute:
|
||||
all: $(EXECUTABLE)
|
||||
|
||||
run: $(EXECUTABLE)
|
||||
./$(EXECUTABLE)
|
||||
|
||||
.PHONY: run
|
||||
run: make execute
|
||||
.IGNORE: clean
|
||||
clean:
|
||||
@rm -rvf $(BUILD) $(EXECUTABLE)
|
||||
|
||||
$(EXECUTABLE): $(OBJS)
|
||||
$(LD) $(LDFLAGS) -o $@ $^
|
||||
|
||||
$(OBJS): $(BUILD)%.o : $(SOURCES)%.c
|
||||
-@mkdir -p $(BUILD)
|
||||
$(CC) $(CFLAGS) -o $@ -c $<
|
||||
|
@@ -1,56 +1,23 @@
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
|
||||
#include <curses.h>
|
||||
#include <string.h>
|
||||
#include <locale.h>
|
||||
|
||||
#include "globals.h"
|
||||
#include "screens.h"
|
||||
|
||||
const unsigned int GETCH_TIMEOUT = 10; /* in ms */
|
||||
const unsigned int MAX_SCREENS = 2; /* size of screens array */
|
||||
const char* LOCALE = "en_US.UTF-8"; /* enable unicode support, set to "ANSI_X3.4-1968" for ascii */
|
||||
|
||||
char errorMessage[60];
|
||||
|
||||
enum Status {
|
||||
STATUS_QUIT,
|
||||
STATUS_WAITING,
|
||||
STATUS_NEED_REFRESH
|
||||
};
|
||||
|
||||
enum ActiveScreen {
|
||||
HOME,
|
||||
ERROR
|
||||
};
|
||||
|
||||
struct Screen {
|
||||
char name[20];
|
||||
WINDOW *win;
|
||||
void (*draw_screen)(struct Screen *, char *input); /* this is run about every 10ms */
|
||||
};
|
||||
|
||||
static void draw_home(struct Screen *screen, char *input) {
|
||||
static char* banner = ""
|
||||
" _ _ _ \n"
|
||||
" | | | | | | \n"
|
||||
" __ _ _ __ ___ | |__ ___ _ __ ___ _ __ | | __ _ ___ ___ _ __ ___| |_ \n"
|
||||
" / _` | '_ ` _ \\| '_ \\ / _ \\ '__/ __| '_ \\| |/ _` |/ __/ _ \\ | '_ \\ / _ \\ __|\n"
|
||||
" | (_| | | | | | | |_) | __/ | \\__ \\ |_) | | (_| | (_| __/_| | | | __/ |_ \n"
|
||||
" \\__,_|_| |_| |_|_.__/ \\___|_| |___/ .__/|_|\\__,_|\\___\\___(_)_| |_|\\___|\\__|\n"
|
||||
" | | \n"
|
||||
" |_| \n";
|
||||
|
||||
mvwprintw(screen->win, 1, 2, "Thank you for visiting:");
|
||||
mvwprintw(screen->win, 2, 1, "%s", banner);
|
||||
mvwprintw(screen->win, 10, 1, "Your current input is: %s", input);
|
||||
}
|
||||
|
||||
static void draw_error(struct Screen *screen, __attribute__((unused)) char *input) {
|
||||
mvwprintw(screen->win, 1, 2, "%s", errorMessage);
|
||||
}
|
||||
char error_message[MAX_ERROR_MESSAGE_SIZE];
|
||||
|
||||
static void draw_screen (struct Screen *screen, char *input) {
|
||||
screen->draw_screen(screen, input);
|
||||
box(screen->win, 0, 0);
|
||||
// wattron(screen->win, A_STANDOUT);
|
||||
/* wattron(screen->win, A_STANDOUT); */
|
||||
mvwprintw(screen->win, 0, 1, " %s ", screen->name);
|
||||
// wattroff(screen->win, A_STANDOUT);
|
||||
/* wattroff(screen->win, A_STANDOUT); */
|
||||
}
|
||||
|
||||
int main() {
|
||||
@@ -61,37 +28,25 @@ int main() {
|
||||
timeout(GETCH_TIMEOUT); /* set timeout for getch() */
|
||||
setlocale(LC_CTYPE, LOCALE); /* set locale, UTF8 support is enabled here */
|
||||
|
||||
unsigned int start_y = 0;
|
||||
unsigned int start_x = 0;
|
||||
unsigned int terminal_width;
|
||||
unsigned int terminal_height;
|
||||
unsigned int old_terminal_width = 0;
|
||||
unsigned int old_terminal_height = 0;
|
||||
|
||||
struct Screen screens[MAX_SCREENS];
|
||||
|
||||
enum Status status = STATUS_NEED_REFRESH; /* refresh screens[active_screen].window on first loop */
|
||||
char input = ' ';
|
||||
|
||||
strcpy(errorMessage, "");
|
||||
strcpy(error_message, "");
|
||||
|
||||
getmaxyx(stdscr, terminal_height, terminal_width);
|
||||
|
||||
unsigned int screen_before_error = HOME;
|
||||
|
||||
enum ActiveScreen active_screen = HOME;
|
||||
struct Screen home = {
|
||||
"home",
|
||||
newwin(terminal_height, terminal_width, start_y, start_x),
|
||||
draw_home
|
||||
};
|
||||
screens[HOME] = home;
|
||||
struct Screen error = {
|
||||
"error",
|
||||
newwin(terminal_height, terminal_width, start_y, start_x),
|
||||
draw_error
|
||||
};
|
||||
screens[ERROR] = error;
|
||||
|
||||
/* could be it's own init func within the screen's file. */
|
||||
screens[HOME].win = newwin(terminal_height, terminal_width, 0, 0);
|
||||
screens[ERROR].win = newwin(terminal_height, terminal_width, 0, 0);
|
||||
|
||||
/* main event loop */
|
||||
while (status != STATUS_QUIT) {
|
||||
@@ -116,7 +71,7 @@ int main() {
|
||||
|
||||
if (terminal_width < 80 || terminal_height < 24) {
|
||||
if (active_screen != ERROR) {
|
||||
strcpy(errorMessage, "This program expects at least 80 rows by 24 columns.");
|
||||
strcpy(error_message, "This program expects at least 80 rows by 24 columns.");
|
||||
screen_before_error = active_screen;
|
||||
};
|
||||
active_screen = ERROR;
|
||||
@@ -136,7 +91,7 @@ int main() {
|
||||
};
|
||||
|
||||
/* clean up */
|
||||
delwin(screens[active_screen].win);
|
||||
delwin(screens[active_screen].win); /* TODO: delete all windows in screens[] */
|
||||
nocbreak();
|
||||
endwin(); /* ends curses mode */
|
||||
curs_set(1);
|
8
src/error.c
Normal file
8
src/error.c
Normal file
@@ -0,0 +1,8 @@
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
|
||||
#include <curses.h>
|
||||
#include "screens.h"
|
||||
|
||||
void draw_error(struct Screen *screen, __attribute__((unused)) char *input) {
|
||||
mvwprintw(screen->win, 1, 2, "%s", error_message);
|
||||
}
|
10
src/error.h
Normal file
10
src/error.h
Normal file
@@ -0,0 +1,10 @@
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
|
||||
#ifndef ERROR_H /* header guard */
|
||||
#define ERROR_H
|
||||
|
||||
#include "screens.h"
|
||||
|
||||
void draw_error(struct Screen *screen, char *input);
|
||||
|
||||
#endif
|
30
src/globals.h
Normal file
30
src/globals.h
Normal file
@@ -0,0 +1,30 @@
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
|
||||
#define MAX_SCREEN_NAME_SIZE 20 /* size of the screen name array */
|
||||
#define MAX_ERROR_MESSAGE_SIZE 60 /* size of error_message array */
|
||||
|
||||
#ifndef GLOBALS_H /* header guard */
|
||||
#define GLOBALS_H
|
||||
|
||||
#include <curses.h>
|
||||
|
||||
extern char error_message[];
|
||||
|
||||
struct Screen {
|
||||
char name[MAX_SCREEN_NAME_SIZE];
|
||||
WINDOW *win;
|
||||
void (*draw_screen)(struct Screen *, char *input); /* GETCH_TIMEOUT determines how often this is run, in ms */
|
||||
};
|
||||
|
||||
enum Status {
|
||||
STATUS_QUIT,
|
||||
STATUS_WAITING,
|
||||
STATUS_NEED_REFRESH
|
||||
};
|
||||
|
||||
enum ActiveScreen {
|
||||
HOME,
|
||||
ERROR
|
||||
};
|
||||
|
||||
#endif
|
20
src/home.c
Normal file
20
src/home.c
Normal file
@@ -0,0 +1,20 @@
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
|
||||
#include <curses.h>
|
||||
#include "screens.h"
|
||||
|
||||
void draw_home(struct Screen *screen, char *input) {
|
||||
static char* banner = ""
|
||||
" _ _ _ \n"
|
||||
" | | | | | | \n"
|
||||
" __ _ _ __ ___ | |__ ___ _ __ ___ _ __ | | __ _ ___ ___ _ __ ___| |_ \n"
|
||||
" / _` | '_ ` _ \\| '_ \\ / _ \\ '__/ __| '_ \\| |/ _` |/ __/ _ \\ | '_ \\ / _ \\ __|\n"
|
||||
" | (_| | | | | | | |_) | __/ | \\__ \\ |_) | | (_| | (_| __/_| | | | __/ |_ \n"
|
||||
" \\__,_|_| |_| |_|_.__/ \\___|_| |___/ .__/|_|\\__,_|\\___\\___(_)_| |_|\\___|\\__|\n"
|
||||
" | | \n"
|
||||
" |_| \n";
|
||||
|
||||
mvwprintw(screen->win, 1, 2, "Thank you for visiting:");
|
||||
mvwprintw(screen->win, 2, 1, "%s", banner);
|
||||
mvwprintw(screen->win, 10, 1, "Your current input is: %s", input);
|
||||
}
|
10
src/home.h
Normal file
10
src/home.h
Normal file
@@ -0,0 +1,10 @@
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
|
||||
#ifndef HOME_H /* header guard */
|
||||
#define HOME_H
|
||||
|
||||
#include "screens.h"
|
||||
|
||||
void draw_home(struct Screen *screen, char *input);
|
||||
|
||||
#endif
|
19
src/screens.c
Normal file
19
src/screens.c
Normal file
@@ -0,0 +1,19 @@
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
|
||||
#include "globals.h"
|
||||
#include "home.h"
|
||||
#include "error.h"
|
||||
|
||||
/* NOTE: this should be compile time */
|
||||
struct Screen screens[] = {
|
||||
{
|
||||
"home",
|
||||
NULL,
|
||||
draw_home
|
||||
},
|
||||
{
|
||||
"error",
|
||||
NULL,
|
||||
draw_error
|
||||
}
|
||||
};
|
10
src/screens.h
Normal file
10
src/screens.h
Normal file
@@ -0,0 +1,10 @@
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
|
||||
#ifndef SCREENS_H /* header guard */
|
||||
#define SCREENS_H
|
||||
|
||||
#include "globals.h"
|
||||
|
||||
extern struct Screen screens[];
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user