Finally, a working build!

I've finally gotten a working build. No longer crashes your switch. So far I've only been able to get it to read most PDF files. Some PDF files do crash the app.
This commit is contained in:
SeanOMik 2019-09-03 17:17:53 -05:00
parent 7a339de16f
commit 613fa5c879
21 changed files with 309 additions and 224 deletions

View File

@ -62,12 +62,12 @@ CFLAGS := -g -std=c++17 -Wall -O2 -ffunction-sections \
CFLAGS += -D__SWITCH__ $(INCLUDE) `sdl2-config --cflags` CFLAGS += -D__SWITCH__ $(INCLUDE) `sdl2-config --cflags`
CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions -DDEBUG=1 #-DEXPERIMENTAL=1
ASFLAGS := -g $(ARCH) ASFLAGS := -g $(ARCH)
LDFLAGS = -specs=$(DEVKITPRO)/libnx/switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map) LDFLAGS = -specs=$(DEVKITPRO)/libnx/switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map)
LIBS := -lstdc++fs `sdl2-config --libs` -lSDL2_ttf -lSDL2_image -lfreetype -lpng -ljpeg -lwebp -lz -lbz2 -ltwili -lnx -lmupdf -lmupdf-third LIBS := -lstdc++fs -lSDL2_ttf -lSDL2_image -lpng -ljpeg `sdl2-config --libs` -lfreetype -lwebp -lz -lbz2 -ltwili -lnx -lmupdf -lmupdf-third
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing # list of directories containing libraries, this must be the top level containing

View File

@ -6,8 +6,8 @@
//#include "SDL_helper.h" //#include "SDL_helper.h"
extern SDL_Window *WINDOW; extern SDL_Window *WINDOW;
extern SDL_Surface *WINDOW_SURFACE; //extern SDL_Surface *WINDOW_SURFACE;
extern SDL_Renderer *RENDERER; extern SDL_Renderer *RENDERER;
extern TTF_Font *ARIAL; extern TTF_Font *ARIAL, *ARIAL_35, *ARIAL_30, *ARIAL_27, *ARIAL_25, *ARIAL_20, *ARIAL_15;
#endif #endif

View File

