No longer crashes switch, doesn't read files yet.

This commit is contained in:
SeanOMik 2019-09-01 14:52:42 -05:00
parent 96ece6a0e3
commit f55236a39b
9 changed files with 248 additions and 76 deletions

View File

@ -42,7 +42,15 @@ BUILD := build
SOURCES := source SOURCES := source
DATA := data DATA := data
INCLUDES := include INCLUDES := include
#ROMFS := romfs ROMFS := RomFS
VERSION_MAJOR := 0
VERSION_MINOR := 1
VERSION_MICRO := 0
APP_TITLE := eBookReader
APP_AUTHOR := SeanOMik
APP_VERSION := ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_MICRO}
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
# options for code generation # options for code generation
@ -59,10 +67,11 @@ CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions
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 := `sdl2-config --libs` -lSDL2_ttf -lSDL2_image #LIBS := `sdl2-config --libs` -lSDL2_ttf -lSDL2_image
#LIBS := -lSDL2 -lSDL2_ttf -lSDL2_gfx -lSDL2_image -lnx #LIBS := `aarch64-none-elf-pkg-config --libs sdl2 SDL2_ttf SDL2_mixer`
#LIBS := `sdl2-config --libs` -lSDL2_ttf -lSDL2_image -lfreetype -lz -lnx
LIBS := `sdl2-config --libs` -lSDL2_ttf -lSDL2_image -lfreetype -lpng -ljpeg -lz -lbz2 -ltwili -lnx -lmupdf -lmupdf-third
#LIBS := `aarch64-none-elf-pkg-config --libs sdl2` -lSDL2_ttf #-lSDL2_image -lmupdf -lmupdf-third #LIBS := `aarch64-none-elf-pkg-config --libs sdl2` -lSDL2_ttf #-lSDL2_image -lmupdf -lmupdf-third
LIBS := `aarch64-none-elf-pkg-config --libs SDL2 SDL2_ttf SDL2_mixer`
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
# 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

@ -4,8 +4,7 @@
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h> #include <SDL2/SDL_ttf.h>
static inline SDL_Color SDL_MakeColour(Uint8 r, Uint8 g, Uint8 b, Uint8 a) static inline SDL_Color SDL_MakeColour(Uint8 r, Uint8 g, Uint8 b, Uint8 a) {
{
SDL_Color colour = {r, g, b, a}; SDL_Color colour = {r, g, b, a};
return colour; return colour;
} }

13
include/common.h Normal file
View File

@ -0,0 +1,13 @@
#ifndef EBOOK_READER_COMMON_H
#define EBOOK_READER_COMMON_H
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
extern SDL_Renderer* RENDERER;
extern SDL_Window* WINDOW;
extern SDL_Surface* WINDOW_SURFACE;
extern TTF_Font* ARIAL_UNICODE;
extern SDL_Event EVENT;
#endif

View File

@ -2,7 +2,10 @@
#define EBOOK_READER_MENU_BOOK_READER_H #define EBOOK_READER_MENU_BOOK_READER_H
#include <switch.h> #include <switch.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
//void Menu_OpenBook(SDL_Renderer *renderer, SDL_Surface* window_surface, TTF_Font *font, char *path);
void Menu_OpenBook(char *path); void Menu_OpenBook(char *path);
#endif #endif

6
include/status_bar.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef NX_SHELL_STATUS_BAR_H
#define NX_SHELL_STATUS_BAR_H
void StatusBar_DisplayTime(void);
#endif

View File

