[ion/simulator] Add a runtime "--headless" option

We probably can ditch the static headless version
This commit is contained in:
Romain Goyet
2020-09-04 21:17:36 -04:00
committed by Léa Saviot
parent 37b8c56b3d
commit 0116dc2e07
4 changed files with 35 additions and 12 deletions

View File

@@ -39,6 +39,11 @@ namespace Ion {
namespace Keyboard {
State scan() {
State state = sKeyboardState;
if (Simulator::Window::isHeadless()) {
return state;
}
// We need to tell SDL to get new state from the host OS
SDL_PumpEvents();
@@ -48,7 +53,6 @@ State scan() {
// Grab this opportunity to refresh the display if needed
Simulator::Window::refresh();
State state = sKeyboardState;
#if !EPSILON_SDL_SCREEN_ONLY
// Register a key for the mouse, if any
Key k = Simulator::Layout::getHighlightedKey();
@@ -65,6 +69,7 @@ State scan() {
state.setKey(pair.key());
}
}
return state;
}

View File

@@ -1,6 +1,7 @@
#include "haptics.h"
#include "journal.h"
#include "platform.h"
#include "random.h"
#include "telemetry.h"
#include "window.h"
#include <vector>
@@ -85,19 +86,26 @@ int main(int argc, char * argv[]) {
}
#endif
bool headless = args.popFlag("--headless");
using namespace Ion::Simulator;
Journal::init();
Random::init();
if (!headless) {
Journal::init();
#if EPSILON_TELEMETRY
Telemetry::init();
Telemetry::init();
#endif
Window::init();
Haptics::init();
Window::init();
Haptics::init();
}
ion_main(args.argc(), args.argv());
Haptics::shutdown();
Window::quit();
if (!headless) {
Haptics::shutdown();
Window::quit();
#if EPSILON_TELEMETRY
Telemetry::shutdown();
Telemetry::shutdown();
#endif
}
return 0;
}

View File

@@ -2,7 +2,6 @@
#include "display.h"
#include "layout.h"
#include "platform.h"
#include "random.h"
#include <assert.h>
#include <ion.h>
@@ -19,14 +18,16 @@ static bool sNeedsRefresh = false;
static SDL_Rect sScreenRect;
#endif
bool isHeadless() {
return sWindow == nullptr;
}
void init() {
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
SDL_Log("Could not init video");
return;
}
Random::init();
sWindow = SDL_CreateWindow(
"Epsilon",
SDL_WINDOWPOS_CENTERED,
@@ -67,6 +68,9 @@ void init() {
}
void relayout() {
if (isHeadless()) {
return;
}
int windowWidth = 0;
int windowHeight = 0;
SDL_GetWindowSize(sWindow, &windowWidth, &windowHeight);
@@ -89,7 +93,7 @@ void setNeedsRefresh() {
}
void refresh() {
if (!sNeedsRefresh) {
if (!sNeedsRefresh || isHeadless()) {
return;
}
sNeedsRefresh = false;
@@ -112,11 +116,15 @@ void refresh() {
}
void quit() {
if (isHeadless()) {
return;
}
#if !EPSILON_SDL_SCREEN_ONLY
Layout::quit();
#endif
Display::quit();
SDL_DestroyWindow(sWindow);
sWindow = nullptr;
SDL_Quit();
}

View File

@@ -8,6 +8,8 @@ namespace Window {
void init();
void quit();
bool isHeadless();
void setNeedsRefresh();
void refresh();
void relayout();