Help with Centering Function

Posted By: Geoff

Help with Centering Function - 08/11/08 19:54

Something is wrong with this function and I'm not seeing it.

Input please.

function center_ text(txt)
{
var cen; var len;
len = str_len(txt);
cen = (screenwidth - len) /2) +1;

return(cen);

}

screenwidth is declared as 480. I've also tried hard-coding this value.

thx.

- Geoff
Posted By: hack-panther

Re: Help with Centering Function - 08/11/08 20:29

str_len returns the number of letters, not the size in pixels...
Posted By: Geoff

Re: Help with Centering Function - 08/12/08 14:24

Ok, so the math would be off by a multipule of 8 or something, but it would still return a value.

The function actually crashes.
Posted By: Petra

Re: Help with Centering Function - 08/12/08 14:30

I see a missing bracket in the "cen" line, a missing type of the txt argument (if no type, it defaults to var)... I doubt that this can even compile.
Posted By: Geoff

Re: Help with Centering Function - 08/12/08 14:47

ok, missed the opening appostophy, but I leaning towards the "txt" not having a type as to why it crashes.

i've tried: center_text(var txt) //does not work
center_text(STRING* txt) // does not work

What am I missing?
Posted By: Petra

Re: Help with Centering Function - 08/12/08 14:52

How are you calling that function?
Posted By: Geoff

Re: Help with Centering Function - 08/12/08 14:57

First, I found the problem with the above (stupid stupid stupid)...I had a space between center_ & text so it was not center_text...blah

however, I'm still tring to figure this out and am calling the function from:

TEXT* credits_txt =
{
layer = 15;
pos_x = center_text(credits_str);
pos_y = 10;
string (credits_str);
flags = VISIBLE;
}

Which now errors out at the call.

credits_str was declared like:

STRING* credits_str = "Copyright (c) 2005\nGeoff Doty\n";
Posted By: Petra

Re: Help with Centering Function - 08/12/08 15:01

You should have posted that first, then the problem had been obvious.

The first thing you learn in the tutorial is, all commands must be inside a function. TEXT* is not a function, its a text definition.

You need a main() function and in that function, among other things you can call your center_text:

credits_txt.pos_x = center_text(credits_str);

I recommend the lite-c tutorial, there you learn how to write programs.


Posted By: Geoff

Re: Help with Centering Function - 08/12/08 15:14

I did not think lite-c was object orientated.

I was going off of the the tutorial(workshop) # 7, which was listed like this:

////////////////////////////////////////////////////////////////////
#include <acknex.h>
#include <default.c>
////////////////////////////////////////////////////////////////////
STRING* aloha_str = "Welcome to Paradise Island!";

TEXT* greetings_txt =
{
pos_x = 300;
pos_y = 250;
string (aloha_str);
flags = VISIBLE;
}
////////////////////////////////////////////////////////////////////
function main()
{
video_mode = 7;
screen_color.blue = 150;
}

and wanted to center the aloha_str.

I dont know the restrictions of "pointers", as i'm a run-time programer - vb, php, tcl...ect...

So, I guess the to-self question is that "ALL *pointers* act like structures where I can modify/assign values outside the structure?"
© 2024 lite-C Forums