0

这让我发疯,我不明白这有什么问题,当我尝试加载它时应用程序会崩溃,但如果我评论这一行:

playerShip.loadGLTexture(gl, this.context);

应用程序运行正常,但是,当然,没有纹理 :(

MainRenderer.java:

package com.example.galaga2d;

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

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

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLUtils;



public class MainRenderer implements Renderer {
    Random rand = new Random();

    private Context     context;

    public float radiusX = 20.0f;
    public float radiusY = 20.0f;

    public float playerX = 100.0f;
    public float playerY = 300.0f;

    public float enemyX = rand.nextFloat() * (30.0f - 1.0f) + 1.0f;
    public float enemyY = 0.0f;

    public Ship playerShip = new Ship();

    public Astedoid enemyAstedoid = new Astedoid();

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        //! TEXTURES
        playerShip.loadGLTexture(gl, this.context);
        gl.glEnable(GL10.GL_TEXTURE_2D);
        gl.glShadeModel(GL10.GL_SMOOTH);
        //! TEXTURES

        gl.glClearColor(1.0f, 0.0f, 0.0f, 1.0f);

    }


    @Override
    public void onDrawFrame(GL10 gl) {
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        gl.glLoadIdentity();

        if (playerShip.life != 0) {

            playerShip.draw(gl, playerX);

            gl.glLoadIdentity();

            enemyY += enemyAstedoid.speed;

            enemyAstedoid.draw(gl, enemyX, enemyY);

            gl.glLoadIdentity();

            float result = (float)Math.sqrt(Math.pow((playerX-enemyX), 2)+Math.pow((playerY-enemyY), 2));

            if (result < 30.0f)
            {
                if(enemyAstedoid.collision == 0)
                {
                    playerShip.life = playerShip.life - 1;
                    enemyAstedoid.collision |= 1;
                }
            } else {
                enemyAstedoid.collision &= ~1;
            }
            /*
            if (enemyX > playerX-radiusX && enemyX < playerX+radiusX && enemyY > playerY-radiusY && enemyY < playerY+radiusY && flag == 0) {
                playerShip.life = playerShip.life - 1;
                flag = 1;
            } else {
                flag = 0;
            }
            */
            gl.glLoadIdentity();

        } else {
            gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        }
    }



    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glOrthof(0, width, height, 0, 1, -1);
        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
    }

// --------------------------------------------------------------------------------

    class Ship {
        public int life = 5;

        public FloatBuffer ShipVertexBuffer;
        public FloatBuffer ShipTextureBuffer;

        //! TEXTURES
        private int[] textures = new int[1];
        //! TEXTURES

        public float ShipVerticles[] = {
            0, 0,       // лево низ
            0, 30,      // лево вверх
            30, 0,      // право низ
            30, 30      // право вверх
        };

        //! TEXTURES
        public float ShipTextures[] = {
                0.0f, 1.0f,
                1.0f, 1.0f,
                0.0f, 0.0f,
                1.0f, 0.0f
            };
        //! TEXTURES

        public Ship() {
            //! Буффер вертексов
            ByteBuffer bb = ByteBuffer.allocateDirect(36);
            bb.order(ByteOrder.nativeOrder());
            ShipVertexBuffer = bb.asFloatBuffer();
            ShipVertexBuffer.put(ShipVerticles);
            ShipVertexBuffer.position(0);

            //! TEXTURES
            bb = ByteBuffer.allocateDirect(ShipTextures.length * 4);
            bb.order(ByteOrder.nativeOrder());
            ShipTextureBuffer = bb.asFloatBuffer();
            ShipTextureBuffer.put(ShipTextures);
            ShipTextureBuffer.position(0);
            //! TEXTURES
        }

        public void loadGLTexture(GL10 gl, Context context) {
            // loading texture
            Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
                    R.drawable.ic_launcher);

            // generate one texture pointer
            gl.glGenTextures(1, textures, 0);
            // ...and bind it to our array
            gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

            // create nearest filtered texture
            gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
            gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

            // Use Android GLUtils to specify a two-dimensional texture image from our bitmap 
            GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

            // Clean up
            bitmap.recycle();
        }

        public void draw(GL10 gl, float x) {
            //! TEXTURE
            gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
            gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
            gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
            //! TEXTURE

            gl.glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
            gl.glTranslatef(x, 300.0f, 0.0f);
            gl.glVertexPointer(2, GL10.GL_FLOAT, 0, ShipVertexBuffer);

            //! TEXTURE         
            gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, ShipTextureBuffer);
            //! TEXTURE         

            gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);

            //! TEXTURE             
            gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
            gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
            //! TEXTURE 
        }

    }

    class Astedoid {
        public float speed = 25.0f;
        public int collision = 0;
        public FloatBuffer AsteroidVertexBuffer;

        public float AsteroidVerticles[] = {
                0, 0,       // лево низ
                0, 30,      // лево вверх
                30, 0,      // право низ
                30, 30      // право вверх
            };

        public Astedoid() {
            ByteBuffer bb = ByteBuffer.allocateDirect(36);
            bb.order(ByteOrder.nativeOrder());
            AsteroidVertexBuffer = bb.asFloatBuffer();
            AsteroidVertexBuffer.put(AsteroidVerticles);
            AsteroidVertexBuffer.position(0);
        }

        public void draw(GL10 gl, float x, float y) {
            if (y > 480.0f) {
                enemyY = 0.0f;
                enemyX = rand.nextFloat() * (300.0f - 1.0f) + 1.0f;
            }

            gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
            gl.glTranslatef(x, y, 0.0f);
            gl.glVertexPointer(2, GL10.GL_FLOAT, 0, AsteroidVertexBuffer);
            gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
        }

    }



// --------------------------------------------------------------------------------

}

MainActivity.java:

package com.example.galaga2d;

import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.app.Activity;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;


public class MainActivity extends Activity {
    MainRenderer r;
    float oldX, newX;

    private static final String TAG = "MainRenderer";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Убираем тайтл приложения, тобишь делаем его FullScreen
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                        WindowManager.LayoutParams.FLAG_FULLSCREEN);

        // Создаём новый Surface и устанавливаем MainRenderer
        GLSurfaceView view = new GLSurfaceView(this);
        r = new MainRenderer();
        view.setRenderer(r);
        setContentView(view);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int eventaction = event.getAction();
        switch (eventaction) {

        case MotionEvent.ACTION_DOWN:
            oldX = (float) event.getX();

            break;

        case MotionEvent.ACTION_MOVE:
            newX = (float) event.getX();
            if (oldX > newX) {
                r.playerX -= 10;
            }

            if (oldX < newX) {
                r.playerX += 10;
            }

            oldX = (float) event.getX();
            break;

        case MotionEvent.ACTION_UP:
            oldX = (float) event.getX();
            break;

        }

        return true;
    }

}

它出什么问题了?也许我忘记了什么或者纹理加载错误......?

4

1 回答 1

0

好吧,我做到了:D

我在 MainRenderer 类中忘记了这段代码:

public MainRenderer(Context context) {
    this.context = context;
}

在 MainActivity 中:

r = new MainRenderer(this);

就这样 :)

于 2013-08-21T14:47:31.147 回答