home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dlib06.zip / DLIB / DIMAGE.C < prev    next >
Text File  |  1994-07-09  |  5KB  |  204 lines

  1. #define INCL_DEV
  2. #define INCL_DOS
  3. #define INCL_WIN
  4. #define INCL_32
  5. #include <os2.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include "dlib.h"
  10. #include "portio.h"
  11. #define sign(x) ((x)>0 ? 1:-1)
  12.  
  13.  
  14. extern PCHAR VScreen;
  15. extern char _TranslateRGB[256];
  16. extern ULONG VScreenX, VScreenY, VScreenSize;  
  17.  
  18.  
  19.  
  20. PIMAGE __syscall dGetImage(unsigned int x, unsigned int y, unsigned int xsize, unsigned int ysize)
  21. /* Creates and image from an area of the virtual screen */
  22. {
  23.     int size, xpos, ypos, i = 0;
  24.     PIMAGE tmp;
  25.     PCHAR loc = VScreen + (y * VScreenX) + x;
  26.  
  27.     size = xsize * ysize;
  28.     tmp = malloc(sizeof(IMAGE)); 
  29.     tmp->image = malloc(size);
  30.     tmp->xs = xsize;
  31.     tmp->ys = ysize;
  32.     for (ypos=0; ypos < ysize; ypos++) {
  33.        for (xpos=0; xpos < xsize; xpos++) {
  34.            tmp->image[i++] = *loc;
  35.            loc++;
  36.        } 
  37.        loc += VScreenX;
  38.     }
  39.     return tmp;
  40. }
  41.  
  42.  
  43.  
  44. void __syscall dBltImage(unsigned int x, unsigned int y, PIMAGE image)
  45. /* Draw the picture defined by 'image' at (x,y) */
  46. {
  47.     PCHAR loc = VScreen + (y * VScreenX) + x;
  48.     ULONG size = image->xs, i, src = 0;
  49.  
  50.     for (i=0; i<(image->ys); i++) {
  51.        memcpy(loc, &image->image[src], size);
  52.        loc += VScreenX;
  53.        src += size;
  54.     }
  55. }
  56.  
  57.  
  58.  
  59.  
  60. /*********************************** sprite execution */
  61. unsigned int _where, _code;
  62. extern void ExecuteSprite(void);
  63. #pragma aux ExecuteSprite = \
  64.      "mov eax, _code"       \
  65.      "mov edx, _where"      \
  66.      "call eax"             \
  67.      modify [eax edx];
  68.  
  69.  
  70. void __syscall dBltSprite(unsigned int x, unsigned int y, PIMAGE image)
  71. /* Draw the picture defined by 'image' at (x,y). This picture 
  72.    shows all color but zero. Zero's appear as transparent bits*/
  73. {
  74.    unsigned int loc=(unsigned long)VScreen + ((y*VScreenX) + x);
  75.    _code = (unsigned)image->code;
  76.    _where = loc;
  77.    ExecuteSprite();
  78. }
  79. /*********************************** sprite execution */
  80.  
  81.  
  82.  
  83. void __syscall dCompileSprite(PIMAGE image)
  84. /* Generates executable code to draw an image. This is a very good way
  85.    to do things. This means no 'cmp' instructions are executed while
  86.    drawing. Very fast. ie Each sprite knows the best way to draw itself */
  87. {
  88.    int ip=0,x,y,d=0,adder = 0;
  89.    unsigned char tmp;
  90.  
  91.    image->code = malloc((image->xs * image->ys) *7);
  92.    for (y=0; y<(image->ys); y++) {
  93.       for (x=0; x<(image->xs); x++) {
  94.          tmp = image->image[d++];
  95.          if (tmp) {
  96.             /* mov byte ptr [adder+edx],tmp */
  97.             image->code[ip++] = 0xc6;
  98.             image->code[ip++] = 0x82;
  99.             image->code[ip++] = (adder & 0xff);
  100.             image->code[ip++] = ((adder >> 8) & 0xff);
  101.             image->code[ip++] = ((adder >> 16) & 0xff);
  102.             image->code[ip++] = ((adder >> 24) & 0xff);
  103.             image->code[ip++] = tmp;
  104.          }
  105.          adder++;
  106.       }
  107.       adder+=(VScreenX - image->xs);
  108.    }
  109.    image->code[ip++] = 0xc3; /* ret*/
  110. }
  111.  
  112.  
  113.  
  114.  
  115. void Stretch(long x1, long x2, long y1, long y2, long yr, long yw, PIMAGE src)
  116. /* used internaly by dBltStretchImage */
  117. {
  118.     long dx,dy,d,dx2;
  119.     register e,sloc,dloc;
  120.     ULONG sx,sy,color;
  121.  
  122.     sx = sign(x2-x1);
  123.     sy = sign(y2-y1);
  124.     dx = abs(x2-x1);
  125.     dy = abs(y2-y1);
  126.     e = (dy<<1)-dx;
  127.     dloc = yw * VScreenX;
  128.     sloc = yr * src->xs;
  129.     dx2 = dx<<1;
  130.     dy <<= 1;
  131.  
  132.     for(d=0; d<=dx; d++) {
  133.         VScreen[x1 + dloc] = src->image[y1 + sloc];
  134.         while (e >= 0) {
  135.             y1 += sy;
  136.             e -= dx2;
  137.         }
  138.         x1 += sx;
  139.         e += dy;
  140.     }
  141. }
  142.  
  143.  
  144. void __syscall dBltStretchImage(PIMAGE src, ULONG xd1, ULONG yd1, ULONG xd2, ULONG yd2)
  145. /* This stretches an image to fill the rectangle specified on the
  146.    virtual screen. (xd1,yd) being the top left, (xd2,yd2) being bottom
  147.    right. Even though this is a well optomised stretch, its not the sort
  148.    of call you want to make often. In my tests is much faster than
  149.    similar OS/2 Gpi calls. Useful for stretching a picture over the
  150.    entire background. */
  151. {
  152.     long dx, dy, e, d, dx2, xs1 = 0, ys1 = 0, xs2, ys2;
  153.     int sx,sy;
  154.  
  155.     xs1 = 0;
  156.     ys1 = 0;
  157.     xs2 = src->xs;
  158.     ys2 = (src->ys-1);
  159.     sx = sign(yd2 - yd1);
  160.     sy = sign(ys2 - ys1);
  161.     dx = abs(yd2 - yd1);
  162.     dy = abs(ys2 - ys1);
  163.     e = (dy<<1) - dx;
  164.     dx2 = dx<<1;
  165.     dy <<= 1;
  166.  
  167.     for(d=0; d<=dx; d++){
  168.         Stretch(xd1, xd2, xs1, xs2, ys1, yd1, src);     
  169.         while (e >= 0) {
  170.             ys1 += sy;
  171.             e -= dx2;
  172.         }
  173.         yd1 += sx;
  174.         e += dy;
  175.     }
  176. }
  177.  
  178.  
  179. PIMAGE __syscall dLoadImage( char *filename )
  180. /* Loads an image from disk. The file has 2 shorts at the beginning.
  181.    These are the Xsize and Ysize. The rest of the file should be
  182.    Xsize * Ysize of color bytes. */
  183. {
  184.    FILE *img;
  185.    PIMAGE tmp;
  186.    int i;
  187.  
  188.    img = fopen(filename,"rb");
  189.    if (img == NULL)
  190.        return NULL;
  191.  
  192.    tmp = malloc(sizeof(IMAGE));
  193.    fread(&tmp->xs, sizeof(USHORT), 1, img);
  194.    fread(&tmp->ys, sizeof(USHORT), 1, img);
  195.    tmp->image = malloc(tmp->xs * tmp->ys);
  196.    fread(tmp->image, (tmp->xs * tmp->ys), sizeof(char), img);
  197.    for (i=0; i<(tmp->xs * tmp->ys); i++)
  198.         tmp->image[i] = _TranslateRGB[tmp->image[i]];
  199.    fclose( img );
  200.    return tmp;
  201. }
  202.  
  203.  
  204.