home *** CD-ROM | disk | FTP | other *** search
/ Game Developers Magazine 4 / GDM004.ZIP / SOURCE / SPRITED3 / SPRITED3.C
C/C++ Source or Header  |  1994-06-06  |  3KB  |  155 lines

  1. /*
  2.  
  3.   SPRITED3.C - More sprite drawing
  4.  
  5.   Written by Phil Inch for Game Developers Magazine (issue 4).
  6.   Contributed to the public domain.
  7.  
  8.   This program written and compiled with Borland C++ v3.1
  9.   Compatibility with other compilers is not guaranteed.
  10.  
  11.   Usage of this program is subject to the disclaimer printed
  12.   in the magazine.  You assume all risks associated with the use
  13.   of this program.
  14.  
  15.  
  16.   The sprite:
  17.  
  18.    01234567890123456
  19.    **......*......** 0
  20.    **......*......** 1
  21.    ........*........ 2
  22.    **.....***.....** 3
  23.    **.....***.....** 4
  24.    **....*****....** 5
  25.    **....*****....** 6
  26.    **...*******...** 7
  27.    **..*********..** 8
  28.    **.***********.** 9
  29.    *****.*****.***** 0
  30.    ****..*****..**** 1
  31.    ***...*****...*** 2
  32.    **.............** 3
  33.    **.....***.....** 4
  34.    **....*.*.*....** 5
  35.  
  36. */
  37.  
  38. /****************************** SPRITE DATA ********************************/
  39.  
  40. char sprite_data[] = {
  41.    17, 16,
  42.    14,14,0,0,0,0,0,0,1,0,0,0,0,0,0,14,14,
  43.    14,14,0,0,0,0,0,0,1,0,0,0,0,0,0,14,14,
  44.    0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,
  45.    12,12,0,0,0,0,0,12,12,12,0,0,0,0,0,12,12,
  46.    12,12,0,0,0,0,0,12,12,12,0,0,0,0,0,12,12,
  47.    12,12,0,0,0,0,12,12,12,12,12,0,0,0,0,12,12,
  48.    12,12,0,0,0,0,12,12,12,12,12,0,0,0,0,12,12,
  49.    12,12,0,0,0,12,12,12,12,12,12,12,0,0,0,12,12,
  50.    12,12,0,0,12,12,12,12,12,12,12,12,12,0,0,12,12,
  51.    12,12,0,12,12,12,12,12,12,12,12,12,12,12,0,12,12,
  52.    12,12,12,12,12,0,12,12,12,12,12,0,12,12,12,12,12,
  53.    12,12,12,12,0,0,12,12,12,12,12,0,0,12,12,12,12,
  54.    12,12,12,0,0,0,12,12,12,12,12,0,0,0,12,12,12,
  55.    12,12,0,0,0,0,0,0,0,0,0,0,0,0,0,12,12,
  56.    12,12,0,0,0,0,0,14,14,14,0,0,0,0,0,12,12,
  57.    12,12,0,0,0,0,14,0,14,0,14,0,0,0,0,12,12
  58.    };
  59.  
  60. /**************************** THE PROGRAM **********************************/
  61.  
  62. #include <stdio.h>
  63. #include <conio.h>
  64. #include <time.h>
  65. #include <dos.h>
  66. #include <mem.h>
  67. #include <string.h>
  68. #include <stdlib.h>
  69.  
  70. char far *screen=MK_FP(0xA000,0);
  71.  
  72. void    SetGraphicsMode( void ) {
  73.     asm {
  74.         mov    ax,0x13
  75.     int    0x10
  76.       }
  77. }
  78.  
  79. void    SetTextMode( void ) {
  80.     asm {
  81.       mov    ax,0x03
  82.     int    0x10;
  83.     }
  84. }
  85.  
  86. void SetPoint( int X, int Y, int C ) {
  87.   *(screen+(Y*320)+X)=C;
  88. }
  89.  
  90. /* Same routine as issue 3 */
  91. void draw_sprite( int sx, int sy ) {
  92.    int x, y, c, sw, sh, length;
  93.  
  94.    /* Retrieve the width and height */
  95.    sw = sprite_data[0];
  96.    sh = sprite_data[1];
  97.    c = 2;
  98.  
  99.    for ( y = 0; y < sh; y++ )
  100.      for ( x = 0; x < sw; x++ )
  101.        SetPoint( sx+x, sy+y, (int) sprite_data[c++] );
  102. }
  103.  
  104. void erase_sprite( int sx, int sy ) {
  105.    int x, y, sw, sh, length;
  106.  
  107.    /* Retrieve the width and height */
  108.    sw = sprite_data[0];
  109.    sh = sprite_data[1];
  110.  
  111.    for ( y = 0; y < sh; y++ )
  112.      for ( x = 0; x < sw; x++ )
  113.        SetPoint( sx+x, sy+y, 0 );  /* Set the points to 0 (black) */
  114. }
  115.  
  116. void main( void ) {
  117.   int sx=10, sy=10, mx=2, my=2;
  118.  
  119.   SetGraphicsMode();
  120.  
  121.   /* "Bounce" the sprite around the screen until the user presses a key */
  122.  
  123.   while (!kbhit()) {
  124.     draw_sprite(sx,sy);
  125.     delay(10);
  126.     erase_sprite(sx,sy);
  127.  
  128.     sx += mx;
  129.     sy += my;
  130.  
  131.     if (sx < 10) {
  132.       sx=10;
  133.       mx=-mx;
  134.       }
  135.  
  136.     if (sx > 300) {
  137.       sx=300;
  138.       mx=-mx;
  139.       }
  140.  
  141.     if (sy < 10) {
  142.       sy=10;
  143.       my=-my;
  144.       }
  145.  
  146.     if (sy > 180) {
  147.       sy=180;
  148.       my=-my;
  149.       }
  150.     }
  151.   getch();
  152.  
  153.   SetTextMode();
  154. }
  155.