3

The problem is onCreate() is not being called! I really have no idea what the cause is, but I know that the Log.d()'s are not being called in onCreate() and in onSurfaceCreated(). I am trying to use OpenGL ES to make a triangle from a book I am reading called Beginning Android Games 2011. The code is here for the example: http://beginning-android-games.googlecode.com/svn/trunk/ch07-gl-basics/

Here is my code:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="another.game"
    android:versionCode="1"
    android:versionName="1.0" >

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:debuggable="true" >
        <activity
            android:name=".Main"
            android:label="@string/app_name"
            android:configChanges="keyboard|keyboardHidden|orientation"
            android:screenOrientation="landscape" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-sdk android:minSdkVersion="3" android:targetSdkVersion="8"/>

</manifest>

Main.java

package another.game;

public class Main extends GLTriangleGame {

//  @Override
//  public Screen getStartScreen() {
//      return new LoadingScreen(this);
//  }
}

GLGame.java

package framework.impl;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import framework.Audio;
import framework.FileIO;
import framework.Game;
import framework.Graphics;
import framework.Input;
import framework.Screen;
import android.app.Activity;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

public abstract class GLGame extends Activity implements Game, Renderer {
    enum GLGameState {
        Initialized,
        Running,
        Paused,
        Finished,
        Idle
    }

    GLSurfaceView glView;
    GLGraphics glGraphics;
    Audio audio;
    Input input;
    FileIO fileIO;
    Screen screen;
    GLGameState state = GLGameState.Initialized;
    Object stateChanged = new Object();
    long startTime = System.nanoTime();
    WakeLock wakeLock;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        Log.d("requestWindowFeature", "Past point");
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        glView = new GLSurfaceView(this);
        glView.setRenderer(this);
        setContentView(glView);

        glGraphics = new GLGraphics(glView);
        fileIO = new AndroidFileIO(getAssets());
        audio = new AndroidAudio(this);
        input = new AndroidInput(this, glView, 1, 1);
        PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "AnotherGame");
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        Log.d("onSurfaceCreated()", "Worked!");
        glGraphics.setGl(gl);

        synchronized(stateChanged) {
            if(state == GLGameState.Initialized) {
                screen = getStartScreen();
                Log.d("getStartScreen()", "Worked!");
            }
            state = GLGameState.Running;
            screen.resume();
            startTime = System.nanoTime();
        }
    }

    @Override
    public void onDrawFrame(GL10 arg0) {
        GLGameState state = null;

        synchronized(stateChanged) {
            state = this.state;
        }

        if(state == GLGameState.Running) {
            float deltaTime = (System.nanoTime() - startTime) / 1000000000.0f;
            startTime = System.nanoTime();

            screen.update(deltaTime);
            screen.present(deltaTime);
        }

        if(state == GLGameState.Paused) {
            screen.pause();
            synchronized(stateChanged) {
                this.state = GLGameState.Idle;
                stateChanged.notifyAll();
            }
        }

        if(state == GLGameState.Finished) {
            screen.pause();
            screen.dispose();
            synchronized(stateChanged) {
                this.state = GLGameState.Idle;
                stateChanged.notifyAll();
            }
        }
    }

    @Override
    public void onSurfaceChanged(GL10 arg0, int arg1, int arg2) {
        // Not much to do. Screen will note change!
    }

    @Override
    public void setScreen(Screen screen) {
        if(screen!= null) {
            this.screen.pause();
            this.screen.dispose();
            screen.resume();
            screen.update(0);
            this.screen = screen;
        } else {
            throw new IllegalArgumentException("Screen must not be null!");
        }
    }

    public GLGraphics getGLGraphics() {
        return this.glGraphics;
    }

    public Graphics getGraphics() {
        throw new IllegalStateException("Using OpenGL!");
    }

    @Override
    public Input getInput() {
        return this.input;
    }

    @Override
    public Screen getScreen() {
        return this.screen;
    }

    @Override
    public Audio getAudio() {
        return this.audio;
    }

    @Override
    public void onResume() {
        super.onResume();
        glView.onResume();
        wakeLock.acquire();
    }

    @Override
    public void onPause() {
        synchronized(stateChanged) {
            if(isFinishing()) {
                state = GLGameState.Finished;
            } else {
                state = GLGameState.Paused;
            }
            while(true) {
                try {
                    stateChanged.wait();
                    break;
                } catch(Exception e) {
                }
            }
        }
        wakeLock.release();
        glView.onPause();
        super.onPause();
    }
}

GLTriangleGame.java

package another.game;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.PowerManager.WakeLock;
import framework.Game;
import framework.Screen;
import framework.impl.FastRenderView;
import framework.impl.GLGame;
import framework.impl.GLGraphics;

public class GLTriangleGame extends GLGame {

    @Override
    public Screen getStartScreen() {
        return new TriangleScreen(this);
    }

    public class TriangleScreen extends Screen {
        GLGraphics glGraphics;
        FloatBuffer vertices;

        public TriangleScreen(Game game) {
            super(game);
            glGraphics = ((GLGame) game).getGLGraphics();

            ByteBuffer byteBuffer = ByteBuffer.allocate(3 * 2 * 4);
            byteBuffer.order(ByteOrder.nativeOrder());
            vertices = byteBuffer.asFloatBuffer();
            vertices.put(new float[] {
                      0.0f,     0.0f,
                    319.0f,     0.0f,
                    160.0f,   479.0f
            });
            vertices.flip();
        }

