Author Topic: Running Line Commands from a String  (Read 3431 times)

0 Members and 1 Guest are viewing this topic.

Offline Blue72

  • LV2 Member (Next: 40)
  • **
  • Posts: 20
  • Rating: +3/-0
    • View Profile
Running Line Commands from a String
« on: September 18, 2011, 10:02:15 am »
I'm writing a BASIC program that does lots of sprite drawing with lines, and I was hoping to optimize it by putting the Line( commands for each sprite into a string, and then simply using expr(String wherever the sprite was needed. Unfortunately, the expr( command seems to not work with commands that don't return answers. It would be very useful if there were another way (Or if someone had a better idea) to achieve the desired result. (I would prefer to stick with using the Line( commands)

Thanks!  ;D

EDIT: Here's a more visual explanation:

This bulky thing repeated multiple times:
Code: [Select]
Line(6X,6Y,6X,6Y+5
Line(6X,6Y+5,6X+5,6Y+5
Line(6X+5,6Y+5,6X+5,6Y
Line(6X+5,6Y,6X,6Y

Versus

At the beginning:
Code: [Select]
"Line(6X,6Y,6X,6Y+5):Line(6X,6Y+5,6X+5,6Y+5):Line(6X+5,6Y+5,6X+5,6Y):Line(6X+5,6Y,6X,6Y"->Str1And then later in the program:
Code: [Select]
expr(Str1
« Last Edit: September 18, 2011, 10:11:02 am by Blue72 »

Offline Hot_Dog

  • CoT Emeritus
  • LV12 Extreme Poster (Next: 5000)
  • *
  • Posts: 3006
  • Rating: +445/-10
    • View Profile
Re: Running Line Commands from a String
« Reply #1 on: September 18, 2011, 10:41:59 am »
I can't think of a way to do just that, but can you explain how that would help optimize your program?  Maybe it's because I'm a noob at Ti-Basic :), but the way I'm looking at it, you add at least 7 bytes (maybe more) to your program, and expr( itself takes time

Offline Blue72

  • LV2 Member (Next: 40)
  • **
  • Posts: 20
  • Rating: +3/-0
    • View Profile
Re: Running Line Commands from a String
« Reply #2 on: September 18, 2011, 10:51:40 am »
Well, there are multiple places in the program where each sprite is drawn, and rather than have subprograms or repeat those large blocks, I'd like to have something short and clean.

These drawing events are also mostly nested in conditionals, and I imagine there would be some speed benefit from the program not having to run over large blocks of code it isn't using at the moment.

In general, it would make the main loop of the game much cleaner and somewhat faster, as well as some size reduction by shrinking repetitive code.
« Last Edit: September 18, 2011, 10:53:37 am by Blue72 »

Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: Running Line Commands from a String
« Reply #3 on: September 18, 2011, 10:56:38 am »
If you have to use it more than four times or so, you could try doing it like this:

While 0
Lbl L
Line(6X,6Y,6X,6Y+5
Line(6X,6Y+5,6X+5,6Y+5
Line(6X+5,6Y+5,6X+5,6Y
Line(6X+5,6Y,6X,6Y
End

<program start>

Then, whenever you need to draw those lines you just do this:
For(A,0,1) // parentheses for speed
If not(A
Goto L
End


It is by no means the most elegant solution, but it would still work.  You could also try using subroutines, but this would add an additional program to your project. :-\
« Last Edit: September 18, 2011, 10:57:30 am by ztrumpet »

Offline Blue72

  • LV2 Member (Next: 40)
  • **
  • Posts: 20
  • Rating: +3/-0
    • View Profile
Re: Running Line Commands from a String
« Reply #4 on: September 18, 2011, 10:59:13 am »
I thought about that, but I am a little reluctant to have it jumping around a lot, as that just contributes to the whole "time spent running over unused code" thing.  :P

I do appreciate the suggestion, however, and I may end up doing something similar.

It's quite frustrating that one of the few situations where expr( would be ideal is also one where it doesn't work.

Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: Running Line Commands from a String
« Reply #5 on: September 18, 2011, 11:18:30 am »
The best thing I can think of doing is something like this at the start:
"6X+5->u
"6Y+5->v


Then doing this when you need to display the lines:
Line(6X,6Y,6X,v
Line(6X,v,u,v
Line(u,v,u,6Y
Line(u,6Y,6X,6Y


Edit: I was fed up with my awful grammar, so I changed it. :P
« Last Edit: September 18, 2011, 11:25:02 am by ztrumpet »

Offline Blue72

  • LV2 Member (Next: 40)
  • **
  • Posts: 20
  • Rating: +3/-0
    • View Profile
Re: Running Line Commands from a String
« Reply #6 on: September 18, 2011, 11:26:45 am »
I like that, but I think it would be better to do it as:
Code: [Select]
6X->U
6Y->V
Line(U,V,U,V+5
Line(U,V+5,U+5,V+5
Line(U+5,V+5,U+5,V
Line(U+5,V,U,V

Since some of the sprites are more complicated, and require different amounts to be added and subtracted.

I still hope to find a way to only have to write the commands once, though.

Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: Running Line Commands from a String
« Reply #7 on: September 18, 2011, 11:31:36 am »
Almost.  You have to remember that "6X" and "u" take the same amount of space because "u" is a two byte token.  However, you can change the values of u and v whenever you want.  If you want four dynamic corners of your box, you could use r1 - r4, with one for each corner.  That could lead to some really interesting box manipulation. ^-^

Offline Blue72

  • LV2 Member (Next: 40)
  • **
  • Posts: 20
  • Rating: +3/-0
    • View Profile
Re: Running Line Commands from a String
« Reply #8 on: September 18, 2011, 11:36:35 am »
True, but I like to prioritize speed over size. Though 6X is a very simple calculation, having it performed only once could see a minor speedup.

As for the box corners, that seems like it would provide less speedup and trim little size.

EDIT:  :P That made it sound like I didn't appreciate its usefulness. It would be useful for manipulating boxes, but the sprites in my program are all the same size.
« Last Edit: September 18, 2011, 11:51:32 am by Blue72 »

Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: Running Line Commands from a String
« Reply #9 on: September 18, 2011, 04:20:26 pm »
Ah, I see that you misunderstood me.  Check this out:

(The u and v are from [2nd] [7] and [2nd] [8].)

Offline Blue72

  • LV2 Member (Next: 40)
  • **
  • Posts: 20
  • Rating: +3/-0
    • View Profile
Re: Running Line Commands from a String
« Reply #10 on: September 18, 2011, 05:21:48 pm »
Intriguing, so they're some kind of expression-storing variable?

(I am clearly a newbie)
« Last Edit: September 18, 2011, 05:24:11 pm by Blue72 »

Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: Running Line Commands from a String
« Reply #11 on: September 18, 2011, 05:27:11 pm »
Intriguing, so they're some kind of expression-storing variable?
Yup.  They're actually just like Y1 through Y0, only for Sequential mode instead of Function mode.  Any of the variables in [Vars] [Right] will do the exact same thing.  u, v, and w are normally used because they look the nicest, are the easiest to access, and users rarely have to use sequential mode.  (You can change the graphing mode by pressing Mode and scrolling down a few lines).
« Last Edit: September 18, 2011, 05:27:24 pm by ztrumpet »