Skip to content

Commit

Permalink
#1247: Added headless mode to run console commands (use --cli argum…
Browse files Browse the repository at this point in the history
…ent), also fixed batch commands processing
  • Loading branch information
nesbox committed May 26, 2021
1 parent 92884fe commit 29aa5fd
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 71 deletions.
35 changes: 21 additions & 14 deletions src/studio/screens/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -2012,7 +2012,7 @@ static void onExport_sfx(Console* console, const char* param, const char* name,
if(params.id >= 0 && params.id < SFX_COUNT)
error = studioExportSfx(params.id, filename) == NULL;

onFileExported(console, filename, error);
onFileExported(console, filename, !error);
}

static void onExport_music(Console* console, const char* type, const char* name, ExportParams params)
Expand All @@ -2023,7 +2023,7 @@ static void onExport_music(Console* console, const char* type, const char* name,
if(params.id >= 0 && params.id < MUSIC_TRACKS)
error = studioExportMusic(params.id, filename) == NULL;

onFileExported(console, filename, error);
onFileExported(console, filename, !error);
}

static void onExport_screen(Console* console, const char* param, const char* name, ExportParams params)
Expand Down Expand Up @@ -2941,7 +2941,9 @@ static void onHelp_commands(Console* console)

static void printTable(Console* console, const char* text)
{
#ifndef BAREMETALPI
printf("%s", text);
#endif

for(const char* textPointer = text, *endText = textPointer + strlen(text); textPointer != endText;)
{
Expand Down Expand Up @@ -3087,8 +3089,6 @@ static void processCommand(Console* console, const char* text)
{
const char* command = console->desc->command;

printf("%s", console->desc->command);

FOR(const Command*, cmd, Commands)
if(casecmp(console->desc->command, cmd->name) == 0 ||
(cmd->alt && casecmp(console->desc->command, cmd->alt) == 0))
Expand All @@ -3111,7 +3111,7 @@ static void processCommand(Console* console, const char* text)

static void processCommands(Console* console)
{
char* command = strdup(console->args.cmd);
char* command = console->args.cmd;
static const char Sep[] = " & ";
char* next = strstr(command, Sep);

Expand All @@ -3125,8 +3125,6 @@ static void processCommands(Console* console)

printFront(console, command);
processCommand(console, command);

free(command);
}

static void fillHistory(Console* console)
Expand Down Expand Up @@ -3182,6 +3180,7 @@ static void processConsoleCommand(Console* console)

if(commandSize)
{
printf(console->input.text);
appendHistory(console, console->input.text);
processCommand(console, console->input.text);
}
Expand Down Expand Up @@ -3520,12 +3519,15 @@ static void tick(Console* console)
{
loadDemo(console, 0);

printBack(console, "\n hello! type ");
printFront(console, "help");
printBack(console, " for help\n");
if(!console->args.skip)
{
printBack(console, "\n hello! type ");
printFront(console, "help");
printBack(console, " for help\n");

if(getConfig()->checkNewVersion)
tic_net_get(console->net, "/api?fn=version", onHttpVesrsionGet, console);
if(getConfig()->checkNewVersion)
tic_net_get(console->net, "/api?fn=version", onHttpVesrsionGet, console);
}

commandDone(console);
}
Expand Down Expand Up @@ -3567,8 +3569,13 @@ static void tick(Console* console)

drawCursor(console);

if(console->active && console->args.cmd)
processCommands(console);
if(console->active)
{
if(console->args.cmd)
processCommands(console);
else if(getConfig()->cli)
exitStudio();
}
}

console->tickCounter++;
Expand Down
5 changes: 5 additions & 0 deletions src/studio/studio.c
Original file line number Diff line number Diff line change
Expand Up @@ -2060,6 +2060,7 @@ static StartArgs parseArgs(s32 argc, char **argv)
OPT_HELP(),
OPT_BOOLEAN('\0', "skip", &args.skip, "skip startup animation"),
OPT_BOOLEAN('\0', "nosound", &args.nosound, "disable sound output"),
OPT_BOOLEAN('\0', "cli", &args.cli, "console only output"),
OPT_BOOLEAN('\0', "fullscreen", &args.fullscreen, "enable fullscreen mode"),
OPT_STRING('\0', "fs", &args.fs, "path to the file system folder"),
OPT_INTEGER('\0', "scale", &args.scale, "main window scale"),
Expand Down Expand Up @@ -2145,13 +2146,17 @@ Studio* studioInit(s32 argc, char **argv, s32 samplerate, const char* folder)

impl.config->data.goFullscreen = args.fullscreen;
impl.config->data.noSound = args.nosound;
impl.config->data.cli = args.cli;

impl.studio.tick = studioTick;
impl.studio.close = studioClose;
impl.studio.updateProject = updateStudioProject;
impl.studio.exit = exitStudio;
impl.studio.config = getConfig;

if(args.cli)
args.skip = true;

if(args.skip)
setStudioMode(TIC_CONSOLE_MODE);

Expand Down
1 change: 1 addition & 0 deletions src/studio/studio.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ typedef struct
{
bool skip;
bool nosound;
bool cli;
bool fullscreen;
s32 scale;
char *fs;
Expand Down
1 change: 1 addition & 0 deletions src/studio/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ typedef struct

bool checkNewVersion;
bool noSound;
bool cli;

#if defined(CRT_SHADER_SUPPORT)
bool crtMonitor;
Expand Down
110 changes: 58 additions & 52 deletions src/system/sdl/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1575,87 +1575,93 @@ static void createMouseCursors()

static s32 start(s32 argc, char **argv, const char* folder)
{
SDL_SetHint(SDL_HINT_WINRT_HANDLE_BACK_BUTTON, "1");
SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, "0");

SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK);

initSound();
DEFER(platform.studio = studioInit(argc, argv, TIC80_SAMPLERATE, folder), platform.studio->close())
{
if (platform.studio->config()->cli)
{
while (!platform.studio->quit)
platform.studio->tick();
}
else
{
SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, "0");
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK);

platform.studio = studioInit(argc, argv, platform.audio.spec.freq, folder);
initSound();

{
const s32 Width = TIC80_FULLWIDTH * platform.studio->config()->uiScale;
const s32 Height = TIC80_FULLHEIGHT * platform.studio->config()->uiScale;
{
const s32 Width = TIC80_FULLWIDTH * platform.studio->config()->uiScale;
const s32 Height = TIC80_FULLHEIGHT * platform.studio->config()->uiScale;

platform.window = SDL_CreateWindow( TIC_TITLE, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
Width, Height, SDL_WINDOW_SHOWN
| SDL_WINDOW_RESIZABLE
platform.window = SDL_CreateWindow( TIC_TITLE, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
Width, Height, SDL_WINDOW_SHOWN
| SDL_WINDOW_RESIZABLE
#if defined(CRT_SHADER_SUPPORT)
| SDL_WINDOW_OPENGL
| SDL_WINDOW_OPENGL
#endif
#if !defined(__EMSCRIPTEN__) && !defined(__MACOSX__)
| SDL_WINDOW_ALLOW_HIGHDPI
| SDL_WINDOW_ALLOW_HIGHDPI
#endif
);
);

setWindowIcon();
createMouseCursors();
setWindowIcon();
createMouseCursors();

initGPU();
initGPU();

if(platform.studio->config()->goFullscreen)
tic_sys_fullscreen();
}
if(platform.studio->config()->goFullscreen)
tic_sys_fullscreen();
}

SDL_PauseAudioDevice(platform.audio.device, 0);
SDL_PauseAudioDevice(platform.audio.device, 0);

#if defined(__EMSCRIPTEN__)
emscripten_set_main_loop(emsGpuTick, 0, 1);
emscripten_set_main_loop(emsGpuTick, 0, 1);
#else
{
u64 nextTick = SDL_GetPerformanceCounter();
const u64 Delta = SDL_GetPerformanceFrequency() / TIC80_FRAMERATE;
{
u64 nextTick = SDL_GetPerformanceCounter();
const u64 Delta = SDL_GetPerformanceFrequency() / TIC80_FRAMERATE;

while (!platform.studio->quit)
{
nextTick += Delta;

gpuTick();
while (!platform.studio->quit)
{
nextTick += Delta;
gpuTick();

{
s64 delay = nextTick - SDL_GetPerformanceCounter();
{
s64 delay = nextTick - SDL_GetPerformanceCounter();

if(delay < 0)
nextTick -= delay;
else
SDL_Delay((u32)(delay * 1000 / SDL_GetPerformanceFrequency()));
if(delay < 0)
nextTick -= delay;
else
SDL_Delay((u32)(delay * 1000 / SDL_GetPerformanceFrequency()));
}
}
}
}
}

#endif

#if defined(TOUCH_INPUT_SUPPORT)
if(SDL_IsTextInputActive())
SDL_StopTextInput();
if(SDL_IsTextInputActive())
SDL_StopTextInput();
#endif

platform.studio->close();

{
destroyGPU();
{
destroyGPU();

#if defined(TOUCH_INPUT_SUPPORT)
if(platform.gamepad.touch.pixels)
SDL_free(platform.gamepad.touch.pixels);
if(platform.gamepad.touch.pixels)
SDL_free(platform.gamepad.touch.pixels);
#endif

SDL_DestroyWindow(platform.window);
SDL_CloseAudioDevice(platform.audio.device);
SDL_DestroyWindow(platform.window);
SDL_CloseAudioDevice(platform.audio.device);

for(s32 i = 0; i < COUNT_OF(platform.mouse.cursors); i++)
SDL_FreeCursor(platform.mouse.cursors[i]);
for(s32 i = 0; i < COUNT_OF(platform.mouse.cursors); i++)
SDL_FreeCursor(platform.mouse.cursors[i]);
}

}
}

return 0;
Expand Down
18 changes: 13 additions & 5 deletions src/system/sokol/sokol.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,11 @@ static void app_frame(void)
sokol_gfx_draw(platform.studio->tic->screen);

s32 count = tic->samples.size / sizeof tic->samples.buffer[0];
for(s32 i = 0; i < count; i++)
platform.audio.samples[i] = (float)tic->samples.buffer[i] / SHRT_MAX;
for(s32 i = 0; i < count; i++)
platform.audio.samples[i] = (float)tic->samples.buffer[i] / SHRT_MAX;

saudio_push(platform.audio.samples, count / 2);

saudio_push(platform.audio.samples, count / 2);
input->mouse.scrollx = input->mouse.scrolly = 0;
platform.keyboard.text = '\0';
}
Expand Down Expand Up @@ -404,7 +404,15 @@ sapp_desc sokol_main(s32 argc, char* argv[])
platform.audio.desc.num_channels = TIC_STEREO_CHANNELS;
saudio_setup(&platform.audio.desc);

platform.studio = studioInit(argc, (const char**)argv, saudio_sample_rate(), "./");
platform.studio = studioInit(argc, argv, saudio_sample_rate(), "./");

if(platform.studio->config()->cli)
{
while (!platform.studio->quit)
platform.studio->tick();

exit(0);
}

const s32 Width = TIC80_FULLWIDTH * platform.studio->config()->uiScale;
const s32 Height = TIC80_FULLHEIGHT * platform.studio->config()->uiScale;
Expand Down

0 comments on commit 29aa5fd

Please sign in to comment.