Omnimaga

Omnimaga => Completed => Our Projects => BatLib => Topic started by: Munchor on April 08, 2011, 11:47:31 am

Title: BatLib/ReCode Examples
Post by: Munchor on April 08, 2011, 11:47:31 am
Most of what I know about programming, I learnt it by looking at sample codes and examples, so I thought that I could make some BatLib samples and explain them.

Here's my first code sample, draw a rectangle around the screen and wait for pressing [CLEAR] to close.

Code: [Select]
dim(1,40                 ; Disables Indicator (dim(1)) and Turns Basic ReCode on ((dim(40)). In BatLib, we can have multiple commands in a single dim(
Fill(0                   ; AxesOff, ClrDraw, basically, it cleans the screen in a Recode Block
While getKey≠15          ; waits till a [CLEAR] press
Line(0,96,0,64,6         ; Draws a rectangle, X=0, WIDTH=96,Y=0;HEIGHT=64;TYPE=6. Type 6 is borders only
DispGraph                ; Displays the graphscreen
End                      ; End of loop
Stop                     ; Disables ReCode and goes back to BASIC
AxesOn                   ; Turns Axes on

First of all, I need to clear that you should read the readme and recode readme first.

Then, this code is very simple and here's how it looks when ran:

(http://img.removedfromgame.com/imgs/CodeExample1.gif)

Hope you enjoyed this example, more to come.

Attached is the 8xp file.

EDIT: fixed up spacing and added ≠ with Scout's approval
Title: Re: BatLib/ReCode Examples
Post by: Munchor on April 09, 2011, 03:58:45 am
Here it comes, the second sample. It's going to do exactly what we did in the first sample, but in a much faster and smaller way!

Code: [Select]
dim(1,40                 ; Disables Indicator (dim(1)) and Turns Basic ReCode on ((dim(40)). In BatLib, we can have multiple commands in a single dim(
Fill(0                   ; AxesOff, ClrDraw, basically, it cleans the screen in a Recode Block
While getKey≠15          ; waits till a [CLEAR] press
Line(0,96,0,64,6         ; Draws a rectangle, X=0, WIDTH=96,Y=0;HEIGHT=64;TYPE=6. Type 6 is borders only
DispGraph                ; Displays the graphscreen
End                      ; End of loop
Stop                     ; Disables ReCode and goes back to BASIC
AxesOn                   ; Turns Axes on

This was last sample, here's the new sample, it doesn't use ReCode, only TI-Basic and BatLib:

Code: [Select]
While getKey!=45  ; "!=" is the difference symbol
dim(1,29,0,96,0,64,6,99
End
ClrDraw:ClrHome

Let's take a look at the meat of the program: dim(1,29,0,96,0,64,6,99, which can be broken to:

Code: [Select]
dim(1 ;Disables Indicator
dim(29,0,96,0,64,6 ;Creates a rectangle of type (6) borders only at (0,0)
dim(99 ;DispGraph

Hope you liked it ;)
Title: Re: BatLib/ReCode Examples
Post by: Xeda112358 on April 09, 2011, 12:45:50 pm
Yes, that is exactly the technique that I love about BatLib, personally. So here is another common example I use...
First, you want to draw a sprite or a rectangle using the XOR (or invert) option and you display the graph. Then, you can use dim(10,x to filter key presses (like waiting for arrows and the column of keys containing enter to be pressed) If there are two or more groups of keys, you can use dim(43 as another getkey. After that, you can use the first thing you did again without updating the screen.

TL;DR
Pretty much, this will display a rectangle, wait for a key press, and restore the screen:
Code: [Select]
dim(29,B,8,C,8,2
dim(99               ;Displays the inverted rectangle
dim(10,3             ;waits until Arrows or the column with enter to be pressed
dim(43→A             ;returns the key in Ans and is stored to A
dim(29,B,8,C,8,2     ;This reinverts the rectangle, but it isn't shown yet. Great for smooth graphics.
However, this can be all put together into one long sequence of commands by combining the inputs:
dim(29,B,8,C,8,2,99,10,3,43,29,B,8,C,8,2→A
Title: Re: BatLib/ReCode Examples
Post by: ztrumpet on April 09, 2011, 12:48:17 pm
I still don't understand how the chaining works; my brain doesn't comprehend the awesome. ;D
What if you leave arguments out of a command?  How does it know to go on to the next command?
Title: Re: BatLib/ReCode Examples
Post by: Xeda112358 on April 09, 2011, 12:53:58 pm
That is the downside to chaining commands... you cannot leave out arguments unless they are at the end. However, if you want to you can leave out arguments before a string argument, even if you are chaining.
Title: Re: BatLib/ReCode Examples
Post by: Munchor on April 10, 2011, 05:11:33 am
I still don't understand how the chaining works; my brain doesn't comprehend the awesome. ;D
What if you leave arguments out of a command?  How does it know to go on to the next command?

That's true, we can't forget any argument, but it makes code much smaller.

Zeda, does it count number of arguments to get to the next command?
Title: Re: BatLib/ReCode Examples
Post by: Xeda112358 on April 10, 2011, 11:05:34 am
No, it takes the first argument to decide which command to execute and then the command will use any arguments necessary. When the command is finished, it reads the next argument to figure out the next command. If BatLib reads a string when it expects a number, though, it reads the number as 0 and waits at the string until it is needed. That is why you can omit arguments before a string, even when stringing commands together :)
Title: Re: BatLib/ReCode Examples
Post by: Munchor on April 10, 2011, 11:30:06 am
Zeda, since I like to keep it clean, here's my code for your example:

Code: [Select]
:dim(40
:Fill(0
:Stop
:6→D
:DelVar B0→C
:Repeat A=15
:dim(29,B,8,C,8,2,99,10,3,43,29,B,8,C,8,2→A
:max(0,min(88,B+Dsum(DeltaList(A={2,3→B
:max(0,min(56,C+Dsum(DeltaList(A={4,1→C
:End

This cleans the screen and we don't have to see the ugly screen of BatLib App.
Title: Re: BatLib/ReCode Examples
Post by: Xeda112358 on April 10, 2011, 11:33:36 am
Yeah, I just needed to show that the cursor doesn't change what is underneath it :)
Title: Re: BatLib/ReCode Examples
Post by: Munchor on April 10, 2011, 11:34:50 am
Yeah, I just needed to show that the cursor doesn't change what is underneath it :)

Can you add a getKey for 2ND that will change it? So, if you press 2ND, it will dim(29,BB,8,C,8, turning to black the place where the cursor is.

So, like a paint program.
Title: Re: BatLib/ReCode Examples
Post by: Xeda112358 on April 10, 2011, 11:37:08 am
Yeah, if you check my demos program topic, you will find a better cursor routine that does something similar XD

EDIT: Never mind, it is here: http://ourl.ca/9627/184739
Title: Re: BatLib/ReCode Examples
Post by: Munchor on April 10, 2011, 02:48:00 pm
I just made a BatLib (almost no ReCode) program that draws a cursor and when the user presses [ENTER] it draws a rectangle of 8*8 in the place where the cursor is. A Paint program we might say.

I'll make a ReCode example in a few minutes and post it here, it should be faster and much better (4*4 rectangle cursor).

Code:
Code: [Select]
dim(40
Fill(0                       ;Inside ReCode blocks, this clears the screen, turns axes off
Stop
6->D
DelVar B0->C
Lbl Y
Repeat A=15             ;Will loop until pressing [CLEAR]
If getKey=105           ;GetKey for drawing
Then
Goto Z
End
dim(29,B,8,C,8,2,99,10,3,43,29,B,8,C,8,2->A
max(0,min(88,B+Dsum(DeltaList(A={2,3->B
max(0,min(56,C+Dsum(DeltaList(A={4,1->C
End
Return
Lbl Z
dim(29,B,8,C,8,1  ;Draws Rectangle
Goto Y               ;This goes back to beginning of loop

Probably it can be optimized, but I like it this way.

Screenshot:
(http://img.removedfromgame.com/imgs/PaintProgram8by8.gif)

Enjoy it, if you have any questions, comment.

EDIT:
Here's some updated, optimized code:

Code: [Select]
dim(1,56,4
6->D
DelVar B0->C
Lbl Y
Repeat A=15             ;Will loop until pressing [CLEAR]
If getKey=105           ;GetKey for drawing
Then
Goto Z
End
dim(29,B,8,C,8,2,99,10,3,43,29,B,8,C,8,2->A
max(0,min(88,B+Dsum(DeltaList(A={2,3->B
max(0,min(56,C+Dsum(DeltaList(A={4,1->C
End
Return
Lbl Z
dim(29,B,8,C,8,1  ;Draws Rectangle
Goto Y               ;This goes back to beginning of loop
Title: Re: BatLib/ReCode Examples
Post by: Xeda112358 on April 10, 2011, 03:20:41 pm
Sweet, nice job, Scout! I actually didn't know it would be that fast! Also, you didn't really need the ReCode block because you could do dim(56,4 instead. Still, it shows how ReCode can be integrated with BASIC nicely! Thanks!
Title: Re: BatLib/ReCode Examples
Post by: Munchor on April 10, 2011, 03:23:43 pm
Here's a very similar example, but it uses ReCode and not Basic+BatLib.

Code:
Code: [Select]
dim(40
Fill(0
3->D
->E
0->A->B->C
While A!=15
If A=9
Line(B,D,C,E,1
Line(B,D,C,E,2
DispGraph
getKey->A=0
DS<(1
Line(B,D,C,E,2
B
If A=3
+1
If A=2
-1
->B
C
If A=1
+1
If A=4
-1
->C
If B>=99
0->B
If B=97-D
96-D->B
If C=65-E
64-E->C
If C>=99
0->C
End
Stop

Screenshot:
(http://img.removedfromgame.com/imgs/CursorSample.gif)

I have to say I think the samples are getting quite complex, so I'll make a few simpler ones too.

Notes about this example:
Attached is the code in a 8xp file.
This was an example that focuses on ReCode and uses a lot of Zeda-made code.
Title: Re: BatLib/ReCode Examples
Post by: Xeda112358 on April 10, 2011, 06:12:42 pm
Again, very nice! And I like the display of speed and smoothness there, too. I keep trying to remake balltrix (made by DJ) into a ReCode game, but I keep forgetting to backup my calc before testing out other code !_!
Title: Re: BatLib/ReCode Examples
Post by: Munchor on April 10, 2011, 07:15:54 pm
I made a new Example for you coders!

Code:
Code: [Select]
dim(1,56,4,22,"FDCB00AEC9"       ;Disables run indicator, clears screen and disables done message
0->A
While A!=45                                      ;Until pressing [CLEAR]
AxesOff
For(I,0,96
AxesOff
dim(56,12,I,99                                   ;Draws vertical lines
I+1->I
End
DispGraph
getKey->A
End

Screenshot:
(http://img.removedfromgame.com/imgs/GridExample.gif)

Notes:
The dim(56 command is one of the best commands to use for graph screen management and it's very important you dominate it well.
dim(56,4 will clear the screen and set axes to off, just like Fill(0 in a ReCode block.

Yet again, if you have any questions on this, reply!
Title: Re: BatLib/ReCode Examples
Post by: Xeda112358 on April 10, 2011, 09:21:32 pm
I have to say, I love this idea, Scout! I wish I could think of graphics like this, but really, keep up the good work! Also, because I like to optimise things:
Code: [Select]
dim(1,56,4,22,"FDCB00AE        ;Disables run indicator, clears screen and disables done message
Repeat getKey=45               ;Until pressing [CLEAR]
For(I,0,94,2
dim(56,12,I,99                 ;Draws vertical lines
End
End
Title: Re: BatLib/ReCode Examples
Post by: Munchor on April 11, 2011, 04:59:20 am
I was beat (in optimization) by Zeda :P

I guess my next program may not be an example, but a demo, but I need ideas :P
Title: Re: BatLib/ReCode Examples
Post by: Munchor on April 11, 2011, 08:32:05 am
New Example!

This is a ReCode example but that could be made using BatLib and Basic and would probably be faster and smaller, but I wanted to show you another ReCode sample guys.

Code: [Select]
dim(1,56,4,22,"FDCB00AE",40
0->I
While getKey!=15
Shade(I
I+1->I
If I>39
0->I
Pause 5
End
Shade(24
Stop

First of all, it disables run indicator, clears the screen and disables "Done Message".

Then it goes through a loop of Shade(. In the end it sets Shade(24 to make it back to what's more or less normal.

Screenshot:
(http://img.removedfromgame.com/imgs/ShadeExample.gif)

What do you think? Attached is SHADEBAC.8xp :)
Title: Re: BatLib/ReCode Examples
Post by: Xeda112358 on April 11, 2011, 08:54:55 am
Aw, it's too bad the shading doesn't work x.x To be honest, I completely forgot about the Shade( command in ReCode XD
Title: Re: BatLib/ReCode Examples
Post by: Munchor on April 11, 2011, 09:07:30 am
Aw, it's too bad the shading doesn't work x.x To be honest, I completely forgot about the Shade( command in ReCode XD

It works, if you try it on WabbitEmu or in your calc, it works, the GIF Recording just doesn't get it.
Title: Re: BatLib/ReCode Examples
Post by: Xeda112358 on April 11, 2011, 08:46:22 pm
Yeah, that's what I meant to say ;D I hope that works someday, but I think I see the reason for why it doesn't record the contrast... the .gif files would get pretty large :/
Title: Re: BatLib/ReCode Examples
Post by: Munchor on April 12, 2011, 02:24:51 pm
Yeah, that's what I meant to say ;D I hope that works someday, but I think I see the reason for why it doesn't record the contrast... the .gif files would get pretty large :/

I made a new shade sample, but this one won't get to Shade(40 and then go back to Shade(1. Instead, it does something like this:

So, since the lightness GIF recording with WabbitEmu doesn't work, I'll just paste the code:

Code: [Select]
dim(56,4,1,22,"FDCB00AE",40
0->I->A
While getKey!=15
If I>40
0->A
If I<1
1->A
Shade(I
If A=1
I+1->I
If A=0
I-1->I
DispGraph
End
Shade(24
Stop

Attached is the file, like it? Any optimizations?
Title: Re: BatLib/ReCode Examples
Post by: Xeda112358 on April 12, 2011, 04:04:09 pm
This is my version... I added a Pause because it was a little fast XD
Code: [Select]
dim(1,22,"FDCB00AE",40
FullFill(0→I                        clears the screen and puts it at 6MHz
DispGraph
1→A
While getKey!=15
I+A→I
If I>40
0→I -A→A
If I>39                 ;this is because I will never be >40 unless negative
39→I -A→A
Shade(I
Pause 3
End
Shade(24
Stop
Title: Re: BatLib/ReCode Examples
Post by: Munchor on April 12, 2011, 04:05:38 pm
This is my version... I added a Pause because it was a little fast XD
Code: [Select]
dim(1,22,"FDCB00AE",40
FullFill(0→I                        clears the screen and puts it at 6MHz
DispGraph
1→A
While getKey!=15
I+A→I
If I>40
0→I -A→A
If I>39                 ;this is because I will never be >40 unless negative
39→I -A→A
Shade(I
Pause 3
End
Shade(24
Stop

Hum, isn't the calculator 6Mhz all the time?
Title: Re: BatLib/ReCode Examples
Post by: Xeda112358 on April 12, 2011, 04:08:29 pm
Not if you have one that can do 15MHz. ReCode starts out at the max speed :) Should I change that?
Title: Re: BatLib/ReCode Examples
Post by: Munchor on April 12, 2011, 04:09:43 pm
Not if you have one that can do 15MHz. ReCode starts out at the max speed :) Should I change that?

It does? I had no idea. I think you should, but just my opinion. Perhaps a poll?
Title: Re: BatLib/ReCode Examples
Post by: Xeda112358 on April 12, 2011, 04:10:55 pm
Yes, good idea! I will add a poll to BatLib Ideas!
Title: Re: BatLib/ReCode Examples
Post by: Munchor on April 13, 2011, 04:19:50 am
Yes, good idea! I will add a poll to BatLib Ideas!

I see you added it already, I could make some neat animations with it.
Title: Re: BatLib/ReCode Examples
Post by: Xeda112358 on April 20, 2011, 09:30:00 pm
Okay, so in this example, I wanted to show off how you can use BatLib to read lists. For example, say you have a game and you store the save files to lists. You can have the user input the name of the "save file" and you can use BatLib to read it :)

Code: [Select]
ClrHome
Input "FILE NAME:",Str1
dim(38,9,"L"+Str1             ;This is the little l for custom lists
Pause Ans
That code is very simple and takes advantage of the fact that if the list doesn't exist, ".BAD NAME" is output.
Title: Re: BatLib/ReCode Examples
Post by: Munchor on April 21, 2011, 11:17:54 am
This is looking great Zeda, I have to try lists too, thanks for the example.
Title: Re: BatLib/ReCode Examples
Post by: Xeda112358 on April 29, 2011, 05:02:57 pm
So, being a lover of patterns, I decided to remake a program that I originally made using xLIB. This one is a bit faster, the graphics are smoother, and doesn't require you to make a picture. So here is the basic concept:
-Copy upper 63 rows of the graphscreen
-XOR one pixel down
-Copy lower 63 pixels of the graphscreen
-XOR one pixel up

This will create some fairly interesting effects on its own, but I decided to add in  some pixel drawing for the first 95 iterations. After that, the pattern continues on its own seeming to scroll right, infinitely.
Code: [Select]
Delvar Bdim(56,4
Repeat Ans=15
If B<95
Then
Pxl-On(0,B
B+1→B
End
dim(53,dim(35,0,0,63,12),63,0,1,2,53,dim(35,0,1,63,12),63,0,0,2,43,99
End
Title: Re: BatLib/ReCode Examples
Post by: DJ Omnimaga on April 29, 2011, 07:50:27 pm
Wow that looks like ASM O.O
Title: Re: BatLib/ReCode Examples
Post by: Xeda112358 on April 29, 2011, 11:02:54 pm
Yeah, it is a little complicated to look at, but if you are coding it, it isn't bad. It is like trying to read another person's source x.x On the plus side, the smoothness and speed is pretty noticeable!