From 4117fac3754b3ce99ea648cc23fcc48dd140ed9d Mon Sep 17 00:00:00 2001 From: M4x1m3 Date: Sat, 2 Nov 2019 16:06:18 +0100 Subject: [PATCH 1/3] [ion/simulator] Add command-line arguments support Added arguments for screen-only mode, fullscreen and a usage menu, while still keeping the original functionality of the EPSILON_SDL_SCREEN_ONLY preprocessor var. --- ion/src/simulator/shared/main_sdl.cpp | 132 ++++++++++++++++---------- ion/src/simulator/shared/platform.h | 4 - 2 files changed, 82 insertions(+), 54 deletions(-) diff --git a/ion/src/simulator/shared/main_sdl.cpp b/ion/src/simulator/shared/main_sdl.cpp index 78c3fa00a..0c9ce6315 100644 --- a/ion/src/simulator/shared/main_sdl.cpp +++ b/ion/src/simulator/shared/main_sdl.cpp @@ -1,24 +1,51 @@ #include "main.h" #include "display.h" #include "platform.h" -#if !EPSILON_SDL_SCREEN_ONLY #include "layout.h" -#endif #include +#include #include +#include #include #include #include #include +static bool argument_screen_only = false; +static bool argument_fullscreen = false; + void Ion::Timing::msleep(uint32_t ms) { SDL_Delay(ms); } +void print_help(char * program_name) { + printf("Usage: %s [options]\n", program_name); + printf("Options:\n"); + printf(" -f, --fullscreen Starts the emulator in fullscreen\n"); + printf(" -s, --screen-only Disable the keyboard.\n"); + printf(" -h, --help Show this help menu.\n"); +} + int main(int argc, char * argv[]) { std::vector arguments(argv, argv + argc); + for(int i = 1; i < argc; i++) { + if(strcmp(argv[i], "-h")==0 || strcmp(argv[i], "--help")==0) { + print_help(argv[0]); + return 0; + } else if(strcmp(argv[i], "-s")==0 || strcmp(argv[i], "--screen-only")==0) { + argument_screen_only = true; + } else if(strcmp(argv[i], "-f")==0 || strcmp(argv[i], "--fullscreen")==0) { + argument_fullscreen = true; + } + } + +#if EPSILON_SDL_SCREEN_ONLY + // Still allow the use of EPSILON_SDL_SCREEN_ONLY. + argument_screen_only = true; +#endif + char * language = IonSimulatorGetLanguageCode(); if (language != nullptr) { arguments.push_back("--language"); @@ -41,13 +68,9 @@ namespace Main { static SDL_Window * sWindow = nullptr; static SDL_Renderer * sRenderer = nullptr; -#if !EPSILON_SDL_SCREEN_ONLY static SDL_Texture * sBackgroundTexture = nullptr; -#endif static bool sNeedsRefresh = false; -#if EPSILON_SDL_SCREEN_ONLY static SDL_Rect sScreenRect; -#endif void init() { if (SDL_Init(SDL_INIT_VIDEO) != 0) { @@ -55,26 +78,33 @@ void init() { return; } - sWindow = SDL_CreateWindow( - "Epsilon", - SDL_WINDOWPOS_CENTERED, - SDL_WINDOWPOS_CENTERED, + uint32_t sdl_window_args = SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_RESIZABLE; + + if (argument_fullscreen) { + sdl_window_args = SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_FULLSCREEN; + } + + if (argument_screen_only) { + sWindow = SDL_CreateWindow( + "Epsilon", + SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED, + Ion::Display::Width, Ion::Display::Height, #if EPSILON_SDL_SCREEN_ONLY - // When rendering the screen only, make a non-resizeable window that whose - // size matches the screen's - Ion::Display::Width, - Ion::Display::Height, - 0 // Default flags: no high-dpi, not resizeable. + 0 #else - 290, 555, // Otherwise use a default size that matches the whole calculator - SDL_WINDOW_ALLOW_HIGHDPI -#if EPSILON_SDL_FULLSCREEN - | SDL_WINDOW_FULLSCREEN -#else - | SDL_WINDOW_RESIZABLE + sdl_window_args #endif -#endif - ); + ); + } else { + sWindow = SDL_CreateWindow( + "Epsilon", + SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED, + 290, 555, + sdl_window_args + ); + } SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"); @@ -88,9 +118,9 @@ void init() { Display::init(sRenderer); -#if !EPSILON_SDL_SCREEN_ONLY - sBackgroundTexture = IonSimulatorLoadImage(sRenderer, "background.jpg"); -#endif + if (!argument_screen_only) { + sBackgroundTexture = IonSimulatorLoadImage(sRenderer, "background.jpg"); + } relayout(); } @@ -101,19 +131,19 @@ void relayout() { SDL_GetWindowSize(sWindow, &windowWidth, &windowHeight); SDL_RenderSetLogicalSize(sRenderer, windowWidth, windowHeight); -#if EPSILON_SDL_SCREEN_ONLY - sScreenRect.x = 0; - sScreenRect.y = 0; - sScreenRect.w = windowWidth; - sScreenRect.h = windowHeight; -#else - Layout::recompute(windowWidth, windowHeight); - SDL_Rect backgroundRect; - Layout::getBackgroundRect(&backgroundRect); + if (argument_screen_only) { + sScreenRect.x = 0; + sScreenRect.y = 0; + sScreenRect.w = windowWidth; + sScreenRect.h = windowHeight; + } else { + Layout::recompute(windowWidth, windowHeight); + SDL_Rect backgroundRect; + Layout::getBackgroundRect(&backgroundRect); - SDL_RenderCopy(sRenderer, sBackgroundTexture, nullptr, &backgroundRect); - SDL_RenderPresent(sRenderer); -#endif + SDL_RenderCopy(sRenderer, sBackgroundTexture, nullptr, &backgroundRect); + SDL_RenderPresent(sRenderer); + } setNeedsRefresh(); } @@ -126,19 +156,21 @@ void refresh() { if (!sNeedsRefresh) { return; } -#if EPSILON_SDL_SCREEN_ONLY - Display::draw(sRenderer, &sScreenRect); -#else - SDL_Rect screenRect; - Layout::getScreenRect(&screenRect); - SDL_Rect backgroundRect; - Layout::getBackgroundRect(&backgroundRect); - SDL_SetRenderDrawColor(sRenderer, 194, 194, 194, 255); - SDL_RenderClear(sRenderer); - SDL_RenderCopy(sRenderer, sBackgroundTexture, nullptr, &backgroundRect); - Display::draw(sRenderer, &screenRect); -#endif + if (argument_screen_only) { + Display::draw(sRenderer, &sScreenRect); + } else { + SDL_Rect screenRect; + Layout::getScreenRect(&screenRect); + SDL_Rect backgroundRect; + Layout::getBackgroundRect(&backgroundRect); + + SDL_SetRenderDrawColor(sRenderer, 194, 194, 194, 255); + SDL_RenderClear(sRenderer); + SDL_RenderCopy(sRenderer, sBackgroundTexture, nullptr, &backgroundRect); + Display::draw(sRenderer, &screenRect); + } + SDL_RenderPresent(sRenderer); IonSimulatorCallbackDidRefresh(); diff --git a/ion/src/simulator/shared/platform.h b/ion/src/simulator/shared/platform.h index 4b8e461cb..740eb4b0f 100644 --- a/ion/src/simulator/shared/platform.h +++ b/ion/src/simulator/shared/platform.h @@ -17,15 +17,11 @@ void IonSimulatorTelemetryInit(); void IonSimulatorTelemetryEvent(const char * eventName); void IonSimulatorTelemetryDeinit(); -#if EPSILON_SDL_SCREEN_ONLY - void IonSimulatorKeyboardKeyDown(int keyNumber); void IonSimulatorKeyboardKeyUp(int keyNumber); void IonSimulatorEventsPushEvent(int eventNumber); -#endif - void IonSimulatorCallbackDidRefresh(); void IonSimulatorCallbackDidScanKeyboard(); From c679793f946dd5246f7b2366be59d396ce4115d8 Mon Sep 17 00:00:00 2001 From: M4x1m3 Date: Sat, 2 Nov 2019 16:55:15 +0100 Subject: [PATCH 2/3] [ion/simulator] Keep original aspect ratio in screen only mode. --- ion/src/simulator/shared/main_sdl.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/ion/src/simulator/shared/main_sdl.cpp b/ion/src/simulator/shared/main_sdl.cpp index 0c9ce6315..0add2d50c 100644 --- a/ion/src/simulator/shared/main_sdl.cpp +++ b/ion/src/simulator/shared/main_sdl.cpp @@ -132,10 +132,18 @@ void relayout() { SDL_RenderSetLogicalSize(sRenderer, windowWidth, windowHeight); if (argument_screen_only) { - sScreenRect.x = 0; - sScreenRect.y = 0; - sScreenRect.w = windowWidth; - sScreenRect.h = windowHeight; + // Keep original aspect ration in screen_only mode. + float scale = (float)(Ion::Display::Width) / (float)(Ion::Display::Height); + if ((float)(windowHeight) * scale > float(windowWidth)) { + sScreenRect.w = windowWidth; + sScreenRect.h = (int)((float)(windowWidth) / scale); + } else { + sScreenRect.w = (int)((float)(windowHeight) * scale); + sScreenRect.h = windowHeight; + } + + sScreenRect.x = (windowWidth - sScreenRect.w) / 2; + sScreenRect.y = (windowHeight - sScreenRect.h) / 2; } else { Layout::recompute(windowWidth, windowHeight); SDL_Rect backgroundRect; From 70655a123c70265bd7d3dadda0f2fcba3b1139b5 Mon Sep 17 00:00:00 2001 From: M4x1m3 Date: Sat, 2 Nov 2019 17:35:57 +0100 Subject: [PATCH 3/3] [ion/simulator] Added flag to make the window unresizable. --- ion/src/simulator/shared/main_sdl.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ion/src/simulator/shared/main_sdl.cpp b/ion/src/simulator/shared/main_sdl.cpp index 0add2d50c..d4aca04c3 100644 --- a/ion/src/simulator/shared/main_sdl.cpp +++ b/ion/src/simulator/shared/main_sdl.cpp @@ -14,6 +14,7 @@ static bool argument_screen_only = false; static bool argument_fullscreen = false; +static bool argument_unresizable = false; void Ion::Timing::msleep(uint32_t ms) { SDL_Delay(ms); @@ -24,6 +25,7 @@ void print_help(char * program_name) { printf("Options:\n"); printf(" -f, --fullscreen Starts the emulator in fullscreen\n"); printf(" -s, --screen-only Disable the keyboard.\n"); + printf(" -u, --unresizable Disable resizing the window.\n"); printf(" -h, --help Show this help menu.\n"); } @@ -38,6 +40,8 @@ int main(int argc, char * argv[]) { argument_screen_only = true; } else if(strcmp(argv[i], "-f")==0 || strcmp(argv[i], "--fullscreen")==0) { argument_fullscreen = true; + } else if(strcmp(argv[i], "-u")==0 || strcmp(argv[i], "--unresizable")==0) { + argument_unresizable = true; } } @@ -78,7 +82,7 @@ void init() { return; } - uint32_t sdl_window_args = SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_RESIZABLE; + uint32_t sdl_window_args = SDL_WINDOW_ALLOW_HIGHDPI | (argument_unresizable ? 0 : SDL_WINDOW_RESIZABLE); if (argument_fullscreen) { sdl_window_args = SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_FULLSCREEN;