OgreVorbis

Watcom DOS Sprite Test mode 12h

Oct 7th, 2021
1,054
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <i86.h>
  4. #include <graph.h>
  5. #include <malloc.h>
  6.  
  7. /*
  8. This shows how I'm drawing sprites in Open Watcom C for DOS. It is just a testing program. Attempting to force page flipping in mode 12h.
  9. It creates a mask for the sprite to make transparency work. It currently has to grab the sprite off the screen cause of watcom
  10. limitations. I will later make a program to convert bitmap to the native watcom _putimage format so that I don't need to draw to the
  11. screen first like in QBASIC. For now just experimenting with how to make the page flipping work. Once I figure this, it will be ready
  12. for a game engine.
  13. */
  14.  
  15. #define SCREEN_WIDTH_IN_BYTES 80
  16. #define PAGE_0_START 0
  17. #define PAGE_1_START (480 * SCREEN_WIDTH_IN_BYTES)
  18.  
  19. typedef unsigned char  byte;
  20. typedef unsigned short word;
  21. word *my_clock=(word *)0x0000046C;
  22.  
  23. const int CAPPY_WIDTH = 20;
  24. const int CAPPY_HEIGHT = 20;
  25. const unsigned char cappy[] = {
  26.     14,14,00,00,00,00,00,12,12,12,12,12,00,00,00,00,00,00,14,14,
  27.     14,00,00,00,00,00,12,12,12,12,12,12,12,00,00,00,00,00,00,14,
  28.     00,00,00,00,00,12,12,04,04,04,04,04,12,12,00,00,00,00,00,00,
  29.     00,00,00,00,12,12,15,15,15,04,15,15,15,12,12,00,00,00,00,00,
  30.     00,00,00,12,12,04,15,00,15,04,15,00,15,04,12,12,00,00,00,00,
  31.     00,15,12,12,04,04,15,15,15,12,15,15,15,04,04,12,12,15,00,00,
  32.     00,15,12,12,04,04,04,04,04,12,04,04,04,04,04,12,12,15,00,00,
  33.     00,15,12,12,04,04,00,04,04,04,04,04,00,04,04,12,12,15,00,00,
  34.     00,00,00,12,12,04,04,00,15,00,15,00,04,04,12,12,00,00,00,00,
  35.     00,00,00,00,12,12,04,04,00,00,00,04,04,12,12,02,00,00,00,00,
  36.     00,00,00,00,02,12,12,04,04,04,04,04,12,12,02,02,02,00,00,00,
  37.     00,00,00,02,02,02,12,12,12,12,12,12,12,02,02,02,02,00,00,00,
  38.     00,00,02,02,02,02,00,12,12,12,12,12,00,00,02,02,02,00,00,00,
  39.     00,01,01,01,01,01,00,00,00,00,00,00,00,00,02,02,02,00,00,00,
  40.     00,01,01,01,01,01,00,00,00,00,00,00,00,00,02,02,02,00,00,00,
  41.     00,00,00,00,00,00,00,00,00,00,00,00,00,00,02,02,02,00,00,00,
  42.     00,00,00,00,00,00,00,00,00,00,00,00,00,00,02,02,02,00,00,00,
  43.     00,00,00,00,00,00,00,00,00,00,00,00,00,01,01,01,01,01,00,00,
  44.     14,00,00,00,00,00,00,00,00,00,00,00,00,01,01,01,01,01,00,14,
  45.     14,14,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,14,14
  46. };
  47.  
  48. void draw_mask()
  49. {
  50.     int x,y;
  51.     int c = 0;
  52.     for (y = 0; y < CAPPY_HEIGHT; y++)
  53.     {
  54.         for (x = 0; x < CAPPY_WIDTH; x++)
  55.         {
  56.             if (cappy[c] == 0)
  57.                 _setcolor(15);
  58.             else
  59.                 _setcolor(0);
  60.             _setpixel(x, y);
  61.             c++;
  62.         }
  63.     }
  64. }
  65.  
  66. void draw_sprite()
  67. {
  68.     int x,y;
  69.     int c = 0;
  70.     for (y = 0; y < CAPPY_HEIGHT; y++)
  71.     {
  72.         for (x = 0; x < CAPPY_WIDTH; x++)
  73.         {
  74.             _setcolor(cappy[c]);
  75.             _setpixel(x, y);
  76.             //putpixel(x + xpos, y + ypos, cappy[c]);
  77.             c++;
  78.         }
  79.     }
  80. }
  81.  
  82. void main()
  83. {
  84.     int x,y,c;
  85.     int randX,randY;
  86.     word i;
  87.     char *cappyBuf;
  88.     char *cappyMask;
  89.     FILE *fp;
  90.  
  91.     long sizeOBuf;
  92.  
  93.     srand(*my_clock);
  94.     _setvideomode(_VRES16COLOR);
  95.  
  96.     /* Flip to page 1 */
  97.     outpw(0x3D4, 0x0C | (((PAGE_1_START * 2) >> 8) << 8));
  98.  
  99.     sizeOBuf = _imagesize(0, 0, 19, 19);
  100.     cappyMask = (char*) malloc(sizeOBuf);
  101.     _getimage(0, 0, 19, 19, cappyMask);
  102.  
  103.     /* We save the empty sprite (before drawing to it) to a file for analysis of the format */
  104.     fp = fopen("empty.spr", "w");
  105.     if (fp != NULL)
  106.     {
  107.         for (i = 0; i < sizeOBuf; i++)
  108.             fputc(cappyMask[i], fp);
  109.         fclose(fp);
  110.     }
  111.  
  112.     /* We try to draw our mask on the offscreen page (not exactly right) */
  113.     draw_mask();
  114.     _getimage(0, 0, 19, 19, cappyMask);
  115.  
  116.     /* Save the actual mask for analysis */
  117.     fp = fopen("mask.spr", "w");
  118.     if (fp != NULL)
  119.     {
  120.         for (i = 0; i < sizeOBuf; i++)
  121.             fputc(cappyMask[i], fp);
  122.         fclose(fp);
  123.     }
  124.  
  125.     getch();
  126.    
  127.     draw_sprite();
  128.  
  129.     getch();
  130.  
  131.     /* Grab our real sprite from the screen */
  132.     sizeOBuf = _imagesize(0, 0, 20, 20);
  133.     cappyBuf = (char*) malloc(sizeOBuf);
  134.     _getimage(0, 0, 20, 20, cappyBuf);
  135.  
  136.     /* Save for analysis */
  137.     fp = fopen("cappy.spr", "w");
  138.     if (fp != NULL)
  139.     {
  140.         for (i = 0; i < sizeOBuf; i++)
  141.             fputc(cappyBuf[i], fp);
  142.         fclose(fp);
  143.     }
  144.  
  145.     /* Flip to page 0
  146.     LOOK THOSE SPRITES WE DREW ARE NOW AT 0,0 */
  147.     outpw(0x3D4, 0x0C | ((PAGE_0_START >> 8) << 8));
  148.    
  149.     /* Draw a few sprites back on the main page again just to see performance
  150.     eventually this should happen on the second page to test the flip */
  151.     for (i = 0; i < 5; i++)
  152.     {
  153.         randX = rand() % 640;
  154.         randY = rand() % 480;
  155.         _putimage(randX, randY, cappyMask, _GAND);
  156.         _putimage(randX, randY, cappyBuf, _GOR);
  157.     }
  158.  
  159.     getch();
  160.    
  161.     /* Even more sprites - note the transparency */
  162.     for (i = 0; i < 50000; i++)
  163.     {
  164.         randX = rand() % 640;
  165.         randY = rand() % 480;
  166.         _putimage(randX, randY, cappyMask, _GAND);
  167.         _putimage(randX, randY, cappyBuf, _GOR);
  168.     }
  169.     getch();
  170. }
RAW Paste Data