Support

If you have a problem or need to report a bug please email : support@dsprobotics.com

There are 3 sections to this support area:

DOWNLOADS: access to product manuals, support files and drivers

HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects

USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here

NEW REGISTRATIONS - please contact us if you wish to register on the forum

Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright

ruby - draw rectangles from array - possible?

For general discussion related FlowStone

ruby - draw rectangles from array - possible?

Postby Nubeat7 » Thu Dec 13, 2012 11:15 pm

hallo, when i want to draw different sized rectangles side by side like in the step lfo, taking the coordinates from an array, it is no prob to write this down
Code: Select all
v.drawRectangle b,array[0]
- on every index i have the individual coordinates for the rectangles, this way i could draw every single rect from the array, the array has 16 different coordinates, but i only need 0..11 and only this 12 rect should be drawn, is it possible to iterate through the array from 0...11 or 0..3 .... and draw only the rectangles from the index i choose?

i was thinking to do a drawRectangle loop wich iterates +1 on the coord array on every loop....
User avatar
Nubeat7
 
Posts: 1347
Joined: Sat Apr 14, 2012 9:59 am
Location: Vienna

Re: ruby - draw rectangles from array - possible?

Postby trogluddite » Fri Dec 14, 2012 12:39 am

You can iterate a range like that in several ways. the easiest is probably to use the built in Range data type...
You define a range by putting two values (number or variables) between curved brackets, separated by two dots...
range = (start..finish)
...or three dots...
range = (start...finish)
Three dots misses out the last number so is the same as (start..(finish-1)).
Then you can either use the 'for' type loop, or an '.each' loop - they are just different syntax for the same thing...
Code: Select all
for index in (start..finish) do
   #do some stuff using 'index' as the pointer variable
end

#OR

(start..finish).each do |index|
  #so some stuff using 'index' as the pointer variable
end

In this case 'start' and 'finish' must be integers, and it always steps by one each time.
The 'for' method is probably a little more like other languages, but it's worth getting used to the '.each' method - it is more versatile, as when you want to iterate over a whole array, you often don't even need an index pointer...
Code: Select all
array.each do |item|
  #do stuff here, 'item' will be each thing in the array in turn
end

Note that when using '.each' the passed value inside the pipe characters '|value|' must be on the same line as the .each statement.
You can also use curly brackets instead of 'do' and 'end' to mark the extent of the loop...
Code: Select all
(0..10).each {|index| #use index as a pointer }
array.each {|item| #do things to 'item' }

In general, these methods are called "iterators", and the 'collections of things' are described as "enumerable" - both terms well worth searching for in any of the on-line Ruby resources out there; it is one of Ruby's most powerful features.
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
User avatar
trogluddite
 
Posts: 1730
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: ruby - draw rectangles from array - possible?

Postby Nubeat7 » Fri Dec 14, 2012 2:34 am

thanks, trog but i cant get it how to write a drawRectangle loop, i uploaded the steplfo which i`m working on to show the problem, i saw that at the original steplfo with the drawloop primitive and the getfloatarray is doing a lot of triggering , so i thought this could be better with ruby but like it is now there are already round 7% cpu on my crappy laptop with the ruby version and the same with the original so i`m notsure if it makes sense to do it in ruby..... i`m working on a bigger project with lots of steplfos and when they are showed on display as vst the difference is about 20% !! of cpu use, i thought all the triggering what is happening is the reason for this... so i deicided to make it in ruby, but i`m lost...
Last edited by Nubeat7 on Sun Aug 02, 2015 2:36 pm, edited 1 time in total.
User avatar
Nubeat7
 
Posts: 1347
Joined: Sat Apr 14, 2012 9:59 am
Location: Vienna

Re: ruby - draw rectangles from array - possible?

Postby Nubeat7 » Fri Dec 14, 2012 1:12 pm

ok i found out now, that there happening strange things with the triggers in steplfo, pleas watch this FSM, there are two steplfos i put triggercounters after the getfloat prims and one triggers only when i do some changes in the steplfo but the other one triggers the whole time when it is showed on display - why does it do that??
Last edited by Nubeat7 on Sun Aug 02, 2015 2:36 pm, edited 1 time in total.
User avatar
Nubeat7
 
Posts: 1347
Joined: Sat Apr 14, 2012 9:59 am
Location: Vienna

Re: ruby - draw rectangles from array - possible?

Postby MyCo » Fri Dec 14, 2012 3:47 pm

That what you see is a redraw loopback. The trigger counter is connected to a trigger signal, that is generated whenever a redraw happens (coming from a "Draw Loop" primitive). Now when a redraw happens, there'll be a trigger. This trigger increments the redraw counter, and this draws it's value on the screen, by forcing a redraw. Now when this redraw is sent out, all visible items that are near or overlapping the counter area are redrawn, too. So this is causing a redraw event in the step LFO, which then causes another trigger in the counter. This goes on and on... like self oscillation :twisted:
User avatar
MyCo
 
Posts: 718
Joined: Tue Jul 13, 2010 12:33 pm
Location: Germany

Re: ruby - draw rectangles from array - possible?

Postby trogluddite » Fri Dec 14, 2012 5:53 pm

Nice explanation, MyCo.

Regarding 'Green' vs. Ruby.
It's unlikely that you will see a huge difference for many designs - the biggest user of the CPU is often not the 'creating' of GUI shapes; it is the refreshing of the display (i.e. putting the shapes up where you can see them).
Using Ruby might make drawing particularly complex arrangements somewhat more efficient than 'green' - but if you are refreshing the display more often than needed, that will easily wipe out any advantages that it might have.

And as MyCo points out, when you re-draw, it is not just the particular module with a redraw trigger that gets re-calculated - so do all other layers that overlap with it. (for a good example see this thread, where a relatively small meter makes a very big background bitmap get resized and redrawn over and over again).

So the key to keeping redraw CPU low is to only redraw when something has actually changed, and even then, to limit how many redraws can happen in a short space of time. I posted an example HERE of a bit of Ruby for doing this kind of thing - though the module as it is may not be suitable for every occasion, as the redraws might be being triggered by an overlapping module, or from within a piece of Ruby code.

Has to be said, this is one of the trickiest things in FS/SM to optimise - harder than code or assembly in some ways, as there is much 'hidden machinery' at work. And now that there is Ruby too, it will take a little while for us all to learn the best way to get good results.
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
User avatar
trogluddite
 
Posts: 1730
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: ruby - draw rectangles from array - possible?

Postby Nubeat7 » Fri Dec 14, 2012 7:28 pm

oh, i didn`t know that with overlapping areas, i`m gonna change this in my project and see how it is working than... thank you guys! this helped me a lot
User avatar
Nubeat7
 
Posts: 1347
Joined: Sat Apr 14, 2012 9:59 am
Location: Vienna


Return to General

Who is online

Users browsing this forum: No registered users and 27 guests

cron