~rabbits/uxn-playdate

71022411bfda20f454fde320d2d40cfa632177e0 — neauoire 5 months ago d1ff604
Fix issue with redraw
3 files changed, 333 insertions(+), 2 deletions(-)

M build.sh
A etc/wireworld.tal
M src/main.c
M build.sh => build.sh +1 -1
@@ 1,6 1,6 @@
#!/bin/sh -e

TARGET=etc/catclock.tal
TARGET=etc/wireworld.tal

rm -rf bin
mkdir bin

A etc/wireworld.tal => etc/wireworld.tal +330 -0
@@ 0,0 1,330 @@
(
	wireworld
	
	A - conductor
	B - tail
	Sel - head
	Start - clear
	mouse1 - paint
	mouse2 - erase

	RULES:
	- electron head<3>, becomes electron tail<2>
	- electron tail<2>, becomes conductor<1>
	- conductor<1>, becomes electron head<3>
		if there are exactly 1 or 2 electron heads around it. )

( devices )

|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
|80 @Controller &vector $2 &button $1 &key $1 &func $1
|90 @Mouse &vector $2 &x $2 &y $2 &state $1 &pad $3 &modx $2 &mody $2

|0000

@color $1
@pointer &x $2 &y $2
@timer &frame $1 &play $1

(
@|vectors )

|0100 ( -> )

	( theme )
	#0fbf .System/r DEO2
	#0f6c .System/g DEO2
	#0f76 .System/b DEO2
	( size )
	#0190 .Screen/width DEO2
	#00f0 .Screen/height DEO2
	( setup )
	#01 .timer/play STZ
	#01 .color STZ
	.Screen/width DEI2 #01 SFT2 .pointer/x STZ2
	.Screen/height DEI2 #01 SFT2 .pointer/y STZ2
	( start )
	;world ;get-addr/current STA2
	#1000 ;run/future STA2
	;redraw JSR2
	( unlock )
	;on-frame .Screen/vector DEO2
	;on-mouse .Mouse/vector DEO2
	;on-button .Controller/vector DEO2

BRK

@on-frame ( -> )

	.Controller/button DEI
	DUP #02 AND #00 EQU ,&no-b JCN
		POP #00
		&no-b
	( move )
	DUP #f0 AND #00 EQU ,&no-clear JCN
		#00 ;draw-cursor JSR2
		&no-clear
	DUP #10 AND #00 EQU ,&no-u JCN
		.pointer/y LDZ2 #0002 SUB2
		DUP2 #8000 LTH2 ,&no-cu JCN
			POP2 .Screen/height DEI2
			&no-cu
			.pointer/y STZ2
		&no-u
	DUP #20 AND #00 EQU ,&no-d JCN
		.pointer/y LDZ2 INC2 INC2
		.Screen/height DEI2 ( mod2 ) [ DIV2k MUL2 SUB2 ]
			.pointer/y STZ2
		&no-d
	DUP #40 AND #00 EQU ,&no-l JCN
		.pointer/x LDZ2 #0002 SUB2
		DUP2 #8000 LTH2 ,&no-cl JCN
			POP2 .Screen/width DEI2
			&no-cl
			.pointer/x STZ2
		&no-l
	DUP #80 AND #00 EQU ,&no-r JCN
		.pointer/x LDZ2 INC2 INC2
		.Screen/width DEI2 ( mod2 ) [ DIV2k MUL2 SUB2 ]
			.pointer/x STZ2
		&no-r
	DUP #f0 AND #00 EQU ,&no-draw JCN
		#01 ;draw-cursor JSR2
		&no-draw
	( paint )
	DUP #01 AND #00 EQU ,&no-a JCN
		#00 .timer/play STZ
		( color ) .color LDZ
		( cell* ) .pointer/x LDZ2 #04 SFT2 NIP .pointer/y LDZ2 #04 SFT2 NIP
			;get-addr JSR2 STA
			;redraw JSR2
		&no-a
	POP

	.timer/play LDZ [ JMP BRK ]

	( every 4th )
	.timer/frame LDZk
		#03 AND ,&no-run JCN
			;run JSR2
			&no-run
		LDZk INC SWP STZ