@ -19,8 +19,10 @@ static inline SDL_Color SDL_MakeColour(Uint8 r, Uint8 g, Uint8 b, Uint8 a) {
#define MENU_BAR_LIGHT SDL_MakeColour(240, 43, 43, 255) #define MENU_BAR_LIGHT SDL_MakeColour(240, 43, 43, 255)
#define MENU_BAR_DARK SDL_MakeColour(163, 20, 20, 255) #define MENU_BAR_DARK SDL_MakeColour(163, 20, 20, 255)
#define TEXT_MIN_COLOUR_LIGHT SDL_MakeColour(32, 32, 32, 255) #define TEXT_MIN_COLOUR_LIGHT SDL_MakeColour(32, 32, 32, 255)
#define SELECTOR_COLOUR_LIGHT SDL_MakeColour(241, 241, 241, 255) #define SELECTOR_COLOUR_LIGHT SDL_MakeColour(220, 220, 220, 255)
#define SELECTOR_COLOUR_DARK SDL_MakeColour(76, 76, 76, 255) #define SELECTOR_COLOUR_DARK SDL_MakeColour(76, 76, 76, 255)
#define HINT_COLOUR_LIGHT SDL_MakeColour(210, 210, 210, 255)
#define HINT_COLOUR_DARK SDL_MakeColour(70, 70, 70, 255)
#define TITLE_COLOUR SDL_MakeColour(30, 136, 229, 255) #define TITLE_COLOUR SDL_MakeColour(30, 136, 229, 255)
#define TITLE_COLOUR_DARK SDL_MakeColour(0, 150, 136, 255) #define TITLE_COLOUR_DARK SDL_MakeColour(0, 150, 136, 255)
#define TEXT_MIN_COLOUR_DARK SDL_MakeColour(185, 185, 185, 255) #define TEXT_MIN_COLOUR_DARK SDL_MakeColour(185, 185, 185, 255)

View File

@ -3,9 +3,9 @@
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
SDL_Texture *battery_20, *battery_20_charging, *battery_30, *battery_30_charging, *battery_50, *battery_50_charging, \ extern SDL_Texture *battery_20, *battery_20_charging, *battery_30, *battery_30_charging, *battery_50, *battery_50_charging, \
*battery_60, *battery_60_charging, *battery_80, *battery_80_charging, *battery_90, *battery_90_charging, \ *battery_60, *battery_60_charging, *battery_80, *battery_80_charging, *battery_90, *battery_90_charging, \
*battery_full, *battery_full_charging, *battery_low, *battery_unknown; *battery_full, *battery_full_charging, *battery_low, *battery_unknown, *error, *warning;
void Textures_Load(void); void Textures_Load(void);
void Textures_Free(void); void Textures_Free(void);

Binary file not shown.

Before

Width:  |  Height:  |  Size: 257 B

After

Width:  |  Height:  |  Size: 393 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 517 B

After

Width:  |  Height:  |  Size: 527 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 263 B

After

Width:  |  Height:  |  Size: 395 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 529 B

After

Width:  |  Height:  |  Size: 515 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 295 B

After

Width:  |  Height:  |  Size: 407 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 437 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 447 B

View File

@ -41,17 +41,17 @@ void SDL_DrawTextf(SDL_Renderer *renderer, TTF_Font *font, int x, int y, SDL_Col
} }
void SDL_LoadImage(SDL_Renderer *renderer, SDL_Texture **texture, char *path) { void SDL_LoadImage(SDL_Renderer *renderer, SDL_Texture **texture, char *path) {
SDL_Surface *loaded_surface = NULL; SDL_Surface *imageSurface = IMG_Load(path);
loaded_surface = IMG_Load(path);
if (loaded_surface) if (imageSurface) {
{ Uint32 colorkey = SDL_MapRGB(imageSurface->format, 0, 0, 0);
Uint32 colorkey = SDL_MapRGB(loaded_surface->format, 0, 0, 0); SDL_SetColorKey(imageSurface, SDL_TRUE, colorkey);
SDL_SetColorKey(loaded_surface, SDL_TRUE, colorkey); *texture = SDL_CreateTextureFromSurface(renderer, imageSurface);
*texture = SDL_CreateTextureFromSurface(renderer, loaded_surface); } else {
printf("Failed to load image: %c", path);
} }
SDL_FreeSurface(loaded_surface); SDL_FreeSurface(imageSurface);
} }
void SDL_DrawImage(SDL_Renderer *renderer, SDL_Texture *texture, int x, int y) { void SDL_DrawImage(SDL_Renderer *renderer, SDL_Texture *texture, int x, int y) {

View File

@ -6,89 +6,119 @@
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h> #include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_image.h>
#include <twili.h> #ifdef DEBUG
#include <twili.h>
#endif
extern "C" { extern "C" {
#include "common.h" #include "common.h"
//#include "SDL_helper.h"
#include "textures.h" #include "textures.h"
#include "MenuChooser.h" #include "MenuChooser.h"
} }
SDL_Renderer* RENDERER; SDL_Renderer* RENDERER;
SDL_Window* WINDOW; SDL_Window* WINDOW;
SDL_Surface* WINDOW_SURFACE; //SDL_Surface* WINDOW_SURFACE;
TTF_Font* ARIAL;
SDL_Event EVENT; SDL_Event EVENT;
TTF_Font *ARIAL, *ARIAL_35, *ARIAL_30, *ARIAL_27, *ARIAL_25, *ARIAL_20, *ARIAL_15;
bool run = true; bool run = true;
void Term_Services() { void Term_Services() {
std::cout << "Terminate Serices" << std::endl; std::cout << "Terminate Serices" << std::endl;
run = false;
timeExit(); timeExit();
TTF_CloseFont(ARIAL_35);
TTF_CloseFont(ARIAL_30);
TTF_CloseFont(ARIAL_27);
TTF_CloseFont(ARIAL_25);
TTF_CloseFont(ARIAL_20);
TTF_CloseFont(ARIAL_15);
TTF_CloseFont(ARIAL); TTF_CloseFont(ARIAL);
TTF_Quit(); TTF_Quit();
Textures_Free(); Textures_Free();
romfsExit(); romfsExit();
IMG_Quit();
SDL_DestroyRenderer(RENDERER); SDL_DestroyRenderer(RENDERER);
SDL_FreeSurface(WINDOW_SURFACE); //SDL_FreeSurface(WINDOW_SURFACE);
SDL_DestroyWindow(WINDOW); SDL_DestroyWindow(WINDOW);
SDL_Quit(); SDL_Quit();
twiliExit(); #ifdef DEBUG
run = false; twiliExit();
#endif
} }
void Init_Services() { void Init_Services() {
timeInitialize(); #ifdef DEBUG
twiliInitialize(); twiliInitialize();
#endif
std::cout << "Initalize Serices" << std::endl; std::cout << "Initalize Serices" << std::endl;
romfsInit();
std::cout << "Initalized RomFs" << std::endl;
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) { if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
SDL_Log("SDL_Init: %s\n", SDL_GetError()); SDL_Log("SDL_Init: %s\n", SDL_GetError());
Term_Services(); Term_Services();
} }
std::cout << "Initalized SDL" << std::endl; std::cout << "Initalized SDL" << std::endl;
if(TTF_Init()==-1) { timeInitialize();
printf("TTF_Init: %s\n", TTF_GetError()); std::cout << "Initalized Time" << std::endl;
if (SDL_CreateWindowAndRenderer(1280, 720, 0, &WINDOW, &RENDERER) == -1) {
SDL_Log("SDL_CreateWindowAndRenderer: %s\n", SDL_GetError());
Term_Services();
}
std::cout << "Initalized Window and Renderer" << std::endl;
/*WINDOW_SURFACE = SDL_GetWindowSurface(WINDOW);
if (!WINDOW_SURFACE) {
SDL_Log("SDL_GetWindowSurface: %s\n", SDL_GetError());
Term_Services();
}
std::cout << "Retrevied Window Surface" << std::endl;*/
SDL_SetRenderDrawBlendMode(RENDERER, SDL_BLENDMODE_BLEND);
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "2");
if (!IMG_Init(IMG_INIT_PNG | IMG_INIT_JPG)) {
SDL_Log("IMG_Init: %s\n", IMG_GetError());
Term_Services();
}
std::cout << "Initalized Image" << std::endl;
if(TTF_Init() == -1) {
SDL_Log("TTF_Init: %s\n", TTF_GetError());
Term_Services(); Term_Services();
} }
std::cout << "Initalized TTF" << std::endl; std::cout << "Initalized TTF" << std::endl;
WINDOW = SDL_CreateWindow("sdl2_gles2", 0, 0, 1280, 720, 0); #ifdef EXPERIMENTAL
if (!WINDOW) { std::cout << "Loading Textures" << std::endl;
SDL_Log("SDL_CreateWindow: %s\n", SDL_GetError()); Textures_Load();
#endif
ARIAL_35 = TTF_OpenFont("romfs:/resources/font/arial.ttf", 35);
ARIAL_30 = TTF_OpenFont("romfs:/resources/font/arial.ttf", 30);
ARIAL_27 = TTF_OpenFont("romfs:/resources/font/arial.ttf", 27);
ARIAL_25 = TTF_OpenFont("romfs:/resources/font/arial.ttf", 25);
ARIAL_20 = TTF_OpenFont("romfs:/resources/font/arial.ttf", 20);
ARIAL_15 = TTF_OpenFont("romfs:/resources/font/arial.ttf", 15);
ARIAL = TTF_OpenFont("romfs:/resources/font/arial.ttf", 20);
if (!ARIAL_35 || !ARIAL_25 || !ARIAL_15 || !ARIAL) {
std::cout << "Failure to retrieve fonts" << std::endl;
Term_Services(); Term_Services();
} }
std::cout << "Initalized Window" << std::endl; std::cout << "Retrevied Fonts" << std::endl;
RENDERER = SDL_CreateRenderer(WINDOW, 0, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (!RENDERER) {
SDL_Log("SDL_CreateRenderer: %s\n", SDL_GetError());
Term_Services();
}
std::cout << "Initalized Renderer" << std::endl;
WINDOW_SURFACE = SDL_GetWindowSurface(WINDOW);
std::cout << "Retrevied Window Surface" << std::endl;
romfsInit();
std::cout << "Initalized RomFs" << std::endl;
/*std::cout << "Loading Textures" << std::endl;
Textures_Load();
std::cout << "Loaded Textures" << std::endl;*/
ARIAL = TTF_OpenFont("romfs:/resources/images/arial.ttf", 35);
if (!ARIAL) {
Term_Services();
}
std::cout << "Gotten Fonts" << std::endl;
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
if (SDL_JoystickOpen(i) == NULL) { if (SDL_JoystickOpen(i) == NULL) {
@ -102,9 +132,6 @@ void Init_Services() {
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
Init_Services(); Init_Services();
//std::cout << "Opening test.pdf" << std::endl;
//Menu_OpenBook("/switch/eBookReader/books/test.pdf");
//Menu_OpenBook("/switch/eBookReader/books/test.epub");
Menu_StartChoosing(); Menu_StartChoosing();
bool isBookReading = false; bool isBookReading = false;

View File

@ -1,27 +1,52 @@
extern "C" { extern "C" {
#include "MenuChooser.h" #include "MenuChooser.h"
#include "menu_book_reader.h" #include "menu_book_reader.h"
#include "common.h"
#include "SDL_helper.h" #include "SDL_helper.h"
#include "common.h"
#include "textures.h"
} }
#include <switch.h> #include <switch.h>
#include <iostream> #include <iostream>
/*#include <dirent.h>
#include <sys/types.h>*/
#include <filesystem> #include <filesystem>
#include <bits/stdc++.h>
#include <SDL2/SDL_image.h>
using namespace std; using namespace std;
namespace fs = filesystem; namespace fs = filesystem;
//using namespace std;
//extern bool readingBook; template <typename T> bool contains(std::list<T> & listOfElements, const T & element) {
auto it = std::find(listOfElements.begin(), listOfElements.end(), element);
return it != listOfElements.end();
}
extern TTF_Font *ARIAL, *ARIAL_35, *ARIAL_25, *ARIAL_15;
void Menu_StartChoosing() { void Menu_StartChoosing() {
int choosenIndex = 0; int choosenIndex = 0;
bool readingBook = false; bool readingBook = false;
list<string> allowedExtentions = {".pdf", ".epub", ".cbz", ".xps"};
list<string> warnedExtentions = {".epub", ".cbz", ".xps"};
string path = "/switch/eBookReader/books"; string path = "/switch/eBookReader/books";
// Count the amount of allowed files
int amountOfFiles = 0;
for (const auto & entry : fs::directory_iterator(path)) {
string filename = entry.path().filename().string();
string extention = filename.substr(filename.find_last_of("."));
if (contains(allowedExtentions, extention)) {
amountOfFiles++;
}
}
bool isWarningOnScreen = false;
int windowX, windowY;
SDL_GetWindowSize(WINDOW, &windowX, &windowY);
int warningWidth = 700;
int warningHeight = 300;
while(appletMainLoop()) { while(appletMainLoop()) {
if (readingBook) { if (readingBook) {
break; break;
@ -39,63 +64,84 @@ void Menu_StartChoosing() {
break; break;
} }
if (kDown & KEY_B) {
isWarningOnScreen = false;
}
if (kDown & KEY_A) { if (kDown & KEY_A) {
int bookIndex = 0; int bookIndex = 0;
for (const auto & entry : fs::directory_iterator(path)) { for (const auto & entry : fs::directory_iterator(path)) {
string filename = entry.path().filename().string(); string filename = entry.path().filename().string();
string extention = filename.substr(filename.length() - 3); string extention = filename.substr(filename.find_last_of("."));
if (extention == "pdf") {
if (contains(allowedExtentions, extention)) {
if (bookIndex == choosenIndex) { if (bookIndex == choosenIndex) {
char* book = (char *) entry.path().string().c_str(); if (contains(warnedExtentions, extention)) {
Menu_OpenBook(book); #ifdef EXPERIMENTAL
readingBook = true; SDL_DrawImage(RENDERER, warning, 5, 10 + (40 * choosingIndex));
break; #endif
if (isWarningOnScreen) {
goto OPEN_BOOK;
} else {
isWarningOnScreen = true;
}
} else {
OPEN_BOOK:
string book = path + "/" + filename;
cout << "Opening book: " << book << endl;
Menu_OpenBook((char*) book.c_str());
readingBook = true;
break;
}
} }
choosenIndex++; bookIndex++;
} }
} }
} }
if (kDown & KEY_DUP) { if (kDown & KEY_DUP) {
if (choosenIndex != 0) choosenIndex--; if (choosenIndex != 0 && !isWarningOnScreen) choosenIndex--;
} }
if (kDown & KEY_DDOWN) { if (kDown & KEY_DDOWN) {
choosenIndex++; if (choosenIndex < amountOfFiles-1 && !isWarningOnScreen) choosenIndex++;
} }
int space_index = 0; int choosingIndex = 0;
for (const auto & entry : fs::directory_iterator(path)) { for (const auto & entry : fs::directory_iterator(path)) {
string filename = entry.path().filename().string(); string filename = entry.path().filename().string();
string extention = filename.substr(filename.length() - 3); string extention = filename.substr(filename.find_last_of("."));
if (extention == "pdf") {
if (choosenIndex == space_index) { if (contains(allowedExtentions, extention)) {
SDL_DrawRect(RENDERER, 15, 20 + (40 * space_index), 1265, 40, SELECTOR_COLOUR_LIGHT); if (choosenIndex == choosingIndex) {
SDL_DrawRect(RENDERER, 15, 15 + (40 * choosingIndex), 1265, 40, SELECTOR_COLOUR_LIGHT);
} }
SDL_DrawText(RENDERER, ARIAL, 20, 20 + (40 * space_index), BLACK, entry.path().filename().c_str()); #ifdef EXPERIMENTAL
if (contains(warnedExtentions, extention)) {
SDL_DrawImage(RENDERER, warning, 5, 10 + (40 * choosingIndex));
}
#endif
space_index++; SDL_DrawText(RENDERER, ARIAL_25, 50, 20 + (40 * choosingIndex), BLACK, entry.path().filename().c_str());
SDL_DrawText(RENDERER, ARIAL_25, windowX - 123, windowY - 35, BLACK, "\"+\" - Exit");
if (isWarningOnScreen) {
SDL_DrawRect(RENDERER, 0, 0, 1280, 720, SDL_MakeColour(50, 50, 50, 150));
SDL_DrawRect(RENDERER, (windowX - warningWidth) / 2, (windowY - warningHeight) / 2, warningWidth, warningHeight, HINT_COLOUR_LIGHT);
SDL_DrawText(RENDERER, ARIAL_30, (windowX - warningWidth) / 2 + 15, (windowY - warningHeight) / 2 + 15, BLACK, "This file is not yet fully supported, and may");
SDL_DrawText(RENDERER, ARIAL_30, (windowX - warningWidth) / 2 + 15, (windowY - warningHeight) / 2 + 50, BLACK, "cause a system, or app crash.");
SDL_DrawText(RENDERER, ARIAL_20, (windowX - warningWidth) / 2 + warningWidth - 250, (windowY - warningHeight) / 2 + warningHeight - 30, BLACK, "\"A\" - Read");
SDL_DrawText(RENDERER, ARIAL_20, (windowX - warningWidth) / 2 + warningWidth - 125, (windowY - warningHeight) / 2 + warningHeight - 30, BLACK, "\"B\" - Cancel.");
}
choosingIndex++;
} }
} }
SDL_RenderPresent(RENDERER); SDL_RenderPresent(RENDERER);
} }
} }
/*void list_dir(const char *path) {
struct dirent *entry;
DIR *dir = opendir(path);
if (dir == NULL) {
return;
}
while ((entry = readdir(dir)) != NULL) {
entry->
//cout << entry->d_name << endl;
}
closedir(dir);
}*/

View File

@ -5,11 +5,11 @@
#include <algorithm> #include <algorithm>
//#include <libconfig.h> //#include <libconfig.h>
//extern "C" { extern "C" {
#include "SDL_helper.h" #include "SDL_helper.h"
#include "status_bar.h" #include "status_bar.h"
//#include "config.h" //#include "config.h"
//} }
fz_context *ctx = NULL; fz_context *ctx = NULL;
/*config_t *config = NULL; /*config_t *config = NULL;
@ -137,28 +137,28 @@ void BookReader::draw() {
layout->draw_page(); layout->draw_page();
#ifdef __SWITCH__ #ifdef __SWITCH__
if (--status_bar_visible_counter > 0) { if (permStatusBar || --status_bar_visible_counter > 0) {
char *title = layout->info(); char *title = layout->info();
int title_width = 0, title_height = 0; int title_width = 0, title_height = 0;
TTF_SizeText(ARIAL, title, &title_width, &title_height); TTF_SizeText(ARIAL_15, title, &title_width, &title_height);
//SDL_Color color = config_dark_theme ? STATUS_BAR_DARK : STATUS_BAR_LIGHT; //SDL_Color color = config_dark_theme ? STATUS_BAR_DARK : STATUS_BAR_LIGHT;
SDL_Color color = STATUS_BAR_LIGHT; SDL_Color color = STATUS_BAR_LIGHT;
SDL_DrawRect(RENDERER, 0, 0, 1280, 40, SDL_MakeColour(color.r, color.g, color.b , 128)); SDL_DrawRect(RENDERER, 0, 0, 1280, 45, SDL_MakeColour(color.r, color.g, color.b , 180));
SDL_DrawText(RENDERER, ARIAL, (1280 - title_width) / 2, (44 - title_height) / 2, WHITE, title); SDL_DrawText(RENDERER, ARIAL_25, (1280 - title_width) / 2, (40 - title_height) / 2, WHITE, title);
StatusBar_DisplayTime(); StatusBar_DisplayTime();
} }
#endif #endif
SDL_RenderPresent(RENDERER); SDL_RenderPresent(RENDERER);
} }
void BookReader::show_status_bar() { void BookReader::show_status_bar() {
status_bar_visible_counter = 50; status_bar_visible_counter = 200;
} }
void BookReader::switch_current_page_layout(BookPageLayout bookPageLayout, int current_page) { void BookReader::switch_current_page_layout(BookPageLayout bookPageLayout, int current_page) {

View File

@ -17,6 +17,8 @@ class BookReader {
BookReader(const char *path); BookReader(const char *path);
~BookReader(); ~BookReader();
bool permStatusBar = false;
void previous_page(int n); void previous_page(int n);
void next_page(int n); void next_page(int n);
void zoom_in(); void zoom_in();

View File

@ -2,8 +2,7 @@
#include "common.h" #include "common.h"
#include <algorithm> #include <algorithm>
LandscapePageLayout::LandscapePageLayout(fz_document *doc, int current_page):PageLayout(doc, current_page) LandscapePageLayout::LandscapePageLayout(fz_document *doc, int current_page):PageLayout(doc, current_page) {
{
int w = viewport.w; int w = viewport.w;
viewport.w = viewport.h; viewport.w = viewport.h;
viewport.h = w; viewport.h = w;
@ -12,14 +11,12 @@ LandscapePageLayout::LandscapePageLayout(fz_document *doc, int current_page):Pag
reset(); reset();
} }
void LandscapePageLayout::reset() void LandscapePageLayout::reset() {
{
page_center = fz_make_point(viewport.h / 2, viewport.w / 2); page_center = fz_make_point(viewport.h / 2, viewport.w / 2);
set_zoom(min_zoom); set_zoom(min_zoom);
}; };
void LandscapePageLayout::draw_page() void LandscapePageLayout::draw_page() {
{
float w = page_bounds.x1 * zoom, h = page_bounds.y1 * zoom; float w = page_bounds.x1 * zoom, h = page_bounds.y1 * zoom;
SDL_Rect rect; SDL_Rect rect;
@ -31,8 +28,7 @@ void LandscapePageLayout::draw_page()
SDL_RenderCopyEx(RENDERER, page_texture, NULL, &rect, 90, NULL, SDL_FLIP_NONE); SDL_RenderCopyEx(RENDERER, page_texture, NULL, &rect, 90, NULL, SDL_FLIP_NONE);
} }
void LandscapePageLayout::move_page(float x, float y) void LandscapePageLayout::move_page(float x, float y) {
{
float w = page_bounds.x1 * zoom, h = page_bounds.y1 * zoom; float w = page_bounds.x1 * zoom, h = page_bounds.y1 * zoom;
page_center.x = fmin(fmax(page_center.x + y, h / 2), viewport.h - h / 2); page_center.x = fmin(fmax(page_center.x + y, h / 2), viewport.h - h / 2);

View File

@ -2,62 +2,51 @@
#include "common.h" #include "common.h"
#include <algorithm> #include <algorithm>
PageLayout::PageLayout(fz_document *doc, int current_page):doc(doc),pdf(pdf_specifics(ctx, doc)),pages_count(fz_count_pages(ctx, doc)) PageLayout::PageLayout(fz_document *doc, int current_page):doc(doc),pdf(pdf_specifics(ctx, doc)),pages_count(fz_count_pages(ctx, doc)) {
{
_current_page = std::min(std::max(0, current_page), pages_count - 1); _current_page = std::min(std::max(0, current_page), pages_count - 1);
SDL_RenderGetViewport(RENDERER, &viewport); SDL_RenderGetViewport(RENDERER, &viewport);
render_page_to_texture(_current_page, false); render_page_to_texture(_current_page, false);
} }
void PageLayout::previous_page(int n) void PageLayout::previous_page(int n) {
{
render_page_to_texture(_current_page - n, false); render_page_to_texture(_current_page - n, false);
} }
void PageLayout::next_page(int n) void PageLayout::next_page(int n) {
{
render_page_to_texture(_current_page + n, false); render_page_to_texture(_current_page + n, false);
} }
void PageLayout::zoom_in() void PageLayout::zoom_in() {
{ set_zoom(zoom + 0.03);
set_zoom(zoom + 0.1);
}; };
void PageLayout::zoom_out() void PageLayout::zoom_out() {
{ set_zoom(zoom - 0.03);
set_zoom(zoom - 0.1);
}; };
void PageLayout::move_up() void PageLayout::move_up() {
{ move_page(0, 3);
move_page(0, 50);
}; };
void PageLayout::move_down() void PageLayout::move_down() {
{ move_page(0, -3);
move_page(0, -50);
}; };
void PageLayout::move_left() void PageLayout::move_left() {
{ move_page(-3, 0);
move_page(-50, 0);
}; };
void PageLayout::move_right() void PageLayout::move_right() {
{ move_page(3, 0);
move_page(50, 0);
}; };
void PageLayout::reset() void PageLayout::reset() {
{
page_center = fz_make_point(viewport.w / 2, viewport.h / 2); page_center = fz_make_point(viewport.w / 2, viewport.h / 2);
set_zoom(min_zoom); set_zoom(min_zoom);
}; };
void PageLayout::draw_page() void PageLayout::draw_page() {
{
float w = page_bounds.x1 * zoom, h = page_bounds.y1 * zoom; float w = page_bounds.x1 * zoom, h = page_bounds.y1 * zoom;
SDL_Rect rect; SDL_Rect rect;
@ -69,15 +58,13 @@ void PageLayout::draw_page()
SDL_RenderCopy(RENDERER, page_texture, NULL, &rect); SDL_RenderCopy(RENDERER, page_texture, NULL, &rect);
} }
char* PageLayout::info() char* PageLayout::info() {
{
static char title[128]; static char title[128];
sprintf(title, "%i/%i, %.2f%%", _current_page + 1, pages_count, zoom * 100); sprintf(title, "%i/%i, %.2f%%", _current_page + 1, pages_count, zoom * 100);
return title; return title;
} }
void PageLayout::render_page_to_texture(int num, bool reset_zoom) void PageLayout::render_page_to_texture(int num, bool reset_zoom) {
{
FreeTextureIfNeeded(&page_texture); FreeTextureIfNeeded(&page_texture);
_current_page = std::min(std::max(0, num), pages_count - 1); _current_page = std::min(std::max(0, num), pages_count - 1);
@ -85,8 +72,7 @@ void PageLayout::render_page_to_texture(int num, bool reset_zoom)
fz_page *page = fz_load_page(ctx, doc, _current_page); fz_page *page = fz_load_page(ctx, doc, _current_page);
fz_rect bounds = fz_bound_page(ctx, page); fz_rect bounds = fz_bound_page(ctx, page);
if (page_bounds.x1 != bounds.x1 || page_bounds.y1 != bounds.y1 || reset_zoom) if (page_bounds.x1 != bounds.x1 || page_bounds.y1 != bounds.y1 || reset_zoom) {
{
page_bounds = bounds; page_bounds = bounds;
page_center = fz_make_point(viewport.w / 2, viewport.h / 2); page_center = fz_make_point(viewport.w / 2, viewport.h / 2);
@ -105,8 +91,7 @@ void PageLayout::render_page_to_texture(int num, bool reset_zoom)
fz_drop_page(ctx, page); fz_drop_page(ctx, page);
} }
void PageLayout::set_zoom(float value) void PageLayout::set_zoom(float value) {
{
value = fmin(fmax(min_zoom, value), max_zoom); value = fmin(fmax(min_zoom, value), max_zoom);
if (value == zoom) if (value == zoom)
@ -118,8 +103,7 @@ void PageLayout::set_zoom(float value)
move_page(0, 0); move_page(0, 0);
} }
void PageLayout::move_page(float x, float y) void PageLayout::move_page(float x, float y) {
{
float w = page_bounds.x1 * zoom, h = page_bounds.y1 * zoom; float w = page_bounds.x1 * zoom, h = page_bounds.y1 * zoom;
page_center.x = fmin(fmax(page_center.x + x, w / 2), viewport.w - w / 2); page_center.x = fmin(fmax(page_center.x + x, w / 2), viewport.w - w / 2);

View File

@ -4,6 +4,7 @@ extern "C" {
#include "common.h" #include "common.h"
} }
#include <iostream>
#include "BookReader.hpp" #include "BookReader.hpp"
void Menu_OpenBook(char *path) { void Menu_OpenBook(char *path) {
@ -68,16 +69,16 @@ void Menu_OpenBook(char *path) {
} else if ((reader->currentPageLayout() == BookPageLayoutLandscape) && (hidGetHandheldMode())) { } else if ((reader->currentPageLayout() == BookPageLayoutLandscape) && (hidGetHandheldMode())) {
reader->move_page_right(); reader->move_page_right();
} }
} else if (kHeld & KEY_LSTICK_LEFT) { } else if (kHeld & KEY_LSTICK_RIGHT) {
if (reader->currentPageLayout() == BookPageLayoutPortrait || (!hidGetHandheldMode())) { /*if (reader->currentPageLayout() == BookPageLayoutPortrait || (!hidGetHandheldMode())) {
reader->move_page_left(); reader->move_page_left();
} else if ((reader->currentPageLayout() == BookPageLayoutLandscape) && (hidGetHandheldMode())) { } else */if ((reader->currentPageLayout() == BookPageLayoutLandscape) && (hidGetHandheldMode())) {
reader->move_page_up(); reader->move_page_up();
} }
} else if (kHeld & KEY_LSTICK_RIGHT) { } else if (kHeld & KEY_LSTICK_LEFT) {
if (reader->currentPageLayout() == BookPageLayoutPortrait || (!hidGetHandheldMode())) { /*if (reader->currentPageLayout() == BookPageLayoutPortrait || (!hidGetHandheldMode())) {
reader->move_page_right(); reader->move_page_right();
} else if ((reader->currentPageLayout() == BookPageLayoutLandscape) && (hidGetHandheldMode())) { } else */if ((reader->currentPageLayout() == BookPageLayoutLandscape) && (hidGetHandheldMode())) {
reader->move_page_down(); reader->move_page_down();
} }
} }
@ -86,6 +87,10 @@ void Menu_OpenBook(char *path) {
break; break;
} }
if (kDown & KEY_X) {
reader->permStatusBar = !reader->permStatusBar;
}
if (kDown & KEY_LSTICK || kDown & KEY_RSTICK) { if (kDown & KEY_LSTICK || kDown & KEY_RSTICK) {
reader->reset_page(); reader->reset_page();
} }
@ -94,12 +99,10 @@ void Menu_OpenBook(char *path) {
reader->switch_page_layout(); reader->switch_page_layout();
} }
/*if (touchInfo.state == TouchEnded && touchInfo.tapType != TapNone) /*if (touchInfo.state == TouchEnded && touchInfo.tapType != TapNone) {
{
float tapRegion = 120; float tapRegion = 120;
switch (reader->currentPageLayout()) switch (reader->currentPageLayout()) {
{
case BookPageLayoutPortrait: case BookPageLayoutPortrait:
if (tapped_inside(touchInfo, 0, 0, tapRegion, 720)) if (tapped_inside(touchInfo, 0, 0, tapRegion, 720))
reader->previous_page(1); reader->previous_page(1);
@ -117,6 +120,8 @@ void Menu_OpenBook(char *path) {
}*/ }*/
} }
//readingBook = false; std::cout << "Exiting reader" << std::endl;
std::cout << "Opening chooser" << std::endl;
Menu_StartChoosing();
delete reader; delete reader;
} }

