( uxnemu icnchr.rom image20x20.chr )

|00 @System &vector $2 &wst $1 &rst $1 &pad $4 &r $2 &g $2 &b $2 &debug $1 &halt $1
|10 @Console &vector $2 &read $1 &pad $5 &write $1 &error $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
|a0 @File &vector $2 &success $2 &stat $2 &delete $1 &append $1 &name $2 &length $2 &read $2 &write $2

|0000

	@src $40

|0100 ( -> )

	set-theme-dark

	#0280 .Screen/width DEO2 ( 640px )
	#01e0 .Screen/height DEO2 ( 480px )

	;on-console .Console/vector DEO2
	;on-control .Controller/vector DEO2

	load-theme

BRK

@on-console ( -> )

	LIT2r =src
	( read source )
	.Console/read DEI
	DUP #20 LTH OVR #7f GTH ORA ?&end
	STH2kr slen #003f GTH2 ?&end
		STH2r sput BRK
		&end
	POP

	( mode from src )
	STH2kr scap #0004 SUB2 ;&icn-ext scmp #00 EQU ?&no-icn
		set-mode-icn
		&no-icn

	( resize from src )
	STH2kr scap #0009 SUB2 read-size
	ORAk #00 EQU ?&no-size
		OVR #00 SWP #30 SFT2 .Screen/width DEO2
		#00 OVR INC #30 SFT2 .Screen/height DEO2
		&no-size
	POP2

	STH2r draw-image

BRK
	&icn-ext ".icn $1

@on-control ( -> )

	.Controller/button DEI
	DUP #10 NEQ ?&no-u
		.Screen/height DEI2 #0008 SUB2 .Screen/height DEO2
		;src draw-image
		&no-u
	DUP #20 NEQ ?&no-d
		.Screen/height DEI2 #0008 ADD2 .Screen/height DEO2
		;src draw-image
		&no-d
	DUP #40 NEQ ?&no-l
		.Screen/width DEI2 #0008 SUB2 .Screen/width DEO2
		;src draw-image
		&no-l
	DUP #80 NEQ ?&no-r
		.Screen/width DEI2 #0008 ADD2 .Screen/width DEO2
		;src draw-image
		&no-r
	DUP #01 NEQ ?&no-a
		toggle-theme
		&no-a
	DUP #02 NEQ ?&no-b
		toggle-mode
		&no-b
	POP

BRK

@toggle-theme ( -- )

	.System/r DEI2 #05cf EQU2 ?&no-light
		set-theme-dark
		;src !draw-image
		&no-light
	set-theme-light
	;src

!draw-image

@set-theme-dark ( -- )

	#05cf .System/r DEO2
	#05cf .System/g DEO2
	#05cf .System/b DEO2

JMP2r

@set-theme-light ( -- )

	#fc50 .System/r DEO2
	#fc50 .System/g DEO2
	#fc50 .System/b DEO2

JMP2r

@toggle-mode ( -- )

	;draw-image/length LDA2 #0010 EQU2 ?&no-chr
		set-mode-chr
		;src !draw-image
		&no-chr
	set-mode-icn
	;src

!draw-image

@set-mode-chr ( -- )

	#0010 ;draw-image/length STA2
	#81 ;draw-image/color STA

JMP2r

@set-mode-icn ( -- )

	#0008 ;draw-image/length STA2
	#03 ;draw-image/color STA

JMP2r

@read-size ( 00x00* -- w h )

	DUP2 sbyte #00 EQU ?&cancel
	INC2k INC2 LDA LIT "x NEQ ?&cancel
	DUP2 #0003 ADD2 sbyte #00 EQU ?&cancel
	( y ) DUP2 #0003 ADD2 sbyte STH
	( x ) sbyte STHr

JMP2r
	&cancel POP2 #0000 JMP2r

(
@|drawing )

@draw-image ( name* -- )

	#0000 DUP2 .Screen/x DEO2 .Screen/y DEO2

	.File/name DEO2
	[ LIT2 &length 0010 ] .File/length DEO2
	;&buf .Screen/addr DEO2
	#01 .Screen/auto DEO
	&stream
		;&buf .File/read DEO2
		[ LIT &color 81 ] .Screen/sprite DEO
		.Screen/x DEI2 .Screen/width DEI2 LTH2 ?&no-line
			#0000 .Screen/x DEO2
			.Screen/y DEI2k #0008 ADD2 ROT DEO2
			&no-line
		.File/success DEI2 ORA ?&stream

!draw-interface
	&buf $10

@draw-interface ( -- )

	#0000 .Screen/x DEO2
	.Screen/height DEI2 #0008 SUB2 .Screen/y DEO2

	;src draw-str

	( size )
	LIT "( draw-chr
	.Screen/width DEI2 #03 SFT2 NIP draw-byte
	LIT "x draw-chr
	.Screen/height DEI2 #03 SFT2 NIP draw-byte
	LIT ")

!draw-chr

@draw-str ( str* -- )

	LDAk #00 EQU ?&skip
	&while
		LDAk draw-chr
		INC2 LDAk ?&while
	&skip
	POP2

JMP2r

@draw-byte ( byte -- )

	DUP #04 SFT draw-hex