@ -2,10 +2,10 @@
#include <string> #include <string>
extern "C" { extern "C" {
#include "SDL_helper.h" #include "SDL_helper.h"
//#include "status_bar.h" #include "status_bar.h"
//#include "common.h" //#include "common.h"
//#include "config.h" //#include "config.h"
} }
fz_context *ctx = NULL; fz_context *ctx = NULL;
@ -24,7 +24,8 @@ BookReader::BookReader(SDL_Renderer *renderer, const char *path) {
fz_register_document_handlers(ctx); fz_register_document_handlers(ctx);
} }
doc = fz_open_document(ctx, path); //doc = fz_open_document(ctx, path);
doc = fz_open_document_with_stream(ctx, path, fz_open_file(ctx, path));
pdf = pdf_specifics(ctx, doc); pdf = pdf_specifics(ctx, doc);
pages_count = fz_count_pages(ctx, doc); pages_count = fz_count_pages(ctx, doc);
@ -93,7 +94,7 @@ void BookReader::draw(SDL_Surface *window_surface, TTF_Font *font) {
SDL_RenderCopy(RENDERER, page_texture, NULL, &rect); SDL_RenderCopy(RENDERER, page_texture, NULL, &rect);
if (--status_bar_visible_counter > 0) { //if (--status_bar_visible_counter > 0) {
char title[128]; 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);
@ -102,11 +103,11 @@ void BookReader::draw(SDL_Surface *window_surface, TTF_Font *font) {
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, 1920, 60, SDL_MakeColour(color.r, color.g, color.b, 128));
SDL_DrawText(window_surface, font, (screen_bounds.x1 - title_width) / 2, (44 - title_height) / 2, WHITE, title); SDL_DrawText(window_surface, font, (screen_bounds.x1 - title_width) / 2, (44 - title_height) / 2, WHITE, title);
//StatusBar_DisplayTime(); StatusBar_DisplayTime();
} //}
SDL_RenderPresent(RENDERER); SDL_RenderPresent(RENDERER);
} }

View File

