Page 1 of 1

PostPosted: Sat Feb 18, 2006 3:01 pm
by MN OrbUser
I am having some trouble understanding the draw object paradigm. I am writing a program to draw a fractal.

This involves a recursive function (i.e., it calls itself) which starts at say Level = 3; on successive calls reduces the Level by 1; when Level = 0 it calls another function update() which draws line segment (or should if I get it to work) and then starts over again to get ready to draw the next line segment.

The program uses two popup lists, to let the user set some parameters, and a "Go" button to start the recursive calculation.

I have created a Draw object named Fractal (in program file Fractal3D.oc) and attached it to mainForm in mainForm.onopen() (in program file mainform.oc). In update() I have the following code:

Fractal.line(clrFG, xa, ya, xb, yb); // try to draw the line

It compiles, but when I launch the app and tap the Go button I get the following error message:

"Func:update Attempt to draw without calling 'begin/nbegin'".

If I comment out the Fractal.line() code it runs. Also, if I put in a counter to show that the recursion is working and put the text to the screen, it increments - which shows the other code is working.

Any thoughts on where I should be putting the draw statement?

PostPosted: Sat Feb 18, 2006 3:52 pm
by tthkbw
Well, don't ask me why, but to fix this, you must use the following:


Your code doesn't have the ()'s after "begin" and "end". I tried removing these in my code, and although OrbForms doesn't give a compile error, nothing draws.

Seems like a compile error should occur.

Terry Brown

PostPosted: Sat Feb 18, 2006 9:41 pm
by nmc
"Fractal.begin" is a pointer to that function, while "Fractal.begin()" is a call to that function - at least this is true in C (I actually dont know if orbforms lets you get the pointer to a build in function)

PostPosted: Sun Feb 19, 2006 11:04 am
by MN OrbUser
Thanks, I used tthkbw's suggestion and it worked. I also appreciate nmc's insight as to the distinction between Fractal.begin and Fractal.begin().

I had tried a number of things in trying to get the draw to work, and then got sloppy when I saw that the compiler didn't bark when I left the () off.

I know why you want to call a function, but what would be a useful reason to use a pointer to a function? What does a pointer to a function do?

PostPosted: Sun Feb 19, 2006 8:17 pm
by nmc
> but what would be a useful reason to use a
> pointer to a function? What does a pointer
> to a function do?
Think about changing the behavior of yout app by exchanging a helper function;
instead of using "if (option) doThis(); else doThat();" a hundert time, you could use "(*doIt)() at those places and assign "doIt = doThis;" once.
Function pointers are also very helpfull when needing callback functions; think about implementing a sort function for any type of structs - you will need a comparison function for this (a different one for every struct).

PostPosted: Tue Feb 21, 2006 3:35 am
by dewey
To answer nmc's question, yes you can take the address of a builtin function. The compiler generates a wrapper function which calls the builtin, and returns a pointer to the wrapper. The same mechanism allows you to create native objects with virtual methods.