AndEngine scene load resources - android

Help me please, wat's wrong in my code? On device shown black background.
public void onLoadResources()
{
this.mTexture = new Texture(1024, 1024);
this.mTextureRegion = TextureRegionFactory.createFromAsset(this.mTexture, this, "gfx/bgr.png",0,0);
this.getEngine().getTextureManager().loadTexture(this.mTexture);
}
#Override
public Scene onLoadScene()
{
final Scene scene = new Scene(1);
backLayer=new Sprite(0,0,this.mTextureRegion);
scene.getTopLayer().addEntity(backLayer);
return scene;
}

I have a few fixes for you:
Don't use the constructor Scene(int), its deprecated. Use Scene() instead.
By your sprite's name, I guess it is your scene background? If this is your intention, you should use this: scene.setBackground(new SpriteBackground(backLayer));, instead of scene.getTopLayer().addEntity(backLayer);.
Lastly, I didn't see the method createFromAsset in TextureRegionFactory. Maybe you should update your AndEngine classes? And try this instead, might work:
BitmapTextureAtlas textureAtlas = new BitmapTextureAtlas(1024, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
this.mTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(textureAtlas, this, "bgr.png", 0, 0);

Related

AndEngine add single image multiple time in different size

I am new in AndEngine. I am using following code to display an image of a ball.
private ITextureRegion mBallTextureRegion;
private Sprite ball1;
#Override
public void onCreateResources() {
ITexture ball = new BitmapTexture(this.getTextureManager(),
new IInputStreamOpener() {
#Override
public InputStream open() throws IOException {
return getAssets().open("gfx/ball.png");
}
});
this.mBallTextureRegion = TextureRegionFactory.extractFromTexture(ring1);
....................
....................
}
#Override
protected Scene onCreateScene() {
final Scene scene = new Scene();
scene.attachChild(backgroundSprite);
...........
ball1 = new Sprite(192, 63, this.mBallTextureRegion, getVertexBufferObjectManager());
scene.attachChild(ball1);
..............
...........
}
Now, depending on the game level I want to add multiple ball of different size in the scene. Is it possible to add ITextureRegion mBallTextureRegion multiple time in different size(using different magnifying it)? If it is, then how? Please help me this sample code.
if you want to resize a Sprite,AnimatedSprite,Text,etc...
//the original image x2, 2f because the parameter is float
youSprite.setScale(2f);
if you use a texture region in more sprites:
Sprite youSprite;
//set deepCopy() in you texture to optimized memory
youSprite= new Sprite(0,0,youTexture.deepCopy(),mEnginge.getVertexTextureManager());
and if you want generate random position of each ball use "Random" variable.
best regards.

CCRenderTexture,GL11ExtensionPack,Libgdx How TO

I currently work on a effect such as "Tiny Wings" http://www.raywenderlich.com/3857/how-to-create-dynamic-textures-with-ccrendertexture ,and find CCRenderTexture is the solution. So I want to know how to make this effect on android , finally I found this link
https://github.com/ZhouWeikuan/cocos2d/blob/master/cocos2d-android/src/org/cocos2d/opengl/CCRenderTexture.java
It shows that its GL11ExtensionPack
GL11ExtensionPack egl = (GL11ExtensionPack)CCDirector.gl;
egl.glGetIntegerv(GL11ExtensionPack.GL_FRAMEBUFFER_BINDING_OES, oldFBO_, 0);
...
But in GLWrapperBase.java ,it shows
// Unsupported GL11ExtensionPack methods
public void glBindFramebufferOES (int target, int framebuffer) {
throw new UnsupportedOperationException();
}
Seems gdx have'nt implement this function . I want to know what's the same feature of libgdx or how to use GL11ExtensionPack at desktop ~
Thanks
In libGDX, you want to use a FrameBuffer object to do the equivalent of a "CCRenderTexture". The FrameBuffer basically lets you use OpenGL commands to draw into an off-screen buffer, and then you can display that buffer's contents as a texture later. See http://code.google.com/p/libgdx/wiki/OpenGLFramebufferObject. Note that the FrameBuffer object is only available if your app requires OpenGL ES 2.0.
Depending on what you want to draw, you might also look at the Pixmap class in libGDX. This supports some simple run-time drawing operations (like lines, rectangels, and pixels). Again the idea is that you draw into this texture and then render the resulting texture on-screen later. This is available in OpenGL ES 1.0, too.
Both FrameBuffer and Pixmap should work fine on Android and on the Desktop (and I believe on GWT and iOS, too..)
Be careful to understand what happens on Android when your app loses focus temporarily (OpenGL context loss causes some texture contents to disappear).
Question : CCRenderTexture,GL11ExtensionPack,Libgdx How TO
interpreted as : In libgdx, how to create dynamic texture.
Answer : Use a private render function to draw in a private frame
Example framework:
==================
package com.badlogic.gdx.tests.bullet;
/**
Question : CCRenderTexture,GL11ExtensionPack,Libgdx How TO
interpreted as : In libgdx, how to create dynamic texture?
Answer : Use a private render function to draw in a private frame buffer
convert the frame bufder to Pixmap, create Texture.
Author : Jon Goodwin
**/
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Pixmap;
...//(ctrl-shift-o) to auto-load imports in Eclipse
public class BaseBulletTest extends BulletTest
{
//class variables
=================
public Texture texture = null;//create this
public Array<Disposable> disposables = new Array<Disposable>();
public Pixmap pm = null;
//---------------------------
#Override
public void create ()
{
init();
}
//---------------------------
public static void init ()
{
if(texture == null) texture(Color.BLUE, Color.WHITE);
TextureAttribute ta_tex = TextureAttribute.createDiffuse(texture);
final Material material_box = new Material(ta_tex, ColorAttribute.createSpecular(1, 1, 1, 1),
FloatAttribute.createShininess(8f));
final long attributes1 = Usage.Position | Usage.Normal | Usage.TextureCoordinates;
final Model boxModel = modelBuilder.createBox(1f, 1f, 1f, material_box, attributes1);
...
}
//---------------------------
public Texture texture(Color fg_color, Color bg_color)
{
Pixmap pm = render( fg_color, bg_color );
texture = new Texture(pm);//***here's your new dynamic texture***
disposables.add(texture);//store the texture
}
//---------------------------
public Pixmap render(Color fg_color, Color bg_color)
{
int width = Gdx.graphics.getWidth();
int height = Gdx.graphics.getHeight();
SpriteBatch spriteBatch = new SpriteBatch();
m_fbo = new FrameBuffer(Format.RGB565, (int)(width * m_fboScaler), (int)(height * m_fboScaler), false);
m_fbo.begin();
Gdx.gl.glClearColor(bg_color.r, bg_color.g, bg_color.b, bg_color.a);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
Matrix4 normalProjection = new Matrix4().setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
spriteBatch.setProjectionMatrix(normalProjection);
spriteBatch.begin();
spriteBatch.setColor(fg_color);
//do some drawing ***here's where you draw your dynamic texture***
...
spriteBatch.end();//finish write to buffer
pm = ScreenUtils.getFrameBufferPixmap(0, 0, (int) width, (int) height);//write frame buffer to Pixmap
m_fbo.end();
// pm.dispose();
// flipped.dispose();
// tx.dispose();
m_fbo.dispose();
m_fbo = null;
spriteBatch.dispose();
// return texture;
return pm;
}
//---------------------------
}//class BaseBulletTest
//---------------------------

Add background to Andengine scene Android

I wanna set My Scene Background but I don't know how! I had read a lot about this, but I can't make this works. Is my start with Andengine, and is hard found precise information for my problem, all is subjective.
Well, I have implemented the splash screen in a scene, and while load all resources and scenes. (https://sites.google.com/site/matimdevelopment/splash-screen---easy-way)
Then, I have to set a Background to my menuScene, I think that I need a TextureRegion and a BitmapTextureAtlas to create each backgroud. I do this:
Declared textures:
//Fondo escenas
private TextureRegion menuBgTexture;
private BitmapTextureAtlas menuBackgroundTexture;
Load Resources and Load scenes (They are called by onPopulateScene when Splash ends)
public void loadResources()
{
//FondoMenu
menuBackgroundTexture = new BitmapTextureAtlas(null, 480, 320, TextureOptions.DEFAULT);
menuBgTexture = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.menuBackgroundTexture, this, "menubg.png", 0, 0);
//Cargamos los fondos
mEngine.getTextureManager().loadTexture(this.menuBackgroundTexture);
}
private void loadScenes()
{
//MenĂº
menuScene = new Scene();
final float centerX = (CAMERA_WIDTH - menuBgTexture.getWidth()) / 2;
final float centerY = (CAMERA_HEIGHT - menuBgTexture.getHeight()) / 2;
SpriteBackground bg = new SpriteBackground(new Sprite(centerX, centerY, menuBgTexture));
menuScene.setBackground(bg);
//menuScene.setBackground(new Background(50, 0, 0));
//Options
optionsScene = new Scene();
//Juego
gameScene = new Scene();
//Pausa
pauseScene = new Scene();
//Gameover
gameOverScene = new Scene();
}
load Resource not shows error, but loadScenes, Line:
SpriteBackground bg = new SpriteBackground(new Sprite(centerX, centerY, menuBgTexture));
Says me that I have to set a new attribute (ISpriteVertexBufferObject), well, what is this?
for the VBOManager object, use
this.getVertexBufferObjectManager();
I also had the same issue with my game. The solution is to have a background image that is of the same dimensions as the camera. For example, if your camera is 800X480, the image should be of the same dimensions as well. Also, make the dimensions of the BitmapTextureAtlas factors of two. In your case, it should be 512px by 512 px. Hope that helps.
Cheers!!

Sprite example from AndEngine's example project, looks pixelated when imported to my own project

I'm trying out AndEngine (GLES2) in the last couple of days.
I'm having a problem with the SpriteExample from the Examples project.
In the SpriteExample in the example project the face_box.png sprite looks nice and sharp.
However when I copy the same code and the face_box.png file to my own separate project, the sprite looks pixelated.
Since the code is just the same I guess the problem is with some configuration settings, however I could not figure it out.
I'm running on Galaxy S2 with ICS.
Does anyone have any idea on what might cause the problem?
This is the code if anyone wondered -
public class AndEngineMapActivity extends SimpleBaseGameActivity implements OnClickListener {
// ===========================================================
// Constants
// ===========================================================
private static final int CAMERA_WIDTH = 800;
private static final int CAMERA_HEIGHT = 480;
// ===========================================================
// Fields
// ===========================================================
private ITexture mTexture;
private ITextureRegion mFaceTextureRegion;
// ===========================================================
// Constructors
// ===========================================================
// ===========================================================
// Getter & Setter
// ===========================================================
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
#Override
public EngineOptions onCreateEngineOptions() {
final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.LANDSCAPE_SENSOR, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
}
#Override
public void onCreateResources() {
try {
this.mTexture = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() {
#Override
public InputStream open() throws IOException {
return getAssets().open("gfx/face_box.png");
}
});
this.mTexture.load();
this.mFaceTextureRegion = TextureRegionFactory.extractFromTexture(this.mTexture);
} catch (IOException e) {
Debug.e(e);
}
}
#Override
public Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
final Scene scene = new Scene();
scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));
/* Calculate the coordinates for the face, so its centered on the camera. */
final float centerX = (CAMERA_WIDTH - this.mFaceTextureRegion.getWidth()) / 2;
final float centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight()) / 2;
/* Create the face and add it to the scene. */
final Sprite face = new Sprite(centerX, centerY, this.mFaceTextureRegion, this.getVertexBufferObjectManager());
scene.attachChild(face);
return scene;
}
#Override
public void onClick(final ButtonSprite pButtonSprite, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(AndEngineMapActivity.this, "Clicked", Toast.LENGTH_LONG).show();
}
});
}
// ===========================================================
// Methods
// ===========================================================
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}
**Update: ** Following to JoenEye's advice I tried loading the texture differently with
#Override
public void onCreateResources() {
try {
this.mTexture = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() {
#Override
public InputStream open() throws IOException {
return getAssets().open("gfx/face_box.png");
}
}, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
this.mTexture.load();
this.mFaceTextureRegion = TextureRegionFactory.extractFromTexture(this.mTexture);
} catch (IOException e) {
Debug.e(e);
}
}
The results have improved and the smiley face picture looks a bit better, but it's still not as sharp as in the Example's project.
****Another Update: ****
These are the images of the results I get.
-
This is the one from the original example project (best result)
This is the one from my project without the TextureOptions.BILINEAR_PREMULTIPLYALPHA
This is the one from my project with the TextureOptions.BILINEAR_PREMULTIPLYALPHA (current result
By the way way - Interesting result, once I created another empty project with only this class in it and it worked flawlessly and looked good.
So I guess it must be some kind of a configuration problem with my own project.
I'd be glad to get any more ideas!
Thanks!
I'll guess that the difference between the example project and the one with the bad result is either the subpixel location of the sprite or it being stretched/skewed slightly. Are you careful to draw the sprite exactly on a pixel boundry?
The issue is when a fragment is drawn, if the fragment is not perfectly lined up with the texture it is sampling, then it must generate a color that is not exactly what is in the texture. For example if you have a 10x10 pixel texture, and you draw it at screen coordinates (0.5, 0.5), then each pixel either needs to just pick the nearest texel (NEAREST sampling), or blend together nearby texels (LINEAR or BILINEAR sampling). NEAREST can have rounding problems here if two texels are equal distances from the sample point (which might explain the slightly ugly image in the first bad result). If you use LINEAR sampling, then it just blends together the nearest texels, which may give the slightly blurred image you see in the second result.
So to remedy this for small images with tiny features, you want to always make sure to draw the image so that it lines up exactly with a pixel boundry. Can you check if you are doing this in your application?
For more information I would do some google searches for 'pixel perfect sampling/rendering`, which will give you more information.
EDIT, I also noticed this, which may explain:
final float centerX = (CAMERA_WIDTH - this.mFaceTextureRegion.getWidth()) / 2;
final float centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight()) / 2;
You're doing integer division here, which could cause you to get the value of 'center' off by a half pixel if the image width/height is an odd number. Then when you pass this value to AndEngine your center is incorrect by a half pixel, explaining the subpixel problems.
Try using the standard way of loading images unless the method you use is necessary for some reason.
mTexture = new BitmapTexture(1024, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
BitmapTextureRegionFactory.createFromAsset(mTexture, this, "face_box.png", 0, 0);
You will probably end up using buildable textures that place the images on the texture automatically, but this could help you determine where the problem is.
Eventually I decided to just open up a new project and move all my code to the new project.
It works.
I know it's a bad solution, but it solved the problem.
If I'll ever come up with this problem again, and find a real solution, I would update it in here.

TiledSprite not working as expected

I can't seem to get TiledSprite to work as I expect. There are no manuals, and the examples in AndEngineExamples is of no help.
Consider this code:
#Override
public void onLoadResources() {
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
mBlockAtlas = new BitmapTextureAtlas(512, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
mBlockTexture = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mBlockAtlas, getApplicationContext(), "num_plasma.png", 0, 0, 3, 3);
getTextureManager().loadTexture(mBlockAtlas);
}
This loads a 512x512 texture that I've - manually - divided into 3x3 tiles just for testing purposes. Now, doing this:
public Scene onLoadScene() {
mScene = new Scene();
mScene.setBackground(new ColorBackground(0.5f, 0.1f, 0.6f));
TiledSprite foo, foo2;
foo = new TiledSprite(0, 0, mBlockTexture);
foo.setCurrentTileIndex(1);
foo2 = new TiledSprite(0, 200, mBlockTexture);
foo2.setCurrentTileIndex(5);
mScene.attachChild(foo);
mScene.attachChild(foo2);
return mScene;
}
This puts up two sprites on the screen (as expected), but they both shows the same tile (5)!
How are one supposed to do if you have a bunch of sprites using the same texture but showing different tiles?
I think you need to deepCopy() the Texture when you are creating the Sprites, like
foo2 = new TiledSprite(0, 200, mBlockTexture.deepCopy());

Categories

Resources