Posted on

Tutorial Viral Live Video with Reaction

Preview FB

Please NOTE : Test it on Demo PAGE DON’T Take it live on your main PAGE.

So after making  a Poll for my page named Viral Pill on US Election. I never though that it will get 106k+ Likes on post.So i decided to make to make different polls using my skill of game development and Box2D but other polls did not got that much attention & then the method got copied by other sites and FB Live reach does not have that powerful effect on new Live Videos.


Let’s start the tutorial for page owners who want to publish Live Videos with diff. competition for Free.

  • You must have a page.
  • Download OBS Studio from https://obsproject.com/
  • A Windows RDP (if you don’t want to run your computer whole day)
  • That’s it

Download the files from here

Facebook Reaction Humor Pill

(The files are modified version of other script from different site)

Open your page

  • Click on Publish Posts
Facebook Publish Posts
Facebook Publish Posts
  • Click  on Videos Link on Left Side bar
Click on Videos
Click on Videos
  • Click on Live Button
Click on Live button
Click on Live button
  • Copy Stream Key
  • Click next & Let the tab opened!
Copy Stream Key
Copy Stream Key
  • Open Settings
Open Settings
Open Settings
  • Select Facebook Live from Servcie in Stream Tab & Paste the Stream key here & click on OK
Select Facebook Live OBS
Select Facebook Live OBS

Let’s change the settings in index.html which you have downloaded

Line 110 access_token Change access_token in “” to the one which you have extended

Line 111 change Post ID we will change it Later because we need the Live Video post id including page id

Let’s come back to OBS Studio

Click on + Icon

OBS Click Add
OBS Click Add

Click on Browser Source

Browser Source
Browser Source

Fill Text whatever you want!

Fill Name Humor PIll
Fill Name Humor PIll

 

Check Local File

Browse for the Path

Fill Width 1280 & Height 720 as the index.html is not that much responsive so fill this width x height

Browse for local Path
Browse for local Path

Start Streaming

Start Streaming OBS
Start Streaming OBS

Your will see the preview in the Facebook tab which have keep opened in the past

Preview FB
Preview Facebook

Now click on Go Live and open your page in new tab get the Facebook Page ID & Post ID

You can get Page ID by right click and copy link on your page profile PIC & get the Facebook Post ID by right click on the post time.

IMPORTANT: Now change your Post id in this Format PAGEID_POSTID .Click on settings icon in Sources Humor Pill (We named it Humor Pill) then click OK so that it can refresh the new POST ID.

Now we will have something like this

Posted on

Android Push Notification using GCM

GCM Server

Welcome to the Android Push Notification using GCM tutorial.

Introduction: Many app developers still struggle to implement Android Push Notification in their apps/games.So what is actually a push notification.Push Notification are used by developers to inform user about new messages,events or any other thing which they can see without opening their app.

In this tutorial i am going to tell you how to use GCM (Google Cloud Messaging) to send push notification on Android device.You can use this code in your android app,games made with LibGDX,Cocos2d-x with little modification.This tutorial contains PHP,MySQL & JAVA code.

Step by Step sheet

  • Getting project id & project number from Google developers site.
  • Application Code
  • Server side code (PHP,MySQL)
  • Sending push notifications

Let’s get started!

Getting project id & project number from Google developers site.

Go to this site https://developers.google.com/mobile/add?platform=android (Make sure you are logged in your google account)

  • Fill app name – Push Testing
  • Fill package name – com.developerhouse.push
  • Click on “continue to” button
Google Project Console
Google Project Console

 

wait for few seconds it will take some time.
click on enable Google cloud messaging

Enable Cloud Messaging
Enable Cloud Messaging

Now the project has been created we have to go on this link https://console.developers.google.com/ to edit IP address from which we can make request to send push messages & to get the API Key & Project ID

Select project from drop down on top ,note down the Project ID & Project Number

Select Project From Console
Select Project From Console

Now Click on Credentials sub menu button under API & auth

you will see something like this, note down the API Key and click on it

Select Credentials in Project Console
Select Credentials in Project Console

 

This is a optional step to make your push notification secure by adding the ip address of your server

Add IP Address
Add IP Address

add ip address & click on save button

Application Code

We have to write the code for android application which will send the device id first time so that it can be save on our server
Create a application with the same package name which we have used while setting up GCM. Add google play services library in your project

AndroidManifest.xml

[code language=”xml”]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.developerhouse.push"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="21" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.google.android.gms.gcm.GcmReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
<category android:name="com.developerhouse.push"/>
</intent-filter>
</receiver>

<service
android:name="com.developerhouse.push.MyGcmListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service
android:name="com.developerhouse.push.MyInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>
</service>
<service
android:name="com.developerhouse.push.RegistrationIntentService"
android:exported="false">
</service>
</application>

</manifest>

[/code]

This class is used for registering the Token in our web server .First it will get the token then it will save on our web server.

RegistrationIntentService.java

 

[code language=”java”]
package com.developerhouse.push;