        @Override
        public void update(float deltaTime) {
            game.getInput().getTouchEvents();
            game.getInput().getKeyEvents();
        }

        @Override
        public void present(float deltaTime) {
            GL10 gl = glGraphics.getGL();
            gl.glViewport(0, 0, glGraphics.getWidth(), glGraphics.getHeight());
            gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
            gl.glMatrixMode(GL10.GL_PROJECTION);
            gl.glLoadIdentity();
            gl.glOrthof(0, 320, 0, 480, 1, -1);

            gl.glColor4f(1, 0, 0, 1);
            gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
            gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertices);
            gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
        }

        @Override
        public Bitmap getCurrentScreen() {
            // Nothing yet
            return null;
        }

        @Override
        public void pause() {
            // Nothing yet
        }

        @Override
        public void resume() {
            // Nothing yet
        }

        @Override
        public void dispose() {
            // Nothing yet
        }
    }

    @Override
    public Assets getAssetsClass() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public FastRenderView getFastRenderView() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int getFrameWidth() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int getFrameHeight() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public float getScaleX() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public float getScaleY() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public Context getContext() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public WakeLock getWakeLock() {
        // TODO Auto-generated method stub
        return null;
    }

}

GLGraphics.java

package framework.impl;

import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLSurfaceView;

public class GLGraphics {
    GLSurfaceView glView;
    GL10 gl;

    GLGraphics(GLSurfaceView glView) {
        this.glView = glView;
    }

    public GL10 getGL() {
        return gl;
    }

    public void setGl(GL10 gl) {
        this.gl = gl;
    }

    public int getWidth() {
        return glView.getWidth();
    }

    public int getHeight() {
        return glView.getHeight();
    }
}
Zeveso
  • 1,274
  • 3
  • 21
  • 41
  • why do u have Main as the launcher activity? – user936414 Feb 21 '12 at 04:56
  • @user936414 it was just generated that way, not exactly sure. I thought it would have to do with my MAIN class, but it seems to not be. That has always worked and I tested it with my loading screen that does not operate in OpenGL... so I doubt that is the problem. – Zeveso Feb 21 '12 at 14:54

4 Answers4

1

Your source does not appear to be a copy of the source to which you link.

In your source the android:name=".Main" is going to look for onCreate in a class Main defined in package "another.game". It is not clear from your source that this exists. Perhaps the class you use should not be abstract?

David
  • 51
  • 3
0

I think the problem lays within Log function.

I'm an Android newbie so I don't know exactly how Log functions work, but I had huge problems with not getting any feedback at all.

After i set Log to e(error), I started getting feedback. Hope it helps you.

Mackan
  • 6,200
  • 2
  • 25
  • 45
tartaruga_casco_mole
  • 1,086
  • 3
  • 21
  • 29
0

Try putting the code that instantiates everything in the onResume as well as the onCreate.

James andresakis
  • 5,335
  • 9
  • 53
  • 88
  • Why would that work? it is not even getting to the Log in the onCreate. (Line 48 in GLGame.java) – Zeveso Feb 21 '12 at 03:56
  • Because sometimes certain conditions cause the app to shoot to the onresume. I know it sounds weird but thats what Ive seen while debugging problems with my apps on different devices and occasionally I can fix it by re instantiating things in the onresume. I check if the things I want to instantiate are null first though. – James andresakis Feb 21 '12 at 03:59
  • Here is the deal, for no reason now the code is running in onCreate until "input = new AndroidInput(this, glView, 1, 1);". However I still can't git rid of title with "this.requestWindowFeature(Window.FEATURE_NO_TITLE);... whats wrong? – Zeveso Feb 21 '12 at 04:09
  • if your just trying to get rid of the title bar you could always just do so by using android:theme="@android:style/Theme.NoTitleBar.Fullscreen" in your manifest for that activity – James andresakis Feb 21 '12 at 04:24
  • thanks, but that method has always worked. I think it is just because it keeps getting stuck on input. I am going to check that out. Weird how it took a while to actually get onCreate() to run. – Zeveso Feb 21 '12 at 14:57
0

I think you are missing following line in your onCreate() method.

setContentView(R.layout.main);
Lucifer
  • 29,392
  • 25
  • 90
  • 143
  • Why wouldn't setContentView(glView); work? Also setContentView(R.layout.main); got me a 'main cannot be resolved or is not a field' error. – Zeveso Feb 21 '12 at 03:54
  • Hes got that at setContentView(glView); I was thinking somethings cause his app to shoot over oncreate and go to onresume – James andresakis Feb 21 '12 at 03:55
  • @Jamesandresakis What should I look for? Any information on this? – Zeveso Feb 21 '12 at 04:00
  • 2
    Try using the debug mode in eclipse and step through your oncreate with break points. If you dont hit anything at all in your oncreate than its been stepped over and onresume has been called. Ive experienced this more than once on certain devices when building apps. Its weird like I said because its a problem that occurs on one device and not another. – James andresakis Feb 21 '12 at 04:03