View File

@ -42,78 +42,70 @@ static void StatusBar_GetBatteryStatus(int x, int y)
if (R_FAILED(psmGetChargerType(&state))) if (R_FAILED(psmGetChargerType(&state)))
state = 0; state = 0;
if (R_SUCCEEDED(psmGetBatteryChargePercentage(&percent))) if (R_SUCCEEDED(psmGetBatteryChargePercentage(&percent))) {
{ if (percent < 20) {
if (percent < 20)
SDL_DrawImage(RENDERER, battery_low, x, 3); SDL_DrawImage(RENDERER, battery_low, x, 3);
else if ((percent >= 20) && (percent < 30)) } else if ((percent >= 20) && (percent < 30)) {
{ if (state != 0) {
if (state != 0)
SDL_DrawImage(RENDERER, battery_20_charging, x, 3); SDL_DrawImage(RENDERER, battery_20_charging, x, 3);
else } else {
SDL_DrawImage(RENDERER, battery_20, x, 3); SDL_DrawImage(RENDERER, battery_20, x, 3);
} }
else if ((percent >= 30) && (percent < 50)) } else if ((percent >= 30) && (percent < 50)) {
{ if (state != 0) {
if (state != 0)
SDL_DrawImage(RENDERER, battery_50_charging, x, 3); SDL_DrawImage(RENDERER, battery_50_charging, x, 3);
else } else {
SDL_DrawImage(RENDERER, battery_50, x, 3); SDL_DrawImage(RENDERER, battery_50, x, 3);
} }
else if ((percent >= 50) && (percent < 60)) } else if ((percent >= 50) && (percent < 60)) {
{ if (state != 0) {
if (state != 0)
SDL_DrawImage(RENDERER, battery_50_charging, x, 3); SDL_DrawImage(RENDERER, battery_50_charging, x, 3);
else } else {
SDL_DrawImage(RENDERER, battery_50, x, 3); SDL_DrawImage(RENDERER, battery_50, x, 3);
} }
else if ((percent >= 60) && (percent < 80)) } else if ((percent >= 60) && (percent < 80)) {
{ if (state != 0) {
if (state != 0)
SDL_DrawImage(RENDERER, battery_60_charging, x, 3); SDL_DrawImage(RENDERER, battery_60_charging, x, 3);
else } else {
SDL_DrawImage(RENDERER, battery_60, x, 3); SDL_DrawImage(RENDERER, battery_60, x, 3);
} }
else if ((percent >= 80) && (percent < 90)) } else if ((percent >= 80) && (percent < 90)) {
{ if (state != 0) {
if (state != 0)
SDL_DrawImage(RENDERER, battery_80_charging, x, 3); SDL_DrawImage(RENDERER, battery_80_charging, x, 3);
else } else {
SDL_DrawImage(RENDERER, battery_80, x, 3); SDL_DrawImage(RENDERER, battery_80, x, 3);
} }
else if ((percent >= 90) && (percent < 100)) } else if ((percent >= 90) && (percent < 100)) {
{ if (state != 0) {
if (state != 0)
SDL_DrawImage(RENDERER, battery_90_charging, x, 3); SDL_DrawImage(RENDERER, battery_90_charging, x, 3);
else } else {
SDL_DrawImage(RENDERER, battery_90, x, 3); SDL_DrawImage(RENDERER, battery_90, x, 3);
} }
else if (percent == 100) } else if (percent == 100) {
{ if (state != 0) {
if (state != 0)
SDL_DrawImage(RENDERER, battery_full_charging, x, 3); SDL_DrawImage(RENDERER, battery_full_charging, x, 3);
else } else {
SDL_DrawImage(RENDERER, battery_full, x, 3); SDL_DrawImage(RENDERER, battery_full, x, 3);
}
} }
snprintf(buf, 5, "%d%%", percent); snprintf(buf, 5, "%d%%", percent);
TTF_SizeText(ARIAL, buf, &width, NULL); TTF_SizeText(ARIAL_15, buf, &width, NULL);
SDL_DrawText(RENDERER, ARIAL, (x - width - 10), y, WHITE, buf); SDL_DrawText(RENDERER, ARIAL_15, (x - width - 10), y, WHITE, buf);
} } else {
else
{
snprintf(buf, 5, "%d%%", percent); snprintf(buf, 5, "%d%%", percent);
TTF_SizeText(ARIAL, buf, &width, NULL); TTF_SizeText(ARIAL_15, buf, &width, NULL);
SDL_DrawText(RENDERER, ARIAL, (x - width - 10), y, WHITE, buf); SDL_DrawText(RENDERER, ARIAL_15, (x - width - 10), y, WHITE, buf);
SDL_DrawImage(RENDERER, battery_unknown, x, 1); SDL_DrawImage(RENDERER, battery_unknown, x, 1);
} }
} }
void StatusBar_DisplayTime(void) void StatusBar_DisplayTime(void) {
{
int width = 0, height = 0; int width = 0, height = 0;
TTF_SizeText(ARIAL, Clock_GetCurrentTime(), &width, &height); TTF_SizeText(ARIAL_25, Clock_GetCurrentTime(), &width, &height);
//StatusBar_GetBatteryStatus(1260 - width - 44, (40 - height) / 2); #ifdef EXPERIMENTAL
SDL_DrawText(RENDERER, ARIAL, 1260 - width, (40 - height) / 2, WHITE, Clock_GetCurrentTime()); //StatusBar_GetBatteryStatus(1260 - width - 44, (40 - height) / 2);
#endif
SDL_DrawText(RENDERER, ARIAL_25, 1260 - width, (40 - height) / 2, WHITE, Clock_GetCurrentTime());
} }