import android.app.IntentService;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.google.android.gms.iid.InstanceID;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class RegistrationIntentService extends IntentService {

private static final String TAG = "RegServicePush";

public RegistrationIntentService() {
super(TAG);
}

@Override
protected void onHandleIntent(Intent intent) {

try {
InstanceID instanceID = InstanceID.getInstance(this);
String token = instanceID.getToken(Constants.GCM_SENDER_ID,
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

// TODO: Implement this method to send any registration to your app’s servers.
sendRegistrationToServer(token);

} catch (Exception e) {
Log.d(TAG, "Failed to complete token refresh", e);
}
// Notify UI that registration has completed, so the progress indicator can be hidden.

}

private void sendRegistrationToServer(String token) {MainActivity.newRegID=token;
WebServerRegistrationTask webServer=new WebServerRegistrationTask();
webServer.execute();
}

public class WebServerRegistrationTask extends AsyncTask<Void, Void, Void> {

@Override
protected Void doInBackground(Void… params) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(RegistrationIntentService.this);

URL url = null;
try {
url = new URL(Constants.WEB_SERVER_URL);
} catch (MalformedURLException e) {
e.printStackTrace();

sharedPreferences.edit().putString(Constants.PREF_GCM_REG_ID, "").apply();
}
Map<String, String> dataMap = new HashMap<String, String>();
dataMap.put("regID", MainActivity.newRegID);

StringBuilder postBody = new StringBuilder();
Iterator<Map.Entry<String, String>> iterator = dataMap.entrySet().iterator();

while (iterator.hasNext()) {
Entry<String,String> param = (Entry<String,String>) iterator.next();
postBody.append(param.getKey()).append(‘=’)
.append(param.getValue());
if (iterator.hasNext()) {
postBody.append(‘&’);
}
}
String body = postBody.toString();
byte[] bytes = body.getBytes();

HttpURLConnection conn = null;
try {
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setFixedLengthStreamingMode(bytes.length);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");

OutputStream out = conn.getOutputStream();
out.write(bytes);
out.close();
String response="";
InputStream is = null;
try {
is = conn.getInputStream();
int ch;
StringBuffer sb = new StringBuffer();
while ((ch = is.read()) != -1) {
sb.append((char) ch);
}
response=sb.toString();

} catch (IOException e) {
throw e;
} finally {
if (is != null) {
is.close();
}
}
int status = conn.getResponseCode();
if (status == 200) {
if(response.equals("1")){
sharedPreferences.edit().putString(Constants.PREF_GCM_REG_ID, MainActivity.newRegID).apply();
Intent registrationComplete = new Intent(Constants.SERVER_SUCCESS);
LocalBroadcastManager.getInstance(RegistrationIntentService.this).sendBroadcast(registrationComplete);
}
} else {
throw new IOException("Request failed with error code "
+ status);
}
} catch (ProtocolException pe) {
pe.printStackTrace();
sharedPreferences.edit().putString(Constants.PREF_GCM_REG_ID, "").apply();

} catch (IOException io) {
io.printStackTrace();
sharedPreferences.edit().putString(Constants.PREF_GCM_REG_ID, "").apply();

} finally {
if (conn != null) {
conn.disconnect();
}
}

return null;
}
}
}
[/code]

MyInstanceIDListenerService.java

[code language=”java”]
package com.developerhouse.push;

import android.content.Intent;
import com.google.android.gms.iid.InstanceIDListenerService;

public class MyInstanceIDListenerService extends InstanceIDListenerService {

@Override
public void onTokenRefresh() {

Intent intent = new Intent(this, RegistrationIntentService.class);
startService(intent);
}

}
[/code]

MyGcmListenerService.java

[code language=”java”]
package com.developerhouse.push;

import com.google.android.gms.gcm.GcmListenerService;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;

public class MyGcmListenerService extends GcmListenerService {

public static final int MESSAGE_NOTIFICATION_ID = 435345;
private NotificationManager mNotificationManager;

@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");

createNotification( message);
}

// Creates notification based on title and body received
private void createNotification( String body) {
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
long[] pattern = {500};

PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Push Notification Test").setVibrate(pattern)
.setStyle(new NotificationCompat.BigTextStyle().bigText(body))
.setContentText(body)
.setAutoCancel(true).setSound(sound);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(MESSAGE_NOTIFICATION_ID, mBuilder.build());

}
}
[/code]

Line 20 : MESSAGE_NOTIFICATION_ID is used to make notification same for example if you send another notification and user have not read the old one it will replace the old one
Line 24 : Method which will receive the content from GCM
Line 34 : Sound for notification
Line 35 : Vibration Pattern
Line 37 : Intent which will run on click of notification you can open link to browser or you can open the activity

Constants.java

[code language=”java”]
package com.developerhouse.push;
public class Constants {
public static String PREF_GCM_REG_ID = "PREF_GCM_REG_ID";
public static String GCM_SENDER_ID = "339415449819";
public static String WEB_SERVER_URL = "http://192.168.1.103/push/reg.php";
public static final String SERVER_SUCCESS="server_success";
}

[/code]

GCM_SENDER_ID = Project ID
WEB_SERVER_URL = URL to register user unique id

MainActivity.java

[code language=”java”]package com.developerhouse.push;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.widget.Toast;

public class MainActivity extends Activity {
private static final String TAG = "MainActivity";

public static String newRegID="";

private BroadcastReceiver mRegistrationBroadcastReceiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mRegistrationBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {

SharedPreferences sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(context);
String sentToken = sharedPreferences
.getString(Constants.PREF_GCM_REG_ID, "");
System.out.println("SERVER_SUCCESS")
; if (sentToken.equals("")) {
Toast.makeText(MainActivity.this, "Failed to save on server", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Succesfully saved on server", Toast.LENGTH_SHORT).show();
}
}
};
if (checkPlayServices()) {

Intent intent = new Intent(this, RegistrationIntentService.class);
startService(intent);
}

}
@Override
protected void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
new IntentFilter(Constants.SERVER_SUCCESS));
}

@Override
protected void onPause() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver);
super.onPause();
}
private boolean checkPlayServices() {
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (resultCode != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
GooglePlayServicesUtil.getErrorDialog(resultCode, this,
9000).show();
} else {
Log.i(TAG, "This device is not supported.");
finish();
}
return false;
}
return true;
}
}</pre>
<pre>[/code]

Server side code (PHP,MySQL)
In Server side we have to create a 4 PHP scripts for inserting the registration id & sending push notification.I am not going to explain basic steps to create PHP Script and MySQL DB for that you can check this link

http://developerhouse.com/blog/2015/08/tutorial-1-connect-android-with-php-mysql/
I am not focusing on design of the admin panel or anything related to it because our main focus will be on coding and getting your work done by adding Login Page(some of readers will be using it in their apps so it can be misuse)
Also i am not saving the username and pass in MySQL DB there is no need for it at this moment it’s just for learning purpose so that android developer can easily get push notification implemented without needing any PHP developer for php code.

Create Database named push
Table named registration

PHPMyAdmin Registration Table
PHPMyAdmin Registration Table

These are the structure of these tables

Name Type Length/Values A I (Auto Increment)
id INT Yes
gcm_regid VARCHAR 300 No (By Default)
created_at timestamp No (By Default)

 

