diff --git a/build.sh b/build.sh index 44fabe9..b23fa0a 100755 --- a/build.sh +++ b/build.sh @@ -45,7 +45,8 @@ fi # bin/uxnasm etc/polycat.tal bin/polycat.rom # bin/uxn11 bin/polycat.rom +# bin/uxnasm etc/friend.tal bin/friend.rom -bin/uxnasm etc/friend.tal bin/friend.rom -bin/uxn11 bin/friend.rom +bin/uxnasm etc/mouse.tal bin/mouse.rom +bin/uxn11 bin/mouse.rom diff --git a/etc/mouse.tal b/etc/mouse.tal new file mode 100644 index 0000000..45c3aca --- /dev/null +++ b/etc/mouse.tal @@ -0,0 +1,227 @@ +( Mouse: + Paint with 3 colors with each mouse button. ) + +|00 @System &vector $2 &wst $1 &rst $1 &pad $4 &r $2 &g $2 &b $2 &debug $1 &halt $1 +|20 @Screen &vector $2 &width $2 &height $2 &auto $1 &pad $1 &x $2 &y $2 &addr $2 &pixel $1 &sprite $1 +|90 @Mouse &vector $2 &x $2 &y $2 &state $1 &pad $3 &modx $2 &mody $2 + +|0000 + + @line &x2 $2 &y2 $2 + @length $2 + @frame $2 + @pen &x $2 &y $2 &x2 $2 &y2 $2 + @pointer &x $2 &y $2 &lastx $2 &lasty $2 &state $1 + +|0100 ( -> ) + + ( theme ) + #4cfd .System/r DEO2 + #4cf3 .System/g DEO2 + #dcf2 .System/b DEO2 + + ( vectors ) + ;on-mouse .Mouse/vector DEO2 + ;on-frame .Screen/vector DEO2 + + draw-mouse + +BRK + +( +@|vectors ) + +@on-frame ( -> ) + + .Mouse/state DEI ?&skip + ;run DUP2 JSR2 JSR2 + &skip + +BRK + +@on-mouse ( -> ) + + ( clear last cursor ) + .pointer/x LDZ2 .Screen/x DEO2 + .pointer/y LDZ2 .Screen/y DEO2 + [ LIT2 40 -Screen/sprite ] DEO + draw-mouse + ( draw new cursor ) + ;pointer-icn .Screen/addr DEO2 + #00 .Screen/auto DEO + .Mouse/x DEI2 DUP2 .pointer/x STZ2 .Screen/x DEO2 + .Mouse/y DEI2 DUP2 .pointer/y STZ2 .Screen/y DEO2 + #45 .Mouse/state DEI #00 NEQ #05 MUL ADD .Screen/sprite DEO + ( on down ) + .Mouse/state DEI #00 NEQ .pointer/state LDZ #00 EQU AND ?on-mouse-down + ( on drag ) + .Mouse/state DEI ?on-mouse-drag + .Mouse/state DEI .pointer/state STZ + +BRK + +@on-mouse-down ( -> ) + + #0000 DUP2 .length STZ2 .frame STZ2 + clear-screen + ( record start position ) + .Mouse/x DEI2 DUP2 .pointer/x STZ2 .pointer/lastx STZ2 + .Mouse/y DEI2 DUP2 .pointer/y STZ2 .pointer/lasty STZ2 + .Mouse/state DEI .pointer/state STZ + +BRK + +@on-mouse-drag ( -> ) + + ( record ) + ;stroke .length LDZ2 #20 SFT2 ADD2 STH2 + .pointer/x LDZ2 .pointer/lastx LDZ2 SUB2 STH2kr STA2 + .pointer/y LDZ2 .pointer/lasty LDZ2 SUB2 STH2r INC2 INC2 STA2 + ( move ptr ) + .length LDZ2 INC2 .length STZ2 + ( draw line ) + .pointer/lastx LDZ2 + .pointer/lasty LDZ2 + .pointer/x LDZ2 + .pointer/y LDZ2 + #01 draw-line + ( record last position ) + .Mouse/x DEI2 + DUP2 .pointer/lastx STZ2 + DUP2 .pen/x STZ2 + .pen/x2 STZ2 + .Mouse/y DEI2 + DUP2 .pointer/lasty STZ2 + DUP2 .pen/y STZ2 + .pen/y2 STZ2 + .Mouse/state DEI + DUP #01 NEQ INC ;run/color STA + .pointer/state STZ + +BRK + +( +@|main ) + +@run ( -- ) + + ( read ) + ;stroke .frame LDZ2 #20 SFT2 ADD2 STH2 + .pen/x LDZ2 STH2kr LDA2 ADD2 .pen/x STZ2 + .pen/y LDZ2 STH2r INC2 INC2 LDA2 ADD2 .pen/y STZ2 + ( line ) + .pen/x LDZ2 .pen/y LDZ2 + .pen/x2 LDZ2 .pen/y2 LDZ2 + .frame LDZ2 #01 SFT2 NIP #01 AND [ LIT &color $1 ] ADD INC draw-line + ( history ) + .pen/x LDZ2 .pen/x2 STZ2 + .pen/y LDZ2 .pen/y2 STZ2 + ( incr frame ) + .frame LDZ2 INC2 .length LDZ2 INC2 ( mod2 ) DIV2k MUL2 SUB2 .frame STZ2 + +JMP2r + +@draw-mouse ( -- ) + + ( clear ) + #0010 + DUP2 .Screen/x DEO2 + .Screen/y DEO2 + #16 .Screen/auto DEO + #40 .Screen/sprite DEOk DEO + ( buttons ) + #0300 + &l + #01 OVR #40 SFT SFT .Mouse/state DEI AND #00 EQU ?&no-draw + #0010 .Screen/y DEO2 + #00 OVR #40 SFT2 ;button-icn ADD2 .Screen/addr DEO2 + #45 .Screen/sprite DEO + &no-draw + INC GTHk ?&l + POP2 + ( outline ) + #0010 .Screen/y DEO2 + ;mouse-icn .Screen/addr DEO2 + #16 .Screen/auto DEO + #4a .Screen/sprite DEOk DEO + +JMP2r + +@draw-line ( x1* y1* x2* y2* color -- ) + + ( load ) + ,&color STR + ,&y STR2 + ,&x STR2 + .line/y2 STZ2 + .line/x2 STZ2 + + ,&x LDR2 .line/x2 LDZ2 SUB2 abs2 ,&dx STR2 + #0000 ,&y LDR2 .line/y2 LDZ2 SUB2 abs2 SUB2 ,&dy STR2 + + #ffff #00 .line/x2 LDZ2 ,&x LDR2 lts2 DUP2 ADD2 ADD2 ,&sx STR2 + #ffff #00 .line/y2 LDZ2 ,&y LDR2 lts2 DUP2 ADD2 ADD2 ,&sy STR2 + + [ LIT2 &dx $2 ] [ LIT2 &dy $2 ] ADD2 ,&e1 STR2 + + &loop + .line/x2 LDZ2 DUP2 .Screen/x DEO2 [ LIT2 &x $2 ] EQU2 + .line/y2 LDZ2 DUP2 .Screen/y DEO2 [ LIT2 &y $2 ] EQU2 + [ LIT2 &color $1 -Screen/pixel ] DEO + AND ?&end + [ LIT2 &e1 $2 ] DUP2 ADD2 DUP2 + ,&dy LDR2 lts2 ?&skipy + ,&e1 LDR2 ,&dy LDR2 ADD2 ,&e1 STR2 + .line/x2 LDZ2 [ LIT2 &sx $2 ] ADD2 .line/x2 STZ2 + &skipy + ,&dx LDR2 gts2 ?&skipx + ,&e1 LDR2 ,&dx LDR2 ADD2 ,&e1 STR2 + .line/y2 LDZ2 [ LIT2 &sy $2 ] ADD2 .line/y2 STZ2 + &skipx + !&loop + &end + +JMP2r + +@abs2 DUP2 #0f SFT2 EQU #05 JCN #0000 SWP2 SUB2 JMP2r +@lts2 #8000 STH2k ADD2 SWP2 STH2r ADD2 GTH2 JMP2r +@gts2 #8000 STH2k ADD2 SWP2 STH2r ADD2 LTH2 JMP2r + +@clear-screen ( -- ) + + #00 .Screen/auto DEO + + .Screen/height DEI2 #03 SFT2 NIP #00 + &y + #00 OVR #30 SFT2 .Screen/y DEO2 + .Screen/width DEI2 #03 SFT2 NIP #00 + &x + #00 OVR #30 SFT2 .Screen/x DEO2 + [ LIT2 00 -Screen/sprite ] DEO + INC GTHk ?&x + POP2 + INC GTHk ?&y + POP2 + +JMP2r + +@pointer-icn [ + 80c0 e0f0 f8e0 1000 ] +@mouse-icn [ + 000d 1212 1212 121d + 00b0 4848 4848 48b8 + 1010 1010 1008 0700 + 0808 0808 0810 e000 ] +@button-icn [ + 000c 1e1e 1e1e 1e0c + 0000 0000 0000 0000 + 0001 0303 0303 0301 + 0080 c0c0 c0c0 c080 + 0000 0000 0000 0000 + 0030 7878 7878 7830 ] + +( +@|memory ) + +@stroke + diff --git a/src/uxn11.c b/src/uxn11.c index 9da7a71..e9d62ec 100644 --- a/src/uxn11.c +++ b/src/uxn11.c @@ -35,12 +35,18 @@ char *rom_path; #define WIDTH (64 * 8) #define HEIGHT (40 * 8) -#define PAD 0 +#define PAD 2 #define CONINBUFSIZE 256 Uint16 deo_mask[] = {0xff28, 0x0300, 0xc028, 0x8000, 0x8000, 0x8000, 0x8000, 0x0000, 0x0000, 0x0000, 0xa260, 0xa260, 0x0000, 0x0000, 0x0000, 0x0000}; Uint16 dei_mask[] = {0x0000, 0x0000, 0x003c, 0x0014, 0x0014, 0x0014, 0x0014, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x07ff, 0x0000, 0x0000, 0x0000}; +static int +clamp(int val, int min, int max) +{ + return (val >= min) ? (val <= max) ? val : max : min; +} + Uint8 uxn_dei(Uxn *u, Uint8 addr) { @@ -118,7 +124,7 @@ emu_event(Uxn *u) XNextEvent(display, &ev); switch(ev.type) { case Expose: - XPutImage(display, window, DefaultGC(display, 0), ximage, 0, 0, 0, 0, uxn_screen.width, uxn_screen.height); + XPutImage(display, window, DefaultGC(display, 0), ximage, 0, 0, PAD, PAD, uxn_screen.width, uxn_screen.height); break; case ClientMessage: { XDestroyImage(ximage); @@ -159,7 +165,9 @@ emu_event(Uxn *u) } break; case MotionNotify: { XMotionEvent *e = (XMotionEvent *)&ev; - mouse_pos(u, &u->dev[0x90], (e->x - PAD) / SCALE, (e->y - PAD) / SCALE); + int x = clamp((e->x - PAD) / SCALE, 0, uxn_screen.width - 1); + int y = clamp((e->y - PAD) / SCALE, 0, uxn_screen.height - 1); + mouse_pos(u, &u->dev[0x90], x, y); } break; } } @@ -233,7 +241,7 @@ main(int argc, char **argv) if(uxn_screen.x2) { int x1 = uxn_screen.x1, y1 = uxn_screen.y1, x2 = uxn_screen.x2, y2 = uxn_screen.y2; screen_redraw(); - XPutImage(display, window, DefaultGC(display, 0), ximage, x1, y1, x1, y1, x2 - x1, y2 - y1); + XPutImage(display, window, DefaultGC(display, 0), ximage, x1, y1, x1 + PAD, y1 + PAD, x2 - x1, y2 - y1); } } XDestroyImage(ximage);