Skip to content

Game programming

Following are some useful links for game programming, specially in C / C++

Stack operation in case of calling any function

I found it very useful to understand how stack works when a call to any function in C++ is made. I found it at and feel to share it

Because parameters and local variables essentially belong to a function, we really only need to consider what happens on the stack when we call a function. Here is the sequence of steps that takes place when a function is called:

  1. The address of the instruction beyond the function call is pushed onto the stack. This is how the CPU remembers where to go after the function returns.
  2. Room is made on the stack for the function’s return type. This is just a placeholder for now.
  3. The CPU jumps to the function’s code.
  4. The current top of the stack is held in a special pointer called the stack frame. Everything added to the stack after this point is considered “local” to the function.
  5. All function arguments are placed on the stack.
  6. The instructions inside of the function begin executing.
  7. Local variables are pushed onto the stack as they are defined.

When the function terminates, the following steps happen:

  1. The function’s return value is copied into the placeholder that was put on the stack for this purpose.
  2. Everything after the stack frame pointer is popped off. This destroys all local variables and arguments.
  3. The return value is popped off the stack and is assigned as the value of the function. If the value of the function isn’t assigned to anything, no assignment takes place, and the value is lost.
  4. The address of the next instruction to execute is popped off the stack, and the CPU resumes execution at that instruction.

Binary search tree implementation in C language

#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>
#include <list>
#include <iostream>

struct node
	node* left;
	node* right;
	node* parent;
	int val;

using namespace std;

void insert_node(node** iterate, int newVal, node* newParent);
void traverse(node* iterate);
node* min(node* iterate);
node* max(node* iterate);
node* search(node* iterate, int newVal);
void del(node** iterate, int newVal, char direction);
void makefree(node** deleted);

node* min(node* iterate)
	if(iterate->left == NULL)
		return iterate;



node* max(node* iterate)
	if(iterate->right == NULL)
		return iterate;



node* search(node* iterate, int newVal)
	if(iterate == NULL)
		return NULL;

	if(iterate->val == newVal)
		return iterate;

	if(newVal < iterate->val)
		search(iterate->left , newVal);
		search(iterate->right , newVal);

void traverse(node* iterate)
	if(iterate != NULL)
		printf("%d ",iterate->val);

void makefree(node** deleted)
	(*deleted)->val = 0;
	(*deleted)->left = NULL;
	(*deleted)->right = NULL;
	(*deleted)->parent = NULL;


void del(node** iterate, int newVal, char direction)

	if((*iterate) == NULL)

	if((*iterate)->val == newVal)
		if((*iterate)->left == NULL && (*iterate)->right == NULL)
			if(direction == 't')
				node* deleted = *iterate;
				*iterate = NULL;

			if(direction == 'l')
				node* deleted = (*iterate)->parent->left;
				(*iterate)->parent->left = NULL;

			if(direction == 'r')
				node* deleted = (*iterate)->parent->right;
				(*iterate)->parent->right = NULL;

		if((*iterate)->left == NULL)
			if(direction == 't')
				node* deleted = *iterate;
				*iterate = (*iterate)->right;
				(*iterate)->parent = NULL;

			if(direction == 'l')
				node* deleted = *iterate;
				(*iterate)->parent->left = (*iterate)->right;
				deleted->right->parent = deleted->parent;

			if(direction == 'r')
				node* deleted = *iterate;
				(*iterate)->parent->right = (*iterate)->right;
				deleted->right->parent = deleted->parent;

		if((*iterate)->right == NULL)
			if(direction == 't')
				node* deleted = *iterate;
				*iterate = (*iterate)->left;
				(*iterate)->parent = NULL;

			if(direction == 'l')
				node* deleted = *iterate;
				(*iterate)->parent->left = (*iterate)->left;
				deleted->left->parent = deleted->parent;

			if(direction == 'r')
				node* deleted = *iterate;
				(*iterate)->parent->right = (*iterate)->left;
				deleted->left->parent = deleted->parent;

		node* findmin = (*iterate)->right;
		int minVal = 0;

		while(findmin != NULL)
			minVal = findmin->val;
			findmin = findmin->left;

		(*iterate)->val = minVal;
		del(&((*iterate)->right), minVal, 'r');

	if(newVal < (*iterate)->val)
		del(&((*iterate)->left) ,newVal, 'l');
		del(&((*iterate)->right) ,newVal, 'r');


void insert_node(node** iterate, int newVal, node* newParent)
	if(*iterate == NULL)
		node* newNode = (node*)malloc(sizeof(node));

		newNode->val = newVal;
		newNode->left = NULL;
		newNode->right = NULL;
		newNode->parent = newParent;
		*iterate = newNode;


	if(newVal < (*iterate)->val)
		insert_node(&((*iterate)->left) , newVal, *iterate);
		insert_node(&((*iterate)->right) , newVal, *iterate);

