From 613fa5c879d4d5da0b8a6e77212c61b831042300 Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Tue, 3 Sep 2019 17:17:53 -0500 Subject: [PATCH] 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. --- Makefile | 4 +- include/common.h | 4 +- include/helpers/SDL_helper.h | 4 +- include/textures.h | 4 +- romfs/resources/images/battery_20.png | Bin 257 -> 393 bytes .../resources/images/battery_20_charging.png | Bin 517 -> 527 bytes romfs/resources/images/battery_30.png | Bin 263 -> 395 bytes .../resources/images/battery_30_charging.png | Bin 529 -> 515 bytes romfs/resources/images/battery_low.png | Bin 295 -> 407 bytes romfs/resources/images/error.png | Bin 0 -> 437 bytes romfs/resources/images/warning.png | Bin 0 -> 447 bytes source/helpers/SDL_helper.c | 16 +-- source/main.cpp | 109 ++++++++++------ source/menus/book-chooser/MenuChooser.cpp | 122 ++++++++++++------ source/menus/book/BookReader.cpp | 38 +++--- source/menus/book/BookReader.hpp | 4 +- source/menus/book/LandscapePageLayout.cpp | 12 +- source/menus/book/PageLayout.cpp | 60 ++++----- source/menus/book/menu_book_reader.cpp | 27 ++-- source/status_bar.c | 90 ++++++------- source/textures.c | 39 +++++- 21 files changed, 309 insertions(+), 224 deletions(-) create mode 100644 romfs/resources/images/error.png create mode 100644 romfs/resources/images/warning.png diff --git a/Makefile b/Makefile index cf425b8..0477107 100644 --- a/Makefile +++ b/Makefile @@ -62,12 +62,12 @@ CFLAGS := -g -std=c++17 -Wall -O2 -ffunction-sections \ 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) 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 diff --git a/include/common.h b/include/common.h index bb34aa4..2e52aea 100644 --- a/include/common.h +++ b/include/common.h @@ -6,8 +6,8 @@ //#include "SDL_helper.h" extern SDL_Window *WINDOW; -extern SDL_Surface *WINDOW_SURFACE; +//extern SDL_Surface *WINDOW_SURFACE; 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 \ No newline at end of file diff --git a/include/helpers/SDL_helper.h b/include/helpers/SDL_helper.h index 579298d..83bd747 100644 --- a/include/helpers/SDL_helper.h +++ b/include/helpers/SDL_helper.h @@ -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_DARK SDL_MakeColour(163, 20, 20, 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 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_DARK SDL_MakeColour(0, 150, 136, 255) #define TEXT_MIN_COLOUR_DARK SDL_MakeColour(185, 185, 185, 255) diff --git a/include/textures.h b/include/textures.h index dc54758..bb40f4e 100644 --- a/include/textures.h +++ b/include/textures.h @@ -3,9 +3,9 @@ #include -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_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_Free(void); diff --git a/romfs/resources/images/battery_20.png b/romfs/resources/images/battery_20.png index f90f63f0292ba2b9807f89bf3e30b27798599076..c98c688279cfb20aa582bb13afabca8e03206f62 100644 GIT binary patch literal 393 zcmeAS@N?(olHy`uVBq!ia0vp^N+8U^1|+TAxeoy;#^NA%Cx&(BWL^R}Ea{HEjtmSN z`?>!lvI6;>1s;*b3=DjSL74G){)!Z!phSslL`iUdT1k0gQ7S`0VrE{6US4X6f{C7? zo@t7E1jw3So-U3d5$Ux*BY1k+!I7GhQS9a)0mhzc*Pw z%T&B%j7yaLpHg3T-iAG{s#>~muhDr$H=UW!1C;yD6t56;e`a~5L;15z){%*oe5Q|1 z*tAO(rp$BhwJb%8#JB-c;vd7GPzJht(nc@fQ yf2>iC&wYCkX#cKe^$Nqc=hw|R|L26k9)>5or_{wtD7*)T0)wZkpUXO@geCw^x|+fO literal 257 zcmeAS@N?(olHy`uVBq!ia0vp^N+8U^3=*07`vZ`23h)VW1=9a1103;}+JL5rl?3?( zGw=yp`mhVyv6y=4djJ3Yc1oF;l>@KwWOZktiU>~^$B>FSZ%++!R6^&SXqkz!i8F1|>)KKi4<0_$8dtS)`qmG> qg`V6Z00DDSM?wIu&K&6g000DMK}|sb0I`n?{9y$E z000SaNLh0L01m?d01m?e$8V@)0000ObVXQnQ*UN;cVTj60B~VxZgehgWpp4kE-@}O zWJg$10004iNkl&g*1WfezHOm z?!jeN3$Wc!R)0vs892>q0k-?e3L#Hm6O5AD9Jc$(3IX~{8qsfc5ItRY=(jeAUXmlw zq2Jmd`fY5`Zfy{~H%@2;;r%ht;jY0mC^E zCw#KcJmgnIKG^OjY1oeWgFDs=fgaGg7iYr;5`60000QXDLn#S9=C=j{glAf0(YP{o8l% z$M1J9XMFfrBjLb*#96BiXwU&q7srr_Ia@Cr7iuyPXn3gVDBvQvIAUW$_ulgR|97(R zbr^eoJMbfZQ+s||I`fBi6QQM=oL3iT$ORd27$zqPC5f~i+4R+9w~9*Nypv)=f0xf$ z664qzeY7JynWIo5q2-$4Bh#9m3u+2SGF|g$m+dOL#64-H{RE->89a*v_GP>=63X8+ z)!mq(e$hX1w#$;AN_n15oAY4@t4{dI4bvtk95&o^H!-FDfrRC}kSAx1V_U>y^WzKa kIZhhaEWNmW!jl)waT}%XG+)-S1p0-+)78&qol`;+0P(mo?*IS* diff --git a/romfs/resources/images/battery_30.png b/romfs/resources/images/battery_30.png index 854557807e508f6dcc4d201d30a9abf2238cac29..5a05d1a058a564f09b80c0cf7a9b8eeb193be56e 100644 GIT binary patch literal 395 zcmeAS@N?(olHy`uVBq!ia0vp^N+8U^1|+TAxeoy;#^NA%Cx&(BWL^R}Ea{HEjtmSN z`?>!lvI6;>1s;*b3=DjSL74G){)!Z!phSslL`iUdT1k0gQ7S`0VrE{6US4X6f{C7? zo@t7E1jw2{o-U3d5$YC2`pM0OoZ_e`o zLHBdTD>{^)TZS9~D${y&0;@8O{^Xyo#3%%t_aOe$IwyJEr^i5!X=Sf4Tl;>U#sAN$ cvENx16uq6o6u5r^FdP^>UHx3vIVCg!0EFkApa1{> literal 263 zcmeAS@N?(olHy`uVBq!ia0vp^N+8U^3=*07`vZ`24e$wZ1=9a120?R+4*?AlFA4Gs zX5bUH^kEmYV=?v8^}h7~^V=z9Vpa~k)-Ud;0F^{}x;TbZ%z1mtny*1YfYl))Ea1-r z_PzfDkGAQY?B?BQVD(kwD}!(6KHWCq;9x25C+i+{O)|zh-4=FVdQ&MBb@09i+X$N&HU diff --git a/romfs/resources/images/battery_30_charging.png b/romfs/resources/images/battery_30_charging.png index 66e06cd132e34285a6890e3ce370eadf4fb52c6b..01cd53a595d8a0e215113a62a31cd3a3ee720ca0 100644 GIT binary patch delta 500 zcmVZ00DDSM?wIu&K&6g000DMK}|sb0I`n?{9y$E z000SaNLh0L01m?d01m?e$8V@)0000ObVXQnQ*UN;cVTj60B~VxZgehgWpp4kE-@}O zWJg$10004WNkl2Z+q(X- zA?gIdl2xz?!ezJsX%MLeUwW4~ZYP}d7>q%>JcsliaokQg>20*6lrhKcq%0|d<95=P zl)!O2X-gt5!GB3o^Kjfw+LDMvaFEnI9JiCUgj|CaFoI=1kfUfEVDh9b%y+EZPpK3j(N{|x%OAwCR3E%8RTl6c=J#ySm qIOz_M_sFOac@uANe6ByLX}$rJWw+^d`~tWD0000IQT98&wQ0>Q^ubg z>HqfK`|Urh0 z|Npg_xsR-J(ARjs?Ea$=Q>S{5gG#DX1+gTe~DWM4fBXm6b diff --git a/romfs/resources/images/battery_low.png b/romfs/resources/images/battery_low.png index 5ad101630e05bc476403c797197932d48858c853..0bdaf42e5b2aa1f120a194478adec96cd58243b5 100644 GIT binary patch delta 392 zcmV;30eAkV0+$1j8Gi-<0025i3&H>Z00DDSM?wIu&K&6g000DMK}|sb0I`n?{9y$E z000SaNLh0L01m?d01m?e$8V@)0000ObVXQnQ*UN;cVTj60B~VxZgehgWpp4kE-@}O zWJg$100039Nkl*4o`E;O6-M3x27e8SufyiT&gMI~I2mW3 z!Wg4xuhv9LBPD67vHZ!GTzYrhpQaE>Cg~CY#FT|V%+uxso_7ZH^A26(X>p-0jo zDojR1j2H=mD}Vc*AQ;PPj2Nlw4Nnt{r8Scgk;#aNT}GN_O9wrFiZGVd7%`%PT}Gnl z4sE+#+Wekubb50|>^l8oLgw3jb@wb>*+;Cuk3E*C$BR!)P-}vg7UgM7Owm m(;K2JSF}g}P0tb$@&F6YVl|p-JYfI;002ovPDHLkU;%;@DxD$# literal 295 zcmeAS@N?(olHy`uVBq!ia0vp^N+8U^3=*07`vZ{j3-AeX1=9bi2PRCH)&-gj>EaktG3V~(?R*UeJg$Ms zv&B}u{PVwm5|7DAE@mc`cQsNS%G}c0*}aI z1_nN$9N!^EQ4OyKprAyFYeY$Kep*R+Vo@qXL1JcJiC$i6iGqoqp`K}qdj!yAMq^JG z$B+p3vy*O5VpimE`TXDBwmWU+)Nd;+cD^aO`cj3LiN8|jcpk{4`aY>U40 zNWlIP@9~5g&&1ka2=@IHHSk%;Zt*PqK!In>pO7@d)}*0uCtfme)}X!g-d z2^LFYzr=`Lz23d_K!L}8jaBQ#y&Dx5dA|x4{4NsRq_~J1%1UhKD-XF6Y!Kg==+M4x z#fsyG^MNdODC=P-UvS8k%Sm=jhlORAtXQ%9Xbnr7sI1e974C-W25Vk0IJw%k?7YM( zT5ADi-CZoH^-41Fo={!LmxDZ_UwaJlIqi2nZU)xzB-3onY?gUoArtn>L*|S=M}$dR`GX&dEsK`H;?~Y%GqDbJ9^dY$sBfI P5HonX`njxgN@xNAlo_z( literal 0 HcmV?d00001 diff --git a/romfs/resources/images/warning.png b/romfs/resources/images/warning.png new file mode 100644 index 0000000000000000000000000000000000000000..adffa49e229b61306522656407b6614adf7876bc GIT binary patch literal 447 zcmeAS@N?(olHy`uVBq!ia0vp^N+8U^1|+TAxeoy;#^NA%Cx&(BWL^R}Ea{HEjtmSN z`?>!lvI6;>1s;*b3=DjSL74G){)!Z!phSslL`iUdT1k0gQ7S`0VrE{6US4X6f{C7? zo@t7E1kf5r15X#nkO=p)lcI&14S1NV|8sxYv4zhxSzG#%PkcsNuvYg8`8(Tkce7;Z z71sZgFo|Y3tE#uLL;quA>II$RPS+C7_D!r8bR4}Ursy_2n-a4}yShxL@tNjM1MTx~ zG@5}79SCDW#Fpu)UyH517FaIPKlSQ)*%Sxii<9-T&kuI(?IZF39Y<3ja z)HTT($ fT~nXF+m<2Y*{{Uu3#|=+;mhFZ>gTe~DWM4fR=220 literal 0 HcmV?d00001 diff --git a/source/helpers/SDL_helper.c b/source/helpers/SDL_helper.c index ca859fa..d6d8402 100644 --- a/source/helpers/SDL_helper.c +++ b/source/helpers/SDL_helper.c @@ -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) { - SDL_Surface *loaded_surface = NULL; - loaded_surface = IMG_Load(path); + SDL_Surface *imageSurface = IMG_Load(path); - if (loaded_surface) - { - Uint32 colorkey = SDL_MapRGB(loaded_surface->format, 0, 0, 0); - SDL_SetColorKey(loaded_surface, SDL_TRUE, colorkey); - *texture = SDL_CreateTextureFromSurface(renderer, loaded_surface); + if (imageSurface) { + Uint32 colorkey = SDL_MapRGB(imageSurface->format, 0, 0, 0); + SDL_SetColorKey(imageSurface, SDL_TRUE, colorkey); + *texture = SDL_CreateTextureFromSurface(renderer, imageSurface); + } 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) { diff --git a/source/main.cpp b/source/main.cpp index 376e60e..1750031 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -6,89 +6,119 @@ #include #include +#include -#include +#ifdef DEBUG + #include +#endif extern "C" { #include "common.h" - //#include "SDL_helper.h" #include "textures.h" #include "MenuChooser.h" } SDL_Renderer* RENDERER; SDL_Window* WINDOW; -SDL_Surface* WINDOW_SURFACE; -TTF_Font* ARIAL; +//SDL_Surface* WINDOW_SURFACE; SDL_Event EVENT; +TTF_Font *ARIAL, *ARIAL_35, *ARIAL_30, *ARIAL_27, *ARIAL_25, *ARIAL_20, *ARIAL_15; bool run = true; void Term_Services() { std::cout << "Terminate Serices" << std::endl; + run = false; 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_Quit(); Textures_Free(); romfsExit(); + IMG_Quit(); + SDL_DestroyRenderer(RENDERER); - SDL_FreeSurface(WINDOW_SURFACE); + //SDL_FreeSurface(WINDOW_SURFACE); SDL_DestroyWindow(WINDOW); SDL_Quit(); - twiliExit(); - run = false; + #ifdef DEBUG + twiliExit(); + #endif } void Init_Services() { - timeInitialize(); - twiliInitialize(); + #ifdef DEBUG + twiliInitialize(); + #endif + std::cout << "Initalize Serices" << std::endl; + romfsInit(); + std::cout << "Initalized RomFs" << std::endl; + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) { SDL_Log("SDL_Init: %s\n", SDL_GetError()); Term_Services(); } std::cout << "Initalized SDL" << std::endl; - if(TTF_Init()==-1) { - printf("TTF_Init: %s\n", TTF_GetError()); + timeInitialize(); + 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(); } std::cout << "Initalized TTF" << std::endl; - WINDOW = SDL_CreateWindow("sdl2_gles2", 0, 0, 1280, 720, 0); - if (!WINDOW) { - SDL_Log("SDL_CreateWindow: %s\n", SDL_GetError()); + #ifdef EXPERIMENTAL + std::cout << "Loading Textures" << std::endl; + 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(); } - std::cout << "Initalized Window" << 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; + std::cout << "Retrevied Fonts" << std::endl; for (int i = 0; i < 2; i++) { if (SDL_JoystickOpen(i) == NULL) { @@ -102,9 +132,6 @@ void Init_Services() { int main(int argc, char *argv[]) { 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(); bool isBookReading = false; diff --git a/source/menus/book-chooser/MenuChooser.cpp b/source/menus/book-chooser/MenuChooser.cpp index 90cb7ad..23a6066 100644 --- a/source/menus/book-chooser/MenuChooser.cpp +++ b/source/menus/book-chooser/MenuChooser.cpp @@ -1,27 +1,52 @@ extern "C" { #include "MenuChooser.h" #include "menu_book_reader.h" - #include "common.h" #include "SDL_helper.h" + #include "common.h" + #include "textures.h" } #include #include -/*#include -#include */ #include +#include + +#include using namespace std; namespace fs = filesystem; -//using namespace std; -//extern bool readingBook; +template bool contains(std::list & 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() { int choosenIndex = 0; bool readingBook = false; + list allowedExtentions = {".pdf", ".epub", ".cbz", ".xps"}; + list warnedExtentions = {".epub", ".cbz", ".xps"}; 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()) { if (readingBook) { break; @@ -39,63 +64,84 @@ void Menu_StartChoosing() { break; } + if (kDown & KEY_B) { + isWarningOnScreen = false; + } + if (kDown & KEY_A) { int bookIndex = 0; for (const auto & entry : fs::directory_iterator(path)) { string filename = entry.path().filename().string(); - string extention = filename.substr(filename.length() - 3); - if (extention == "pdf") { + string extention = filename.substr(filename.find_last_of(".")); + + if (contains(allowedExtentions, extention)) { if (bookIndex == choosenIndex) { - char* book = (char *) entry.path().string().c_str(); - Menu_OpenBook(book); - readingBook = true; - break; + if (contains(warnedExtentions, extention)) { + #ifdef EXPERIMENTAL + SDL_DrawImage(RENDERER, warning, 5, 10 + (40 * choosingIndex)); + #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 (choosenIndex != 0) choosenIndex--; + if (choosenIndex != 0 && !isWarningOnScreen) choosenIndex--; } 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)) { string filename = entry.path().filename().string(); - string extention = filename.substr(filename.length() - 3); - if (extention == "pdf") { - if (choosenIndex == space_index) { - SDL_DrawRect(RENDERER, 15, 20 + (40 * space_index), 1265, 40, SELECTOR_COLOUR_LIGHT); + string extention = filename.substr(filename.find_last_of(".")); + + if (contains(allowedExtentions, extention)) { + 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 + + 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"); - space_index++; + 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); } -} - - -/*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); -}*/ \ No newline at end of file +} \ No newline at end of file diff --git a/source/menus/book/BookReader.cpp b/source/menus/book/BookReader.cpp index ea98af5..b397c27 100644 --- a/source/menus/book/BookReader.cpp +++ b/source/menus/book/BookReader.cpp @@ -5,11 +5,11 @@ #include //#include -//extern "C" { +extern "C" { #include "SDL_helper.h" #include "status_bar.h" //#include "config.h" -//} +} fz_context *ctx = NULL; /*config_t *config = NULL; @@ -137,28 +137,28 @@ void BookReader::draw() { layout->draw_page(); -#ifdef __SWITCH__ - if (--status_bar_visible_counter > 0) { - char *title = layout->info(); - - int title_width = 0, title_height = 0; - TTF_SizeText(ARIAL, title, &title_width, &title_height); - - //SDL_Color color = config_dark_theme ? STATUS_BAR_DARK : 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_DrawText(RENDERER, ARIAL, (1280 - title_width) / 2, (44 - title_height) / 2, WHITE, title); - - StatusBar_DisplayTime(); - } -#endif + #ifdef __SWITCH__ + if (permStatusBar || --status_bar_visible_counter > 0) { + char *title = layout->info(); + + int title_width = 0, title_height = 0; + TTF_SizeText(ARIAL_15, title, &title_width, &title_height); + + //SDL_Color color = config_dark_theme ? STATUS_BAR_DARK : STATUS_BAR_LIGHT; + SDL_Color color = STATUS_BAR_LIGHT; + + SDL_DrawRect(RENDERER, 0, 0, 1280, 45, SDL_MakeColour(color.r, color.g, color.b , 180)); + SDL_DrawText(RENDERER, ARIAL_25, (1280 - title_width) / 2, (40 - title_height) / 2, WHITE, title); + + StatusBar_DisplayTime(); + } + #endif SDL_RenderPresent(RENDERER); } 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) { diff --git a/source/menus/book/BookReader.hpp b/source/menus/book/BookReader.hpp index 9f5a47a..83c328a 100644 --- a/source/menus/book/BookReader.hpp +++ b/source/menus/book/BookReader.hpp @@ -16,7 +16,9 @@ class BookReader { public: BookReader(const char *path); ~BookReader(); - + + bool permStatusBar = false; + void previous_page(int n); void next_page(int n); void zoom_in(); diff --git a/source/menus/book/LandscapePageLayout.cpp b/source/menus/book/LandscapePageLayout.cpp index 241442b..fcb4d14 100644 --- a/source/menus/book/LandscapePageLayout.cpp +++ b/source/menus/book/LandscapePageLayout.cpp @@ -2,8 +2,7 @@ #include "common.h" #include -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; viewport.w = viewport.h; viewport.h = w; @@ -12,14 +11,12 @@ LandscapePageLayout::LandscapePageLayout(fz_document *doc, int current_page):Pag reset(); } -void LandscapePageLayout::reset() -{ +void LandscapePageLayout::reset() { page_center = fz_make_point(viewport.h / 2, viewport.w / 2); set_zoom(min_zoom); }; -void LandscapePageLayout::draw_page() -{ +void LandscapePageLayout::draw_page() { float w = page_bounds.x1 * zoom, h = page_bounds.y1 * zoom; SDL_Rect rect; @@ -31,8 +28,7 @@ void LandscapePageLayout::draw_page() 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; page_center.x = fmin(fmax(page_center.x + y, h / 2), viewport.h - h / 2); diff --git a/source/menus/book/PageLayout.cpp b/source/menus/book/PageLayout.cpp index b40d561..48e5db4 100644 --- a/source/menus/book/PageLayout.cpp +++ b/source/menus/book/PageLayout.cpp @@ -2,62 +2,51 @@ #include "common.h" #include -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); SDL_RenderGetViewport(RENDERER, &viewport); 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); } -void PageLayout::next_page(int n) -{ +void PageLayout::next_page(int n) { render_page_to_texture(_current_page + n, false); } -void PageLayout::zoom_in() -{ - set_zoom(zoom + 0.1); +void PageLayout::zoom_in() { + set_zoom(zoom + 0.03); }; -void PageLayout::zoom_out() -{ - set_zoom(zoom - 0.1); +void PageLayout::zoom_out() { + set_zoom(zoom - 0.03); }; -void PageLayout::move_up() -{ - move_page(0, 50); +void PageLayout::move_up() { + move_page(0, 3); }; -void PageLayout::move_down() -{ - move_page(0, -50); +void PageLayout::move_down() { + move_page(0, -3); }; -void PageLayout::move_left() -{ - move_page(-50, 0); +void PageLayout::move_left() { + move_page(-3, 0); }; -void PageLayout::move_right() -{ - move_page(50, 0); +void PageLayout::move_right() { + move_page(3, 0); }; -void PageLayout::reset() -{ +void PageLayout::reset() { page_center = fz_make_point(viewport.w / 2, viewport.h / 2); set_zoom(min_zoom); }; -void PageLayout::draw_page() -{ +void PageLayout::draw_page() { float w = page_bounds.x1 * zoom, h = page_bounds.y1 * zoom; SDL_Rect rect; @@ -69,15 +58,13 @@ void PageLayout::draw_page() SDL_RenderCopy(RENDERER, page_texture, NULL, &rect); } -char* PageLayout::info() -{ +char* PageLayout::info() { static char title[128]; sprintf(title, "%i/%i, %.2f%%", _current_page + 1, pages_count, zoom * 100); 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); _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_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_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); } -void PageLayout::set_zoom(float value) -{ +void PageLayout::set_zoom(float value) { value = fmin(fmax(min_zoom, value), max_zoom); if (value == zoom) @@ -118,8 +103,7 @@ void PageLayout::set_zoom(float value) 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; page_center.x = fmin(fmax(page_center.x + x, w / 2), viewport.w - w / 2); diff --git a/source/menus/book/menu_book_reader.cpp b/source/menus/book/menu_book_reader.cpp index 9e2ddbf..0489815 100644 --- a/source/menus/book/menu_book_reader.cpp +++ b/source/menus/book/menu_book_reader.cpp @@ -4,6 +4,7 @@ extern "C" { #include "common.h" } +#include #include "BookReader.hpp" void Menu_OpenBook(char *path) { @@ -68,16 +69,16 @@ void Menu_OpenBook(char *path) { } else if ((reader->currentPageLayout() == BookPageLayoutLandscape) && (hidGetHandheldMode())) { reader->move_page_right(); } - } else if (kHeld & KEY_LSTICK_LEFT) { - if (reader->currentPageLayout() == BookPageLayoutPortrait || (!hidGetHandheldMode())) { + } else if (kHeld & KEY_LSTICK_RIGHT) { + /*if (reader->currentPageLayout() == BookPageLayoutPortrait || (!hidGetHandheldMode())) { reader->move_page_left(); - } else if ((reader->currentPageLayout() == BookPageLayoutLandscape) && (hidGetHandheldMode())) { + } else */if ((reader->currentPageLayout() == BookPageLayoutLandscape) && (hidGetHandheldMode())) { reader->move_page_up(); } - } else if (kHeld & KEY_LSTICK_RIGHT) { - if (reader->currentPageLayout() == BookPageLayoutPortrait || (!hidGetHandheldMode())) { + } else if (kHeld & KEY_LSTICK_LEFT) { + /*if (reader->currentPageLayout() == BookPageLayoutPortrait || (!hidGetHandheldMode())) { reader->move_page_right(); - } else if ((reader->currentPageLayout() == BookPageLayoutLandscape) && (hidGetHandheldMode())) { + } else */if ((reader->currentPageLayout() == BookPageLayoutLandscape) && (hidGetHandheldMode())) { reader->move_page_down(); } } @@ -85,6 +86,10 @@ void Menu_OpenBook(char *path) { if (kDown & KEY_B) { break; } + + if (kDown & KEY_X) { + reader->permStatusBar = !reader->permStatusBar; + } if (kDown & KEY_LSTICK || kDown & KEY_RSTICK) { reader->reset_page(); @@ -94,12 +99,10 @@ void Menu_OpenBook(char *path) { reader->switch_page_layout(); } - /*if (touchInfo.state == TouchEnded && touchInfo.tapType != TapNone) - { + /*if (touchInfo.state == TouchEnded && touchInfo.tapType != TapNone) { float tapRegion = 120; - switch (reader->currentPageLayout()) - { + switch (reader->currentPageLayout()) { case BookPageLayoutPortrait: if (tapped_inside(touchInfo, 0, 0, tapRegion, 720)) 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; } diff --git a/source/status_bar.c b/source/status_bar.c index 3c57195..a1da81a 100644 --- a/source/status_bar.c +++ b/source/status_bar.c @@ -42,78 +42,70 @@ static void StatusBar_GetBatteryStatus(int x, int y) if (R_FAILED(psmGetChargerType(&state))) state = 0; - if (R_SUCCEEDED(psmGetBatteryChargePercentage(&percent))) - { - if (percent < 20) + if (R_SUCCEEDED(psmGetBatteryChargePercentage(&percent))) { + if (percent < 20) { SDL_DrawImage(RENDERER, battery_low, x, 3); - else if ((percent >= 20) && (percent < 30)) - { - if (state != 0) + } else if ((percent >= 20) && (percent < 30)) { + if (state != 0) { SDL_DrawImage(RENDERER, battery_20_charging, x, 3); - else + } else { SDL_DrawImage(RENDERER, battery_20, x, 3); - } - else if ((percent >= 30) && (percent < 50)) - { - if (state != 0) + } + } else if ((percent >= 30) && (percent < 50)) { + if (state != 0) { SDL_DrawImage(RENDERER, battery_50_charging, x, 3); - else + } else { SDL_DrawImage(RENDERER, battery_50, x, 3); - } - else if ((percent >= 50) && (percent < 60)) - { - if (state != 0) + } + } else if ((percent >= 50) && (percent < 60)) { + if (state != 0) { SDL_DrawImage(RENDERER, battery_50_charging, x, 3); - else + } else { SDL_DrawImage(RENDERER, battery_50, x, 3); - } - else if ((percent >= 60) && (percent < 80)) - { - if (state != 0) + } + } else if ((percent >= 60) && (percent < 80)) { + if (state != 0) { SDL_DrawImage(RENDERER, battery_60_charging, x, 3); - else + } else { SDL_DrawImage(RENDERER, battery_60, x, 3); - } - else if ((percent >= 80) && (percent < 90)) - { - if (state != 0) + } + } else if ((percent >= 80) && (percent < 90)) { + if (state != 0) { SDL_DrawImage(RENDERER, battery_80_charging, x, 3); - else + } else { SDL_DrawImage(RENDERER, battery_80, x, 3); - } - else if ((percent >= 90) && (percent < 100)) - { - if (state != 0) + } + } else if ((percent >= 90) && (percent < 100)) { + if (state != 0) { SDL_DrawImage(RENDERER, battery_90_charging, x, 3); - else + } else { SDL_DrawImage(RENDERER, battery_90, x, 3); - } - else if (percent == 100) - { - if (state != 0) + } + } else if (percent == 100) { + if (state != 0) { SDL_DrawImage(RENDERER, battery_full_charging, x, 3); - else + } else { SDL_DrawImage(RENDERER, battery_full, x, 3); + } } snprintf(buf, 5, "%d%%", percent); - TTF_SizeText(ARIAL, buf, &width, NULL); - SDL_DrawText(RENDERER, ARIAL, (x - width - 10), y, WHITE, buf); - } - else - { + TTF_SizeText(ARIAL_15, buf, &width, NULL); + SDL_DrawText(RENDERER, ARIAL_15, (x - width - 10), y, WHITE, buf); + } else { snprintf(buf, 5, "%d%%", percent); - TTF_SizeText(ARIAL, buf, &width, NULL); - SDL_DrawText(RENDERER, ARIAL, (x - width - 10), y, WHITE, buf); + TTF_SizeText(ARIAL_15, buf, &width, NULL); + SDL_DrawText(RENDERER, ARIAL_15, (x - width - 10), y, WHITE, buf); SDL_DrawImage(RENDERER, battery_unknown, x, 1); } } -void StatusBar_DisplayTime(void) -{ +void StatusBar_DisplayTime(void) { 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); - SDL_DrawText(RENDERER, ARIAL, 1260 - width, (40 - height) / 2, WHITE, Clock_GetCurrentTime()); + #ifdef EXPERIMENTAL + //StatusBar_GetBatteryStatus(1260 - width - 44, (40 - height) / 2); + #endif + SDL_DrawText(RENDERER, ARIAL_25, 1260 - width, (40 - height) / 2, WHITE, Clock_GetCurrentTime()); } \ No newline at end of file diff --git a/source/textures.c b/source/textures.c index 00c80e1..668b6dd 100644 --- a/source/textures.c +++ b/source/textures.c @@ -2,8 +2,12 @@ #include "common.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) { - 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_30, "romfs:/resources/images/battery_30.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_charging, "romfs:/resources/images/battery_full_charging.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) { - SDL_DestroyTexture(battery_unknown); + SDL_DestroyTexture(warning); + //SDL_DestroyTexture(error); + /*SDL_DestroyTexture(battery_unknown); SDL_DestroyTexture(battery_low); SDL_DestroyTexture(battery_full_charging); SDL_DestroyTexture(battery_full); @@ -36,5 +67,5 @@ void Textures_Free(void) { SDL_DestroyTexture(battery_30_charging); SDL_DestroyTexture(battery_30); SDL_DestroyTexture(battery_20_charging); - SDL_DestroyTexture(battery_20); + SDL_DestroyTexture(battery_20);*/ } \ No newline at end of file