Android Game Development Android basics About Android Linux
Android Game Development Android basics
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)
Top mobile operating systems
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
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
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
Activity lifecycle
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
IDE basics Net. Beans 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/addingpackages. html
Set up virtual devices
Presets Set up virtual devices
API levels
API levels
New Project (Net. Beans)
New Project
New Project
Android. Manifest. xml <? xml version="1. 0" encoding="utf-8"? > <manifest xmlns: android="http: //schemas. android. com/apk/res/android" package="gamedev. android. UEngine. Platform. Game" android: version. Code="1" Our application version android: version. Name="1. 0"> <application android: label="@string/app_name" > Activity to start <activity android: name="Main. Activity" android: label="@string/app_name"> Icon label of our <intent-filter> Activity <action android: name="android. intent. action. MAIN"We />can change this to e. g. : /> <category android: name="android. intent. category. LAUNCHER" “My. Platform. Game” </intent-filter> </activity> </application> </manifest>
Start a virtual device
Build and run the project Main. Activity. java: set. Content. View(R. layout. main); Main. xml <? xml version="1. 0" encoding="utf-8"? > <Linear. Layout xmlns: android="http: //schemas. android. com/apk/res/android" android: orientation="vertical" android: layout_width="fill_parent" android: layout_height="fill_parent" > <Text. View android: layout_width="fill_parent" android: layout_height="wrap_content" android: text="Hello World, Main. Activity" /> </Linear. Layout>
Open. GL ES – Main. Activity. java import android. app. Activity; import android. opengl. GLSurface. View; import android. os. Bundle; public class Main. Activity extends Activity { private GLSurface. View m. GLView; @Override public void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); m. GLView = new Main. Surface. View(this); set. Content. View(m. GLView); } }
Open. GL ES – Main. Surface. View. java package gamedev. android. UEngine. Platform. Game; import android. content. Context; import android. opengl. GLSurface. View; public class Main. Surface. View extends GLSurface. View { private Main. Renderer m. Renderer; } public Main. Surface. View(Context context) { super(context); this. m. Renderer = new Main. Renderer(get. Context()); set. Renderer(this. m. Renderer); }
Open. GL ES – Main. Renderer. java package gamedev. android. UEngine. Platform. Game; import android. content. Context; import android. opengl. GLSurface. View; import javax. microedition. khronos. egl. EGLConfig; import javax. microedition. khronos. opengles. GL 10; public class Main. Renderer implements GLSurface. View. Renderer{ private Context context; public Main. Renderer(Context context) { this. context = context; } public void on. Surface. Created(GL 10 gl, EGLConfig config) { gl. Clear. Color(0. 5 F, 1. 0 F, 1. 0 F); } public void on. Draw. Frame(GL 10 gl) { gl. Clear(gl. GL_COLOR_BUFFER_BIT | gl. GL_DEPTH_BUFFER_BIT ); } public void on. Surface. Changed(GL 10 gl, int width, int height) { gl. Viewport(0, 0, width, height); } }
Build and run
Draw something - Main. Renderer New imports import java. nio. Byte. Buffer; import java. nio. Byte. Order; import java. nio. Float. Buffer; New data member protected Float. Buffer VB = null; public float rot. Angle = 0;
Draw something - Main. Renderer public void on. Surface. Created(GL 10 gl, EGLConfig config) { gl. Clear. Color(0. 5 F, 1. 0 F, 1. 0 F); float[] VCoords = { -1. 0 F, 0. 0 F, 1. 0 F, 0. 0 F, -1. 0 F, 0. 0 F }; } Byte. Buffer vbb = Byte. Buffer. allocate. Direct(VCoords. length * 4); vbb. order(Byte. Order. native. Order()); this. VB = vbb. as. Float. Buffer(); this. VB. put(VCoords); this. VB. position(0);
Draw something - Main. Renderer public void on. Draw. Frame(GL 10 gl) { gl. Clear(gl. GL_COLOR_BUFFER_BIT | gl. GL_DEPTH_BUFFER_BIT ); gl. Matrix. Mode(gl. GL_MODELVIEW); gl. Load. Identity(); gl. Translatef(0, 0, -6); gl. Color 4 f(1, 0, 0, 1); gl. Rotatef(rot. Angle, 0, 1, 0); gl. Enable. Client. State(gl. GL_VERTEX_ARRAY); gl. Vertex. Pointer(3, gl. GL_FLOAT, 0, this. VB); gl. Draw. Arrays(gl. GL_TRIANGLES, 0, 6); gl. Disable. Client. State(gl. GL_VERTEX_ARRAY); } rot. Angle += 2. 5 f;
Draw something - Main. Renderer public void on. Surface. Changed(GL 10 gl, int width, int height) { gl. Viewport(0, 0, width, height); float ratio = (float) width / height; gl. Matrix. Mode(gl. GL_PROJECTION); gl. Load. Identity(); gl. Frustumf(-ratio, -1, 1, 1. 0 f, 10); }
Build and run
Input handling - Main. Surface. View protected float last. X = -1; public boolean on. Touch. Event(Motion. Event e) { float x = e. get. X(); switch (e. get. Action()) { case Motion. Event. ACTION_MOVE: float dx = last. X == -1 ? 0 : x - last. X; m. Renderer. rot. Angle += dx * 1. 0 f; break; } } last. X = x; return true; Also remove rot. Angle += 2. 5 f; from Main. Renderer. on. Draw. Frame
Build and run Quad can be rotated with the mouse in emulator
Orientation – Full screen Fix orientation to landscape (typical in games) Activity: : on. Create(): set. Requested. Orientation(Activity. Info. SCREEN_ORIENTATION_LANDSCAPE); Set to full screen (typical in games) Activity: : on. Create(): request. Window. Feature(Window. FEATURE_NO_TITLE); get. Window(). set. Flags(Window. Manager. Layout. Params. FLAG_FULLSCREEN, Window. Manager. Layout. Params. FLAG_FULLSCREEN); Rotate emulator to landscape mode: Ctrl+F 12
Compile and run
The End
- Slides: 33