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

  1. /*
  2.  
  3.   SPRITED4.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. char save_background[sizeof(sprite_data)];
  61.  
  62. /**************************** THE PROGRAM **********************************/
  63.  
  64. #include <stdio.h>
  65. #include <conio.h>
  66. #include <time.h>
  67. #include <dos.h>
  68. #include <mem.h>
  69. #include <string.h>
  70. #include <stdlib.h>
  71.  
  72. char far *screen=MK_FP(0xA000,0);
  73.  
  74. void    SetGraphicsMode( void ) {
  75.     asm {
  76.         mov    ax,0x13
  77.     int    0x10
  78.       }
  79. }
  80.  
  81. void    SetTextMode( void ) {
  82.     asm {
  83.       mov    ax,0x03
  84.     int    0x10;
  85.     }
  86. }
  87.  
  88. void GetPoint( int X, int Y, unsigned char *C ) {
  89.   *C = *(screen+(Y*320)+X);
  90. }
  91.  
  92. void SetPoint( int X, int Y, unsigned char C ) {
  93.   *(screen+(Y*320)+X)=C;
  94. }
  95.  
  96. /* For the first time, we are now going tell the draw routine which sprite
  97. we want to draw.  To do this, we simply pass the address of the sprite data */
  98.  
  99. void draw_sprite( char *sprite, int sx, int sy ) {
  100.    int x, y, c, sw, sh, length;
  101.  
  102.    /* Retrieve the width and height */
  103.    sw = sprite[0];
  104.    sh = sprite[1];
  105.  
  106.    save_background[0] = sw;
  107.    save_background[1] = sh;
  108.    c = 2;
  109.  
  110.    for ( y = 0; y < sh; y++ )
  111.      for ( x = 0; x < sw; x++ ) {
  112.        if (sprite[c]) {
  113.          GetPoint( sx+x, sy+y, &(save_background[c]) );
  114.          SetPoint( sx+x, sy+y, sprite[c] );
  115.          }
  116.        else
  117.          save_background[c]=0;
  118.        c++;
  119.        }
  120. }
  121.  
  122. /* To erase the sprite, just draw the background.  This is similar to the
  123. sprite drawing routine except we're not saving the background first! */
  124.  
  125. void erase_sprite( char *save, int sx, int sy ) {
  126.    int x, y, c, sw, sh, length;
  127.  
  128.    /* Retrieve the width and height */
  129.    sw = save[0];
  130.    sh = save[1];
  131.    c = 2;
  132.  
  133.    for ( y = 0; y < sh; y++ )
  134.      for ( x = 0; x < sw; x++ ) {
  135.        if (save[c]) SetPoint( sx+x, sy+y, save[c] );
  136.        c++;
  137.        }
  138. }
  139.  
  140. void main( void ) {
  141.   int sx=10, sy=10, mx=2, my=2;
  142.  
  143.   SetGraphicsMode();
  144.  
  145.   /* Set up a colourful background */
  146.  
  147.   _fmemset(screen,1,12800);
  148.   _fmemset(screen+12800,2,12800);
  149.   _fmemset(screen+25600,3,12800);
  150.   _fmemset(screen+38400,4,12800);
  151.   _fmemset(screen+51200,5,12800);
  152.  
  153.   /* "Bounce" the sprite around the screen until the user presses a key */
  154.  
  155.   while (!kbhit()) {
  156.     draw_sprite(sprite_data,sx,sy);
  157.     delay(10);
  158.     erase_sprite(save_background,sx,sy);
  159.  
  160.     sx += mx;
  161.     sy += my;
  162.  
  163.     if (sx < 10) {
  164.       sx=10;
  165.       mx=-mx;
  166.       }
  167.  
  168.     if (sx > 300) {
  169.       sx=300;
  170.       mx=-mx;
  171.       }
  172.  
  173.     if (sy < 10) {
  174.       sy=10;
  175.       my=-my;
  176.       }
  177.  
  178.     if (sy > 180) {
  179.       sy=180;
  180.       my=-my;
  181.       }
  182.     }
  183.   getch();
  184.  
  185.   SetTextMode();
  186. }
  187.