home *** CD-ROM | disk | FTP | other *** search
/ Quark 3 / Quark3.iso / KATALOG / ARCHIV / TOOL / T001.ZIP / SOURCE.ZIP / win32_msg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-19  |  13.5 KB  |  579 lines

  1. /*
  2. Copyright (C) Matthew 'pagan' Baranowski & Sander 'FireStorm' van Rossen
  3.  
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17. */
  18.  
  19. #ifdef WIN32
  20.  
  21. #include "system.h"
  22. #include "ndictionary.h"
  23. #include "md3gl.h"
  24. #include "md3view.h"
  25. #include "Resource.h"
  26. #include "AFXRES.H"
  27.  
  28. extern HINSTANCE WinhInstance;
  29. extern NodeSequenceInfo tagMenuList;
  30. HDC  hDC_;
  31.  
  32. bool sys_rbuttondown = false;
  33. bool sys_lbuttondown = false;
  34. bool sys_mbuttondown = false;
  35.  
  36.  
  37. OPENFILENAME *FileOpenDialog(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl, int type);
  38. OPENFILENAME *FileSaveDialog(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl, int type);
  39.  
  40. /*
  41. selects a 16 bit color file format, could be higher though it wouldn't work with a voodoo
  42. */
  43. void SetPixelFormat( HDC hdc)
  44. {
  45.     PIXELFORMATDESCRIPTOR pfd, *ppfd;
  46.     int pixelformat;
  47.     ppfd = &pfd;    
  48.  
  49.     memset(ppfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
  50.     ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
  51.     ppfd->nVersion = 1;
  52.     ppfd->dwFlags = PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  53.     ppfd->dwLayerMask = PFD_MAIN_PLANE;
  54.     ppfd->iPixelType = PFD_TYPE_RGBA;
  55.     ppfd->cColorBits = 16;
  56.     ppfd->cDepthBits = 16;
  57.     ppfd->cAccumBits = 0;
  58.     ppfd->cStencilBits = 0;
  59.  
  60.     pixelformat = ChoosePixelFormat( hdc, ppfd);
  61.  
  62.     if ( pixelformat == 0) {
  63.         Error("ChoosePixelFormat failed");
  64.     }
  65.  
  66.     if (ppfd->dwFlags & PFD_NEED_PALETTE) {
  67.        Error ("ChoosePixelFormat needs palette" );
  68.     }
  69.  
  70.     if (SetPixelFormat( hdc, pixelformat, ppfd) == FALSE) {
  71.         Error("SetPixelFormat failed");
  72.     }
  73. }
  74.  
  75. /*
  76. creates an OpenGL rendering context and makes it current
  77. */
  78.  
  79. void InitOpenGL( HWND hwnd )
  80. {
  81.     HDC hdc = GetDC( hwnd );
  82.     mdview.hdc = hdc;
  83.  
  84.     SetPixelFormat( hdc );
  85.  
  86.     HGLRC glrc = wglCreateContext( hdc );
  87.     mdview.glrc = glrc;
  88.     
  89.     if ( glrc == NULL) {    
  90.         Error("Failed on wglCreateContext( HDC  hdc )");            
  91.     }
  92.                 
  93.     if (!wglMakeCurrent( hdc, glrc)) {        
  94.         Error("Failed on wglMakeCurrent(..)" );
  95.     }
  96. }
  97.  
  98. /*
  99. handles WM_CREATE
  100. */
  101.  
  102. bool SysOnCreate(HWND hwnd, CREATESTRUCT FAR* lpCreateStruct) 
  103.     mdview.hwnd = hwnd;
  104.     InitOpenGL( hwnd );
  105.     initialize_glstate();
  106.  
  107.     RECT rect;
  108.     GetClientRect( hwnd, &rect );
  109.     set_windowSize( rect.right, rect.bottom );
  110.     
  111.     if (getCmdLine() != NULL )
  112.     {
  113.         if (!loadmdl( getCmdLine() )) {
  114.             Debug( "could not load %s", getCmdLine() );
  115.         }
  116.     }
  117.     
  118.     return true;
  119. }
  120.  
  121. /*
  122. called every pass of the event loop
  123. */
  124. void SysOnIdle()
  125. {
  126.     animation_loop();
  127. }
  128.  
  129. /*
  130. renders the model with simple viewing parameters
  131. */
  132. void SysOnPaint( HWND hwnd ) 
  133.     PAINTSTRUCT ps;            
  134.         
  135.     BeginPaint(hwnd, &ps);
  136.  
  137.     wglMakeCurrent( mdview.hdc, mdview.glrc );
  138.     render_mdview();
  139.  
  140.     SwapBuffers( GetDC(hwnd) );  
  141.     EndPaint(hwnd, &ps);    
  142.  
  143. /* 
  144. notifies md3view of resize event 
  145. */
  146. void SysOnSize(HWND hwnd, UINT state, int cx, int cy) 
  147.     RECT rect;
  148.     GetClientRect( hwnd, &rect );
  149.     set_windowSize( rect.right, rect.bottom );
  150. }
  151.  
  152. /*
  153. sets quit parameter to break out of the message loop
  154. */
  155. int  SysOnDestroy(HWND hWnd)
  156. {        
  157.     wglDeleteContext( mdview.glrc );
  158.     mdview.done = true;
  159.     return 0;     
  160. }
  161.  
  162. /*
  163. passes control to drag.cpp drag function
  164. */
  165. void SysOnMouseMove(HWND hwnd, int x, int y, UINT keyFlags) 
  166.     if (!(sys_rbuttondown || sys_lbuttondown || sys_mbuttondown)) return;
  167.  
  168.     POINT point;
  169.     GetCursorPos(&point);
  170.  
  171.     drag( (mkey_enum)keyFlags, point.x, point.y );
  172. }
  173.  
  174. /*
  175. releases and shows the cursor again
  176. */
  177. void SysOnRButtonUp(HWND hwnd, int x, int y, UINT flags) 
  178.     if (!sys_rbuttondown) return;
  179.  
  180.     ReleaseCapture(); 
  181.     ShowCursor( true ); 
  182.     end_drag( (mkey_enum)flags, x, y );
  183.     sys_rbuttondown = false;
  184. }
  185.  
  186. /* 
  187. same as above
  188. */
  189. void SysOnLButtonUp(HWND hwnd, int x, int y, UINT flags) 
  190.     if (!sys_lbuttondown) return;
  191.  
  192.     ReleaseCapture();
  193.     ShowCursor( true );     
  194.     end_drag( (mkey_enum)flags, x, y );
  195.     sys_lbuttondown = false;
  196. }
  197.  
  198. /*
  199. on mouse down, hide the cursor and capture it to window
  200. */
  201. void SysOnRButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags) 
  202.     POINT point;
  203.     GetCursorPos(&point);
  204.     start_drag( (mkey_enum)keyFlags, point.x, point.y );
  205.     SetCapture( hwnd );
  206.     ShowCursor( false ); 
  207.     sys_rbuttondown = true;
  208. }
  209.  
  210. /*
  211. */
  212. void SysOnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags) 
  213.     POINT point;
  214.     GetCursorPos(&point);
  215.     start_drag( (mkey_enum)keyFlags, point.x, point.y );
  216.     SetCapture( hwnd );
  217.     ShowCursor( false ); 
  218.     sys_lbuttondown = true;
  219. }
  220.  
  221.  
  222. /*
  223. processes command issued from the tag menu
  224. */
  225. void SysOnTagCommand( HWND hwnd, int id, HWND hwndCtl, UINT codeNotify ) 
  226. {
  227.         int i = 0, tagID = id - ID_TAG_START;
  228.         NodePosition pos;
  229.         GLMODEL_DBLPTR dblptr;
  230.         for (pos=tagMenuList.first() ; pos!=NULL ; pos=tagMenuList.after(pos)) {            
  231.             if (i == tagID) break;
  232.             i++;
  233.         }
  234.  
  235. #ifdef DEBUG_PARANOID
  236.         if (pos==NULL) Debug("tagMenuList entry not found");
  237. #endif
  238.         dblptr = (GLMODEL_DBLPTR)pos->element();
  239.         
  240.             OPENFILENAME *ofn;
  241.             ofn = FileOpenDialog(hwnd, id, codeNotify, hwndCtl, IDS_FILESTRING );
  242.             if (ofn) 
  243.             {
  244.                 char fullName[256];
  245.                 strcpy( fullName, ofn->lpstrFile );
  246.                 strcat( fullName, ofn->lpstrFileTitle );
  247.             
  248.                 if (dblptr != 0) freemdl_fromtag(dblptr);
  249.                 loadmdl_totag( fullName, dblptr );
  250.                 
  251.                 
  252.                 InvalidateRect( hwnd, NULL, FALSE );
  253.                 
  254.                 //leave this here!! without the screen doesn't 
  255.                 //refresh properly sometimes!!
  256.                 InvalidateRect( mdview.hwnd, NULL, FALSE );
  257.                 free( ofn );
  258.             }
  259.             else {
  260.                 if (dblptr != 0) {
  261.                     freemdl_fromtag( dblptr );
  262.                 }
  263.             }
  264.  
  265. }
  266.  
  267.  
  268. /*
  269. processes events from the menu
  270. */
  271. void SysOnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify) 
  272.  
  273.     // process tag menu requests
  274.     if ((id >= ID_TAG_START) && (id < ID_TAG_START+tagMenuList.size())) {
  275.         SysOnTagCommand( hwnd, id, hwndCtl, codeNotify );
  276.         InvalidateRect( mdview.hwnd, NULL, FALSE );
  277.     }
  278.             
  279.  
  280.     switch(id)
  281.     {
  282.         case ID_FILE_OPEN:
  283.         {
  284.             OPENFILENAME *ofn;
  285.             ofn = FileOpenDialog(hwnd, id, codeNotify, hwndCtl, IDS_FILESTRING );
  286.             if (ofn) 
  287.             {
  288.                 char fullName[256];
  289.                 strcpy( fullName, ofn->lpstrFile );
  290.                 strcat( fullName, ofn->lpstrFileTitle );
  291.             
  292.                 free_mdviewdata();
  293.                 loadmdl( fullName );
  294.                 
  295.                 InvalidateRect( hwnd, NULL, FALSE );
  296.                 
  297.                 //leave this here!! without the screen doesn't 
  298.                 //refresh properly sometimes!!
  299.                 InvalidateRect( mdview.hwnd, NULL, FALSE );
  300.                 free( ofn );
  301.             }
  302.             break;
  303.         }
  304.         case ID_FILE_EXPORTTORAW:
  305.         {
  306.             OPENFILENAME *ofn;
  307.             ofn = FileSaveDialog(hwnd, id, codeNotify, hwndCtl, IDS_RAWFILEFILTER );
  308.             if (ofn) 
  309.             {
  310.                 char fullName[256];
  311.                 strcpy( fullName, ofn->lpstrFileTitle );
  312.                 
  313.             
  314.                 write_baseModelToRaw( fullName );
  315.                     
  316.                 free( ofn );
  317.             }
  318.             break;
  319.         }
  320.         
  321.         case ID_FILE_IMPORTSKIN:
  322.         {
  323.             OPENFILENAME *ofn;
  324.             ofn = FileOpenDialog(hwnd, id, codeNotify, hwndCtl, IDS_SKINFILEFILTER );
  325.             if (ofn) 
  326.             {
  327.                 char fullName[256];
  328.                 strcpy( fullName, ofn->lpstrFileTitle );
  329.             
  330.                 importNewSkin( fullName );
  331.                 
  332.                 InvalidateRect( hwnd, NULL, FALSE );
  333.                 
  334.                 //leave this here!! without the screen doesn't 
  335.                 //refresh properly sometimes!!
  336.                 InvalidateRect( mdview.hwnd, NULL, FALSE );
  337.                 free( ofn );
  338.             }
  339.             break;
  340.         }
  341.         case ID_FILE_EXIT:
  342.         {
  343.             mdview.done = true;
  344.             break;
  345.         }
  346.         case ID_ABOUT:
  347.         {
  348.             MessageBox(hwnd, ABOUT_TEXT, "About",MB_OK | MB_ICONINFORMATION);
  349.             break;
  350.         }
  351.  
  352.         case ID_VIEW_REFRESHTEXTURE:
  353.             refreshTextureRes();
  354.             InvalidateRect( hwnd, NULL, FALSE );
  355.             break;
  356.  
  357.         case ID_VIEW_WIREFRAME: 
  358.             oglStateWireframe(); 
  359.             InvalidateRect( hwnd, NULL, FALSE );
  360.             break;
  361.  
  362.         case ID_VIEW_FLATSHADED: 
  363.             oglStateShadedFlat(); 
  364.             InvalidateRect( hwnd, NULL, FALSE );
  365.             break;
  366.  
  367.         case ID_VIEW_TEXTURED: 
  368.             oglStateFlatTextured(); 
  369.             InvalidateRect( hwnd, NULL, FALSE );
  370.             break;
  371.             
  372.         case ID_VIEW_TEXTUREDSHADED: 
  373.             oglStateShadedTextured();
  374.             InvalidateRect( hwnd, NULL, FALSE );
  375.             break;
  376.  
  377.         case ID_VIEW_NEAREST:
  378.             mdview.texMode = TEX_FAST;
  379.             setTextureFilter();
  380.             InvalidateRect( hwnd, NULL, FALSE );
  381.             break;
  382.  
  383.         case ID_VIEW_UNFILTEREDTEXTURE:
  384.             mdview.texMode = TEX_UNFILTERED;
  385.             setTextureFilter();
  386.             InvalidateRect( hwnd, NULL, FALSE );
  387.             break;
  388.  
  389.         case ID_VIEW_FILTEREDTEXTURE:
  390.             mdview.texMode = TEX_FILTERED;
  391.             setTextureFilter();
  392.             InvalidateRect( hwnd, NULL, FALSE );
  393.             break;
  394.  
  395.         case ID_ANIMATION_SLOWER:
  396.             mdview.animSpeed *= ANIM_SLOWER;
  397.             break;
  398.  
  399.         case ID_ANIMATION_FASTER:
  400.             mdview.animSpeed *= ANIM_FASTER;
  401.             break;
  402.  
  403.         case ID_ANIMATION_STOP:
  404.             mdview.animate = false;
  405.             break;
  406.  
  407.         case ID_ANIMATION_START:
  408.             mdview.animate = true;
  409.             break;
  410.  
  411.         case ID_ANIMATION_REWIND:
  412.             rewindAnim();
  413.             InvalidateRect( hwnd, NULL, FALSE );
  414.             break;
  415.  
  416.         case ID_ANIMATION_INTERPOLATE:
  417.             if (mdview.interpolate) mdview.interpolate = false;
  418.             else mdview.interpolate = true;
  419.             break;
  420.     }
  421. }
  422.  
  423. void SysOnKeyDown(HWND hwnd, UINT vk, BOOL fDown, int cRepeat, UINT flags) { 
  424.  
  425. }
  426. void SysOnKeyUp(HWND hwnd, UINT vk, BOOL fDown, int cRepeat, UINT flags) { }
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434. /* ---------------------------------------------- dialog code -------------------------------------------- */
  435.  
  436. /*
  437. saves the directory name after every dialog use
  438. */
  439.  
  440. char*    szDirName = new char[256];
  441. void SetDirectory(LPOPENFILENAME lpofn)
  442. {
  443.     lpofn->lpstrFile[lpofn->nFileOffset] = '\0';
  444.     lstrcpy(szDirName, lpofn->lpstrFile);
  445.     lstrcpy(mdview.basepath, lpofn->lpstrFile);
  446. }
  447.  
  448.  
  449. /*
  450. handles the file open dialog
  451. */
  452. OPENFILENAME *FileOpenDialog(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl, int type)
  453. {
  454.     OPENFILENAME *ofn = (OPENFILENAME *)malloc(sizeof( OPENFILENAME ));
  455.     memset( ofn, 0, sizeof( OPENFILENAME ) );
  456.     char szFile[256];       // filename string
  457.     char szFileTitle[256];  // file-title string
  458.     char szFilter[256];     // filter string
  459.     char chReplace;         // strparator for szFilter
  460.     int i, cbString;        // integer count variables
  461.  
  462.     // Retrieve the current directory name and store it in szDirName.
  463.  
  464.     if (szDirName[0] == '\0')
  465.     {
  466.         GetCurrentDirectory(256,szDirName);
  467.     }
  468.  
  469.     // Place the terminating null character in the szFile.
  470.  
  471.     szFile[0] = '\0';
  472.  
  473.     // Load the filter string from the resource file.
  474.  
  475.     cbString = LoadString(WinhInstance, type, szFilter, sizeof(szFilter));
  476.  
  477.     // Add a terminating null character to the filter string.
  478.  
  479.     chReplace = szFilter[cbString - 1];
  480.     for (i = 0; szFilter[i] != '\0'; i++)
  481.     {
  482.         if (szFilter[i] == chReplace)
  483.             szFilter[i] = '\0';
  484.     }
  485.  
  486.     // Set the members of the OPENFILENAME structure.
  487.  
  488.     ofn->lStructSize = sizeof(OPENFILENAME);
  489.     ofn->hwndOwner = hwnd;
  490.     ofn->lpstrFilter = szFilter;
  491.     ofn->nFilterIndex = 1;
  492.     ofn->lpstrFile = szFile;
  493.     ofn->nMaxFile = sizeof(szFile);
  494.     ofn->lpstrFileTitle = szFileTitle;
  495.     ofn->nMaxFileTitle = sizeof(szFileTitle);
  496.     ofn->lpstrInitialDir = szDirName;
  497.     ofn->Flags = OFN_SHOWHELP | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  498.  
  499.     // Display the Open dialog box.
  500.  
  501.     if (GetOpenFileName(ofn))
  502.     {        
  503.         SetDirectory(ofn);
  504.         return ofn;
  505.     }
  506.     return NULL;
  507. }
  508.  
  509. /*
  510. handles the file save dialog
  511. */
  512. OPENFILENAME *FileSaveDialog(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl, int type)
  513. {
  514.     OPENFILENAME *ofn = (OPENFILENAME *)malloc(sizeof( OPENFILENAME ));
  515.     memset( ofn, 0, sizeof( OPENFILENAME ) );
  516.     char szFile[256];       // filename string
  517.     char szFileTitle[256];  // file-title string
  518.     char szFilter[256];     // filter string
  519.     char chReplace;         // strparator for szFilter
  520.     int i, cbString;        // integer count variables
  521.  
  522.     // Retrieve the current directory name and store it in szDirName.
  523.  
  524.     if (szDirName[0] == '\0')
  525.     {
  526.         GetCurrentDirectory(256,szDirName);
  527.     }
  528.  
  529.     // Place the terminating null character in the szFile.
  530.  
  531.     szFile[0] = '\0';
  532.  
  533.     // Load the filter string from the resource file.
  534.  
  535.     cbString = LoadString(WinhInstance, type, szFilter, sizeof(szFilter));
  536.  
  537.     // Add a terminating null character to the filter string.
  538.  
  539.     chReplace = szFilter[cbString - 1];
  540.     for (i = 0; szFilter[i] != '\0'; i++)
  541.     {
  542.         if (szFilter[i] == chReplace)
  543.             szFilter[i] = '\0';
  544.     }
  545.  
  546.     // Set the members of the OPENFILENAME structure.
  547.  
  548.     ofn->lStructSize = sizeof(OPENFILENAME);
  549.     ofn->hwndOwner = hwnd;
  550.     ofn->lpstrFilter = szFilter;
  551.     ofn->nFilterIndex = 1;
  552.     ofn->lpstrFile = szFile;
  553.     ofn->nMaxFile = sizeof(szFile);
  554.     ofn->lpstrFileTitle = szFileTitle;
  555.     ofn->nMaxFileTitle = sizeof(szFileTitle);
  556.     ofn->lpstrInitialDir = szDirName;
  557.     ofn->Flags = OFN_SHOWHELP | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  558.  
  559.     // Display the Open dialog box.
  560.  
  561.     if (GetSaveFileName(ofn))
  562.     {        
  563.         //SetDirectory(ofn);
  564.         return ofn;
  565.     }
  566.     return NULL;
  567. }
  568.  
  569. #endif