About Monkey 2 › Forums › Monkey 2 Programming Help › Compile and crash
This topic contains 6 replies, has 3 voices, and was last updated by
wiebow
2 years, 10 months ago.
-
AuthorPosts
-
June 8, 2016 at 6:38 pm #986
Sorry Mark, I have another one. This code results in a compile error, but the application starts anyway, and I need to kill the process:
Monkey12345678910111213141516171819202122232425262728293031323334Namespace test#Import "<std>"#Import "<mojo>"Using std..Using mojo..#Import "assets/sprites.png"#Import "assets/arcade.ttf"Class MyTest Extends WindowField canvas:CanvasField i:ImageMethod New()' this sets title. no other way to do this.Super.New("Test!!!")i = Image.Load("asset::sprites.png")i.Handle = New Vec2f(0.0, 0.5)canvas.Font = Font.Load( "asset::arcade.ttf", 10 )End MethodEnd ClassFunction Main:Void()New AppInstanceNew MyTestApp.Run()End FunctionThis is the output from mx2cc:
Parsing…
Semanting…
Translating…
Compiling….
Linking /home/wiebo/Monkey2Stuff/boulderdash/bd.buildv009/desktop_debug_linux/bd
Running /home/wiebo/Monkey2Stuff/boulderdash/bd.buildv009/desktop_debug_linux/bdMemory access violation
{{!DEBUG!}}
>Font:Void(font:mojo.graphics.Font);/home/wiebo/Monkey2/modules/mojo/graphics/canvas.monkey2;1110018;4431205
font:mojo.graphics.Font=@0x1848420
>new:Void();/home/wiebo/Monkey2Stuff/boulderdash/bd.monkey2;110594;3342060
>Main:Void();/home/wiebo/Monkey2Stuff/boulderdash/bd.monkey2;147457;89When I remove the canvas:Canvas field from the Class, the program compiles, but ofcourse it does not work. I was trying this to see if I can get to the canvas in the window. Still no luck
After killing the application I get the rest of the output:
***** Fatal mx2cc error *****
Build error.
:build_project_debug FAILEDBUILD FAILED
FAILURE: Build failed with an exception.
* Where:
Build file ‘/home/wiebo/Monkey2Stuff/boulderdash/build.gradle’ line: 16* What went wrong:
Execution failed for task ‘:build_project_debug’.
> Build project failed with exit code: 255* Try:
Run with –stacktrace option to get the stack trace. Run with –info or –debug option to get more log output.Total time: 23.415 secs
[Finished in 23.8s with exit code 1]June 9, 2016 at 4:26 am #999wiebow:
Your ‘Field canvas:Canvas’ is not initialized by using ‘New’
or a getter function/method. Means ‘canvas’ is Null, and you
write to canvas.FontIf ‘i = Image.Load()’ returns Null, accessing ‘i.Handle’ would
also crash (at least if they are Classes, not so with Structs).June 9, 2016 at 7:00 am #1000Try something like this…
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960Namespace test#Import "<std>"#Import "<mojo>"Using std..Using mojo..#Import "assets/sprites.png"#Import "assets/arcade.ttf"Class MyTest Extends WindowField initialized:BoolField i:ImageMethod New()' this sets title. no other way to do this.'Super.New("Test!!!")Title = "Test!!!" ' Title is a property in class Windowi = Image.Load("asset::sprites.png")' Check for NullIf i <> Nulli.Handle = New Vec2f(0.0, 0.5)Endif'Later in MX2, the Null-Check would be as easy as that:' i?.Handle = New Vec2f(0.0, 0.5)ClearColor=New Color( 1,1,0 )End MethodMethod Init(canvas:Canvas)canvas.Font = Font.Load( "asset::arcade.ttf", 10 )initialized = TrueEnd MethodMethod OnRender( canvas:Canvas ) OverrideIf Not initialized Then Init(canvas)App.RequestRender()End MethodEnd ClassFunction Main:Void()New AppInstanceNew MyTestApp.Run()End FunctionJune 9, 2016 at 7:22 am #1001Sorry Mark, I have another one.
No problem – I want to get the bugs out ASAP.
It is actually working as expected though. The program successfully compiles and runs but generates a ‘memory access violation’ due to the null pointer, so it’s not the compiler crashing at least. And on the 97% finished version of Ted2 I’ve got running here, the debugger pops up and you can actually see where the error is – what luxury!
When I remove the canvas:Canvas field from the Class, the program compiles
Doesn’t here – I get ‘identifier canvas not found’ (as I would have expected).
I was trying this to see if I can get to the canvas in the window.
You can’t. The render canvas is ‘special’, and I want to make sure it’s state is not changed behind my back, and that I can change it on the fly if necessary (ie: you may not always get the same canvas in ‘OnRender’) for future features.
You can always render to an ‘image canvas’ and just DrawImage that to the render canvas in OnRender. This allows you to ‘draw’ at any time, and to apply funky effects at OnRender time like fade out etc. I’ll probably end up adding a CanvasView that does just that eventually, but the whole view system needs cleaning up and doccing before that’s realistic, so that’s an after V1.0 thing.
June 9, 2016 at 7:35 am #1002Mark Sibly wrote:
You can’t. The render canvas is ‘special’, and I want to make sure it’s state is not changed behind my back, and that I can change it on the fly if necessary (ie: you may not always get the same canvas in ‘OnRender’) for future features.
So, if the canvas doesn’t preserve all states, it would be:
Monkey12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455Namespace test#Import "<std>"#Import "<mojo>"Using std..Using mojo..#Import "assets/sprites.png"#Import "assets/arcade.ttf"Class MyTest Extends WindowField i:ImageField myFont:FontMethod New()' this sets title. no other way to do this.'Super.New("Test!!!")Title = "Test!!!" ' Title is a property in class Windowi = Image.Load("asset::sprites.png")' Check for NullIf i <> Nulli.Handle = New Vec2f(0.0, 0.5)Endif'Later in MX2, the Null-Check would be as easy as that:' i?.Handle = New Vec2f(0.0, 0.5)ClearColor=New Color( 1,1,0 )myFont = Font.Load( "asset::arcade.ttf", 10 )End MethodMethod OnRender( canvas:Canvas ) Overridecanvas.Font = myFontApp.RequestRender()End MethodEnd ClassFunction Main:Void()New AppInstanceNew MyTestApp.Run()End FunctionJune 9, 2016 at 8:06 am #1003That’d work.
Also, not sure if it’s docced yet but each view also has a ‘Style’ object that has DefaultColor and DefaultFont properties. These are set just before OnRender is called, so you can provide a default font and color for OnRender, eg:
Monkey1234Method New() 'Window ctorStyle.DefaultFont=...Style.DefaultColor=...EndJune 9, 2016 at 8:08 am #1005This makes sense now. Thanks. The inner workings of Monkey 2 and mojo are most interesting to explore
@Mark: Oh that default style looks interesting. I’ll go dive into the style code. Thanks.
@Danillo: Thanks for the tip about setting the font in the render method. It’s something I can live with!
-
AuthorPosts
You must be logged in to reply to this topic.