Monday, June 23, 2014

Useful Links

C
1>  GCC Compilation Options :
http://www.thegeekstuff.com/2012/10/gcc-compiler-options/

2> 50 Linux Most Used Commands :
http://www.thegeekstuff.com/2010/11/50-linux-commands/

3>Compilation Steps :
http://www.thegeekstuff.com/2011/10/c-program-to-an-executable/

4>Make Utility :
http://www.thegeekstuff.com/2010/08/make-utility/

5>Memory Management ( Pointers) :
http://staff.um.edu.mt/csta1/courses/lectures/csa2060/c8a.html

6>Memory management :
http://shareprogrammingtips.com/c-language-programming-tips/structure-of-a-c-program-in-memory-how-heapstackdata-and-code-segments-are-stored-in-memory/

http://www.geeksforgeeks.org/memory-layout-of-c-program/

7> Dynamic Memory Allocation :
http://fresh2refresh.com/c/c-dynamic-memory-allocation/

7> Debugging in GCC -GDB- :
http://www.thegeekstuff.com/2010/03/debug-c-program-using-gdb/

Ping-Pong Game in C

Game : Ping-Pong Game
Language : C ( TURBO C Libraries used )

/*  Start of Code */
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <stdlib.h>
#include <bios.h>
#include <math.h>
#include <time.h>
#include <dos.h>
void game_status (int state);
int move_pad(int key);
void erase(int obj );
void ball_dynamix(void );
void play(void );
#define R_RIGHT 77 // Right_Arrow
#define R_LEFT 75 // Left_Arrow
#define R_UP 72 // Up_Arrow
#define R_DOWN 80 // Down_Arrow
#define QUIT   1 // (ESC)
#define L_RIGHT 32 //(D)
#define L_LEFT 30 //(A)
#define L_UP 17 //(W)
#define L_DOWN 31 //(S)
#define RESET 19 //(R)
#define INIT 0
#define UPDATE 10
#define X 3
#define Y 3
#define R_INIT_X_TOP 480
#define R_INIT_Y_TOP 215
#define R_INIT_X_BTM 482
#define R_INIT_Y_BTM 265
#define L_INIT_X_TOP 156
#define L_INIT_Y_TOP 215
#define L_INIT_X_BTM 158
#define L_INIT_Y_BTM 265
#define BALL_X_POS 319
#define BALL_Y_POS 240
#define SPEED_X 3
#define SPEED_Y 3

struct ball
{
int x_new;
int x_old;
int x_speed;
int y_new;
int y_old;
int y_speed;
}b;
struct pad
{
int cur_x_top;
int cur_y_top;
int cur_x_btm;
int cur_y_btm;
}r,l;

