Unable to display SVG in angengine - java

I am trying to render a SVG to the screen using andengine, but unfortunately it is not being displayed. Also, I am not getting any exceptions or errors while running the project, thus I'm finding it very hard to debug.
I've had a look at this post but I guess BlackPawnTextureBuilder is not available on GLES2.
I've included part of my code below,
public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws Exception
{
BuildableBitmapTextureAtlas buildableBitmapTextureAtlas = new BuildableBitmapTextureAtlas(this.getTextureManager(), 2048, 2048, TextureOptions.BILINEAR);
logoSplashITextureRegion = SVGBitmapTextureAtlasTextureRegionFactory.createFromAsset(buildableBitmapTextureAtlas, this, "gfx/BrickRed.svg", 80, 40);
buildableBitmapTextureAtlas.load();
pOnCreateResourcesCallback.onCreateResourcesFinished();
}
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws Exception
{
logoSplashScene = new Scene();
logoSplashSprite = new Sprite(0, 0, logoSplashITextureRegion,mEngine.getVertexBufferObjectManager())
{
#Override
protected void preDraw(GLState pGLState, Camera pCamera)
{
super.preDraw(pGLState, pCamera);
pGLState.enableDither();
}
};
logoSplashSprite.setPosition((CAMERA_WIDTH - logoSplashSprite.getWidth()) * 0.5f, (CAMERA_HEIGHT - logoSplashSprite.getHeight()) * 0.5f);
logoSplashScene.attachChild(logoSplashSprite);
pOnCreateSceneCallback.onCreateSceneFinished(BrickActivity.logoSplashScene);
}
Am I forgetting something in the code?
Thanks in advance for you help.

I finally managed to make it work.
It seems like as I'm using GLES2 I had to include the following line just before I load my textureAtlas.
buildableBitmapTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 1, 0));
Now my onCreateResources looks like this,
try
{
buildableBitmapTextureAtlas = new BuildableBitmapTextureAtlas(textureManager, 2048, 2048, TextureOptions.BILINEAR);
SVG redBrick = SVGParser.parseSVGFromAsset(assertManager, "gfx/BrickRed.svg");
iTextureRegion = SVGBitmapTextureAtlasTextureRegionFactory.createFromSVG(buildableBitmapTextureAtlas, redBrick, 80, 40);
buildableBitmapTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 1, 0));
buildableBitmapTextureAtlas.load();
} catch (TextureAtlasBuilderException e)
{
e.printStackTrace();
}

Related

OpenGL functions give FunctionNotSupported error

I am trying to make a 2D OpenGL project using LWJGL 2, and I am having trouble with basic rendering.
Main class:
public class ScratchCord {
private Renderer renderer;
public TextureManager manager;
private int i = 0;
private int i2 = 0;
private ScratchCord() {
renderer = new Renderer(this);
manager = new TextureManager();
ContextAttribs attribs = new ContextAttribs(3, 2).withForwardCompatible(true).withProfileCore(true);
try {
Display.setDisplayMode(new DisplayMode(1240, 740));
Display.create(new PixelFormat(), attribs);
Display.setTitle("ScratchCord");
} catch (LWJGLException e) {
e.printStackTrace();
}
GL11.glViewport(0, 0, 1240, 740);
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 1240, 740, 0, -1, 1);
}
private void start() {
while (!Display.isCloseRequested()) {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
renderer.draw(i, i2, "ScratchCord/src/main/resources/textures/download.jpg", 200, 200);
Display.sync(60);
Display.update();
i += 1;
i2 += 1;
}
}
public static void main(String[] args) {
System.setProperty("org.lwjgl.util.NoChecks","true");
ScratchCord scratchCord = new ScratchCord();
scratchCord.start();
}
}
For some reason, I get a FunctionNotSupported error, but the code works in http://www.cokeandcode.com/info/tut2d-4.html.
I tried setting the OpenGl version to 2.0, and I got an error in Display.create and the OpenGL context was not created.
I've heard that sometimes graphics drivers can break LWJGL, but mine are updated and other projects I have works.
Also some people say that the function just isn't in 3.2, and if so how would I do this?
Your machine likely does not support Legacy GL, making it a "Core" context. Core contexts have deprecated legacy functions, like matrix operations, unavailable.
Find a tutorial that is for modern core opengl.