PHP Files
connect.php // Change this for settings of MySQL DB
login.php // To login into admin panel
reg.php // For saving registration ID’s
send.php // For sending the push notification

connect.php
It’s used for changing login page username,password & also for changing mysql details.

[code language=”php”]
<?php
define("apiKey","AIzaXXXXXXXXXXXXXXXXXXXXXXXX");//Fill the key which we got from Developer Console
$user="admin";
$pass="password";
$host="localhost";
$db_user_name="root";
$db_password="";
$db_name="push";
$con = new PDO(‘mysql:host=’.$host.’;dbname=’.$db_name.’;charset=utf8′, $db_user_name, $db_password);
$con->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
?>
[/code]

login.php

It’s used for login into Android Push Notification panel so that no one can access it without username and password .You can change the username & password in connect.php file

[code language=”php”]

<?php
session_start();
if (isset($_POST[‘submit’])) {
if (empty($_POST[‘username’]) || empty($_POST[‘password’])) {
echo "Login is invalid";
}
else
{
$username=$_POST[‘username’];
$password=$_POST[‘password’];
include_once "connect.php";
if ($username==$user & $password==$pass) {
$_SESSION[‘login_user’]=$user;
header("location: send.php");
exit();
} else {
echo "Login is invalid";
}

}
}
if(isset($_SESSION[‘login_user’])){
header("location: send.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Admin Panel Login: Developer House</title>
</head>
<body>
<div id="main">
<h1>Admin Panel Login Push Notification</h1>
<div id="login">
<h2>Login Form</h2>
<form action="" method="post">
<label>Username :</label>
<input id="name" name="username" placeholder="username" type="text">
<label>Password :</label>
<input id="password" name="password" placeholder="**********" type="password">
<input name="submit" type="submit" value=" Login ">
</form>
</div>
</div>
</body>
</html>
[/code]

reg.php
This is the file which will be requested through Android platform via HTTP Request.

[code language=”php”]<?php

include_once "connect.php";
function storeUser($con,$gcm_regid) {

$stmt = $con->prepare("INSERT INTO registration(gcm_regid, created_at) VALUES(:field, NOW())");
$stmt->execute(array(‘:field’ => $gcm_regid));

$id = $con->lastInsertId();

$result = $con->query("SELECT * FROM registration WHERE id = $id");
if ($result->rowCount() > 0) {
return true;
} else {
return false;
}
}

function getSame($con,$gcm_regid) {

$result =$con->query("SELECT * FROM registration WHERE gcm_regid=\"".$gcm_regid."\"");

if ($result->rowCount()>0) {
return true;
} else {
return false;
}
}
if (isset($_POST["regID"])) {

$gcm_regid = $_POST["regID"];

if(!getSame($con,$gcm_regid)){
storeUser($con,$gcm_regid);
echo "1";

}else{echo "0";}
} else {
echo "0";
}?>
[/code]

send.php
This file is used for sending the push notification of our choice to the all users in group.

[code language=”php”]
<?php
session_start();
include_once ‘connect.php’;

function send_notification($con,$registatoin_ids, $message) {
$url = ‘https://android.googleapis.com/gcm/send’;

$fields = array(
‘data’ =>array("message" => $message),
‘registration_ids’ => $registatoin_ids
);
$headers = array(
‘Authorization: key=’ . apiKey,
‘Content-Type: application/json’
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die(‘Curl failed: ‘ . curl_error($ch));
}
curl_close($ch);
echo $result;

}

if(isset($_SESSION[‘login_user’])){
if(isset($_POST["submit"]) && isset($_POST["message"])) {
$num=$con->query("SELECT gcm_regid from registration")->rowCount();
$current_num=0;
$message=$_POST["message"];
for($i=0;$i<$num/1000;$i++){

$query=$con->query("SELECT gcm_regid from registration LIMIT $current_num,1000");

foreach($query as $data) {
$row[]=$data["gcm_regid"];
}

$pushStatus = send_notification($con,$row, $message);
$current_num+=1000;
}
}else if(isset($_POST["logout"])){

if(session_destroy()) // Destroying All Sessions
{
header("Location: login.php"); // Redirecting To Home Page
}
}

?>

<?php
?><!DOCTYPE html>
<html>
<head>
<title>Send Push Notification : Developer House</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<?php

$result = $con->query("SELECT * FROM registration");

if ($result != false)
$no_of_users =$result->rowCount();
else
$no_of_users = 0;
?>
<div >
<h1>No of Devices Registered: <?php echo $no_of_users; ?></h1>
<hr/>
<?php
if ($no_of_users > 0) {
?>

<form method="POST" action="">
<textarea rows="3" name="message" cols="25" placeholder="Type message here"></textarea>
<button type="submit" name="submit" value="Submit">Submit</button>
<button type="submit" name="logout" value="Submit">Logout</button>
</div>
</form>
<?php
} else { ?>
<li>
No Users Registered!
</li>
<?php }
}?>
</div>
</body>
</html>
[/code]

 

Sending push notifications

Now if you have followed the tutorial this step will work for you.

So steps are

  1. Open Android Costants.java file & replace GCM_SENDER_ID variable value to the project number from project console ,WEB_SERVER_URL value to the one with your server url and file path for example if you have uploaded in wamp www then check IP of your computer it should be like this http://192.168.1.103/reg.php or if you have used a Linux Hosting and uploaded in public_html your main domain is developerhouse.com then it will be http://developerhouse.com/reg.php
  2. Open connect.php change apiKey value to the one we got from project console Api & auth -> Credentials
  3. Compile & Run the android application . Don’t worry if it does not show anything because we have not made it for this purpose .
  4. It will show Toast Registered with web server if everything works well
  5. Now check PHPMyAdmin it will have a new value in registration table
  6. Now to send the Push Notification go to this url http://192.168.1.103/login.php .Fill username & password.You will be redirected to the http://192.168.1.103/send.php file it will show no. of registered user & a textarea with two buttons
  7. Press submit & check notification.
  8. Finish

Code Download Click here (Dropbox)

Few Notes

  • In send.php i have used 1000 limit code because i had read this on few sites that in 1 request you cannot send push notification to more then 1000 device id so i have created a loop for this (Of-course you can try other approach)
  • I have not gave my focus to the design of Login page or Send page because you are admin you don’t have to show this page to the world But a nice design is always best so feel free to change it .
  • I have edited the code from first version because it was not using MyInstanceIDListenerService class it’s useful if the token got expired or compromised it will refresh the token in onTokenRefresh which will call RegistrationIntentService .Another reason to edit the post was using PDO in PHP Files which can helps us to prevent SQL Injection.
Posted on

List of Open Source Game Engine for Android

Open Source Game Engine for Android

List of Open Source Game Engine for Android Platform

Open Source Game Engine for Android
Open Source Game Engine for Android

Summary:

In this article i am going to tell you about 3 open source game engine which i have used to make Games / Live Wallpaper for android platform.

 

Game Engine Language Platforms 2D/3D Live Wallpaper
Cocos2d-x C++,LUA,JS-Binding Android,IOS,Mac,Linux,Win32 others 2D + 3D (50% Features as Compared to LibGDX)

No

LibGDX Java Windows, Mac, Linux, Android, IOS, BlackBerry and HTML5 2D + 3D Yes
AndEngine Java Android 2D Yes

History Cocos2d-x  (Updated)

Language: C++

Details
Cocos2d-x is a very powerfull engine to create Games & Apps for cross platforms.It comes with editor like CocoStudio which contains Animation Editor,GUI Editor and other tools
Some games which are created with Cocos2d-x are BadLand,Hill Climb Racing.The 3D support is not completed there are some features for 3D like loading 3D FBX models,Texturing but advance features has not yet implemented.
It also comes with win32 version so if you are working on desktop you can use it in Visual studio and test.

Support: Coocs2d-x community is consist of active members which can help if you come with any error just post on their forum.

History LibGDX (Updated)

Language: C++ is used for performance but you will write the code in JAVA which will be used with NDK (Native Development Kit)

Details
LibGDX is a good engine which can be used for creating games ,apps & live wallpaper.It has a full 3D Support from loaidng model to textures and 3D physics engine.You can use it to make baics to advance 3D Games.,
It also comes with a desktop version so if you are creating it on desktop ,you can run & test it for fast developing.Games created using LibGDX are Five Nights at Freddy’s

Support:LibGDX also has a forum for members but active members are less then Cocos2d-x

History AndEngine (Outdated)

Language: It uses same technology as LibGDX C++ for performance in NDK and JAVA Wrapper for you to use the JAVA code

Details
AndEngine is used for 2D Games you will not be able to create 3D games in AndEngine.You can use it for Games,Apps,Wallpapers.

Support: Same as LibGDX active members are less so the chances of getting help is less then the Cocos2d-x Forum.

How to chose best open source game engine ?

It depends upon you what language,what style you like to work with.

My personal choice is Cocos2d-x as i have used it to create Rickshaw Racer and i found it interesting to work. I have also used LibGDX but for Live Wallpaper Wizard Times

Learn one game engine and you can follow the logic with other because the Logic for creating sprites is same. Physics engine is same if you are going to use Box2D ,just Programming language is different if you know JAVA then i would suggest go for LibGDX or AndEngine else if you know C++ then Cocos2d-x is the best choice.

Sample link for the features ,Use it ,Test it & Choose It

Cocos2d-x: https://github.com/cocos2d/cocos2d-x/tree/v3/tests/cpp-tests (It’s a GitHub Directory so you have to download it and install it with Cocos2d-x Code) (Updated)

LibGDX: https://github.com/libgdx/libgdx/tree/master/tests (Updated)

AndEngine: https://github.com/nicolasgramlich/AndEngine (Outdated)

 

Official Website Link:

Cocos2d-x –http://cocos2d-x.org/

LibGDX- https://libgdx.badlogicgames.com

AndEngine- http://www.andengine.org/

 

(Updated) = Updated Regulary and supported with developers

(Outdated)= Outdated not updated for more then 1 year.

 

Note: If you would like to list your Open Source game engine then please let us know in comments

 

Posted on

Tutorial 2 : Connect Android with PHP,MySQL (JAVA Code)

Fetched Data by Tutorial Connect

Welcome to Connect Android with PHP & MySQL Series Part 2

Summary

In this tutorial i will show you how to access the fetched data from PHP Script using Volley Library,Parse the fetched JSON and show it in ListView

0.) Requirement

To test the communication between WAMP and your android app you must be on same network.For eg. you are connected to same Wireless router on your device and with your computer on which WAMP is running.

1.) Setting up Required Libraries (Volley Library)

