LibGDX - How to use a skin after loading it in the assetManager - java

I'm having trouble using the skin once I've loaded it in the assetManager.
assetManager.load(skinPath+".atlas", TextureAtlas.class);
assetManager.finishLoading();
assetManager.load(skinPath+".json", Skin.class, new SkinLoader.SkinParameter(skinPath+".atlas"/*,Resources*/));
assetManager.finishLoading();
skin = skin.get(skinPath+".json", Skin.class);
The last line of code throws java.lang.NullPointerException, does anyone know why?

skin = skin.get(UIAssetsPath+skinName+".json", Skin.class);
skin.get() is your issue here. As skin is null and I am not sure why are you trying to get skin from a skin when you have your asset loader.
You need to call
assetManager.get(UIAssetsPath+skinName+".json", Skin.class);
Also suggest you reading more about exceptions. This seems a very beginner mistake.

Related

GdxRuntimeException: Asset not loaded

I'm following this tutorial, but instead if xml i use json.
I think that the asset is loaded but apparently it's not! Would be nice if one of you have any solution.
The code is on github.
That is the output:
SUCCESS!
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Asset not loaded: core\assets\badlogic.jpg
at com.badlogic.gdx.assets.AssetManager.get(AssetManager.java:150)
at epytotorp.managers.AssetManager.get(AssetManager.java:78)
at epytotorp.Game.create(Game.java:18)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:149)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:126)
Process finished with exit code 0
I tryed many thinks but none of them worked can you pls help me.
Many thanks in advance.
With the Java libraries for dealing with files, you can safely use / (slash, not backslash) on all platforms. The library code handles translating things into platform-specific paths internally.
I made some modification in your create() method of your Game game and then it's works.
#Override
public void create() {
AssetManager.getInstance().initialize("core/assets/test.json");
AssetManager.getInstance().load("group 1");
AssetManager.getInstance().finishLoading();
System.out.println("SUCCESS!");
Texture texture = AssetManager.getInstance().get("core/assets/badlogic.jpg", Texture.class);
System.out.println("Texture"+texture);
ScreenManager.getInstance().initialize(this);
ScreenManager.getInstance().setScreen(ScreenEnum.SPLASH);
}

Sharing resources between OpenGL context's on Android

I am trying to use multiple EGL contexts to load textures outside of my main thread. I'm getting an EGL_BAD_CONTEXT error after my eglCreateContext call.
Inside my android.opengl.Renderer
public void onSurfaceCreated (javax.microedition.khronos.opengles.GL10 gl, EGLConfig config) {
// ...
EGLContext sharedContext = egl.getCurrentContext();
EGLDisplay display = eglGetCurrentDisplay();
eglCreateContext(display, config, sharedContext, new int[] { EGL_CONTEXT_CLIENT_VERSION, 2 } );
}
The EGL_BAD_CONTEXT lead me to the documentation here, that says
EGL_BAD_CONTEXT is generated if share_context is not an EGL rendering context of the same client API type as the newly created context and is not EGL_NO_CONTEXT.
That's why I added in the EGL_CONTEXT_CLIENT_VERSION parameter, but it seems to have made no effect.
What I'm seeing is that, even though I'm getting this error, the context seems semi-valid. I'm able to use it on another thread
egl.eglMakeCurrent(display, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE, context);
After this, creating textures on that thread does not cause an error. But I do see that the texture names are not shared, each thread seems to count up from 0 itself.
My next assumption was that I need to share the surface between contexts. But, if I pass through the same surface from the original context into my eglMakeCurrent, but I fail completely with
E/AndroidRuntime(3210): java.lang.IllegalArgumentException
E/AndroidRuntime(3210): at com.google.android.gles_jni.EGLImpl._eglCreateContext(Native Method)
E/AndroidRuntime(3210): at com.google.android.gles_jni.EGLImpl.eglCreateContext(EGLImpl.java:54)
I feel as though I'm almost there, does somebody know what's missing?
Turns out, thanks to some help from this question:
My secondary context requires a surface. It is not the same as the original context's surface.
I needed to create that new surface using eglCreatePbufferSurface. The reason my attempts with this had failed before is that it defaults to a width and height of 0. By setting that to a 1x1 surface, it worked perfectly.
egl.eglCreatePbufferSurface(display, config, new int[] { EGL10.EGL_WIDTH, 1, EGL10.EGL_HEIGHT, 1, EGL10.EGL_NONE });

