# PureBasic Forum

 It is currently Fri May 24, 2013 9:59 pm

 All times are UTC + 1 hour

 Page 1 of 1 [ 10 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Help converting a function from bmax to pb?Posted: Sat Mar 10, 2012 8:51 pm
 Enthusiast

Joined: Wed Jul 12, 2006 4:38 pm
Posts: 384
Location: UK
I don't understand bmax syntax and i need a function converted so that i can use the leadwerks engine in pb.
The function converts a string to a vector. Can someone at least help convert part of it, i may be able to figure out the rest.

Code:
Function StringToVec3:TVec3(text:String, scale:Float = 1.0)
Local t:TVec3 = Vec3(1)
Local sarr:String[]
sarr = text.split(",")
If sarr
If sarr.length > 0 t.x = Float(sarr[0]) * scale
If sarr.length > 1 t.y = Float(sarr[1]) * scale
If sarr.length > 2 t.z = Float(sarr[2]) * scale
EndIf
Return t
EndFunction

Top

 Post subject: Re: Help converting a function from bmax to pb?Posted: Sat Mar 10, 2012 9:11 pm

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 1386
Location: Boston, MA
They are passing numbers in comma separated strings --> "1.1,2.2,3.3"
Use the necessary PB functions( StringField(), len() ) to extract them.
You also have to return a pointer to the vector structure, not the structure.

_________________
To understand recursion, you must first understand recursion. ~ unknown
I never make stupid mistakes. Only very, very clever ones. ~ John Peel

Top

 Post subject: Re: Help converting a function from bmax to pb?Posted: Sat Mar 10, 2012 9:23 pm
 Enthusiast

Joined: Wed Jul 12, 2006 4:38 pm
Posts: 384
Location: UK
Thanks, the problem is, the string that needs to be converted is Ä. Does this mean the command returning the string is not working?

Top

 Post subject: Re: Help converting a function from bmax to pb?Posted: Sat Mar 10, 2012 9:25 pm
 Enthusiast

Joined: Wed Jul 12, 2006 4:38 pm
Posts: 384
Location: UK
This is the structure in the leadwerks include file
Code:
Structure TVec3
X.f
Y.f
Z.f
EndStructure

this is the procedure in the include file
Code:
Procedure Vec3(*Result.Tvec3, px.f = 0, py.f = #MAX_DEC, pz.f = #MAX_DEC)
With *Result
\X = px
If py = #MAX_DEC
\Y = px
Else
\Y = py
EndIf
If pz = #MAX_DEC
\Z = px
Else
\Z = pz
EndIf
EndWith
ProcedureReturn *Result
EndProcedure

and this is the command that returns the string
Code:
LEGetEntityKey(entity.i, hkey.s, defaultvalue.s = "")

Top

 Post subject: Re: Help converting a function from bmax to pb?Posted: Sat Mar 10, 2012 9:45 pm

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 1386
Location: Boston, MA
Ok, that is ascii 196. Are you compiling with unicode on?
It could be since:
Code:
If sarr.length > 0 t.x = Float(sarr[0]) * scale

They might only pass a scalar? Meaning, no Y or Z component.
So, 196 would get scaled and returned in \X.

_________________
To understand recursion, you must first understand recursion. ~ unknown
I never make stupid mistakes. Only very, very clever ones. ~ John Peel

Top

 Post subject: Re: Help converting a function from bmax to pb?Posted: Sat Mar 10, 2012 10:03 pm
 Enthusiast

Joined: Wed Jul 12, 2006 4:38 pm
Posts: 384
Location: UK
unicode is definitely off

Top

 Post subject: Re: Help converting a function from bmax to pb?Posted: Sun Mar 11, 2012 12:57 am

Joined: Tue Dec 23, 2003 3:54 am
Posts: 932
Location: New York
This is not an exact "one-to-one" conversion, but it should give the same result.

Note as Skywalk said, it returns a pointer to a new vector, which is different from actually returning an object itself (in an object oriented language).

Code:
; 3D vector structure

Structure TVec3
X.f
Y.f
Z.f
EndStructure

Procedure.i StringtoVec3(text.s, scale.f = 1.0)
Protected *t.TVec3, s.s

; Allocate and pre-definte a new vector
*t   = AllocateMemory(SizeOf(TVec3))
*t\X = 1.0
*t\Y = 1.0
*t\Z = 1.0

; Extract x field
s = StringField(text, 1, ",")
If (s) : *t\X = ValF(s) * scale : EndIf

; Extract y field
s = StringField(text, 2, ",")
If (s) : *t\Y = ValF(s) * scale : EndIf

; Extract z field
s = StringField(text, 3, ",")
If (s) : *t\Z = ValF(s) * scale : EndIf

; Return pointer to new vector
ProcedureReturn *t
EndProcedure

; Create and display an example vector

*myVec3.TVec3 = StringtoVec3("5.4,3.2,1.0", 2.0)

Debug "x y z ="
Debug StrF(*myVec3\X, 1)
Debug StrF(*myVec3\Y, 1)
Debug StrF(*myVec3\Z, 1)

It expects a string of 1 to 3 numbers separated by commas... if your other function is returning a single non-numeric character, then something is probably wrong there...

Top

 Post subject: Re: Help converting a function from bmax to pb?Posted: Sun Mar 11, 2012 12:24 pm
 Enthusiast

Joined: Wed Jul 12, 2006 4:38 pm
Posts: 384
Location: UK
Thanks for your efforts, it's not working because of the string returned.

Top

 Post subject: Re: Help converting a function from bmax to pb?Posted: Sun Mar 11, 2012 12:49 pm

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 1445
Location: Germany
Hi,

I think you have to use 'Static' instead of 'Protected' for t.
Else it makes no sense to return a pointer on it.

Bernd

Top

 Post subject: Re: Help converting a function from bmax to pb?Posted: Sun Mar 11, 2012 5:07 pm

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 1386
Location: Boston, MA
Bernd, since PB does not clear AllocateMemory() in Procedures, the memory lives on!

If you are not using the return value directly in an expression, pass the Structure in the parameters list so you don't have to worry about memory leaks.

Code:
Structure TVec3
X.f
Y.f
Z.f
EndStructure
; Safer code and no Global or memory leak.
Procedure.i StringtoVec3_1(text.s, *t.TVec3, scale.f = 1.0)
*t\X = ValF(StringField(text, 1, ",")) * scale  ; No need for If len(text)!
EndProcedure
Define myV.TVec3
Debug StrF(myV\X)
StringtoVec3_1("5.4,3.2,1.0", @myV, 2.0)
Debug StrF(myV\X)

; Requires Memory Management or possible global variable
Procedure.i StringtoVec3_2(text.s, scale.f = 1.0)
Protected *t.TVec3 = AllocateMemory(SizeOf(TVec3))
*t\X = ValF(StringField(text, 1, ",")) * scale  ; No need for If len(text)!
Debug "memaddr = " + Str(*t)
ProcedureReturn *t    ; this memory is not cleared so it lives on!
EndProcedure
Define *myV.TVec3 = AllocateMemory(SizeOf(TVec3))
Debug "memaddr = " + Str(*myV)
Debug StrF(*myV\X)
*myV = StringtoVec3_2("5.4,3.2,1.0")
Debug StrF(*myV\X)

_________________
To understand recursion, you must first understand recursion. ~ unknown
I never make stupid mistakes. Only very, very clever ones. ~ John Peel

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 10 posts ]

 All times are UTC + 1 hour

#### Who is online

Users browsing this forum: No registered users and 4 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forum

Search for:
 Jump to:  Select a forum ------------------ PureBasic    Coding Questions    Game Programming    3D Programming    Assembly Programming    The PureBasic Editor    The PureBasic Form Designer    General Discussion    Feature Requests and Wishlists    Tricks 'n' Tips Bug Reports    Bugs - Windows    Bugs - Linux    Bugs - Mac OSX    Bugs - Documentation OS Specific    AmigaOS    Linux    Windows    Mac OSX Miscellaneous    Announcement    Off Topic Showcase    Applications - Feedback and Discussion    PureFORM & JaPBe    TailBite