Programming in C, C++ for Mac and Windows?
-
- My understanding is that c is a purely functional language whereas c++ is pretty similar to c but with some object oriented stuff added on.
-
+1 For sticking just with C. (BTW C is a procedural language, a functional language is something else entirely)
Ruby is itself written in C so even if you choose to use C++, you need to do C calls into Ruby.
Start with TBD example code. It deliberately starts with just doing some simple math to get your confidence.
The key thing for dealing with Ruby in SU, is that Ruby has been bolted-in (in a good way) to the underlying C++ SU application. This means in your Ruby extension you can call all of the Ruby functions you normally have.
eg
VALUE model = rb_funcall(sketchup, rb_intern("active_model"), 0);
will set model to a reference to the active model. You could then do:
rb_funcall( model, rb_intern("entities"), 0)
to get the entities of the active model.
Because Ruby automatically cleans up unused objects, you ask for the active_model object, use it for a bit and then move on, knowing Ruby will "Garbage Collect" the memory used for that by automatically working out when you're not using it anymore.
<gross simplification>You therefore cannot hold onto Ruby references beyond the scope of your function</gross simplification>
Notwithstanding Todd's comments in the other thread, Ruby Garbage Collection will remove objects from underneath you if you keep references around. There are ways around this but it makes things complex for anything but very simple Extensions.
I'd suggest you take an educated guess as to where the bottlenecks in your plugin are, and then write a single C function that does the heavy lifting just for that. But be aware that, walking every face in a model in Ruby and doing the same in C which is calling Ruby, is going to be the same speed. You need to identify areas that can be cast into pure C, do lots of calcs, then turn the answer back into a Ruby object.
Adam
-
@unknownuser said:
start with C, it is easier to debug ruby libraries problems
C is a small, fast language originally designed as a 3rd-generation substitute for assembly language. Your new minicomputer? You write a subset of C in assembler. You write C in the subset. You start compiling C programs. Pretty quick you have an OS, compilers, editors, etc. C ruled in the '80s.
Unfortunately, memory management is up to the programmer. If you want an array, you alloc() space for it. When you are done with the array, you free() its space. Huge pain in the posterior and big source of errors.
C++ added two major improvements: OOP and automated memory management. You want an array? You declare it. It goes out of scope? The runtime frees its space. C++ ruled in the 90s.
Either is a step backward from Ruby. I've a more complete language geneology here. I'd not recommend either unless you have a desperate need for speed. Try a nice, multi-paradigm language like JavaScript and do some functional programming and prototypal inheritance.
-
@martinrinehart said:
Either is a step backward from Ruby. I've a more complete language geneology here. I'd not recommend either unless you have a desperate need for speed. Try a nice, multi-paradigm language like JavaScript and do some functional programming and prototypal inheritance.
Can you really compare Ruby/Javascript vs C++/C? Compiled programming languages vs Scripting languages? Always seen that as apples and oranges - each its use.
-
Thank you guys for those responses, very informative. C++ soundy cooler, but perhaps I'll stick with C for now. I hope if I ever (not likely) need to move on to C++ it will be fairly easy to learn the differences.
Well this does give me some good insight into what I might try to accomplish first with this. I'll start with your code you gave TBD. Thanks for the link to it. I think I'll install pelles and the microsoft visual studio lite so I can see what the difference is, what they offer. I get confused by how the same language can be non-compatible with different compilers. I guess each compiler sort of expects its own dialect of the C language?
And thanks for the hints Adam on what to process. I have a script where I do a lot of math in ruby to process large arrays of points3ds and determine new coordinates for them. Perhaps I'll try to rewrite that in C. I guess I would send C an array of xyz coordinates, let C do all the math and then send back a new array of coordinates. Something like that. I don't know if it will speed anything up, but I think it will be something I try.
Thanks again everyone,
Chris
-
What about Java? Is that a viable alternative here? Does it provide any advantages over the C class languages?
Chris
-
@chris fullmer said:
What about Java? Is that a viable alternative here? Does it provide any advantages over the C class languages?
Chris
Java is a viable alternative. I've written some Ruby web dialogs that are expanded by my Java UI, and I place calls back and forth to Javascript with it (which in turn can be called from Ruby, so, Ruby -> Javascript -> Java -> Javascript -> Ruby). Javascript and Java, in an Applet, integrate well.
The nice thing about Java is the cross platform-ness of it, as well as the robust UI it has built-in.
-
@chris fullmer said:
And thanks for the hints Adam on what to process. I have a script where I do a lot of math in ruby to process large arrays of points3ds and determine new coordinates for them. Perhaps I'll try to rewrite that in C. I guess I would send C an array of xyz coordinates, let C do all the math and then send back a new array of coordinates. Something like that. I don't know if it will speed anything up, but I think it will be something I try.
Have you investigated where most the time of your script is spent?
I've found that in most my scripts, the majority of the time is spent with the creation of geoemtry, and not actually the math being calculated.
And I also found great speed improvement using Hash lookup instead of arrays in some cases. All though this depend from case to case.
UsingEntities.fill_from_mesh
is the fastest method I've found to add geometry to a scene. Saved many seconds vs anything else.Just a suggestion - that if you're looking to find ways to improve speed in your scripts, C might not be a magic bullet. Optimizing the methods you use in Ruby might be good enough.
-
Most of what I would add has been said already.
I'm a proponent of C++. For me, it's a much more natural programming structure than C (being object oriented), but you should probably pick what you are more comfortable with. If you use a lot of modules and classes in Ruby, I think you'll find the transition to C++ much more logical, with namespaces, classes, etc.Visual Studio Express 2008 is great for windows machines. I've had years of experience with VS so I can't really say how easy it is to just jump into, but I think it's as easy as most other IDE or compile environments.
As far as cross-platform, if you are planning on any kind of GUI, I strongly suggest a good cross-platform framework, like wxWidgets. Not only does it add tons of GUI support but there is a lot of stuff you may not think about being cross-platform, file io, networking, images, etc.
But even with a cross-platform framework, switching from Win to Mac is rarely straightforward. Be prepared to make some specialization.Last, don't underestimate the garbage-collection issue. If you have a single C/C++ method in which you make many ruby calls:
void my_method(VALUE something) { VALUE val_a = rb_funcall(something, rb_intern("blah_blah"), 0); VALUE val_b = rb_funcall(something, rb_intern("blah_blah2"), 0); VALUE val_c = rb_funcall(something, rb_intern("blah_blah3"), 0);
At any point, ruby can "collect" and delete the ruby objects, regardless of whether you are still in your C method. Ruby doesn't know that you are still in the function you were in when you asked for the object. So if these are transforms, and you go to multiply val_c by val_a, you may very well find that val_a has been deleted before you even get to val_c. I suggest understanding how Ruby does it's mark-and-sweep garbage collection and work with it.
-
FWIW I've used in anger most languages over the years and my problem with C++ has always been it gives you a ton of opportunities to really screw yourself over. I've seen it happen with inexperienced programmers countless times, so to that degree I'd suggest that sticking with C and keeping it simple is sound advice.
@chris_at_twilight said:
Last, don't underestimate the garbage-collection issue. If you have a single C/C++ method in which you make many ruby calls:
+10000
All the Ruby docs claim that the GC searches down your stack frame and ensures that anything referencing a Ruby VALUE will not be garbage collected. But my experience is that is simply not true.
What is true, is that as long as you don't call Ruby functions, it cannot run its Garbage Collector. So grab your info from Ruby, turn it into C data and then process without any calls to Ruby and you'll be good. The moment you call Ruby, you run (as said above) the possibility of the GC running (I believe it actually gets run every 256 internal opcodes Ruby executes - scary huh!).
Adam
-
it all depends on how much time and brain power do you want to allocate to it
for myself I always love that my first language learned was assembler (Z80 to be more exact), so I can easily understand what happens inside the CPU - be that x86 (Win and Mac) or ARM (iphone) and can debug at the lowest level.
another advantage in learning C is that you can look inside Ruby source code and see where the problem in your code comes from.
everyone has a favorite language and each one comes with pros and cons.
in the end it is your decision anyway - so take a look on all and see what fits you better. if you don't love it at first sight it will be harder in the future. and don't waste time choosing one, just start experimenting.
-
I think this thread may create the impression that the C variant languages are the only serious options if you wish to learn a mainstream programming language to do 3D/graphics work. C/C++ is the AutoCAD of the programming world. There are simpler entry points for those who wish to take a step up from just using a scripting API. If you wish to spend a substantial time learning about using a violin instead of getting to a point where you actually create music with the tool, then by all means : go off and learn C/C++.
But unfortunately as with Sketchup and the die-hard AutoCAD users in the early days, you may have to bear the brunt of half-researched abuse from the C-club if you go for something modern and simple like C#,- or dare I even say it: VB.NET.
-
@toxicvoxel said:
if you go for something modern and simple like C#,- or dare I even say it: VB.NET.
if you want to do simple things go with "simple" languages but after running the simple examples you will have problems that need knowledge of the things that run under the hood.
-
@toxicvoxel said:
... if you go for something modern and simple like C#, ...
Simple? Nothing about .NET is simple. C# is an argument for bariatric language surgery.
When MSFT created Windows, it deliberately pessimized the programming interface. You could tell when you tried to use it that a) the people in Redmond were idiots (you knew this was not the case), or b) the people in Redmond wanted to make it as hard as possible to create Windows programs. (Subsequent MSFT documents, dug out at trial, confirmed that the answer was b).)
MSFT did not change with .NET. It is simply a way to get anyone who uses it locked into a Windows-only future. Use C# if you want a language more bloated than Ada and you want your code to never run on a Mac and ...
Oh dear. Gotta do something to lower my heart rate.
-
@unknownuser said:
and you want your code to never run on a Mac and ...
- An good example of a half-researched comment. Ever heard of the Mono Project? http://www.mono-project.com/Main_Page
@unknownuser said:
If you want to do simple things go with "simple" languages but after running the simple examples you will have problems that need knowledge of the things that run under the hood.
TBD, that comment reminds me of the silly remarks you made a while back on the old @Last forum relating to the Gelato rendering system until the NVIDIA development team put you in your place. These languages have been used on projects of far greater complexity than anything you will ever code in C.
If you like C/C++ then fine, but don't mislead the youngsters who are contemplating first steps into choosing a programming language. I have personally used .NET for more than 10 years on a range of design automation projects, some for one of the largest engineering firms in the world, and I do not recognise any of the characteristics either of you assign to these languages in your posts.
-
I realise this is all set for a flame war..
The thread is about producing Extensions for Ruby (which is written in C). So TBD comment about keeping it simple and using C/C++ is pretty reasonable.
Its all well saying Mono, but the reality is that on a Mac, Xcode has great support for C/C++ and not Mono/C#.
-
toxic: create a simple .NET ruby extension example that works inside Sketchup or quit babbling around
-
a ruby extension - create a new ruby class with one simple method in .NET. compile, copy .so in SU Plugins and ready to use in .rb code
-
TBD, How is this relevant in the context of the thread and specifically my post, - unless you wish to narrow the scope of the discussion in order to make your point?
I can remember an AutoCAD guy in the office way back in SU V3 insisting that I show him how I was going to do a boolean intersection with SU. Like him you are missing the point and like him I don't think you will ever get the point.
-
Sorry to get everyone so riled up around here. Lets not be bashful, dig out those old skeletons from the cloest.....oh wait, we already are!
The point of this thread is in fact SU based, and about how to write extensions for SU ruby. Or how to interface into SU from another program, or dll, etc. I really am not sure what my question is because I just don't know what is possible, and I don't have a specific task in mind that I am striving to find a solution for (yet!). I just want to broaden my horizons and I want to do it with a compiled language.
But I will say that I have installed a few compilers and such and I am reading up on C++ currently. But it gives a bit of background into C, C# and Java. Hopefully I'll be able to wrap my head around it.
Chris
Advertisement