Libgdx create a TextButton with background with uiskin.json

I want to define the background of TextButton with uiskin.json.
Here is what i tried but didn't work:
com.badlogic.gdx.scenes.scene2d.ui.Skin$TintedDrawable: {
img: { file: bg.png }
},
com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle: {
default: { down: default-round-down, up: img, font: default-font, fontColor: white }
}
So I want to make bg.png as the default background. How can i do it?
Skin cannot read file locations from the json. If you followed tutorials for Skin, you probably instantiated it with a TextureAtlas like this:
skin = new Skin(skinFilePath, textureAtlas);
When you load it like that, all the images in the json must be available by their names in the TextureAtlas.
Normally, you need all your images to be in a single TextureAtlas, for performance reasons. So the best solution would be to add this bg.png image into your TextureAtlas, and then you can refer to it by its TextureAtlas name.
If you must load it as a separate file, then you must manually load it before loading your skin.
Texture bgTexture = new Texture("bg.png");
skin = new Skin(); //empty constructor causes it not to load yet.
skin.add("bg", bgTexture);
skin.addRegions(textureAtlas); // you will be responsible for disposing the atlas separately from the skin now.
skin.load(skinFilePath);

Google Maps Utils IconGenerator text styles and background

I just started using Google Maps Utils library so that I can have markers with text in them. I understand the library so far and I'm successfully using it. Code snippet:
IconGenerator icnGenerator = new IconGenerator(this);
Bitmap iconBitmap = icnGenerator.makeIcon(item.placeItemName);
myPlaceItemMarkers.add(mMap.addMarker(new MarkerOptions().position(new LatLng(item.latitude, item.longitude))
.icon(BitmapDescriptorFactory.fromBitmap(iconBitmap)).anchor(0.5f, 0.6f)));
Now I started playing around with styling the window a bit and there are two functions that interest me in particular:
icnGenerator.setBackground(Drawable background);
icnGenerator.setTextAppearance(int resid);
I looked up for info on docs and there's only info for BubbleIconFactory which is deprecated. Could someone tell me how to use these 2 functions? I know setTextAppearance is for changing text style but how do I do it? And if I'm not wrong setBackground is for setting custom marker background, but I don't know how to use that either.
I'm still not completely sure how to use setBackground() because I tried using Drawables but it wouldn't work but I figured out how to use setTextAppearance().
I just made a new style:
<style name="iconGenText">
<item name="android:textSize">14sp</item>
<item name="android:textColor">#000000</item>
</style>
And then applied it to IconGenerator:
IconGenerator icnGenerator = new IconGenerator(this);
icnGenerator.setTextAppearance(R.style.iconGenText);
It works as it should.
Set the background like this:
IconGenerator icnGenerator = new IconGenerator(this);
icnGenerator.setBackground(getResources().getDrawable(R.drawable.marker_background));
marker_background has to be a .9.png file. Like the drawables of the library.
It works pretty good for me.
if you're just wanting to change away from the default marker styling without getting fancy you can do this.
icnGenerator.setStyle(IconGenerator.STYLE_BLUE)
There's currently 7 basic styles to select from:
IconGenerator.STYLE_BLUE
IconGenerator.STYLE_DEFAULT
IconGenerator.STYLE_GREEN
IconGenerator.STYLE_ORANGE
IconGenerator.STYLE_PURPLE
IconGenerator.STYLE_RED
IconGenerator.STYLE_WHITE
doc: https://www.javadoc.io/doc/com.google.maps.android/android-maps-utils/latest/com/google/maps/android/ui/IconGenerator.html

Libgdx reload scene2d skin after application hidden

I'm having an issue with libgdx skin.
When the app is paused or goes into the background and then reopens all my scene2d textures are just showing up black. I assume that the underlying textures need to be reloaded.
I'm creating my skin using the following code
FileHandle jsonFile = Gdx.files.internal( "ui/uiskin.json" );
FileHandle atlasFile = Gdx.files.internal( "ui/uiskin.atlas" );
TextureAtlas atlas = new TextureAtlas(atlasFile);
skin = new Skin(jsonFile, atlas);
My question is if there any way that I can detect when the texture needs to be reloaded and how to best do this?
I have found that it is best to rebuild the skin when you reopen your app.

Categories

Resources