Download volley library from here

Add the downloaded jar file to project
Create a new Project in Eclipse File -> New -> Android Application Project -> Fill the required fields.
once the project is succesfully created paste the volley.jar in libs folder (if there is no libs folder then you can make one)

2.) Starting Project

Creating Volley Applicatio class this class will maintain volley objects & request queue.
In your project create a new class named VolleyController.java & extend the class from Application & add following code
VolleyController.java

[code language=”java”]

package com.developerhouse.android.connect;

import android.app.Application;
import android.text.TextUtils;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;

public class VolleyController extends Application {

public static final String TAG_APP = VolleyController.class
.getSimpleName();

private RequestQueue requestQueue;
private ImageLoader imageLoader;

private static VolleyController instance;

@Override
public void onCreate() {
super.onCreate();
instance = this;
}

public static synchronized VolleyController getInstance() {
return instance;
}

public RequestQueue getRequestQueue() {
if (requestQueue == null) {
requestQueue = Volley.newRequestQueue(getApplicationContext());
}

return requestQueue;
}

public ImageLoader getImageLoader() {
getRequestQueue();
if (imageLoader == null) {
imageLoader = new ImageLoader(this.requestQueue,
new LruBitmapCache());
}
return this.imageLoader;
}

public <T> void addToRequestQueue(Request<T> req, String tag) {
req.setTag(TextUtils.isEmpty(tag) ? TAG_APP : tag);
getRequestQueue().add(req);
}

public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG_APP);
getRequestQueue().add(req);
}

