can't get loadimage to work

meems

New Member
Joined
Feb 28, 2018
Code:
Import mojo
Function Main()
Local b:Image=LoadImage("block.png")
End
just trying to get 1st program to acknowledge an image
get surface null error.
I've stuffed the block.png in every directory that I can think cerebus would look for it.
 

MikeHart

Administrator
Staff member
Joined
Jun 19, 2017
Location
Germany
Hi.

Code:
Strict

Import mojo

Class myGame Extends App
    Field b:Image

    Method OnCreate:Int()
        SetUpdateRate(0)
        b = LoadImage("block.png")
        Return 0
    End

    Method OnUpdate:Int()
        Return  0
    End

    Method OnRender:Int()
        DrawImage(b, 400, 300)
        Return 0
    End
End

Function Main:Int()
    New myGame
    Return 0
End
Make sure the image is stored inside the yourGameName.data folder.
 
Last edited:

meems

New Member
Joined
Feb 28, 2018
thx. it works now. don't know why my much simpler code didn't work though. I'm guessing images can't be loaded until app is loaded, in the same way images can't be loaded in blitz until graphics is initiated
 

Gerry Quinn

Active Member
Joined
Jun 24, 2017
Yes, I'm not certain but it's quite likely that you need to create an app-derived class to get all the mojo infrastructure such as LoadImage working. [Edit: posted at the same time - looks like this was indeed the answer.]

[You can run plain Monkey programs from Main(), e.g. printing out Fibbonaci numbers to the console or something.]
 

MikeHart

Administrator
Staff member
Joined
Jun 19, 2017
Location
Germany
When it comes to mojo and mojo2, you need to extend your own class from the App class and work with that.

Using only the Main function, well only of Console apps and some text printing in the HTML target. Or you run your own graphics/input/sound/etc module. OpenGL would go too I guess, but then you are missing input and stuff like that.
 

PixelPaladin

Active Member
CX Code Contributor
3rd Party Module Dev
Joined
Aug 27, 2017
Location
Germany
@meems: Images should be loaded in App.OnCreate() (or later).
The reference page of mojo.app says the following:
The OnCreate method is called when mojo has been initialized and the application has been successfully created.
At this point, applications can start loading resources such as images and sounds, and set the update rate using SetUpdateRate.
The return value of this method is currently ignored, but should be 0 for future compatibility.
 

MikeHart

Administrator
Staff member
Joined
Jun 19, 2017
Location
Germany
No need to add to this topic. After his short visit here he announced on Syntaxbomb that he won't use CX. To advanced for him.
 

meems

New Member
Joined
Feb 28, 2018
Mike you are an all seeing eye. But I'm fickle, and its early days in my move to bmax. I'm learning new stuff everyday. Who knows, tomorrow I might become aware of something about bmax or cerebus that convinces me to use cerebus. Take heart that cerebus is #2 on my list after bmax-ng.

now excuse me while I write out a rant on syntax bomb about case sensitive languages!
 
Top Bottom