[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.
This commit is contained in:
M4x1m3
2019-11-02 16:06:18 +01:00
parent b09e0c9867
commit 4117fac375
2 changed files with 82 additions and 54 deletions

View File

@@ -1,24 +1,51 @@
#include "main.h"
#include "display.h"
#include "platform.h"
#if !EPSILON_SDL_SCREEN_ONLY
#include "layout.h"
#endif
#include <assert.h>
#include <string.h>
#include <ion.h>
#include <stdio.h>
#include <ion/timing.h>
#include <ion/events.h>
#include <SDL.h>
#include <vector>
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<const char *> 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();

View File

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