I am trying to add an image to the background in wallpaper activity. But I am unable to do so, can some one suggest some way to do it, here is my code :
menuBackgroundTexture = new BitmapTextureAtlas(getTextureManager(),
2*CAMERA_WIDTH, 2*CAMERA_HEIGHT, TextureOptions.DEFAULT);
menuBgTexture = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.menuBackgroundTexture,
this, "land.png", 0, 0);
After tht I tried using
this.menuBackgroundTexture.load();
but it is not working, I also tried following code
SpriteBackground bg = new SpriteBackground(new Sprite(0, 0,
menuBgTexture,this.getVertexBufferObjectManager()));
mScene.setBackground(bg);
This is also not working, please help me out.
I finally found a way to do that. Here is the code for the same :
final BitmapTexture backgroundTexture = new
BitmapTexture(this.getTextureManager(), new IInputStreamOpener() {
#Override
public InputStream open() throws IOException {
return getAssets().open("gfx/background1.jpg");
}
});
backgroundTexture.load();
backgroundTextureRegion = TextureRegionFactory.extractFromTexture(backgroundTexture); final
Sprite background = new Sprite(CAMERA_WIDTH / 2f -
backgroundTextureRegion.getWidth() / 2f, CAMERA_HEIGHT / 2f -
backgroundTextureRegion.getHeight() / 2f, backgroundTextureRegion,
this.getVertexBufferObjectManager());
mScene.attachChild(background);
I hope it helps some one else.
#Rahul, also work below code for wallaper activity in my wallpaper service
public class MagicWallpaper extends BaseLiveWallpaperService {
private TextureRegion region;
#Override
public EngineOptions onCreateEngineOptions() {
calculateWidthAndHeight();
Camera mCamera = new Camera(0, 0, Helper.GAME_WIDTH, Helper.GAME_HEIGHT);
EngineOptions engOptions = new EngineOptions(true,
ScreenOrientation.PORTRAIT_FIXED, new FillResolutionPolicy(),
mCamera);
return engOptions;
}
#SuppressWarnings("deprecation")
private void calculateWidthAndHeight() {
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay();
Helper.GAME_WIDTH = display.getWidth();
Helper.GAME_HEIGHT = display.getHeight();
}
#Override
public void onCreateResources(
OnCreateResourcesCallback pOnCreateResourcesCallback)
throws Exception {
BitmapTextureAtlas atlas = new BitmapTextureAtlas(getTextureManager(),
600, 1024);
region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(atlas,
this, "wall.png", 0, 0);
atlas.load();
pOnCreateResourcesCallback.onCreateResourcesFinished();
}
#Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
throws Exception {
mEngine.registerUpdateHandler(new FPSLogger());
Scene dScene = new Scene();
dScene.setBackground(new SpriteBackground(new Sprite(0, 0, region,
getVertexBufferObjectManager())));
pOnCreateSceneCallback.onCreateSceneFinished(dScene);
}
#Override
public void onPopulateScene(Scene pScene,
OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
pOnPopulateSceneCallback.onPopulateSceneFinished();
}
}
Related
I'm using AndEngine GLES2, and I tried to load an image on my phone but what I got was a black screen
Here are my code below:
public class BaseActivity extends SimpleBaseGameActivity {
static final int CAMERA_WIDTH = 800;
static final int CAMERA_HEIGHT = 480;
public Font mFont;
public Camera mCamera;
private MainMenuScene mainMenuScene;
public Engine mEngine;
public Scene mScene;
public BuildableBitmapTextureAtlas mBitmapTextureAtlas;
public ITextureRegion mITextureRegion;
#Override
public EngineOptions onCreateEngineOptions() {
instance = this;
mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), mCamera);
}
#Override
protected void onCreateResources() {
mFont = FontFactory.create(this.getFontManager(), this.getTextureManager(), 256, 256, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 32);
mFont.load();
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
mBitmapTextureAtlas = new BuildableBitmapTextureAtlas(this.getTextureManager(), 256, 256, TextureOptions.BILINEAR);
mITextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mBitmapTextureAtlas, this, "splash1.png");
try {
mBitmapTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 1, 1));
} catch (TextureAtlasBuilderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mBitmapTextureAtlas.load();
}
#Override
protected Scene onCreateScene() {
final float positionX = CAMERA_WIDTH * 0.5f;
final float positionY = CAMERA_HEIGHT * 0.5f;
Sprite mSprite = new Sprite(positionX, positionY, mITextureRegion, mEngine.getVertexBufferObjectManager());
mScene.attachChild(mSprite);
return mCurrentScene;
}
The Logcat shows that I have a nullPointerException in my onCreateResources and onCreateScene, and I have no idea what has gone wrong. What might be the problem?
One error was because you never created your Scene object, then tried to attach a sprite to it, the other was that the VertexbufferObjectManager is not that of the Engine class but of the SimpleBaseGameActivity class. onCreateScene() becomes:
#Override
protected Scene onCreateScene() {
mScene = new Scene();
final float positionX = CAMERA_WIDTH * 0.5f;
final float positionY = CAMERA_HEIGHT * 0.5f;
Sprite mSprite = new Sprite(positionX, positionY, mITextureRegion, getVertexBufferObjectManager());
mScene.attachChild(mSprite);
return mScene;
}
I also commented out some variables to make it build, though I suspect these are due to stripping down your code sample:
// private MainMenuScene mainMenuScene;
// instance = this;
I'm building a live wallpaper with Andengine and I am having troubles starting it off. I cannot seem to get a parallexing background working. The end result of my code results in the assets upsidedown on the top right of the screen.
public class WallpaperService extends BaseLiveWallpaperService {
private static final int CAMERA_WIDTH = 800;
private static final int CAMERA_HEIGHT = 480;
private static final String BG_FOREGROUND = "background/arctic_foreground.png";
private static final String BG_MIDGROUND = "background/arctic_midground.png";
private static final String BG_BACKGROUND = "background/arctic_land.png";
private static final int BG_TEXTURE_ATLAS_HEIGHT_WIDTH = 2048;
private static final int BG_FOREGROUND_START_HEIGHT = 0;
private static final int BG_MIDGROUND_START_HEIGHT = 600;
private static final int BG_BACKGROUND_START_HEIGHT = 1200;
private Camera camera;
private BitmapTextureAtlas backgroundTexture;
private TextureManager textureManager;
private TextureRegion mParallaxLayerFront;
private TextureRegion mParallaxLayerBack;
private TextureRegion mParallaxLayerMid;
#Override
public EngineOptions onCreateEngineOptions() {
camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(), this.camera);
engineOptions.setWakeLockOptions(WakeLockOptions.SCREEN_ON);
textureManager = new TextureManager();
return engineOptions;
}
#Override
public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws Exception {
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
this.backgroundTexture = new BitmapTextureAtlas(textureManager, BG_TEXTURE_ATLAS_HEIGHT_WIDTH, BG_TEXTURE_ATLAS_HEIGHT_WIDTH, TextureOptions.DEFAULT);
this.mParallaxLayerFront = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture, this, BG_FOREGROUND, 0, BG_FOREGROUND_START_HEIGHT);
this.mParallaxLayerBack = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture, this, BG_BACKGROUND, 0, BG_MIDGROUND_START_HEIGHT);
this.mParallaxLayerMid = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture, this, BG_MIDGROUND, 0, BG_BACKGROUND_START_HEIGHT);
this.mEngine.getTextureManager().loadTexture(this.backgroundTexture);
}
#Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws Exception {
final Scene scene = new Scene();
final AutoParallaxBackground autoParallaxBackground = new AutoParallaxBackground(0, 0, 0, 0);
autoParallaxBackground.attachParallaxEntity(new ParallaxBackground.ParallaxEntity(0.0f,
new Sprite(0, 0, this.mParallaxLayerBack, getVertexBufferObjectManager())));
autoParallaxBackground.attachParallaxEntity(new ParallaxBackground.ParallaxEntity(0.0f,
new Sprite(0, 0, this.mParallaxLayerMid, getVertexBufferObjectManager())));
autoParallaxBackground.attachParallaxEntity(new ParallaxBackground.ParallaxEntity(0.0f,
new Sprite(0, 0, this.mParallaxLayerFront, getVertexBufferObjectManager())));
scene.setBackground(autoParallaxBackground);
pOnCreateSceneCallback.onCreateSceneFinished(scene);
}
#Override
public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
pOnPopulateSceneCallback.onPopulateSceneFinished();
}
Any help is greatly appreciated
All the images have 600 px of heigth? the problem seems to be that the images are collapsing with each other.
Maybe the problem is here:
autoParallaxBackground.attachParallaxEntity(
new ParallaxBackground.ParallaxEntity(0.0f,
new Sprite(0, 0, this.mParallaxLayerBack, getVertexBufferObjectManager())));
try to use this code:
autoParallaxBackground.attachParallaxEntity(new ParallaxBackground.ParallaxEntity(0.0f,
new Sprite(0, CAMERA_HEIGHT- this.mParallaxLayerBack.getHeight(),
this.mParallaxLayerBack, getVertexBufferObjectManager())));
i hope this will help you!
private Camera mCamera;
private Scene mMainScene;
private BitmapTextureAtlas mBitmapTextureAtlas;
private TextureRegion mPlayerTextureRegion;
#Override
public EngineOptions onCreateEngineOptions() {
this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera);
}
#Override
public void onCreateResources(
OnCreateResourcesCallback pOnCreateResourcesCallback)
throws Exception {
mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 32, 32);
mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "face_box.png", 0, 0);
mBitmapTextureAtlas.load();
}
#Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
throws Exception {
this.mEngine.registerUpdateHandler(new FPSLogger());
this.mMainScene = new Scene();
this.mMainScene.setBackground(new Background(1, 1, 1));
final int iStartX = (CAMERA_WIDTH - mBitmapTextureAtlas.getWidth()) / 2;
final int iStartY = (CAMERA_HEIGHT - mBitmapTextureAtlas.getHeight()) / 2;
final Sprite oPlayer = new Sprite(iStartX, iStartY, mPlayerTextureRegion, getVertexBufferObjectManager());
this.mMainScene.attachChild(oPlayer);
}
My sprite looks like it is cut in half. Can somebody explain why?
And background color is still black and it should be white.
You have to return the scene in your onCreateScene() method.
And you said that your sprite looks like it is cut in half, may be your image size is bigger than your bitmaptextureAtlas. If so, increase your textureAtlas size.
Please help! My game crash after remove Joint with collides.
The game is the body that is hanging on the rope. Finger cut the rope, and the game crashes!
My code:
#Override
protected Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
this.mScene = new Scene();
this.mScene.setBackground(new Background(0, 0, 0));
this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false);
/* Create the face and add it to the scene. */
ball = new Sprite(200, 50, this.mBallTextureRegion, this.getVertexBufferObjectManager());
ball.setScale(0.5f);
final Rectangle point = new Rectangle(400, 0, 5, 5, this.getVertexBufferObjectManager());
rope = new Line(point.getX()+5/2, point.getY()+5/2, ball.getX(), ball.getY(), this.getVertexBufferObjectManager());
this.mScene.attachChild(ball);
this.mScene.attachChild(rope);
this.mScene.attachChild(point);
final Body ballBody = PhysicsFactory.createCircleBody(this.mPhysicsWorld, ball, BodyType.DynamicBody, FIXTURE_DEF);
final Body pointBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, point, BodyType.StaticBody, FIXTURE_DEF);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(ball, ballBody, true, true) {
#Override
public void onUpdate(final float pSecondsElapsed) {
super.onUpdate(pSecondsElapsed);
final Vector2 movingBodyWorldCenter = ballBody.getWorldCenter();
rope.setPosition(rope.getX1(), rope.getY1(), movingBodyWorldCenter.x * PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT, movingBodyWorldCenter.y * PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT);
}
});
final RevoluteJointDef revoluteJointDef = new RevoluteJointDef();
revoluteJointDef.initialize(pointBody, ballBody, pointBody.getWorldCenter());
revoluteJointDef.enableMotor = true;
revoluteJointDef.maxMotorTorque = 1;
final Joint joint = this.mPhysicsWorld.createJoint(revoluteJointDef);
//collide detector
this.mScene.registerUpdateHandler(new IUpdateHandler() {
#Override
public void reset() { }
#Override
public void onUpdate(final float pSecondsElapsed) {
if(rope.collidesWith(cutLine)) {
mPhysicsWorld.destroyJoint(joint);
mScene.detachChild(rope);
}
}
});
this.mScene.registerUpdateHandler(this.mPhysicsWorld);
this.mScene.setOnSceneTouchListener(this);
return this.mScene;
}
#Override
public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) {
if(pSceneTouchEvent.isActionDown()) {
this.addCuter(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
return true;
}
if(pSceneTouchEvent.isActionMove()) {
this.moveCuter(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
return true;
}
if(pSceneTouchEvent.isActionUp()) {
this.delCuter(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
return true;
}
return false;}
private void addCuter(final float pX, final float pY) {
cutBegin = new Rectangle(pX, pY, 5, 5, this.getVertexBufferObjectManager());
cutEnd = new Rectangle(pX, pY, 5, 5, this.getVertexBufferObjectManager());
cutLine = new Line(cutBegin.getX()+5/2, cutBegin.getY()+5/2, cutEnd.getX(), cutEnd.getY(), this.getVertexBufferObjectManager());
this.mScene.attachChild(cutBegin);
this.mScene.attachChild(cutEnd);
this.mScene.attachChild(cutLine);
cutEnd.setColor(1, 0, 0);
cutLine.setColor(1, 0, 0);}
private void moveCuter(final float pX, final float pY) {
cutEnd.setPosition(pX-5/2, pY-5/2);
cutLine.setPosition(cutBegin.getX()+5/2, cutBegin.getY()+5/2, pX, pY); }
private void delCuter(final float pX, final float pY) {
this.mScene.detachChild(cutBegin);
this.mScene.detachChild(cutEnd);
this.mScene.detachChild(cutLine);}
Error LogCat
03-14 10:45:48.329: A/libc(12926): Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1)
I find that this usually occurs when you try to alter some aspect in the game which the update is dependant on from an event firing, i.e., outside of an,
mActivity.runOnUpdateThread(new Runnable()
{
#Override
public void run()
{
//...
}
});
Your onSceneTouchEvent looks like it may be a likely contender! Try adding the above code around the function calls which make changes to the scene to make it 'safe', i.e., you are not altering game variables from an interrupt while they are being processed by the update thread.
I want to capture the screen by rendering scene into the RenderTexture, i've tried to do this in MotionStreakExample and everything is ok, but when i copy code to my project i have this picture (maybe there are some troubles with textures... i can't understand):
#Override
public Engine onCreateEngine(EngineOptions pEngineOptions) {
return new Engine(pEngineOptions) {
private boolean mRenderTextureInitialized;
private RenderTexture mRenderTextures ;
private Sprite mRenderTextureSprites ;
#Override
public void onDrawFrame(final GLState pGLState) throws InterruptedException {
final boolean firstFrame = !this.mRenderTextureInitialized;
if(firstFrame) {
this.initRenderTextures(pGLState);
this.mRenderTextureInitialized = true;
}
final int surfaceWidth = WIDTH*2;
final int surfaceHeight = HEIGHT;
this.mRenderTextures.begin(pGLState, false, true);
{
/* Draw current frame. */
super.onDrawFrame(pGLState);
/* Draw previous frame with reduced alpha. */
}
this.mRenderTextures.end(pGLState);
{
pGLState.pushProjectionGLMatrix();
pGLState.orthoProjectionGLMatrixf(0, surfaceWidth, 0, surfaceHeight, -1, 1);
{
this.mRenderTextureSprites.setAlpha(1);
this.mRenderTextureSprites.onDraw(pGLState, this.mCamera);
}
pGLState.popProjectionGLMatrix();
}
if (needToSave)
{
needToSave = false;
FSHelper.saveBitmapToFile(this.mRenderTextures.getBitmap(pGLState), SAVED_PATH+"/test.png");
}
/* Flip RenderTextures. */
}
private void initRenderTextures(final GLState pGLState) {
final int surfaceWidth = WIDTH*2;
final int surfaceHeight = HEIGHT;
final VertexBufferObjectManager vertexBufferObjectManager = mEngine.getVertexBufferObjectManager();
this.mRenderTextures = new RenderTexture(mEngine.getTextureManager(), surfaceWidth, surfaceHeight);
this.mRenderTextures.init(pGLState);
final ITextureRegion renderTextureATextureRegion = TextureRegionFactory.extractFromTexture(this.mRenderTextures);
this.mRenderTextureSprites = new Sprite(0, 0, renderTextureATextureRegion, vertexBufferObjectManager);
}
};
}
PS. this cross with red background is a sprite on my scene.
Andengine already has an infrastructure for taking screenshots, have you tried using that? Take a look at the example:
https://github.com/nicolasgramlich/AndEngineExamples/blob/GLES2/src/org/andengine/examples/ScreenCaptureExample.java
I found the answer !
Textures are loaded dynamicly and on the first frame i haven't load textures at all, because i have a choose dialog there and create all textures after it. I need to call initRenderTextures each time when textures are changed.