Skip to content

Commit

Permalink
feat: Added chapter-based loading
Browse files Browse the repository at this point in the history
  • Loading branch information
otidev committed Jan 11, 2024
1 parent 1981712 commit 255c0c3
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 25 deletions.
14 changes: 14 additions & 0 deletions src/Books.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ void CloseBook(Book* book) {
free(book->chapters);
}

void CloseChapter(Book* book, int chapter) {
SDL_DestroyTexture(book->chapters[chapter].tex.data);
}

void OpenBook(Book* dstBook, TTF_Font* font, BibleData* data, cJSON* jsonBooks, ezxml_t xmlBible) {
ezxml_t xmlBook = ezxml_idx(ezxml_child(xmlBible, "b"), data->usedBook);
for (dstBook->numChapters = 0; ezxml_idx(ezxml_child(xmlBook, "c"), dstBook->numChapters); dstBook->numChapters++);
Expand All @@ -182,6 +186,16 @@ void OpenBook(Book* dstBook, TTF_Font* font, BibleData* data, cJSON* jsonBooks,
}
}

void OpenChapter(Book* dstBook, TTF_Font* font, BibleData* data, cJSON* jsonBooks, ezxml_t xmlBible, int chapter) {
if (chapter > dstBook->numChapters || chapter < 0) {
fprintf(stderr, "Error: Insufficent range\n");
return;
}

dstBook->chapters[chapter].tex.data = RenderChapter(font, data, jsonBooks, xmlBible, chapter + 1);
SDL_QueryTexture(dstBook->chapters[chapter].tex.data, NULL, NULL, &dstBook->chapters[chapter].tex.width, &dstBook->chapters[chapter].tex.height);
}

