android basics. about android linux based operating system open source designed for handheld devices...

33
Android Game Development Android basics

Upload: gerard-crooker

Post on 02-Apr-2015

215 views

Category:

Documents


1 download

TRANSCRIPT

  • Slide 1

Android basics Slide 2 About Android Linux based operating system Open source Designed for handheld devices Developed by Android Inc. Google (2005) Open Handset Alliance (2007) Very successful system (75% sales in 2012) Slide 3 Top mobile operating systems Slide 4 Android basics Can run Java code on Dalvik VM Multi-user system (each application is a different user) Unique user ID Has its own virtual machine Has its own Linux process Principle of least privilege Applications can share data Slide 5 Application components Building blocks of an application 4 types Activity Single screen with UI Service Backgroud process without UI Content provider Manages a shared set of application data Broadcast receiver Responds to system-wide broadcast announcements Slide 6 Applications All components of an application runs in the same process The system tries to keep a process as long as possible Applications are stored in a stack The systems duty to destroy the applications The system uses importance hierarchy Foreground processes Visible process Service process Background process Empty process Slide 7 Activity lifecycle Slide 8 Android development Andriod SDK (http://developer.android.com/sdk) Java API libraries for different platforms SDK manager AVD Manager (emulator) Developer IDE Eclipse with ADT (Juno has it by default) Netbeans with NBAndroid plugin Android device USB Driver Enable USB debugging Useful link: http://developer.android.com Slide 9 IDE basics NetBeans https://kenai.com/projects/nbandroid/pages/Install https://kenai.com/projects/nbandroid/pages/Intro Eclipse For ADT bundle everything is set up http://developer.android.com/sdk/installing/bundle.html For Juno install is not required, for other existing Eclipse installs: http://developer.android.com/sdk/installing/adding- packages.html Add platforms and packages http://developer.android.com/sdk/installing/adding- packages.html Slide 10 Set up virtual devices Slide 11 Presets Slide 12 API levels Slide 13 Slide 14 New Project (NetBeans) Slide 15 New Project Slide 16 Slide 17 Our application version Activity to start Icon label of our Activity We can change this to e.g.: MyPlatformGame"> AndroidManifest.xml Our application version Activity to start Icon label of our Activity We can change this to e.g.: MyPlatformGame Slide 18 Start a virtual device Slide 19 Build and run the project"> MainActivity.java: setContentView(R.layout.main); Main.xml Build and run the project Slide 20 OpenGL ES MainActivity.java import android.app.Activity; import android.opengl.GLSurfaceView; import android.os.Bundle; public class MainActivity extends Activity { private GLSurfaceView mGLView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mGLView = new MainSurfaceView(this); setContentView(mGLView); } Slide 21 OpenGL ES MainSurfaceView.java package gamedev.android.UEngine.PlatformGame; import android.content.Context; import android.opengl.GLSurfaceView; public class MainSurfaceView extends GLSurfaceView { private MainRenderer mRenderer; public MainSurfaceView(Context context) { super(context); this.mRenderer = new MainRenderer(getContext()); setRenderer(this.mRenderer); } Slide 22 OpenGL ES MainRenderer.java package gamedev.android.UEngine.PlatformGame; import android.content.Context; import android.opengl.GLSurfaceView; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; public class MainRenderer implements GLSurfaceView.Renderer{ private Context context; public MainRenderer(Context context) { this.context = context; } public void onSurfaceCreated(GL10 gl, EGLConfig config) { gl.glClearColor(0.5F, 0.5F, 1.0F, 1.0F); } public void onDrawFrame(GL10 gl) { gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT ); } public void onSurfaceChanged(GL10 gl, int width, int height) { gl.glViewport(0, 0, width, height); } Slide 23 Build and run Slide 24 Draw something - MainRenderer New imports import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; New data member protected FloatBuffer VB = null; public float rotAngle = 0; Slide 25 Draw something - MainRenderer public void onSurfaceCreated(GL10 gl, EGLConfig config) { gl.glClearColor(0.5F, 0.5F, 1.0F, 1.0F); float[] VCoords = { -1.0F, -1.0F, 0.0F, 1.0F, -1.0F, 0.0F, 1.0F, 1.0F, 0.0F, -1.0F, -1.0F, 0.0F, 1.0F, 1.0F, 0.0F, -1.0F, 1.0F, 0.0F }; ByteBuffer vbb = ByteBuffer.allocateDirect(VCoords.length * 4); vbb.order(ByteOrder.nativeOrder()); this.VB = vbb.asFloatBuffer(); this.VB.put(VCoords); this.VB.position(0); } Slide 26 Draw something - MainRenderer public void onDrawFrame(GL10 gl) { gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT ); gl.glMatrixMode(gl.GL_MODELVIEW); gl.glLoadIdentity(); gl.glTranslatef(0,0,-6); gl.glColor4f(1, 0, 0, 1); gl.glRotatef(rotAngle, 0, 1, 0); gl.glEnableClientState(gl.GL_VERTEX_ARRAY); gl.glVertexPointer(3, gl.GL_FLOAT, 0, this.VB); gl.glDrawArrays(gl.GL_TRIANGLES, 0, 6); gl.glDisableClientState(gl.GL_VERTEX_ARRAY); rotAngle += 2.5f; } Slide 27 Draw something - MainRenderer public void onSurfaceChanged(GL10 gl, int width, int height) { gl.glViewport(0, 0, width, height); float ratio = (float) width / height; gl.glMatrixMode(gl.GL_PROJECTION); gl.glLoadIdentity(); gl.glFrustumf(-ratio, ratio, -1, 1, 1.0f, 10); } Slide 28 Build and run Slide 29 Input handling - MainSurfaceView protected float lastX = -1; public boolean onTouchEvent(MotionEvent e) { float x = e.getX(); switch (e.getAction()) { case MotionEvent.ACTION_MOVE: float dx = lastX == -1 ? 0 : x - lastX; mRenderer.rotAngle += dx * 1.0f; break; } lastX = x; return true; } Also remove rotAngle += 2.5f; from MainRenderer.onDrawFrame Slide 30 Build and run Quad can be rotated with the mouse in emulator Slide 31 Orientation Full screen Fix orientation to landscape (typical in games) Activity::onCreate(): setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); Set to full screen (typical in games) Activity::onCreate(): requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); Rotate emulator to landscape mode: Ctrl+F12 Slide 32 Compile and run Slide 33