@ -1,11 +1,21 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <iostream>
#include <switch.h> #include <switch.h>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h> #include <SDL2/SDL_ttf.h>
void draw_rects(SDL_Renderer *renderer, int x, int y) { #include <twili.h>
extern "C" {
#include "menu_book_reader.h"
#include "common.h"
#include "SDL_helper.h"
}
/*void draw_rects(SDL_Renderer *renderer, int x, int y) {
// R // R
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_Rect r = {x, y, 64, 64}; SDL_Rect r = {x, y, 64, 64};
@ -20,48 +30,75 @@ void draw_rects(SDL_Renderer *renderer, int x, int y) {
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255); SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
SDL_Rect b = {x + 128, y, 64, 64}; SDL_Rect b = {x + 128, y, 64, 64};
SDL_RenderFillRect(renderer, &b); SDL_RenderFillRect(renderer, &b);
}*/
SDL_Renderer* RENDERER;
SDL_Window* WINDOW;
SDL_Surface* WINDOW_SURFACE;
TTF_Font* ARIAL_UNICODE;
SDL_Event EVENT;
bool run = true;
void Term_Services() {
std::cout << "Terminate Serices" << std::endl;
TTF_CloseFont(ARIAL_UNICODE);
TTF_Quit();
romfsExit();
SDL_DestroyRenderer(RENDERER);
SDL_DestroyWindow(WINDOW);
SDL_Quit();
//socketExit();
twiliExit();
run = false;
} }
int main(int argc, char *argv[]) { void Init_Services() {
SDL_Event event; twiliInitialize();
SDL_Window *window; /*socketInitializeDefault();
SDL_Renderer *renderer; nxlinkStdio();*/
int done = 0, x = 0, w = 1920, h = 1080; std::cout << "Initalize Serices" << std::endl;
// mandatory at least on switch, else gfx is not properly closed // mandatory at least on switch, else gfx is not properly closed
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());
return -1; Term_Services();
} }
// create an SDL window (OpenGL ES2 always enabled) TTF_Init();
// when SDL_FULLSCREEN flag is not set, viewport is automatically handled by SDL (use SDL_SetWindowSize to "change resolution")
// create an SDL WINDOW (OpenGL ES2 always enabled)
// when SDL_FULLSCREEN flag is not set, viewport is automatically handled by SDL (use SDL_SetWINDOWSize to "change resolution")
// available switch SDL2 video modes : // available switch SDL2 video modes :
// 1920 x 1080 @ 32 bpp (SDL_PIXELFORMAT_RGBA8888) // 1920 x 1080 @ 32 bpp (SDL_PIXELFORMAT_RGBA8888)
// 1280 x 720 @ 32 bpp (SDL_PIXELFORMAT_RGBA8888) // 1280 x 720 @ 32 bpp (SDL_PIXELFORMAT_RGBA8888)
window = SDL_CreateWindow("sdl2_gles2", 0, 0, 1920, 1080, 0); WINDOW = SDL_CreateWindow("sdl2_gles2", 0, 0, 1920, 1080, 0);
if (!window) { if (!WINDOW) {
SDL_Log("SDL_CreateWindow: %s\n", SDL_GetError()); SDL_Log("SDL_CreateWindow: %s\n", SDL_GetError());
SDL_Quit(); Term_Services();
return -1;
} }
// create a renderer (OpenGL ES2) // create a renderer (OpenGL ES2)
renderer = SDL_CreateRenderer(window, 0, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); RENDERER = SDL_CreateRenderer(WINDOW, 0, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (!renderer) { if (!RENDERER) {
SDL_Log("SDL_CreateRenderer: %s\n", SDL_GetError()); SDL_Log("SDL_CreateRenderer: %s\n", SDL_GetError());
SDL_Quit(); Term_Services();
return -1;
} }
// Get a reference to the window surface
// This has to be ran after you create the renderer.
WINDOW_SURFACE = SDL_GetWindowSurface(WINDOW);
romfsInit(); romfsInit();
TTF_Init(); ARIAL_UNICODE = TTF_OpenFont("romfs:/arial-unicode-ms.ttf", 30);
/*TTF_Font *arialUnicode = TTF_OpenFont("RomFs:/arial-unicode-ms.ttf", 30); if (!ARIAL_UNICODE) {
if (!arialUnicode) { Term_Services();
SDL_Quit(); }
return -1;
}*/
// open CONTROLLER_PLAYER_1 and CONTROLLER_PLAYER_2 // open CONTROLLER_PLAYER_1 and CONTROLLER_PLAYER_2
// when railed, both joycons are mapped to joystick #0, // when railed, both joycons are mapped to joystick #0,
@ -70,29 +107,75 @@ int main(int argc, char *argv[]) {
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
if (SDL_JoystickOpen(i) == NULL) { if (SDL_JoystickOpen(i) == NULL) {
SDL_Log("SDL_JoystickOpen: %s\n", SDL_GetError()); SDL_Log("SDL_JoystickOpen: %s\n", SDL_GetError());
SDL_Quit(); Term_Services();
return -1;
} }
} }
}
while (!done) { int main(int argc, char *argv[]) {
while (SDL_PollEvent(&event)) { /*SDL_Renderer* RENDERER;
switch (event.type) { SDL_Window* WINDOW;
SDL_Surface* WINDOW_SURFACE;
TTF_Font* ARIAL_UNICODE;*/
//int x = 0, w = 1920, h = 1080;
Init_Services();
std::cout << "Opening test.epub" << std::endl;
Menu_OpenBook("/switch/eBookReader/books/test.pdf");
//Menu_OpenBook("/switch/eBookReader/books/test.epub");
while (run) {
while (SDL_PollEvent(&EVENT)) {
switch (EVENT.type) {
case SDL_JOYAXISMOTION: case SDL_JOYAXISMOTION:
SDL_Log("Joystick %d axis %d value: %d\n", SDL_Log("Joystick %d axis %d value: %d\n",
event.jaxis.which, EVENT.jaxis.which,
event.jaxis.axis, event.jaxis.value); EVENT.jaxis.axis, EVENT.jaxis.value);
break;
case SDL_JOYBUTTONDOWN:
SDL_Log("Joystick %d button %d down\n",
EVENT.jbutton.which, EVENT.jbutton.button);
// https://github.com/devkitPro/SDL/blob/switch-sdl2/src/joystick/switch/SDL_sysjoystick.c#L52
// seek for joystick #0
if (EVENT.jbutton.which == 0) {
if (EVENT.jbutton.button == 0) {
// (A) button down
} else if (EVENT.jbutton.button == 10) {
// (+) button down
run = false;
}
}
break;
default:
break;
}
}
/*SDL_ClearScreen(RENDERER, WHITE);
SDL_RenderClear(RENDERER);
SDL_RenderPresent(RENDERER);*/
}
/*while (!done) {
while (SDL_PollEvent(&EVENT)) {
switch (EVENT.type) {
case SDL_JOYAXISMOTION:
SDL_Log("Joystick %d axis %d value: %d\n",
EVENT.jaxis.which,
EVENT.jaxis.axis, EVENT.jaxis.value);
break; break;
case SDL_JOYBUTTONDOWN: case SDL_JOYBUTTONDOWN:
SDL_Log("Joystick %d button %d down\n", SDL_Log("Joystick %d button %d down\n",
event.jbutton.which, event.jbutton.button); EVENT.jbutton.which, EVENT.jbutton.button);
// https://github.com/devkitPro/SDL/blob/switch-sdl2/src/joystick/switch/SDL_sysjoystick.c#L52 // https://github.com/devkitPro/SDL/blob/switch-sdl2/src/joystick/switch/SDL_sysjoystick.c#L52
// seek for joystick #0 // seek for joystick #0
if (event.jbutton.which == 0) { if (EVENT.jbutton.which == 0) {
if (event.jbutton.button == 0) { if (EVENT.jbutton.button == 0) {
// (A) button down // (A) button down
} else if (event.jbutton.button == 10) { } else if (EVENT.jbutton.button == 10) {
// (+) button down // (+) button down
done = 1; done = 1;
} }
@ -122,14 +205,9 @@ int main(int argc, char *argv[]) {
if (x > w - 192) { if (x > w - 192) {
x = 0; x = 0;
} }
} }*/
//SDL_Renderer *renderer, SDL_Surface* window_surface, TTF_Font *font, char *path
//Menu_OpenBook(renderer, SDL_GetWindowSurface(window), font, "/switch/eBookReader/books/test.epub");
//TTF_Quit();
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
Term_Services();
return 0; return 0;
} }

View File

@ -1,57 +1,80 @@
#include <iostream>
extern "C" { extern "C" {
#include "menu_book_reader.h" #include "menu_book_reader.h"
#include "common.h"
//#include "touch_helper.h" //#include "touch_helper.h"
} }
#include "BookReader.hpp" #include "BookReader.hpp"
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
void Menu_OpenBook(SDL_Renderer *renderer, SDL_Surface* window_surface, TTF_Font *font, char *path) extern SDL_Renderer* RENDERER;
{ extern SDL_Window* WINDOW;
BookReader *reader = new BookReader(renderer, path); extern SDL_Surface* WINDOW_SURFACE;
void Menu_OpenBook(char *path) {
BookReader *reader = new BookReader(RENDERER, path);
/*TouchInfo touchInfo; /*TouchInfo touchInfo;
Touch_Init(&touchInfo);*/ Touch_Init(&touchInfo);*/
while(appletMainLoop()) while(appletMainLoop()) {
{ reader->draw(WINDOW_SURFACE, ARIAL_UNICODE);
reader->draw(window_surface, font);
hidScanInput(); hidScanInput();
//Touch_Process(&touchInfo); //Touch_Process(&touchInfo);
u64 kDown = hidKeysDown(CONTROLLER_P1_AUTO); u64 kDown = hidKeysDown(CONTROLLER_P1_AUTO);
u64 kHeld = hidKeysHeld(CONTROLLER_P1_AUTO); u64 kHeld = hidKeysHeld(CONTROLLER_P1_AUTO);
if (kDown & KEY_B) if ((kDown & KEY_B) || (kDown & KEY_PLUS)) {
std::cout << "B | PLUS" << std::endl;
break; break;
}
if ((kDown & KEY_DLEFT) || (kDown & KEY_L)) if ((kDown & KEY_DLEFT) || (kDown & KEY_L)) {
std::cout << "B | LEFT" << std::endl;
reader->previous_page(); reader->previous_page();
}
if ((kDown & KEY_DRIGHT) || (kDown & KEY_R)) if ((kDown & KEY_DRIGHT) || (kDown & KEY_R)) {
std::cout << "B | RIGHT" << std::endl;
reader->next_page(); reader->next_page();
}
if (kDown & KEY_LSTICK) if (kDown & KEY_LSTICK) {
std::cout << "B | LEFT STICK KEY" << std::endl;
reader->reset_page(); reader->reset_page();
}
if (kHeld & KEY_DUP) if (kHeld & KEY_DUP) {
std::cout << "B | UP" << std::endl;
reader->zoom_in(); reader->zoom_in();
}
if (kHeld & KEY_DDOWN) if (kHeld & KEY_DDOWN) {
std::cout << "B | DOWN" << std::endl;
reader->zoom_out(); reader->zoom_out();
}
if (kHeld & KEY_LSTICK_UP) if (kHeld & KEY_LSTICK_UP) {
std::cout << "B | LEFT STICK UP" << std::endl;
reader->move_page_up(); reader->move_page_up();
}
if (kHeld & KEY_LSTICK_DOWN) if (kHeld & KEY_LSTICK_DOWN) {
std::cout << "B | LEFT STICK DOWN" << std::endl;
reader->move_page_down(); reader->move_page_down();
}
if (kHeld & KEY_LSTICK_LEFT) if (kHeld & KEY_LSTICK_LEFT) {
std::cout << "B | LEFT STICK LEFT" << std::endl;
reader->move_page_left(); reader->move_page_left();
}
if (kHeld & KEY_LSTICK_RIGHT) if (kHeld & KEY_LSTICK_RIGHT) {
std::cout << "B | LEFT STICK RIGHT" << std::endl;
reader->move_page_right(); reader->move_page_right();
}
/*if (touchInfo.state == TouchEnded && touchInfo.tapType != TapNone) /*if (touchInfo.state == TouchEnded && touchInfo.tapType != TapNone)
{ {

40
source/status_bar.c Normal file
View File

@ -0,0 +1,40 @@
#include <time.h>
#include <switch.h>
#include "common.h"
#include "SDL_helper.h"
#include "status_bar.h"
static char *Clock_GetCurrentTime(bool _12hour) {
static char buffer[10];
u64 current_time;
timeGetCurrentTime(TimeType_UserSystemClock, &current_time);
struct tm* time_struct = gmtime((const time_t *)&current_time);
int hours = time_struct->tm_hour;
int minutes = time_struct->tm_min;
int amOrPm = 0;
if (_12hour) {
if (hours < 12)
amOrPm = 1;
if (hours == 0)
hours = 12;
else if (hours > 12)
hours = hours - 12;
if ((hours >= 1) && (hours < 10))
snprintf(buffer, 10, "%2i:%02i %s", hours, minutes, amOrPm ? "AM" : "PM");
else
snprintf(buffer, 10, "%2i:%02i %s", hours, minutes, amOrPm ? "AM" : "PM");
}
return buffer;
}
void StatusBar_DisplayTime(void) {
int width = 0, height = 0;
TTF_SizeText(ARIAL_UNICODE, Clock_GetCurrentTime(true), &width, &height);
SDL_DrawText(WINDOW_SURFACE, ARIAL_UNICODE, 1900 - width, (40 - height)/2, WHITE, Clock_GetCurrentTime(true));
}