minor corrections to drawing pixels section

This commit is contained in:
sejo 2024-03-14 21:07:23 +01:00
parent f4c8f22e31
commit 806aa98322
2 changed files with 27 additions and 25 deletions

View File

@ -30,7 +30,7 @@ implement the following concepts as {coloring computers}:
### day 2 and onwards
* remove square brackets in devices?
* recap how to use learn-uxn for the programs there
* divide into morning and evening
* update images if needed
* check blend mode 0
=> https://lists.sr.ht/~rabbits/uxn/%3C692CA0D5-0200-408D-9357-BF0D8887D2BF%40noyu.me%3E Blend mode 0

View File

@ -248,18 +248,18 @@ whatever is drawn in the foreground layer will cover anything that is drawn in t
in the beginning, the foreground layer is completey transparent: a process of alpha blending makes sure that we can see the background layer.
# drawing a pixel
# drawing pixels
the first and simpler way to draw into the screen is drawing a single pixel.
in order to do this we need to set a pair of x,y coordinates where we want the pixel to be drawn, and we need to set the 'pixel' byte to a specific value to actually perform the drawing.
in order to do this, we need to set a pair of x,y coordinates where we want the pixel to be drawn, and we need to set the 'pixel' byte to a specific value to actually perform the drawing.
## setting the coordinates
the x,y coordinates follow conventions that are common to other computer graphics software:
* x starts in 0 at the left, and increases towards the right of the screen
* y starts in 0 at the top, and increases towards the bottom of the screen
* x starts at 0 from the left, and increases towards the right of the screen.
* y starts at 0 from the top, and increases towards the bottom of the screen.
if we wanted to draw a pixel in coordinates ( 8, 8 ), we'd set its coordinates in this way:
@ -290,15 +290,15 @@ the low nibble of the byte, i.e. the hexadecimal digit at the right, will determ
the 8 possible combinations of the 'pixel' byte that we have for drawing a pixel are:
+ <table>
+ <tr><th>pixel</th><th>color</th><th>layer</th></tr>
+ <tr><td class="num">00</td><td class="num">0</td><td>bg</td></tr>
+ <tr><td class="num">01</td><td class="num">1</td><td></td></tr>
+ <tr><td class="num">02</td><td class="num">2</td><td></td></tr>
+ <tr><td class="num">03</td><td class="num">3</td><td></td></tr>
+ <tr><td class="num">40</td><td class="num">0</td><td>fg</td></tr>
+ <tr><td class="num">41</td><td class="num">1</td><td></td></tr>
+ <tr><td class="num">42</td><td class="num">2</td><td></td></tr>
+ <tr><td class="num">43</td><td class="num">3</td><td></td></tr>
+ <tr><th>pixel</th><th>layer</th><th>color</th></tr>
+ <tr><td class="num">00</td><td>bg</td><td class="num">0</td></tr>
+ <tr><td class="num">01</td><td></td><td class="num">1</td></tr>
+ <tr><td class="num">02</td><td></td><td class="num">2</td></tr>
+ <tr><td class="num">03</td><td></td><td class="num">3</td></tr>
+ <tr><td class="num">40</td><td>fg</td><td class="num">0</td></tr>
+ <tr><td class="num">41</td><td></td><td class="num">1</td></tr>
+ <tr><td class="num">42</td><td></td><td class="num">2</td></tr>
+ <tr><td class="num">43</td><td></td><td class="num">3</td></tr>
+ </table>
& background layer:
& * 00: draw pixel with color 0
@ -314,7 +314,7 @@ the 8 possible combinations of the 'pixel' byte that we have for drawing a pixel
## hello pixel
let's try it all together! the following code will draw a pixel with color 1 in the foreground layer, at coordinates (8,8)
let's try it all together! the following code will draw a pixel using color1 in the foreground layer, at coordinates ( 8, 8 )
```
#0008 .Screen/x DEO2
@ -328,8 +328,8 @@ the complete program would look as follows:
( hello-pixel.tal )
( devices )
|00 @System [ &vector $2 &pad $6 &r $2 &g $2 &b $2 ]
|20 @Screen [ &vector $2 &width $2 &height $2 &pad $2 &x $2 &y $2 &addr $2 &pixel $1 &sprite $1 ]
|00 @System &vector $2 &pad $6 &r $2 &g $2 &b $2
|20 @Screen &vector $2 &width $2 &height $2 &pad $2 &x $2 &y $2 &addr $2 &pixel $1 &sprite $1
( main program )
|0100
@ -341,13 +341,11 @@ the complete program would look as follows:
( draw a pixel in the screen )
#0008 .Screen/x DEO2
#0008 .Screen/y DEO2
#41 .Screen/pixel DEO ( fg layer, color 1 )
#41 .Screen/pixel DEO ( fg layer, color1 )
```
woohoo!
remember you can use F1 to switch between zoom levels, and F3 to take screenshots of your sketches :)
## hello pixels
the values we set to the x and y coordinates stay there until we overwrite them.
@ -378,17 +376,19 @@ for example, we can draw multiple pixels in an horizontal line, setting the y co
#41 .Screen/pixel DEO
```
note that we have to set the color for each pixel we draw; that operation signals the drawing and has to be repeated.
note that we have to set the color for each pixel we draw. remember that the operation of writing to the pixel byte signals the drawing and therefore has to be repeated.
we can define a macro to make this process easier to write:
we can define a macro to make the process of drawing a pixel easier to write:
```
%DRAW-PIXEL { #41 .Screen/pixel DEO } ( -- )
```
in this way, whenever we write DRAW-PIXEL, the assemblers that support macros will replace it with the corresponding uxntal code.
## reading and manipulating coordinates
we will not cover repetitive structures yet, but this is a good opportunity to start aligning our code towards that.
we will not cover control flow yet, but this is a good opportunity to start aligning our code towards eventually creating loops and repetive structures.
even though the x and y coordinates of the screen device are intended as outputs, we can also read them as inputs.
@ -444,6 +444,8 @@ our macro for incrementing the x coordinate could be then written as follows:
%INC-X { .Screen/x DEI2 INC2 .Screen/x DEO2 } ( -- )
```
this change saves a couple of bytes and for performance-intensive applications can make our programs run slightly faster.
## hello pixels using macros
using these macros we defined above, our code could end up looking as following:
@ -452,8 +454,8 @@ using these macros we defined above, our code could end up looking as following:
( hello-pixels.tal )
( devices )
|00 @System [ &vector $2 &pad $6 &r $2 &g $2 &b $2 ]
|20 @Screen [ &vector $2 &width $2 &height $2 &pad $2 &x $2 &y $2 &addr $2 &pixel $1 &sprite $1 ]
|00 @System &vector $2 &pad $6 &r $2 &g $2 &b $2
|20 @Screen &vector $2 &width $2 &height $2 &pad $2 &x $2 &y $2 &addr $2 &pixel $1 &sprite $1
( macros )
%DRAW-PIXEL { #41 .Screen/pixel DEO } ( -- )