Better externalization, begin storybook class

This commit is contained in:
Dane Johnson 2021-12-23 16:59:05 -06:00
parent 0accce69f1
commit 106786279f
7 changed files with 148 additions and 60 deletions

6
.gitignore vendored
View File

@ -1,4 +1,4 @@
cyoa
cyoa.c
*.o
storybook
src/storybook
src/storybook
src/cyoa.cpp

View File

@ -1,3 +1,4 @@
.PHONY: all clean storybook
all: storybook
storybook:
$(MAKE) -C src storybook

View File

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

62
src/cyoa.h Normal file
View File

@ -0,0 +1,62 @@
#ifndef CYOA_H
#define CYOA_H
#include <vector>
#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<Choice*> *choices;
char *link;
};
Footer();
~Footer();
};
struct Page {
char *id;
char *body;
Footer footer;
Page();
~Page();
};
std::vector<Page*> CyoaParse(FILE *);
#endif /* CYOA_H */
// Local Variables:
// mode: c++
// End:

View File

@ -1,9 +1,9 @@
%{
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cassert>
#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<Choice*> *choices;
char *link;
};
Footer();
~Footer();
};
struct Page {
char *id;
char *body;
Footer footer;
Page();
~Page();
};
std::vector<Page*> pages;
Page *emit_ending();

57
src/storybook.cpp Normal file
View File

@ -0,0 +1,57 @@
#include <cstdlib>
#include <cstring>
#include "storybook.h"
void usage(const char* bin) {
fprintf(stderr, "Usage: %s <storyfile>", 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
}

19
src/storybook.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef STORYBOOK_H
#define STORYBOOK_H
#include <vector>
#include <cstdio>
#include "cyoa.h"
class Storybook {
private:
std::vector<Page*> pages;
Page *current;
public:
Storybook(FILE* fin);
void Find(const char*);
void Play();
bool IsEnded();
};
#endif /* STORYBOOK_H */