int dirn,l_score=0,r_score=0;
char ballpos[20],r_skor[10],l_skor[10];
void main()
{
   /* request autodetection */
   int gdriver = DETECT, gmode, errorcode;
   int mid_x = getmaxx()/2,mid_y = getmaxy()/2; // mid_x = 239.5 , mid_y = 319.5
   printf("X = %d, Y = %d",mid_x,mid_y);
   /* initialize graphics mode */
   initgraph(&gdriver, &gmode, "\\tc\\bgi");
   /* read result of initialization */
   errorcode = graphresult();
   if (errorcode != grOk)    /* an error occurred */
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1);               /* return with error code */
   }

  game_status(INIT);
  play();
  getch();
  closegraph();
}
void game_status(int state)
{
   srand(time(NULL));  // Seed ONCE
   dirn = rand()%10;
   if (state == INIT)
   {
  /* Draw the Net */
  setfillstyle(XHATCH_FILL, BLUE);
  bar(317,30,320,449);
  /* Draw the Pitch Borders */
  setcolor(WHITE);
  rectangle(20,30,619,449);
  /* Display Instructions & Other Texts */
  outtextxy (25,20,"Press ESC to exit game");
  outtextxy (25,452,"Player-1 :");
  outtextxy (515,452,"Player-2 :");
  /* Create Ball */
  setfillstyle(SOLID_FILL, BLUE);
  fillellipse(BALL_X_POS,BALL_Y_POS,3,3);
   b.x_new = BALL_X_POS;
   b.y_new = BALL_Y_POS;
   b.x_old = BALL_X_POS;
   b.y_old = BALL_Y_POS;
   b.x_speed = SPEED_X;
   b.y_speed = SPEED_Y;
   if ( dirn == 2)
   {
   b.y_speed = 0;
   printf("IN-2");
   }
   if (dirn == 7)
   {
   b.y_speed = 0;
   printf("IN-7");
   }  
 
  /* Draws Right & Left Pad*/
  setfillstyle(SOLID_FILL, GREEN);
  bar(R_INIT_X_TOP,R_INIT_Y_TOP,R_INIT_X_BTM,R_INIT_Y_BTM);
  bar(L_INIT_X_TOP,L_INIT_Y_TOP,L_INIT_X_BTM,L_INIT_Y_BTM);
  /* Initialise The Pads */
   r.cur_x_top = R_INIT_X_TOP;
   r.cur_y_top = R_INIT_Y_TOP;
   r.cur_x_btm = R_INIT_X_BTM;
   r.cur_y_btm = R_INIT_Y_BTM;
   l.cur_x_top = L_INIT_X_TOP;
   l.cur_y_top = L_INIT_Y_TOP;
   l.cur_x_btm = L_INIT_X_BTM;
   l.cur_y_btm = L_INIT_Y_BTM;
   }
    if ( state == RESET )
   {
    erase(0);
erase(1);
erase(2);
game_status(INIT);
   }
     if ( state == QUIT )
   {
    exit(0);
   }
}
int move_pad(int key)
{
switch(key)
{
case R_LEFT:
if (r.cur_x_top > 325)
{
erase(1);
setfillstyle(SOLID_FILL, GREEN);
bar(r.cur_x_top-X,r.cur_y_top,r.cur_x_btm-X,r.cur_y_btm);
r.cur_x_top -= X;
r.cur_x_btm -= X;
}
break;
case R_RIGHT:
if (r.cur_x_btm > 315 && r.cur_x_btm <610)
{
erase(1);
setfillstyle(SOLID_FILL, GREEN);
bar(r.cur_x_top+X,r.cur_y_top,r.cur_x_btm+X,r.cur_y_btm);
r.cur_x_top += X;
r.cur_x_btm += X;
}
break;
case R_UP:
if (r.cur_y_top > 35)
{
erase(1);
setfillstyle(SOLID_FILL, GREEN);
bar(r.cur_x_top,r.cur_y_top-Y,r.cur_x_btm,r.cur_y_btm-Y);
r.cur_y_top -= Y;
r.cur_y_btm -= Y;
}
break;
case R_DOWN:
if (r.cur_y_btm < 445)
{
erase(1);
setfillstyle(SOLID_FILL, GREEN);
bar(r.cur_x_top,r.cur_y_top+Y,r.cur_x_btm,r.cur_y_btm+Y);
r.cur_y_top += Y;
r.cur_y_btm += Y;
}
break;
case L_LEFT:
if (l.cur_x_top > 23)
{
erase(2);
setfillstyle(SOLID_FILL, GREEN);
bar(l.cur_x_top-X,l.cur_y_top,l.cur_x_btm-X,l.cur_y_btm);
l.cur_x_top -= X;
l.cur_x_btm -= X;
}
break;
case L_RIGHT:
if (l.cur_x_btm > 20 && l.cur_x_btm < 310 )
{
erase(2);
setfillstyle(SOLID_FILL, GREEN);
bar(l.cur_x_top+X,l.cur_y_top,l.cur_x_btm+X,l.cur_y_btm);
l.cur_x_top += X;
l.cur_x_btm += X;
}
break;
case L_UP:
if (l.cur_y_top > 30)
{
erase(2);
setfillstyle(SOLID_FILL, GREEN);
bar(l.cur_x_top,l.cur_y_top-Y,l.cur_x_btm,l.cur_y_btm-Y);
l.cur_y_top -= Y;
l.cur_y_btm -= Y;
}
break;
case L_DOWN:
if ( l.cur_y_btm < 445)
{
erase(2);
setfillstyle(SOLID_FILL, GREEN);
bar(l.cur_x_top,l.cur_y_top+Y,l.cur_x_btm,l.cur_y_btm+Y);
l.cur_y_top += Y;
l.cur_y_btm += Y;
}
break;
}
return 1;
//printf("R:%d:%d:%d:%d\n",r.cur_x_top,r.cur_y_top,r.cur_x_btm,r.cur_y_btm);
}
void erase(int obj)
{
  if ( obj== 0)  // Erase Ball
  {
   b.x_old = b.x_new;
   b.y_old = b.y_new;
   setcolor (BLACK);
   setfillstyle(SOLID_FILL, BLACK);
   fillellipse(b.x_new,b.y_new,3,3);
   }
  if ( obj== 1)  // Erase Right Paddle
  {
   setfillstyle(SOLID_FILL, BLACK);
   bar(r.cur_x_top,r.cur_y_top,r.cur_x_btm,r.cur_y_btm);
  }
  if ( obj== 2)  // Erase Left Paddle
  {
   setfillstyle(SOLID_FILL, BLACK);
   bar(l.cur_x_top,l.cur_y_top,l.cur_x_btm,l.cur_y_btm);
  }
}
void play(void )
{
int move;
while (1)
{

  delay (20); // Reduce game speed to human playable level
       if (inportb (0X60) == R_RIGHT)  move_pad (R_RIGHT);
       if (inportb (0X60) == R_LEFT)   move_pad (R_LEFT);
       if (inportb (0X60) == R_UP)     move_pad (R_UP);
       if (inportb (0X60) == R_DOWN)   move_pad (R_DOWN);
  if (inportb (0X60) == L_RIGHT)  move_pad (L_RIGHT);
       if (inportb (0X60) == L_LEFT)   move_pad (L_LEFT);
       if (inportb (0X60) == L_UP)     move_pad (L_UP);
       if (inportb (0X60) == L_DOWN)   move_pad (L_DOWN);
  if (inportb (0X60) == RESET)    game_status(RESET);
  if (inportb (0X60) == QUIT)     game_status(QUIT);
  ball_dynamix();
  setfillstyle(XHATCH_FILL, BLUE);
  bar(317,30,320,449);
 }
}
void ball_dynamix()
{
   erase(0);
   switch (dirn)
   {
    case 0: case 1: // RIGHT-UP
b.x_new = b.x_new + b.x_speed;
b.y_new = b.y_new - b.y_speed;
break;
case 2: // RIGHT
b.x_new = b.x_new + b.x_speed;
    b.y_new = b.y_new + b.y_speed;
break;
    case 3: case 4: // RIGHT-DOWN
b.x_new = b.x_new + b.x_speed;
b.y_new = b.y_new + b.y_speed;
break;
case 5: case 6: // LEFT-UP
b.x_new = b.x_new - b.x_speed;
b.y_new = b.y_new - b.y_speed;
break;
case 7: // LEFT
b.x_new = b.x_new - b.x_speed;
b.y_new = b.y_new + b.y_speed;
break;
case 8: case 9: // LEFT-DOWN
b.x_new = b.x_new - b.x_speed;
    b.y_new = b.y_new - b.y_speed;
break;
}
   setfillstyle (SOLID_FILL,GREEN); // Display New postion.
   fillellipse ( b.x_new, b.y_new , 3,3);
   if ( b.y_new - 3 < 35 ) b.y_speed = -b.y_speed; // Reflect From Top
   if ( b.y_new + 3 > 445 ) b.y_speed = -b.y_speed; // Reflect From Bottom
   if ( b.x_new - 3 < 25 ) // Reflect From Left
   {
   b.x_speed = -b.x_speed;
   l_score++;
   }
   if ( b.x_new + 3 > 615 ) // Reflect From Right
   {
   b.x_speed = -b.x_speed;
   r_score++;
   }
 
// Deflect Left UP
   if  ((b.y_new >= l.cur_y_top && b.y_new <= l.cur_y_btm-35 ) && (b.x_new >= l.cur_x_top - 6 && b.x_new <= l.cur_x_btm + 6))
    {
b.x_speed = -b.x_speed;
b.y_speed = -2;
}
// Deflect Left Straight
if  ((b.y_new >= l.cur_y_top+15 && b.y_new <= l.cur_y_btm-15 ) && (b.x_new >= l.cur_x_top - 6 && b.x_new <= l.cur_x_btm + 6))
    {
b.x_speed = -b.x_speed;
}
// Deflect Left DOWN
   if  ((b.y_new >= l.cur_y_top+35 && b.y_new <= l.cur_y_btm ) && (b.x_new >= l.cur_x_top - 6 && b.x_new <= l.cur_x_btm + 6))
    {
b.x_speed = -b.x_speed;
b.y_speed = 2;
}

// Deflect Right UP
   if  ((b.y_new >= r.cur_y_top && b.y_new <= r.cur_y_btm-35 ) && (b.x_new >= r.cur_x_top - 6 && b.x_new <= r.cur_x_btm + 6))
    {
b.x_speed = -b.x_speed;
b.y_speed = -2;
}
// Deflect Right Straight
   if  ((b.y_new >= r.cur_y_top+15 && b.y_new <= r.cur_y_btm-15 ) && (b.x_new >= r.cur_x_top - 6 && b.x_new <= r.cur_x_btm + 6))
       {
b.x_speed = -b.x_speed;
}
// Deflect Right DOWN
   if  ((b.y_new >= r.cur_y_top+35 && b.y_new <= r.cur_y_btm ) && (b.x_new >= r.cur_x_top - 6 && b.x_new <= r.cur_x_btm + 6))
    {
b.x_speed = -b.x_speed;
b.y_speed = 2;
}
 }
