NiftyGUI on Android

Hi there!

I’m really confused about this, because i think i’ve done everything right.
I want to create some Nifty GUI.
Here is the XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<nifty xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://nifty-gui.lessvoid.com/nifty-gui" xsi:schemaLocation="https://raw.githubusercontent.com/void256/nifty-gui/1.4/nifty-core/src/main/resources/nifty.xsd https://raw.githubusercontent.com/void256/nifty-gui/1.4/nifty-core/src/main/resources/nifty.xsd">
<useControls filename="nifty-default-controls.xml"/>
<useStyles filename="nifty-default-styles.xml"/>
<screen id="start" controller="tower.WorldState">
    <layer id="GLayer0" childLayout="absolute">
        <control name="label" id="GLabel0" width="82px" x="9" y="11" text="HP: 50" font="aurulent-sans-16.fnt" height="26px"/>
        <control name="label" id="GLabel1" width="82px" x="9" y="24" text="SCORE: 0" font="aurulent-sans-16.fnt" height="38px"/>
    </layer>
</screen>

As you can see, nothing fancy here.

And in an AppState I do the following:

@Override
public void initialize(AppStateManager stateManager, Application app) {
    super.initialize(stateManager, app);
    this.app = app;
    NiftyJmeDisplay disp = NiftyJmeDisplay.newNiftyJmeDisplay(Assets.manager(), app.getInputManager(), app.getAudioRenderer(), app.getGuiViewPort());
    nifty = disp.getNifty();
    nifty.fromXml("Interface/ingame.xml", "start", this);
    nifty.gotoScreen("start");
    app.getGuiViewPort().addProcessor(disp);
    ...

On my desktop this works wonderfully, but on my (little bit old, Sony Xperia U, Android 4.0.3) Android phone i get the following exception right at startup:

E/dalvikvm-heap( 3656): Out of memory on a 16777232-byte allocation.
I/dalvikvm( 3656): "GLThread 861" prio=5 tid=11 RUNNABLE
I/dalvikvm( 3656):   | group="main" sCount=0 dsCount=0 obj=0x4158aec0 self=0x16de918
I/dalvikvm( 3656):   | sysTid=3674 nice=0 sched=0/0 cgrp=default handle=23426176
I/dalvikvm( 3656):   | schedstat=( 9109161344 1914367710 12563 ) utm=848 stm=62 core=0
I/dalvikvm( 3656):   at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
I/dalvikvm( 3656):   at java.nio.MemoryBlock.allocate(MemoryBlock.java:126)
I/dalvikvm( 3656):   at java.nio.ReadWriteDirectByteBuffer.<init>(ReadWriteDirectByteBuffer.java:46)
I/dalvikvm( 3656):   at java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:68)
I/dalvikvm( 3656):   at com.jme3.util.BufferUtils.createByteBuffer(BufferUtils.java:997)
I/dalvikvm( 3656):   at com.jme3.niftygui.JmeBatchRenderBackend.createTextureAtlas(JmeBatchRenderBackend.java:189)
I/dalvikvm( 3656):   at de.lessvoid.nifty.render.batch.BatchRenderDevice.createTextureAtlas(BatchRenderDevice.java:525)
I/dalvikvm( 3656):   at de.lessvoid.nifty.render.batch.BatchRenderDevice.createInitialTextureAtlases(BatchRenderDevice.java:519)
I/dalvikvm( 3656):   at de.lessvoid.nifty.render.batch.BatchRenderDevice.<init>(BatchRenderDevice.java:151)
I/dalvikvm( 3656):   at com.jme3.niftygui.NiftyJmeDisplay.<init>(NiftyJmeDisplay.java:230)
I/dalvikvm( 3656):   at com.jme3.niftygui.NiftyJmeDisplay.newNiftyJmeDisplay(NiftyJmeDisplay.java:150)
I/dalvikvm( 3656):   at tower.WorldState.initialize(WorldState.java:54)

Without these few lines the whole app runs fine.
Am i missing something trivial about it (as usual)? Or what should i check?
Any help would be appriciated.

Just looking at the stacktrace, I get a feeling that the batched Nifty renderer’s default image size might be too big. Try making it smaller. The default size was 2048x2048 I think. Try 512x512 etc.

Thank you, you were absolutely right! I did not know where i could change that, but some googleing helped.
For future readers:

    BatchRenderConfiguration conf = new BatchRenderConfiguration();
    conf.atlasHeight = 256;
    conf.atlasWidth = 256;
    NiftyJmeDisplay disp = NiftyJmeDisplay.newNiftyJmeDisplay(Assets.manager(), app.getInputManager(), app.getAudioRenderer(), app.getGuiViewPort(), conf);

Again, thank you. :slight_smile: