diff --git a/.gitignore b/.gitignore index 1a4fa05..4370f81 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -cyoa -cyoa.c +*.o storybook -src/storybook \ No newline at end of file +src/storybook +src/cyoa.cpp \ No newline at end of file diff --git a/Makefile b/Makefile index 2ec63b6..8b0bdb4 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,4 @@ +.PHONY: all clean storybook all: storybook storybook: $(MAKE) -C src storybook diff --git a/src/Makefile b/src/Makefile index 16dd71c..73dda3c 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,10 +1,9 @@ -CC = g++ -CFLAGS = -g +CXXFLAGS = -g +OBJS = cyoa.o all: storybook -storybook: main.c cyoa.c - $(CC) $(CFLAGS) main.c -o storybook -cyoa.c: cyoa.leg +storybook: storybook.cpp cyoa.o +cyoa.cpp: cyoa.leg leg -o $@ $^ clean: - rm -f cyoa.c cyoa storybook + rm -f cyoa.c $(OBJS) storybook diff --git a/src/cyoa.h b/src/cyoa.h new file mode 100644 index 0000000..e86f7a1 --- /dev/null +++ b/src/cyoa.h @@ -0,0 +1,62 @@ +#ifndef CYOA_H +#define CYOA_H + +#include + +#define STATCHECK_GT 1 +#define STATCHECK_LT 2 + +struct statcheck_t { + char *stat; + int value; + int rel; +}; + +struct statchange_t { + char *stat; + int addend; +}; + +struct Choice { + int option; + char *flavor; + statcheck_t *statcheck; + char *id; + statchange_t *statchange; + + Choice(char*, statcheck_t *statcheck, statchange_t *statchange); +}; + +enum class FooterType { + End, + Goto, + Choices +}; + +struct Footer { + FooterType type; + union { + std::vector *choices; + char *link; + }; + + Footer(); + ~Footer(); +}; + +struct Page { + char *id; + char *body; + Footer footer; + + Page(); + ~Page(); +}; + +std::vector CyoaParse(FILE *); + +#endif /* CYOA_H */ + +// Local Variables: +// mode: c++ +// End: diff --git a/src/cyoa.leg b/src/cyoa.leg index 383b1df..4e0f249 100644 --- a/src/cyoa.leg +++ b/src/cyoa.leg @@ -1,9 +1,9 @@ %{ -#include #include #include #include +#include "cyoa.h" FILE* yyin = stdin; @@ -12,56 +12,6 @@ FILE* yyin = stdin; result = (EOF == yyc) ? 0 : (*(buf) = yyc, 1); \ } -#define STATCHECK_GT 1 -#define STATCHECK_LT 2 - -struct statcheck_t { - char *stat; - int value; - int rel; -}; - -struct statchange_t { - char *stat; - int addend; -}; - -struct Choice { - int option; - char *flavor; - statcheck_t *statcheck; - char *id; - statchange_t *statchange; - - Choice(char*, statcheck_t *statcheck, statchange_t *statchange); -}; - -enum class FooterType { - End, - Goto, - Choices -}; - -struct Footer { - FooterType type; - union { - std::vector *choices; - char *link; - }; - - Footer(); - ~Footer(); -}; - -struct Page { - char *id; - char *body; - Footer footer; - - Page(); - ~Page(); -}; - std::vector pages; Page *emit_ending(); diff --git a/src/storybook.cpp b/src/storybook.cpp new file mode 100644 index 0000000..5dbe449 --- /dev/null +++ b/src/storybook.cpp @@ -0,0 +1,57 @@ +#include +#include + +#include "storybook.h" + +void usage(const char* bin) { + fprintf(stderr, "Usage: %s ", bin); + exit(1); +} + +int main(int argc, const char *argv[]) { + if (argc != 2) { + usage(argv[0]); + } + + FILE *fin = fopen(argv[1], "r"); + if (!fin) { + fprintf(stderr, "Error: could not read %s", argv[1]); + return 2; + } + Storybook sb(fin); + fclose(fin); + + sb.Find("START"); + sb.Play(); + + while (!sb.IsEnded()) { + sb.Play(); + } + + return 0; +} + + +Storybook::Storybook(FILE* fin) { + pages = CyoaParse(fin); +} + + +void Storybook::Find(const char* id) { + for (Page* page : pages) { + if (strcmp(page->id, id) == 0) { + current = page; + return; + } + } + // If not found + current = NULL; +} + +void Storybook::Play() { + printf(current->body); +} + +bool Storybook::IsEnded() { + return true; // TODO +} diff --git a/src/storybook.h b/src/storybook.h new file mode 100644 index 0000000..04862a4 --- /dev/null +++ b/src/storybook.h @@ -0,0 +1,19 @@ +#ifndef STORYBOOK_H +#define STORYBOOK_H + +#include +#include +#include "cyoa.h" + +class Storybook { +private: + std::vector pages; + Page *current; +public: + Storybook(FILE* fin); + void Find(const char*); + void Play(); + bool IsEnded(); +}; + +#endif /* STORYBOOK_H */