public void cancelPendingRequests(Object tag) {
if (requestQueue != null) {
requestQueue.cancelAll(tag);
}
}
}
[/code]

LruBitmapCache.java

[code language=”java”]
package com.developerhouse.android.connect;
import com.android.volley.toolbox.ImageLoader.ImageCache;

import android.graphics.Bitmap;
import android.support.v4.util.LruCache;

public class LruBitmapCache extends LruCache<String, Bitmap> implements
ImageCache {
public static int getDefaultLruCacheSize() {
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
final int cacheSize = maxMemory / 8;

return cacheSize;
}

public LruBitmapCache() {
this(getDefaultLruCacheSize());
}

public LruBitmapCache(int sizeInKiloBytes) {
super(sizeInKiloBytes);
}

@Override
protected int sizeOf(String key, Bitmap value) {
return value.getRowBytes() * value.getHeight() / 1024;
}

@Override
public Bitmap getBitmap(String url) {
return get(url);
}

@Override
public void putBitmap(String url, Bitmap bitmap) {
put(url, bitmap);
}
}

&nbsp;

[/code]

Edit AndroidManifest.xml file and add the VolleyController class in <application> tag using android:name
this will execute the class automatically when the app launch & add INTERNET permission because we need to call web services.
AndroidManifest.xml

[code language=”java”]

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.developerhouse.android.connect"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="22" />
<uses-permission android:name="android.permission.INTERNET"/>

<application
android:name="com.developerhouse.android.connect.VolleyController"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
[/code]

We are outputting JSON array from php Script in JSON if the starting tag is [ then it’s a Array else if it is { then it’s a Object

Json Array (Current output)

[code language=”java”]
[{"id":"1","title":"Rickshaw Racer","description":"Get Rickshaw Racer on Google Play","img_url":"http:\/\/s22.postimg.org\/kg4dj2usx\/Icon_Rickshaw_512.png"}]
[/code]

Json Object

[code language=”java”]
{"id":"1","title":"Rickshaw Racer","description":"Get Rickshaw Racer on Google Play","img_url":"http:\/\/s22.postimg.org\/kg4dj2usx\/Icon_Rickshaw_512.png"}
[/code]

MainActivity.java

[code language=”java”]package com.developerhouse.android.connect;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

public class MainActivity extends Activity {

private ProgressDialog dialog=null ;
private String TAG="Tutorial Connect";
private String tag_json_arry = "json_array_req";
private String url = "http://192.168.1.5";
private String url_file="/fetch.php";
private CustomAdapter adapter;
private ListView list;ArrayList<RowData> rowdata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

list=(ListView) findViewById(R.id.listView1);
rowdata=new ArrayList<RowData>();

dialog= new ProgressDialog(this);

dialog.setMessage("Loading…");
dialog.show();

JsonArrayRequest request = new JsonArrayRequest(url+url_file,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString()); try {
for(int i=0;i<response.length();i++){
String title=response.getJSONObject(i).getString("title");
String description=response.getJSONObject(i).getString("description");
String img_url;

img_url = response.getJSONObject(i).getString("img_url");

rowdata.add(new RowData(title, description, img_url));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
adapter=new CustomAdapter(MainActivity.this, rowdata);
list.setAdapter(adapter);
dialog.dismiss();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "Error: " + error.getMessage());
dialog.dismiss();
}
});

// Adding request to request queue
VolleyController.getInstance().addToRequestQueue(request, tag_json_arry);

}

}

[/code]

Line 22

[code language=”java”]
private String url = "http://192.168.1.5";
[/code]

Change this to the IP of your computer

How to get IP of the computer ?

Open CMD type ipconfig you will see something like this

How to use IPConfig
How to use IPConfig

check your default Network Adapter IP

the above code will Log the response & show fetched data in ListView so fire up WAMP ,check PHP Script it’s fetching result or not & run your android app if it output something in LogCat then you are good to continue else post the Error in Comment section or search it on Google

 

CustomAdapter.java

this class is Custom Adapter which will fill be filled by listview

[code language=”java”]
package com.developerhouse.android.connect;

import java.util.ArrayList;

import com.android.volley.toolbox.NetworkImageView;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class CustomAdapter extends BaseAdapter{
private ArrayList<RowData> result;
private Context context;
private static LayoutInflater inflater=null;
public CustomAdapter(MainActivity mainActivity, ArrayList<RowData> data) {
// TODO Auto-generated constructor stub
result=data;
context=mainActivity;

inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return result.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

public class Holder
{
TextView title;
TextView description;
NetworkImageView img;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.list_item, null);
holder.title=(TextView) rowView.findViewById(R.id.textView1);
holder.description=(TextView) rowView.findViewById(R.id.textView2);
holder.img=(NetworkImageView) rowView.findViewById(R.id.networkImageView);
holder.title.setText(result.get(position).getTitle());
holder.description.setText(result.get(position).getDescription());

// If you are using NetworkImageView
holder.img.setImageUrl(result.get(position).getImageURL(), VolleyController.getInstance().getImageLoader());

return rowView;
}
}
[/code]

RowData.java

this class contains variable to hold list item data which is Title,Description,Image Url

[code language=”java”]
package com.developerhouse.android.connect;

public class RowData {
private String title;
private String description;
private String img_url;
public RowData(String title,String description,String img_url){
this.title=title;
this.description=description;
this.img_url=img_url;

}

public String getTitle(){
return title;

}
public String getDescription(){
return description;

}
public String getImageURL(){
return img_url;

}
}
[/code]

 

XML Layout

activity_main.xml

[code language=”java”]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.developerhouse.android.connect.MainActivity"
android:orientation="vertical">

<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

</ListView>

</LinearLayout>
[/code]

list_item.xml

[code language=”java”]
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<com.android.volley.toolbox.NetworkImageView
android:id="@+id/networkImageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerHorizontal="false" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/networkImageView"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/networkImageView"
android:layout_below="@+id/textView1"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

[/code]

Fetched data will show like this

Fetched Data by Tutorial Connect
Fetched Data by Tutorial Connect
Posted on

Tutorial 1 : Connect Android with PHP,MySQL (PHP Code)

