Andengine Troubles, Nothing showing on screen? SimpleBaseGameActivity - android

I have this code:
public class GameActivity extends SimpleBaseGameActivity{
static final int CAMERA_WIDTH = 800;
static final int CAMERA_HEIGHT = 480;
public Font mFont;
public Camera mCamera;
public Scene mCurrentScene;
public static BaseActivity instance;
#Override
public EngineOptions onCreateEngineOptions() {
// TODO Auto-generated method stub
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() {
// TODO Auto-generated method stub
mFont = FontFactory.create(this.getFontManager(), this.getTextureManager(), 256, 256, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 32);
mFont.load();
}
#Override
protected Scene onCreateScene() {
// TODO Auto-generated method stub
mEngine.registerUpdateHandler(new FPSLogger());
mCurrentScene = new Scene();
mCurrentScene.setBackground(new Background(0.09804f, 0.7274f, 0.8f));
return mCurrentScene;
}
}
My Whole goal is to just get something on the screen, just a simple bitmap. Ive followed a bunch of tutorials on andengine on youtube and google, but for some reason when i start the basegameactivity it doesnt display anything at all just a blank white screen. I also downloaded an example from github and copied and pasted the code and the example gave me the same thing just a blank white screen. Can anyone help me? Id appreciate it :]

I think you are using GLES2 andengine branch and may be your phone is not supporting GLES2.

Related

How to pass parameters to SurfaceView from main activity in Android

I need to draw a line diagram using surfaceview so from Main activity i will pass 2 parameters to surfaceview following this [link][1] solution
[1]: Passing arguments to SurfaceView via Constructor but still my passed parameters are 0 inside surfaceview draw method, please help
MySurfaceView.java:
private int len,theta;
public void setParameter(int length, int angle){
this.len = length;
this.theta = angle;
System.out.println("inside setParameter, len: "+len+" ,theta: "+theta);
}
protected void drawSomething(Canvas canvas) {
System.out.println("Inside drawSomething() , len: "+len+" ,theta: "+theta);
canvas.drawColor(Color.WHITE);
}
MainActivity.java
MySurfaceView myView = new MySurfaceView(MainActivity.this);
myView.setParameter(90,30);
myView.invalidate();
Values are printed only inside setParameter() not inside drawSomething(), i need to use those values inside drawSomething(),please help
Edited my code below,
public MySurfaceView(Context context) {
super(context);
init();
}
private void init(){
paint = new Paint();
paint.setColor(Color.BLUE);
paint.setStrokeWidth(3);
paint.setStyle(Paint.Style.STROKE);
surfaceHolder = getHolder();
surfaceHolder.addCallback(new SurfaceHolder.Callback(){
#Override
public void surfaceCreated(SurfaceHolder holder) {
canvas = holder.lockCanvas(null);
drawSomething(canvas);
holder.unlockCanvasAndPost(canvas);
}
#Override
public void surfaceChanged(SurfaceHolder holder,
int format, int width, int height) {
// TODO Auto-generated method stub
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}});
}
Since drawSomething(canvas) is called in a callback, you cannot know if it will be called earlier or not than your next function setParameter(int, int).
If you want to have the values available for drawSomething(canvas), you have to pass them in the constructor of MySurfaceView, or extend the class SurfaceHolder.Callback and pass the value to those.

why arent my ITextureRegions being displayed on my scene

