[libGdx] Loading screen

Hey,

how do I render something during create() in libGdx?

I have a rather long map generation process and all I want to show (for now) is basically a BitmapFont that says “loading”, so in create() I


bmf = new BitmapFont();
sb = new SpriteBatch();
sb.begin();
bmf.draw(sb, "Loading", 100, 100);
sb.end();

// map.init();


but this gets never shown. Is there a way to force this to be rendered? I’m probably missing a function here or something :slight_smile:

Thanks in advance

I’m not entirely sure of how libGDX works, but I think there is a function you have to call to update the display. (Like Display.update() in pure LWJGL)

I think so too, I just don’t know the function :frowning:
Gdx.graphics.requestRendering()
sounded good, but doesn’t do anything :slight_smile:

 Gdx.graphics.requestRendering() 

It is asking for render if you’re on passive mode. Really has nothing to do with your question.

What you need:
http://code.google.com/p/libgdx/wiki/AssetManager

If you want show something rather than plain text on loading screen, separate it from main bloated atlas and load it first before anything else. Make sure it is light so you can load it from loading screen’s constructor. After the manager done, process to your game.

Thank you, but I don’t really want to use the AssetManager, as I do not care about asynchronous loading or reference counting. I have my own class for texture keeping, which probably wasn’t very smart but whatever.

Is there no other way to update the display in libGdx during create()?

Stupid workaround is to render 1 frame that says “loading” in render() then load the rest of the stuff from create(), but it feels and looks so bad. sigh

Maybe this will help.

Thanks but no :slight_smile:
I can render a bitmap font just fine. I just can’t render it (or anything for that matter) during create() :frowning:

I’ve not used libGDX, so I might be (very) wrong, but seems like create() is some sort of construct? So why not perform your map generation outside of that function?

Something like:

create();
showLoadingScreen();
generateMap();

I could do that, but then I’d have to have that code in the render() function of libGdx, which does work, sure, but it looks a bit strange in my opinion to have a “load-something” function as part of the render()-function of libgdx.

I might just be picky here tho, and this is the way to go about it seeing as libGdx doesn’t provide a function to just present the screen or update the display (as lwjgl does)

Sounds like what you want/need is to have multiple states/screens/whatever-you-want-to-call it.

so something like:


activeScreen.render();

Where activeScreen is, in this case, an instance of Screen which handles how it’ll be drawn. Then you can generate the map while that screen is active? (Horrible explanation, right here! :P)

I get it and you’re right, that is probably the best solution :smiley:
Although I still feel libGdx should implement a function like display.update(); :frowning:

That’s at least how I do it myself. :slight_smile:

Maybe you should consider asset manager and use something like this:

AssetManager is how you do it. This allows you to render while your resources are loaded on a separate thread. Otherwise I guess you could load minimal stuff in create, render a frame, then load the rest.

I guess he don’t want to do the load/unload part, or already have his own manager.

Yeah the problem is, that it isn’t just textures or sounds or other resources that I need to load. I have a rather long (and bad) map-creation code and I just wanted to display a text like “loading” before creating the map.

However FYI AssetManager supports custom class too. You just need to register it :smiley:

Using the AssetManager does not solve OP’s problem. It’s even completely unrelated to a ‘map generation process’, so I’m not sure why it’s being brought up here repeatedly.

Basically you need to call the render loop at least twice to do this: once to render the loading screen, once to also do the map generating and other time-intensive stuff. For example:

private boolean rendered = false;

public void render() {
   if (!rendered){
      // Do nothing for one update to make sure something has been rendered
      rendered = true;
   } else {
      // Now that we had at least one render finished, do some time-intensive stuff
      map.init();
   }
   sb.begin();
   bmf.draw(sb, "Loading", 100, 100);
   sb.end();
}

Edit: in other words, you cannot do this in create().

This is the price you pay for getting multiplatform compatibility I think.

Thank you, that is how I solved it too currently. I just think it’s not very sexy to have a function that loads(/generates) stuff inside the render() function.