Thursday 21 November 2013

XMPP Client Android GTalk Sample



Before doing this app Download ASmack Library and Enable Internet permission in you manifiest.


 public class ASmackAsync extends AsyncTask<String, Void, String>{  
 private ProgressDialog pd;  
 private Context ctxt;  
 public ASmackAsync(Context ctxt) {  
 this.ctxt=ctxt;  
 }  
 @Override  
 protected void onPreExecute() {  
 super.onPreExecute();  
 pd=new ProgressDialog(ctxt);  
 pd.show();  
 pd.setContentView(new ProgressBar(ctxt), new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));  
 }  
 @Override  
 protected String doInBackground(String... params) {  
 return ASmack.connectGServer(params[0], params[1]);  
 }  
 @Override  
 protected void onPostExecute(String result) {  
 super.onPostExecute(result);  
 pd.dismiss();  
 if(result==ASmack.GCONNECT){  
 Toast.makeText(ctxt, "Connected",10).show();  
 }else{  
 Toast.makeText(ctxt, result,10).show();  
 }  
 }  
 }  
 public class ASmack {  
 public static String GCONNECT="GCONNECTED";  
 public static XMPPConnection gconn;  
 public static String connectGServer(String user,String pass){  
 String host="talk.google.com";  
 int port=5222;  
 String service="gmail.com";  
 ConnectionConfiguration config=new ConnectionConfiguration(host,port,service);  
 gconn=new XMPPConnection(config);  
 SASLAuthentication.supportSASLMechanism("PLAIN", 0);  
 config.setDebuggerEnabled(true);  
 try {  
 gconn.connect();  
 Log.i("Host", gconn.getHost());  
 gconn.login(user, pass);  
 } catch (XMPPException e) {  
 e.printStackTrace();  
 }  
 return GCONNECT;  
 }  
 }  



Thursday 28 March 2013

3D Game Tutorial 1 Android

1. Set Activity as a Game View
GameView:- Here our game will be run.
 public class MainActivity extends Activity {  
 @Override  
 protected void onCreate(Bundle savedInstanceState) {  
 super.onCreate(savedInstanceState);  
 GLSurfaceView view=new GLSurfaceView(this);  
 view.setRenderer(new MyRender());  
 setContentView(view);  
 }  
  }  


GLSurfaceView: on Activity we are setting the game view. Using this class, we can set our game view.
So we are creating the object of GLSurfaceView and giving the reference of Activity class. Using GLSurfaceView object we can handle activity operations.
setRenderer(new MyRender): To render(drawing) on the screen we need one Render class(MyRender) which can perform render operations.
and at last we are setting the view of an activity.
2. Implementing the Renderer 
 public class MyRender implements Renderer {  
 Cube cb=new Cube();  
 @Override  
 public void onDrawFrame(GL10 gl) {  
 gl.glMatrixMode(GL10.GL_MODELVIEW);  
 gl.glLoadIdentity();  
 gl.glTranslatef(0, 0, -1);  
 cb.draw(gl);  
 }  
 @Override  
 public void onSurfaceChanged(GL10 gl, int width, int height) {  
 gl.glViewport(0, 0, width, height);  
 float ar=(float)width/height;  
 gl.glMatrixMode(GL10.GL_PROJECTION);  
 gl.glLoadIdentity();  
 //gl.glOrthof(-1, 1, -1, 1, 1, -1);  
 gl.glFrustumf(-ar, ar, -1, 1, 1, 10);  
 }  
 @Override  
 public void onSurfaceCreated(GL10 gl, EGLConfig config) {  
 }  
 }  

Our MyRender class implementing Renderer method. When we call set Render method in Activity.
First of all, our onSurfaceCreated method called and Its create the surface for an render.
After that,onSurfaceChanged method called and Its create our viewport to draw.
At Last, onDrawFrame called and this method is the game loop. It will execute continously like infinite loop.
if we want to draw any thing on the screen and want to perform any operation on it, we must write our code into onDrawFrame.

onSurfaceChanged()
we are going to project our view so we are setting projection matrix.
we will calculate the aspect ratio to correct the dispaly ( we can correct horizontal display or vertical display).
and setting on the frustum.
and Finally.
onDrawFrame()
It is a game loop. So here we transalting our view with respect to z. So we are setting ModelView matrix. and Translating with respect to z.

3.Creating Cube Class

 public class Cube {  
 float vertices1[]={  
 -1,-1,  
 1,-1,  
 1,1,  
 -1,1,  
 };  
 byte indices1[]={  
 0,1,2,0,2,3 };  
 FloatBuffer vb;  
 ByteBuffer ib;  
 Cube(){  
 ByteBuffer bb=ByteBuffer.allocateDirect(vertices1.length*4);  
 bb.order(ByteOrder.nativeOrder());  
 vb=bb.asFloatBuffer();  
 vb.put(vertices1);  
  vb.position(0);  
 ib=ByteBuffer.allocateDirect(indices1.length);  
 ib.put(indices1);  
 ib.position(0);  
 }  
 void draw(GL10 gl){  
 gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);  
 gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vb);  
 gl.glDrawElements(GL10.GL_TRIANGLES, 6, GL10.GL_UNSIGNED_BYTE, ib);  
  gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);  
 }  
  }  


vertices[]


First of all we will set vertices and set the indices.
vertices    indices
 -1,-1           0
  1,-1           1
  1,1            2
  -1,1           3
in indices we are setting 0,1,2 means first Triangle and 0,2,3 means second Triangle means complete square.

Cube()
In cube constructor we are preparing the buffer to store the arrays. for the FloatBuffer we have to calculate the byte to store in Float buffer. so we are calculating the bytes and storing into in and setting the proper order. indices already in bytes so we don't need to convert.
draw(GL10 gl)
In draw method,
First of all, we will enable client vertax array(becuase this operation happeing in client side not an opengl side) to store the data into it and using the VertexPointer method we will store vertex buffer into it.
After that using DrawElements methods we will set our indexbuffer and draw the square.

References:-
http://songho.ca/opengl/index.html
Books:-
Mathematics for 3D Game Programming and Computer Graphics, Eric Lengyel.
Pro OpengGL ES for Android, Mike Smithwick and Mayank Verma

Things after Kotlin Android Extensions

Things after Kotlin Android Extensions(KTX) I remembered in MVVM when I have to declare ViewModel, I initialize View Model like this. p...