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
DATA := data
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
@ -59,10 +67,11 @@ CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions
ASFLAGS := -g $(ARCH)
LDFLAGS = -specs=$(DEVKITPRO)/libnx/switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map)
LIBS := `sdl2-config --libs` -lSDL2_ttf -lSDL2_image
#LIBS := -lSDL2 -lSDL2_ttf -lSDL2_gfx -lSDL2_image -lnx
#LIBS := `sdl2-config --libs` -lSDL2_ttf -lSDL2_image
#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 SDL2_ttf SDL2_mixer`
#---------------------------------------------------------------------------------
# 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_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};
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
#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);
#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>
extern "C" {
#include "SDL_helper.h"
//#include "status_bar.h"
//#include "common.h"
//#include "config.h"
#include "SDL_helper.h"
#include "status_bar.h"
//#include "common.h"
//#include "config.h"
}
fz_context *ctx = NULL;
@ -24,7 +24,8 @@ BookReader::BookReader(SDL_Renderer *renderer, const char *path) {
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);
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);
if (--status_bar_visible_counter > 0) {
//if (--status_bar_visible_counter > 0) {
char title[128];
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_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);
//StatusBar_DisplayTime();
}
StatusBar_DisplayTime();
//}
SDL_RenderPresent(RENDERER);
}

View File

@ -1,11 +1,21 @@
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <switch.h>
#include <SDL2/SDL.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
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
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_Rect b = {x + 128, y, 64, 64};
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[]) {
SDL_Event event;
SDL_Window *window;
SDL_Renderer *renderer;
int done = 0, x = 0, w = 1920, h = 1080;
void Init_Services() {
twiliInitialize();
/*socketInitializeDefault();
nxlinkStdio();*/
std::cout << "Initalize Serices" << std::endl;
// mandatory at least on switch, else gfx is not properly closed
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
SDL_Log("SDL_Init: %s\n", SDL_GetError());
return -1;
Term_Services();
}
// 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")
TTF_Init();
// 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 :
// 1920 x 1080 @ 32 bpp (SDL_PIXELFORMAT_RGBA8888)
// 1280 x 720 @ 32 bpp (SDL_PIXELFORMAT_RGBA8888)
window = SDL_CreateWindow("sdl2_gles2", 0, 0, 1920, 1080, 0);
if (!window) {
WINDOW = SDL_CreateWindow("sdl2_gles2", 0, 0, 1920, 1080, 0);
if (!WINDOW) {
SDL_Log("SDL_CreateWindow: %s\n", SDL_GetError());
SDL_Quit();
return -1;
Term_Services();
}
// create a renderer (OpenGL ES2)
renderer = SDL_CreateRenderer(window, 0, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (!renderer) {
RENDERER = SDL_CreateRenderer(WINDOW, 0, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (!RENDERER) {
SDL_Log("SDL_CreateRenderer: %s\n", SDL_GetError());
SDL_Quit();
return -1;
Term_Services();
}
// Get a reference to the window surface
// This has to be ran after you create the renderer.
WINDOW_SURFACE = SDL_GetWindowSurface(WINDOW);
romfsInit();
TTF_Init();
/*TTF_Font *arialUnicode = TTF_OpenFont("RomFs:/arial-unicode-ms.ttf", 30);
if (!arialUnicode) {
SDL_Quit();
return -1;
}*/
ARIAL_UNICODE = TTF_OpenFont("romfs:/arial-unicode-ms.ttf", 30);
if (!ARIAL_UNICODE) {
Term_Services();
}
// open CONTROLLER_PLAYER_1 and CONTROLLER_PLAYER_2
// 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++) {
if (SDL_JoystickOpen(i) == NULL) {
SDL_Log("SDL_JoystickOpen: %s\n", SDL_GetError());
SDL_Quit();
return -1;
Term_Services();
}
}
}
while (!done) {
while (SDL_PollEvent(&event)) {
switch (event.type) {
int main(int argc, char *argv[]) {
/*SDL_Renderer* RENDERER;
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:
SDL_Log("Joystick %d axis %d value: %d\n",
event.jaxis.which,
event.jaxis.axis, event.jaxis.value);
EVENT.jaxis.which,
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;
case SDL_JOYBUTTONDOWN:
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
// seek for joystick #0
if (event.jbutton.which == 0) {
if (event.jbutton.button == 0) {
if (EVENT.jbutton.which == 0) {
if (EVENT.jbutton.button == 0) {
// (A) button down
} else if (event.jbutton.button == 10) {
} else if (EVENT.jbutton.button == 10) {
// (+) button down
done = 1;
}
@ -122,14 +205,9 @@ int main(int argc, char *argv[]) {
if (x > w - 192) {
x = 0;
}
}
//Menu_OpenBook(renderer, SDL_GetWindowSurface(window), font, "/switch/eBookReader/books/test.epub");
//TTF_Quit();
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}*/
//SDL_Renderer *renderer, SDL_Surface* window_surface, TTF_Font *font, char *path
Term_Services();
return 0;
}

View File

@ -1,57 +1,80 @@
#include <iostream>
extern "C" {
#include "menu_book_reader.h"
#include "common.h"
//#include "touch_helper.h"
}
#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)
{
BookReader *reader = new BookReader(renderer, path);
extern SDL_Renderer* RENDERER;
extern SDL_Window* WINDOW;
extern SDL_Surface* WINDOW_SURFACE;
void Menu_OpenBook(char *path) {
BookReader *reader = new BookReader(RENDERER, path);
/*TouchInfo touchInfo;
Touch_Init(&touchInfo);*/
while(appletMainLoop())
{
reader->draw(window_surface, font);
while(appletMainLoop()) {
reader->draw(WINDOW_SURFACE, ARIAL_UNICODE);
hidScanInput();
//Touch_Process(&touchInfo);
u64 kDown = hidKeysDown(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;
}
if ((kDown & KEY_DLEFT) || (kDown & KEY_L))
if ((kDown & KEY_DLEFT) || (kDown & KEY_L)) {
std::cout << "B | LEFT" << std::endl;
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();
}
if (kDown & KEY_LSTICK)
if (kDown & KEY_LSTICK) {
std::cout << "B | LEFT STICK KEY" << std::endl;
reader->reset_page();
}
if (kHeld & KEY_DUP)
if (kHeld & KEY_DUP) {
std::cout << "B | UP" << std::endl;
reader->zoom_in();
}
if (kHeld & KEY_DDOWN)
if (kHeld & KEY_DDOWN) {
std::cout << "B | DOWN" << std::endl;
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();
}
if (kHeld & KEY_LSTICK_DOWN)
if (kHeld & KEY_LSTICK_DOWN) {
std::cout << "B | LEFT STICK DOWN" << std::endl;
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();
}
if (kHeld & KEY_LSTICK_RIGHT)
if (kHeld & KEY_LSTICK_RIGHT) {
std::cout << "B | LEFT STICK RIGHT" << std::endl;
reader->move_page_right();
}
/*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));
}