Sunday, February 22, 2015

Cocos2d-x: How to create a new project(Android, iOS, Windows)


cocos new -p com.honesitconsultancy.indiegame -d "D:\Cocos\Projects" -l cpp

cocos-2dx-project-structure
Fig - 1: An empty Cocos2d-x Project
Once you have all the pre-requisites needed for working with Cocos 2Dx game development installed on your Windows PC, you are ready to use one of the best cross platform indie game development engines.

A new Cocos 2DX C++ project is created through a Python programming language script, remember Python was listed in the list of software we need to install beforehand. To read about list of prerequisites needed to create cross platform games with Cocos2d-x is visit following link:


Fig - 1 shows a new empty Cocos2d-x project. You can see it already contains projects for iOS, Android, and Windows(win32).

How to create new project using Cocos2d-console

Well that's just a fancy name for a Python script. Even the file name is different, "cocos.py".
When you install Cocos framework, the path is automatically configured and you can type "cocos" anywhere on command prompt.
This script file is present in Cocos framework installation folder(FYI). In my case I installed it on path "C:\Cocos". Full path of Cocos2d-x is given below:

C:\Cocos\tools\Python27\Tools\Scripts

The how-to guide present on Cocos website tells a different path, I couldn't find it on my PC and discovered this by just searching for .py files in the installation folders.
Launch command prompt, and type the command given below








cocos new -p com.honesitconsultancy.indiegame -d "D:\Cocos\Projects" -l cpp

Important: Please create the project path folders before running the command, i.e. the path specified through -d switch.
cocos-2d-new-project-screenshot
Fig-2: cocos2d-x console screenshot

To learn more about the Cocos2d-X console, type following command to get the list of Cocos2d-x console options

cocos new --help

A screenshot of the help description is shown below
cocos2d-studio-help
Fig-3: Cocos console help 

How to write Cocos2d-x code using Visual Studio 2012

Go to the path which you specified in command line for creating a new project. Please refer back to Fig-1.
Open the folder "proj.win32", this folder will contain a solution file which will be named "indiegame.sln" or "MyCppGame.sln" in my case.
cocos-2dx-win32-project-contents
Fig-4: Cocos2d-x new Windows(win32) Project contents
A screenshot of how the project looks inside Visual Studio is given below:
cocos2dx-visual-studio-inside
Fig-5: Inside Visual Studio 2012 Cocos2d-x C++ Project

Where will you write the code?

To start with we will put the code inside HelloWorldScene.cpp file. If you're going through the programmer guide doc, try your practice code in the C++ function mentioned below

bool HelloWorld::init()

Here's a short video demo to show off and make you jealous :D


For places with Youtube blocked, Vimeo video

Cocos2dx Visual Studio Helloworld Demo from Naeem Akram on Vimeo.

What about Android, iOS and other platforms?

First of all, refer to Fig-1 you will see there's a folder "Classes". This folder is utilized by Cocos2d-x framework to generate object files/dlls which are then used by all projects. Any change you make in Classes folder will be systematically used in Android, iOS,Windows, and Windows Phone Project(what?).

Just yesterday I figured how to build and run a Cocos2d-x game project on my Android phone. In a few days I will write another blog post about how to run your Cocos2d-x indie game project on Android using NDK(Native Development Kit) and Eclipse IDE.

Stay with me on this journey to learn game programming, and feel free to communicate/ask questions. You may post a comment or send me an e-mail through the form on right side of page.

Stay tuned for more!

Tuesday, February 17, 2015

Getting Started With Cocos 2D using C++ on Windows, What To Install?

cocos-2dx

Cocos 2d-x is a very popular cross platform game development engine. C++ is the programming language most university/college students now right out of college, that means maybe they can create a game while studying in college! Unity 3D, another game development platform is taking the market by storm but still many professional developers prefer Cocos 2D. My friend Mudaser recommended using Cocos-2D instead of Unity, he's got several years of game development experience so I followed his advice.


Getting started with Cocos was not very easy to be honest, it took me several hours to figure out which way to go. They got documentation on their website but it was not very helpful.

Tools to install for developing Cocos 2D C++ games on Windows





  1. Cocos Studio - Download
  2. Cocos Framework - Download
  3. Python - Download
  4. Visual Studio 2012 - Download
  5. Windows Phone 8 SDK - Download
  6. Java Development Kit (JDK), for Android version of your game - Download
  7. Android SDK, or Android version of your game- Download
  8. Android Native Development Kit (NDK), for Android version of your game - Download
