home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / COMMON.ZIP / 13SMPL.TXT < prev    next >
Encoding:
Text File  |  1995-10-03  |  9.3 KB  |  374 lines

  1. mode13 graphics
  2. In mode 13 things are very simple. You have an arrary thats 320x200
  3. and each byte is a pixel.
  4.  
  5. Sprites
  6.  
  7. A sprite is just a small bitmap that you draw over and over, moving a
  8. bit each time to make it move.
  9.  
  10. Making a sprite.
  11. Draw it in your favorite paint program, and save it. Then you have
  12. to convert the saved file to a plain linear bitmap. Once the saved
  13. art is in the same format as the mode13 screen, you just copy it to
  14. the screen mem to display it.
  15.  
  16. Drawing a sprite without messing up the background.
  17.  
  18. Before you draw the sprite you get the area beneath were the sprite
  19. is to be drawn and save it. At the start of the next frame replace
  20. this saved bitmap before drawing your sprite at its new location.
  21. When drawing a lot of sprites it is best to write a routine that
  22. saves all the 'saved bitmaps' in a list, then you have to put them
  23. back in reverse order to prevent any garbage from appearing.
  24.  
  25. Flicker
  26.  
  27. If you just draw straight to the screen, drawing large bitmaps will
  28. cause flicker, and shearing. There are a number of methods to prevent
  29. this and here is a simple one.
  30.  
  31. Draw everything to an off-screen buffer. This is just 64k that you
  32. malloc and draw to as if it was the screen. When you have drawn
  33. everything for one frame, copy this offscreen buffer to the video
  34. mem. BUT wait for the vertical re-trace before copying it over. This
  35. way you will be changing the screen mem while the hardware is moving
  36. the 'scan-line' back to the top of the screen, and you won't get any
  37. flicker.
  38.  
  39.  
  40. BGI
  41. Borland includes a BGI graphics lib, it is easy to use but DEAD SLOW.
  42. The init_graph() (or whatever it called) can be used to get into
  43. 320x200 256 mode, I think they callit MCGA mode.
  44.  
  45. Here is some code to get you started.
  46.  
  47. /*
  48.  
  49.    mode13.h
  50.  
  51.    copyright 1993, Alec Russell, ALL rights reserved
  52.  
  53.    320x200, 256 color mode routines, BIOS mode 0x13
  54.  
  55. */
  56.  
  57. #ifndef DEF_MODE13
  58. #define DEF_MODE13 1
  59.  
  60. #define USHORT unsigned short;
  61.  
  62. // dead simple sprite struct
  63. typedef struct
  64.    {
  65.    USHORT width, height;
  66.    BYTE far *bitmap;
  67.    }
  68. shape_t;
  69.  
  70. typedef unsigned char far * FARPTR;
  71.  
  72. #define INPUT_STATUS_1  03dah   //Input Status 1 register
  73. #define INPUT_STATUS_0  03dah   //Input status 0 register
  74.  
  75. #endif
  76.  
  77. /* ----------------------- end of file ------------------------------- */
  78.  
  79. // start of blit13.c---------------------------------------------------
  80.  
  81. /*
  82.    Copyright 1993, Alec Russell, ALL rights reserved
  83.    Permission granted to use this as you wish.
  84.  
  85.    FILE :13blit.c
  86.  
  87.          mode 13 blit stuff  --- MODE 13 !!! 320x200 x 256 colurs
  88.  
  89.  
  90.    HISTORY:
  91.       created :  may 19, 1993
  92.       updates :
  93.  
  94. */
  95.  
  96. #pragma inline
  97.  
  98.  
  99. /*
  100.    draws to the screen directly.
  101.  
  102.    x, y, width, height all in pixels, mode 0x13 ONLY
  103.  
  104.    MAX width and height is 255!!!!!
  105.  
  106.    buffer pointer to a width by height array of bytes that are a bitmap
  107.  
  108.    x, y is position to display at, NO checking done for valid
  109.    co-ords etc...
  110.  
  111.    WIDTH MUST BE EVEN!!!!
  112.  
  113.  
  114. */
  115. /* ---------------------- put_blit() --------------------- March 23,1993 */
  116. void put_blit(FARPTR buffer, short x, short y, short width, short height)
  117. {
  118.    asm   {
  119.          push  ds
  120.          push  di
  121.  
  122.          /* calc start address in vido mem */
  123.          mov   ax, y
  124.          mov   bx, x
  125.          xchg  ah, al
  126.          add   bx, ax
  127.          shr   ax, 1
  128.          shr   ax, 1
  129.          add   bx, ax
  130.  
  131.          /* set up address registers for movsw */
  132.          mov   ax, 0xa000
  133.          mov   es, ax
  134.          mov   di, bx
  135.          lds   si, buffer
  136.  
  137.          /* set up width and adjustment for fast blat */
  138.          mov   dx, width
  139.          shr   dx, 1
  140.          mov   bx, 320
  141.          sub   bx, width
  142.          mov   ax, height
  143.          }
  144.  
  145.          /* blast each line */
  146. l1:
  147.    asm   {
  148.          mov   cx, dx
  149.          rep   movsw
  150.          add   di, bx
  151.          dec   ax
  152.          jnz   l1
  153.  
  154.          /* all done */
  155.          pop   di
  156.          pop   ds
  157.          }   
  158. }
  159.  
  160.  
  161.  
  162. /*
  163.    draws to any buffer that is 320x200 in size
  164.  
  165.    x, y, width, height all in pixels, mode 0x13 ONLY
  166.  
  167.    MAX width and height is 255!!!!!
  168.  
  169.    buffer pointer to a width by height array of bytes that are a bitmap
  170.  
  171.    x, y is position to display at, NO checking done for valid
  172.    co-ords etc...
  173.  
  174.    WIDTH MUST BE EVEN!!!!
  175.  
  176. */
  177. /* ---------------------- put_blit2() --------------------- March 23,1993 */
  178. void put_blit2(FARPTR buffer, short x, short y,
  179.               short width, short height,
  180.               FARPTR dest)
  181. {
  182.    asm   {
  183.          push  ds
  184.          push  di
  185.  
  186.          /* calc start address in vido mem */
  187.          mov   ax, y
  188.          mov   bx, x
  189.          xchg  ah, al
  190.          add   bx, ax
  191.          shr   ax, 1
  192.          shr   ax, 1
  193.          add   bx, ax
  194.  
  195.          /* set up address registers for movsw */
  196.          les   di, dest  // es:di points to start dest
  197.          add   di, bx    // make es:di point to x,y in dest
  198.  
  199.          lds   si, buffer
  200.  
  201.          /* set up width and adjustment for fast blat */
  202.          mov   dx, width
  203.          shr   dx, 1
  204.          mov   bx, 320
  205.          sub   bx, width
  206.          mov   ax, height
  207.          }
  208.  
  209.          /* blast each line */
  210. l1:
  211.    asm   {
  212.          mov   cx, dx
  213.          rep   movsw
  214.          add   di, bx
  215.          dec   ax
  216.          jnz   l1
  217.  
  218.          /* all done */
  219.          pop   di
  220.          pop   ds
  221.          }   
  222. }
  223.  
  224.  
  225. /* wait for vertical sync */
  226. /* ---------------------- wait_vert() -------------------- March 27,1993 */
  227. void wait_vert(void)
  228. {
  229.    asm   {
  230.          mov     dx,INPUT_STATUS_1
  231.          }
  232. WaitVS:
  233.    asm   {
  234.          in      al,dx
  235.          test    al,08h
  236.          jz      WaitVS  /* vertical sync is active high (1 = active) */
  237.          }
  238. }
  239.  
  240.  
  241. /*
  242.  
  243.     pcx.c
  244.  
  245.     Internet: alexad3@icebox.iceonline.com
  246.     Copyright 1995, April 6 by Alec Russell, ALL rights reserved
  247.     Permission granted to use this code as anyone wishes.
  248.  
  249.     Created - 1995/4/6
  250.  
  251.     History:
  252.         New file
  253.  
  254.     Originally programmed for medium mem model, thus the liberal
  255.     lacing of FAR * throughout the code.
  256.  
  257. */
  258.  
  259. #include <stdio.h>
  260. #include <string.h>
  261.  
  262. #include <xfileio.h>
  263. #include <gmalloc.h>
  264.  
  265.  
  266. // for all pcx files (far as I know)
  267. typedef struct
  268.    {
  269.    char manu; /// usually 10
  270.    char version;
  271.    char encoding;
  272.    char bits_per_pixel;
  273.    int xmin, ymin, xmax, ymax;
  274.    int hres, yres;
  275.    char palette[48];  // not used by 256 color pcx
  276.    char reserved;
  277.    char color_planes;
  278.    int bytes_per_line;
  279.    int palette_type;
  280.    char spare[58];
  281.    }
  282. pcx_head_t;
  283.  
  284.  
  285. #define PAL_SIZE 768
  286. #define PCX_HD_SIZE 128
  287.  
  288. /* for 256 color PC images ONLY, works with dpaint images
  289.    Make sure b is big enough for unpacked image.
  290.    Don't use this to get ideas for compression as
  291.    there are much better ways to do even simple RLE encoding.
  292. */
  293. /* ---------------------- unpack_pcx() ------------------ January 4,1995 */
  294. int unpack_pcx(char *fname, unsigned char far *b, unsigned char *pal,
  295.                int *p_width, int *p_height)
  296. {
  297.    int err=0, i;
  298.    unsigned int size;
  299.    pcx_head_t pcx;
  300.    unsigned char far *t1, far *raw;
  301.  
  302.    raw=far_load(fname);  // load the whole file into mem,
  303.                          // an easy function to write
  304.  
  305.    size=load_size;       // load_size is a global set by far_load()
  306.                          // if not obvious load_size is file size
  307.    if ( raw )
  308.       {
  309.       if ( size > PCX_HD_SIZE )
  310.          {
  311.          _fmemcpy(&pcx, raw, PCX_HD_SIZE);  // far memcpy
  312. #if 0
  313. // debug stuff
  314. pr2("manu %d", pcx.manu);
  315. pr2("ver  %d", pcx.version);
  316. pr2("enc  %d", pcx.encoding);
  317. pr2("bits %d", pcx.bits_per_pixel);
  318. pr2("xmin %d ymin %d xmax %d ymax %d",
  319.     pcx.xmin, pcx.ymin, pcx.xmax, pcx.ymax);
  320. pr2("hres %d yres %d", pcx.hres, pcx.yres);
  321. pr2("rsv %d", pcx.reserved);
  322. pr2("planes %d", pcx.color_planes);
  323. pr2("bytes/line %d", pcx.bytes_per_line);
  324. pr2("pal type %d", pcx.palette_type);
  325. #endif
  326.          // get width and height
  327.          *p_width =pcx.xmax - pcx.xmin + 1;
  328.          *p_height=pcx.ymax - pcx.ymin + 1;
  329.          // get palette
  330.          // the palette is just tacked onto the end of the file
  331.          _fmemcpy(pal, raw+size-PAL_SIZE, PAL_SIZE);
  332.          for ( i=0, t1=pal; i < PAL_SIZE; i++, t1++ )
  333.             *t1>>=2;
  334.  
  335.          // this type of de-compression is common to most pcx files
  336.  
  337.          t1=raw+PCX_HD_SIZE; // skip header
  338.          // decompress raw into b
  339.          size-=PCX_HD_SIZE; // skip header
  340.          size-=PAL_SIZE; // don't include palette at end
  341.          while ( size )
  342.             {
  343.             if ( (*t1 & 0xc0) == 0xc0 ) // are the two high bits set?
  344.                {
  345.                // hi bits set, its a run
  346.                i=*t1 & 0x3f;    // mask with 00111111 to get size of run
  347.                t1++;            // next byte is color to repeat
  348.                size--;
  349.                while ( i-- )
  350.                   *b++=*t1;
  351.                t1++;
  352.                size--;
  353.                }
  354.             else
  355.                {
  356.                *b++=*t1++;  // not a run, copy one byte
  357.                size--;
  358.                }
  359.             }
  360.  
  361.          gfree(raw, "raw");  // like a free()
  362.          }
  363.       else
  364.          err=2;
  365.       }
  366.    else
  367.       err=1;
  368.  
  369.    return(err);
  370. }
  371.  
  372.  
  373. /* ------------------------------ end of file ------------------------- */
  374.