Android Open Source - Android_Pong Dodgeball Renderer






From Project

Back to project page Android_Pong.

License

The source code is released under:

MIT License

If you think the Android project Android_Pong listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * The MIT License/*from  w  w  w .  j av  a  2  s.  c  o  m*/
 *
 * Copyright 2014 Felix B?rring <felixbarring@gmail.com>.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

/**
 * @author Felix Brring <felixbarring@gmail.com>
 */

package com.pong.android.modell.dodgeball;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;

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

import android.app.Activity;
import android.content.Context;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.Matrix;

import com.pong.android.GameOverActivity;
import com.pong.android.IFGameEvents;
import com.pong.android.MenuActivity;
import com.pong.android.R;
import com.pong.android.util.ShaderUtility;

public class DodgeballRenderer implements Renderer, IFGameEvents {

    private static final String A_POSITION = "a_Position";
    private static final String U_COLOR = "u_Color";
    private static final String U_POSITION_OFFSET = "u_offset";
    private static final String U_MATRIX = "u_Matrix";
    private static final int POSITION_COMPONENT_COUNT = 2;
    private static final int BYTES_PER_FLOAT = 4;
    private final FloatBuffer vertexData;
    private final Context context;
    private int program;
    public static int uColorLocation;
    public static int aPositionLocation;
    public static int uPositionOffsetLocation;
    public static int uMatrixLocation;

    public static final float[] projectionMatrix = new float[16];

    public static LinkedBlockingQueue<Float> queueOfTouchCoordinates =
        new LinkedBlockingQueue<Float>();

    
    private final List<Ball> balls = new ArrayList<Ball>();
    private int spawnCooldown = 0;
    private int ticksBetweenSpawns = 60;
    private int reductionCooldown = 5;
    private float ballSpeed = 0.01f;
    private int score = 0;
    
    
    private final DodgeballPlayer player;
    private final Rectangle board;

    public DodgeballRenderer(Activity activity) {
        this.context = activity;
        
        Ball fakeBall = new Ball(1.0f, 0.0f, 0.1f, 0, 1.0f, 1.0f, 1.0f);
        player = new DodgeballPlayer(-0.9f, 0.0f, 6, 1.0f, 1.0f, 1.0f);
        board = new Rectangle(2.0f,2.0f,-1.0f,1.0f,12, 0.1f, 0.1f, 0.1f);
        
        float[] tableVerticesWithTriangles =
            {
                // Ball Triangle 1
                fakeBall.topLeftX,
                fakeBall.topLeftY,
                fakeBall.topLeftX,
                fakeBall.topLeftY - fakeBall.HEIGHT,
                fakeBall.topLeftX + fakeBall.WIDTH,
                fakeBall.topLeftY - fakeBall.HEIGHT,
                // Ball Triangle 2
                fakeBall.topLeftX + fakeBall.WIDTH,
                fakeBall.topLeftY,
                fakeBall.topLeftX,
                fakeBall.topLeftY,
                fakeBall.topLeftX + fakeBall.WIDTH,
                fakeBall.topLeftY - fakeBall.HEIGHT,
            
                // Player Triangle 1
                player.topLeftX,
                player.topLeftY,
                player.topLeftX,
                player.topLeftY - player.HEIGHT,
                player.topLeftX + player.WIDTH,
                player.topLeftY - player.HEIGHT,
                // Player Triangle 2
                player.topLeftX + player.WIDTH,
                player.topLeftY,
                player.topLeftX,
                player.topLeftY,
                player.topLeftX + player.WIDTH,
                player.topLeftY - player.HEIGHT,
                
                // Board Triangle 1
                board.topLeftX,
                board.topLeftY,
                board.topLeftX,
                board.topLeftY - board.HEIGHT,
                board.topLeftX + board.WIDTH,
                board.topLeftY - board.HEIGHT,
                // Board Triangle 2
                board.topLeftX + board.WIDTH,
                board.topLeftY,
                board.topLeftX,
                board.topLeftY,
                board.topLeftX + board.WIDTH,
                board.topLeftY - board.HEIGHT,
            };
            
        
        vertexData =
            ByteBuffer
                .allocateDirect(
                    tableVerticesWithTriangles.length * BYTES_PER_FLOAT)
                .order(ByteOrder.nativeOrder()).asFloatBuffer();

        vertexData.put(tableVerticesWithTriangles);
        
    }