/*  End of Code */

                     ENJOY!!!!!

Sunday, June 1, 2014

Matlab Useful Commands


Commands Purpose
General Ver Gets Details of all the toolbox installed with version #'s
  Version MATLAB Version Running
  clc Clear Command Window
  clear all Clear complete workspace
  clear variable Clears the variable specified
  Exit  Exits from matlab
  Quit  
  Ctrl + C Stop the current execution ( in Command window )
  lookfor Search for similar commands
     
Workspace who Shows variable names in the currently available workspace
  whos Shows variable names & Details in the currenty available workspace
  exist Check for existence of the variable in the workspace
  save filename Save as filename.mat to disk
  Load filename Load filename.mat from disk
     
Variables Global Declares variable as global
  persistent Defines a persistent variable ( static )
  a = [ 1 , 2 ,3 ] Create Row Matrix
  a = [ 1  2 3 ]  
  b = [ 1; 2; 3 ] Creates Column Matrix
  c = [1 2 3 ; 4 5 6 ; 7 8 9] Creates a 3*3 Matrix
     
Directory / System Dir Lists contents of Current Directory
  cd newdir Change directory  newdir
  cd .. Goes to current directorie's parent directory
  mkdir newdir creates a new directory newdir
  rmdir newdir Deletes directory newdir
  rmdir ('newdir','s') Deletes directory newdirand any folders inside recursively
  delete newfile delete's the file newfile
  pwd Displays the current working directory
  date displays the current system date
  type newfile Display the contents of newfile
  what Lists all the matlab files in the current directory
  diary  Switches on/off the diary for recording contents of the command window
     

