MonkeyMax - Part 3

Monkey Targets Forums/User Targets/MonkeyMax - Part 3

therevills(Posted 2012) [#1]
Thread continued from here...

MonkeyMax is a BlitzMax target for Monkey, it allows you to code in Monkey and it outputs BlitzMax code so you can run it on Windows, Mac OS and Linux


Updated 24/08/2012
Lastest Download:
http://code.google.com/p/monkey-max/downloads/list

Instructions can be found here:
http://code.google.com/p/monkey-max/wiki/MonkeyMax


therevills(Posted 2012) [#2]
TODO List:

* Music commands
* LoadState / SaveState
* OnPause / OnResume / OnLoading
* CONFIG variables

Have I missed anything?


therevills(Posted 2012) [#3]
All music and sound commands are done...

http://code.google.com/p/diddy/source/detail?r=411

The looping was interesting because in BlitzMax you can't swap the loop flag so we have to reload the sound sample if we want to change it...


therevills(Posted 2012) [#4]
LoadState / SaveState added:

http://code.google.com/p/diddy/source/detail?r=415

A lot easier than I thought it would be...


therevills(Posted 2012) [#5]
And OnResume and OnSuspend added too :)

http://code.google.com/p/diddy/source/detail?r=416


Qcat(Posted 2012) [#6]
Nice! did not no this was so far along will have to try it out when i am back from camping.


DruggedBunny(Posted 2012) [#7]
Didn't see these recent updates -- thanks!


therevills(Posted 2012) [#8]
Config variables added:

BMAX_WINDOW_TITLE
BMAX_WINDOW_WIDTH
BMAX_WINDOW_HEIGHT
BMAX_WINDOW_FULLSCREEN

http://code.google.com/p/diddy/source/detail?r=417

And added a new zip for the lazy people out there ;)

http://code.google.com/p/diddy/downloads/detail?name=monkeymax_r417.zip&can=2&q=

Re: Config variables do you think MonkeyMax should support MOJO_AUTO_SUSPEND_ENABLED and MOJO_IMAGE_FILTERING_ENABLED?

I think that MonkeyMax is now Monkey/Mojo feature complete :)


Neuro(Posted 2012) [#9]
Awesome stuff, can now use the config files like the rest of the targets :). Great work therevills :)!


therevills(Posted 2012) [#10]
Great work therevills

Thanks :)

Its been a team effort though: Samah, Karja and even Mark Sibly helped a lot along the way.


Paul - Taiphoz(Posted 2012) [#11]
God I wish this went both ways. but stellar work guys.!


Raz(Posted 2012) [#12]
Great job everyone :)


ErikT(Posted 2012) [#13]
This is great! Thanks alot :)


Skn3(Posted 2012) [#14]
Congrats to all this is a really helpful community project!


Neuro(Posted 2012) [#15]
I thinks it time for Mark to include the BMax target as part of the Monkey installation now. I mean, there's even a metro target there now, why not Bmax :)?


Raz(Posted 2012) [#16]
I really appreciate the effort involved in getting this working, but must admit I've not really tried using it purely because I didn't want to mess things up with Monkey. I'd be happy to see it as an official target :)


therevills(Posted 2012) [#17]
God I wish this went both ways

Until (if ever!) Monkey has got the full command set and capabilities of BlitzMax it will never happen both ways...

I didn't want to mess things up with Monkey

That's why you back up and rename your Monkey folder ;)


Raz(Posted 2012) [#18]
Therevills : I know, I know ;) It was more like... I didn't want to have loads of versions of Monkey floating around. The sooner Monkey supports either MonkeyMax officially, or, a "plugin" style system for targets, the better :).

For what it's worth though, I did install Monkey + MonkeyMax on my lil laptop last night and it runs perfectly. Looking forward to getting Ninjah running on PC's as a test this weekend.


Raz(Posted 2012) [#19]
Also to answer your question..

Re: Config variables do you think MonkeyMax should support MOJO_AUTO_SUSPEND_ENABLED and MOJO_IMAGE_FILTERING_ENABLED?


1 vote for filtering here :)


Neuro(Posted 2012) [#20]
Got an EXCEPTION_ACCESS_VIOLATION when trying to run SpaceBugs with this version of MonkeyMax.

EDIT : this was actually related to a sound issue, but been resolved now :).


therevills(Posted 2012) [#21]
Just to let you guys know I have now moved MonkeyMax away from Diddy to its own spot on the web:

http://code.google.com/p/monkey-max/

And shortly I will be updating it to work with v60 and add the filtering and supending config items.


therevills(Posted 2012) [#22]
MOJO_AUTO_SUSPEND_ENABLED and MOJO_IMAGE_FILTERING_ENABLED are now supported :)

http://code.google.com/p/monkey-max/source/detail?r=5


therevills(Posted 2012) [#23]
And MonkeyMax is now up-to-date with Monkey v60... I think ;)

http://code.google.com/p/monkey-max/source/detail?r=9

New download: http://code.google.com/p/monkey-max/downloads/detail?name=monkey-max_r9.zip&can=2&q=


Paul - Taiphoz(Posted 2012) [#24]
is there a way of installing this without harming the normal monkey install yet ? or without having to create a second monkey folder ?


therevills(Posted 2012) [#25]
Well it doesnt "harm" the Monkey install, it just adds to it ;)

Its just "safer" to make a backup :P

But no, really wish Monkey had some kind of plug-in system so we could add targets alot quicker and easier.


Paul - Taiphoz(Posted 2012) [#26]
Yeah.. I thought this project altered the trans binary or summit, I like to keep all my build tools authentic as mark intended when ever I go messing with stuff I tend to break things lol.

And yeah I really hope mark sorts a plugin system out for want of a better word.


therevills(Posted 2012) [#27]
I thought this project altered the trans binary or summit


It does... you can check out all the code and then recompile trans yourself or you can just use the precompiled binary.


Tibit(Posted 2012) [#28]
I got the error: mojo not implemented.

If not, how did you guys convert your Monkey games too Bmax? Or did I miss something obvious here? :)

btw I notices that when I added the monkey-max svn it MERGED a lot of files - will that be a problem? I assume the intention was for the files to be replaced.


Neuro(Posted 2012) [#29]
If not, how did you guys convert your Monkey games too Bmax? Or did I miss something obvious here? :)

It adds a BMAX target to monk. Did you follow the instructions here ?


Tibit(Posted 2012) [#30]
Does that mean mojo are part of it?

Yes those where the instructions I followed.

I assume then the svn-merge thing was what caused this error? Has no one but me installed MonkeyMax directly from the svn?


Neuro(Posted 2012) [#31]
Has no one but me installed MonkeyMax directly from the svn?

I just downloaded the file directly, didn't use the svn. Worked fine for me. Maybe you need to get clean copy of monkey to setup also?


therevills(Posted 2012) [#32]
I suggest using the zip file, as I'll try to keep the zip file and SVN up-to-date together... Think of the zip as the stable release and the tip of the SVN as WIP.

Because Mojo is BRL's module we had to alter a file or two to make it work with other targets, so you need to replace instead of merge if using SVN:

http://code.google.com/p/monkey-max/source/browse/trunk/modules/mojo/mojo.monkey

^ This is an example of that, if you check BRL's mojo.monkey you see it will only compile offical targets, so we needed to change it to work with non-offical targets.


Karja(Posted 2012) [#33]
Hey, I noticed some issues with slice_string... I don't have a suggested fix yet (since I made some workarounds in my own code instead), but these are the symptoms:

- Parsing an XML file (with Diddy) where an attribute is empty, i.e. attribute="", results in assigning the entire XML file string to the attribute instead. It probably has something to do with e.g. "value = str[startIndex..endIndex]" in Diddy's XML parser.
- Doing "str = str[..count-1]" where count = 1 should result in an empty string, but instead it returns a string of length 1.
- Doing "str = str[1..]" on a string of length 1 should result in an empty string, but I get some...strange results instead. I don't recall exactly.

All in all, slice_string is unfortunately not working properly. But it's pretty easy to work around these issues. (Except for the XML parsing... The easiest fix is to check if the attribute is unreasonably large, and in that case set it to be empty.)

Great work other than that! One of these days I should look into sound and see if there's anything left to do there. :)


therevills(Posted 2012) [#34]
Not the slicing stuff again Orz!!

I'm also not happy with the arrya resize stuff either, but it works (I think) for simple resizing.

Oh BTW Karja, do you want to have access to the SVN repository?


Karja(Posted 2012) [#35]
I think I'd better look into the slicing to spare you some frustrated rage over those never ending problems... :)

Sure thing, SVN access would be nice! If you'd like, you can add me (karjasoft@...) and I can check in the fixes when I have them ready.


therevills(Posted 2012) [#36]
SVN access would be nice!


Added :)

And thanks for the help.


Karja(Posted 2012) [#37]
I've checked in a couple of fixes for the slicing, and I don't think I've broken anything. ;)

It seems like my BlitzMax IDE wanted to auto-capitalize a couple of functions, so those unfortunately got checked in as well. But that shouldn't be a problem since BMax isn't case sensitive.


Raz(Posted 2012) [#38]
From what I've read I am not entirely sure of the state of reflection.

Anyway, my game uses reflection (will is set up ready to use it when I implement it properly) and I get the following error when I try to build

Compile Error: Unable to convert from '_Object' to '<unknown>'

And the line of code in question is the IF line below...
Method  m_GetClass:bb_reflection__1Class_1Info(t_o:_Object)
		If(((ThrowableObject)((t_o))) <> ThrowableObject(Null)) Then


Not a complaint by any means, just wondering if this is meant to work yet :)

Ta


Raz(Posted 2012) [#39]
Sorry also, is it best to report stuff here, or over at code.google ?


therevills(Posted 2012) [#40]
is it best to report stuff here, or over at code.google

Either :) If you post it here, least we can have a dialog of the issue.

Compile Error: Unable to convert from '_Object' to '<unknown>'

I actually havent tried reflection with MonkeyMax, I thought it might just work since its just Monkey generated code. Can you post the translated Java code?


@Karja - Thanks for taking the time to fix that issue :)


Samah(Posted 2012) [#41]
It seems like my BlitzMax IDE wanted to auto-capitalize a couple of functions, so those unfortunately got checked in as well. But that shouldn't be a problem since BMax isn't case sensitive.

I usually do a diff before I hit commit to make sure that the only changes are the changes I intended. Easy enough to merge in KDiff3 before committing. I hate it when IDEs do things I didn't ask them to. :)


Raz(Posted 2012) [#42]
Hows Blitzmax with multi dimensional arrays?

bb_generation__1P_1M_1Data=[new Int[t_tm_1H],new Int[][0],new Int[0]]


is causing this error

Compile Error: Auto array elements must have identical types



which is translated from this Monkey code

PMData = New Bool[tmH][][]


(tmH is an Int)


Raz(Posted 2012) [#43]
Sorry also Therevills

Can you post the translated Java code?


You mean, build for Android and post the code?


therevills(Posted 2012) [#44]
I really need to re-look at the array stuff in MonkeyMax...

You mean, build for Android and post the code?

Please :)


Samah(Posted 2012) [#45]
@therevills: I really need to re-look at the array stuff in MonkeyMax...

Can I declare an array foo:Int[][][][][][][][][][][][][][][][]?


Raz(Posted 2012) [#46]
Therevills: Ok, will do when I get home :)


Raz(Posted 2012) [#47]
As for the java code of the reflection problem, I believe it's...

public bb_reflection_ClassInfo m_GetClass(Object t_o){
		Object t_=t_o;
		if((t_ instanceof ThrowableObject ? (ThrowableObject)t_ : null)!=null){
			return bb_reflection.bb_reflection__classes[10];
		}



therevills(Posted 2012) [#48]
Thanks Raz, I'll have a look on the weekend. Any chance you could quickly put together some runnable code which causes this issue?


Neuro(Posted 2012) [#49]
There seems to be an issue with PlayMusic() in the bmax target. If you are currently playing a music file, and then call a StopMusic() (or any music functions for that matter), and then call PlayMusic() again it will throw and exception access violation. Basically, changing or switching music more than twice will crash it.

Not sure if this is a BlitzMax issue or not. Doesn't seem to happen in monkey. Any ideas?


therevills(Posted 2012) [#50]
What happens if you run it in Debug mode, do you get a better error message?

I guess it might be the StopChannel in PlayMusic:

In mojo.bmax.bmx, could you try this change:
[monkeycode]
Method PlayMusic:Int( path:String, flags:Int )
StopMusic()
channels[MUSIC_CHANNEL].channel = AllocChannel() ' << NEW LINE HERE
music = LoadSample( path )
If Not music Then Return -1

PlaySample(music, MUSIC_CHANNEL, flags)
Return 0
EndMethod[/monkeycode]


Neuro(Posted 2012) [#51]
Thanks therevills, that definitely fixed it :).

When running the debug, it threw a "Unhandled Exception attempt..." in the StopChannel() Method.

The only minor issue now is that there's a slight pause when switching music files (depending on file size). In Bmax, we were able to preload the music files, unfortunately can't do the same in monkey. Any suggestions on this one :)?


Raz(Posted 2012) [#52]
Any chance you could quickly put together some runnable code which causes this issue?


Therevills, sure thing, although I think it's literally a case of, if you use reflection the code is included (that is, it's not anything that my code has done).


therevills(Posted 2012) [#53]
@Neuro - thats great, I've committed the fix :)
I guess to fix the slight delay, is to actually use just a Sound instead Music - maybe or create an extern class to preload the music and use your own commands to play it ;)

@Raz, dont worry about the example I just added "Import reflection" to the top of mojoTest and found a similar error:
Compile Error: Unable to convert from 'bb_app__1App_1Device' to '_Object'

(Also found that I missed a method in the Surface class too, due to the reflection compiling everything ;))


Raz(Posted 2012) [#54]
Ok cool :)


benmc(Posted 2012) [#55]
I'm unfamiliar with BlitzMax, but would building for this target create a single EXE with all the art assets, sounds, etc, bundled in a single executable? If so, it may be the perfect solution for me when trying to create PC games with Monkey.


Raz(Posted 2012) [#56]
That has been exactly my thinking too Ben. Even if it wasn't automatic I know it's possible to pack data in with the a Blitzmax exe so that's what I will be doing


Neuro(Posted 2012) [#57]
I'm unfamiliar with BlitzMax, but would building for this target create a single EXE with all the art assets, sounds, etc, bundled in a single executable?

I believe BlitzMax has this "incbin" feature that can do it but i'm not sure how easy it would be to use that with the export from Monkey. However, i have used the SmartPacker Pro tool can actually do it and works well with BlitzMax executables.


benmc(Posted 2012) [#58]
I tried the demo of SmartPacker Pro but it didn't work for me. With BlitzMax being about the same price, if this option made it possible to bundle it all together, that would be awesome - unfortunately my trial of BlitzMax ended ages ago so I can't test it :(


therevills(Posted 2012) [#59]
Neuro is correct that you can use IncBin to include external files into the EXE in BlitzMax.

MonkeyMax does not do this for you, but you could alter the translated code and add it yourself. Also when I actually release games to Portals I dont use incbin.


benmc(Posted 2012) [#60]
@therevills

Do you mean that your leave your assets in a public data folder that anyone could edit when you publish?


therevills(Posted 2012) [#61]
Yep... also if you want to encourage modding its the way to go.


Raz(Posted 2012) [#62]
Yep... also if you want to encourage modding its the way to go.

Do you ever use licensed media Therevills? I'm not concerned about packing my own creations but am worried about making licensed music too easily available when I've only licensed it to be used during the game in question.


outsider(Posted 2012) [#63]
Hi, I have a problem with multidimensional arrays (arrays of arrays).

This is my small utility class for arrays:

This code unfortunately does not work under MonkeyMax(all other targets works):
Strict
Import mojo
Import utils.arrays

Class MyApp Extends App
	Field arr2d:Int[][]
	Field printed:Bool = false

	Method OnCreate:Int()
		SetUpdateRate 30
		arr2d = Arrays<Int>.Create2d(5,5)
		arr2d = Arrays<Int>.Clear2d(arr2d, 111)
		Return 0
	end
	
	Method OnLoading:Int()
		Return 0
	end
	
	Method OnSuspend:Int()
		Return 0
	end
	
	Method OnUpdate:Int()
		If printed = False
			Print(arr2d[1][1])
			printed = true
		Endif
		Return 0
	End
	
	Method OnRender:Int()
		Return 0
	End
End

Function Main:Int()
	New MyApp
	Return 0
End


(Monkey v60, MonkeyMax r9, BlitzMax 1.48)


therevills(Posted 2012) [#64]
Do you ever use licensed media

Yeah, it all depends on the license. In Pirates I had to compress a few sounds as I wasnt allowed to have them outside the game.

I have a problem with multidimensional arrays (arrays of arrays).


NOOOOOOOOoooooooooo........ :'(

If you check above, the major issues with MonkeyMax are arrays and reflection. But I currently dont have any time to fix them, hopefully soon.


outsider(Posted 2012) [#65]
Oh, sorry ;)


Neuro(Posted 2012) [#66]
Some issues i've encountered with MonkeyMax :

1) Logical operators statements that worked in all the targets in Monkey didn't always work in the BMAX target. Not a huge issue though, just required cleaner statements.
2) In my Tevada Trigger game, I used the tiling code for the background grid tiles. The Monkey targets didn't have this issue, but in BMAX it tended to "flicker" occasionally - only on the PC platform though, not Mac.
3) I never did got the music loading routine to work right BMAX. We talked about it before in the MonkeyMax thread since Monkey doesn't actually a "LoadMusic" function, everything is streamed from the file itself thus causing a momentary pause if you decide to switch music. This isn't acceptable so i went ahead and just kept one music file playing in Tevada Trigger.


therevills(Posted 2012) [#67]
@Outsider, nothing to be sorry about :)

@Neuro, thanks for that. Which logical operators where you having issues with? And what do you mean with "just required cleaner statements"?


Neuro(Posted 2012) [#68]
@Neuro, thanks for that. Which logical operators where you having issues with? And what do you mean with "just required cleaner statements"?

Its been a while since i had that issue, but it was something along the lines of :
local b:Int = 2;
local a:Int = 2;
local c:Int = 1;
If (a=2) and (b=2) and (Not c=2)
 print ("It works");
EndIf  

And which nothing ever prints. However, been trying to reproduce the issue but it actually works now. I'll let you know if i encountered anything else with it.


benmc(Posted 2012) [#69]
Is it mostly the 2D arrays and special cases where there are problems? Do standard 1D arrays work fine?


outsider(Posted 2012) [#70]
Hi,
please, try this version of method TransNewArrayExpr$( expr:NewArrayExpr ) from "bmxtranslator.monkey". Now it's should works for multidimensional arrays.
(Monkey v60, MonkeyMax r9, BlitzMax 1.48)
[monkeycode]Method TransNewArrayExpr$( expr:NewArrayExpr )

Local texpr$ = expr.expr.Trans()
'Print "TransNewArrayExpr texpr="+texpr
Local elemTy := ArrayType( expr.exprType ).elemType
'
' If StringType( elemTy ) Return "bb_std_lang.stringArray"+Bra(texpr)
'
Local t$ = "[" + texpr + "]"
Local tmp$, i%=0
Local ma? = False
While ArrayType( elemTy )
elemTy = ArrayType( elemTy ).elemType
tmp = t
If i = 0
t = "new " + TransType( elemTy ) + "[]"
t += tmp
Endif
i += 1
ma = True
Wend
' TEST THIS BIT LATER!!!!!!!!!!!!!!!!!!!!!!!!<!<!>!<!><!

If ma
Return t
Else
Return "new " + TransType( elemTy ) + t
End

End[/monkeycode]

The problem was this:
1. array declaration (in the generated bmx code)
[monkeycode]Global arr:Int[][] = [New Int[10], New Int[0]] 'was
Global arr:Int[][] = New Int[][10] 'should be[/monkeycode]

2. differences between Monkey and BlitzMax arrays init
Monkey init array
[monkeycode]Const x:Int = 10, y:Int = 5, z:Int = 15
Global tab:Int[10][][] '<---

For Local i:Int = 0 Until x
tab[i] = New Int[y][] '<---
For Local j:Int = 0 Until y
tab[i][j] = New Int[z]
Next
Next[/monkeycode]
BlitzMax init array
[bbcode]Const x:Int = 10, y:Int = 5, z:Int = 15
Global t:Int[][][10] '<---

For Local i:Int=0 Until x
t[i] = New Int[][y] '<---
For Local j:Int=0 Until y
t[i][j] = New Int[z]
Next
Next [/bbcode]

I hope it works now ;)

Edit: and small test



Neuro(Posted 2012) [#71]
Just wondering, is MonkeyMax still being updated for the latest version of Monkey? Does it even need to be or can we just use the current version with it?


therevills(Posted 2012) [#72]
I am going to update it to v63(b) soon, I've got finish my Monkey Touch game by the 30th August, so it'll be after that.

It'll be interesting to see how slow its going to be with the Write/Read Pixel stuff -_-


And thanks outsider I'll add your changes at that time too :)


Karja(Posted 2012) [#73]
Way ahead of you, therevills :) I just popped in to say thanks to outsider for the fix, and to mention that I've updated MonkeyMax to v63(b) too.


therevills(Posted 2012) [#74]
Way ahead of you


Wouldnt be the first time! LOL!

Great Job! :)

Hows the Read/Write pixel stuff?

[Edit]

I actually can't see your changes which adds Read/Write pixel?

[Edit2]

I've just updated to all your new changes, but I was unable to run trans_winnt.exe. Which compiler are you using to compile trans?

I noticed that your exe is over 2MB in size whereas the official exe is less than 1.5MB...

[Edit3]

Just committed a bmax.mojo change which just adds the stubs for Read/Write pixel so at least MonkeyMax compiles if anyone tries to use them ;)


therevills(Posted 2012) [#75]
Added code for ReadPixels and WritePixels2. Also added a test example to test these methods (destructableTerrain by NoOdle).

New download zip added too :)

http://code.google.com/p/monkey-max/downloads/list


Karja(Posted 2012) [#76]
Hmm.. I'm using MinGW64 - that might be the issue. Good to know for the next time! Might have to install the 32 bit version.

And I actually skipped the Read/Write pixel stuff since it looked like one can just use it to Grab from the backbuffer; i.e, not have an alpha=0 background. Thought it was relatively useless in that case, since it would force the masking to be binary, "mask away all black", instead of a smooth and gradual alpha border around sprites. But I'll look at it more thoroughly later.


therevills(Posted 2012) [#77]
I'm using MinGW64 - that might be the issue


Yeah maybe.

And I actually skipped the Read/Write pixel stuff since it looked like one can just use it to Grab from the backbuffer


I've added it for MonkeyMax and that example, check it out. But now with your work and the stuff I've done today at least MonkeyMax is up-to-date :)


We found a couple more bugs which we need to check out:

* Casting a string to a bool fails: Local test:Bool = Bool("true")
* Mouse clicking works sometimes and doesnt other times, check the Diddy Simple GUI example


outsider(Posted 2012) [#78]
Hi, thanks for new version and Your hard work on MonkeyMax :) I love it.

PS: I'm working on OpenGLES 1.1 for MonkeyMax.


BlitzProg(Posted 2012) [#79]
hey there! I'm porting my app to BlitzMax... just found a translation issue

Top = monkeycoder file
Bottom = translated blitzmax file


headlen is the size of the header. This makes my app jump through the replay file data and read wrong data&block size, eventually causing it to crash. Everything work "fine" when I add parenthesis (still having other problems atm, but I didn't investigate yet.)


therevills(Posted 2012) [#80]
Hmmm looks like BlitMax's order of precedence is a bit different with the bit shifting...

[monkeycode] Local x% = 10
Local y% = 5
Local z% = 100 Shl (x * y)
Print z[/monkeycode]

Results:
Target   | Result
---------+--------
BlitzMax | 512000
Flash    | 26214400
HTML5    | 26214400
GLFW     | 0
XNA      | 26214400
STDCPP   | 26214400


Wonder whats wrong with GLFW too...

[Edit]
Just committed a fix for this, grab the trans.exe from r20.


BlitzProg(Posted 2012) [#81]
Here's another problem ^_^ I was busy fixing my replay desync on my app, had the following issue:

[monkeycode]hero[2]=initphorj( generate(24,24,New sector([5,2],[0,5,2],50,2,0,0,0)) ,24*getrnd()+24)[/monkeycode]
where function "generate" generates a randomized sector using function getrnd()

In BlitzMax, it seems that 24*getrnd()+24 is evaluated first, causing the whole sector generation to not match. Worked around with:

[monkeycode]Local tempvar = generate(24,24,New sector([5,2],[0,5,2],50,2,0,0,0))
hero[2]=initphorj( tempvar ,24*getrnd()+24)[/monkeycode]


therevills(Posted 2012) [#82]
I dont think I can fix that one, sorry. IMO I think its actually safer the second way.

FYI: http://stackoverflow.com/questions/376278/parameter-evaluation-order-before-a-function-calling-in-c

The following code:

int i=1;
printf("%d %d %d\n", i++, i++, i);

results in

2 1 3 - using g++ 4.2.1 on Linux.i686
1 2 3 - using SunStudio C++ 5.9 on Linux.i686
2 1 3 - using g++ 4.2.1 on SunOS.x86pc
1 2 3 - using SunStudio C++ 5.9 on SunOS.x86pc
1 2 3 - using g++ 4.2.1 on SunOS.sun4u
1 2 3 - using SunStudio C++ 5.9 on SunOS.sun4u


So I would say its bad practice to rely on a parameter for another parameter.


Samah(Posted 2012) [#83]
I would be dedicating an entire line to calling getrnd() so that you can guarantee nothing else is interfering with it.


BlitzProg(Posted 2012) [#84]
Alright then, thanks. Yes, the problem was solved this way. :)

I'm already so much grateful for this target, it's working so well. I ported this game to monkeycoder, and using BlitzMax Target have it run at about three times the speed. So I'm doing my bit to contribute ^_^

So, How about this? I very frequently get memory access violation because of this in my game somehow. I don't know why it happens (as in: why it tries to retrieve a field from a null object) but it happens at the same frame each time.

Debug screenshot. As you can see from the fields and the code, something is wrong here.


Execution always causes this glitch at the same time during the replay of the gameplay.



BlitzProg(Posted 2012) [#85]
Actually it's somewhat a more general problem, also got that thing to occur at the same frame somewhere else, when a bomb explode in my game.



It happens when a bomb (with a countdown sound) explodes and get deleted from the game. It's strange it doesn't happen with all bombs tho...


therevills(Posted 2012) [#86]
Any chance you could put together an example which causes the crash?


BlitzProg(Posted 2012) [#87]
If I figure out why there is the crash, I'll post a code... in the meanwhile I have no idea yet why it does this, so I added a check that prevents accessing the fields to let the game continue running.


jjsonick(Posted 2012) [#88]
This is great stuff, therevills. I have MonkeyMax converted projects compiling from Windows and Mac BlitzMax now.

I'm okay with working with a Monkey v63 so I can use MonkeyMax for now, but I just ask, along with looking into the apparently known 2-D array issues discussed above (haven't tested that myself yet), that when Mark implements his proposed filestream module that that be mirrored in MonkeyMax. Thanks for all your work on this.


therevills(Posted 2012) [#89]
Re: 2D Arrays: Hopefully these have been fixed.

Re: v63+ versions, due to what Mark has written in his latest blog about his thoughts on how to add new targets I don't think its worth spending too much time on MonkeyMax as it might all have to change shortly in the future.

So you could say, for me at least, MonkeyMax is on hold.


jjsonick(Posted 2012) [#90]
2D Arrays: Great!

Re: v63+: After reading Mark's blog post, I see what you mean. Sounds like exciting changes for the adding-targets process.


Raz(Posted 2012) [#91]
Re: 2D Arrays: Hopefully these have been fixed.


Would this maybe include 3D arrays too? :D (e.g. Local tDate:Int[][][])


byo(Posted 2012) [#92]
Hi, therevills.

Is there a way for me to make it work with v66?
I'd love to be able to use it with BMax with the latest version.

Is that too much work? :-)

Thank you for such an amazing target.


therevills(Posted 2012) [#93]
@raz, I havent tested 3D arrays, but Im pretty sure some of the slicing methods will not work.

@byo, it shouldnt be too much work although the new brl modules (async stuff) wouldnt be included if that is what you are after.


byo(Posted 2012) [#94]
@therevills: I don't mind the async stuff for now since I'm a beginner. ;-) I just wanted to be able to use Diddy while testing the new monkey version. I feel that I'm helping test the software for bugs using the latest version. Thank you such an amazing work.


therevills(Posted 2012) [#95]
Okay that was painful to update MonkeyMax to v66!!!

The mojoTest works at least, havent really tested too much else as it took far to long to debug issues.


byo(Posted 2012) [#96]
That's awesome! I will test everything I can and post any issues I find.
Thank you.


Raz(Posted 2012) [#97]
Just again saying thank you for this, just got Ninjah working via BlitzMax using incbin to embed my licensed music as well :D


byo(Posted 2012) [#98]
I agree. Thank you. Translation is working great. :-)


Raz(Posted 2012) [#99]
Sorry me again!

Does anyone have any experience running a Monkey produced BlitzMax game on Linux? I tried it last night and I think I got maybe 0.1 fps (even just displaying a single graphic in the middle of the screen). When coding a BlitzMax game natively and running it on Linux, it's 60fps.

Ta :)


therevills(Posted 2012) [#100]
I don't use Linux, but that sounds very strange. I gather the same generated bmx file runs fine in Windows/MacOS?


Raz(Posted 2012) [#101]
Haven't been able to check MacOS yet, but yeah in Windows it's absolutely fine (even on a quite old laptop). I'll do some snooping tonight/at the weekend and try to help work out where the slow down is.


Raz(Posted 2012) [#102]
Ok so, I've been messing around with this and it seems that calling MusicState() is the cause for the really low frame rate.

Removing this causes the game to run at a more respectable rate :)

I'm aware that sound support with BlitzMax in Linux is patchy at best, so I am not sure what can be done here really!


therevills(Posted 2012) [#103]
Hmmm, all that MusicState() does is call the BlitzMax command ChannelPlaying(), I could wrap the MusicState with a preprocessor check to see if the target is Linux then return -1 instead of doing the BlitzMax command.

What do you think?


skid(Posted 2012) [#104]
Fixing this issue properly could help many people.

raz, how many times are you calling MusicState, every update or more?

If you are able, I would try to patch in either ALSA or Pulse driver modules as documented in linux threads on bb.com.


therevills(Posted 2012) [#105]
Fixing this issue properly could help many people.

I don't think MonkeyMax can fix it, I suggested the preprocessor so it kind of matches what Monkey does for Flash with MusicState:

"Flash : ChannelRate does nothing. ChannelState and MusicState always returns -1, ie: 'unknown'"


Raz(Posted 2012) [#106]
Skid: Calling it every frame to see if the music has stopped playing (and then select a random alternative track)

Therevills: I'll give the audio drivers Skid mentions a go first and then let you know how I get on. I think this issue is beyond MonkeyMax though, so it shouldn't really be anything you need to fix.


therevills(Posted 2012) [#107]
Calling it every frame to see if the music has stopped playing


Could you keep a map of the music length and have a counter time down? Once the counter reaches zero then change tracks?

I think this issue is beyond MonkeyMax though, so it shouldn't really be anything you need to fix.


Yeah me too... but if this is a common issue with Linux I think it might be safer not to allow it for that target.


Raz(Posted 2012) [#108]
Could you keep a map of the music length and have a counter time down? Once the counter reaches zero then change tracks?


Yeah I could do this, but I suspect once I get the sound working properly, the ChannelState() call will actually work just fine.

I'll give Skid's suggestions a go and see how I get on.


Raz(Posted 2012) [#109]
I'm struggling to rebuild the modules at the moment (the process is hanging on freeaudioglue.cpp), but that said if I do what is suggested here

http://blitzbasic.com/Community/posts.php?topic=98035

and run

padsp ./MonkeyGame


everything works fine, sound and music


BlitzProg(Posted 2012) [#110]
From version 63, if this wasn't corrected already:

[monkeycode]
Import mojo

Function Main()
Local i

Print "ONE!"

For i=1 To test()
Print "THREE!"
Next

Print "FOUR!"
New BP()
End Function

Function test()
Print "TWO!"
Return 5
End Function

Class BP Extends App
End Class
[/monkeycode]

MonkeyCoder
ONE!
TWO!
THREE!
TWO!
THREE!
TWO!
THREE!
TWO!
THREE!
TWO!
THREE!
TWO!
FOUR!


BlitzMax
ONE!
TWO!
THREE!
THREE!
THREE!
THREE!
THREE!
FOUR!



therevills(Posted 2012) [#111]
Sorry BlitzProg but that's unfixable plus I would never do that in a For loop, you are asking for trouble ;)


BlitzProg(Posted 2012) [#112]
This was just in case. I'm never trying this again either... :D


Raz(Posted 2012) [#113]
Just been messing around with some things that weren't working before and they seem to work now! :)

3d Arrays of ints and objects are fine.

2d arrays holding different types of objects (that both extend the same type of object) now work too!

This is amazing, it means I can carry on working on Perling knowing I can release it on PC now :D


Gerry Quinn(Posted 2012) [#114]
The for loop thing is basically just Monkey not doing an optimisation that Max does. (And strictly-speaking, it's not actually an optimisation unless either 'Print' in Max is specified as a debug-only command, or Max is specified to always call such a function in a for loop only once whatever it is.)

If the latter is true, you could always translate it in the same way as Max does.


Raz(Posted 2012) [#115]
I'm not entirely sure but.... I think MonkeyMax struggles with the following code (which I use as a more elegant way of doing lots of If/ElseIfs

Local i:Float = 0.1
Local testLimit:Float = 0.2
Local testLimitAnother:Float = 0.4

Select True
Case i > testLimit

Case i > testLimitAnother

End


I think "Case i > testLimit" is translating to the following

If(t_ = t_i > t_land_1Low) Then

which isn't an error, but produces incorrect results.


devolonter(Posted 2012) [#116]
It seems that I found a bug. BlitzMax doesn't support JoyX, JoyY and JoyZ indexes, only ports (units?). Therefore, probable fix for it:

[bbcode]
Method JoyX:Int(index:Int)
Return Pub.FreeJoy.JoyX(index Shr 4)
EndMethod

Method JoyY:Int(index:Int)
Return Pub.FreeJoy.JoyY(index Shr 4)
EndMethod

Method JoyZ:Int( index:Int )
Return Pub.FreeJoy.JoyZ(index Shr 4)
EndMethod
[/bbcode]


devolonter(Posted 2012) [#117]
OK, it seems there is another bug. If you try to call SetChannelVolume after StopChannel you will get null pointer exception. SetChannelVolume for stopped channels works fine on all Monkey targets, but crashes in MonkeyMax.

Fix. Remove lines chan.channel=null and chan.sample=null in StopChannel and get following code:
Method StopChannel( channel:Int )
	Local chan:gxtkChannel = channels[channel]
		
	If chan.state <> 0
		chan.channel.Stop()
		chan.state=0
	EndIf
EndMethod


For example, if you look in to html5 native code you can will see the following:
gxtkAudio.prototype.StopChannel=function( channel ){
	var chan=this.channels[channel];
	
	if( chan.state!=0 ){
		chan.audio.pause();
		chan.state=0;
	}
}

This code doesn't contain chan.channel = null and chan.sample = null


devolonter(Posted 2012) [#118]
And one small fix for bmxtranslator :)

Replace line:
Case "pow" Return Bra(arg0)+"^"+arg1

by:
Case "pow" Return "(" + Bra(arg0) + "^" + Bra(arg1) + ")"

Otherwise the following code gives incorrect results:
'incorrect results for both cases
Pow(2, 10 * (t - 1))
-Pow(2, -10 * t) + 1



therevills(Posted 2012) [#119]
@devolonter, would you like SVN access, so you can add these fixes yourself? If so send me your gmail account and I'll add you to the MonkeyMax project :)


Samah(Posted 2012) [#120]
Ewww SVN. ;)


devolonter(Posted 2012) [#121]
@therevills I would be glad to get SVN access :) Email is in my profile. Thanks!


therevills(Posted 2012) [#122]
Ewww SVN. ;)

You love it ;P

I would be glad to get SVN access :)

Cool! Just added you as a Project committers :) Now go break some things ;)


This has really turned into the community target, quite a few people have really helped to get it to where it is today :D


devolonter(Posted 2012) [#123]
Nice! I just commited all fixes that I suggested earlier. But I didn't rebuild trans_winnt.exe

This has really turned into the community target, quite a few people have really helped to get it to where it is today :D

It's realy great! :)


Raz(Posted 2012) [#124]
I know BlitzMax has plenty of file IO related commands, so I'm wondering how easy would it be to support the OS module?

I wish I could contribute more actually seeing as I am using this target more and more!


Raz(Posted 2012) [#125]
By the looks of it, the OS module was based on the BlitzMax codes which means most of the commands are native already. So all OS functions work exactly the same except for...

' No BlitzMax implementation
'
' AppPath : String ()
' Execute : Int ( cmd:String )
' ExitApp : Int ( retcode:Int )
' GetEnv : String ( name:String )
' HostOS : String ()
' LoadDir : String[] ( path:String, recursive:Bool, hidden:Bool )
'
' Implementation but different?
'
' CreateDir : Int ( path:String ) - NATIVE but has second recursive=False option
' ExitApp : Int ( retcode:Int )   - End command?
' GetEnv : String ( name:String ) - _GetEnv ?
' LoadDir : String[] ( path:String ) - NATIVE but has second skipdots=True option
'
' Maybe implemented?
'
' SetEnv : Int ( name:String, value:String ) - does _SetEnv exist ?



Raz(Posted 2012) [#126]
Ok, I think I managed it!

updated modules/os/os.monkey changing

#end


to

#elseif TARGET="bmax"

	Import "native/os.bmx"

	Extern

	Function HostOS$() = "BmaxHostOS"
	Function AppPath$() = "BmaxAppPath"
	Function AppArgs$[]() = "BMaxAppArgs"
	Function RealPath$( path$ ) = "RealPath"
	Function FileType( path$ ) = "FileType"
	Function FileSize( path$ ) = "FileSize"
	Function FileTime( path$ ) = "FileTime"
	Function CopyFile( src$,dst$ ) = "CopyFile"
	Function DeleteFile( path$ ) = "DeleteFile"
	
	Function SaveString( str$,path$ ) = "SaveString"
	Function LoadDir$[]( path$ ) = "LoadDir"
	Function CreateDir( path$ ) = "CreateDir"
	Function DeleteDir( path$ ) = "DeleteDir"
	Function ChangeDir( path$ ) = "ChangeDir"
	Function CurrentDir$() = "CurrentDir"
	Function SetEnv( name$,value$ ) = "BMaxSetEnv"
	Function GetEnv$( name$ ) = "getenv_"
	Function Execute( cmd$ , silent = True ) = "BmaxExecute"
	Function ExitApp( retcode = 0 ) = "BmaxEnd"

	Public
	

#end


added modules/os/native/os.bmx

Function BmaxEnd(ret:Int)
	End
EndFunction

Function BmaxAppPath$()
	Return AppDir
EndFunction

?Win32

Function BMaxExecute( tCommand:String, tSilent:Int = True )

	' start doesnt work unless called via a CMD prompt (env var issue?)
	'If tSilent = True
	'	system_( "start "+tCommand )
	'Else
		system_( tCommand )
	'EndIf
	
EndFunction	

?Linux

Function BMaxExecute( tCommand:String, tSilent:Int = True )

	If tSilent = True
		system_( tCommand+" &" )
	Else
		system_( tCommand )
	EndIf
	
EndFunction

?


Function BMaxAppArgs:String[]()
	Return AppArgs
EndFunction

Function BmaxSetEnv( name$, value$ )
	putenv_ name+"="+value
EndFunction

Function BmaxHostOS$()
?Win32
	Return "win32"
?MacOS
	Return "macos"
?Linux
	Return "linux"
?
EndFunction


it works as expected (well, as I expect!) but it'd be cool if someone could check it for me


therevills(Posted 2012) [#127]
Looks good to me! If I knew it was that easy I would have done it ages ago :P

Good job! I've added it to MonkeyMax :)

(And I've added you (Raz) to the SVN project using your email in your profile)


ziggy(Posted 2013) [#128]
The function BMaxEnd does not return to the OS the passed return errorlevel value. Maybe this could be changed? If you compile trans, as instance, wich uses return code to inform on compilation errors, it would fail to provide a proper errorlevel upon execution. It's a minnor thing and I don't know wich is the appropriate way to solve this on BlitzMax (can't remember) but as it is an inconsitency, I thought I had to mention it.
I'm sure the BMK source code does provide errorlevel on execution completion, so maybe someone could take a look to that source code? (Not at my computer right now)


Skn3(Posted 2013) [#129]
My XML lib uses String.FromChars and this seemed to be the only thing monkeymax complain about. So......

I have a small tweak to add String.FromChars

in monkey_folder/modules/trans/bmxtranslator.monkey, near line 577 add:
Case "fromchars" Return "string_from_chars" + Bra(arg0)


in monkey_folder/modules/monkey/native/lang.bmx add the following function:
Function string_from_chars:String(chars:Int[])
	' --- this will replicate monkeys FromChars string function ---
	'seems like it would be the fastest option to build a short array and then convert it in 1 call
	'build temp short array
	Local converted:Short[chars.length]
	For Local index:Int = 0 Until chars.length
		converted[index] = Short(chars[index])
	Next
	
	'convert and return short array
	Return String.FromShorts(converted, converted.length)
End Function



MikeHart(Posted 2013) [#130]
Will the MonkeyMax target be compatible with V67d soon?


therevills(Posted 2013) [#131]
I haven't even looked into the new target system... and Im pretty busy in my own projects.


MikeHart(Posted 2013) [#132]
Ok, thanks for the info.