Tile map editing UI

This commit is contained in:
Jeremy Penner 2019-02-17 20:14:56 -05:00
parent ae1b5712ef
commit a876a9332f
9 changed files with 145 additions and 64 deletions

BIN
game.exe

Binary file not shown.

110
game.jor
View file

@ -13,6 +13,8 @@ REPL start-repl
1 const ^ESC 1 const ^ESC
28 const ^ENTER 28 const ^ENTER
29 const ^CTRL 29 const ^CTRL
51 const ^<
52 const ^>
56 const ^ALT 56 const ^ALT
57 const ^SPACE 57 const ^SPACE
72 const ^UP 72 const ^UP
@ -43,6 +45,12 @@ defer draw
2 const N 2 const N
3 const S 3 const S
: dir>pos ( dir -- dx dy )
dup W = if drop -1 0 ret then
dup E = if drop 1 0 ret then
N = if 0 -1
else 0 1 then ;
: defsprite ( s n e w ) b, b, b, b, here 4 - const ; : defsprite ( s n e w ) b, b, b, b, here 4 - const ;
: sprindex ( sprite dir ) + b@ ; : sprindex ( sprite dir ) + b@ ;
@ -50,8 +58,8 @@ defer draw
defentity player defentity player
100 player entity.x ! 128 player entity.x !
100 player entity.y ! 128 player entity.y !
( timer + lerping ) ( timer + lerping )
: clamp0 ( range val -- i ) : clamp0 ( range val -- i )
@ -59,9 +67,8 @@ defentity player
dup 0 <= if drop drop 0 else dup 0 <= if drop drop 0 else
swap drop then then ; swap drop then then ;
: >ratio ( range value -- f ) : >ratio ( range value -- f )
over swap clamp0 >fix swap >fix fix/ ; over swap clamp0 swap />ratio ;
: <ratio ( range ratio -- v ) : <ratio ( range ratio -- v ) *<ratio ;
swap >fix fix* <fix ;
: >range ( start end -- start range ) over - ; : >range ( start end -- start range ) over - ;
: <range ( start range -- start end ) over + ; : <range ( start range -- start end ) over + ;
: lerpr ( start end ratio ) r> >range r< <ratio + ; : lerpr ( start end ratio ) r> >range r< <ratio + ;
@ -87,39 +94,65 @@ var footer-y
: text2 6 12 rot text ; : text2 6 12 rot text ;
: clear s" " dup text1 text2 ; : clear s" " dup text1 text2 ;
var footer-timer ( hmmm, todo: explicit "mover" struct with create does> ? )
var move-timer
var move-speed
: move-footer-to ( ytarget -- ) : move-to ( p target speed -- )
footer-y @ swap ( from to -- ) move-speed ! swap dup r> @ swap ( from to -- )
footer-timer now! move-timer now!
begin begin
2dup 10 footer-timer lerp ( from to now -- ) 2dup move-speed @ move-timer lerp ( from to now -- )
dup footer-y ! dup r< dup r> !
over != ( from to -- ) over != ( from to -- )
while while
suspend suspend
repeat drop drop ; repeat drop drop r< drop ;
: show-footer 24 move-footer-to ; : show-footer footer-y 24 10 move-to ;
: hide-footer 0 move-footer-to ; : hide-footer footer-y 0 10 move-to ;
: say1 ( s -- ) clear text1 show-footer ^ENTER wait-key ; : say1 ( s -- ) clear text1 show-footer ^ENTER wait-key ;
: say2 ( s1 s2 -- ) clear text2 text1 show-footer ^ENTER wait-key ; : say2 ( s1 s2 -- ) clear text2 text1 show-footer ^ENTER wait-key ;
( T I C K ) ( M O U S E )
: tick-player var prevbutton
0 ^LEFT key-down if 3 - W player entity.dir ! then : tick-debounce
^RIGHT key-down if 3 + E player entity.dir ! then mousebuttons prevbutton ! ;
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 +! ;
1 const MOUSEL
: mousedown ( button -- bool ) mousebuttons & ;
: clicked ( button -- bool )
dup mousedown not swap
prevbutton @ & and ;
( M A P )
: +pos ( x1 y1 x2 y2 -- x y )
rot + rot rot + swap ;
var tileselect
3 const MAXTILE
: mouseworldpos mousepos scrollpos +pos ;
: mousetile mouseworldpos 4 >> swap 4 >> swap ;
: tile ( x y -- ptr ) mapsize drop * + map + ;
: tick-mapedit
tileselect @
^< key-pressed if 1 - then
^> key-pressed if 1 + then
dup 0 < if drop MAXTILE then
dup MAXTILE > if drop 0 then
tileselect !
MOUSEL mousedown if tileselect @ mousetile tile b! then ;
( J O B )
var MODE-MOVE var MODE-MOVE
var MODE-WAIT var MODE-WAIT
( J O B )
: listen-for-jobs activate blah : listen-for-jobs activate blah
begin receive begin receive
MODE-WAIT @ ' tick redefine MODE-WAIT @ ' tick redefine
@ -131,15 +164,35 @@ var MODE-WAIT
task const JOB task const JOB
JOB listen-for-jobs JOB listen-for-jobs
: hello-world s" Hello, world!" say1 s" How are you" s" today?" say2 ; ( T I C K )
: move-player
player entity.dir @ dir>pos
dup if swap drop player entity.y ( d v -- )
else drop player entity.x then
swap 16 * over @ + 5 move-to ;
: tick-player
0 ^LEFT key-down if drop 1 W player entity.dir ! then
^RIGHT key-down if drop 1 E player entity.dir ! then
^UP key-down if drop 1 N player entity.dir ! then
^DOWN key-down if drop 1 S player entity.dir ! then
if ' move-player JOB send then ;
: hello-world
s" Hello, world!" say1
s" How are you" s" today?" say2 ;
: mode-move : mode-move
tick-player tick-player
tick-mapedit
^SPACE key-pressed if ^SPACE key-pressed if
' hello-world JOB send ' hello-world JOB send
then ; then
tick-debounce ;
' mode-move MODE-MOVE ! ' mode-move MODE-MOVE !
' noop MODE-WAIT ! ' tick-debounce MODE-WAIT !
: draw-player : draw-player
player entity.x @ player entity.x @
@ -153,8 +206,9 @@ JOB listen-for-jobs
scroll scroll
draw-player draw-player
50 50 0 draw-sprite 48 64 0 draw-sprite
600 600 2 draw-sprite 640 640 2 draw-sprite
mouseworldpos 4 draw-sprite
draw-screen draw-screen
draw-footer ; draw-footer ;

