Skip to content

Commit

Permalink
Fix status bar being on side in portriat
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanOMik committed Sep 7, 2019
1 parent 5dec89c commit d4b1c38
Show file tree
Hide file tree
Showing 10 changed files with 235 additions and 17 deletions.
6 changes: 6 additions & 0 deletions include/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef EBOOK_READER_CONFIG_H
#define EBOOK_READER_CONFIG_H

extern bool configDarkMode;

#endif
4 changes: 4 additions & 0 deletions include/helpers/SDL_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 14 additions & 0 deletions include/helpers/fs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef NX_SHELL_FS_H
#define NX_SHELL_FS_H

#include <switch.h>

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
2 changes: 1 addition & 1 deletion include/status_bar.h
Original file line number Diff line number Diff line change
@@ -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
69 changes: 68 additions & 1 deletion source/helpers/SDL_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, &center, 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;
Expand All @@ -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);
Expand Down
93 changes: 93 additions & 0 deletions source/helpers/fs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <switch.h>

#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;
}
8 changes: 7 additions & 1 deletion source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ extern "C" {
#include "common.h"
#include "textures.h"
#include "MenuChooser.h"
#include "fs.h"
#include "config.h"
}

SDL_Renderer* RENDERER;
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() {
Expand Down Expand Up @@ -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[]) {
Expand Down
30 changes: 20 additions & 10 deletions source/menus/book/BookReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
#include "LandscapePageLayout.hpp"
#include "common.h"
#include <algorithm>
#include <iostream>
//#include <libconfig.h>

extern "C" {
#include "SDL_helper.h"
#include "status_bar.h"
//#include "config.h"
#include "config.h"
}

fz_context *ctx = NULL;
Expand Down Expand Up @@ -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);

Expand All @@ -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_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);
SDL_Color color = configDarkMode ? STATUS_BAR_DARK : STATUS_BAR_LIGHT;

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

Expand Down
14 changes: 12 additions & 2 deletions source/menus/book/PageLayout.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#include "PageLayout.hpp"
#include "common.h"
#include <algorithm>

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);

Expand Down Expand Up @@ -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);

Expand Down
12 changes: 10 additions & 2 deletions source/status_bar.c
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit d4b1c38

Please sign in to comment.