Skip to content

Commit 3d8bd14

Browse files
committed
Implement "resume reload" command.
When resuming, you can pass "reload" as the argument, which causes the cart's code to be reloaded before resuming the game. This allows for a much smoother iterative development style where you can pause the game, make changes, and see the effect of those changes without restarting the game. It does require some special handling in the cart to support saving state to a global and avoiding that state being reinitialized if it's already set; for instance: state = state or {x=25, y=100, shots={}} This implements the reload method for the Lua and Fennel scripts, but it will need to be implemented for other scripts independently.
1 parent 43a0317 commit 3d8bd14

File tree

4 files changed

+73
-27
lines changed

4 files changed

+73
-27
lines changed

src/api/fennel.c

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,31 @@ static const char* execute_fennel_src = FENNEL_CODE(
3838
if(not ok) then return msg end
3939
);
4040

41+
static bool loadFennelCode(tic_mem* tic, const char* code)
42+
{
43+
tic_core* core = (tic_core*)tic;
44+
lua_State* fennel = core->currentVM;
45+
46+
if (luaL_loadbuffer(fennel, execute_fennel_src,
47+
strlen(execute_fennel_src), "execute_fennel") != LUA_OK)
48+
{
49+
core->data->error(core->data->data, "failed to load fennel shim");
50+
return false;
51+
}
52+
53+
lua_pushstring(fennel, code);
54+
lua_call(fennel, 1, 1);
55+
const char* err = lua_tostring(fennel, -1);
56+
57+
if (err)
58+
{
59+
core->data->error(core->data->data, err);
60+
return false;
61+
} else {
62+
return true;
63+
}
64+
}
65+
4166
static bool initFennel(tic_mem* tic, const char* code)
4267
{
4368
tic_core* core = (tic_core*)tic;
@@ -48,38 +73,25 @@ static bool initFennel(tic_mem* tic, const char* code)
4873

4974
luaapi_init(core);
5075

51-
{
52-
lua_State* fennel = core->currentVM;
53-
54-
lua_settop(fennel, 0);
55-
56-
if (luaL_loadbuffer(fennel, (const char *)loadfennel_lua,
57-
loadfennel_lua_len, "fennel.lua") != LUA_OK)
58-
{
59-
core->data->error(core->data->data, "failed to load fennel compiler");
60-
return false;
61-
}
76+
lua_State* fennel = core->currentVM;
6277

63-
lua_call(fennel, 0, 0);
78+
lua_settop(fennel, 0);
6479

65-
if (luaL_loadbuffer(fennel, execute_fennel_src, strlen(execute_fennel_src), "execute_fennel") != LUA_OK)
66-
{
67-
core->data->error(core->data->data, "failed to load fennel compiler");
68-
return false;
69-
}
80+
if (luaL_loadbuffer(fennel, (const char *)loadfennel_lua,
81+
loadfennel_lua_len, "fennel.lua") != LUA_OK)
82+
{
83+
core->data->error(core->data->data, "failed to load fennel compiler");
84+
return false;
85+
}
7086

71-
lua_pushstring(fennel, code);
72-
lua_call(fennel, 1, 1);
73-
const char* err = lua_tostring(fennel, -1);
87+
lua_call(fennel, 0, 0);
7488

75-
if (err)
76-
{
77-
core->data->error(core->data->data, err);
78-
return false;
79-
}
80-
}
89+
return loadFennelCode(tic, code);
90+
}
8191

82-
return true;
92+
static bool reloadFennel(tic_mem* tic)
93+
{
94+
return loadFennelCode(tic, tic->cart.code.data);
8395
}
8496

8597
static const char* const FennelKeywords [] =
@@ -195,6 +207,7 @@ TIC_EXPORT const tic_script EXPORT_SCRIPT(Fennel) =
195207
.projectComment = ";;",
196208
{
197209
.init = initFennel,
210+
.reload = reloadFennel,
198211
.close = luaapi_close,
199212
.tick = luaapi_tick,
200213
.boot = luaapi_boot,

src/api/lua.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@ static bool initLua(tic_mem* tic, const char* code)
5555
return true;
5656
}
5757

58+
static bool reloadLua(tic_mem* tic)
59+
{
60+
tic_core* core = (tic_core*)tic;
61+
lua_State* lua = core->currentVM;
62+
const char* code = tic->cart.code.data;
63+
64+
if(luaL_loadstring(lua, code) != LUA_OK || lua_pcall(lua, 0, LUA_MULTRET, 0) != LUA_OK)
65+
{
66+
core->data->error(core->data->data, lua_tostring(lua, -1));
67+
return false;
68+
}
69+
return true;
70+
}
71+
5872
static const char* const LuaKeywords [] =
5973
{
6074
"and", "break", "do", "else", "elseif",
@@ -214,6 +228,7 @@ TIC_EXPORT const tic_script EXPORT_SCRIPT(Lua) =
214228
.projectComment = "--",
215229
{
216230
.init = initLua,
231+
.reload = reloadLua,
217232
.close = luaapi_close,
218233
.tick = luaapi_tick,
219234
.boot = luaapi_boot,

src/script.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ struct tic_script
3333
struct
3434
{
3535
bool(*init)(tic_mem* memory, const char* code);
36+
bool(*reload)(tic_mem* memory);
3637
void(*close)(tic_mem* memory);
3738

3839
tic_tick tick;

src/studio/screens/console.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2667,6 +2667,23 @@ static void onRunCommand(Console* console)
26672667

26682668
static void onResumeCommand(Console* console)
26692669
{
2670+
if(console->desc->count)
2671+
{
2672+
const char* param = console->desc->params->key;
2673+
2674+
if(strcmp(param, "reload") == 0)
2675+
{
2676+
const tic_script* script_config = tic_get_script(console->tic);
2677+
if (script_config->reload)
2678+
{
2679+
script_config->reload(console->tic);
2680+
}
2681+
else
2682+
{
2683+
printError(console, "reload not implemented for the script");
2684+
}
2685+
}
2686+
}
26702687
commandDone(console);
26712688

26722689
resumeGame(console->studio);

0 commit comments

Comments
 (0)