Wednesday, January 1, 2014

Bit Operations

Useful C Programs
1> Bit Operations
          Set , Clear, Check bit.

#include<stdio.h>.
void showbit(int);
int setbit(int,int);
int clearbit(int,int);
int rtshift(int,int);
int lfshift(int,int);
int checkbit(int,int);
int dectohex(int);
int bintohex(int);
int main()
{
    int a,b,value;
    value =(10);
    printf("%d:\n",value);
    showbit(value);
    a = rtshift(value ,2);
    printf("%d:\n",a);
    showbit(a);
    b = lfshift(value ,2);
    printf("%d:\n",b);
    showbit(b);
    checkbit(value,1)? printf("Bit Set"):printf("Bit Clear");
    return 0;
}
void showbit(int a)
{
    int i,d=0;
    for(i=31;i>=0;i--)
    {
     if(!(a &(1<<i)))
     {
      printf("0");
      d++;
     }
      else
     {
      printf("1");
      d++;
     }
     if (d == 4)
     {
     printf(" ");
     d=0;
     }
    }
 printf("\n");

//Simpler way for above
//!(a &(1<<i))?printf("0"):printf("1");
}
int rtshift(int val,int rtshft)
{
return (val>>rtshft);
}
int lfshift(int val,int lfshft)
{
return (val<<lfshft);
}

int setbit(int val,int pos)
{
    return (val|(1<<pos));
}
int clearbit(int val,int pos)
{
    return (val&=~(1 << (pos-1)));
}
int checkbit(int val,int pos)
{
    return (val & (1<<pos));
}