    @Override
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {
        GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        
        int vertexShader =
            ShaderUtility.compileVertexShader(ShaderUtility
                .readTextFileFromResource(context, R.raw.vertex_shader));

        int fragmentShader =
            ShaderUtility.compileFragmentShader(ShaderUtility
                .readTextFileFromResource(context, R.raw.fragment_shader));

        program = ShaderUtility.linkProgram(vertexShader, fragmentShader);

        GLES20.glUseProgram(program);

        aPositionLocation = GLES20.glGetAttribLocation(program, A_POSITION);
        uColorLocation = GLES20.glGetUniformLocation(program, U_COLOR);
        uPositionOffsetLocation =
            GLES20.glGetUniformLocation(program, U_POSITION_OFFSET);
        uMatrixLocation = GLES20.glGetUniformLocation(program, U_MATRIX);

        // Bind our data, specified by the variable vertexData, to the vertex
        // attribute at location A_POSITION_LOCATION.
        vertexData.position(0);
        GLES20.glVertexAttribPointer(aPositionLocation,
            POSITION_COMPONENT_COUNT, GLES20.GL_FLOAT, false, 0, vertexData);
        
        GLES20.glEnableVertexAttribArray(aPositionLocation);
        
    }

    @Override
    public void onSurfaceChanged(GL10 glUnused, int width, int height) {
        // Set the OpenGL viewport to fill the entire surface.
        GLES20.glViewport(0, 0, width, height);
        
        if (width > height) {
            float aspectRatio = (float) width / (float) height;
            Matrix.orthoM(projectionMatrix, 0, -aspectRatio, aspectRatio,
                -1.0f, 1.0f, -1.0f, 1.0f);
        } else {
            float aspectRatio = (float) height / (float) width;
            Matrix.orthoM(projectionMatrix, 0, -1.0f, 1.0f, -aspectRatio,
                aspectRatio, -1.0f, 1.0f);
        }
        
    }

    @Override
    public void onDrawFrame(GL10 glUnused) {
        // Update game logic
        for (Float f : queueOfTouchCoordinates) {
            player.touch(f);
        }
        queueOfTouchCoordinates.clear();

        gameTick();

        // Clear the rendering surface and start drawing.
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
        GLES20.glUniformMatrix4fv(uMatrixLocation, 1, false, projectionMatrix,
            0);
        
        board.draw();
        
        for (Ball b : balls){
            b.draw();
        }
        
        player.draw();

    }

    private void gameTick() {
        
        player.tick();
        
        if (spawnCooldown <= 0){
            
            float y = ((float)Math.random()*2.0f)-1;
            float red = ((float)Math.random()*0.8f);
            float green = ((float)Math.random()*0.8f);
            float blue = ((float)Math.random()*0.8f);
            
            
            
            Ball b = new Ball(1.0f, 0.0f, ballSpeed, 0, 0.2f+red, 0.2f+green, 0.2f+blue);
            b.move(0.0f, y);
            balls.add(b);
            System.out.println("BALL ADDED "+balls.size());
            spawnCooldown = ticksBetweenSpawns;
            if (reductionCooldown <= 0){
                if (ticksBetweenSpawns > 5){
                    ticksBetweenSpawns -= 1;
                }
                reductionCooldown = 5;
            }
            reductionCooldown--;
            ballSpeed += 0.0002;
            
        }
        spawnCooldown--;
        
        int i = 0;
        while (i < balls.size()){
            balls.get(i).tick();
            
            
            if (balls.get(i).isDead()){
                balls.remove(i);
                score++;
            }
            else {
                i++;
            }
        }
        
        for (Ball b : balls){
            if (b.intersects(player)){
                playerLose(score);
            }
        }
    }

    @Override
    public void playerLose(int number) {
        GameOverActivity.retry = 2;
        MenuActivity.dis.gameOver("You dodged "+number+" balls!\n Do you like balls?\n Want to play again?");
    }

    @Override
    public void playerWin() {
        // Theres no winning in dodgeball
    }

}




Java Source Code List

com.pong.android.GameOverActivity.java
com.pong.android.IFGameEvents.java
com.pong.android.MenuActivity.java
com.pong.android.PongActivity.java
com.pong.android.modell.breakout.BreakOutActivity.java
com.pong.android.modell.breakout.BreakOutBall.java
com.pong.android.modell.breakout.BreakOutPlayer.java
com.pong.android.modell.breakout.BreakOutRenderer.java
com.pong.android.modell.breakout.Brick.java
com.pong.android.modell.breakout.Rectangle.java
com.pong.android.modell.dodgeball.Ball.java
com.pong.android.modell.dodgeball.DodgeballActivity.java
com.pong.android.modell.dodgeball.DodgeballPlayer.java
com.pong.android.modell.dodgeball.DodgeballRenderer.java
com.pong.android.modell.dodgeball.Rectangle.java
com.pong.android.modell.pong.Board.java
com.pong.android.modell.pong.PongBall.java
com.pong.android.modell.pong.PongOpponent.java
com.pong.android.modell.pong.PongPlayer.java
com.pong.android.modell.pong.PongRenderer.java
com.pong.android.modell.pong.Rectangle.java
com.pong.android.util.ShaderUtility.java