AndEngine can not shows sprite

In Android Studio, I'm trying to create a sprite with AndEngine, but it failed to display the sprite, it only shows a black screen. I'v searched and tried different methods for hours.
Here is the structure:
-myApplication
-andEngine
-app
-res
-drawable
-stand.png
Here is the codes:
#Override
public void onCreateResources() {
this.mBitmapTextureAtlas = new BitmapTextureAtlas(getTextureManager(), 30, 30, TextureOptions.DEFAULT);
mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromResource(mBitmapTextureAtlas, this, R.drawable.stand, 0, 0);
mBitmapTextureAtlas.load();
}
#Override
public Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
this.mMainScene = new Scene();
this.mMainScene.setBackground(new Background(1, 1, 1));
final Sprite oPlayer = new Sprite(100, 100, mPlayerTextureRegion, getVertexBufferObjectManager());
this.mMainScene.attachChild(oPlayer);
return this.mMainScene;
}
Any help will be appreciated
Thanks.
Check your logcat it's may be an exception.
As I check your code, this line may be reason of exception.
mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromResource(mBitmapTextureAtlas, this, R.drawable.stand, 0, 0);
Check dimension of your stand.png it should not greater than 30*30 otherwise pass greater value, when you're creating object of BitmapTextureAtlas.

Inherent damping in LibGdx DistanceJoint

I am trying to learn LibGdx and simulate a Spring Mass connection. I am able to define everything correctly and run the simulation but the spring defined seems to have an inherent damping dur to which the oscillations of the dynamic body die out.
I have set the damping ratio to zero but still get the decay.
Can some one explain if this is a bug or am i doing something wrong.
Below are my codes.
SpringMass class:
public class SpringMass implements Screen {
private void createScene() {
trackers = new ArrayList<Tracker>();
isTracking = new ArrayList<Boolean>();
world.setGravity(new Vector2(0, 0));
Vector2 boxPos = new Vector2(200, 200);
staticBox = new Box(world, boxPos, 10, 10, false);
dynBox = new Box(world, boxPos.add(150, 0), 10, 10, true);
new Spring(staticBox.body, dynBox.body, world, 100, 0.8f, 0);
}
}
Spring Class:
public class Spring {
private DistanceJointDef distanceJointDef;
public Spring(Body bodyA, Body bodyB, World world, float length, float stiffness, float dampingRatio) {
distanceJointDef = new DistanceJointDef();
distanceJointDef.bodyA = bodyA;
distanceJointDef.bodyB = bodyB;
distanceJointDef.length = length*CONSTANTS.WORLD_TO_BOX;
distanceJointDef.frequencyHz = stiffness;
distanceJointDef.dampingRatio=dampingRatio;
world.createJoint(distanceJointDef);
}
}

LWUIT Painter : How to make an image the background?

I am trying to use Painter to make a certain jpg become my background.
mapScreen = new Form("Map");
try
{
Image image = Image.createImage("/res/try.jpg");
map = new Map(image);
mapScreen.addComponent(map);
} catch (Exception e)
{
System.out.print("Error\n\n"+e.getMessage());
mapScreen.addComponent(new Label(e.getMessage()));
}
And for the map class,
public Map(Image image)
{
this.mapImage = image;
painter = new Painter()
{
public void paint(Graphics g, Rectangle clippingRect)
{
g.clipRect(0, 0, getWidth(), getHeight());
g.drawImage(mapImage, getX(), getY());
}
};
}
public void initComponent()
{
setX(0);
setY(0);
getSelectedStyle().setBgTransparency(0);
getSelectedStyle().setBgPainter(painter);
getUnselectedStyle().setBgTransparency(0);
getUnselectedStyle().setBgPainter(painter);
}
The problem with this is that the image doesn't show up at all and when I try to debug, It doesn't even enter the paint(Graphics g, Rectangle clippingRect)...
The code
try
{
Image image = Image.createImage("/res/try.jpg");
map = new Map(image);
mapScreen.addComponent(map);
}
is successful.
Can anyone tell me how to do it properly?
And also, if anyone know how to do panning on an image larger than the size of the screen, Can you help me with that also? Thanks.
Use setBgTransparency to 255 and don't call the clipRect method.
You can look at the bg painter code within Component.java which is pretty flexible.