View File

@ -2,8 +2,12 @@
#include "common.h" #include "common.h"
#include "SDL_helper.h" #include "SDL_helper.h"
SDL_Texture *battery_20, *battery_20_charging, *battery_30, *battery_30_charging, *battery_50, *battery_50_charging, \
*battery_60, *battery_60_charging, *battery_80, *battery_80_charging, *battery_90, *battery_90_charging, \
*battery_full, *battery_full_charging, *battery_low, *battery_unknown, *error, *warning;
void Textures_Load(void) { void Textures_Load(void) {
SDL_LoadImage(RENDERER, &battery_20, "romfs:/resources/images/resources/images/battery_20.png"); /*SDL_LoadImage(RENDERER, &battery_20, "romfs:/resources/images/battery_20.png");
SDL_LoadImage(RENDERER, &battery_20_charging, "romfs:/resources/images/battery_20_charging.png"); SDL_LoadImage(RENDERER, &battery_20_charging, "romfs:/resources/images/battery_20_charging.png");
SDL_LoadImage(RENDERER, &battery_30, "romfs:/resources/images/battery_30.png"); SDL_LoadImage(RENDERER, &battery_30, "romfs:/resources/images/battery_30.png");
SDL_LoadImage(RENDERER, &battery_30_charging, "romfs:/resources/images/battery_30_charging.png"); SDL_LoadImage(RENDERER, &battery_30_charging, "romfs:/resources/images/battery_30_charging.png");
@ -18,11 +22,38 @@ void Textures_Load(void) {
SDL_LoadImage(RENDERER, &battery_full, "romfs:/resources/images/battery_full.png"); SDL_LoadImage(RENDERER, &battery_full, "romfs:/resources/images/battery_full.png");
SDL_LoadImage(RENDERER, &battery_full_charging, "romfs:/resources/images/battery_full_charging.png"); SDL_LoadImage(RENDERER, &battery_full_charging, "romfs:/resources/images/battery_full_charging.png");
SDL_LoadImage(RENDERER, &battery_low, "romfs:/resources/images/battery_low.png"); SDL_LoadImage(RENDERER, &battery_low, "romfs:/resources/images/battery_low.png");
SDL_LoadImage(RENDERER, &battery_unknown, "romfs:/resources/images/battery_unknown.png"); SDL_LoadImage(RENDERER, &battery_unknown, "romfs:/resources/images/battery_unknown.png");*/
//SDL_LoadImage(RENDERER, &error, "romfs:/resources/images/error.png");
SDL_Surface *imageSurface = IMG_Load("romfs:/resources/images/warning.png");
if (imageSurface) {
warning = SDL_CreateTextureFromSurface(RENDERER, imageSurface);
if (warning == NULL) {
fprintf(stderr, "CreateTextureFromSurface failed: %s\n", SDL_GetError());
exit(1);
}
printf("Loaded \"romfs:/resources/images/warning.png\"\n");
} else {
printf("Failed to load image: \"romfs:/resources/images/warning.png\"\n");
}
/*if (imageSurface) {
Uint32 colorkey = SDL_MapRGB(imageSurface->format, 0, 0, 0);
SDL_SetColorKey(imageSurface, SDL_TRUE, colorkey);
warning = SDL_CreateTextureFromSurface(RENDERER, imageSurface);
} else {
printf("Failed to load image: %c", "romfs:/resources/images/warning.png");
}
SDL_FreeSurface(imageSurface);*/
//SDL_LoadImage(RENDERER, &warning, "romfs:/resources/images/warning.png");
} }
void Textures_Free(void) { void Textures_Free(void) {
SDL_DestroyTexture(battery_unknown); SDL_DestroyTexture(warning);
//SDL_DestroyTexture(error);
/*SDL_DestroyTexture(battery_unknown);
SDL_DestroyTexture(battery_low); SDL_DestroyTexture(battery_low);
SDL_DestroyTexture(battery_full_charging); SDL_DestroyTexture(battery_full_charging);
SDL_DestroyTexture(battery_full); SDL_DestroyTexture(battery_full);
@ -36,5 +67,5 @@ void Textures_Free(void) {
SDL_DestroyTexture(battery_30_charging); SDL_DestroyTexture(battery_30_charging);
SDL_DestroyTexture(battery_30); SDL_DestroyTexture(battery_30);
SDL_DestroyTexture(battery_20_charging); SDL_DestroyTexture(battery_20_charging);
SDL_DestroyTexture(battery_20); SDL_DestroyTexture(battery_20);*/
} }