I compiled this list after spending some time and I believe it is going to be helpful for people.

Tip

Most of the stuff can be downloaded after installing Cocos Studio on your system through the Cocos Store tab shown below. I recommend downloading Cocos Studio first.


cocos-store

Are you struggling with Cocos 2D-x game engine, let's learn together!!!

Feel free to get in touch with me through the "Contact Me" form on left side, you may comment the blog post as well.

What is Cocos Studio then?

It is not the place you will write your code in. Instead, it is a place where you create scene files which are then used by a programmer.
Your project will have various folders containing projects for Win32, Android, and Windows Phone as well. You will open appropriate project folder and modify files in there.

Why Python?

Not everybody loves PowerShell, open source community has got a much older and well tested alternative i.e. Python programming language. It is used to perform various tasks like creating projects etc. You will need to install it as well, no way around it ma techie.

Where do you write the Cocos2D-X game code?

On a Windows PC, the C++ game code is written in Visual Studio 2012. On a Machintosh, XCode is used to write and edit code. Same code can be reused as it is to generate an Android and iOS version of your game. You will need to provide graphics for major size changes, but don't worry about that for now.

Is it possible to create a Cocos2D-X game without C++?

Yes, it is possible to write HTML5/JavaScript based games, the tools are not covered in this blog post.
Lua scripting language is also supported and it can be used to create Cocos2D-X games.

android-os-logoNo direct debugging on Android

Don't just freak out, that's a matter of fact. You can't debug directly from Android devices. That's why its recommended to use Windows Phone emulator on Windows.
There is a good news for Apple iOS developers, they can directly debug their game code from their device. The reason is objective C is a child of C and it supports code written in C++.
On the contrary Android uses a virtual machine to run code, and C++ code gets converted into suitable byte code and back to assembly before hitting the processor.

Friday, February 13, 2015

SQLite Date Time Functions Examples


SELECT strftime('%Y', trxDateTime) as sYear 
FROM trx_log
This is the SQL for selecting year part of a date time column from an SQLite data table. For more explanation & examples, please keep reading below. 
Computer time

We have an SQLite database table named 'trx_log'. This table contains a string date time field trxDateTime. It also contains other columns like trxTargetNumber, trxAmount, trxStatus. A snapshot of table schema is given below, some sample data is also included.

sqlite-android-search-by-year-group-by-month
Example SQLite Table for query



All fields other than trxAmount are string, only trxAmount which represents a superficial transaction amount is a number type.

SQLite Year calculation function

Sqlite provides strftime function to extract specific part from a date time string. We will use it to find out year in SQLite. Sample SQL query code is given below:

SELECT strftime('%Y', trxDateTime) as sYear 
FROM trx_log
This query will return four rows of data (based on snapshot shown earlier) each containing 2014 and labeled sYear.
The first parameter here is a format string, in this format string %Y represents a year.
Please notice that the Y here is a capital letter.









How to make where clause based on year in SQLite SQL?

You will get the year part of a date using strftime function and then make a decision based on that. For example, if we want data from year 2014 we can use following query.

SELECT strftime('%Y', trxDateTime) as sYear 
FROM trx_log WHERE  strftime('%Y', trxDateTime)='2014'  

Same technique can be used to group data based on year as well. Use of this technique to group by data is well demonstrated in following post:
http://aprogrammersday.blogspot.com/2014/02/group-by-month-or-year-and-sum-in-sqlite.html
Database SQLite

SQLite month calculation function

The strftime function can also be used to find out the month section from a date time string column. The exact SQLite SQL query for finding the month is given below

SELECT strftime('%m', trxDateTime) as sMonth 
FROM trx_log
The query mentioned above will return four rows of data (based on snapshot shown earlier) containing 02 as month. It is worth mentioning again that the m in this case will be a small letter. Capital M will cause the SQLite engine to return minutes instead of month.

How to make where clause based on month in SQLite SQL?

It is very easy to use a month field as basis of a where query or group by operation in SQLite. We will use the same strftime function again. An example SQ< query is shown below.
SELECT strftime('%m', trxDateTime) as sYear 
FROM trx_log WHERE  strftime('%m', trxDateTime)='02'  
Please note that SQLite will return months between January to September as two digits where the leading digit will be 0 always. For months December to 

