Sunday, February 17, 2013

Dos game programming

This is a post that introduces DOS ( Disk Operating System ) game programming and relevant skills.  In this post , it will introduce some easy and simple game programming skills under DOS environment.  Before starting , we assume you were familiar with C programming language and its basic skills.
Anyway, let's see some major and important features of  C .
Pointers (指標) - This is an important feature in C programming language. The major goal of it is related memory and address's manipulation . For following example , see below codes :


char *t1; /* Declare a pointer that point to character type */  
strcpy(t1, "hello world"); /* using string copy routine copy a string "hello world" */ 

It seems that above codes are available. But it may causes system crash (why ? ). The reason is we just declare a pointer variable t1 , but we didn't allocate its available memory space. 
When a variable was declared , compiler handles this variable via scanner (token/identifier construction ) and construct them by parser. The front end of Compiler , (scanner ) tokenizer - it responsible for extract that identifiers , they include available user defined identifiers , keywords , reverse words and so forth. Once it completed this stage and  they were constructed in a symbol table. The above character pointer that was allocated an available address .But  it just is a starting address , not an available memory space.  you may use the following codes , such as 
cout.setf(ios::hex, ios::basefield) ; 
cout << t1 ; /* To show its address in hexadecimal format */  

So This will crash many computers. We have declared the pointer t1, but we are now trying to copy data to it, BEFORE we make it point to a valid range of memory. 

char *t1; 
t1 = malloc(100);/* allocate an available memory space ; its size is 100 characters */  
strcpy(t1, "hello world"); /* Copy the string to the address of t1 */ 

Basically , malloc is a standard ANSI C library function and it responsible for allocating an available memory space from heap. but we must check the address of pointer is vaild or not. if the address is unavailable , its value should be NULL (this is a macro and its value is (void*)0 )   
So we should use a brief codes avoid an issue - NULL pointer assignment  ( See Wiki's answer ) the codes as below
t1 = malloc(100) ;

#if defined NULL 
  if ( t1 == NULL ) 
   std::cout  << "Error ! Allocating t1 is failed " << endl ;  
 else 
  strcpy(t1, "hello world");  /* Copy string to valid memory address */ 
#else  
  if ( t1 == (void*)0 )  /* type promotion */   
   std::cout  << "Error ! Allocating t1 is failed " << endl ;
 else 
  strcpy(t1, "hello world");  /* Copy string to valid memory address */ 
#endif  
the above was called defense programming skill . okay. Let's go ahead. In C , programmers can define or customize user defined data types. It can enhance readability. for example ,  see below

typedef const  char* CCPtr  ;
typedef char  (*Fptr) ( char* , int , CCPtr ) ;  
typedef char  (*FFptr) ( char* , CCptr , Fptr  ) ;
typedef void (*ptr_fun) (CCPtr , int , Fptr , FFptr ) ;

ptr_fun Sorting_strFun ;

Declare a function pointer that point to a function that owns three arguments , 1st    argument is a pointer which points to constant char , 2nd is integral type argument and the last is a pointer that points to a function which has three arguments.
If we don't customized type , this declaration likes below
void (*Sorting_strFun) (   const char* ,
                                        int ,
                                        char (*) (char* , int , const char* ) ,
                                        char (*) (char* ,   char (*) (char* , int , const char* ) ,
                                        char (*) ( char* , int , const char* )  ) ;
   About more complex typedef syntax , please refer to this book - Expert C programming and this link about C programming language typedef usage .
Again , we starting introduce some basic concepts and skills about game programming under DOS.
   
    

No comments:

Post a Comment