Loading Obj files in Libgdx not working on android

The following code shows a torus slowly revolving and coming into display:
package com.objloader.example;
import ...
public class ObjLoaderProg implements ApplicationListener{
String torus;
Mesh model;
private PerspectiveCamera camera;
#Override
public void create() {
InputStream stream=null;
try {
stream = new FileInputStream(Gdx.files.internal("data/torus.obj").path());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
model = ObjLoader.loadObj(stream, true);
Gdx.gl.glEnable(GL10.GL_DEPTH_TEST);
Gdx.gl10.glTranslatef(0.0f,0.0f,-3.0f);
}
#Override
public void dispose() {
}
#Override
public void pause() {
}
protected int lastTouchX;
protected int lastTouchY;
protected float rotateZ=0.01f;
protected float increment=0.01f;
#Override
public void render() {
Gdx.gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
camera.update();
camera.apply(Gdx.gl10);
Gdx.gl10.glTranslatef(0.0f,0.0f,-3.0f);
Gdx.gl10.glRotatef(rotateZ, rotateZ, 5.0f, rotateZ);
model.render(GL10.GL_TRIANGLES);
if (Gdx.input.justTouched()) {
lastTouchX = Gdx.input.getX();
lastTouchY = Gdx.input.getY();
} else if (Gdx.input.isTouched()) {
camera.rotate(0.2f * (lastTouchX - Gdx.input.getX()), 0, 1.0f, 0);
camera.rotate(0.2f * (lastTouchY - Gdx.input.getY()), 1.0f, 0, 0);
lastTouchX = Gdx.input.getX();
lastTouchY = Gdx.input.getY();
}
rotateZ+=increment;
System.out.println(""+rotateZ);
}
#Override
public void resize(int arg0, int arg1) {
float aspectRatio = (float) arg0 / (float) arg1;
camera = new PerspectiveCamera(67, 2f * aspectRatio, 2f);
camera.near=0.1f;
camera.translate(0, 0, 0);
}
#Override
public void resume() {
}
}
It renders a torus obj that's saved in the data folder, and by clicking and dragging on the screen the user can rotate the camera.
This works fine on the desktop, but when I try to run it on android, I get a NullPointerException at:
model.render(GL10.GL_TRIANGLES);
I've tried placing torus.obj just inside assets, and within assets/data. I'm using libgdx 0.9.2.
I assume you are referring to model.render(GL10.GL_TRIANGLES);. I believe you have two problems. First, model is null because you are catching a FileNotFoundException and ignoring it. I suggest you don't catch the FileNotFoundException right now, let it crash, and look at the stack trace. That will give you a better indication of why this failing. Note that e.printStackTrace() is not useful for debugging on android, try using the gdx log.
The second problem is that I suspect the path/stream is actually going to the wrong place. Instead of creating a FileInputStream, use the FileHandle.read() function. It returns a java.io.InputStream that you can pass to ObjLoader.loadObj().
in = Gdx.files.internal("data/torus.obj").read();
model ObjLoader.loadObj(in);
in.close();
The difference between this and your code is that FileHandle.read() in libgdx's android backend uses the android AssetManager to open files that are bundled with the program.
Finally, copy the torus.obj file to <YourAndroidProject>/assets/data/torus.obj.
An aside: if you specify the line number, please provide the full file otherwise the line will be off. Line 52 of the code you have provided is: lastTouchY = Gdx.input.getY();. Note the "import ..." at the beginning of your code. Those imports affect the line number.

Categories