SQLite day calculation function

How to find a day from SQLite table using SQL? Use the strftime function and supply appropriate formatting string in first parameter. The SQLite SQL query for finding the month is given below:

SELECT strftime('%d', trxDateTime) as sDay 
FROM trx_log

How to make where clause based on month in SQLite SQL?

You can also use strftime SQL(Structured Query Language) function to filter data and group data it by certain values of a specific day. I showed you earlier how to find out the day from a SQLite date field. How to use it in a select statement in conjunction with a where clause is shown below:

SELECT strftime('%d', trxDateTime) as sYear 
FROM trx_log WHERE  strftime('%d, trxDateTime)='02'  

Thursday, February 12, 2015

Network Programming Interview Questions C#.Net


  1. Which namespaces are used for TCP/IP network socket programming in C#.Net? AnswerSystem.Net and System.Net.Sockets
  2. An object of System.Net.Sockets.TcpListener will be used to listen for incoming connection requests. Answer: True
  3. Once we call the method TcpListener.Start, our TcpListener object starts handling incoming connection requests. Answer: False
  4. Which class is used to represent a callback or delegate method? Answer: AsyncCallback
  5. IAsyncResult.AsyncState contains the original TCPListener object which was passed as parameter to BeginAcceptTCPClient method call. Answer: True

  6. The Windows ____ can stop your code from using network, you better disable it for a while. Answer: Firewall
  7. An object of which class is used to handle client communication? Answer: TcpClient
  8. NetworkStream.BeginRead method will return data in ____ array. Answer: byte
  9. TcpClient.Client represent an object of which type? Answer: Socket
  10. Encoding.ASCII.GetBytes is used to convert text to bytes in order to send it over network. Answer: True
  11. A TCP/IP socket server needs to perform following network operations. Answer: Listen for incoming connections, send data to clients, receive data from clients.
Learn more about Socket and network programming in C# .Net in my Udemy course:
TCP/IP Socket Programming For Coders & Students 





Demo TCP/IP Sockets Read and Write in C#, Free Udemy Course from Naeem Akram on Vimeo.

Tuesday, February 10, 2015

Fix Parse 101 Error object not found for update

parse-com-cloud-screenshot


Parse.com is one of leading cloud service provider. I implemented push notifications in an ODesk based freelance client's app last year. They returned with some more work (I love returning clients!) and a request for updating the old app to latest Parse.com APIs. Latest Parse API version number is 1.8.2 at time of this writing.

I opened Parse.com documentation and started following it for sake of update. Several old methods have been deprecated from the API. For example the method
        PushService.subscribe();

Google's Mindset about adopted children Mozilla Firefox & Eclipse IDE

I had to switch from Eclipse IDE to Android Studio which is the new Google darling for writing code. Google did the same thing with Mozilla Firefox browser a few years back, they kept feeding Firefox to fight bloody battles with Microsoft Internet Explorer while they were conceiving their own child 'Chrome'. Once their own thing was in good shape, they kicked the adopted open source child out of their home.







How to fix Error object not found for update code 101

While following the instructions in Parse.com documentation I pretty much hit the wall with error 101. I then downloaded their sample application and in there I found some lines of code which were not mentioned in the docs. Adding these lines to my app code (application class onCreate) fixed things right away. The magic lines of Java code are given below:

ParseCrashReporting.enable(this);

// Enable Local Datastore.
Parse.enableLocalDatastore(this);

// Add your initialization code here
Parse.initialize(this, "*******", "******");

ParseUser.enableAutomaticUser();
ParseACL defaultACL = new ParseACL();

ParseACL.setDefaultACL(defaultACL, true);

// save the installation
ParseInstallation.getCurrentInstallation().saveInBackground();

I believe my readers will have enough sanity to avoid using * strings as parameters to initialize call.

Side note, more than one Parse.com apps

I also noticed later that I already had a parse.com app installed on my phone. This was the older version of the app which I was trying to update. So, I removed the older app as well just to get over with any confusion.

Bonus, a cool code image

Last but not least, here's a bonus C code image for you. I got it from pixabay, its a royalty free image. 

source-code-code-programming-c