int main()
	node* iterate = NULL;

	insert_node(&iterate, 8, NULL);
	insert_node(&iterate, 15, NULL);
	insert_node(&iterate, 4, NULL);
	insert_node(&iterate, 2, NULL);
	insert_node(&iterate, 1, NULL);
	insert_node(&iterate, 3, NULL);
	insert_node(&iterate, 7, NULL);
	insert_node(&iterate, 6, NULL);
	insert_node(&iterate, 11, NULL);
	insert_node(&iterate, 22, NULL);
	insert_node(&iterate, 12, NULL);
	insert_node(&iterate, 13, NULL);	


	printf("\nSearch 7->%d",(search(iterate,7))->val);

	del(&iterate, 8, 't');
	del(&iterate, 22, 't');
	del(&iterate, 7, 't');
	del(&iterate, 11, 't');


	char buffer[50];


Codeigniter with Model-View-Controller (MVC) architecture

Codeigniter with the power of MVC

PHP as being open source enjoys a lot of free available frameworks. Some are very huge and some are small. People adopt according to their needs.

I am very font of Codeigniter by Ellis Lab because sometime you have very strict timelines and you need Agile or rapid development. Here you feel a need for a framework that could be configure with zero or minimal configuration, and structured and complete enough to fulfill your requirements. So, Codeigniter is there to rescue.

For every framework, it is always considerable to choose best architecture for its organization, extensibility and management. Model-View-Controller MVC is the most popular among all for PHP.

To implement MVC in PHP, you have to have Object Oriented Programming (OOP) concepts which is out of the scope for this article. I am writing another article on that and will release soon.

Something about Model – View – Controller (MVC)

I will elaborate MVC implementation in codeigniter but first something about MVC

  • Controllers received all request from users and call models and views to render the information as directed in the request.
  • Models are modulation for any database or other system. Models do not play role in every request received by controllers. Actually controllers manage model when there is any change occurred in model. Views are not aware of any change in models, its controller responsibility to notify related views about the change.
  • Views are customized output representations, generated as per current models state and request sent to controller.

MVC is controlled and desired separation or abstraction for result outputs. User never has access to models or views, he/she only interact with controllers. Controllers are the component, which received calls, sends commands to models to know about its state, do any mention change in models, call views to tell about changes in models and render customized output for views according to direction provide in the call and current state of models. One controller can call many models and views.

There are many variations in MVC architecture. Google it to get it :). Now back to Codeigniter.

MVC in Codeigniter

This article is about codeigniter2.1.3 which is current stable version at this time. Codeigniter has very simple directory structure. In application folder, you create your web application. Inside application, controller will go to controllers folder, model to models folder and views to views folder. There are many other folders in application folder for many other purposes.

A big attraction for MVC in PHP is to handle web request smartly with SEO-friendly, User-friendly or clean URL.

Consider following request to Codeigniter web application

After index.php, first parameter is controller name (users), second is function name inside controller (update) and last one is the parameter (97), which will be provided to the function in the controller.

With the help of some tweaks in your .htacess, you can get rid of index.php in above URL and can get neat and clean URL as follow

Now a look on how models, views, controller are accessed in Codeigniter. There are some rules you have to follow to route the web request to correct controller. We will take about URL as an example.

  • There must be user.php in application/controllers folder.
  • User.php must have a class named Users (Note he first letter is capital here).
  • User class must be inherited by CI_Controller class( A parent controller class for all controllers).
  • Users class must have public function named update.
  • Update function must have a parameter, to which the last parameter (97) will be provided.

Function name and parameters are optional in URL. If function name is not provided, Codeigniter will take index as a function name by default. The number of parameters function has, the number of parameters you have to provide in the URL and they will be passed to the function’s parameters as they occurred in URL

Codeigniter load any model through model method of load object as follow


After that you will have class level variable Applicants_model ($Applicants_model) which is the object for model class Applicants_model. Now you can call any method from Applicants_model class with the variable you got, as follow


To load Applicants_model

  • There must be applicants.php file in application/models folder.
  • applicants.php must contain Applicants_model class.
  • Applicants_model class must be inherited by CI_Model class (A parent model class for all models).
  • Applicants_model class must have function named insert.

Same for view, controllers can call view as follow

$this->load->view('applicants_view', $data);

Second parameter is an array of data that will be passed to the view. Second parameter is optional and if it is array it each key will be available as a variable in called view. Mainly its keys are extracted by PHP extract function.

