home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / divesrc.zip / main.cpp < prev   
C/C++ Source or Header  |  1996-02-22  |  7KB  |  205 lines

  1. //*****************************************************************************
  2. //* Demonstration of DIVE                         - February 1996 - D.A.Braun *
  3. //*****************************************************************************
  4.  
  5. //**************************** General Includes *******************************
  6. #define INCL_WIN    // Include windowing and queue functions
  7. #define INCL_DOS    // Include "DOS" functions - for threads
  8.  
  9. #include <os2.h>    // Include for OS/2 functions
  10. #include "diveobj.h"    // Include for DIVE Class
  11.  
  12. //************************* GLOBAL VARIABLES **********************************
  13. HWND    ghMainWinFrame,ghMainWinClient;    // Window handles for the app. window
  14.  
  15. TID    gtDrawThread;            // Thread handle for Draw Thread
  16. HMQ    gqDrawQueue;            // Queue for the Draw Thread
  17. INT    gDrawReady;            // Flag to verify setup of Draw Thread
  18.  
  19. //************************* DRAW THREAD ***************************************
  20. // Message Command Definitions
  21. #define UM_EXIT        WM_USER
  22. #define UM_PAINT    WM_USER + 1
  23. #define UM_VRNDIS    WM_USER + 10
  24. #define UM_VRNENA    WM_USER + 11
  25. #define UM_RELPAL    WM_USER + 12
  26.  
  27. void APIENTRY fDrawProc (char *ImageFileName)
  28. {
  29. HAB        hab;
  30. QMSG        Message;
  31. DIVEOBJ        *MyDiveObject;
  32.  
  33. hab = WinInitialize( 0 );            // Initialize this thread to PM
  34. gqDrawQueue = WinCreateMsgQueue( hab, 0 );    // Get a message queue
  35. WinCancelShutdown( gqDrawQueue, TRUE );        // Cancel WM_QUIT message
  36.  
  37. gDrawReady = TRUE;                // Ready to receive messages
  38.  
  39. MyDiveObject = new DIVEOBJ (ImageFileName);
  40. MyDiveObject->Location (0,0,1,1);
  41.  
  42. // Message Loop begins here
  43. do    {
  44.     WinGetMsg( hab, &Message, 0L, 0L, 0L );
  45.  
  46.     switch( Message.msg )
  47.         {
  48.         case UM_VRNDIS:
  49.             MyDiveObject->VRN_Disable ();
  50.             break;
  51.         case UM_VRNENA:
  52.             MyDiveObject->VRN_Enable (ghMainWinClient,ghMainWinFrame);
  53.             break;
  54.         case UM_RELPAL:
  55.             MyDiveObject->Realize_Palette ();
  56.             break;
  57.         case UM_PAINT:
  58.             MyDiveObject->Blit ();
  59.             break;
  60.         }
  61.     } while( Message.msg != UM_EXIT );
  62.  
  63. delete (MyDiveObject);
  64.  
  65. WinDestroyMsgQueue(gqDrawQueue);    // Eliminate the message queue
  66. WinTerminate(hab);            // Proper exit of PM
  67. DosExit( EXIT_THREAD, 0L );            // Proper thread exit
  68. }
  69.  
  70.  
  71.  
  72. //**************************** Window Proc For MAIN Window ********************
  73. MRESULT EXPENTRY main_window (HWND handle,ULONG mess,MPARAM parm1,MPARAM parm2)
  74. {
  75. static HPS p_space;
  76.  
  77. switch (mess)
  78.     {
  79.     case WM_CREATE:
  80.         // Ensure that the DIVE blitter will be set up
  81.         WinPostMsg (handle,WM_VRNENABLED,0L,0L);
  82.         break;
  83.     case WM_PAINT:
  84.         // We must open a presentation space even though we aren't
  85.         // going to use it because otherwise OS/2 will complain
  86.         p_space=WinBeginPaint (handle, 0, 0);
  87.         WinEndPaint (p_space);
  88.         // Post a message to the draw thread to paint the DIVE region
  89.         WinPostQueueMsg( gqDrawQueue, UM_PAINT, 0L, 0L );
  90.         break;
  91.     case WM_VRNDISABLED:
  92.         // Turn the blitter off for now
  93.         WinPostQueueMsg( gqDrawQueue, UM_VRNDIS, 0L, 0L );
  94.         break;
  95.     case WM_VRNENABLED:
  96.         // Turn the blitter on now
  97.         WinPostQueueMsg( gqDrawQueue, UM_VRNENA, (MPARAM)handle, 0L );
  98.         break;
  99.     case WM_REALIZEPALETTE:
  100.         // The system's palette has changed, make the DIVE region
  101.         // adjust accordingly.
  102.         WinPostQueueMsg( gqDrawQueue, UM_RELPAL, 0L, 0L );
  103.         break;
  104.     case WM_CLOSE:
  105.         WinPostQueueMsg( gqDrawQueue, UM_VRNDIS, 0L, 0L );
  106.     default:
  107.         return WinDefWindowProc (handle,mess,parm1,parm2);
  108.     }
  109. return (MRESULT) FALSE;
  110. }
  111.  
  112. //****************************** MAIN FUNCTION ********************************
  113. void main (int argc, char * argv[])
  114. {
  115. QMSG    q_message;              // Use this to hold a message
  116. HAB    h_anchor;            // Anchor block for out program
  117. HMQ    h_mqueue;            // Main thread's message queue
  118. ULONG    nwin_flags;            // Holds the window flags
  119. unsigned char mclass[] = "DemoClass";    // Name of out window class
  120.  
  121. // Initialize or program into the window system
  122. if (!(h_anchor=WinInitialize (0)))
  123.     DosExit( EXIT_PROCESS, 0L );            // Proper process exit
  124.  
  125. // Get a message queue
  126. if (!(h_mqueue=WinCreateMsgQueue (h_anchor,0)))
  127.     {
  128.     WinTerminate (h_anchor);
  129.     DosExit( EXIT_PROCESS, 0L );            // Proper process exit
  130.     }
  131.  
  132. // See if the system can handle DIVE
  133. if (!QueryDive ())    // If it can't - show the error message...
  134.     {
  135.     WinMessageBox     (HWND_DESKTOP,HWND_DESKTOP,"ERROR: No DIVE Support",
  136.         "DIVE Example Error",0, MB_OK | MB_MOVEABLE | MB_ICONEXCLAMATION);
  137.     WinDestroyMsgQueue (h_mqueue);
  138.     WinTerminate (h_anchor);
  139.     DosExit( EXIT_PROCESS, 0L );            // Proper process exit
  140.     }
  141.  
  142. // Check if we received a filename parameter
  143. if (argc < 2)        // If we didn't - show the error message
  144.     {
  145.     WinMessageBox     (HWND_DESKTOP,HWND_DESKTOP,"ERROR: No Targa Specified",
  146.         "DIVE Example Error",0, MB_OK | MB_MOVEABLE | MB_ICONEXCLAMATION);
  147.     WinDestroyMsgQueue (h_mqueue);
  148.     WinTerminate (h_anchor);
  149.     DosExit( EXIT_PROCESS, 0L );            // Proper process exit
  150.     }
  151.  
  152. // Start up our draw thread - wait for queue to be ready
  153. gDrawReady = FALSE;
  154. DosCreateThread (>DrawThread, (PFNTHREAD) fDrawProc, (ULONG)argv[1],0,4096);
  155. while (!gDrawReady) DosSleep (10);
  156.  
  157. // Now Initialize a window class
  158. if (!WinRegisterClass (h_anchor,(PSZ) mclass,main_window,CS_SIZEREDRAW,0))
  159.     {
  160.     WinDestroyMsgQueue (h_mqueue);
  161.     WinTerminate (h_anchor);
  162.     DosExit( EXIT_PROCESS, 0L );            // Proper process exit
  163.     }
  164.  
  165. // Create the main application window
  166.  
  167. // Some setup information
  168. nwin_flags = FCF_STANDARD&(~FCF_ACCELTABLE)&(~FCF_SHELLPOSITION)
  169.         &(~FCF_MENU)&(~FCF_ICON);
  170. if (!(ghMainWinFrame = WinCreateStdWindow (HWND_DESKTOP,WS_VISIBLE,&nwin_flags,
  171.     (PSZ) mclass,(PSZ) "Dive Demo App - February 1996",    0L,0,0,&ghMainWinClient)))
  172.     {
  173.     WinDestroyMsgQueue (h_mqueue);
  174.     WinTerminate (h_anchor);
  175.     DosExit( EXIT_PROCESS, 0L );            // Proper process exit
  176.     }
  177.  
  178. // Move and resize the application window (and raise to top)
  179. WinSetWindowPos (ghMainWinFrame, HWND_TOP, 10, 10, 600, 450,
  180.     SWP_SIZE | SWP_MOVE | SWP_SHOW | SWP_ACTIVATE );
  181.  
  182. // Enable visible region notification - Necessary for DIVE - this
  183. // tells OS/2 to send the window the VRN_ENABLED and VRN_DISABLED messages
  184. WinSetVisibleRegionNotify (ghMainWinFrame,TRUE);
  185.  
  186. //  Message loop - the program runs here
  187. while (WinGetMsg (h_anchor,&q_message,0L,0,0))
  188.     WinDispatchMsg (h_anchor, &q_message);
  189.  
  190. // Stop the drawing thread
  191. WinPostQueueMsg( gqDrawQueue, UM_EXIT, (MPARAM)FALSE, (MPARAM)0L );
  192. DosWaitThread (>DrawThread,(ULONG)0);
  193.  
  194. // Disable visible region notification
  195. WinSetVisibleRegionNotify (ghMainWinFrame,FALSE);
  196.  
  197. // The user has requested closing the program - now shutdown
  198. WinDestroyWindow (ghMainWinFrame);        // Destroy our window
  199. WinDestroyMsgQueue (h_mqueue);            // Donw w/message queue
  200. WinTerminate (h_anchor);                // Done w/window system
  201. DosExit( EXIT_PROCESS, 0L );            // Proper process exit
  202. }
  203.  
  204. //********************************* END ***************************************
  205.