Welcome to Connect Android with PHP & Mysql Series Part 1

Summary

In this tutorial i will show you how to create database, get data from PHP scripts & show it in json form which you access by a URL.

1.) Installing Wamp Server on Windows.

Go to http://www.wampserver.com/en/ download the Wamp as per your software architecture 64 bit or 32 bit

Wamp Folder
Wamp Folder

Then go to install location mine is C:\wamp (by default this is the install location for are users) open wampmanager.exe

Now you can test your wamp server by going to this address in browser http://localhost/ or http://127.0.0.1. If it’s opening default page of wamp then you’re good to proceed further.

Check the phpmyadmin also with the url : http://localhost/phpmyadmin (PHPMyAdmin helps us to create,manage,insert into the database,tables with Graphical User interface instead of MySQL Console window)

2.) Creating our first PHP Script to test

Open notepad or notepad++ (i am using Notepad++)

write this code

[code language=”php”]
<?php

echo "Hello,World";

?>
[/code]

<?php means opening tag for any PHP Script it’s like public class name { in java

and ?> means closing tag like closing bracket in JAVA

what ever you code write in these tag will be handle as PHP code

echo means System.out.println

Save it as test.php in c:\wamp\www (all files in this folder will be accessible from localhost)

now when you access it with this URL http://localhost/test.php it will print this Hello,World

Hello-World
Hello-World

3. )Let’s move to create database through PHPMYADMIN

open this http://localhost/phpmyadmin

Click on Database button on top header now it will take you to create database page

Localhost PHPMyAdmin
Localhost PHPMyAdmin

Enter DB (Database) name in first text box and create on create.

Create Database in PHPMyAdmin
Create Database

Now on left sidebar you will see list of database.Click on your newly created database then click on Create Table.

Create Table in PHPMyAdmin
Create Table

Fill fields

  1. Table Name : products
  2. No. of columns : 4

Columns

Products Table
Products Table
Name Type Length/Values A I (Auto Increment)
id INT Empty (By Default) Yes
title VARCHAR 40 No (By Default)
description VARCHAR 250 No (By Default)
img_url VARCHAR 100 No (By Default)

Now we will add a entry in MySQL Table named products

Select products from side panel.Click on Insert on Header

Now fill it like this

Column Type Function Value
id INT(11)
title VARCHAR(40) Rickshaw Racer
description VARCHAR(250) Get Rickshaw Racer on Google Play
img_url VARCHAR(100) http://s22.postimg.org/kg4dj2usx/Icon_Rickshaw_512.png

Now you have filled it with details click on Go Button.

Insert 1 or  more rows with the details you want to be shown in Android App.

4.) Move to PHP again to show our data by echo command

Let’s make a config.php file in the www folder in wamp directory.

Detail about config.php : It’s usefull to save all the MySQL database info in one file so you don’t have to fill it again & again.

  • Host = localhost (by default if you’re using it on wamp and MySQL Server is also installed on same machine)
  • Username = root (by default)
  • Password =  (by default Empty 0 length)
  • Database name = android_db (same)
  • Table name = products (same)

[code language=”php”]
<?php
$host="localhost";
$user_name="root";
$password="";
$db_name="android_db";
$table_name="products";

//Let’s make a mysql connection to connect the server.
$con=mysqli_connect($host,$user_name,$password);
mysqli_select_db($con,$db_name);

?>
[/code]

Explanation by lines

1.) Opening Tag.

2.) Host name or Ip Address where our MySQL Server is installed.

3.) Username to access the database.

4.) Password for the username to get pass authentication.

5.) Database name in our case android_db which we made in earlier steps.

6.) Table name in our case products

8.) Comment just like we do in JAVA

9.) It’s connecting to MySQL Database to make communication

10.) Select database to communicate

5.) Create fetch.php to get results from table

Code

[code language=”php”]
<?php
include_once("config.php");

$query=mysqli_query($con,"SELECT * FROM ".$table_name);
$array;
while($result=mysqli_fetch_assoc($query)){

$array[]=$result;
}

echo json_encode($array);
?>
[/code]

Explanation by lines

2.) Include config.php just like Import in Java.

4.) Here we query the MySQL Database to give us all rows from table name

5.) Declaring array as a variable

6.) While Loop to fetch all results

8.)The $result fill all results in $array

11.) Print encoded JSON

try to access the file via Browser from this url http://localhost/fetch.php it will print something like this

[code language=”java”]
[{"id":"1","title":"Rickshaw Racer","description":"Get Rickshaw Racer on Google Play","img_url":"http:\/\/s22.postimg.org\/kg4dj2usx\/Icon_Rickshaw_512.png"}]
[/code]

So the PHP Part is completed in next Tutorial we will work on Android Code

Posted on

Android App Tutorial Series Connect with PHP,MySQL

Web Services Diagram

Android App Tutorial Series

In this Series of android app tutorial we will focus on how to connect with PHP Scripts with MySQL as database and communicating with android.

Basically it is used as Web Services for fetching,inserting,managing data through android app with the help of PHP Scripts.

We will call the PHP Script with Volley Library (Saves time,Easy to use)

So the operation will be simple as defined in below image

Web Services Diagram
Web Services Diagram

your code calls PHP script

 

What is PHP ?

PHP is a server-side scripting language designed for web development but also used as a general-purpose programming language

What is MySQL ?

With PHP, you can connect to and manipulate databases.

MySQL is the most popular database system used with PHP

How to host files of PHP & MySQL (It’s not same as putting *.java files in your Project) ?

You need a hosting if  you want to access it over Internet

OR

You can use WAMP,XAMPP or any other server on your computer install one of them by this you can host your PHP files locally and access it via 127.0.0.1 or localhost (these are the address to access your files just like domain name developerhouse.com)

PHP script will work on Server Side for inserting, Editing, Deleting ,Showing Records with MySQL Database.

I’ll be using Hostgator shared hosting to host PHP Scripts.if you want to use any other hosting the process will be same if the hosting provides CPanel.

OR

I will also post Screenshot to get you started with WAMP so that you ca use localhost instead of Linux Hosting (WAMP,XAMPP)

Tutorial List:

Part 1- Tutorial 1 : Connect Android with PHP,MySQL (PHP Code)

Part 2- Tutorial 2 : Connect Android with PHP,MySQL (JAVA Code)

Posted on

Get our new game Rickshaw Racer Extreme on Google Play

Rickshaw Racer Android


The new game has better graphics then the first game new Levels,new Obstsacles,new Rickshaws

Rickshaw Racer Extreme is second version of the game Rickshaw Racer.It’s a fun based Rickshaw Racing game
you can choose you own rickshaw ,Unlock them and use it in the game by completing levels Rickshaw will be unlock and the same happens to Levels
New level will be added soon till then enjoy the game & unlock all Rickshaw

There are many obstacles in the game like breakable meteor Platform ,Pressing machines , Roll Over platform and many more

NOTE 1: Game is totally free but some items can be unlocked by purchasing if you don’t want it please turn off the IAP in Options
Music: www.bensound.com

 

Main Menu
Menu

 

Download it from Google Play https://play.google.com/store/apps/details?id=com.developerhouse.rickshaw

Posted on

The Wizard Times Live Wallpaper

Wizard Times Live Wallpaper

Our recent launch The Wizard Times Live Wallpaper is  based on Harry Potter News Paper (The Daily Prophet) . You have seen the animation in that news paper and same thing you can do in this Live Wallpaper .Capture your animation using the In App Camera Save it & set it as Live Wallpaper by Going into Live Wallpaper Settings of your Android Smartphone.

Here is small description copied from Google Play

Now you can have magical News paper as Live Wallpaper on your home Screen. Make & choose your own image or animation & make it as a Live Wallpaper in The Wizard Times ,make headlines & much more

You have seen these type of news paper in some movies & now it has came to your android mobile as a Live Wallpaper

In Free version you can choose your own image as Wallpaper.

Features:
Default set of animation (Free/Paid Version)
Custom Title,Heading,Content (Paid Version)
Custom Animation (make set of animation using your own images captured by the app) (Free/Paid Version)
Custom Image (choose your image as News image from Gallery or file Manager) (Free/Paid Version)
Grey Image will be display for all your Animation & Wallpaper so that it will look like real Wizard news paper (Free/Paid Version)
Lens Effect (Paid Version)
Animated headings (Free/Paid Version)

Buy Paid Ad Free version to unlock customization Features

To set the Wallpaper

Home Screen (Home > Menu > Wallpapers > Live Wallpaper > The Wizard Times Free)

 

You can get it for free on Google Play

https://play.google.com/store/apps/details?id=com.developerhouse.wizardtimesfree

 

 

Posted on

HostGator Hosting Review

What is web hosting ?
Web Hosting is type of Internet Hosting service that allows users to make their website & make it accessible from WWW
World Wide Web.
In Short you host your files on it / save your files on it and people can access it over WWW
it’s like a Computer over Internet (move your localhost to Internet)

HostGator

HostGator Hosting
HostGator a popular web hosting service provider since 2002.
It provides Shared , Reseller, VPS, Dedicated types of hosting to their users.
In this review i will share my personal experince about HostGator .I will be talking about their Shared
hosting solution.

Plans of HostGator
Plans of HostGator

I have used the HostGator hosting for my HTML5 Games,API of my Government Job Application & for diff. websites.

Server Uptime Guarantee
I am using it since 2010 i have not noticed any downtime on my sites & API.

Customer Service
Good Customer service is the key to any successful business & HostGator is doing the same .
They have Live Chat feature they are available 24/7/365 but you can be in queue of 1-2 customers.
You can also open the Support ticket for any issue related to the hosting,sales,billing or make a phone call to solve your issue.

Hostgator Addition Features
1. Toll Free Number
2. QuickInstall to install a varity of PHP Scripts from WordPress to Prestashop
here is the list of Programs which can be installed via QuickInstall CLICK ME
3. You will also get the Free Advertising bonus of 100$ AdWords Credit.You can use it in billing.
4. Free Weebly SiteBuilder & WebSite building tools.
5. 4,500 Free Templates for your website http://templates.hostgator.com/
6. Hostgator Free Website Transfer

HostGator Website Transfer
HostGator Website Transfer

7. 45 Day money back guaranteee is provided by HostGator

You can try HostGator hosting for 1 Cent ( $0.01 ) It’s true
go to http://hostgator.com use this a Coupon

[code language=”php”]COSTONECENT[/code]

OR try 25% Coupon

[code language=”php”]25OFFATHOSTING[/code]

New Coupons

To use these coupons you have to visit it via this URL

Click Me

Spring Sale: Tuesday, April 14th at 12am – Wednesday, April 15th 11:59pm CST,we will be running a Spring Sale, featuring 55% off all new hosting packages!

 

[code language=”php”]
FLOWERS
[/code]

FLASH Sale: Thursday April 16th at 8:00am – 12:00pm CST, we will be running a FLASH sale, featuring 60% off all new hosting packages!

Coupon Code:

[code language=”php”]APRILFLASH

[/code]

For all the night owls, FLASH SALE Pt. 2: Wednesday, April 22nd at 11:00pmThursday, April 23rd 2:00am CST, we will be running another FLASH Sale, featuring 60% off all new hosting packages!

Coupon Code:

[code language=”php”]APRILSAVINGS
[/code]

Posted on

2. Hello World!

Hello World

Summary: In this tutorial i am going to show you how to create cross platform Project by just 1 line of Text in CygWin & work on it !
Requirement: Python for running the Script 
Detail: Let’s start by creating Cross platform Project
Let’s open CYGWIN
Enter :
1. cd C:/Downloads/cocos2d-x-2.2.2/cocos2d-x-2.2/tools/project-creator

2. ./create_project.py -project Hello -package com.developerhouse.hello -language cpp
Let me explain the Parameters
project Project Name
package Package Name
language  Language Name (cpp | lua | javascript)
Press Enter it will create the Project in Cocos2dx Root Directory /Projects/ Project_Name (Hello in our Case)
if you have followed our last tutorial then you know how to Import the project & Run it
if not then Please read it
Open Hello Project folder
now you know proj.[android|blackberry|tizen|win32] Folder
let’s talk about Classes & Resources Folder
Classes folder includes your C++ Classes & Headers file (Codes)
Resources folder includes your assets (Graphics,Sound,Shaders,JSON Files,XML Files or any other type)
Open project in Visual Studio or in any other IDE
Open AppDelegate.cpp
#include "AppDelegate.h"
#include "HelloWorldScene.h"