To load applicants_view

  • There must be applicants_view.php file in application/views folder.

Architecture is, any web request received by be Codeigniter be directed to the controller specified in web request’s URL. This eliminates the direct access to models and views and make controllers the sole responsible to trigger and control all operations. Then, to handle operation for views and models, functionality is provided to controller to load any model and view for interaction. By loading any model, controller is now have rights to do any changes, get any information or check any expected changes. After that, controller can notify any view about this change, pass information to customize it as needed and render it as an output. Clearly model and views are unaware of each other, controller is the glue, which becomes middle-ware to transit any change between views and models.

I want to split this article in two articles, one of MVC and another is for Codeigniter, but then I decided that it should be better in this way.

MVC is architecture to control access and customized provided information in desired fashion. Codeigniter comply it very well and developers enjoy coding their application due to its simplicity and power of MVC.

Some useful functions for WordPress plugins development

A glance on wordpress plugins

WordPress is one of the popular CMS. I like it due to its simplicity. A person who does not know PHP programming can customized WordPress highly, from back-end. Along With, WordPress has lots of PHP classes and functions for PHP developer to show their magic.

WordPress is not an object-oriented paradigm. You occasionally can find classes but it does not have true concepts of object-oriented programming. You often just call functions to get your way. However, I believe, this thing make development in WordPress faster. Although, it lacks the organization of related methods and functionalities but still it is a charm for developers due to its ease.

Programming level customization normally done through plugins or you can say it is the best way to customize WordPress. There are tons of article on web about how to create plugin. In my article I will not focus on how plugin is developed but I will listed some useful functions for its development

WordPress plugins are normally easy to install. You just have to paste your plugin in wp-content/plugins folder and activate it through admin panel and it starts to work. Also WordPress has mechanism that help you to create plugin without changing the code for core libraries. They call it hooks. To know about hooks , read my another article, WordPress hooks – filters and actions.

Your plugin could be small as a single file and could be large as a folder contain lots of php, javascript, CSS files and images. All you have to do is to put following header, in a file you want to make entry point for your plugin. This file will be called on every page load in WordPress

 * 	Plugin Name: Greyinfotech Job Portal
 * 	Plugin URI:
 * 	Description: Job Manager for all jobs related operations.
 * 	Version: 1.0
 * 	Author: Junaid Hassan
 * 	Author URI:

All information is optional except Plugin Name. From here, WordPress recognizes that this is entry point for a plugin.

Function often used in WordPress plugins

Every plugin must contain two functions. One for activation, one for deactivation. You hook these two functions, to be called when plugin is activated or deactivated through admin panel as follow

