Forum Replies Created
-
AuthorPosts
-
indeed…
There is the FromCString for that purpose..?
[/crayon]Monkey1234567891011[crayon-5cba97e189413277968490 inline="true" ]#Import "<std>"Using std..Function Main()Local USERNAME:= libc.getenv("USER")Local s:=String.FromCString(USERNAME)Print sEndBut the argument of get env is wrong:
Function getenv:char_t Ptr( name:@cstring )So “USER” has to be converted to a cstring (don’t know what the @ means!)
from the docs:
monkey:monkey.types.String.ToCString
Method ToCString:Void( buf:Void Ptr, bufSize:Int )
Converts the string to a CString.If there is enough room in the memory buffer, a null terminating ‘0’ is appended to the CString.
Parameters
buf Memory buffer to write the CString to.
bufSize Size of the memory buffer in bytes.Mark just added String.FromChars a few days ago. It’s in the develop branch on github. I suppose it’s for that purpose..
https://github.com/blitz-research/monkey2/commit/b2976c674ad445a321b8f9c9c175c4025336b664
Great! Thanks.
I use them for 2d physics debug draw so perfection is not mandatory! A bit of consistency when scaling is a good point, I hope it’s not slowing down the process..Structs goes on the stack and classes on the heap… So structs are theoretically faster.. if you’ll use your vects in very iterative process it will slowdown your app. In some very hungry loops it’s even interresting to use x and y with no structs at all!
here is some working example with starting and ending point, so if you invert the handles to arc will flip.
May be you don’t want it to flip when the angle is small. EDIT:If so just invert start and end when the switch occurs.Won’t work “just” like that…
You should have used Vec2f instead of redefine a vec2. Monkey is full of little gentle things like that..[/crayon]Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284[crayon-5cba97e194baa807721911 inline="true" ]Namespace shapeEditor#Import "<std>"#Import "<mojo>"Using std..Using mojo..Const Size:=New Vec2i( 640,480 )Class MyWindow Extends WindowField arc:ArcMethod New()Super.New( "My Window",640,480,WindowFlags.Resizable )Layout = "letterbox"arc = New Arc(100,100,100)EndMethod OnRender( canvas:Canvas ) OverrideApp.RequestRender()arc.Update()canvas.DrawText(arc.startAngle,10,10)canvas.DrawText(arc.endAngle,10,30)canvas.DrawText(arc.stepSize,10,50)canvas.DrawText(arc.angleStep,10,70)canvas.Color = New Color(.8,.1,.1)arc.Render(canvas)EndMethod OnKeyEvent( event:KeyEvent ) OverrideIf event.Type=EventType.KeyDown And event.Key=Key.Enter And event.Modifiers & Modifier.Alt'Fullscreen=Not Fullscreen Returned an error..EndifEndMethod OnMeasure:Vec2i() OverrideReturn SizeEndEndFunction Main()New AppInstanceNew MyWindowApp.Run()EndClass HandleField pos:Vec2dField location:Vec2iField radius:FloatMethod New(pos:Vec2d,radius:Float = 4)Self.pos = posEnd MethodMethod SetLocation(p:Vec2i)End MethodEnd ClassClass EntityField pos:Vec2dField color:ColorMethod New()pos = New Vec2dcolor = New Color(1,1,1,1)End MethodEnd ClassClass Arc Extends EntityField radius:FloatField startAngle:FloatField endAngle:FloatField angleSteps:FloatField stepSize:FloatField angleStep:FloatField lineWidth:FloatField arcCenter:Vec2dField prevs:FloatField preve:FloatField tpos:Vec2dField trad:Vec2dField tstart:Vec2dField teend:Vec2dField temp:Vec2dConst HANDLE_SIZE:Int = 4Method New(x:Float,y:Float,radius:Float=10,startAngle:Float=0,endAngle:Float=Pi/2.0,lineWidth:Int = 6)Self.pos =New Vec2d(x,y)Self.radius = radiusSelf.startAngle = startAngleSelf.endAngle = endAngleSelf.lineWidth = lineWidthSelf.arcCenter = New Vec2dSelf.prevs = Self.startAngleSelf.preve = Self.endAngleSelf.Init()End MethodMethod Init()Self.stepSize = 1.0/Pi*radius'-------modified from hereLocal angleWidth:FloatIf startAngle<=endAngleangleWidth=(endAngle-startAngle)ElseangleWidth=(TwoPi+endAngle-startAngle)EndSelf.angleStep = angleWidth/stepSizeLocal mid:Float = angleWidth/2.0'-------to herearcCenter.Set(Cos(startAngle+mid)*radius,Sin(startAngle+mid)*radius)temp = New Vec2dEnd MethodMethod MouseInHandle:Bool()Local vx:Float = Mouse.X - pos.xLocal vy:Float = Mouse.Y - pos.yReturn Sqrt(vx*vx+vy*vy) <= HANDLE_SIZEEnd MethodMethod MouseInRadius:Bool()Local vx:Float = Mouse.X - (pos.x+arcCenter.x)Local vy:Float = Mouse.Y - (pos.y+arcCenter.y)Return Sqrt(vx*vx+vy*vy) <= HANDLE_SIZEEnd MethodMethod MouseInStart:Bool()Local x:Float = pos.x+Cos(startAngle)*radiusLocal y:Float = pos.y+Sin(startAngle)*radiusLocal vx:Float = Mouse.X - xLocal vy:Float = Mouse.Y - yReturn Sqrt(vx*vx+vy*vy) <=HANDLE_SIZEEnd MethodMethod MouseAtEnd:Bool()Local x:Float = pos.x+Cos(endAngle)*radiusLocal y:Float = pos.y+Sin(endAngle)*radiusLocal vx:Float = Mouse.X - xLocal vy:Float = Mouse.Y - yReturn Sqrt(vx*vx+vy*vy) <= HANDLE_SIZEEnd methodMethod Update()If Mouse.ButtonPressed(MouseButton.Left)If MouseInHandle()tpos = posElseif MouseInRadius()trad = arcCenterElseif MouseInStart()tstart = tempElseif MouseAtEnd()teend = tempEndifElseif Mouse.ButtonReleased(MouseButton.Left)tpos = Nulltrad = Nulltstart = Nullteend = NullEndifIf tpostpos.x = Mouse.Xtpos.y = Mouse.YElseif tradtrad.x = Mouse.X - pos.xtrad.y = Mouse.Y - pos.yLocal vx:Float = pos.x - (pos.x+trad.x)Local vy:Float = pos.y - (pos.y+trad.y)radius = Sqrt(vx*vx+vy*vy)Init()Elseif tstartLocal vx:Float = Mouse.X - pos.xLocal vy:Float = Mouse.Y - pos.ystartAngle = ATan2(vy,vx) '-------modfied this line for unusefull calculus'-------removed things hereInit()Elseif teendLocal vx:Float = Mouse.X - pos.xLocal vy:Float = Mouse.Y - pos.yendAngle = ATan2(vy,vx) '--------modfied this line for unusefull calculus'-------removed things hereInit()EndifEnd MethodMethod Render(canvas:Canvas)canvas.LineWidth = lineWidth' Determine coordinates of first point using basic circle math.Local xx:Float = Cos(startAngle) * radiusLocal yy:Float = Sin(startAngle) * radiusLocal px:Float = pos.x+xxLocal py:Float = pos.y+yycanvas.DrawCircle(px,py,HANDLE_SIZE)For Local i:Int = 1 To stepSize'' Increment the angle by "angleStep".Local angle:Float = startAngle + i * angleStep'' Determine Next point's coordinates using basic circle math.xx = pos.x + Cos(angle) * radiusyy = pos.y + Sin(angle) * radiuscanvas.DrawCircle(pos.x,pos.y,HANDLE_SIZE)'' Draw a line To the Next point.canvas.DrawLine(px,py,xx, yy)px = xxpy = yyNextLocal ex := Cos(endAngle) * radiusLocal ey := Sin(endAngle) * radiuscanvas.DrawLine(px,py,pos.x+ex,pos.y+ey)canvas.DrawCircle(pos.x+arcCenter.x,pos.y+arcCenter.y,HANDLE_SIZE)canvas.LineWidth = 1canvas.DrawLine(pos.x,pos.y,pos.x+arcCenter.x,pos.y+arcCenter.y)canvas.DrawCircle(pos.x+ex,pos.y+ey,HANDLE_SIZE)End MethodEnd ClassClass Vec2dField x:FloatField y:FloatMethod New()End MethodMethod New(x:Float,y:Float)Set(x,y)End MethodMethod New(x1:Float,y1:Float,x2:Float,y2:Float)Set(x2-x1,y2-y1)End MethodMethod New(v:Vec2d)Set(v)End MethodMethod Set:Vec2d(v:Vec2d)x = v.xy = v.yReturn SelfEnd MethodMethod Set:Vec2d(x:Float,y:Float)Self.x = xSelf.y = yReturn SelfEnd MethodEnd ClassCould you post the entire rectangle example codes? I have difficulties with the gcnew in cpp side and how it becomes a simple new in mx2. (Or you can’t ‘new’ it in mx2?)
ThxNovember 17, 2016 at 6:55 pm in reply to: anyway to get the GC to delete a c++ object pointer ? #5159There is at least this way but I suppose it’s not possible in every case..
http://monkey2.monkey-x.com/forums/topic/how-to-get-an-extern-class-that-extend-bbobject-and-not-void/some mx2 objects are not Garbage Collected, like Image for example.
You have to use .Discard() to destruct themDunno if it works with GC objects but you can force the GC to clean memory with GCCollect()
Edit: oops looks you are looking for a onDelete callback, so this answer is probably not relevant..
I think you have nothing to do to get them working.. (here tested working example with box2d c++ wrap)
in c++ .h:
[/crayon]Monkey12345678910[crayon-5cba97e1aa31d040265989 inline="true" ]class b2World{public:/// Construct a world object./// @param gravity the world gravity vector.b2World(const b2Vec2& gravity);/// Destruct the world. All physics entities are destroyed and all heap memory is released.~b2World();in monkey 2 extern section:
[/crayon]Monkey123[crayon-5cba97e1aa323678594479 inline="true" ]Class b2World Extends VoidMethod New(gravity:b2Vec2)Method Destroy() Extension="delete"If I understand your question..
Here some working code (with an int). But you probably already get it.. or may be this is not relevant at all but I wanted to play with the ‘cast’ solution for const ptr. And this is nothing official, so could be deprecated? Never saw it in the docs..
.monkey2
[/crayon]Monkey12345678910111213141516171819[crayon-5cba97e1aebd0430974035 inline="true" ]#Import "<std>"#Import "facto_c.h"ExternStruct c_int="const int"EndFunction Factorial:c_int Ptr (M:int)PublicFunction Main()Local ci:c_int ptrLocal ip:Int Ptrci=Factorial(4) 'should be 24..ip = Cast<int Ptr>(ci)Print ip[0]Endfacto_c.h:
[/crayon]Monkey123456789101112[crayon-5cba97e1aebd7268501374 inline="true" ]const int* Factorial(int M){int factorial=1;int* returner;for(int i=1; i<=M; i++){factorial = factorial*i;}returner=&factorial;return returner; //' returning directly &factorial was not working, it had to be assigned to a 'named' int pointer}Great!
Happy that mx2 extern capabilities are pleasing some people! For me it’s the main new feature compared to mx1.
Looking forward to see your repos!In case you didn’t had the occasion to pass ode includes in c2cmx2, here is a pass. Didn’t tested anything. Just passed it. The imports are not complete (some *.h would need to be added at least)
The attached zip contain the c2mx2 log and the json.
I’ll have a look at your glfw stuff.
It’s the latest ode dev version.. If you want me to pass another don’t hesitate to ask..Attachments:
I am! But have experience with 3D gfx with Matlab only, so very far from GL stuff that look very complex to me (one of the reasons I like mx). For me it would require a basical 3D gfx capable module before attacking 3D physics.
I can pass C code trough c2mx2 as I have all os types on my PC.
Seeing how fast your get things running I think your programming skills are way over mine. But I probably can help.Defining extern with ‘const something’ is very limited. It can lead to some mess if you don’t use for functions arguments only. Because the transpiler will just copy the defined name and ‘const something’ is more than just the name on The C side. Mark has manualy made some glue code for these situation. For example for the chipmunk debugdraw drawpoly callback function that have a ‘const ptr’ as argument.
I had also a similar problem with pure virtual methods for the box2d wrap but I chose to remove the const in the cpp code because box2d is solid enough not to need the const barrier imo. So if you may remove ‘const’ from the C code, it’s probably the fastest way.
It’s one of the difficulties with c to mx2. The only one I encoutered for now.I had the intention to switch to a yearly donate equivalent to 12 monthly patreon (starting in january). If it’s not a good idea please tell..
-
AuthorPosts