USING_NS_CC;

AppDelegate::AppDelegate() {

}

AppDelegate::~AppDelegate()
{
}

bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
CCDirector* pDirector = CCDirector::sharedDirector();
CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();

pDirector->setOpenGLView(pEGLView);

// turn on display FPS
pDirector->setDisplayStats(true);

// set FPS. the default value is 1.0/60 if you don't call this
pDirector->setAnimationInterval(1.0 / 60);

// create a scene. it's an autorelease object
CCScene *pScene = HelloWorld::scene();

// run
pDirector->runWithScene(pScene);

return true;
}

// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground() {
CCDirector::sharedDirector()->stopAnimation();

// if you use SimpleAudioEngine, it must be pause
// SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
CCDirector::sharedDirector()->startAnimation();

// if you use SimpleAudioEngine, it must resume here
// SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}

AppDelegate.cpp Class
This is the  main class of cocos2d-x it’s is used for 
Setting OpenGL View ,frame rate ,Scene,
applicationDidEnterBackground() just like onPause() in android called when Game minimize,when call comes etc..
applicationDidEnterBackground() just like onResume() in android called when resume from onPause() State when player open game again or call ends.
CCDirector* pDirector = CCDirector::sharedDirector(); //Get shared CCDirector pointer 
Q: What is CCDirector ?
A: It’s used to set Framerate,OpenGL View,setStats (Showing FPS etc..),Changing Scene (like Current Screen)
CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();//Get Shared OpenGL View
pDirector->setOpenGLView(pEGLView); //Set Current OpenGL View may be some of you guys are beginner with C++ i am also so instead of period . we are using -> because it’s a Pointer
pDirector->setDisplayStats(true); // Set Display Stats True FPS on bottom left corner of the Screen
pDirector->setAnimationInterval(1.0 / 60); //Set FPS the default value is 1.0/60
About 28 to 31 i will show you later let’s continue to applicationDidEnterBackground & applicationWillEnterForeground
applicationDidEnterBackground is used when game minimize or call comes in this we have to Pause our game animation or Sound or any other custom Game loop
applicationWillEnterForeground is used when game maximize to the user device resume animation or Sound or your custom Game loop
i will come later to Line 28 to 31
First we have to understand how Scene ,Layers & Sprite works
CCScene (Scene) can hold many CCLayer(Layer)
Layers can Hold CCSprite (Sprites) & other visual elements
think it like those 3 Classes are in Photoshop
CCScene  as Canvas
CCLayer  as Layers 
CCSprite as Image,your art work,Rectangles or other things which you draw or Place on Layer
here is a diagram to explain

Red Color Stroke -> CCScene

Green Color Stroke -> CCLayer

Blue Color Stroke -> CCSprite

you can add many CCLayer in CCScene 1 for background ,1 for HUD etc… etc..

Line 28 get Pointer of HelloWorld scene function which is returning a CCScene

Line 31 using our SharedDirector run the HelloWorld::scene()

Line 38 CCDirector::sharedDirector()->stopAnimation(); //Stop the animation when game pause

Line 46 CCDirector::sharedDirector()->startAnimation(); // Run it again when game resumes

_________________________________________________________________

The HelloWorldScene class is sub class of CCLayer

Before we move to code let me explain some Data types

CCSize -> holds two floating point Numbers think it as width and height

CCPoint -> holds two floating point Numbers think it as x and y

ccp -> use it to set CCPoint values // ccp(0,0);

Delete the content of HelloWorldScene to this code according to me by default the code is not easy to understand as a complete beginner

#include "HelloWorldScene.h"

USING_NS_CC;

CCScene* HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create();

// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create();

// add layer as a child to scene
scene->addChild(layer);

// return the scene
return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !CCLayer::init() )
{
return false;
}

CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();

// add "HelloWorld" splash screen"
CCSprite* pSprite = CCSprite::create("HelloWorld.png");

// position the sprite on the center of the screen
pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

// add the sprite as a child to this layer
this->addChild(pSprite, 0);

return true;
}

Line 3 defined as using namespace cocos2d (Short keyword)

Line 5-18 static scene function which is returning pointer of cocos2d::CCScene*

Line 8 create new CCScene by CCScene::create();

Line 11 create new HelloWorld Layer by HelloWorld::create();

Line 14 add the HelloWorld Layer as child to CCScene by scene->addChild(layer); //the add child method is gonna used many times when you add sprite ,CCrenderTexture etc. etc.

Line 17 reutnr that CCScene which added HelloWorld layer to the scene

Line 21-77 initialize function named init()  return boolean value it’s a Virtual function

Line 25-28 CCLayer::init() call init method of CCLayer if it’s false then return false exit method

Line 30 get Visible size of OpenGL View in CCSize data type CCSize holds two float values like width & height

Line 31 get Visible origin point of OpenGL View in CCPoint data type CCPoint holds two float values like x & y

Line 34 create a new Sprite with the file name which is in Resources folder

Line 37 set Position of the Sprite which is made in 34 by create a new CCPoint using ccp the Position will be center

Line 40 add the sprite as a child of Layer the second parameter is z-index (Hope you knwo what it is if you don’t then no worries it’s like the layer index which will be on top of that layer
just like we do in photoshop or illustrator we drag and drop the layer to the top of it if you set z-index 0 it will top on -1 if you set it 1 then it will be top on 0 Simple)

_______________________________________

This is the end of Hello World Tutorial hope you liked it if you did not then please post in comments what you did not like and what i can improve
we will go more advance step by step

Here is the Project Hello Classes just replace them with the project you created in Classes folder (I have not uploaded the full project because it will let you do some work of  creating your own project OR saving my time for 9 MB project uplaod :D)
https://www.dropbox.com/s/gu64thsadl32wqf/Classes.rar