home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mnth0109.zip / Timur / bitmap.c next >
Text File  |  1993-06-07  |  3KB  |  101 lines

  1. /* BITMAP.C - Global Bitmap resource manager
  2.  
  3. Copyright (c) 1992-1993 Timur Tabi
  4. Copyright (c) 1992-1993 Fasa Corporation
  5.  
  6. The following trademarks are the property of Fasa Corporation:
  7. BattleTech, CityTech, AeroTech, MechWarrior, BattleMech, and 'Mech.
  8. The use of these trademarks should not be construed as a challenge to these marks.
  9.  
  10. */
  11.  
  12. #define INCL_DEV
  13. #define INCL_GPICONTROL
  14. #define INCL_GPIBITMAPS
  15. #define INCL_WINWINDOWMGR
  16. #define INCL_WINDIALOGS
  17. #include <os2.h>
  18. #include <stdio.h>
  19.  
  20. #include "header.h"
  21. #include "window.h"
  22. #include "errors.h"
  23.  
  24. static HDC hdcMemory=NULLHANDLE;
  25. static HPS hpsMemory=NULLHANDLE;
  26. static HBITMAP hbmCurrent=NULLHANDLE;   // The current bitmap selected.  Used to avoid GpiSetBitmap() errors
  27.  
  28. ERROR BitmapShutdown(void) {
  29. /* Releases the resouces allocated by BitmapLoad() during initialization.  OS/2 automatically deletes
  30.    all bitmaps when the process termintes.  Therefore, after BitmapShutdown() is called, you can only
  31.    call BitmapLoad() if you delete all bitmaps manually.
  32.    This function may be called even if this module has never been initialized.
  33. */
  34. // First check if the module was initialized in the first place
  35.   if (hdcMemory==NULLHANDLE) return ERR_NOERROR;
  36.  
  37.   if (!GpiDestroyPS(hpsMemory)) return ERR_BITMAP_SHUT_PS;
  38.   if (DevCloseDC(hdcMemory) != DEV_OK) return ERR_BITMAP_SHUT_DC;
  39.  
  40.   hdcMemory=NULLHANDLE;
  41.   hpsMemory=NULLHANDLE;
  42.   hbmCurrent=NULLHANDLE;
  43.   return ERR_NOERROR;
  44. }
  45.  
  46. ERROR BitmapLoad(ULONG ulBitmapID, PHBITMAP phbm) {
  47. /* Loads a bitmap into the Bitmap Memory PS.  Also initializes this module when first called.
  48. */
  49.   SIZEL sizel={0,0};
  50.   HBITMAP hbm;
  51.  
  52. // Is this the first call to BitmapLoad() ?
  53.   if (hdcMemory==NULLHANDLE) {
  54.     hdcMemory=DevOpenDC(hab,OD_MEMORY,"*",0,NULL,NULLHANDLE);
  55.     if (hdcMemory==NULLHANDLE) return ERR_BITMAP_LOAD_DC;
  56.  
  57.     hpsMemory=GpiCreatePS(hab,hdcMemory,&sizel,PU_PELS | GPIF_DEFAULT | GPIT_MICRO | GPIA_ASSOC);
  58.     if (hpsMemory==NULLHANDLE) {
  59.       DevCloseDC(hdcMemory);
  60.       hdcMemory=NULLHANDLE;
  61.       return ERR_BITMAP_LOAD_PS;
  62.     }
  63.     hbmCurrent=NULLHANDLE;
  64.   }
  65.  
  66.   hbm=GpiLoadBitmap(hpsMemory,NULLHANDLE,ulBitmapID,0,0);
  67.   if (hbm==NULLHANDLE) return ERR_BITMAP_LOAD_HBM;
  68.  
  69.   *phbm=hbm;
  70.   return ERR_NOERROR;
  71. }
  72.  
  73. void BitmapDraw(HBITMAP hbm, HBITMAP hbmMask, POINTL ptl) {
  74.   POINTL aptl[3];
  75.   HPS hps=WinGetPS(hwndClient);
  76.  
  77.   aptl[0]=ptl;
  78.   aptl[1].x=aptl[0].x+29;
  79.   aptl[1].y=aptl[0].y+25;
  80.   aptl[2].x=0;
  81.   aptl[2].y=0;
  82.  
  83. // First, mask out the background if requested
  84.   if (hbmMask != NULLHANDLE) {
  85.     if (hbmCurrent != hbmMask) {
  86.       GpiSetBitmap(hpsMemory,hbmMask);
  87.       hbmCurrent=hbmMask;
  88.       }
  89.  
  90.     GpiBitBlt(hps,hpsMemory,3L,aptl,ROP_SRCAND,0);
  91.   }
  92.  
  93. // Now, draw the bitmap
  94.   if (hbmCurrent != hbm) {
  95.     GpiSetBitmap(hpsMemory,hbm);
  96.     hbmCurrent=hbm;
  97.   }
  98.   GpiBitBlt(hps,hpsMemory,3L,aptl,ROP_SRCPAINT,0);
  99.   WinReleasePS(hps);
  100. }
  101.