BIN
game.prj

Binary file not shown.

23
jorth.c
View file

@ -96,18 +96,15 @@ BINOP(f_bitxor, u, ^)
BINOP(f_shr, u, >>) BINOP(f_shr, u, >>)
BINOP(f_shl, u, <<) BINOP(f_shl, u, <<)
void f_itofix() { #define RATIO_FRACTIONAL_BITS 14
TOP().i = TOP().i << FIX_FRACTIONAL_BITS;
} void f_toratio() { // a/b ( a b -- r )
void f_fixtoi() { ST1().i = ((long)ST1().i * (1 << RATIO_FRACTIONAL_BITS)) / TOP().i;
TOP().i = TOP().i >> FIX_FRACTIONAL_BITS;
}
void f_fixmul() {
ST1().i = ((long)ST1().i * (long)TOP().i) / (1 << FIX_FRACTIONAL_BITS);
DROP(1); DROP(1);
} }
void f_fixdiv() {
ST1().i = ((long)ST1().i * (1 << FIX_FRACTIONAL_BITS)) / TOP().i; void f_fromratio() { // a*r ( a r -- b )
ST1().i = ((long)ST1().i * (long)TOP().i) / (1 << RATIO_FRACTIONAL_BITS);
DROP(1); DROP(1);
} }
@ -813,10 +810,8 @@ void f_init() {
CDEF("^", f_bitxor); CDEF("^", f_bitxor);
CDEF("<<", f_shl); CDEF("<<", f_shl);
CDEF(">>", f_shr); CDEF(">>", f_shr);
CDEF(">fix", f_itofix); CDEF("/>ratio", f_toratio);
CDEF("<fix", f_fixtoi); CDEF("*<ratio", f_fromratio);
CDEF("fix*", f_fixmul);
CDEF("fix/", f_fixdiv);
CDEF("@", f_get); CDEF("@", f_get);
CDEF("!", f_set); CDEF("!", f_set);
CDEF("+!", f_addset); CDEF("+!", f_addset);

View file

@ -3,7 +3,6 @@
#define MEM_SIZE 16384 #define MEM_SIZE 16384
#define STACK_SIZE 64 #define STACK_SIZE 64
#define RSTACK_SIZE 32 #define RSTACK_SIZE 32
#define FIX_FRACTIONAL_BITS 5
void f_init(); void f_init();

Binary file not shown.

View file

@ -74,6 +74,8 @@ void game_init() {
FILE *f; FILE *f;
TifImageMeta_t meta; TifImageMeta_t meta;
mouse_init();
setEGAMode(); setEGAMode();
atexit(vid_cleanup); atexit(vid_cleanup);
@ -105,7 +107,6 @@ void game_init() {
scroll(0, 0); scroll(0, 0);
} }
void f_seremit() { void f_seremit() {
ser_write_byte(TOP().i); ser_write_byte(TOP().i);
if (TOP().i == '\n') { if (TOP().i == '\n') {
@ -130,6 +131,11 @@ void f_scroll() { // ( x y -- )
scroll(ST1().i, TOP().i); scroll(ST1().i, TOP().i);
DROP(2); DROP(2);
} }
void f_scrollpos() { // ( -- x y )
PUSHI(screen.scrollX);
PUSHI(screen.scrollY);
}
void f_ticks() { void f_ticks() {
PUSHU(timer_counter); PUSHU(timer_counter);
} }
@ -142,6 +148,24 @@ void f_text() { // ( col line s -- )
text_draw(ST2().u + (ST1().u * PAGE_STRIDE), TOP().s); text_draw(ST2().u + (ST1().u * PAGE_STRIDE), TOP().s);
DROP(3); DROP(3);
} }
void f_map() {
PUSHP(map);
}
void f_mapsize() { // ( -- w h )
PUSHI(screen.w);
PUSHI(screen.h);
}
void f_mousepos() { // ( -- x y )
PUSHI(MOUSE.x);
PUSHI(MOUSE.y);
}
void f_mousebuttons() {
PUSHI(MOUSE.buttons);
}
void game_f_init() { void game_f_init() {
f_init(); f_init();
CDEF("seremit", f_seremit); CDEF("seremit", f_seremit);
@ -149,10 +173,15 @@ void game_f_init() {
CDEF("key-down", f_keyIsDown); CDEF("key-down", f_keyIsDown);
CDEF("draw-sprite", f_drawSprite); CDEF("draw-sprite", f_drawSprite);
CDEF("scroll", f_scroll); CDEF("scroll", f_scroll);
CDEF("scrollpos", f_scrollpos);
CDEF("draw-screen", drawScreen); CDEF("draw-screen", drawScreen);
CDEF("split-screen", f_splitscreen); CDEF("split-screen", f_splitscreen);
CDEF("ticks", f_ticks); CDEF("ticks", f_ticks);
CDEF("text", f_text); CDEF("text", f_text);
CDEF("map", f_map);
CDEF("mapsize", f_mapsize);
CDEF("mousepos", f_mousepos);
CDEF("mousebuttons", f_mousebuttons);
f_loadfile("game.jor"); f_loadfile("game.jor");
} }

21
tiles.c
View file

@ -35,26 +35,7 @@ void blitTile(unsigned int offsetFrom, unsigned int offsetTo) {
#define D_BGTILE 0x81 #define D_BGTILE 0x81
#define isBufIndex(d) (!((d) & 0x80)) #define isBufIndex(d) (!((d) & 0x80))
#define NUM_BUFFERS 32 #define nextBufferIndex(i) ((i + 1) % NUM_BUFFERS)
#define nextBufferIndex(i) ((i + 1) % 32)
#define BUF_WSTRIDE 16
#define BUF_WSIZE (BUF_WSTRIDE * 4)
typedef struct {
unsigned int w;
unsigned int h;
int scrollX;
int scrollY;
unsigned int pageOffset[2];
unsigned char dirty[2][PAGE_TILES_COUNT];
unsigned int tilesOffset;
unsigned int *memTiles;
unsigned char *map;
unsigned int buffer[NUM_BUFFERS][BUF_WSIZE];
unsigned int bufferOffset[NUM_BUFFERS];
unsigned char currentPage;
unsigned char nextBuffer;
unsigned char firstBuffer;
} TiledScreen_t;
TiledScreen_t screen = { 0, 0, 0, 0, { 0x0600, 0x2B00 }, 0, 0, NULL, NULL, TiledScreen_t screen = { 0, 0, 0, 0, { 0x0600, 0x2B00 }, 0, 0, NULL, NULL,
0, 0, 0, 0, 0 }; 0, 0, 0, 0, 0 };

23
tiles.h
View file

@ -12,3 +12,26 @@ void drawScreen();
#define PAGE_TILES_H 14 #define PAGE_TILES_H 14
#define PAGE_TILES_COUNT (PAGE_TILES_H * PAGE_TILES_W) #define PAGE_TILES_COUNT (PAGE_TILES_H * PAGE_TILES_W)
#define PAGE_STRIDE (PAGE_TILES_W << 1) #define PAGE_STRIDE (PAGE_TILES_W << 1)
#define NUM_BUFFERS 32
#define BUF_WSTRIDE 16
#define BUF_WSIZE (BUF_WSTRIDE * 4)
typedef struct {
unsigned int w;
unsigned int h;
int scrollX;
int scrollY;
unsigned int pageOffset[2];
unsigned char dirty[2][PAGE_TILES_COUNT];
unsigned int tilesOffset;
unsigned int *memTiles;
unsigned char *map;
unsigned int buffer[NUM_BUFFERS][BUF_WSIZE];
unsigned int bufferOffset[NUM_BUFFERS];
unsigned char currentPage;
unsigned char nextBuffer;
unsigned char firstBuffer;
} TiledScreen_t;
extern TiledScreen_t screen;