I'm just learning AndEngine Anchor center and I'm working my way through the cookbook. However I'm having some trouble with loading the images to the screen (code compiles fine). Trying to load 3 rectangles(30x40, 40x30,70x50) in the oncreateResources method, code compiles, but does not display images.Thanks
public class GameActivity extends BaseGameActivity {
// The following constants will be used to define the width and height
// of our game's camera view
private static final int WIDTH = 800;
private static final int HEIGHT = 480;
// Declare a Camera object for our activity
private Camera mCamera;
// Declare a Scene object for our activity
private Scene mScene;
#Override
public EngineOptions onCreateEngineOptions() {
// Define our mCamera object
mCamera = new Camera(0, 0, WIDTH, HEIGHT);
// Declare & Define our engine options to be applied to our Engine
// object
EngineOptions engineOptions = new EngineOptions(true,
ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(),
mCamera);
engineOptions.setWakeLockOptions(WakeLockOptions.SCREEN_ON);
return engineOptions;
}
#Override
public void onCreateResources(
OnCreateResourcesCallback pOnCreateResourcesCallback) {
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
BitmapTextureAtlas mBitmapTextureAtlas = new BitmapTextureAtlas(mEngine.getTextureManager(), 120, 120);
ITextureRegion mRetangleOneTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mBitmapTextureAtlas, this, "rectangle_one.png", 10, 10);
ITextureRegion mRetangleTwoTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mBitmapTextureAtlas, this, "rectangle_two.png", 50, 10);
ITextureRegion mRetangleThreeTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mBitmapTextureAtlas, this, "rectangle_three.png", 10, 60);
mBitmapTextureAtlas.load();
pOnCreateResourcesCallback.onCreateResourcesFinished();
}
#Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) {
mScene = new Scene();
pOnCreateSceneCallback.onCreateSceneFinished(mScene);
}
#Override
public void onPopulateScene(Scene pScene,
OnPopulateSceneCallback pOnPopulateSceneCallback) {
pOnPopulateSceneCallback.onPopulateSceneFinished();
}
}
I haven't used the center anchor branch yet, but your problem seems to be that you need to create sprites with the loaded ITextures. Then, you need to attach those sprites in the scene.

Starting libGdx from android code

I am making android- specific game, and would like to use android API's. But I'm wondering, is it possible to have like an android menu, and from there call libGdx? I've tried adapting the manifest in the android part, so android activity (menu) is the LAUNCHER activity and gets called first. Then I thought I could start the ApplicationListener with an intent, but it doesn't work. What am I doing wrong? Is it even possible?
It is pretty simple:
// my call of the loading screen from inside a fragment
Intent intent = new Intent(getActivity(), LoadingScreenActivity.class);
startActivity(intent);
and the LoadingScreenActivity is, guess what, an Activity.
// the onCreate method
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = true;
cfg.useAccelerometer = false;
cfg.useCompass = false;
initialize(new LoadingScreen(), cfg);
}
and LoadingScreen is the the ApplicationListener you already mentioned
public class LoadingScreen implements ApplicationListener {
// create(), resize() and more
}
So basically you need embed libgdx inside an Activity...
Edit:
Here is my complete loading screen activity which uses libgdx. It should work (just remove the CustomApplication stuff that you don't have).
public class LoadingScreenActivity extends AndroidApplication {
private final static String LOG_TAG = LoadingScreenActivity.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = true;
cfg.useAccelerometer = false;
cfg.useCompass = false;
initialize(new LoadingScreen(), cfg);
}
private class LoadingScreen implements ApplicationListener {
private OrthographicCamera mCamera = new OrthographicCamera();
private SpriteBatch mBatch;
private Stage mStage;
private ShapeRenderer mProgressLine;
#Override
public void create() {
AssetManager manager = CustomApplication.getAssetManager();
manager.load("ic_launcher.png", Texture.class);
mStage = new Stage();
mProgressLine = new ShapeRenderer();
mBatch = new SpriteBatch();
mCamera.setToOrtho(false, DesignUtil.getScreenWidth(), DesignUtil.getScreenHeight());
mStage.setCamera(mCamera);
}
#Override
public void resize(int width, int height) {
mStage.setViewport(width, height, true);
}
#Override
public void render() {
if (CustomApplication.getAssetManager().update()) {
IntentUtil.startActivity(LoadingScreenActivity.this, GameActivity.class);
finish();
return;
}
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// tell the camera to update its matrices.
mCamera.update();
// tell the SpriteBatch to render in the
// coordinate system specified by the camera.
mBatch.setProjectionMatrix(mCamera.combined);
mBatch.begin();
mProgressLine.begin(ShapeRenderer.ShapeType.Rectangle);
mProgressLine.rect(0 + mStage.getWidth() / 5, mStage.getHeight() / 2, mStage.getWidth() - mStage.getWidth() / 2.5f, DesignUtil.percentToPixel(5, mStage.getHeight()));
mProgressLine.setColor(0, 1, 0, 1);
mProgressLine.end();
mProgressLine.begin(ShapeRenderer.ShapeType.FilledRectangle);
mProgressLine.filledRect(0 + mStage.getWidth() / 5, mStage.getHeight() / 2, (mStage.getWidth() - mStage.getWidth() / 2.5f) * CustomApplication.getAssetManager().getProgress(), DesignUtil.percentToPixel(5, mStage.getHeight()));
mProgressLine.setColor(0, 1, 0, 1);
mProgressLine.end();
mBatch.end();
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
mBatch.dispose();
mStage.dispose();
mProgressLine.dispose();
}
}
}
If it helps, you CAN use the Android UI elements from Libgdx. That way, you can get the behavior you want, but not the method you want.
http://code.google.com/p/libgdx-users/wiki/IntegratingAndroidNativeUiElements
http://code.google.com/p/libgdx-users/wiki/IntegratingAndroidNativeUiElements3TierProjectSetup

