home *** CD-ROM | disk | FTP | other *** search
/ Quark 3 / Quark3.iso / KATALOG / ARCHIV / TOOL / T001.ZIP / SOURCE.ZIP / win32_driver.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-05  |  7.4 KB  |  303 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. /*
  20. win32 specific driver file:
  21. this include the WinMain function and all the code to initialize the win32 gui
  22. look for comments in md3view.h to see how the viewer system independent code needs to be
  23. initialized and used in the framwork
  24. */
  25.  
  26. #ifdef WIN32
  27.  
  28. #include "system.h"
  29. #include "ndictionary.h"
  30. #include "md3gl.h"
  31. #include "md3view.h"
  32. #include "resource.h"
  33.  
  34. HINSTANCE    WinhInstance;
  35. HWND        mainhWnd;
  36. char*    CmdLine = NULL;
  37.  
  38. #define WINDOW_STYLE WS_OVERLAPPEDWINDOW
  39.  
  40. // system specific event handler functions
  41. bool SysOnCreate(HWND hwnd, CREATESTRUCT FAR* lpCreateStruct);
  42. int  SysOnDestroy(HWND hWnd);        
  43. void SysOnMouseMove(HWND hwnd, int x, int y, UINT keyFlags);
  44. void SysOnRButtonUp(HWND hwnd, int x, int y, UINT flags);
  45. void SysOnLButtonUp(HWND hwnd, int x, int y, UINT flags);
  46. void SysOnRButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags);
  47. void SysOnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags);
  48. void SysOnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify);
  49. void SysOnKeyDown(HWND hwnd, UINT vk, BOOL fDown, int cRepeat, UINT flags);
  50. void SysOnKeyUp(HWND hwnd, UINT vk, BOOL fDown, int cRepeat, UINT flags);
  51. void SysOnIdle();
  52. void SysOnPaint( HWND hwnd );
  53. void SysOnSize(HWND hwnd, UINT state, int cx, int cy);
  54. void SysOnIdle();
  55.  
  56.  
  57. NodeSequenceInfo tagMenuList;
  58.  
  59. /*
  60. add a seperator
  61. */
  62. void tagMenu_seperatorAppend( char *name )
  63. {
  64.     char newname[512];
  65.     strcpy( newname, "...");
  66.     strcat( newname, name );
  67.     strcat( newname, "...");
  68.     char *n = new char[strlen(newname)+1];
  69.     strcpy( n, newname );
  70.  
  71.     HMENU subMenu = GetSubMenu( GetMenu(mdview.hwnd), TAG_MENU_ID );
  72.     AppendMenu( subMenu, MF_DISABLED|MF_STRING, ID_TAG_START+tagMenuList.size(), n );
  73.     tagMenuList.insertLast( (Object)NULL );
  74. }
  75.  
  76. /*
  77. remove a tag entry from the menu
  78. */
  79. void tagMenu_remove( GLMODEL_DBLPTR tagid )
  80. {
  81.     NodePosition pos;
  82.     GLMODEL_DBLPTR dblptr;
  83.     int i=1;
  84.     bool remove=false;
  85.  
  86.     // get the tag menu position
  87.     for (pos=tagMenuList.first() ; pos!=NULL ; pos=tagMenuList.after(pos)) {
  88.         dblptr = (GLMODEL_DBLPTR)pos->element();
  89.         if (dblptr == tagid) break;
  90.         i++;
  91.     }
  92.  
  93.     // return if not found
  94.     if (!pos) return;
  95.     
  96.     // remove the menu
  97.     HMENU subMenu = GetSubMenu( GetMenu(mdview.hwnd), TAG_MENU_ID );
  98.     DeleteMenu( subMenu, i, MF_BYPOSITION );
  99.         
  100.     // see if this is the last tag entry in a block
  101.     // if so remove the seperator
  102.     NodePosition b = tagMenuList.before(pos);
  103.     NodePosition a = tagMenuList.after(pos);
  104.     
  105.  
  106.     if (b==0) {
  107.         if (a==0) remove = true;
  108.         else if (a->element() == 0) remove = true;
  109.     }
  110.     else if  (b->element() == 0) {    
  111.         if (a==0) remove = true;
  112.         else if (a->element() == 0) remove = true;
  113.     }
  114.  
  115.     if (remove) {
  116.             DeleteMenu( subMenu, i-1, MF_BYPOSITION );
  117.             tagMenuList.remove( b );
  118.     }
  119.         
  120.     
  121.     // remove the pos from list
  122.     tagMenuList.remove( pos );    
  123. }
  124.  
  125.  
  126. /*
  127. add a tag to the menu
  128. */
  129. void tagMenu_append( char *name, GLMODEL_DBLPTR model )
  130. {
  131.     char *n = new char[strlen(name)+1];
  132.     strcpy( n, name );
  133.  
  134.     HMENU subMenu = GetSubMenu( GetMenu(mdview.hwnd), TAG_MENU_ID );
  135.     AppendMenu( subMenu, MF_ENABLED|MF_STRING, ID_TAG_START+tagMenuList.size(), n );
  136.     
  137.     tagMenuList.insertLast( (Object)model );
  138. }
  139.  
  140.  
  141. void swap_buffers()
  142. {
  143.     SwapBuffers( mdview.hdc );
  144. }
  145.  
  146. char *getCmdLine()
  147. {
  148.     return CmdLine;
  149. }
  150.  
  151. bool file_exists( char *fname )
  152. {
  153.     if (!fname) return false;
  154.     FILE *f = fopen( fname, "r" );
  155.     if (f) {
  156.         fclose(f);
  157.         return true;
  158.     }
  159.     else {
  160.         return false;
  161.     }
  162. }
  163.  
  164. void repaint_main()
  165. {
  166.     InvalidateRect( mdview.hwnd, NULL, FALSE );
  167. }
  168.  
  169.  
  170. void set_cursor( int x, int y )
  171. {
  172.     SetCursorPos( x, y );  
  173. }
  174.  
  175. /*
  176. time measuring stuff
  177. */
  178. double getDoubleTime (void)
  179. {
  180.     return (double)clock() / (double)CLOCKS_PER_SEC;
  181. }
  182.  
  183. /*
  184. main event handler
  185. */
  186.  
  187.  
  188. // event handler itself
  189. LONG WINAPI WinProcInstance(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  190. {
  191.  
  192.       switch (message)
  193.       {
  194.         HANDLE_MSG( hwnd, WM_DESTROY,     SysOnDestroy );
  195.         HANDLE_MSG( hwnd, WM_CLOSE,          SysOnDestroy );
  196.         HANDLE_MSG( hwnd, WM_CREATE,      SysOnCreate );     
  197.         HANDLE_MSG( hwnd, WM_PAINT,          SysOnPaint );
  198.         HANDLE_MSG( hwnd, WM_SIZE,          SysOnSize );     
  199.         HANDLE_MSG( hwnd, WM_COMMAND,     SysOnCommand );       
  200.         HANDLE_MSG( hwnd, WM_LBUTTONDOWN, SysOnLButtonDown );
  201.         HANDLE_MSG( hwnd, WM_RBUTTONDOWN, SysOnRButtonDown );
  202.         HANDLE_MSG( hwnd, WM_LBUTTONUP,   SysOnLButtonUp );
  203.         HANDLE_MSG( hwnd, WM_RBUTTONUP,   SysOnRButtonUp );
  204.         HANDLE_MSG( hwnd, WM_KEYDOWN,     SysOnKeyDown );
  205.         HANDLE_MSG( hwnd, WM_KEYUP,       SysOnKeyUp );     
  206.         case WM_MOUSEMOVE:
  207.         {
  208.             SysOnMouseMove(hwnd, (int)(short)LOWORD(lParam),(int)(short)HIWORD(lParam),(UINT)(wParam));
  209.             return 0L;
  210.         }
  211.         default:       
  212.             return(DefWindowProc(hwnd, message, wParam, lParam));
  213.       }
  214. }
  215.  
  216. /*
  217. creates window
  218. */
  219.  
  220. void WindowSystemInit( HINSTANCE hInstance )
  221. {
  222.     WNDCLASS wc;
  223.  
  224.     memset (&wc, 0, sizeof(wc));
  225.  
  226.     wc.style         = 0;
  227.     wc.lpfnWndProc   = (WNDPROC)WinProcInstance;
  228.     wc.cbClsExtra    = 0;
  229.     wc.cbWndExtra    = 0;
  230.     wc.hInstance     = hInstance;
  231.     wc.hIcon         = LoadIcon   (NULL, IDI_APPLICATION);
  232.     wc.hCursor       = LoadCursor (NULL, IDC_ARROW);
  233.     wc.hbrBackground = (HBRUSH)(BLACK_BRUSH);
  234.     wc.lpszMenuName  = NULL;
  235.     wc.lpszClassName = "mainWindow";
  236.  
  237.     RegisterClass(&wc);
  238.             
  239.     mainhWnd = CreateWindow (
  240.                 "mainWindow" , 
  241.                 FILENAME,
  242.                 WINDOW_STYLE,
  243.                 0, 0, 
  244.                 400, 
  245.                 400,
  246.                 0,
  247.                 LoadMenu(hInstance,MAKEINTRESOURCE(IDR_MENU1)),
  248.                 hInstance,
  249.                 NULL);
  250.      
  251.      ShowWindow ( mainhWnd, SW_SHOW );
  252.      UpdateWindow ( mainhWnd );    
  253. }
  254.  
  255. /*
  256. initializes app
  257. calls
  258. */
  259. void Init( HINSTANCE hInstance )
  260. {        
  261.     WindowSystemInit( hInstance );    
  262. }
  263.  
  264.  
  265. /*
  266. main program entry point
  267. */
  268. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  269. {
  270.      MSG        msg;    
  271.  
  272.      if ( ( lpCmdLine    != NULL ) && 
  273.           ( lpCmdLine[0] != '\0' ) )
  274.      {
  275.          CmdLine = new char[strlen(lpCmdLine)];
  276.          strcpy(CmdLine,lpCmdLine);
  277.      }
  278.  
  279.      WinhInstance = hInstance;
  280.  
  281.      /* initilizes mdview data */
  282.      init_mdview();
  283.  
  284.      Init( hInstance );
  285.      mdview.done = false;
  286.      
  287.      // main message loop     
  288.      while (!mdview.done)
  289.      {         
  290.              SysOnIdle();
  291.              while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
  292.              {                        
  293.                   TranslateMessage (&msg);
  294.                 TranslateAccelerator( mainhWnd, LoadAccelerators( hInstance, MAKEINTRESOURCE(IDR_ACCELERATOR1)), &msg ); 
  295.                   DispatchMessage (&msg);                        
  296.              }
  297.      }        
  298.     
  299.     shutdown_mdviewdata();
  300.     return 1;
  301. }        
  302.  
  303. #endif