BRK

@on-button ( -> )

	.Controller/button DEI
	DUP #03 NEQ ,&no-ab JCN
		#00 .Controller/button DEO
		;toggle-play JSR2 POP BRK
		&no-ab
	DUP #12 NEQ ,&no-bu JCN
		#00 ;set-mode JSR2 POP BRK
		&no-bu
	DUP #22 NEQ ,&no-bd JCN
		#01 ;set-mode JSR2 POP BRK
		&no-bd
	DUP #42 NEQ ,&no-bl JCN
		#02 ;set-mode JSR2 POP BRK
		&no-bl
	DUP #82 NEQ ,&no-br JCN
		#03 ;set-mode JSR2 POP BRK
		&no-br
	[ #08 ] NEQk NIP ,&no-start JCN
		;world #2000 ;mclr JSR2 ;redraw JSR2 
		&no-start
	POP
	( space )
	.Controller/key DEI #20 NEQ ,&no-space JCN
		;toggle-play JSR2
		&no-space

BRK

@on-mouse ( -> )

	#00 ;draw-cursor JSR2
	( draw new cursor )
	.Mouse/x DEI2 .pointer/x STZ2
	.Mouse/y DEI2 .pointer/y STZ2
	#01 ;draw-cursor JSR2
	( paint )
	.Mouse/state DEI ,on-mouse-down JCN

BRK

@on-mouse-down ( -> )

	( color ) .color LDZ .Mouse/state DEI #01 GTH #00 EQU MUL
	( cell* ) .Mouse/x DEI2 #04 SFT2 NIP .Mouse/y DEI2 #04 SFT2 NIP
		;get-addr JSR2 STA
	;redraw JSR2

BRK

(
@|drawing )

@draw-cursor ( mask -- )

	#01 .Screen/auto DEO
	;pointer-icn .Screen/addr DEO2
	.pointer/x LDZ2 .Screen/x DEO2
	.pointer/y LDZ2 .Screen/y DEO2
	DUP #40 ADD .Screen/sprite DEO

	.pointer/x LDZ2 #0008 ADD2 .Screen/x DEO2
	.pointer/y LDZ2 #0008 ADD2 .Screen/y DEO2
	;mode-icns #00 .color LDZ #30 SFT2 ADD2 .Screen/addr DEO2
	#40 ADD .Screen/sprite DEO

JMP2r

(
@|core )

@run ( -- )

	#2000
	&ver
		STHk
		#2000
		&hor
			( x,y ) DUP STHkr
			( cell ) DUP2 ,get-addr JSR STH2k LDA
			( transform ) ;transform JSR2 STH2r [ LIT2 &future $2 ] ADD2 STA
			INC GTHk ,&hor JCN
		POP2
		POPr
		INC GTHk ,&ver JCN
	POP2
	( Swap worlds )
	;get-addr/current LDA2k ;run/future LDA2 STH2k ADD2 SWP2 STA2
	#0000 STH2r SUB2 ;run/future STA2

@redraw ( -- )

	#15 .Screen/auto DEO
	#0000 DUP2 .Screen/x DEO2 .Screen/y DEO2
	;cell-icn .Screen/addr DEO2
	#0f00
	&ver
		STHk
		#1900
		&hor
			#00 OVR STHkr ,get-addr JSR LDA #50 SFT2 ;cell-chrs ADD2 .Screen/addr DEO2
			#01 .Screen/sprite DEOk DEO
			INC GTHk ,&hor JCN
		POP2
		#0000 .Screen/x DEO2
		.Screen/y DEI2k #0010 ADD2 ROT DEO2
		POPr
		INC GTHk ,&ver JCN
	POP2

JMP2r

@get-addr ( x y -- addr* )

	#00 SWP #50 SFT2 ROT #00 SWP ADD2 [ LIT2 &current $2 ] ADD2

JMP2r

@transform ( xy cell -- cell )

	DUP ,&no-null JCN NIP NIP JMP2r &no-null
	DUP #03 NEQ ,&no-head JCN POP POP2 #02 JMP2r &no-head
	DUP #02 NEQ ,&no-tail JCN POP POP2 #01 JMP2r &no-tail
	DUP #01 NEQ ,&no-cond JCN POP
		LITr 00
		DUP2 #01 SUB ,get-addr JSR
			( tl ) #0001 SUB2 LDAk #03 NEQ JMP INCr
			( tc ) INC2 LDAk #03 NEQ JMP INCr
			( tr ) INC2 LDA #03 NEQ JMP INCr
		DUP2 ,get-addr JSR
			( ml ) #0001 SUB2 LDAk #03 NEQ JMP INCr
			( mr ) INC2 INC2 LDA #03 NEQ JMP INCr
		INC ,get-addr JSR
			( bl ) #0001 SUB2 LDAk #03 NEQ JMP INCr
			( bc ) INC2 LDAk #03 NEQ JMP INCr
			( br ) INC2 LDA #03 NEQ JMP INCr
		STHkr #02 EQU STHr #01 EQU ORA
		DUP ADD INC JMP2r
		&no-cond
	( unknown )
	NIP NIP

JMP2r

@set-mode ( color -- )

	#00 ;draw-cursor JSR2
	.color STZ
	#01 ;draw-cursor ( .. )

JMP2

@toggle-play ( -- )

	.timer/play LDZk #00 EQU SWP STZ
	#01 ;draw-cursor ( .. )

JMP2

(
@|stdlib )

@mclr ( addr* len* -- )

	OVR2 ADD2 SWP2
	&loop
		STH2k #0000 STH2r STA2
		INC2 INC2 GTH2k ,&loop JCN
	POP2 POP2

JMP2r

@print ( short* -- )

	SWP ,&byte JSR
	&byte ( byte -- ) DUP #04 SFT ,&char JSR
	&char ( char -- ) #0f AND DUP #09 GTH #27 MUL ADD #30 ADD #18 DEO

JMP2r

(
@|assets )

@pointer-icn
	80c0 e0f0 f8e0 1000
@cell-icn
	7c82 8282 8282 7c00
@cell-chrs
	0000 0000 0000 0001 0000 0000 0000 0000
	0000 0000 0000 0000 0000 0000 0000 0000
	0f30 4040 8080 8080 8080 8040 4030 0f00
	e018 0404 0202 0202 0202 0204 0418 e000
	0f30 4040 8384 8888 8884 8340 4030 0f00
	e018 0404 8242 2222 2242 8204 0418 e000
	0f3f 7f7f fefc f8f0 f8fc fe7f 7f3f 0f00
	e0f8 fcfc fe7e 3e1e 3e7e fefc fcf8 e000
@mode-icns
	0000 0000 0000 0000
	3844 8282 8244 3800
	3844 92aa 9244 3800
	1038 7cfe 7c38 1000
@color-icn
	7cfe fefe fefe 7c00
@toggle-icn
	( pause ) 6666 6666 6666 6600
	( play ) 4666 767e 7666 4600

(
	I live in the atom with the happy protons and neutrons.
	I'm also so negative all the freakin time.
	What do I do?
	How do I find peace? )

@world

M src/main.c => src/main.c +2 -1
@@ 96,7 96,7 @@ screen_deo(Uint8 *d, Uint8 port)
		POKDEV(0xc, addr);   /* auto addr+length */
		POKDEV(0x8, x + dx); /* auto x+8 */
		POKDEV(0xa, y + dy); /* auto y+8 */
		pd->graphics->markUpdatedRows(y, y + 7);
		pd->graphics->markUpdatedRows(y, y + 7 + n*8);
		break;
	}
	}


@@ 221,6 221,7 @@ eventHandler(PlaydateAPI *playdate, PDSystemEvent event, uint32_t arg)
	if(event == kEventInit) {
		pd = playdate;
		pd->display->setRefreshRate(60);
		pd->display->setScale(1);
		pd->graphics->clear(kColorBlack);
		memset(&ppu, 0, sizeof(ppu));
		if(!ppu_init(&ppu, (uint32_t *)pd->graphics->getFrame()))