I made a program to design 8x8 monochrome sprites and then output the 16-character hex code for it. I've been doing some optimizations, but I feel like it could lose quite a lot of bytes, and at the end when it calculates the hex code it could be much faster. Any ideas? (Sorry that this is such a mess.) My focus is mainly on size, except for the last part, which I'm trying to optimize for speed.
This code essentially draws 2 boxes: One, with opposite vertices of (18,-18) and (29,-29), is for the user. A small cross (I used Pt-On for this) is moved around by the arrow keys, and is prevented from leaving the square. The square is 10x10, but the movement area is 8x8, so that the cross cannot erase any of the square.
The second box has opposite vertices of (38,-18) and (49,-29), and is for the image. When the user presses "Enter," the pixel in the Image box corresponding to the position of the user in the User box changes color. So you move the user in the user box, then press Enter to change around how the Image box looks. This is solely so that the image isn't screwed up by having the user pointer in it. The Image Box also is 10x10, so that even if the entire 8x8 sprite is filled, there is a layer of white pixels between the box outlines and the sprite.
Recall GDB0 //See note below on GDB0
DispGraph
18→X
29→Y
Line(X,-X,X,-Y //This entire mess of code is to draw the 2 box outlines.
Line(X,-X,Y,-X
Line(Y,-Y,X,-Y
Line(Y,-Y,Y,-X
38→I
49→J
Line(I,-X,J,-X
Line(I,-X,I,-Y
Line(J,-Y,I,-Y
Line(J,-Y,J,-X
0→K //Keypress Tester
20→X
-20→Y
Pt-On(X,Y,3
Repeat K=22 //This main loop gets exited out when the user presses "Mode"
Repeat Ans
getKey //Waits for a keypress
End
Ans→K
Pt-Off(X,Y,3 //Erases the user pointer
max(20,min(27,X=sum(ΔList(K={24,26→X //Change X and Y coords
max(-27,min(-20,Y+sum(ΔList(K={34,25→Y
Pt-On(X,Y,3 //Redraw User Pointer
If K=105 //If you press Enter, change the Image Square pixel
Pxl-Change(-Y,20+X
If K=23:Then //If you press Del, clear the Image box
For(I,40,47
For(J,20,27
Pxl-Off(J,I
End:End:End
If K=45 //If Clear is pressed, end the Program
Return
End
//When Mode is pressed, the main loop terminates, and the to-hex part begins
"?→Str1
For(I,0,15 //Repeat for all 16 4-by-1 "segments" of the image, each of which is 1 character.
remainder(I,2→J //Calculate if this segment is on the right or left side of the image
20+(I-J)/2→K //Figure out which row of the image this segment is on
0 //Reset Ans
For(X,3,0,-1
Ans+2^X*pxl-Test(K,40+4J+3-X //Each time it loops through, it reads a new pixel in the segment, and adds it
on to Ans. So 1110 translates to 8=8, 8+4=12, 12+2=14, so the value output
at the end of this loop is 14.
End
sub("0123456789ABCDEF",Ans+1,1 //Translates the decimal value into a hex value
Str1+Ans→Str1 //Adds the letter (which is the hex value of that segment) onto Str1
End
sub(Str1,2,length(Str1)-1→Str1 //Gets rid of the "?" at the beginning of Str1
Ans
Note on GDB0:
This is here so that all of the graph settings get set so that Xmin=0, Xmax=94, Ymin=-62, Ymax=0 (so that every pixel has its own coordinates, making the Pxl-On( and Pt-On( interchangeable, and to improve usage with the Line(Command
Note on what I mean by "Segments:"
An 8x8 sprite outputs a 16-character hex string. Each set of 4 pixels in a row translates to half a byte, or one of those characters. So the string of pixels 1101 would translate to the hex character D. I call each of these segments "segments" (I think the proper term is actually nibbles, but I'm not sure.) There are 2 segments per row of the sprite, and 8 rows of the image, which makes the 16 segments.