home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / wvis0626.zip / warpvision_20020626.zip / libDIVE.h < prev    next >
C/C++ Source or Header  |  2002-06-24  |  15KB  |  447 lines

  1. /*
  2.     OS/2 DIVE class library: public header file
  3.     Copyright (C) 1997 by Andrew Zabolotny <bit@eltech.ru>
  4.  
  5.     This library is free software; you can redistribute it and/or
  6.     modify it under the terms of the GNU Library General Public
  7.     License as published by the Free Software Foundation; either
  8.     version 2 of the License, or (at your option) any later version.
  9.  
  10.     This library is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.     Library General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU Library General Public
  16.     License along with this library; if not, write to the Free
  17.     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19.  
  20. #ifndef __LIBDIVE_H__
  21. #define __LIBDIVE_H__
  22.  
  23. //*** Definitions for Resource Compiler ****************************************
  24.  
  25. // DIVE Window resources ID
  26. #define idDive          32100
  27. #define idDiveMenu      32101
  28.  
  29. // Messages handled by DIVE window handler
  30. #define cmdIgnore       32767
  31. #define cmdAlign        32101
  32. #define cmdAlignTop     32102
  33. #define cmdAlignBottom  32103
  34. #define cmdAlignLeft    32104
  35. #define cmdAlignRight   32105
  36. #define cmdAlignCenter  32106
  37. #define cmdScale        32107
  38. #define cmdSnap1to1     32108
  39. #define cmdSnap2to1     32109
  40. #define cmdSnap3to1     32110
  41. #define cmdSnap4to1     32111
  42. #define cmdFullScreen   32112
  43. #define cmdToggleAspect 32113
  44. #define cmdPause        32114
  45. #define cmdToggleMouse  32115
  46. #define cmdClose        32116
  47.  
  48. #define cmdFullScreen2  32201
  49. #define cmdPause2       32202
  50. #define cmdSeek10sFwd   32203
  51. #define cmdSeek60sFwd   32204
  52. #define cmdSeek10mFwd   32205
  53. #define cmdSeek10sBack  32206
  54. #define cmdSeek60sBack  32207
  55. #define cmdSeek10mBack  32208
  56. #define cmdSnap         32209
  57. #define cmdGammInc      32210
  58. #define cmdGammaDec     32211
  59. #define cmdGamma        32214
  60.  
  61. #define NUM_EXCTLS      4
  62.  
  63. #ifndef RC_INVOKED
  64.  
  65. typedef unsigned char bool;
  66.  
  67. #include <stdlib.h>
  68. #define INCL_DOS
  69. #define INCL_WIN
  70. #define INCL_GPI
  71. #include <os2.h>
  72. #include <pmddi.h>
  73. #include <mmioos2.h>
  74. #include <dive.h>
  75. #include <fourcc.h>
  76. #include <sys/types.h>
  77.  
  78. //#include "def.h"
  79.  
  80. //*** Video mode structures and definitions *************************************
  81. #define vmfWindowed 0x00000001  // 1 - window; 0 - full-screen
  82. #define vmfNative   0x00000002  // 1 - native mode; 0 - emulated
  83. #define vmfRGBAmode 0x00000004  // 1 - OpenGL in RGBA mode, 0 - color index
  84.  
  85. #define vmfAlphaBuffer  0x00010000  // 1 - Alpha buffer supported
  86. #define vmfHardware2D   0x00020000  // 1 - Hardware-supported 2D acceleration
  87. #define vmfHardware3D   0x00040000  // 1 - Hardware-supported 3D acceleration
  88.  
  89. // Read-only vmfXXX flags
  90. #define vmfReadOnly      vmfWindowed + vmfNative
  91.  
  92. // Handy typedefs
  93. typedef unsigned int u_int;
  94. typedef unsigned char u_char;
  95.  
  96. struct FGVideoMode
  97. {
  98.   u_int Width, Height;                  // Width and Height in pixels
  99.   // Readonly if (Flags & vmfWindowed == 0)
  100.   u_int PixelFormat;                    // Pixel format - FOURCC_###
  101.   // Readonly
  102.   u_int Buffers;                        // Available videobuffers (double buffering
  103.                                         // available if >1)
  104.   long SrcWidth, SrcHeight;
  105.  
  106.   // Read/write
  107.   u_int Flags;                          // See vmfXXX constants above
  108.   // Flags & vmfReadOnly are readonly
  109.   u_int IndexBits;                      // Bits per pixel in color index mode (OpenGL
  110.                                         // only)
  111.   // Readonly if >0; otherwise read/write
  112.   u_int DepthBits;                      // Bits per element of depth buffer
  113.   // Readonly if >0; otherwise read/write
  114.   u_int StencilBits;                    // Bits per element of stencil buffer
  115.   // Readonly if >0; otherwise read/write
  116.   u_int AccumBits;                      // Bits per element of stencil buffer
  117.   // Readonly if >0; otherwise read/write
  118. };
  119.  
  120. // DIVE errors
  121. enum tDiveError
  122. {
  123.   derrOK,                               // All right
  124.   derrBadNBuffers,                      // Invalid number of buffers
  125.   derrBadWindow,                        // Cannot set some window parameters
  126.   derrCannotOpen,                       // Cannot open DIVE instance
  127.   derrAllocBuffer,                      // Cannot allocate DIVE buffer
  128.   derrCreateSem,                        // Cannot create event semaphore
  129.   derrPalette,                          // Cannot set up palette
  130.   derrDestroyed,                        // User dismissed the window
  131. };
  132.  
  133. #define DIVE_MAXBUFFERS 16      // Maximal number of backbuffers
  134. #define DIVE_NEXTBUFFER -1              // Choose next backbuffer
  135.  
  136. extern FGVideoMode *vmList;             // Pointer to an array of tVideoMode`s
  137. extern u_int vmCount;                   // Number of videomodes in vmList
  138. extern long DesktopW, DesktopH;         // Desktop width and height
  139.  
  140. class diveWindow;
  141.  
  142. typedef void (*tKeyboardHandler)(diveWindow*, void *param,
  143.         unsigned char ScanCode, unsigned char CharCode, bool Down,
  144.         unsigned char RepeatCount, int ShiftFlags);
  145.  
  146. typedef void (*tMouseHandler)(diveWindow*, HWND hWnd, void *param,
  147.         int Button, bool Down, int x, int y, int ShiftFlags);
  148.  
  149. typedef MPARAM (*tCmdHandler)(diveWindow*, void *param, HWND hWnd,
  150.         MPARAM mp1, MPARAM mp2, int Mode);  //Called twice for each command 0 - entry, 1 - exit.
  151.  
  152. typedef void (*tTerminateHandler)(diveWindow*, void *param);
  153. typedef void (*tFocusHandler)(diveWindow*, void *param, bool Enable);
  154.  
  155. // Mask for shift keys (used in mouse handler)
  156. #define KF_SHIFT 0x000000001
  157. #define KF_ALT   0x000000002
  158. #define KF_CTRL  0x000000004
  159.  
  160. /**
  161.  * DIVE window class
  162.  * This class implements everything needed to use a DIVE context.
  163.  * If you attach it to a frame window (by passing a handle of a
  164.  * frame window in constructor) it will replace system menu (or
  165.  * application menu if there is one) by a specialized one. If you
  166.  * pass a handle of a different-type window, DIVE context will simply
  167.  * attach to that window (@@ NOT TESTED! but should work). All window
  168.  * resises, moves etc are tracked so that DIVE buffer always covers
  169.  * entire frame window client area (or entire window area). You can
  170.  * set up some additional callbacks to process all input to the window
  171.  * such as mouse and keyboard.
  172.  */
  173. class diveWindow
  174. {
  175. private:
  176.   PFNWP OldClientWindowProc, OldFrameWindowProc;
  177.   HDIVE hDive;                          // DIVE handle
  178.   ULONG hBuffer[DIVE_MAXBUFFERS];       // Image buffers
  179.   FOURCC BufferF;                       // Image buffer format
  180.   long BufferW, BufferH;                // Image buffers parameters
  181.   long nBuffers;                        // Count of image buffers
  182.   long ActiveBuffer;                    // ID of active image buffer
  183.   long VisibleBuffer;                   // ID of visible image buffer
  184.   long ScreenW, ScreenH;                // Screen size
  185.   ULONG *CLUT;                          // The palette for 256-color images
  186.   HEV sRedrawComplete;                  // Redraw complete event semaphore
  187.   bool fAspect;                         // Maintain aspect ratio?
  188.   bool fPause;                          // Set to true will freeze drawing
  189.   bool fMouseVisible;                   // If false mouse pointer will be hidden
  190.   ULONG MouseCursorID;          // Mouse cursor shape ID (SPTR_ARROW ...)
  191.   bool fMinimized;                      // This is true when window is minimized
  192.   bool fFullScreen;                     // true if we`re in full-screen
  193.   bool fPhysCLUT;                       // true if we changed physical palette
  194.   bool fActive;                         // Window is active
  195.   long FailedCount;                     // Count failed attempts to redraw between
  196.                                         // WM_VRNDIS/ENABLED
  197.   SWP swpFullScreen;                    // Window position before full-screen
  198.   long WindowW;                         // Client window width
  199.   long WindowH;                         // Client window height
  200.   RECTL DirtyRect;                      // Rectangle that should be updated
  201.   RECTL oldDirtyRect;                   // Dirty rectangle that is already set up
  202.   ULONG MouseButtonMask;        // Current mouse button states
  203.   bool MouseCaptured;           // Mouse captured flag
  204.  
  205.   RECTL RealRect;
  206.   long FrameW;
  207.   long FrameH;
  208.   long SourceWidth;
  209.   long SourceHeight;
  210.  
  211.   tKeyboardHandler hKeyboard;           // Keyboard handler if not NULL
  212.   void *paramKeyboard;                  // Parameter passed to keyboard handler
  213.   tTerminateHandler hTerminate;         // Called on window close if not NULL
  214.   void *paramTerminate;                 // Parameter passed to terminate handler
  215.   tFocusHandler hFocus;                 // Called on focus change if not NULL
  216.   void *paramFocus;                     // Parameter passed to focus handler
  217.   tMouseHandler hMouse;                 // Called on mouse events if not NULL
  218.   void *paramMouse;                     // Parameter passed to mouse handler
  219.   tCmdHandler hCmd;                     // Called on command events if not NULL
  220.   void *paramCmd;                       // Parameter passed to command handler
  221.  
  222.   short lastKeyCode [128];       // Last scan->character encountered
  223.   HWND hCtl[NUM_EXCTLS];
  224.  
  225. public:
  226.   HWND diveFR, diveCL, diveMN;          // Window frame, client and menu handles
  227.   ULONG FrameCount;                     // Incremented on each complete redraw
  228.   tDiveError lastError;                 // Last error condition
  229.  
  230.   /// Constructor: attach DIVE view to client of given frame handle
  231.   diveWindow (long Width, long Height, FOURCC Format, long SrcWidth, long SrcHeight, long nBuff);
  232.  
  233.   /// Destructor: free DIVE buffers and memory
  234.   virtual ~diveWindow ();
  235.  
  236.   /// Bind DIVE buffer to a PM window, either client or frame window
  237.   bool Bind (HWND winHandle);
  238.  
  239.   /// Unbind from window; optionally destroy window
  240.   bool Unbind (bool Destroy);
  241.  
  242.   /// Set *logical* color lookup table (AKA palette)
  243.   bool SetCLUT (ULONG * NewCLUT, int Count);
  244.  
  245.   /// Set physical CLUT to logical CLUT
  246.   bool SetPhysCLUT ();
  247.  
  248.   /// Restore default desktop palette
  249.   bool ResetPhysCLUT ();
  250.  
  251.   /// Show the window
  252.   bool Show (bool Visible);
  253.  
  254.   /// Disable standard accelerator table for window so we can use ALT, ALT+F4 etc
  255.   bool DisableAccelTable ();
  256.  
  257.   /// Two virtual functions that performs PM message management for frame and client
  258.   virtual MRESULT ClientMessage (ULONG Message, MPARAM MsgParm1, MPARAM MsgParm2);
  259.   virtual MRESULT FrameMessage (ULONG Message, MPARAM MsgParm1, MPARAM MsgParm2);
  260.  
  261.   /// Call this to get the address of the buffer to paint in
  262.   u_char *BeginPaint (ULONG * BytesPerLine, long BufferNo);
  263.  
  264.   /// Call this when you`re done with painting
  265.   void EndPaint ();
  266.  
  267.   /// Enable or disable constant aspect ratio of window
  268.   inline void MaintainAspectRatio (bool State)
  269.   {
  270.     if (!fFullScreen)
  271.     {
  272.       fAspect = State;
  273.       WinCheckMenuItem (diveMN, cmdToggleAspect, fAspect);
  274.     }
  275.   }
  276.  
  277.   /// Pause window: application will skip ticks in BeginPaint ()
  278.   inline void Pause (bool State)
  279.   {
  280.     fPause = State;
  281.     WinCheckMenuItem (diveMN, cmdPause, fPause);
  282.   }
  283.  
  284.   /// Set mouse cursor visibility when it is over DIVE window
  285.   void MouseVisible (bool State);
  286.  
  287.   /// Set mouse cursor shape when it is over DIVE window
  288.   inline void MouseCursor (ULONG ID)
  289.   { MouseCursorID = ID; }
  290.  
  291.   /// Repaint DIVE buffer
  292.   inline void Switch (long BufferNo, PRECTL rect = NULL)
  293.   {
  294.     ULONG Count;
  295.     DosResetEventSem (sRedrawComplete, &Count);
  296.     VisibleBuffer = (BufferNo == DIVE_NEXTBUFFER) ? ActiveBuffer : BufferNo;
  297.     WinInvalidateRect (diveCL, rect, FALSE);
  298.   }
  299.  
  300.   /// Wait until buffer redraw is complete. Recommended before calling Switch()
  301.   inline void WaitSwitch ()
  302.   {
  303.     DosWaitEventSem (sRedrawComplete, 1000);
  304.   }
  305.  
  306.   /// Return active buffer
  307.   inline long ActiveBuff ()
  308.   {
  309.     return ActiveBuffer;
  310.   }
  311.  
  312.   /// Set window title: This function requires a message queue
  313.   inline bool SetTitle (char *Title)
  314.   {
  315.     return WinSetWindowText (diveFR, (PSZ) Title);
  316.   }
  317.  
  318.   /// Send a message to frame window (any of cmdXXX works too)
  319.   inline void Command (ULONG CommandCode)
  320.   {
  321.     WinPostMsg (diveCL, WM_COMMAND, MPFROMSHORT (CommandCode), MPFROM2SHORT (CMDSRC_OTHER, CommandCode));
  322.   }
  323.  
  324.   /// Adjust these variables so that Width/Height == BufferWidth/BufferHeight
  325.   bool AdjustAspectRatio (long *Width, long *Height);
  326.  
  327.   /// Resize *client* window, compute and set new frame window size
  328.   bool Resize (long Width, long Height, bool Center);
  329.  
  330.   /// Resize *image buffer*; all previous images will be lost
  331.   bool ResizeBuffer (long Width, long Height, FOURCC Format);
  332.  
  333.   /// Switch to/from full-screen mode
  334.   bool FullScreen (bool State);
  335.  
  336.   /// Set DIVE window position
  337.   bool SetPos (long X, long Y);
  338.  
  339.   /// Define Keyboard Handler
  340.   inline void SetKeyboardHandler (tKeyboardHandler Handler, void *param)
  341.   {
  342.     hKeyboard = Handler;
  343.     paramKeyboard = param;
  344.   }
  345.  
  346.   /// Define Terminate Handler
  347.   inline void SetTerminateHandler (tTerminateHandler Handler, void *param)
  348.   {
  349.     hTerminate = Handler;
  350.     paramTerminate = param;
  351.   }
  352.  
  353.   /// Define mouse handler
  354.   inline void SetMouseHandler (tMouseHandler Handler, void *param)
  355.   {
  356.     hMouse = Handler;
  357.     paramMouse = param;
  358.   }
  359.  
  360.   /// Define window focus change handler
  361.   inline void SetFocusHandler (tFocusHandler Handler, void *param)
  362.   {
  363.     hFocus = Handler;
  364.     paramFocus = param;
  365.   }
  366.  
  367.   /// Define window focus change handler
  368.   inline void SetCmdHandler (tCmdHandler Handler, void *param)
  369.   {
  370.     hCmd = Handler;
  371.     paramCmd = param;
  372.   }
  373.  
  374.   /// Return if window is paused
  375.   inline bool isPaused()
  376.   {
  377.     return fPause;
  378.   }
  379.  
  380.   /// Return if FS mode is active
  381.   inline bool isFullScreen()
  382.   {
  383.     return fFullScreen;
  384.   }
  385.  
  386.   /// Return 'constant aspect ratio' flag
  387.   inline bool isAspectRatioConst()
  388.   {
  389.     return fAspect;
  390.   }
  391.  
  392.   /// Return minimized status
  393.   inline bool isMinimized ()
  394.   {
  395.     return fMinimized;
  396.   }
  397.  
  398.   /// Return logical buffer width
  399.   inline long BufferWidth ()
  400.   {
  401.     return BufferW;
  402.   }
  403.  
  404.   /// Return logical buffer height
  405.   inline long BufferHeight ()
  406.   {
  407.     return BufferH;
  408.   }
  409.  
  410.   /// Return physical client window width
  411.   inline long WindowWidth ()
  412.   {
  413.     return WindowW;
  414.   }
  415.  
  416.   /// Return physical client window height
  417.   inline long WindowHeight ()
  418.   {
  419.     return WindowH;
  420.   }
  421. private:
  422.   bool SetupBlitter ();
  423.   bool SetupPalette ();
  424.   static MRESULT EXPENTRY ClientHandler (HWND Handle, ULONG Message, MPARAM MsgParm1, MPARAM MsgParm2);
  425.   static MRESULT EXPENTRY FrameHandler (HWND Handle, ULONG Message, MPARAM MsgParm1, MPARAM MsgParm2);
  426. };
  427.  
  428. // DIVE simple application class; you can use it for simple applications
  429. class diveApp
  430. {
  431. public:
  432.   HAB AB;
  433.   HMQ MQ;
  434.   HWND appWN[16];
  435.   u_int appWNlength;
  436.  
  437.   diveApp ();
  438.   ~diveApp ();
  439.   HWND CreateWindow (PSZ Title, HMODULE ModID, ULONG ResID, ULONG Flags);
  440.   void Run ();
  441.   bool ProcessQueuedMessages ();
  442. };
  443.  
  444. #endif // RC_INVOKED
  445.  
  446. #endif // __LIBDIVE_H__
  447.