Re: Tcl/TK and C
- From: "Donal K. Fellows" <donal.k.fellows@xxxxxxxxxxxxxxxx>
- Date: Sat, 30 Sep 2006 23:19:00 GMT
Dave Dean wrote:
I've previously attempted to write a game-type application that consists
mostly of a bunch of pieces the user can move with a mouse.
I should mention here that I'm from a hardware background, and not super
experienced with PC applications.
Welcome to a *totally* different world!
My first attempt was purely in C++, using MFC with Visual Studio. I
managed to make something somewhat functional, but in the end the complexity
of the whole thing got the better of me, and I grew bored with the project.
Heh! I've coded games in C (and Pascal) and the experience does grow
very old very fast.
Recently, I've picked up Tcl/Tk, and it seems that the whole Tk toolkit is
an excellent way to reduce the complexity I was running into (the canvas
widget automates everything I struggled for weeks to figure out). On the
downside, I still want some of the extra power of C/C++, especially pointers
and more complicated data structures.
I'll let you into a (not very well-hidden) secret. Tcl has lots of
cunning data structures in the background. I've written some of them, so
I should definitely know! :-)
But the real point is that what happens very often when you write some
Tcl/Tk code is that you end up with some code that's using a pretty good
data structure for the purpose. Of course, that's not the whole story.
You *can* do much better in C, but it'll probably drive you a little bit
mad in the process; debugging a complex data structure (especially one
that is manipulated in many places in your program) is stupendously
difficult. By comparison, debugging a Tcl program by just opening up a
console on it and running a few commands interactively to poke inside
the code is *so* much easier.
I'm vaguely familiar with the process of extending Tcl/Tk by writing a
function in C, and calling it from Tcl. What I'd like is to be able to use
Tk only as a GUI, and retain all the data structures and computations behind
the scenes in C. Is there a way to hold all your data in C structures, and
share that data with the Tk GUI?
Yes (you might be interested in the Tcl C library routine Tcl_LinkVar
right now) but you should think very carefully about just how your data
is going to be structured throughout the whole program. It might be
better to write a specialized data structure in C that has some Tcl
commands to manipulate it. Like that you can debug that first before you
start wrapping complicated GUI code round it. (This is just a suggestion
but it saves you a lot of time and hair.)
One thing I had from Visual Studio was a project using the Document/View
architecture. If I could implement the View in Tk, but keep the Document in
C, I think I'd be on track.
That's definitely possible.
I know this is a very broad and somewhat vague question (reflecting my level
of experience). If anyone can point me in the right direction, either with
an answer or pointing me towards a particular website or book, I'd
The advice others gave you on using SWIG (an automated toolkit for
joining C code to scripting languages) is good. While it might not get
you all the way (computers aren't nearly as smart as people, after all)
it's a great way to get going.
However, remember what I said earlier. Chances are there's a suitable
data structure for much of what you want to do already. Can't say 100%
for sure though without much more detail.
- Tcl/TK and C
- From: Dave Dean
- Tcl/TK and C
- Prev by Date: Re: How do I clear the wish "workspace"
- Next by Date: Re: How does xotcl compare to ruby?
- Previous by thread: Re: Tcl/TK and C
- Next by thread: Re: Tcl/TK and C