Basic Sprite not loading - AndEngine

Hello I have just started with AndEngine. I have been working with a tutorial here. I thought it was pretty straightforward but I can't seem to get it to work properly. The purpose is simply to display a simple Sprite. I did the code precisely how the tutorial instructed but when I run the game, nothing loads at all. I threw in some Log.i statements and it doesn't appear that any of the basic methods (OnCreateResources etc...) run at all. I'm not getting any errors but the nothing is running either. Does anyone know what I am doing wrong?
(I hope this isn't a stupid question) Code is below:
public class GameMain extends BaseGameActivity {
Scene scene;
protected static final int CAMERA_WIDTH = 800;
protected static final int CAMERA_HEIGHT = 480;
BitmapTextureAtlas playerTexture;
ITextureRegion playerTextureRegion;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_main);
Log.i("TEST", "CREATE GAME");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.game_main, menu);
return true;
}
#Override
public EngineOptions onCreateEngineOptions() {
// TODO Auto-generated method stub
Camera mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
EngineOptions options = new EngineOptions(true,
ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(
CAMERA_WIDTH, CAMERA_HEIGHT), mCamera);
return options;
}
#Override
public void onCreateResources(
OnCreateResourcesCallback pOnCreateResourcesCallback)
throws Exception {
// TODO Auto-generated method stub
Log.i("TEST", "LOADING GFX");
loadGFX();
pOnCreateResourcesCallback.onCreateResourcesFinished();
}
private void loadGFX() {
// TODO Auto-generated method stub
Log.i("TEST", "LOAD GFX");
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
playerTexture = new BitmapTextureAtlas(getTextureManager(), 64, 64);
playerTextureRegion = BitmapTextureAtlasTextureRegionFactory
.createFromAsset(playerTexture, this, "star.png", 0, 0);
playerTexture.load();
}
#Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
throws Exception {
// TODO Auto-generated method stub
Log.i("TEST", "Scene Background");
this.scene = new Scene();
this.scene.setBackground(new Background(0, 125, 58));
pOnCreateSceneCallback.onCreateSceneFinished(this.scene);
}
#Override
public void onPopulateScene(Scene pScene,
OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
// TODO Auto-generated method stub
Sprite sPlayer = new Sprite(CAMERA_WIDTH / 2, CAMERA_HEIGHT / 2,
playerTextureRegion,
this.mEngine.getVertexBufferObjectManager());
sPlayer.setRotation(45.0f);
this.scene.attachChild(sPlayer);
pOnPopulateSceneCallback.onPopulateSceneFinished();
}
}
in a short look at the tutorial, i didn't see anything about overwriting the onCreate method, esp. the setContentView part could be wrong.
makes me wonder, that none of the Log.i calls appear in your log, did you set your log to a higher level than info?
let me take a deeper look, if these two hints where not right ;-)

AndEngine Text.setText not working

