Implement game loop in Jorth

This commit is contained in:
Jeremy Penner 2019-02-10 19:17:58 -05:00
parent c8331edece
commit e9e8fa804a
7 changed files with 82 additions and 21 deletions

View file

@ -35,8 +35,7 @@ key const sp
then then
then ; then ;
: interpreter : interpreter
begin word dup b@ while compileword repeat begin word dup b@ while compileword repeat drop ;
s" ok" type cr drop ;
: load-input swap-input r> r> interpreter r< r< swap-input ; : load-input swap-input r> r> interpreter r< r< swap-input ;
: loadstring ' key-string load-input drop drop ; : loadstring ' key-string load-input drop drop ;
: loadfile ' key-file load-input drop close ; : loadfile ' key-file load-input drop close ;

View file

@ -6,6 +6,10 @@ s" jorth.log" open const LOGFILE
: :noname here docolon , ] ; : :noname here docolon , ] ;
: 2dup over over ; : 2dup over over ;
: noop ;
: defer word new-word docolon , ' noop , ' ret , ;
: is ( cp -- ) word lookup drop cell + ! ;
: decompile : decompile
word lookup if 1 begin ( cp i ) word lookup if 1 begin ( cp i )

BIN
game.exe

Binary file not shown.

View file

@ -4,12 +4,10 @@
: start-repl activate blah : start-repl activate blah
s" .:: J O R T H ( jean forth) ::." type cr s" .:: J O R T H ( jean forth) ::." type cr
begin receive loadstring again ; begin receive loadstring s" ok" type cr again ;
task const REPL task const REPL
REPL start-repl REPL start-repl
task const TEST
1 const ^ESC 1 const ^ESC
28 const ^ENTER 28 const ^ENTER
29 const ^CTRL 29 const ^CTRL
@ -29,9 +27,53 @@ task const TEST
then ; then ;
: sleep ( count -- ) : sleep ( count -- )
ticks swap begin over ticks udelta over u< while suspend repeat drop drop ; ticks swap begin over ticks udelta over u< while suspend repeat drop drop ;
: test TEST activate blah
^SPACE wait-key defer tick
s" SPACE" type defer draw
30 sleep
s" is big" type ; : defentity var 2 cells allot ;
test : entity.x ;
: entity.y cell + ;
: entity.dir 2 cells + ;
0 const W
1 const E
2 const N
3 const S
: defsprite ( s n e w ) b, b, b, b, here 4 - const ;
: sprindex ( sprite dir ) + b@ ;
3 1 0 2 defsprite s_car
defentity player
100 player entity.x !
100 player entity.y !
: tick-player
0 ^LEFT key-down if 3 - W player entity.dir ! then
^RIGHT key-down if 3 + E player entity.dir ! then
player entity.x +!
0 ^UP key-down if 3 - N player entity.dir ! then
^DOWN key-down if 3 + S player entity.dir ! then
player entity.y +! ;
: draw-player
player entity.x @
player entity.y @
s_car player entity.dir @ sprindex
draw-sprite ;
: full-draw
player entity.x @ 152 -
player entity.y @ 92 -
scroll
draw-player
50 50 0 draw-sprite
600 600 2 draw-sprite
draw-screen ;
' tick-player is tick
' full-draw is draw

BIN
game.prj

Binary file not shown.

21
jorth.c
View file

