home *** CD-ROM | disk | FTP | other *** search
/ Game Developers Magazine 3 / GDM003.ZIP / SPRITDEM.C < prev    next >
C/C++ Source or Header  |  1994-02-18  |  7KB  |  218 lines

  1. // SPRITDEM.C - Introduction to sprite drawing
  2.  
  3. // Written by Phil Inch for Game Developers Magazine (issue 3).
  4. // Contributed to the public domain.
  5.  
  6. // This program written and compiled with Borland C++ v3.1
  7. // Compatibility with other compilers is not guaranteed.
  8.  
  9. // Usage of this program is subject to the disclaimer printed
  10. // in the magazine.  You assume all risks associated with the use
  11. // of this program.
  12.  
  13. //
  14. // The sprite:
  15. //
  16. //   01234567890123456
  17. //   **......*......** 0
  18. //   **......*......** 1
  19. //   ........*........ 2
  20. //   **.....***.....** 3
  21. //   **.....***.....** 4
  22. //   **....*****....** 5
  23. //   **....*****....** 6
  24. //   **...*******...** 7
  25. //   **..*********..** 8
  26. //   **.***********.** 9
  27. //   *****.*****.***** 0
  28. //   ****..*****..**** 1
  29. //   ***...*****...*** 2
  30. //   **.............** 3
  31. //   **.....***.....** 4
  32. //   **....*.*.*....** 5
  33.  
  34.  
  35. /****************************** SPRITE DATA ********************************/
  36.  
  37. /* Method one - two dimensional array, where
  38.  
  39.    method1_data[p][0] = X
  40.    method1_data[p][1] = Y
  41.    method1_data[p][2] = COLOUR                  */
  42.  
  43. char method1_data[139][3] = {
  44.    {0,0,14}, {1,0,14}, {8,0,1}, {15,0,14}, {16,0,14},
  45.    {0,1,14}, {1,1,14}, {8,1,1}, {15,1,14}, {15,1,15},
  46.    {8,2,1},
  47.    {0,3,12}, {1,3,12}, {7,3,12}, {8,3,12}, {9,3,12}, {15,3,12}, {16,3,12},
  48.    {0,4,12}, {1,4,12}, {7,4,12}, {8,4,12}, {9,4,12}, {15,4,12}, {16,4,12},
  49.    {0,5,12}, {1,5,12}, {6,5,12}, {7,5,12}, {8,5,12}, {9,5,12}, {10,5,12}, {15,5,12}, {16,5,12},
  50.    {0,6,12}, {1,6,12}, {6,6,12}, {7,6,12}, {8,6,12}, {9,6,12}, {10,6,12}, {15,6,12}, {16,6,12},
  51.    {0,7,12}, {1,7,12}, {5,7,12}, {6,7,12}, {7,7,12}, {8,7,12}, {9,7,12}, {10,7,12}, {11,7,12}, {15,7,12}, {16,7,12},
  52.    {0,8,12}, {1,8,12}, {4,8,12}, {5,8,12}, {6,8,12}, {7,8,12}, {8,8,12}, {9,8,12}, {10,8,12}, {11,8,12}, {12,8,12}, {15,8,12}, {16,8,12},
  53.    {0,9,12}, {1,9,12}, {3,9,12}, {4,9,12}, {5,9,12}, {6,9,12}, {7,9,12}, {8,9,12}, {9,9,12}, {10,9,12}, {11,9,12}, {12,9,12}, {13,9,12}, {15,9,12}, {16,9,12},
  54.    {0,10,12}, {1,10,12}, {2,10,12}, {3,10,12}, {4,10,12}, {6,10,12}, {7,10,12}, {8,10,12}, {9,10,12}, {10,10,12}, {12,10,12}, {13,10,12}, {14,10,12}, {15,10,12}, {16,10,12},
  55.    {0,11,12}, {1,11,12}, {2,11,12}, {3,11,12}, {6,11,12}, {7,11,12}, {8,11,12}, {9,11,12}, {10,11,12}, {13,11,12}, {14,11,12}, {15,11,12}, {16,11,12},
  56.    {0,12,12}, {1,12,12}, {2,12,12}, {6,12,12}, {7,12,12}, {8,12,12}, {9,12,12}, {10,12,12}, {14,12,12}, {15,12,12}, {16,12,12},
  57.    {0,13,12}, {1,13,12}, {15,13,12}, {16,13,12},
  58.    {0,14,12}, {1,14,12}, {7,14,14}, {8,14,14}, {9,14,14}, {15,14,12}, {16,14,12},
  59.    {0,15,12}, {1,15,12}, {6,15,14}, {8,15,14}, {10,15,14}, {15,15,12}, {16,15,12}
  60.    };
  61.  
  62.  
  63. /* Method two - two dimensional array of the same size as the sprite */
  64.  
  65. char method2_data[16][17] = {
  66.    { 14,14,0,0,0,0,0,0,1,0,0,0,0,0,0,14,14 },
  67.    { 14,14,0,0,0,0,0,0,1,0,0,0,0,0,0,14,14 },
  68.    { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 },
  69.    { 12,12,0,0,0,0,0,12,12,12,0,0,0,0,0,12,12},
  70.    { 12,12,0,0,0,0,0,12,12,12,0,0,0,0,0,12,12},
  71.    { 12,12,0,0,0,0,12,12,12,12,12,0,0,0,0,12,12},
  72.    { 12,12,0,0,0,0,12,12,12,12,12,0,0,0,0,12,12},
  73.    { 12,12,0,0,0,12,12,12,12,12,12,12,0,0,0,12,12},
  74.    { 12,12,0,0,12,12,12,12,12,12,12,12,12,0,0,12,12},
  75.    { 12,12,0,12,12,12,12,12,12,12,12,12,12,12,0,12,12},
  76.    { 12,12,12,12,12,0,12,12,12,12,12,0,12,12,12,12,12},
  77.    { 12,12,12,12,0,0,12,12,12,12,12,0,0,12,12,12,12},
  78.    { 12,12,12,0,0,0,12,12,12,12,12,0,0,0,12,12,12},
  79.    { 12,12,0,0,0,0,0,0,0,0,0,0,0,0,0,12,12},
  80.    { 12,12,0,0,0,0,0,14,14,14,0,0,0,0,0,12,12},
  81.    { 12,12,0,0,0,0,14,0,14,0,14,0,0,0,0,12,12}
  82.    };
  83.  
  84.  
  85. /* Method three - flat array which also contains the dimensions */
  86.  
  87. char method3_data[] = {
  88.    17, 16,
  89.    14,14,0,0,0,0,0,0,1,0,0,0,0,0,0,14,14,
  90.    14,14,0,0,0,0,0,0,1,0,0,0,0,0,0,14,14,
  91.    0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,
  92.    12,12,0,0,0,0,0,12,12,12,0,0,0,0,0,12,12,
  93.    12,12,0,0,0,0,0,12,12,12,0,0,0,0,0,12,12,
  94.    12,12,0,0,0,0,12,12,12,12,12,0,0,0,0,12,12,
  95.    12,12,0,0,0,0,12,12,12,12,12,0,0,0,0,12,12,
  96.    12,12,0,0,0,12,12,12,12,12,12,12,0,0,0,12,12,
  97.    12,12,0,0,12,12,12,12,12,12,12,12,12,0,0,12,12,
  98.    12,12,0,12,12,12,12,12,12,12,12,12,12,12,0,12,12,
  99.    12,12,12,12,12,0,12,12,12,12,12,0,12,12,12,12,12,
  100.    12,12,12,12,0,0,12,12,12,12,12,0,0,12,12,12,12,
  101.    12,12,12,0,0,0,12,12,12,12,12,0,0,0,12,12,12,
  102.    12,12,0,0,0,0,0,0,0,0,0,0,0,0,0,12,12,
  103.    12,12,0,0,0,0,0,14,14,14,0,0,0,0,0,12,12,
  104.    12,12,0,0,0,0,14,0,14,0,14,0,0,0,0,12,12
  105.    };
  106.  
  107.  
  108. /**************************** THE PROGRAM **********************************/
  109.  
  110. #include <stdio.h>
  111. #include <conio.h>
  112. #include <time.h>
  113. #include <dos.h>
  114. #include <mem.h>
  115. #include <string.h>
  116. #include <stdlib.h>
  117.  
  118. char far *screen=MK_FP(0xA000,0);
  119.  
  120. void    SetGraphicsMode( void ) {
  121.     asm {
  122.         mov    ax,0x13
  123.     int    0x10
  124.       }
  125. }
  126.  
  127. void    SetTextMode( void ) {
  128.     asm {
  129.       mov    ax,0x03
  130.     int    0x10;
  131.     }
  132. }
  133.  
  134. void SetPoint( int X, int Y, int C ) {
  135.   *(screen+(Y*320)+X)=C;
  136. }
  137.  
  138. void method1_draw( int sw, int sh, int sx, int sy ) {
  139.    int c;
  140.  
  141.    for ( c = 0; c < 139; c++ )
  142.        SetPoint( (int)(method1_data[c][0]+sx), (int)(method1_data[c][1]+sy), (int)method1_data[c][2] );
  143. }
  144.  
  145. void method2_draw( int sw, int sh, int sx, int sy ) {
  146.    int c, x, y;
  147.  
  148.    for ( y = 0; y < sh; y++ )
  149.        for ( x = 0; x < sw; x++ )
  150.            SetPoint( x+sx, y+sy, (int)method2_data[y][x] );
  151.  
  152. }
  153.  
  154. void method3_draw( int sx, int sy ) {
  155.    int x, y, c, sw, sh, length;
  156.  
  157.    /* Retrieve the width and height */
  158.    sw = method3_data[0];
  159.    sh = method3_data[1];
  160.    c = 2;
  161.  
  162.    for ( y = 0; y < sh; y++ )
  163.        for ( x = 0; x < sw; x++ )
  164.            SetPoint( sx+x, sy+y, (int) method3_data[c++] );
  165. }
  166.  
  167. void main( void ) {
  168.   char input[64];
  169.   int d,sx,sy,number_of_sprites;
  170.  
  171.     clrscr();
  172.  
  173.     printf( "*** SPRITE DRAWING DEMO ***\n\n" );
  174.     printf( "Suggestions for number of sprites:\n\n" );
  175.     printf( "486/50:  5000\n" );
  176.     printf( "486/33:  4000\n" );
  177.     printf( "486/25:  3000\n" );
  178.     printf( "386/40:  1000\n" );
  179.     printf( "386/33:   500\n" );
  180.     printf( "lower :   250\n\n" );
  181.  
  182.     printf( "Enter number of sprites to draw: " );
  183.     gets( input );
  184.  
  185.     number_of_sprites = atol(input);
  186.     if ( number_of_sprites <= 0 ) {
  187.         printf( "That's not a valid number!" );
  188.         exit(1);
  189.         }
  190.  
  191.   SetGraphicsMode();
  192.  
  193.   gotoxy( 16, 12 ); printf( "Method 1" ); delay(3000);
  194.   for ( d = 0; d < number_of_sprites; d++ ) {
  195.     sx = rand()%320;
  196.     sy = rand()%200;
  197.     method1_draw( 17, 16, sx, sy );
  198.     }
  199.   SetGraphicsMode();  // quick way of clearing the graphics screen
  200.  
  201.   gotoxy( 16, 12 ); printf( "Method 2" ); delay(3000);
  202.   for ( d = 0; d < number_of_sprites; d++ ) {
  203.     sx = rand()%320;
  204.     sy = rand()%200;
  205.     method2_draw( 17, 16, sx, sy );
  206.     }
  207.   SetGraphicsMode();  // quick way of clearing the graphics screen
  208.  
  209.   gotoxy( 16, 12 ); printf( "Method 3" ); delay(3000);
  210.   for ( d = 0; d < number_of_sprites; d++ ) {
  211.     sx = rand()%320;
  212.     sy = rand()%200;
  213.     method3_draw( sx, sy );
  214.     }
  215.  
  216.   SetTextMode();
  217. }
  218.