home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / RAYCAST.ZIP / WINRUN.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-15  |  14.5 KB  |  398 lines

  1. #include "ray.h"
  2. #include "globals.h"
  3. #ifdef OS_WINDOWS
  4. #include "winfuncs.h"
  5. #include "scrcntl.h"
  6. #include "gamerun.h"
  7. #include "palette.h"
  8. #include "raybuff.h"
  9. #include "winvars.h"
  10. #include "keyboard.h"
  11. #include "loadwor.h"
  12. #include <iostream.h>
  13.  
  14. BOOL active_app, initialized_app;
  15. char    szAppName[]="BloodBath Prototype!";
  16. char    szAppBaseWindowTitle[]="BloodBath";
  17.  OPENFILENAME ofn;
  18.  char szFileName[MAXFILENAME];
  19.  char szFileTitle[MAXFILENAME];
  20.  char szFilterSpec [128] =                       /* file type filters */
  21.              "World Files (*.DAT)\0*.DAT\0Doom Files(*.WAD)\0*.WAD\0All Files (*.*)\0*.*\0";
  22.  
  23. HINSTANCE hInstApp;
  24. HWND      hwndApp;
  25. /*----------------------------------------------------------------------------*\
  26. |                                                                              |
  27. |   f u n c t i o n   d e f i n i t i o n s                                    |
  28. |                                                                              |
  29. \*----------------------------------------------------------------------------*/
  30.  
  31. LONG FAR PASCAL Win_Window_Proc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
  32. LONG AppCommand (HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
  33.  
  34. void Win_Close(void);
  35. BOOL Win_Idle(void);
  36.  
  37. /*----------------------------------------------------------------------------*\
  38. |   AppAbout( hDlg, uiMessage, wParam, lParam )                                |
  39. |                                                                              |
  40. |   Description:                                                               |
  41. |       This function handles messages belonging to the "About" dialog box.    |
  42. |       The only message that it looks for is WM_COMMAND, indicating the use   |
  43. |       has pressed the "OK" button.  When this happens, it takes down         |
  44. |       the dialog box.                                                        |
  45. |                                                                              |
  46. |   Arguments:                                                                 |
  47. |       hDlg            window handle of about dialog window                   |
  48. |       uiMessage       message number                                         |
  49. |       wParam          message-dependent                                      |
  50. |       lParam          message-dependent                                      |
  51. |                                                                              |
  52. |   Returns:                                                                   |
  53. |       TRUE if message has been processed, else FALSE                         |
  54. |                                                                              |
  55. \*----------------------------------------------------------------------------*/
  56. BOOL FAR PASCAL  AppAbout(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
  57. {
  58.     switch (msg)
  59.     {
  60.         case WM_COMMAND:
  61.             if (LOWORD(wParam) == IDOK)
  62.             {
  63.                 EndDialog(hwnd,TRUE);
  64.             }
  65.             break;
  66.  
  67.         case WM_INITDIALOG:
  68.             return TRUE;
  69.     }
  70.     return FALSE;
  71. }
  72.  
  73. /*----------------------------------------------------------------------------*\
  74. |   Win_Init( hInst, hPrev)                                                     |
  75. |                                                                              |
  76. |   Description:                                                               |
  77. |       This is called when the application is first loaded into               |
  78. |       memory.  It performs all initialization that doesn't need to be done   |
  79. |       once per instance.                                                     |
  80. |                                                                              |
  81. |   Arguments:                                                                 |
  82. |       hInstance       instance handle of current instance                    |
  83. |       hPrev           instance handle of previous instance                   |
  84. |                                                                              |
  85. |   Returns:                                                                   |
  86. |       TRUE if successful, FALSE if not                                       |
  87. |                                                                              |
  88. \*----------------------------------------------------------------------------*/
  89. BOOL Win_Init(HINSTANCE hInst,HINSTANCE hPrev,int sw,LPSTR szCmdLine)
  90. {
  91.     WNDCLASS cls;
  92.  
  93.     /* Save instance handle for DialogBoxs */
  94.     hInstApp = hInst;
  95.  
  96.     if (!hPrev)
  97.     {
  98.         /*
  99.          *  Register a class for the main application window
  100.          */
  101.         cls.hCursor        = LoadCursor(NULL,IDC_ARROW);
  102.         cls.hIcon          = LoadIcon(hInst,"AppIcon");
  103.         cls.lpszMenuName   = "AppMenu";
  104.         cls.lpszClassName  = szAppName;
  105.         cls.hbrBackground  = (HBRUSH)(COLOR_WINDOW + 1);
  106.         cls.hInstance      = hInst;
  107.         cls.style          = CS_BYTEALIGNCLIENT | CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS;
  108.         cls.lpfnWndProc    = (WNDPROC)Win_Window_Proc;
  109.         cls.cbWndExtra     = 0;
  110.         cls.cbClsExtra     = 0;
  111.  
  112.         if (!RegisterClass(&cls))
  113.             return FALSE;
  114.     }
  115.  
  116.    long nBitmapW = 320;                 //default size (window client size to use).
  117.    long nBitmapH = 200;
  118.  
  119.         //work backwards from client size to desired window size.  doesn't have
  120.         //to be super accurate, approximate will do.
  121.  
  122.         int windx, windy;
  123.  
  124.         windx = windy = GetSystemMetrics(SM_CXFRAME)*2;
  125.         windx += nBitmapW;      
  126.         windy += nBitmapH + GetSystemMetrics(SM_CYCAPTION) - 1;
  127.         windy += GetSystemMetrics(SM_CYMENU);
  128.  
  129.    hwndApp = CreateWindow (szAppName,    // Class name
  130.                            szAppName,              // Caption
  131.                            WS_OVERLAPPEDWINDOW,    // Style bits
  132.                            CW_USEDEFAULT, 0,       // Position
  133.                                      windx,windy,                   // Size
  134.                            (HWND)NULL,             // Parent window (no parent)
  135.                            (HMENU)NULL,            // use class menu
  136.                            hInst,                  // handle to window instance
  137.                            (LPSTR)NULL             // no params to pass on
  138.                            );
  139.     ShowWindow(hwndApp,sw);
  140.  
  141.     HMENU Menu = GetMenu(hwndApp);
  142.     CheckMenuItem(Menu,ID_STUFF_1X,MF_CHECKED);
  143.  
  144.     initialized_app=FALSE;
  145.     return TRUE;
  146. }
  147.  
  148. void Win_Close() {
  149.  
  150.    if (initialized_app)
  151.       Global_Close();
  152.    }
  153.  
  154. /*----------------------------------------------------------------------------*\
  155. |   WinMain( hInst, hPrev, lpszCmdLine, cmdShow )                              |
  156. |                                                                              |
  157. |   Description:                                                               |
  158. |       The main procedure for the App.  After initializing, it just goes      |
  159. |       into a message-processing loop until it gets a WM_QUIT message         |
  160. |       (meaning the app was closed).                                          |
  161. |                                                                              |
  162. |   Arguments:                                                                 |
  163. |       hInst           instance handle of this instance of the app            |
  164. |       hPrev           instance handle of previous instance, NULL if first    |
  165. |       szCmdLine       ->null-terminated command line                         |
  166. |       cmdShow         specifies how the window is initially displayed        |
  167. |                                                                              |
  168. |   Returns:                                                                   |
  169. |       The exit code as specified in the WM_QUIT message.                     |
  170. |                                                                              |
  171. \*----------------------------------------------------------------------------*/
  172. int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
  173. {
  174.     MSG     msg;
  175.  
  176.     /* Call initialization procedure */
  177.     if (!Win_Init(hInst,hPrev,sw,szCmdLine))
  178.                         return FALSE;
  179.  
  180.     /*
  181.      * Polling messages from event queue
  182.      */
  183.     for (;;)
  184.     {
  185.         if (PeekMessage(&msg, NULL, 0, 0,PM_REMOVE))
  186.         {
  187.             if (msg.message == WM_QUIT)
  188.                 break;
  189.  
  190.             TranslateMessage(&msg);
  191.             DispatchMessage(&msg);
  192.         }
  193.         else
  194.                  {
  195.             if (Win_Idle())
  196.                 WaitMessage();
  197.         }
  198.     } 
  199.  
  200.     Win_Close();
  201.     return msg.wParam;
  202. }
  203.  
  204. ////////////////////////////////////////////////////////////////////////////
  205. // Win_Copy_Buff. Copys buffer to screen
  206. //
  207. void Win_Copy_Buff(void)
  208. {
  209.         HDC hdc = GetDC(hwndApp);
  210.         Set_Screen_Window(hdc);
  211.         //don't bother to realize the palette, because we've got Activate_Palette().
  212.         copyBuff();
  213.         ReleaseDC(hwndApp, hdc);
  214. }
  215.  
  216. BOOL Win_Idle()
  217. {
  218.    if (active_app) {
  219.         // we are the foreground app.
  220.         Do_Main_Cycle();
  221.         return FALSE;
  222.     }
  223.     else
  224.     {
  225.        return TRUE;
  226.         // we are background, so do nothing!
  227.     }
  228. }
  229.  
  230. /////////////////////////////////////////////////////////////////////////////
  231. // AppSetCaption().  Sets the window caption to the specified string,
  232. // appended to the BloodBath base caption.
  233. //
  234. void AppSetCaption(char *szCaption)
  235. {
  236.         char szWindowTitle[MAXFILENAME];
  237.  
  238.         lstrcpy(szWindowTitle, szAppBaseWindowTitle);
  239.         lstrcat(szWindowTitle, " - ");
  240.         lstrcat(szWindowTitle, szCaption);
  241.         SetWindowText(hwndApp, szWindowTitle);
  242. }
  243.  
  244. /////////////////////////////////////////////////////////////////////////////
  245. // Win_Window_Proc( hwnd, uiMessage, wParam, lParam )
  246. //
  247. // The window proc for the app's main (tiled) window.  This processes all
  248. // of the parent window's messages.
  249. //
  250. LONG FAR PASCAL  Win_Window_Proc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
  251. {
  252.     PAINTSTRUCT ps;
  253.     HDC hdc;
  254.     BOOL f;
  255.  
  256.     switch (msg)
  257.     {
  258.     case WM_CREATE:
  259.                         //let WM_SIZE do all the work.
  260.               break;
  261.  
  262.    case WM_ACTIVATEAPP:
  263.                     active_app = (BOOL)wParam;
  264.                    hdc=GetDC(hwnd);
  265.                    Set_Screen_Window(hdc);
  266.                      // *** Remap the system colors and deal with the palette
  267.                     Activate_Palette(active_app);
  268.                    ReleaseDC(hwnd, hdc);
  269.       break;
  270.    case WM_SIZE:                        
  271.  
  272.                 if (!initialized_app) {
  273.                    Init_Screen(LOWORD(lParam), HIWORD(lParam));
  274.                    Global_Initialize();
  275.                    initialized_app=TRUE;
  276.                 } else {
  277.                    Change_Screen(LOWORD(lParam), HIWORD(lParam));
  278.                 }
  279.       break;
  280.    case WM_KEYDOWN:
  281.       Process_Key(wParam);
  282.       break;
  283.    case WM_KEYUP:
  284.       Process_Key(wParam+128);
  285.       break;
  286.    case WM_LBUTTONDOWN:
  287.       break;
  288.    case WM_RBUTTONDOWN:
  289.       break;
  290.    case WM_MOUSEMOVE:
  291.       break;
  292.    case WM_COMMAND:
  293.       return AppCommand(hwnd,msg,wParam,lParam);
  294.    case WM_DESTROY:
  295.       PostQuitMessage(0);
  296.       break;
  297.    case WM_CLOSE:
  298.       break;
  299.    case WM_PALETTECHANGED:
  300.        if ((HWND)wParam == hwnd)
  301.                  break;
  302.       // fall through to WM_QUERYNEWPALETTE
  303.    case WM_QUERYNEWPALETTE:
  304.       hdc=GetDC(hwnd);
  305.       Set_Screen_Window(hdc);
  306.      f = Update_Palette();
  307.       ReleaseDC(hwnd, hdc);
  308.       if (f)
  309.          InvalidateRect(hwnd,NULL,TRUE); 
  310.       return f;
  311.    case WM_PAINT:
  312.         hdc = BeginPaint(hwnd,&ps);
  313.         Set_Screen_Window(hdc);
  314.         Update_Palette();
  315.         copyBuff();
  316.         EndPaint(hwnd,&ps);
  317.         return 0L;
  318.         }
  319.         return DefWindowProc(hwnd,msg,wParam,lParam);
  320. }
  321.  
  322.  
  323. /////////////////////////////////////////////////////////////////////////////
  324. // Handler for File|OpenWorld menu command.  Prompt user, via dialog box, for
  325. // a world-file to open, then re-init Nottingham with this new world.
  326. //
  327. BOOL AppOnFileOpen(void)
  328. {
  329.         //fill in non-variant fields of OPENFILENAME struct.  don't have to do
  330.         //this every time, but I really don't care.
  331.   
  332.    ofn.lStructSize       = sizeof(OPENFILENAME);
  333.    ofn.hwndOwner                = hwndApp;
  334.    ofn.lpstrFilter           = szFilterSpec;
  335.    ofn.lpstrCustomFilter = NULL;
  336.    ofn.nMaxCustFilter     = 0;
  337.    ofn.nFilterIndex          = 1;
  338.    ofn.lpstrFile         = szFileName;
  339.    ofn.nMaxFile         = MAXFILENAME;
  340.    ofn.lpstrInitialDir   = NULL;
  341.    ofn.lpstrFileTitle    = szFileTitle;
  342.    ofn.nMaxFileTitle     = MAXFILENAME;
  343.    ofn.lpstrTitle        = NULL;
  344.    ofn.lpstrDefExt       = "DAT";
  345.    ofn.Flags             = 0;
  346.  
  347.    if (!GetOpenFileName ((LPOPENFILENAME)&ofn))
  348.       return FALSE;   
  349.  
  350.         AppSetCaption(szFileTitle);
  351.    Load_World(szFileName);
  352.    return TRUE;
  353. }
  354.  
  355. /////////////////////////////////////////////////////////////////////////////
  356. // AppCommand(hwnd, msg, wParam, lParam )
  357. //
  358. //      handles WM_COMMAND messages for the main window (hwndApp)
  359. // of the parent window's messages.
  360. //
  361. LONG AppCommand (HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
  362. {
  363.     switch(wParam)
  364.     {
  365.         case ID_FILE_ABOUT:
  366.                         DialogBox(hInstApp,"AppAbout",hwnd,(DLGPROC)AppAbout);
  367.             break;
  368.  
  369.         case ID_FILE_EXIT:
  370.             PostMessage(hwnd,WM_CLOSE,0,0L);
  371.             break;
  372.  
  373.         case ID_FILE_OPEN:
  374.             AppOnFileOpen();
  375.             break;
  376.  
  377.                   case ID_STUFF_1X:                     //stretch choices.
  378.                   case ID_STUFF_2X:
  379.                                 {
  380.                            HMENU Menu = GetMenu(hwndApp);
  381.  
  382.                                         // uncheck current selection
  383.                                 CheckMenuItem(Menu,ID_STUFF_1X - 1 + Get_Stretch_Factor(),MF_UNCHECKED);
  384.  
  385.                                 // get the stretch factor
  386.                                 Set_Stretch_Factor(1 + wParam - ID_STUFF_1X);
  387.  
  388.                                 CheckMenuItem(Menu,wParam,MF_CHECKED);  //check new selection.
  389.  
  390.                                 InvalidateRect(hwnd,NULL,TRUE);                 //force a re-Blit
  391.                                 }
  392.                                 break;
  393.  
  394.         }
  395.     return 0L;
  396. }
  397. #endif
  398.