:: krowemoh

Friday | 13 DEC 2024
Posts Links Other About Now

previous
next

Uxn - Day 4

2023-01-07

Another day!

https://compudanzas.net/uxn_tutorial_day_4.html

This day went over running things on frames for smoother experiences. Instead of triggering events based on the controller, now events can be monitored on each frame which is pretty cool and definitely smooths things out.

I think that was what was missing in my idea of how stuff works. I have tried to build something that will just run in universe but it always requires input. This is because it is reliant on their being input for the next tick. In Uxn you can tick alongside the frame and do all the controller handling on each tick.

This also made it clear what it means to make sure all of you logic runs in the ms of each tick.

One thing that bugs me is that on day 3 I got wasd controls working. I wanted to continue using them today but for some reason they just won't work. I followed the tutorial to get the controller buttons working but the keys are being weird. Hopefully it will become clear once I finish the tutorial.

I'm slowly getting the hang of uxn.

The final code as of day 4:

( day 4 )

( devices )

|00 @System [ &vector $2 &pad $6 &r $2 &g $2 &b $2 ]
|10 @Console [ &vector $2 &read $1 &pad $5 &write $1 &error $1 ]
|20 @Screen [ &vector $2 &width $2 &height $2 &pad $2 &x $2 &y $2 &addr $2 &pixel $1 &sprite $1 ]
|80 @Controller [ &vector $2 &button $1 &key $1 ]

( macros )
%DRAW-SQUARE {
    .sprite/x LDZ2 .Screen/x DEO2
    .sprite/y LDZ2 .Screen/y DEO2

    ;flower .Screen/addr DEO2
    #01 .Screen/sprite DEO
}

( zero page )
|0000
    @framecount $1
    @sprite [ &x $2 &y $2 &speed $2 ]

( main )
|0100
    #2ce9 .System/r DEO2
    #01c0 .System/g DEO2
    #2ce5 .System/b DEO2


    #0030 .sprite/x STZ2
    #0030 .sprite/y STZ2
    #0004 .sprite/speed STZ2

    ;on-frame .Screen/vector DEO2

    DRAW-SQUARE
BRK

@on-frame
    .framecount LDZ INC .framecount STZ
    #00 .Screen/sprite DEO

    .framecount LDZ
    #01 SFT ( cut in half the frame rate)
    #07 AND ( get the modulo of the framerate to get 0-7 )
    #08 MUL ( multiply by 8 to get the offset)
    #00 SWP ( padd the offset to a short)
    ;animation ADD2 ( add the offset and address to get the frame to display)
    .Screen/addr DEO2 ( set the screen addr to the frame)

    #0030 .Screen/x DEO2
    #0030 .Screen/y DEO2

    #4e .Screen/sprite DEO

    .Controller/key DEI
    LIT "w  EQU
    ,&up JCN

    .Controller/key DEI
    LIT "a EQU
    ,&left JCN

    .Controller/key DEI
    LIT "s EQU
    ,&down JCN

    .Controller/key DEI
    LIT "d EQU
    ,&right JCN

    .Controller/button DEI
    #10 AND 
    ,&up JCN

    .Controller/button DEI
    #20 AND 
    ,&down JCN

    .Controller/button DEI
    #40 AND 
    ,&left JCN

    .Controller/button DEI
    #80 AND
    ,&right JCN

    ,&drawSingle JMP

    &drawSingle
        ,&draw JMP

    &up
        .sprite/y LDZ2
        .sprite/speed LDZ2 SUB2
        DUP2 #0000 EQU2 ,&edge JCN

        .sprite/y STZ2
        ,&draw JMP

    &left
        .sprite/x LDZ2
        .sprite/speed LDZ2 SUB2
        DUP2 #0000 EQU2 ,&edge JCN

        .sprite/x STZ2
        ,&draw JMP

    &down
        .sprite/y LDZ2
        .sprite/speed LDZ2 ADD2
        DUP2 .Screen/height DEI2 #0008 SUB2 EQU2 ,&edge JCN

        .sprite/y STZ2
        ,&draw JMP

    &right
        .sprite/x LDZ2
        .sprite/speed LDZ2 ADD2
        DUP2 .Screen/width DEI2 #0008 SUB2 EQU2 ,&edge JCN

        .sprite/x STZ2
        ,&draw JMP

    &edge
        POP
        ,&draw JMP

    &draw
        DRAW-SQUARE
BRK

@flower 9942 2499 9924 4299

@animation
    &frame0 00 00 00 00 00 00 01 03
    &frame1 00 00 00 00 01 03 06 0c
    &frame2 00 00 01 03 06 0c 18 30
    &frame3 01 03 06 0c 18 30 60 c0
    &frame4 03 06 0c 18 30 60 c0 80
    &frame5 0c 18 30 60 c0 80 00 00
    &frame6 30 60 c0 80 00 00 00 00
    &frame7 c0 80 00 00 00 00 00 00