I created a helper class that will return Text object based on the custom font and text I give.
Here is code :
public class CustomFontTextHelper {
private Font font;
private ITexture fontTexture;
private Text text;
public Text getTextWithFont(String myText,String fontPath,BaseGameActivity activity,float x,float y,int fontsize,int color){
fontTexture = new BitmapTextureAtlas(activity.getTextureManager(), 256, 256, TextureOptions.BILINEAR);
FontFactory.setAssetBasePath("fonts/");
this.font = FontFactory.createFromAsset(activity.getFontManager(), fontTexture, activity.getAssets(), fontPath, fontsize, true, color);
this.font.load();
text = new Text(x, y, this.font, myText, activity.getVertexBufferObjectManager());
return text;
}
}
Using this helper class I create a text and attach to my scene. Its working perfectly. But when I try to change the text using text.setText method it crashes.
Below is the code I use to change the text.
public class StartingScreen extends BaseGameActivity {
// ===========================================================
// Constants
// ===========================================================
private static final int CAMERA_WIDTH = 480;
private static final int CAMERA_HEIGHT = 720;
// ===========================================================
// Fields
// ===========================================================
public Text loadingPercentage;
public float percentLoaded;
public CustomFontTextHelper fontHelper;
#Override
public EngineOptions onCreateEngineOptions() {
final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.PORTRAIT_SENSOR, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
}
#Override
public void onCreateResources(
OnCreateResourcesCallback pOnCreateResourcesCallback)
throws Exception {
pOnCreateResourcesCallback.onCreateResourcesFinished();
}
#Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
throws Exception {
this.mEngine.registerUpdateHandler(new FPSLogger());
final Scene scene = new Scene();
scene.setBackground(new Background(0, 0, 0));
fontHelper = new CustomFontTextHelper();
percentLoaded = 0;
loadingPercentage = fontHelper.getTextWithFont("0%", "MyriadPro-Cond.ttf", this, CAMERA_WIDTH-120, 100, 64, Color.BLACK);
scene.attachChild(loadingPercentage);
pOnCreateSceneCallback.onCreateSceneFinished(scene);
}
#Override
public void onPopulateScene(Scene pScene,
OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
pOnPopulateSceneCallback.onPopulateSceneFinished();
loadingPercentage.setText("5%");
new Thread(new Runnable(){
#Override
public void run() {
int incr;
for (incr = 0; incr <= 100; incr+=5) {
StartingScreen.this.runOnUpdateThread(new Runnable(){
#Override
public void run() {
// TODO Auto-generated method stub
StartingScreen.this.loadingPercentage.setText(Float.toString(StartingScreen.this.percentLoaded) + "%");
}
});
try {
Thread.sleep(5*1000);
} catch (InterruptedException e) {
}
}
// TODO Auto-generated method stub
}
}).start();
// TODO Auto-generated method stub
}
Please help me with the code.
As far as I can see you are using AndEngine GLES2, so as Matim already said you can change the Text instance. The problem is that when you instantiate the Text object the first time you either need to include some text or better you should tell the object how many letters it has to hold (max).
so instead of
text = new Text(x, y, this.font, myText, activity.getVertexBufferObjectManager());
you do
int textLength = 4;
text = new Text(x, y, this.font, myText, textLength, activity.getVertexBufferObjectManager());
I just set the length to 4 since you use it as a percentage, which can only be between 0% and 100% so maximal 4 letters. Of course you can set the text length as you want, if you set it to 1000 and only put "hello" in your text, its fine (if you can, make them small to save memory) .
You just can't reset the text size afterwards – meaning, once you created a Text object with length 5 you can't put 6 letters in it, nor can you call something like setSize(6).. that's not working.
I hope this helps!
Christoph
As pointed out by game droid, the problem is with the text length. However I would like to answer this question and the next one at the same time. The next question is, how do I avoid the delay when changing the text from one value to another, and the solution is to initialize the text with all of the characters you plan to use, like so:
Text text = new Text(x, y, this.font, "1234567890%",activity.getVertexBufferObjectManager());
text.setText("0%");
This form of the constructor sets the maximum text length to whatever the length of the initial string is, so in this case 11.
Text is only set your text value, you can't change that text,
if you want change your text then use ChangeableText.
ChangeableText changeableText = new ChangeableText(x, y, Font, "Your Text");
yourscene.attachChild(changeableText);
changeableText.setText("Your New Text");

Categories

Resources