home *** CD-ROM | disk | FTP | other *** search
/ 3D Games - Real-time Rend…ng & Software Technology / 3D Games - Real-time Rendering & Software Technology.iso / flysdk / frontend / flyFrontend / flyFrontend.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-03-01  |  11.2 KB  |  458 lines

  1. #include "../../lib/Fly3D.h"
  2. #include "resource.h"
  3.  
  4. ATOM    MyRegisterClass(HINSTANCE hInstance);
  5. BOOL    InitInstance(HINSTANCE, int);
  6.  
  7. LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
  8. LRESULT CALLBACK    DlgAbout(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam);
  9. LRESULT CALLBACK    DlgVideoModes(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam);
  10.  
  11. int OpenFlyFileDialog();
  12. void CommandLine(char *str);
  13.  
  14. char fly_file_name[256]="";
  15. HCURSOR mousecur=LoadCursor(NULL, IDC_ARROW);
  16. int menuon=0;
  17.  
  18. HINSTANCE hInst;
  19. HWND hWndMain;
  20.  
  21. HMENU hMenu=0;
  22. char szTitle[100]="Fly3D";
  23. char szWindowClass[100]="Fly3Dfe";
  24.                          
  25. int APIENTRY WinMain(HINSTANCE hInstance,
  26.                      HINSTANCE hPrevInstance,
  27.                      LPSTR     lpCmdLine,
  28.                      int       nCmdShow)
  29. {
  30.     if (hPrevInstance)
  31.         return 0;
  32.  
  33.     hInst=hInstance;
  34.  
  35.     MSG msg;
  36.     HACCEL hAccelTable;
  37.  
  38.     MyRegisterClass(hInstance);
  39.  
  40.     if (!InitInstance(hInstance, nCmdShow)) 
  41.         return FALSE;
  42.  
  43.     hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDR_FLY_ACCEL);
  44.  
  45.     PostMessage(hWndMain,WM_USER,0,0);
  46.  
  47.     CommandLine(lpCmdLine);
  48.  
  49.     while (1) 
  50.         {
  51.         while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) == TRUE) 
  52.             {
  53.             if (GetMessage(&msg, NULL, 0, 0)) 
  54.                 {
  55.                 if (flyengine)
  56.                     if ( (msg.message==WM_CHAR && flyengine->con.mode && msg.wParam!=VK_ESCAPE) || 
  57.                          (msg.message==WM_KEYDOWN && msg.wParam==VK_ESCAPE))
  58.                             flyengine->con.key_press(msg.wParam);
  59.                 if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
  60.                     {
  61.                     TranslateMessage(&msg);
  62.                     DispatchMessage(&msg);
  63.                     }
  64.                 }
  65.             else 
  66.                 return TRUE;
  67.             }
  68.         
  69.         if (rend && flyengine)
  70.             {
  71.             if (flyengine->step())
  72.                 rend->DrawView();
  73.             SetClassLong(hWndMain,GCL_HCURSOR,(long)(flyengine->mouse?mousecur:0));
  74.             }
  75.         }
  76. }
  77.  
  78. ATOM MyRegisterClass(HINSTANCE hInst)
  79. {
  80.     WNDCLASS wcex;
  81.  
  82.     wcex.style            = CS_HREDRAW | CS_VREDRAW;
  83.     wcex.lpfnWndProc    = (WNDPROC)WndProc;
  84.     wcex.cbClsExtra        = 0;
  85.     wcex.cbWndExtra        = 0;
  86.     wcex.hInstance        = hInst;
  87.     wcex.hIcon            = LoadIcon(hInst, (LPCTSTR)IDI_FLY);
  88.     wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
  89.     wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
  90.     wcex.lpszMenuName    = (LPCSTR)IDM_FLY;
  91.     wcex.lpszClassName    = szWindowClass;
  92.  
  93.     return RegisterClass(&wcex);
  94. }
  95.  
  96. BOOL InitInstance(HINSTANCE hInst, int nCmdShow)
  97. {
  98.    hWndMain = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN|WS_CLIPSIBLINGS,
  99.       CW_USEDEFAULT, 0, 256, 256, NULL, NULL, hInst, NULL);
  100.    if (!hWndMain)
  101.       return FALSE;
  102.  
  103.    hMenu=GetMenu(hWndMain);
  104.  
  105.    ShowWindow(hWndMain, nCmdShow);
  106.    UpdateWindow(hWndMain);
  107.  
  108.    return TRUE;
  109. }
  110.  
  111. void WndCommand(int wmId,int wmEvent)
  112. {
  113.     switch (wmId)
  114.     {
  115.     case ID_SAVESCR:
  116.         if (rend)
  117.         {
  118.             static int count=1;
  119.  
  120.             picture p;
  121.             p.CreatePicture24(screen_sx,screen_sy);
  122.  
  123.             glPixelTransferf(GL_RED_SCALE,1);
  124.             glPixelTransferf(GL_GREEN_SCALE,1);
  125.             glPixelTransferf(GL_BLUE_SCALE,1);
  126.             glReadPixels(0,0,p.sx,p.sy,GL_RGB,GL_UNSIGNED_BYTE,p.buf[0]);
  127.             glPixelTransferf(GL_RED_SCALE,brightness);
  128.             glPixelTransferf(GL_GREEN_SCALE,brightness);
  129.             glPixelTransferf(GL_BLUE_SCALE,brightness);
  130.  
  131.             unsigned char *buf=new unsigned char[p.sx*3];
  132.             for( int i=0;i<p.sy/2;i++ )
  133.             {
  134.                 memcpy( buf,p.buf[i],p.sx*3 );
  135.                 memcpy( p.buf[i],p.buf[p.sy-i-1],p.sx*3 );
  136.                 memcpy( p.buf[p.sy-i-1],buf,p.sx*3 );
  137.             }
  138.             delete buf;
  139.  
  140.             char str[256];
  141.             strcpy(str,flyengine->flydatapath);
  142.             sprintf(&str[strlen(str)],"screen%03i.tga",count);
  143.             p.SaveTGA(str);
  144.             
  145.             sprintf(str,"SAVED 'SCREEN%03i.TGA'",count);
  146.             flyengine->set_status_msg(str);
  147.             count++;
  148.         }
  149.         break;
  150.     case ID_VIDEOMODE:
  151.         DialogBox(hInst,MAKEINTRESOURCE(IDD_VIDEOMODES),0,(DLGPROC)DlgVideoModes);
  152.         break;
  153.     case ID_FULLSCREEN:
  154.         fullscreen=!fullscreen;
  155.         rend->SetFullScreen();
  156.         if (fullscreen)
  157.             SetMenu(hWndMain,0);
  158.         else SetMenu(hWndMain,hMenu);
  159.         break;
  160.     case ID_MENU:
  161.         if (strcmp(flyengine->flyfile,"menu/menu.fly"))
  162.             flyengine->con.command_exec("map menu/menu.fly");
  163.         break;
  164.     case ID_FOG:
  165.         flyengine->fog=!flyengine->fog;
  166.         flyengine->set_status_msg(flyengine->fog?"FOG ON":"FOG OFF");
  167.         break;
  168.     case ID_WIREFRAME:
  169.         flyengine->wireframe=!flyengine->wireframe;
  170.         flyengine->set_status_msg(flyengine->wireframe?"WIRE ON":"WIRE OFF");
  171.         break;
  172.     case ID_NODEONLY:
  173.         flyengine->nodeonly=!flyengine->nodeonly;
  174.         flyengine->set_status_msg(flyengine->nodeonly?"NODEONLY ON":"NODEONLY OFF");
  175.         break;
  176.     case ID_PVS:
  177.         flyengine->pvsoff=!flyengine->pvsoff;
  178.         flyengine->set_status_msg(flyengine->pvsoff?"PVS OFF":"PVS ON");
  179.         break;
  180.     case ID_TEXTUREINTERPOLATION:
  181.         textfilter=!textfilter;
  182.         flyengine->set_status_msg(textfilter?"FILTERING ON":"FILTERING OFF");
  183.         flyengine->close_texture_cache();
  184.         flyengine->init_texture_cache();
  185.         break;
  186.     case ID_STATUS:
  187.         flyengine->status=!flyengine->status;
  188.         break;
  189.     case ID_CLEARBKGND:
  190.         flyengine->clearbk=!flyengine->clearbk;
  191.         flyengine->set_status_msg(flyengine->clearbk?"CLEAR BKGND ON":"CLEAR BKGND OFF");
  192.         break;
  193.     case ID_MULTITEXTURE:
  194.         if (ntextureunits>1)
  195.             {
  196.             flyengine->multitexture=!flyengine->multitexture;
  197.             flyengine->set_status_msg(flyengine->multitexture?"MULTITEXT ON":"MULTITEXT OFF");
  198.             }
  199.         break;
  200.     case ID_MAPMODE:
  201.         flyengine->mapmode=(flyengine->mapmode+1)%MAPPING_OPTIONS;
  202.         flyengine->set_status_msg("MAPMODE %i",flyengine->mapmode);
  203.         break;
  204.     case ID_MIPMAP:
  205.         mipmap=!mipmap;
  206.         flyengine->set_status_msg(mipmap?"MIPMAP ON":"MIPMAP OFF");
  207.         flyengine->close_texture_cache();
  208.         flyengine->init_texture_cache();
  209.         break;
  210.     case ID_MUTE:
  211.         flyengine->mute=!flyengine->mute;
  212.         flyengine->set_status_msg(flyengine->mute?"MUTE ON":"MUTE OFF");
  213.         break;
  214.     case ID_ABOUT:
  215.         DialogBox(hInst,MAKEINTRESOURCE(IDD_ABOUTBOX),0,(DLGPROC)DlgAbout);
  216.         break;
  217.     case ID_FILE_OPEN:
  218.         flyengine->close_multiplayer();
  219.         flyengine->close_fly_file();
  220.         InvalidateRect(hWndMain,0,0);
  221.         if (OpenFlyFileDialog())
  222.         {
  223.             char str[256];
  224.             strcpy(str,flyengine->flysdkpath);
  225.             strcat(str,"data\\");
  226.             strlwr(fly_file_name);
  227.             if (strncmp(fly_file_name,str,strlen(str))==0)
  228.                 flyengine->open_fly_file(&fly_file_name[strlen(str)]);
  229.             if (flyengine->flyfile[0]==0)
  230.                 {
  231.                 strcpy(str,"Could not load file:\n");
  232.                 strcat(str,fly_file_name);
  233.                 MessageBox(hWndMain,str,"Fly3D",MB_OK|MB_ICONEXCLAMATION);
  234.                 }
  235.         }
  236.         InvalidateRect(hWndMain,0,0);
  237.         flyengine->set_status_msg("map: %s",flyengine->flyfile[0]==0?"(none)":flyengine->flyfile);
  238.         break;
  239.     case ID_MULTIPLAYER:
  240.         flyengine->close_multiplayer();
  241.         flyengine->close_fly_file();
  242.         InvalidateRect(hWndMain,0,0);
  243.         flyengine->join_multiplayer();
  244.         InvalidateRect(hWndMain,0,0);
  245.         flyengine->set_status_msg("map: %s",flyengine->flyfile[0]==0?"(none)":flyengine->flyfile);
  246.         break;
  247.     case ID_FILE_CLOSE:
  248.         flyengine->close_multiplayer();
  249.         flyengine->close_fly_file();
  250.         InvalidateRect(hWndMain,0,0);
  251.         flyengine->set_status_msg("map: %s",flyengine->flyfile[0]==0?"(none)":flyengine->flyfile);
  252.         break;
  253.     case ID_EXIT:
  254.         DestroyWindow(hWndMain);
  255.         break;
  256.     }
  257. }
  258.  
  259. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  260. {
  261.     static PAINTSTRUCT ps;
  262.     static HDC hdc;
  263.     static RECT rect;
  264.  
  265.     switch (message) 
  266.     {
  267.         case WM_USER:
  268.             {
  269.             init_engine(hWnd,hInst,FLYAPPID_FLY);
  270.             init_directx();
  271.             init_render(FLY_RENDER_OPENGL);
  272.  
  273.             if (fly_file_name[0])
  274.             {
  275.                 flyengine->open_fly_file(fly_file_name);
  276.                 if (flyengine->flyfile[0]==0)
  277.                     {
  278.                     char str[256];
  279.                     strcpy(str,"Could not load file:\n");
  280.                     strcat(str,fly_file_name);
  281.                     MessageBox(hWndMain,str,"Fly3D",MB_OK|MB_ICONEXCLAMATION);
  282.                     }
  283.             }
  284.  
  285.             if (fullscreen)
  286.             {
  287.                 fullscreen=0;
  288.                 PostMessage(hWnd,WM_COMMAND,ID_FULLSCREEN,0);
  289.             }
  290.  
  291.             InvalidateRect(hWndMain,0,0);
  292.             }
  293.             break;
  294.         case WM_COMMAND:
  295.             WndCommand(LOWORD(wParam),HIWORD(wParam));
  296.             break;
  297.         case WM_DESTROY:
  298.             if (flyengine)
  299.                 {
  300.                 flyengine->close_multiplayer();
  301.                 flyengine->close_fly_file();
  302.                 }
  303.             free_engine();
  304.             free_render();
  305.             free_directx();
  306.             PostQuitMessage(0);
  307.             break;
  308.         case WM_SIZE:
  309.             if (rend)
  310.                 rend->ResizeView(LOWORD(lParam),HIWORD(lParam));
  311.             break;
  312.         case WM_ACTIVATE:
  313.             if (flyengine)
  314.                 if (LOWORD(wParam)==WA_INACTIVE || flyengine->con.mode)
  315.                     flyengine->noinput=1;
  316.                 else flyengine->noinput=0;
  317.             break;
  318.         case WM_SETCURSOR:
  319.             if (menuon==1)
  320.                 SetCursor(mousecur);
  321.             else if (flyengine)
  322.                     SetCursor(flyengine->mouse?mousecur:0);
  323.             break;
  324.         case WM_ENTERMENULOOP:
  325.             InvalidateRect(hWnd,0,0);
  326.             SetCursor(mousecur);
  327.             flyengine->noinput=1;
  328.             menuon=1;
  329.             break;
  330.         case WM_EXITMENULOOP:
  331.             if (flyengine->con.mode==0)
  332.                 flyengine->noinput=0;
  333.             menuon=0;
  334.             break;
  335.         case WM_PAINT:
  336.             hdc = BeginPaint(hWnd, &ps);
  337.             EndPaint(hWnd, &ps);
  338.             break;
  339.         case WM_ERASEBKGND:
  340.             {
  341.             if (rend==0 || flyengine==0 || flyengine->bsp==0)
  342.                 {
  343.                 RECT rect;
  344.                 GetClientRect(hWnd,&rect);
  345.                 FillRect((HDC)wParam,&rect,(HBRUSH)GetStockObject(BLACK_BRUSH));
  346.                 }
  347.             return 1;
  348.             }
  349.         default:
  350.             return DefWindowProc(hWnd, message, wParam, lParam);
  351.    }
  352.    return 0;
  353. }
  354.  
  355. LRESULT CALLBACK DlgAbout(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam)
  356. {
  357.     switch(Msg)
  358.     {
  359.     case WM_CLOSE:
  360.         EndDialog(hWnd,IDCANCEL);
  361.         break;
  362.     case WM_COMMAND:
  363.         switch(wParam)
  364.         {
  365.         case IDOK:
  366.             EndDialog(hWnd,IDOK);
  367.             break;
  368.         }
  369.         break;
  370.     }
  371.     return 0;
  372. }
  373.  
  374. LRESULT CALLBACK DlgVideoModes(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam)
  375. {
  376.     switch(Msg)
  377.     {
  378.     case WM_INITDIALOG:
  379.         {
  380.             int i;
  381.             char str[256];
  382.             for( i=0;i<numvideomodes;i++ )
  383.             {
  384.                 if (videomodes[i].dmDisplayFrequency!=0)
  385.                     sprintf(str,"%ix%i %i bits/pixel %i hz",
  386.                         videomodes[i].dmPelsWidth,videomodes[i].dmPelsHeight,videomodes[i].dmBitsPerPel,videomodes[i].dmDisplayFrequency);
  387.                 else 
  388.                     sprintf(str,"%ix%i %i bits/pixel",
  389.                         videomodes[i].dmPelsWidth,videomodes[i].dmPelsHeight,videomodes[i].dmBitsPerPel);
  390.                 SendDlgItemMessage(hWnd,IDC_VIDEOMODELIST,LB_ADDSTRING,0,(LPARAM)str);
  391.             }
  392.             SendDlgItemMessage(hWnd,IDC_VIDEOMODELIST,LB_SETCURSEL,selvideomode,0);
  393.         }
  394.         break;
  395.     case WM_CLOSE:
  396.         EndDialog(hWnd,IDCANCEL);
  397.         break;
  398.     case WM_COMMAND:
  399.         switch(wParam)
  400.         {
  401.         case IDCANCEL:
  402.             EndDialog(hWnd,IDCANCEL);
  403.             break;
  404.         case IDOK:
  405.             selvideomode=SendDlgItemMessage(hWnd,IDC_VIDEOMODELIST,LB_GETCURSEL,0,0);
  406.             EndDialog(hWnd,IDOK);
  407.             break;
  408.         }
  409.         break;
  410.     }
  411.     return 0;
  412. }
  413.  
  414. void CommandLine(char *str)
  415. {
  416.     char *c1=str,*c2;
  417.     while(c1)
  418.     {
  419.         c2=strchr(c1,' ');
  420.         if (c2) 
  421.             *c2=0;
  422.         if (c1[0]!='/' && c1[0]!='-')
  423.             strcpy(fly_file_name,c1);
  424.         else
  425.         if (!stricmp(&c1[1],"fullscreen"))
  426.             fullscreen=1;
  427.         if (c2)
  428.             c1=c2+1;
  429.         else c1=0;
  430.     }
  431. }
  432.  
  433. int OpenFlyFileDialog()
  434. {
  435.     OPENFILENAME ofn;
  436.  
  437.     memset(&ofn,0,sizeof(OPENFILENAME));
  438.     char filename[256]="";
  439.  
  440.     ofn.lStructSize=sizeof(OPENFILENAME);
  441.     ofn.hwndOwner=hWndMain;
  442.     ofn.hInstance=hInst;
  443.     ofn.lpstrFilter="Fly scene (*.fly)\0*.fly\0";
  444.     ofn.lpstrDefExt="fly";
  445.     ofn.lpstrFile=filename;
  446.     ofn.nMaxFile=255;
  447.     ofn.lpstrTitle="Open Fly3D Scene";
  448.     ofn.Flags=OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST;
  449.  
  450.     if (GetOpenFileName(&ofn))
  451.     {
  452.         strcpy(fly_file_name,filename);
  453.         if (filename[0])
  454.             return 1;
  455.     }
  456.     return 0;
  457. }
  458.