void ChangeBibleVersion(char* filename, BibleData* data, ezxml_t* xmlBible, cJSON* jsonBooks) {
*xmlBible = ezxml_parse_file(filename);
if (!ezxml_attr(*xmlBible, "lang")) {
Expand Down
18 changes: 10 additions & 8 deletions src/Navigation.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ void LoadBibleIcon(BibleData* data, cJSON* jsonBooks) {
void ScrollAndZoom(Book* books, BibleData* data, TTF_Font* font, cJSON* jsonBooks, ezxml_t xmlBible, Timer* text) {
if (globalWindow->mouseScroll.y > 0 || (globalWindow->keys[SDL_SCANCODE_UP] && !globalWindow->lastKeys[SDL_SCANCODE_UP])) {
if (globalWindow->keys[SDL_SCANCODE_LCTRL]) {
CloseBook(&books[data->usedBook]);
CloseChapter(&books[data->usedBook], data->chapter);
data->magnifier += 0.2;
OpenBook(&books[data->usedBook], font, data, jsonBooks, xmlBible);
OpenChapter(&books[data->usedBook], font, data, jsonBooks, xmlBible, data->chapter);
} else {
text->start = data->scrollAmount;
if (data->scrollAmount + 200 < globalWindow->height / 2)
Expand All @@ -31,9 +31,9 @@ void ScrollAndZoom(Book* books, BibleData* data, TTF_Font* font, cJSON* jsonBook
}
} if (globalWindow->mouseScroll.y < 0 || (globalWindow->keys[SDL_SCANCODE_DOWN] && !globalWindow->lastKeys[SDL_SCANCODE_DOWN])) {
if (globalWindow->keys[SDL_SCANCODE_LCTRL]) {
CloseBook(&books[data->usedBook]);
CloseChapter(&books[data->usedBook], data->chapter);
data->magnifier -= 0.2;
OpenBook(&books[data->usedBook], font, data, jsonBooks, xmlBible);
OpenChapter(&books[data->usedBook], font, data, jsonBooks, xmlBible, data->chapter);
} else {
text->start = data->scrollAmount;
if (data->scrollAmount - 200 > -books[data->usedBook].chapters[data->chapter].tex.height + globalWindow->height / 2)
Expand All @@ -54,12 +54,13 @@ void ChangeChapter(Book* books, BibleData* data, TTF_Font* font, cJSON* jsonBook
textTransition->timePlayed = 0;
if (data->chapter + 1 == books[data->usedBook].numChapters && data->usedBook != 65) {
data->usedBook++;
OpenBook(&books[data->usedBook], font, data, jsonBooks, xmlBible);
data->chapter = 0;
OpenChapter(&books[data->usedBook], font, data, jsonBooks, xmlBible, data->chapter);
textTransition->duration = 1.2;
LoadBibleIcon(data, jsonBooks);
} else {
data->chapter++;
OpenChapter(&books[data->usedBook], font, data, jsonBooks, xmlBible, data->chapter);
textTransition->duration = 0.7;
}
}
Expand All @@ -71,12 +72,13 @@ void ChangeChapter(Book* books, BibleData* data, TTF_Font* font, cJSON* jsonBook
textTransition->timePlayed = 0;
if (data->chapter == 0 && data->usedBook != 0) {
data->usedBook--;
OpenBook(&books[data->usedBook], font, data, jsonBooks, xmlBible);
data->chapter = books[data->usedBook].numChapters - 1;
OpenChapter(&books[data->usedBook], font, data, jsonBooks, xmlBible, data->chapter);
textTransition->duration = 1.2;
LoadBibleIcon(data, jsonBooks);
} else {
data->chapter--;
OpenChapter(&books[data->usedBook], font, data, jsonBooks, xmlBible, data->chapter);
textTransition->duration = 0.7;
}
}
Expand Down Expand Up @@ -145,9 +147,9 @@ void SearchVerse(Highlight* hl, bool* lookup, Book* books, TTF_Font* font, Bible
if (data->usedBook == i) {
break;
} else {
CloseBook(&books[data->usedBook]);
CloseChapter(&books[data->usedBook], data->chapter);
data->usedBook = i;
OpenBook(&books[data->usedBook], font, data, jsonBooks, xmlBible);
OpenChapter(&books[data->usedBook], font, data, jsonBooks, xmlBible, data->chapter);
LoadBibleIcon(data, jsonBooks);
}
break;
Expand Down
8 changes: 6 additions & 2 deletions src/include/Books.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ cJSON* GetRoot(char* filename);

SDL_Texture* RenderChapter(TTF_Font* font, BibleData* data, cJSON* books, ezxml_t xmlBible, int chapter);

void CloseBook(Book* book);
// void CloseBook(Book* book);

void OpenBook(Book* dstBook, TTF_Font* font, BibleData* data, cJSON* jsonBooks, ezxml_t xmlBible);
void CloseChapter(Book* book, int chapter);

// void OpenBook(Book* dstBook, TTF_Font* font, BibleData* data, cJSON* jsonBooks, ezxml_t xmlBible);

void OpenChapter(Book* dstBook, TTF_Font* font, BibleData* data, cJSON* jsonBooks, ezxml_t xmlBible, int chapter);

void RenderBookAndBooksBeside(TTF_Font* font, int fontSize, int textWrapWidth, Book* books, int bookNumber, cJSON* jsonBooks, ezxml_t xmlBible);

Expand Down
37 changes: 22 additions & 15 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,15 @@ int main(int argc, char** argv) {
ezxml_t xmlBible = NULL;
ChangeBibleVersion(bibleVersion, &d, &xmlBible, jsonBooks);
Book* books = malloc(sizeof(Book) * (d.numBooks));

for (int i = 0; i < d.numBooks; i++) {
ezxml_t xmlBook = ezxml_idx(ezxml_child(xmlBible, "b"), i);
for (books[i].numChapters = 0; ezxml_idx(ezxml_child(xmlBook, "c"), books[i].numChapters); books[i].numChapters++);
books[i].chapters = realloc(NULL, books[i].numChapters * sizeof(Chapter));
}

if (d.usedBook >= d.numBooks) d.usedBook = 0;
OpenBook(&books[d.usedBook], font, &d, jsonBooks, xmlBible);
OpenChapter(&books[d.usedBook], font, &d, jsonBooks, xmlBible, d.chapter);
free(bibleVersion);
LoadBibleIcon(&d, jsonBooks);

Expand Down Expand Up @@ -271,7 +278,7 @@ int main(int argc, char** argv) {
int lastWindowHeight = window.height;
bool lookup = false;
bool highlightVerse = false;
bool fullscreen = true;
bool fullscreen = false;
bool notetaking = false;

bool startScreen = true, firstFrame = true;
Expand All @@ -285,25 +292,25 @@ int main(int argc, char** argv) {
ChangeChapter(books, &d, font, jsonBooks, xmlBible, &text, &textTransition);

if (globalWindow->droppedFile[0] != 0) {
CloseBook(&books[d.usedBook]);
CloseChapter(&books[d.usedBook], d.chapter);
ChangeBibleVersion(globalWindow->droppedFile, &d, &xmlBible, jsonBooks);
if (d.usedBook >= d.numBooks) d.usedBook = 0;
OpenBook(&books[d.usedBook], font, &d, jsonBooks, xmlBible);
OpenChapter(&books[d.usedBook], font, &d, jsonBooks, xmlBible, d.chapter);
for (int i = 0; i < 500; i++) globalWindow->droppedFile[i] = 0;
}

if (window.keys[SDL_SCANCODE_EQUALS] && !window.lastKeys[SDL_SCANCODE_EQUALS]) {
CloseBook(&books[d.usedBook]);
CloseChapter(&books[d.usedBook], d.chapter);
if ((int)round(d.wrapWidth * d.wrapWidthMult) < window.width - 100)
d.wrapWidthMult += 0.1;
OpenBook(&books[d.usedBook], font, &d, jsonBooks, xmlBible);
OpenChapter(&books[d.usedBook], font, &d, jsonBooks, xmlBible, d.chapter);
}

if (window.keys[SDL_SCANCODE_MINUS] && !window.lastKeys[SDL_SCANCODE_MINUS]) {
CloseBook(&books[d.usedBook]);
CloseChapter(&books[d.usedBook], d.chapter);
if ((int)round(d.wrapWidth * d.wrapWidthMult) > 200)
d.wrapWidthMult -= 0.1;
OpenBook(&books[d.usedBook], font, &d, jsonBooks, xmlBible);
OpenChapter(&books[d.usedBook], font, &d, jsonBooks, xmlBible, d.chapter);
}

if (window.keys[SDL_SCANCODE_LCTRL] && (window.keys[SDL_SCANCODE_F] && !window.lastKeys[SDL_SCANCODE_F])) {
Expand Down Expand Up @@ -333,13 +340,13 @@ int main(int argc, char** argv) {
}

if (window.keys[SDL_SCANCODE_LCTRL] && (window.keys[SDL_SCANCODE_L] && !window.lastKeys[SDL_SCANCODE_L])) {
CloseBook(&books[d.usedBook]);
CloseChapter(&books[d.usedBook], d.chapter);
if (d.versePerLine) {
d.versePerLine = false;
} else {
d.versePerLine = true;
}
OpenBook(&books[d.usedBook], font, &d, jsonBooks, xmlBible);
OpenChapter(&books[d.usedBook], font, &d, jsonBooks, xmlBible, d.chapter);
}

if (window.keys[SDL_SCANCODE_LCTRL] && (window.keys[SDL_SCANCODE_N] && !window.lastKeys[SDL_SCANCODE_N])) {
Expand Down Expand Up @@ -408,13 +415,13 @@ int main(int argc, char** argv) {
}

if ((lastWindowWidth != window.width) || (lastWindowHeight != window.height)) {
CloseBook(&books[d.usedBook]);
CloseChapter(&books[d.usedBook], d.chapter);
if ((int)round(d.wrapWidth * d.wrapWidthMult) >= window.width) {
d.wrapWidth = window.width - 100;
d.wrapWidthMult = 1;
} else
d.wrapWidth = 900;
OpenBook(&books[d.usedBook], font, &d, jsonBooks, xmlBible);
OpenChapter(&books[d.usedBook], font, &d, jsonBooks, xmlBible, d.chapter);
if (lastWindowWidth != window.width) {
lastWindowWidth = window.width;
} if (lastWindowHeight != window.height) {
Expand All @@ -425,9 +432,9 @@ int main(int argc, char** argv) {

if (d.textOffset == textTransition.end && textTransition.start != textTransition.end) {
if (textTransition.start > textTransition.end && d.chapter + 1 >= books[d.usedBook].numChapters) {
CloseBook(&books[d.usedBook + 1]);
CloseChapter(&books[d.usedBook + 1], 0);
} if (textTransition.start < textTransition.end && d.chapter - 1 < 0) {
CloseBook(&books[d.usedBook - 1]);
CloseChapter(&books[d.usedBook - 1], books[d.usedBook - 1].numChapters - 1);
}
textTransition.start = textTransition.end = 0;
}
Expand Down Expand Up @@ -524,7 +531,7 @@ int main(int argc, char** argv) {
}

SDL_DestroyTexture(lookupTex);
CloseBook(&books[d.usedBook]);
CloseChapter(&books[d.usedBook], d.chapter);

ezxml_free(xmlBible);

Expand Down

0 comments on commit 255c0c3

Please sign in to comment.