From d4b1c382e1a1a935881297c6bdf5bd653ee06aec Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Fri, 6 Sep 2019 23:45:10 -0500 Subject: [PATCH] Fix status bar being on side in portriat --- include/config.h | 6 +++ include/helpers/SDL_helper.h | 4 ++ include/helpers/fs.h | 14 +++++ include/status_bar.h | 2 +- source/helpers/SDL_helper.c | 69 +++++++++++++++++++++++- source/helpers/fs.c | 93 ++++++++++++++++++++++++++++++++ source/main.cpp | 8 ++- source/menus/book/BookReader.cpp | 30 +++++++---- source/menus/book/PageLayout.cpp | 14 ++++- source/status_bar.c | 12 ++++- 10 files changed, 235 insertions(+), 17 deletions(-) create mode 100644 include/config.h create mode 100644 include/helpers/fs.h create mode 100644 source/helpers/fs.c diff --git a/include/config.h b/include/config.h new file mode 100644 index 0000000..5a6c407 --- /dev/null +++ b/include/config.h @@ -0,0 +1,6 @@ +#ifndef EBOOK_READER_CONFIG_H +#define EBOOK_READER_CONFIG_H + +extern bool configDarkMode; + +#endif \ No newline at end of file diff --git a/include/helpers/SDL_helper.h b/include/helpers/SDL_helper.h index 83bd747..ba00621 100644 --- a/include/helpers/SDL_helper.h +++ b/include/helpers/SDL_helper.h @@ -32,10 +32,14 @@ void SDL_ClearScreen(SDL_Renderer *renderer, SDL_Color colour); void SDL_DrawRect(SDL_Renderer *renderer, int x, int y, int w, int h, SDL_Color colour); void SDL_DrawCircle(SDL_Renderer *renderer, int x, int y, int r, SDL_Color colour); void SDL_DrawText(SDL_Renderer *renderer, TTF_Font *font, int x, int y, SDL_Color colour, const char *text); +void SDL_DrawRotatedText(SDL_Renderer *renderer, TTF_Font *font, double rotation, int x, int y, SDL_Color colour, const char *text); void SDL_DrawTextf(SDL_Renderer *renderer, TTF_Font *font, int x, int y, SDL_Color colour, const char* text, ...); void SDL_LoadImage(SDL_Renderer *renderer, SDL_Texture **texture, char *path); void SDL_LoadImageBuf(SDL_Renderer *renderer, SDL_Texture **texture, void *mem, int size); void SDL_DrawImage(SDL_Renderer *renderer, SDL_Texture *texture, int x, int y); void SDL_DrawImageScale(SDL_Renderer *renderer, SDL_Texture *texture, int x, int y, int w, int h); +void SDL_InvertSurfaceColor(SDL_Surface *surface); +Uint32 SDL_GetPixel32(SDL_Surface *surface, int x, int y); +void SDL_PutPixel32(SDL_Surface *surface, int x, int y, Uint32 pixel); #endif \ No newline at end of file diff --git a/include/helpers/fs.h b/include/helpers/fs.h new file mode 100644 index 0000000..3b86807 --- /dev/null +++ b/include/helpers/fs.h @@ -0,0 +1,14 @@ +#ifndef NX_SHELL_FS_H +#define NX_SHELL_FS_H + +#include + +int FS_MakeDir(const char *path); +int FS_RecursiveMakeDir(const char * dir); +bool FS_FileExists(const char *path); +bool FS_DirExists(const char *path); +const char *FS_GetFileExt(const char *filename); +char *FS_GetFileModifiedTime(const char *filename); +u64 FS_GetFileSize(const char *filename); + +#endif \ No newline at end of file diff --git a/include/status_bar.h b/include/status_bar.h index 0306165..9e3a40e 100644 --- a/include/status_bar.h +++ b/include/status_bar.h @@ -1,6 +1,6 @@ #ifndef EBOOK_READER_STATUS_BAR_H #define EBOOK_READER_STATUS_BAR_H -void StatusBar_DisplayTime(void); +void StatusBar_DisplayTime(bool portriat); #endif \ No newline at end of file diff --git a/source/helpers/SDL_helper.c b/source/helpers/SDL_helper.c index d6d8402..f4a2247 100644 --- a/source/helpers/SDL_helper.c +++ b/source/helpers/SDL_helper.c @@ -31,6 +31,73 @@ void SDL_DrawText(SDL_Renderer *renderer, TTF_Font *font, int x, int y, SDL_Colo SDL_DestroyTexture(texture); } +void SDL_DrawRotatedText(SDL_Renderer *renderer, TTF_Font *font, double rotation, int x, int y, SDL_Color colour, const char *text) { + SDL_Surface *surface = TTF_RenderText_Blended_Wrapped(font, text, colour, 1280); + SDL_SetSurfaceAlphaMod(surface, colour.a); + SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface); + SDL_FreeSurface(surface); + + SDL_Rect position; + position.x = x; position.y = y; + SDL_QueryTexture(texture, NULL, NULL, &position.w, &position.h); + SDL_Point center = {position.w / 2, position.h / 2}; + SDL_Rect crop = {0, 0, &position.w, &position.h}; // the crop is what part of the image we want to display. + + SDL_SetRenderTarget(renderer, texture); + //SDL_RenderCopyEx(RENDERER, texture, &crop, &position, rotation, ¢er, SDL_FLIP_NONE); + SDL_RenderCopyEx(RENDERER, texture, NULL, &position, rotation, NULL, SDL_FLIP_NONE); + SDL_SetRenderTarget(renderer, NULL); + /*SDL_QueryTexture(texture, NULL, NULL, &position.w, &position.h); + SDL_RenderCopy(renderer, texture, NULL, &position);*/ + SDL_DestroyTexture(texture); +} + +void SDL_InvertSurfaceColor(SDL_Surface *surface) { + for(int x = 0; x < (*surface).w; x++) { + for(int y = 0; y < (*surface).h; y++) { + int r, g, b, a; + Uint32 pixel = SDL_GetPixel32(&surface, x, y); + SDL_GetRGBA(pixel, SDL_GetWindowPixelFormat(&WINDOW), &r, &g, &b, &a); + SDL_Color color = SDL_MakeColour(255 - r, 255 - g, 255 - b, a); + SDL_PutPixel32(&surface, x, y, pixel); + } + } +} + +Uint32 SDL_GetPixel32(SDL_Surface *surface, int x, int y) { + //Lock the surface. + if(SDL_MUSTLOCK(surface)) { + SDL_LockSurface(surface); + } + + //Convert the pixels to 32 bit + Uint32 *pixels = (Uint32 *)surface->pixels; + + //Get the requested pixel + return pixels[ ( y * surface->w ) + x ]; + + //Unlock when we are done. + if(SDL_MUSTLOCK(surface)) + SDL_UnlockSurface(surface); +} + +void SDL_PutPixel32(SDL_Surface *surface, int x, int y, Uint32 pixel) { + //Lock the surface. + if(SDL_MUSTLOCK(surface)) { + SDL_LockSurface(surface); + } + + //Convert the pixels to 32 bit + Uint32 *pixels = (Uint32 *)surface->pixels; + + //Set the pixel + pixels[ ( y * surface->w ) + x ] = pixel; + + //Unlock when we are done. + if(SDL_MUSTLOCK(surface)) + SDL_UnlockSurface(surface); +} + void SDL_DrawTextf(SDL_Renderer *renderer, TTF_Font *font, int x, int y, SDL_Color colour, const char* text, ...) { char buffer[256]; va_list args; @@ -48,7 +115,7 @@ void SDL_LoadImage(SDL_Renderer *renderer, SDL_Texture **texture, char *path) { SDL_SetColorKey(imageSurface, SDL_TRUE, colorkey); *texture = SDL_CreateTextureFromSurface(renderer, imageSurface); } else { - printf("Failed to load image: %c", path); + printf("Failed to load image: %s", path); } SDL_FreeSurface(imageSurface); diff --git a/source/helpers/fs.c b/source/helpers/fs.c new file mode 100644 index 0000000..a7bb453 --- /dev/null +++ b/source/helpers/fs.c @@ -0,0 +1,93 @@ +#include +#include +#include +#include + +#include "fs.h" + +int FS_MakeDir(const char *path) +{ + if (!path) + return -1; + + return mkdir(path, 0777); +} + +int FS_RecursiveMakeDir(const char * dir) +{ + int ret = 0; + char buf[256]; + char *p = NULL; + size_t len; + + snprintf(buf, sizeof(buf), "%s",dir); + len = strlen(buf); + + if (buf[len - 1] == '/') + buf[len - 1] = 0; + + for (p = buf + 1; *p; p++) + { + if (*p == '/') + { + *p = 0; + + ret = FS_MakeDir(buf); + + *p = '/'; + } + + ret = FS_MakeDir(buf); + } + + return ret; +} + +bool FS_FileExists(const char *path) +{ + FILE *temp = fopen(path, "r"); + + if (temp == NULL) + return false; + + fclose(temp); + return true; +} + +bool FS_DirExists(const char *path) +{ + struct stat info; + + if (stat(path, &info) != 0) + return false; + else if (info.st_mode & S_IFDIR) + return true; + else + return false; +} + +const char *FS_GetFileExt(const char *filename) +{ + const char *dot = strrchr(filename, '.'); + + if (!dot || dot == filename) + return ""; + + return dot + 1; +} + +char *FS_GetFileModifiedTime(const char *filename) +{ + struct stat attr; + stat(filename, &attr); + + return ctime(&attr.st_mtime); +} + +u64 FS_GetFileSize(const char *filename) +{ + struct stat st; + stat(filename, &st); + + return st.st_size; +} \ No newline at end of file diff --git a/source/main.cpp b/source/main.cpp index 1750031..d7f6b67 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -16,6 +16,8 @@ extern "C" { #include "common.h" #include "textures.h" #include "MenuChooser.h" + #include "fs.h" + #include "config.h" } SDL_Renderer* RENDERER; @@ -23,7 +25,7 @@ SDL_Window* WINDOW; //SDL_Surface* WINDOW_SURFACE; SDL_Event EVENT; TTF_Font *ARIAL, *ARIAL_35, *ARIAL_30, *ARIAL_27, *ARIAL_25, *ARIAL_20, *ARIAL_15; - +bool configDarkMode; bool run = true; void Term_Services() { @@ -127,6 +129,10 @@ void Init_Services() { } } std::cout << "Initalized Input" << std::endl; + + FS_RecursiveMakeDir("/switch/eBookReader/books"); + + configDarkMode = false; } int main(int argc, char *argv[]) { diff --git a/source/menus/book/BookReader.cpp b/source/menus/book/BookReader.cpp index b397c27..899f258 100644 --- a/source/menus/book/BookReader.cpp +++ b/source/menus/book/BookReader.cpp @@ -3,12 +3,13 @@ #include "LandscapePageLayout.hpp" #include "common.h" #include +#include //#include extern "C" { #include "SDL_helper.h" #include "status_bar.h" - //#include "config.h" + #include "config.h" } fz_context *ctx = NULL; @@ -128,10 +129,11 @@ void BookReader::switch_page_layout() { } void BookReader::draw() { - /*if (config_dark_theme == true) - SDL_ClearScreen(RENDERER, SDL_MakeColour(33, 39, 43, 255)); - else */ + if (configDarkMode == true) { + SDL_ClearScreen(RENDERER, BLACK); + } else { SDL_ClearScreen(RENDERER, WHITE); + } SDL_RenderClear(RENDERER); @@ -144,13 +146,21 @@ void BookReader::draw() { 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_Color color = configDarkMode ? STATUS_BAR_DARK : 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(); + if (_currentPageLayout == BookPageLayoutPortrait) { + 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(false); + } else if (_currentPageLayout == BookPageLayoutLandscape) { + SDL_DrawRect(RENDERER, 1280 - 45, 0, 45, 720, SDL_MakeColour(color.r, color.g, color.b , 180)); + int x = (1280 - title_width) - ((40 - title_height) / 2); + int y = (720 - title_height) / 2; + SDL_DrawRotatedText(RENDERER, ARIAL_25, (double) 90, x, y, WHITE, title); + + StatusBar_DisplayTime(true); + } } #endif diff --git a/source/menus/book/PageLayout.cpp b/source/menus/book/PageLayout.cpp index 48e5db4..2bee82b 100644 --- a/source/menus/book/PageLayout.cpp +++ b/source/menus/book/PageLayout.cpp @@ -1,7 +1,12 @@ #include "PageLayout.hpp" -#include "common.h" #include +extern "C" { + #include "common.h" + #include "config.h" + #include "SDL_helper.h" +} + 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); @@ -83,8 +88,13 @@ void PageLayout::render_page_to_texture(int num, bool reset_zoom) { fz_pixmap *pix = fz_new_pixmap_from_page_contents(ctx, page, fz_scale(zoom, zoom), fz_device_rgb(ctx), 0); SDL_Surface *image = SDL_CreateRGBSurfaceFrom(pix->samples, pix->w, pix->h, pix->n * 8, pix->w * pix->n, 0x000000FF, 0x0000FF00, 0x00FF0000, 0); + /*if (configDarkMode) { + SDL_InvertSurfaceColor(image); + }*/ + page_texture = SDL_CreateTextureFromSurface(RENDERER, image); - + //SDL_SetTextureColorMod(&page_texture, page_texture) + SDL_FreeSurface(image); fz_drop_pixmap(ctx, pix); diff --git a/source/status_bar.c b/source/status_bar.c index a1da81a..9d57c5f 100644 --- a/source/status_bar.c +++ b/source/status_bar.c @@ -100,12 +100,20 @@ static void StatusBar_GetBatteryStatus(int x, int y) } } -void StatusBar_DisplayTime(void) { +void StatusBar_DisplayTime(bool portriat) { int width = 0, height = 0; TTF_SizeText(ARIAL_25, Clock_GetCurrentTime(), &width, &height); #ifdef EXPERIMENTAL //StatusBar_GetBatteryStatus(1260 - width - 44, (40 - height) / 2); #endif - SDL_DrawText(RENDERER, ARIAL_25, 1260 - width, (40 - height) / 2, WHITE, Clock_GetCurrentTime()); + if (portriat) { + int x = (1280 - width) + height; //- ((45 - height) / 2); + int y = (720 - width) + 15; + SDL_DrawRotatedText(RENDERER, ARIAL_25, (double) 90, x, y, WHITE, Clock_GetCurrentTime()); + + //SDL_DrawRotatedText(RENDERER, ARIAL_25, (double) 90, 1270 - width, (720 - height), WHITE, Clock_GetCurrentTime()); + } else { + SDL_DrawText(RENDERER, ARIAL_25, 1260 - width, (40 - height) / 2, WHITE, Clock_GetCurrentTime()); + } } \ No newline at end of file