From 914ce0d7adf9229f1162e8ae2148e8158440df82 Mon Sep 17 00:00:00 2001 From: Amber Date: Tue, 5 Aug 2025 17:30:03 -0400 Subject: [PATCH] each screen now has it's own file, and there is a framework for adding more --- bbs.c | 34 ++++------------------------------ error.c | 8 ++++++++ error.h | 10 ++++++++++ globals.h | 22 +++++++++++----------- home.c | 2 +- home.h | 2 +- screens.c | 19 +++++++++++++++++++ screens.h | 10 ++++++++++ 8 files changed, 64 insertions(+), 43 deletions(-) create mode 100644 error.c create mode 100644 error.h create mode 100644 screens.c create mode 100644 screens.h diff --git a/bbs.c b/bbs.c index fc0d9ea..c59ef6c 100644 --- a/bbs.c +++ b/bbs.c @@ -5,17 +5,13 @@ #include #include "globals.h" -#include "home.h" +#include "screens.h" const unsigned int GETCH_TIMEOUT = 10; /* in ms */ const char* LOCALE = "en_US.UTF-8"; /* enable unicode support, set to "ANSI_X3.4-1968" for ascii */ char error_message[MAX_ERROR_MESSAGE_SIZE]; -static void draw_error(struct Screen *screen, __attribute__((unused)) char *input) { - mvwprintw(screen->win, 1, 2, "%s", error_message); -} - static void draw_screen (struct Screen *screen, char *input) { screen->draw_screen(screen, input); box(screen->win, 0, 0); @@ -48,31 +44,9 @@ int main() { enum ActiveScreen active_screen = HOME; - struct Screen screens[] = { - { - "home", - newwin(terminal_height, terminal_width, 0, 0), - draw_home - }, - { - "error", - newwin(terminal_height, terminal_width, 0, 0), - draw_error - } - }; - - // 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) { diff --git a/error.c b/error.c new file mode 100644 index 0000000..03d5adf --- /dev/null +++ b/error.c @@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: MIT */ + +#include +#include "screens.h" + +void draw_error(struct Screen *screen, __attribute__((unused)) char *input) { + mvwprintw(screen->win, 1, 2, "%s", error_message); +} diff --git a/error.h b/error.h new file mode 100644 index 0000000..292a6a8 --- /dev/null +++ b/error.h @@ -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 diff --git a/globals.h b/globals.h index abfe0ed..373266a 100644 --- a/globals.h +++ b/globals.h @@ -1,11 +1,21 @@ /* SPDX-License-Identifier: MIT */ -#define MAX_ERROR_MESSAGE_SIZE 60 /* size of error_message array */ #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 + +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, @@ -17,14 +27,4 @@ enum ActiveScreen { ERROR }; -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 */ -}; - -/* TODO: put this in a screens.c file and include all screens there, that way the array can be initialized at compile - * time */ -extern struct Screen screens[]; - #endif diff --git a/home.c b/home.c index e31ad18..ad4d8d7 100644 --- a/home.c +++ b/home.c @@ -1,7 +1,7 @@ /* SPDX-License-Identifier: MIT */ #include -#include "globals.h" +#include "screens.h" void draw_home(struct Screen *screen, char *input) { static char* banner = "" diff --git a/home.h b/home.h index 74966a8..d88904c 100644 --- a/home.h +++ b/home.h @@ -3,7 +3,7 @@ #ifndef HOME_H /* header guard */ #define HOME_H -#include "globals.h" +#include "screens.h" void draw_home(struct Screen *screen, char *input); diff --git a/screens.c b/screens.c new file mode 100644 index 0000000..a506b25 --- /dev/null +++ b/screens.c @@ -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 + } +}; diff --git a/screens.h b/screens.h new file mode 100644 index 0000000..e9126bc --- /dev/null +++ b/screens.h @@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: MIT */ + +#ifndef SCREENS_H /* header guard */ +#define SCREENS_H + +#include "globals.h" + +extern struct Screen screens[]; + +#endif