|
|
structures
#258747
04/01/09 21:36
04/01/09 21:36
|
Joined: Jun 2006
Posts: 2,640 Earth
Germanunkol
OP
Expert
|
OP
Expert
Joined: Jun 2006
Posts: 2,640
Earth
|
typedef struct SCROLLABLELINE{
TEXT* contentsTxt;
struct SCROLLABLELINE* nextLine;
struct SCROLLABLELINE* prevLine;
} SCROLLABLELINE;
typedef struct {
int lines;
int width;
SCROLLABLELINE* firstLine;
} SCROLLABLETEXT;
function initText(SCROLLABLETEXT* curText, int numOfLines)
{
curText.lines = numOfLines;
curText.firstLine = line_create(...);//another way of doing this?
SCROLLABLELINE* curLine;
curLine = curText.firstLine;
var count = 0;
while(count < numOfLines)
{//create [numOfLines] Lines:
curLine.nextLine = line_create(...);
curLine.nextLine.prevLine = curLine;
curLine = curLine.nextLine;
count += 1;
}
}
I hate to ask for code, but I can't code this. I'm trying to get a SCROLLABLETEXT object to have a pointer to a SCROLLABLELINE object, which again has an object to the next SCROLLABLELINE object and so on: a linked list with the SCROLLABLETEXT as the "head". (should be linked in both directions, but that's not the problem.) I can't write that initialization function though. My problem: SCROLLABLELINE curLine[numOfLines]; won't work inside the function, and there's no: curLine.nextLine = scrollableLine_create(...); function. So how do I define a dynamic (not a set) amount of SCROLLABLELINES inside a function and link them together with pointers? Thankful for any help... I'm really lost on this, tried everything I could think of...
~"I never let school interfere with my education"~ -Mark Twain
|
|
|
Re: structures
[Re: Germanunkol]
#258787
04/02/09 05:43
04/02/09 05:43
|
Joined: Jun 2006
Posts: 2,640 Earth
Germanunkol
OP
Expert
|
OP
Expert
Joined: Jun 2006
Posts: 2,640
Earth
|
something like a "new" function in Java would be helpful. I think C++ has it to. But I can't find it in liteC.
~"I never let school interfere with my education"~ -Mark Twain
|
|
|
Re: structures
[Re: Germanunkol]
#258791
04/02/09 06:09
04/02/09 06:09
|
Joined: Mar 2009
Posts: 112 Germany
KDuke
Member
|
Member
Joined: Mar 2009
Posts: 112
Germany
|
Hi Germanunkol. I think malloc is what you are looking for... Furthermore if you want to change the size in runtime you have to use either realloc or you go get the GSVector Plugin . greetings K-Duke
Using A7 Free Click and join the 3dgs irc community! Room: #3dgs
|
|
|
Re: structures
[Re: KDuke]
#258808
04/02/09 09:16
04/02/09 09:16
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
I dont see a reason to init the header with empty data. Mines a bit more long-winded, but if its not good for what you want, you may be able to see chunks of my code you can use differently. (this code is untested, but Ive just finished writing a similar linked list for handling entities, so it will be close) NOTE: Ive left out most error checking for simplicitys sake.
typedef struct SCROLLABLELINE
{ TEXT* contentsTxt;
struct SCROLLABLELINE* nextLine;
struct SCROLLABLELINE* prevLine;
}SCROLLABLELINE;
//
//
typedef struct SCROLLABLETEXT
{ int lines;
int width;
SCROLLABLELINE* firstLine;
}SCROLLABLETEXT;
//
//
SCROLLABLETEXT* initText()
{
SCROLLABLETEXT* header = malloc(sizeof(SCROLLABLETEXT));
memset(header, 0, sizeof(SCROLLABLETEXT));
return(header);
}
//
//
SCROLLABLELINE* create_line(SCROLLABLETEXT* header, TEXT* contentsTxt);
{
SCROLLABLELINE* thisline = malloc(sizeof(SCROLLABLELINE));
thisline.contentsTxt = contentsTxt;
thisline.prevLine = thisline.nextLine = NULL;
//
if(header.firstline==NULL)
header.firstline = thisline; //list was empty.
else
{
SCROLLABLELINE* tmp_ptr = header.firstLine;
while(tmp_ptr!=NULL) //find last entry in list
{
if(tmp_ptr.nextLine==NULL) break; //found end of list
tmp_ptr = tmp_ptr.nextLine;
}
tmp_ptr.nextLine = thisline; //add to end of list and set
thisline.prevLine = tmp_ptr; // link pointers both ways
}
header.lines++;
//header.width = (whatever you need to do to get this value);
return(thisline);
}
//
//
void delete_line(SCROLLABLETEXT* header, SCROLLABLELINE* thisline);
{
if(header.firstLine==thisline)
{ //first in list
header.firstLine = thisline.next);
thisline.nextLine->prevLine = NULL;
header.lines--:
//header.width = (any adjustments necessary);
free(thisline);
return;
}
//
thisline.nextLine->prevLine = thisline.prevLine;
thisline.prevLine->nextLine = thisline.nextLine;
}
//
//////////////// EXAMPLE USAGE...
//
SCROLLABLETEXT* TextData;
//
function eg-main()
{
TextData = initText();
...
TEXT* ThisLineText = txt_create(blah);
SCROLLABLETEXT* New_Line0 = create_line(TextData, LineText);
...or
SCROLLABLETEXT* New_Line0 = create_line(TextData, txt_create(blah));
...
}
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: structures
[Re: EvilSOB]
#258907
04/02/09 19:29
04/02/09 19:29
|
Joined: Jun 2006
Posts: 2,640 Earth
Germanunkol
OP
Expert
|
OP
Expert
Joined: Jun 2006
Posts: 2,640
Earth
|
Wow, thanks both of you. I had started with malloc after reading the first post (WOW... again, as so often I ask: WHY ISN'T THIS IN THE MANUAL?!) and it's working much better now. but this second post clears up a lot as well.
EvilSob, the .width is used for a spify line break function.
Just one question: do you use the memset to initialize it all to 0? I looked it up on the net, because, again, it's NOT in the manual. memset's mentioned 3 times, once in "bugs", once in "structs" (just in a define with pretty much zero(ptr) explanation) and once under "define", with just as much explanation... I'm starting to really dislike the manual. How am I to know if I'm not a c++ coder that this is a c++ function that works just like in c++? ... -.-
I'll let you know how I do, kinda tired today, but I'll see how far I get. Thanks a lot to both of you!!
Last edited by Germanunkol; 04/02/09 19:29.
~"I never let school interfere with my education"~ -Mark Twain
|
|
|
Re: structures
[Re: Germanunkol]
#258914
04/02/09 20:08
04/02/09 20:08
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
The manual does have a few holes in it, but its as good as can be expected. You cant really expect the manual to teach usage of uncommon or advanced commands like malloc and memset (that were inherited from C), when there is oodles of doco on the net regarding their usage.
usage :: memset(void*, value, size) :: where void* - is the pointer to the memory location to start setting the memory value - the value to set all the memory locations to. (range 0-255). size - the number of bytes to change, starting at 0. Note: only ever use a value of 0 with memset, otherwise strange values will occur, especially with floats and vars. Note2: My memset example does EXACTLY the same as the zero(ptr) macro. But Ive been dabbling with C since 1990, so the zero macro is too new to be habit yet.
You wanna see how much isnt in the manual? Go to the includes folder of gstudio and open the windows.h file. All that is "missing" from the manual too. I understand Conitec's position on that score.
So if you find something in lite-c code that isnt in the manual, assume it is ansi-C, C+, or C++, and google it. You'll be correct 98% of the time.
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: structures
[Re: EvilSOB]
#258999
04/03/09 12:00
04/03/09 12:00
|
Joined: Jun 2006
Posts: 2,640 Earth
Germanunkol
OP
Expert
|
OP
Expert
Joined: Jun 2006
Posts: 2,640
Earth
|
It's working!  thanks, EvilSOB, I used your code as a base and went from there, works like a charm  . I did need to initialize all the lines of the SCROLLABLETEXT straight away, because I want to be able to scroll up and down in that text, meaning that I need the first and the last line already initialized. So I rewrote parts of it and used other parts. I'm sorry for blaming the manual. You're right, they do have a point. But I think that when they mention it they could at least say "this is a c++ function, google it." One last question remains: void scrollText(SCROLLABLETEXT* curText, int* newLineColor) { ... } int* theColor = {0,42,42} scrollText(consoleText,theColor); doesn't work. But: void scrollText(SCROLLABLETEXT* curText, var* newLineColor) { ... } int* theColor = {0,42,42} scrollText(consoleText,theColor); works. Why does the vector have to be of type var and can't be of type int? Thanks...
Last edited by Germanunkol; 04/03/09 12:02.
~"I never let school interfere with my education"~ -Mark Twain
|
|
|
Re: structures
[Re: Germanunkol]
#259261
04/05/09 05:40
04/05/09 05:40
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
Dont be sorry, everyone opinion is important, and saying "google it" would be nice.
Because the type VECTOR is defined in 3DGS as var[3] OR float[3]. (Ive never used the float version though) Having said that, I would use the COLOR* type in your case rather than playing with var pointers. eg void scrollText(SCROLLABLETEXT* curText, COLOR* newLineColor)
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: structures
[Re: EvilSOB]
#259326
04/05/09 14:02
04/05/09 14:02
|
Joined: Jun 2006
Posts: 2,640 Earth
Germanunkol
OP
Expert
|
OP
Expert
Joined: Jun 2006
Posts: 2,640
Earth
|
Hm... but I never used the type VECTOR? I defined the color as int* theColor =... ? Oh well, I'll use COLOR, thanks 
~"I never let school interfere with my education"~ -Mark Twain
|
|
|
|