@ -123,6 +123,11 @@ void f_bset() {
DROP(1); DROP(1);
} }
void f_addset() {
TOP().p->i += ST1().i;
DROP(2);
}
void f_drop() { void f_drop() {
DROP(1); DROP(1);
} }
@ -300,6 +305,11 @@ void f_comma() {
DROP(1); DROP(1);
} }
void f_allot() {
HERE = CELL_OFFSET(HERE, TOP().u);
DROP(1);
}
void f_bcomma() { void f_bcomma() {
*((char*)HERE) = TOP().i; *((char*)HERE) = TOP().i;
HERE = CELL_OFFSET(HERE, 1); HERE = CELL_OFFSET(HERE, 1);
@ -375,9 +385,6 @@ void f_execute() {
f_colondispatch(); f_colondispatch();
} }
void f_noop() {
}
void f_lit_() { void f_lit_() {
PUSHC(*IP.p); PUSHC(*IP.p);
IP.p++; IP.p++;
@ -750,17 +757,21 @@ void f_init() {
CDEF("word", f_word); CDEF("word", f_word);
CDEF("immediate", f_immediate); CDEF("immediate", f_immediate);
CDEF("execute", f_execute); CDEF("execute", f_execute);
CDEF("create", f_create); CDEF("new-word", f_create);
CDEF("here", f_here); CDEF("here", f_here);
CDEF("latest", f_latest); CDEF("latest", f_latest);
CDEF("state", f_state); CDEF("state", f_state);
CDEF("'", f_quote); f_immediate(); CDEF("'", f_quote); f_immediate();
CDEF("`", f_revlookup); CDEF("`", f_revlookup);
CDEF("wordname", f_wordname);
CDEF("wordflags", f_wordflags);
CDEF("codepointer", f_codepointer);
CDEF("lookup", f_lookup); CDEF("lookup", f_lookup);
CDEF(":", f_colon); CDEF(":", f_colon);
CDEF(";", f_semicolon); f_immediate(); CDEF(";", f_semicolon); f_immediate();
CDEF("const", f_const); CDEF("const", f_const);
CDEF("var", f_var); CDEF("var", f_var);
CDEF("allot", f_allot);
CDEF("+", f_add); CDEF("+", f_add);
CDEF("-", f_sub); CDEF("-", f_sub);
CDEF("*", f_mul); CDEF("*", f_mul);
@ -785,6 +796,7 @@ void f_init() {
CDEF("^", f_bitxor); CDEF("^", f_bitxor);
CDEF("@", f_get); CDEF("@", f_get);
CDEF("!", f_set); CDEF("!", f_set);
CDEF("+!", f_addset);
CDEF("b@", f_bget); CDEF("b@", f_bget);
CDEF("b!", f_bset); CDEF("b!", f_bset);
CDEF("dup", f_dup); CDEF("dup", f_dup);
@ -814,7 +826,6 @@ void f_init() {
CDEF("b,", f_bcomma); CDEF("b,", f_bcomma);
CDEF("s\"", f_string); f_immediate(); CDEF("s\"", f_string); f_immediate();
CDEF("S\"_", f_string_); CDEF("S\"_", f_string_);
CDEF("noop", f_noop);
CDEF("open", f_open); CDEF("open", f_open);
CDEF("close", f_close); CDEF("close", f_close);
CDEF("quiet", f_quiet); CDEF("quiet", f_quiet);

View file

@ -170,6 +170,7 @@ void f_drawSprite() { // ( x y sprite -- )
} }
void f_scroll() { // ( x y -- ) void f_scroll() { // ( x y -- )
scroll(ST1().i, TOP().i); scroll(ST1().i, TOP().i);
DROP(2);
} }
void f_ticks() { void f_ticks() {
PUSHU(timer_counter); PUSHU(timer_counter);
@ -180,9 +181,9 @@ void game_f_init() {
CDEF("seremit", f_seremit); CDEF("seremit", f_seremit);
CDEF("key-pressed", f_keyWasPressed); CDEF("key-pressed", f_keyWasPressed);
CDEF("key-down", f_keyIsDown); CDEF("key-down", f_keyIsDown);
CDEF("sprite", f_drawSprite); CDEF("draw-sprite", f_drawSprite);
CDEF("scroll", f_scroll); CDEF("scroll", f_scroll);
CDEF("draw", drawScreen); CDEF("draw-screen", drawScreen);
CDEF("ticks", f_ticks); CDEF("ticks", f_ticks);
f_loadfile("game.jor"); f_loadfile("game.jor");
@ -213,7 +214,6 @@ void f_poll() {
line[i] = '\0'; line[i] = '\0';
} }
} }
f_taskloop();
} }
int main() { int main() {
@ -224,14 +224,19 @@ int main() {
while (!keyIsDown(K_ESC)) { while (!keyIsDown(K_ESC)) {
kbd_debounce(); kbd_debounce();
f_poll(); f_poll();
f_taskloop();
f_runstring("tick");
f_taskloop();
f_runstring("draw");
if (game.state == STATE_MAP) { /* if (game.state == STATE_MAP) {
overworldThink(); overworldThink();
} else if (game.state == STATE_DIALOG) { } else if (game.state == STATE_DIALOG) {
dialogThink(); dialogThink();
} }
drawEntity(&game.player); drawEntity(&game.player);
drawScreen(); drawScreen();*/
} }
return 0; return 0;