eBookReaderSwitch/source/main.cpp

214 lines
6.3 KiB
C++
Raw Normal View History

2019-08-29 02:40:26 +00:00
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
2019-08-30 14:38:38 +00:00
#include <switch.h>
2019-08-29 02:40:26 +00:00
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#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) {
2019-08-30 14:38:38 +00:00
// R
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_Rect r = {x, y, 64, 64};
SDL_RenderFillRect(renderer, &r);
// G
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
SDL_Rect g = {x + 64, y, 64, 64};
SDL_RenderFillRect(renderer, &g);
// B
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;
2019-08-30 14:38:38 +00:00
}
2019-08-29 02:40:26 +00:00
void Init_Services() {
twiliInitialize();
/*socketInitializeDefault();
nxlinkStdio();*/
std::cout << "Initalize Serices" << std::endl;
2019-08-29 02:40:26 +00:00
// mandatory at least on switch, else gfx is not properly closed
2019-08-29 02:40:26 +00:00
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
SDL_Log("SDL_Init: %s\n", SDL_GetError());
Term_Services();
2019-08-29 02:40:26 +00:00
}
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) {
2019-08-29 02:40:26 +00:00
SDL_Log("SDL_CreateWindow: %s\n", SDL_GetError());
Term_Services();
2019-08-29 02:40:26 +00:00
}
// create a renderer (OpenGL ES2)
RENDERER = SDL_CreateRenderer(WINDOW, 0, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (!RENDERER) {
2019-08-29 02:40:26 +00:00
SDL_Log("SDL_CreateRenderer: %s\n", SDL_GetError());
Term_Services();
2019-08-29 02:40:26 +00:00
}
// Get a reference to the window surface
// This has to be ran after you create the renderer.
WINDOW_SURFACE = SDL_GetWindowSurface(WINDOW);
2019-08-30 14:38:38 +00:00
romfsInit();
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,
// else joycons are individually mapped to joystick #0, joystick #1, ...
// https://github.com/devkitPro/SDL/blob/switch-sdl2/src/joystick/switch/SDL_sysjoystick.c#L45
for (int i = 0; i < 2; i++) {
if (SDL_JoystickOpen(i) == NULL) {
SDL_Log("SDL_JoystickOpen: %s\n", SDL_GetError());
Term_Services();
}
}
}
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;
2019-08-29 02:40:26 +00:00
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) {
2019-08-30 14:38:38 +00:00
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);
// 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);
2019-08-30 14:38:38 +00:00
break;
2019-08-29 02:40:26 +00:00
2019-08-30 14:38:38 +00:00
case SDL_JOYBUTTONDOWN:
SDL_Log("Joystick %d button %d down\n",
EVENT.jbutton.which, EVENT.jbutton.button);
2019-08-30 14:38:38 +00:00
// 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) {
2019-08-30 14:38:38 +00:00
// (A) button down
} else if (EVENT.jbutton.button == 10) {
2019-08-30 14:38:38 +00:00
// (+) button down
done = 1;
}
}
break;
default:
break;
}
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
// fill window bounds
SDL_SetRenderDrawColor(renderer, 111, 111, 111, 255);
SDL_GetWindowSize(window, &w, &h);
SDL_Rect f = {0, 0, w, h};
SDL_RenderFillRect(renderer, &f);
draw_rects(renderer, x, 0);
draw_rects(renderer, x, h - 64);
SDL_RenderPresent(renderer);
x++;
if (x > w - 192) {
x = 0;
}
}*/
//SDL_Renderer *renderer, SDL_Surface* window_surface, TTF_Font *font, char *path
2019-08-29 02:40:26 +00:00
Term_Services();
return 0;
2019-08-29 02:40:26 +00:00
}