Re: Tcl/TK and C



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
apprecitate it.

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.

Donal.
.



Relevant Pages

  • Re: after ids
    ... to do a better data structure for for almost ten years now, ... Tcl stands alone. ... fundamental discomfort about the unique dependency that Tcl shows on ... irrespective of the chosen timebase. ...
    (comp.lang.tcl)
  • About arraylists
    ... Everyone using Tcl should have in interest in it. ... arraylists is as an object to represent any typed structure in Tcl. ... As you can see this data structure can lead to a huge lot of stuff that has ... It provides a common framework for data type definition which allows one ...
    (comp.lang.tcl)
  • Re: Tcl event loop and threading
    ... model is fine, as long as the individual event handlers run only for a short time, which is very typical for GUI applications or network I/O. ... The multitasking needs not be realized by threads, however, you can run the solver in a separate process. ... For instance, you might have a GUI displaying a complex data structure, while at the same time lots of complex and time-consuming updates are being made to that structure. ...
    (comp.lang.tcl)
  • Tcl-URL! - weekly Tcl news and links (Mar 27)
    ... to make it easier, and functions which manipulate that data structure, so ... Recent convert Webb Sprague hopes to represent Tcl publically ... The Tcl Developer Site is Tcl's "home base". ... Cameron Laird tracks several Tcl/Tk references of interest (but ...
    (comp.lang.tcl)
  • Re: function and structures
    ... lines,, i want to implement the same in TcL. ... I recall your original problem showed a data structure with character ... Some specially-formated strings can be treated specially. ... but that is more like a series of bullet lists that an experienced ...
    (comp.lang.tcl)