register_activation_hook(__FILE__, 'activate');
register_deactivation_hook(__FILE__, deactivate');

Note that, these two functions must be in the same plugin file, which you define as an entry point. Following are few functions, commonly used in plugin.

In WordPress you have global array $wp_filter, this array can be manipulated to see all hooks added so far.

add_filter and add_action is used to add functionality into system without effecting core APIs and add_shortcode is to add something to html design at specific place. Following is the example of short code

add_shortcode('my_shotcodes', 'my_shotcodes_func');

With the help of add_shortcode you can add shortcode for my_shotcodes tag.. In html where you want to add your desire html in the post, you add following

[my _shotcodes ]


[my _shotcodes param1=”register” param2=”form”]

Above are two examples for shotcodes, both will execute function my_shotcodes_func and replace my_shotcodes tag with the response return by the my_shotcodes_func. Second example above also passing two parameters (param1 & param2) to my_shotcodes_func. The function will get these in a form of array. Param1 and param2 will be the keys of array.

Functions could be used, as per need

If you are creating your own tables in plugin, you have to use dbDelta. dbDelta is very handy as it Identifies difference between new and old version changes and update accordingly.

If you have your own custom roles then you have to use add_role / remove_role / get_role to add/ delete and retrieve information regarding the roles. Also with custom role, custom capabilities could be added to the system. add_cap / remove_cap used for this purpose.

Something you need to add a static page or post to show on the front-end. Here, wp_insert_post / wp_delete_post is very handy. You have to define attributes for a post in any array and pass to the wp_insert_post function. Following is an example

$_p = array();
$_p['post_title'] = __('Job listing');
$_p['post_status'] = 'publish';
$_p['post_type'] = 'page';
$_p['comment_status'] = 'closed';
$_p['ping_status'] = 'closed';
$_p['post_category'] = array(1);
$the_page_id = wp_insert_post($_p);

Above are the most common functions I have seen, to be used in plugins so far. However, thousands of plugins are available and every developer has its own approach. But it is always better to go for the best practices for any framework, also it is always better to comment and test your code thoroughly before publish.

WordPress hooks – filters and actions

A glance on WordPress hooks – filters and actions

WordPress has become far more popular than anyother blogging framework in a very short period of time. With the time, along with the popularity, it also extends. Tons of plugins are available on WordPress’ website.

WordPress provides very handy mechanism to extend functionality of wordpress with out messing-up with its core libraries. This thing is very helpful when you try to upgrade the version, as you do not have to remember what you changed. The objective to makes new changes separated from original code is achieved by a mechanism known as Hooks in WordPress terminology.

What is hook ?

There are two types of hooks. We will talk about later but first, what is hook? Named suggest, to hook something, to attach something, to tag something. Of course, it is used to attach some extra functionality with WordPress’ core behavior for any event. Instead of, introducing new code in core libraries of WordPress we write code separately in a file (that could be your plugin file in plugins folder) and attach the reference or hook the reference to that code in WordPress global hook list and call that code where you want it to be executed. Looks complicated? But it is not.

Types of hooks – action and filter

As I said, there are two types of hooks, Number one is filter and second one is action. Filter is when you want to modify something; it could be anything, any variable, any text which is going to be echoed. On the other side, action is when you want to run something entirely new. In other words, you passed some value to filter and it changes that value and pass back to you while to action you just call and it will perform something.

Ok, let understand filter first with example, we have two magic function add_filter and apply_filters

function modify_data(data) {
   return $data." Is passed on ".date('l jS \of F Y h:i:s A');


First argument is a unique identifier for your filter. You will call it through this identifier. Second is the name of function that we created. add_filter also has 2 other parameters, priority that describe the sequence in which all functions hooked with a single hook will be executed. Default value for priority is 10 but you can change as say. And second one is number of arguments passed to filter.

Ok, now simply where you want to execute that filter just call it with apply_filters like this

$data = apply_filters(("my_hook", $data );

As you can figure out the above statement, it is quite self-explanatory.

Similarly for action but here we want to start another process to do something. add_action and do_action are magic functions.

function email_friends( $post_ID )
   $friends = ',';
   wp_mail( $friends, "sally's blog updated", 'I just put something on my blog:' );

   return $post_ID;
add_action('publish_post', 'email_friends');

Now you can call your action

do_action('publish_post' , 12);

You call the action by providing its unique identifier and the parameter required by function.

In the example for filter, we created our own hook that is my_hook but in action’s example we used a hook that is defined by WordPress already.
It elaborates that WordPress has defined many hooks for their system and if you exactly know what hook is required or where you want your code to be executed you can use pre-defined hook or you always free to create new one.

List of all hooks available on WordPress webiste

Apple IBook’s textbooks

IBook’s textbooks – Apple in education with digital books

Apple Corporation faced huge criticism for IPhone 5. User was expecting something very new but Apple failed to deliver that. Consumers always have trust for latest trends and this could be EDGE for any company.

People say Steve Jobs last dream was IBook’s textbooks, as he again wanted to through something innovative and new in market.

Apple provides the tool named IBook for Author. They can rewrite it to form digital for IPad. Apple has launched IBook 2, which allows author to aid 3-D images, videos, sounds and other very helpful visual aids to make study more attractive.

Many publishers have started worked on digital books and many digital textbooks are available on apple store. Apple has created a section for digital textbooks on ITunes.

According to Global Equities Research report, Apple sold 350,000 digital textbooks in 3 days after it launch. It was very big success and great start; it also indicates that people are following this technology.

In Future?

No doubt, it is big start, but what about future. One thing is it is only for IPad, which is not in the range of every student. Digital books itself are more expensive than ordinary book. Apple can only capture sub-set of students around the globe. In 3rd world countries, IBook’s textbooks are out of reach for many students due to cost of IPad. If Apple wants to capture global market, it has to define some approach to drag-down the expense on IBook’s textbooks at certain level where it is affordable for average student. Student study abroad already living hand to mouth, they have to bear their expenses and allowed to work in limited range of hours. IBook’s textbooks will be charm for them but could only remain the charm.

In addition, one thing that hurt Apple business is digital eBooks piracy. Anyone can find tons of eBooks on free file sharing website like rapidshare, filestube, 4shared, etc. Additionally, on one can deny the eBook piracy promotion through P2P file sharing programs like BitTorrent, eDonkey, kazaa, etc. After few years, tons of IBook’s textbooks will be available on these channels and people will go on apple store only for new books. Apple should also keep this depreciation in mind.

IBook’s textbooks are doing good. Let see where it will go. From Apple, people always expect better in existing and introduction of new technologies.


Get every new post delivered to your Inbox.