From 389796649ffb9165c5751ff6d8a15049d3c823aa Mon Sep 17 00:00:00 2001 From: kenkeiras Date: Mon, 3 Jul 2017 20:43:22 +0200 Subject: [PATCH] Base canvas usage. --- .../codigoparallevar/minicards/DrawView.java | 23 ++++++ .../minicards/TestActivity.java | 13 +-- .../minicards/rendering/MyGLSurfaceView.java | 20 ----- .../minicards/rendering/MyGlRenderer.java | 45 ----------- .../minicards/rendering/Square.java | 67 ---------------- .../minicards/rendering/Triangle.java | 80 ------------------- 6 files changed, 27 insertions(+), 221 deletions(-) create mode 100644 app/src/main/java/com/codigoparallevar/minicards/DrawView.java delete mode 100644 app/src/main/java/com/codigoparallevar/minicards/rendering/MyGLSurfaceView.java delete mode 100644 app/src/main/java/com/codigoparallevar/minicards/rendering/MyGlRenderer.java delete mode 100644 app/src/main/java/com/codigoparallevar/minicards/rendering/Square.java delete mode 100644 app/src/main/java/com/codigoparallevar/minicards/rendering/Triangle.java diff --git a/app/src/main/java/com/codigoparallevar/minicards/DrawView.java b/app/src/main/java/com/codigoparallevar/minicards/DrawView.java new file mode 100644 index 0000000..c99e47c --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/DrawView.java @@ -0,0 +1,23 @@ +package com.codigoparallevar.minicards; + +import android.content.Context; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.view.View; + +class DrawView extends View { + + Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); + + public DrawView(Context context) { + super(context); + paint.setColor(Color.RED); + } + + @Override + public void onDraw(Canvas canvas){ + canvas.drawLine(0, 0, 20, 20, paint); + canvas.drawLine(20, 0, 0, 20, paint); + } +} diff --git a/app/src/main/java/com/codigoparallevar/minicards/TestActivity.java b/app/src/main/java/com/codigoparallevar/minicards/TestActivity.java index 9945027..89b3542 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/TestActivity.java +++ b/app/src/main/java/com/codigoparallevar/minicards/TestActivity.java @@ -1,22 +1,17 @@ package com.codigoparallevar.minicards; -import android.opengl.GLSurfaceView; +import android.graphics.Color; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; -import com.codigoparallevar.minicards.rendering.MyGLSurfaceView; - public class TestActivity extends AppCompatActivity { - private GLSurfaceView mGLView; - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - // Create a GLSurfaceView instance and set it - // as the ContentView for this Activity. - mGLView = new MyGLSurfaceView(this); - setContentView(mGLView); + DrawView drawView = new DrawView(this); + drawView.setBackgroundColor(Color.BLACK); + setContentView(drawView); } } diff --git a/app/src/main/java/com/codigoparallevar/minicards/rendering/MyGLSurfaceView.java b/app/src/main/java/com/codigoparallevar/minicards/rendering/MyGLSurfaceView.java deleted file mode 100644 index d482f6c..0000000 --- a/app/src/main/java/com/codigoparallevar/minicards/rendering/MyGLSurfaceView.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.codigoparallevar.minicards.rendering; - -import android.content.Context; -import android.opengl.GLSurfaceView; - -public class MyGLSurfaceView extends GLSurfaceView { - - private final MyGlRenderer mRenderer; - - public MyGLSurfaceView(Context context) { - super(context); - - setEGLContextClientVersion(2); - - mRenderer = new MyGlRenderer(); - - setRenderer(mRenderer); - setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); - } -} diff --git a/app/src/main/java/com/codigoparallevar/minicards/rendering/MyGlRenderer.java b/app/src/main/java/com/codigoparallevar/minicards/rendering/MyGlRenderer.java deleted file mode 100644 index 5531c8f..0000000 --- a/app/src/main/java/com/codigoparallevar/minicards/rendering/MyGlRenderer.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.codigoparallevar.minicards.rendering; - -import android.opengl.GLES20; -import android.opengl.GLSurfaceView; -import android.util.Log; - -import javax.microedition.khronos.egl.EGLConfig; -import javax.microedition.khronos.opengles.GL10; - -class MyGlRenderer implements GLSurfaceView.Renderer { - - private Triangle mTriangle; - private Square mSquare; - - @Override - public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) { - GLES20.glClearColor(0.75f, 0.81f, 1.0f, 1.0f); - - mTriangle = new Triangle(); - mSquare = new Square(); - } - - @Override - public void onSurfaceChanged(GL10 gl10, int width, int height) { - GLES20.glViewport(0, 0, width, height); - } - - @Override - public void onDrawFrame(GL10 gl10) { - GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); - mTriangle.draw(); - } - - public static int loadShader(int type, String shaderCode){ - // create a vertex shader type (GLES20.GL_VERTEX_SHADER) - // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER) - int shader = GLES20.glCreateShader(type); - - // add the source code to the shader and compile it - GLES20.glShaderSource(shader, shaderCode); - GLES20.glCompileShader(shader); - - return shader; - } -} diff --git a/app/src/main/java/com/codigoparallevar/minicards/rendering/Square.java b/app/src/main/java/com/codigoparallevar/minicards/rendering/Square.java deleted file mode 100644 index 42846b7..0000000 --- a/app/src/main/java/com/codigoparallevar/minicards/rendering/Square.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.codigoparallevar.minicards.rendering; - -import android.opengl.GLES20; - -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.nio.FloatBuffer; -import java.nio.ShortBuffer; -import java.sql.SQLInvalidAuthorizationSpecException; - -public class Square { - - private final String vertexShaderCode = - "attribute vec4 vPosition;" + - "void main(){" + - "gl_Position = vPosition;" + - "}"; - - private final String fragmentShaderCode = - "precision medium float;" + - "uniform vec4 vColor;" + - "void main(){" + - "gl_FragColor = vColor;" + - "}"; - private final int mProgram; - - - private FloatBuffer vertexBuffer; - private ShortBuffer drawListBuffer; - - static final int COORDS_PER_VERTEX = 3; - static float squareCoords[] = { - -0.5f, 0.5f, 0.0f, // top left - -0.5f, -0.5f, 0.0f, // bottom left - 0.5f, -0.5f, 0.0f, // bottom right - 0.5f, 0.5f, 0.0f, // top right - }; - - private short drawOrder[] = { 0, 1, 2, 0, 2, 3 }; // order to draw vertices - - public Square(){ - ByteBuffer bb = ByteBuffer.allocateDirect(squareCoords.length * 4); - bb.order(ByteOrder.nativeOrder()); - - vertexBuffer = bb.asFloatBuffer(); - vertexBuffer.put(squareCoords); - vertexBuffer.position(0); - - ByteBuffer dlb = ByteBuffer.allocateDirect(drawOrder.length * 2); - dlb.order(ByteOrder.nativeOrder()); - drawListBuffer = dlb.asShortBuffer(); - drawListBuffer.put(drawOrder); - drawListBuffer.position(0); - - - int vertexShader = MyGlRenderer.loadShader(GLES20.GL_VERTEX_SHADER, - vertexShaderCode); - int fragmentShader = MyGlRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER, - fragmentShaderCode); - - mProgram = GLES20.glCreateProgram(); - GLES20.glAttachShader(mProgram, vertexShader); - GLES20.glAttachShader(mProgram, fragmentShader); - - GLES20.glLinkProgram(mProgram); - } -} diff --git a/app/src/main/java/com/codigoparallevar/minicards/rendering/Triangle.java b/app/src/main/java/com/codigoparallevar/minicards/rendering/Triangle.java deleted file mode 100644 index d4c9a93..0000000 --- a/app/src/main/java/com/codigoparallevar/minicards/rendering/Triangle.java +++ /dev/null @@ -1,80 +0,0 @@ -package com.codigoparallevar.minicards.rendering; - -import android.opengl.GLES20; -import android.util.Log; - -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.nio.FloatBuffer; - -public class Triangle { - - private final String vertexShaderCode = - "attribute vec4 vPosition;" + - "void main(){" + - "gl_Position = vPosition;" + - "}"; - - private final String fragmentShaderCode = - "precision mediump float;" + - "uniform vec4 vColor;" + - "void main(){" + - "gl_FragColor = vColor;" + - "}"; - - private FloatBuffer vertexBuffer; - private final int mProgram; - - static final int COORDS_PER_VERTEX = 3; - static float triangleCoords[] = { - 0.0f, 0.622008459f, 0.0f, // top - -0.5f, -0.311004243f, 0.0f, // bottom left - 0.5f, -0.311004243f, 0.0f // bottom right - }; - - // Set color with red, green, blue and alpha (opacity) values - float color[] = { 0.63671875f, 0.76953125f, 0.22265625f, 1.0f }; - private int mPositionHandle; - private int mColorHandle; - - private final int vertexCount = triangleCoords.length / COORDS_PER_VERTEX; - private final int vertexStride = COORDS_PER_VERTEX * 4; // 4 bytes per vertex - - public Triangle(){ - ByteBuffer bb = ByteBuffer.allocateDirect(triangleCoords.length * 4); - bb.order(ByteOrder.nativeOrder()); - - vertexBuffer = bb.asFloatBuffer(); - vertexBuffer.put(triangleCoords); - vertexBuffer.position(0); - - int vertexShader = MyGlRenderer.loadShader(GLES20.GL_VERTEX_SHADER, - vertexShaderCode); - int fragmentShader = MyGlRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER, - fragmentShaderCode); - - mProgram = GLES20.glCreateProgram(); - GLES20.glAttachShader(mProgram, vertexShader); - GLES20.glAttachShader(mProgram, fragmentShader); - - GLES20.glLinkProgram(mProgram); - } - - public void draw(){ - GLES20.glUseProgram(mProgram); - - mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition"); - GLES20.glEnableVertexAttribArray(mPositionHandle); - - GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, - GLES20.GL_FLOAT, false, - vertexStride, vertexBuffer); - - mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor"); - - GLES20.glUniform4fv(mColorHandle, 1, color, 0); - Log.d("Triangle", "VB "+ vertexBuffer.get(1)); - GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount); - GLES20.glDisableVertexAttribArray(mPositionHandle); - } -}