OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Ocean.java
Go to the documentation of this file.
1 /* Copyright (c) 2015-2017, ARM Limited and Contributors
2  *
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge,
6  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  */
20 
21 package com.arm.malideveloper.openglessdk.ocean;
22 
23 import java.io.File;
24 import java.io.InputStream;
25 import java.io.BufferedOutputStream;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import android.content.res.AssetManager;
29 import android.util.Log;
30 
31 import javax.microedition.khronos.egl.EGLConfig;
32 import javax.microedition.khronos.opengles.GL10;
33 import android.content.Context;
34 import android.opengl.GLSurfaceView;
35 import android.os.Bundle;
36 import android.view.Window;
37 import android.view.WindowManager;
38 import com.arm.malideveloper.openglessdk.*;
39 
40 class OceanView extends MaliSamplesView
41 {
42  public OceanView(Context context)
43  {
44  super(context, GlesVersion.GLES3);
45  }
46 
47  @Override protected void setRendererCallback()
48  {
49  setRenderer(new Renderer());
50  }
51 
52  @Override protected void destroyContextCallback()
53  {
54  Ocean.uninit();
55  }
56 
57  protected class Renderer implements GLSurfaceView.Renderer
58  {
59  public void onDrawFrame(GL10 gl)
60  {
61  Ocean.step();
62  }
63 
64  public void onSurfaceChanged(GL10 gl, int width, int height)
65  {
66  Ocean.init(width, height);
67  }
68 
69  public void onSurfaceCreated(GL10 gl, EGLConfig config)
70  {
71  }
72  }
73 };
74 
75 public class Ocean extends MaliSamplesActivity
76 {
77  OceanView mView;
78  private static android.content.Context applicationContext = null;
79  private static String assetsDirectory = null;
80  private static String LOGTAG = "libNative";
81 
82  public static native void init(int width, int height);
83  public static native void step();
84  public static native void uninit();
85 
86  @Override protected void onCreate(Bundle savedInstanceState)
87  {
88  super.onCreate(savedInstanceState);
89 
90  this.requestWindowFeature(Window.FEATURE_NO_TITLE);
91  getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
92  WindowManager.LayoutParams.FLAG_FULLSCREEN);
93 
94  applicationContext = getApplicationContext();
95  assetsDirectory = applicationContext.getFilesDir().getPath() + "/";
96 
97  extractAssets();
98 
99  mView = new OceanView(getApplication());
100  mView.getHolder().setFixedSize(1280, 720);
101  setContentView(mView);
102  }
103 
104  @Override protected void onPause()
105  {
106  super.onPause();
107  mView.onPause();
108  }
109 
110  @Override protected void onResume()
111  {
112  super.onResume();
113  mView.onResume();
114  }
115 
116  @Override protected void onDestroy()
117  {
118  super.onDestroy();
119  }
120 
121  private byte[] loadAsset(String file) {
122  try {
123  InputStream stream = getAssets().open(file);
124  int len = stream.available();
125  byte[] buf = new byte[len];
126  stream.read(buf, 0, len);
127  stream.close();
128  return buf;
129  } catch (IOException e) {
130  return null;
131  }
132  }
133 
134  private void extractAssets()
135  {
136  try {
137  String[] assets = getAssets().list("");
138 
139  for (String file : assets) {
140  byte[] buf = loadAsset(file);
141  if (buf == null)
142  continue;
143 
144  String path = assetsDirectory + file;
145  BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(new File(path)));
146 
147  writer.write(buf, 0, buf.length);
148  writer.flush();
149  writer.close();
150 
151  Log.d(LOGTAG, "Asset " + path + " extracted successfully.");
152  }
153  }
154  catch (IOException e) {
155  Log.e(LOGTAG, "Failure in extractAssets(): " + e.toString());
156  }
157  }
158 
159  static
160  {
161  // Load the NDK library for this example, built with ndk-build
162  System.loadLibrary("Native");
163  }
164 }
static native void init(int width, int height)
void onSurfaceCreated(GL10 gl, EGLConfig config)
Definition: Ocean.java:69
GLint GLsizei GLsizei height
Definition: gl2ext.h:179
static android.content.Context applicationContext
Definition: Ocean.java:78
void onCreate(Bundle savedInstanceState)
Definition: Ocean.java:86
void onSurfaceChanged(GL10 gl, int width, int height)
Definition: Ocean.java:64
GLenum GLuint GLenum GLsizei length
Definition: gl2ext.h:134
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: gl2ext.h:134
GLint GLsizei width
Definition: gl2ext.h:179