Forum Replies Created
-
AuthorPosts
-
I see the problem now. When the std.graphics.color module is compiled, an overloaded bbCompare() function is created. In this function, the fields are compared 1×1 and result is returned. This is what the If/Else/End statement is using to compare the two parameters. If I overload the = operator, a function called m_eq() is created and this is used instead of bbCompare.
Monkey2
Monkey12345If currentColor = Color.WhitePrint "Current color is white"ElsePrint "Current color is not white"End IfCompiles to C++
C++12345if((bbCompare(l_currentColor,g_std_graphics_Color_White)==0)){bb_print(bbString(L"Current color is white",22));}else{bb_print(bbString(L"Current color is not white",26));}Now the Select/Case statement is different. Instead of using bbCompare(), it is using C++’s built in == operator. Since Monkey2 has no way to overload C++’s == operator, it creates the error.
monkey2
Monkey12345678Select currentColorCase Color.WhitePrint "Select/Case Color is white"Case Color.RedPrint "Select/Case Color is Red"DefaultPrint "Select/Case Color is neither red nor white"End selectCompiles to C++
C++1234567if(l_currentColor==g_std_graphics_Color_White){bb_print(bbString(L"Select/Case Color is white",26));}else if(l_currentColor==g_std_graphics_Color_Red){bb_print(bbString(L"Select/Case Color is Red",24));}else{bb_print(bbString(L"Select/Case Color is neither red nor white",42));}So the solution would be either 1) Have Select/Case use bbCompare instead of == or 2) be able to override == at the C++ level.
Your example has nothing to do with my problem. [b]If currentColor = Color.White [/b] works just fine. Succeeds when currentColor is white, fails on any other color. The problem is not with If/Then, but with Select/Case, which won’t even compile. Your example compiles fine, even though you don’t get the expected results.
Also, floats are comparable. The reason why your test fails is due to the tiny imprecision caused by storing floats. Due to that imprecision, they are rarely exactly equal. Instead, you should subtract the two and compare against a tolerance. If c-(a-b) < .001 gives the expected results in your example.
You could always keep a copy of the image in a pixmap, then you can alter the pixmap with pixmap.SetPixel() when you need to and use New Image(pixmap) to transfer the altered pixmap to an image for rendering
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081Namespace myapp#Import "<std>"#Import "<mojo>"Using std..Using mojo..Class MyWindow Extends WindowField pixmap:Pixmap = New Pixmap(64,64)Field image:ImageField currentColor:Color = Color.WhiteMethod New( title:String="Simple mojo app",width:Int=640,height:Int=480,flags:WindowFlags=Null )Super.New( title,width,height,flags )'let's fill the pixmap with a red circleFor Local x:Int = -32 To 31For Local y:Int = -32 To 31If Sqrt(x*x+y*y) < 32pixmap.SetPixel(x+32,y+32,Color.Red)Elsepixmap.SetPixel(x+32,y+32,Color.Black)End IfNextNext'Now insert white squareFor Local x:Int = 24 To 40For Local y:Int = 24 To 40pixmap.SetPixel(x,y,Color.White)NextNext'now load the pixmap into an imageimage = New Image(pixmap)EndMethod OnRender( canvas:Canvas ) OverrideApp.RequestRender()'if space key is pressed, change the color to the next oneIf Keyboard.KeyPressed(Key.Space)'get the next colorSelect currentColor.ToARGB()Case Color.White.ToARGB()currentColor = Color.BlueCase Color.Blue.ToARGB()currentColor = Color.GreenCase Color.Green.ToARGB()currentColor = Color.YellowCase Color.Yellow.ToARGB()currentColor = Color.WhiteEnd Select'draw a square in the pixmapFor Local x:Int = 24 To 40For Local y:Int = 24 To 40pixmap.SetPixel(x,y,currentColor)NextNext'transfer the pixmap to imageimage = New Image(pixmap)End If'draw the imagecanvas.DrawImage(image,100,100)EndEndFunction Main()New AppInstanceNew MyWindowApp.Run()EndMaybe try out the different themes (window->themes). Going through them, I find a couple blurry, just assumed it is my bad eyes trying to focus on the different color combinations. Maybe it is just certain fonts can’t scale properly at certain resolutions.
Took a look at a simple json output from Tiled.
The “layers” array is actually an array of objects. Each object contains info about each map layer. The “data” array within the layer object contains the actual map data.
I wrote a little sample here that reads the data, stores it in an array, and prints the info. Not complete, but hopefully will help.
Hmm, When I post the code here, I get a “Forbidden” error. well, you can grab the source here. https://drive.google.com/open?id=0B1zQ5dfVU3jbZGtjZmtCckx3SWM
Not a bug. Using a leading 0 is how you type numbers in octal with Monkey2, just like typing a leading $ for hexadecimal. I guess you will need to use leading spaces instead
Monkey123456789Function Main()Local a:Int = 31Local b:Int = $1F 'hex for 31Local c:Int = 037 'oct for 31Print a + " " + b + " " + cEndThis example creates a vector class in which the vector can be stored as direction/magnitude, or as x/y. Internally, it is stored the same way so uses properties to convert between the two. This is handy as it allows you to change implementation behind the scenes without needing to change the interface as well.
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263Namespace myapp#Import "<std>"Using std..Class VectorPrivateField _x:DoubleField _y:DoublePublicProperty x:Double()Return _xSetter (n:Double)_x = nEndProperty y:Double()Return _ySetter (n:Double)_y = nendProperty direction:Double()Return ATan2(_y,_x)Setter(n:Double)Local magnitude:Double = Sqrt(_x*_x+_y*_y)_x = Cos(n) * magnitude_y = Sin(n) * magnitudeEndProperty magnitude:Double()Return Sqrt(_x*_x+_y*_y)Setter(n:Double)Local direction:Double = ATan2(_y,_x)_x = Cos(direction)*n_y = Sin(direction)*nEndMethod normalize()Self.magnitude = 1End methodEndFunction Main()Local vector := New Vectorvector.x = 10vector.y = 10Print "direction = "+vector.directionPrint "magnitude = "+vector.magnitudevector.normalize()Print "normalized direction = "+vector.directionPrint "normalized magnitude = "+vector.magnitudePrint "normalized x = "+vector.xPrint "normlaized y = "+vector.yEndAn Enum will compile to an opaque enum class in the c++ source which uses a signed int for its base. For some reason, M2’s Enum will not accept negative values. I think this might be a parser bug as I can do this
Monkey1234567891011121314151617#Import"<std>"Using std..Enum Directionup = 0-10, down, left, right,endFunction Main()Local direction := Direction.downIf direction = Direction.upPrint "You are moving up"ElsePrint "You are not moving up"EndifEndAnd it works as expected. (much easier to understand than the $FFFFFFFF hack I posted earlier)
Don’t know if this is the best way, but it works. MyImage is the image you wish to save, and savefile is the name of the png file to be saved.
Monkey1(New Canvas(MyImage)).CopyPixmap(New Recti(0,0,MyImage.Width,MyImage.Height)).Save(savefile)Two dimensional arrays cannot currently be resized. You need to roll your own function to do so. Here is an example function that works.
Monkey1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465Namespace myapp#Import "<std>"Using std..Function resize2<T>:T[,](source:T[,],x:Int,y:Int)Local dest := New T[x,y]For Local i:Int = 0 Until Min(source.GetSize(0),dest.GetSize(0))For Local j:Int = 0 Until Min(source.GetSize(1),dest.GetSize(1))dest[i,j] = source[i,j]NextNextReturn destendFunction Main()'create array and fill with random dataLocal test:String[,] = New String[5,5]For Local x:Int = 0 To 4For Local y:Int = 0 To 4test[x,y] = String.FromChar(Rnd(26)+65)NextnextprintArray(test)'increase array sizetest = resize2(test,10,10)printArray(test)'decrease array sizetest = resize2(test,7,7)printArray(test)'increase one dimention, decrease anothertest = resize2(test,10,3)printArray(test)EndFunction printArray(source:String[,])Print "-----------------------------------------~n"For Local y:Int = 0 Until source.GetSize(1)Local s:String = ""For Local x:Int = 0 Until source.GetSize(0)If Not source[x,y]s += "# "Elses += source[x,y] + " "EndifNextPrint sNextPrint "~n----------------------------------------"endYou could cheat a bit and use twos compliment
[/crayon]Monkey123[crayon-5cb9d04b7604b190385103 inline="true" ]Enum FlagMyNum = $FFFFFFFFEndis equivalent to C++
[/crayon]Monkey1234[crayon-5cb9d04b76050618506230 inline="true" ]enum class Flag{MyNum = -1};To find the twos compliment of a signed Int, you can use Print Hex(UInt(-1)) in Monkey2
I never understood the value in dynamic types. Seems that it is unproductive to save a few seconds of typing int, float, etc.. when the result is hours of debugging headaches when you type myNum = 3 instead of myNum = 3.0
As for null objects, it makes sense that you should create an instance with new controls before calling fortifyunit() method. Without an instance, there is no fortifyunit() to call, only a definition.
I have the same problem. Open Ted2go, start a new document, type Self. and just as soon as you hit the ., the IDE closes. If you turn off autocomplete, the problem goes away (but you loose autocomplete)
Edit: Just submitted an issue with the Ted2Go github site.
I get 11598408 for both on Windows 10. I think it has to do with the Float to Int conversion done on the .1. If you use 0.1 instead or define y as a float, then you get correct results.
A class for swapping big/little endian from/to native format. Just create an instance of the class and call endian.swapEndian(n:int, original:Int) where n is the number to convert and original is the byte order you are converting from/to. Nice thing is, the same method is used in each case. pass the int you wish to save followed by the ByteOrder value you are saving to, or pass the value you just read followed by the stored ByteOrder .
Monkey123456789101112131415161718192021222324252627282930313233343536373839#Import "<std>"Using std..Class EndianField endian:IntMethod New()Local a:Int = $AABBCCDDIf UByte Ptr(Varptr a)[0] = $AAendian = ByteOrder.BigEndianElseendian = ByteOrder.LittleEndianEndEndMethod swapEndian:Int(n:Int, original:Int)If endian = original Then Return nReturn ((n & $FF000000) Shr 24) | ((n & $FF0000) Shr 8) | ((n & $FF00) Shl 8) | ((n & $FF) Shl 24)EndEndFunction Main()Local endian := New EndianLocal a:Int = $AABBCCDD 'Load a with a valuePrint "Value of a = "+Hex(a)Local be:String = Hex(endian.swapEndian(a,ByteOrder.BigEndian)) 'save a as big endianLocal le:String = Hex(endian.swapEndian(a,ByteOrder.LittleEndian)) 'save a as little endianPrint "be = "+be+"~nle = "+le 'print the contents of be and leLocal b:Int = endian.swapEndian(FromHex(be),ByteOrder.BigEndian) 'load from big endianLocal c:Int = endian.swapEndian(FromHex(le),ByteOrder.LittleEndian) 'load from little endianPrint "~nb = "+Hex(b)+"~nc = "+Hex(c) 'both should be the sameend -
AuthorPosts