@draw-hex ( char -- )

	#0f AND DUP #09 GTH #07 MUL ADD #30 ADD

@draw-chr ( char -- )

	#20 SUB #00 SWP #30 SFT2 ;font ADD2 .Screen/addr DEO2
	[ LIT &color 03 ] .Screen/sprite DEO

JMP2r

(

@|stdlib )

@slen ( str* -- len* )

	DUP2 scap SWP2 SUB2

JMP2r

@scap ( str* -- end* )

	LDAk #00 NEQ JMP JMP2r
	&while INC2 LDAk ?&while

JMP2r

@sput ( char str* -- )

	scap STA

JMP2r

@sbyte ( str* -- byte )

	LDAk chex STH
	INC2 LDA chex
	STHr #40 SFT ADD

JMP2r

@chex ( char -- hex )

	DUP #2f GTH OVR #3a LTH AND ?&number
	DUP #60 GTH OVR #67 LTH AND ?&lc
	DUP #40 GTH OVR #47 LTH AND ?&uc
		POP #00 JMP2r
	&number #30 SUB JMP2r
	&uc #37 SUB JMP2r
	&lc #57 SUB

JMP2r

@scmp ( a* b* -- flag )

	STH2
	&loop
		LDAk LDAkr STHr NEQ ?&end
		LDAk LDAkr STHr ORA ?&not-end
			POP2 POP2r #01 JMP2r
			&not-end
		INC2 INC2r !&loop
	&end
	POP2 POP2r #00

JMP2r

( theme )

@load-theme ( -- )

	;&path .File/name DEO2
	#0002 .File/length DEO2
	;&r .File/read DEO2
	;&g .File/read DEO2
	;&b .File/read DEO2
	.File/success DEI2 ORA #01 JCN JMP2r
	LIT2 &r $2 .System/r DEO2
	LIT2 &g $2 .System/g DEO2
	LIT2 &b $2 .System/b DEO2

JMP2r
	&path ".theme $1

(
@|assets )

@font ( block8 )
0000 0000 0000 0000 1010 1010 1000 1000
2828 0000 0000 0000 44fe 4444 44fe 4400
107e 807c 02fc 1000 0044 0810 2044 0000
7884 8864 9a84 7a00 1020 0000 0000 0000
2040 4040 4040 2000 0804 0404 0404 0800
0044 2810 2844 0000 0010 107c 1010 0000
0000 0000 0010 2000 0000 007c 0000 0000
0000 0000 0000 1000 0204 0810 2040 8000
7c82 8282 8282 7c00 3010 1010 1010 3800
7c82 027c 8080 fe00 7c82 021c 0282 7c00
2242 82fe 0202 0200 fe80 807c 0282 7c00
7c82 80fc 8282 7c00 fe82 0408 0810 1000
7c82 827c 8282 7c00 7c82 827e 0202 0200
0000 1000 1000 0000 0000 1000 1010 2000
0008 1020 1008 0000 0000 7c00 7c00 0000
0010 0804 0810 0000 7c82 820c 1000 1000
7c82 92aa aa92 6400 7c82 82fe 8282 8200
fc82 82fc 8282 fc00 7c82 8080 8082 7c00
fc82 8282 8282 fc00 fe80 80f0 8080 fe00
fe80 80f0 8080 8000 7c82 808e 8282 7c00
8282 82fe 8282 8200 3810 1010 1010 3800
0602 0202 0282 7c00 8284 88f0 8884 8200
8080 8080 8080 fe00 ec92 9292 9292 9200
c2a2 a292 8a8a 8600 7c82 8282 8282 7c00
fc82 82fc 8080 8000 7c82 8282 8a84 7a00
fc82 82fc 8282 8200 7c82 807c 0282 7c00
fe10 1010 1010 1000 8282 8282 8286 7a00
8282 4444 2828 1000 9292 9292 9292 6c00
8244 2810 2844 8200 8282 4428 1010 1000
fe02 0c10 6080 fe00 6040 4040 4040 6000
8040 2010 0804 0200 0c04 0404 0404 0c00
1028 0000 0000 0000 0000 0000 0000 fe00
1008 0000 0000 0000 0000 7c02 7e82 7e00
0000 fc82 fc82 fc00 0000 7c82 8082 7c00
0000 027e 8282 7e00 0000 7c82 fc80 7e00
0000 7c82 e080 8000 0000 7c82 7e02 fc00
0000 80fc 8282 8200 0000 1000 1010 1000
0000 0200 0282 7c00 0000 8284 f884 8200
0000 8080 8080 7e00 0000 6c92 9292 9200
0000 bcc2 8282 8200 0000 7c82 8282 7c00
0000 fc82 fc80 8000 0000 7c82 7e02 0200
0000 bcc2 8080 8000 0000 7e80 7c02 fc00
0000 fe10 1010 1000 0000 8282 8282 7c00
0000 8282 4428 1000 0000 9292 9292 6c00
0000 8244 3844 8200 0000 8282 7e02 7c00
0000 fe02 7c80 fe00 2040 4080 4040 2000
1010 1010 1010 1000 0804 0402 0404 0800
0000 0032 4c00 0000 aa55 aa55 aa55 aa55