each screen now has it's own file, and there is a framework for adding more

This commit is contained in:
2025-08-05 17:30:03 -04:00
parent db26be2a07
commit 914ce0d7ad
8 changed files with 64 additions and 43 deletions

34
bbs.c
View File

@@ -5,17 +5,13 @@
#include <locale.h>
#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) {

8
error.c Normal file
View 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
error.h Normal file
View 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

View File

@@ -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 <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,
@@ -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

2
home.c
View File

@@ -1,7 +1,7 @@
/* SPDX-License-Identifier: MIT */
#include <curses.h>
#include "globals.h"
#include "screens.h"
void draw_home(struct Screen *screen, char *input) {
static char* banner = ""

2
home.h
View File

@@ -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);

19
screens.c Normal file
View 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
screens.h Normal file
View 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