home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / developer_install / ReplicaNetFreewareV5_4.exe / data1.cab / Program_Executable_Files / Example9 / Network.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-30  |  4.6 KB  |  194 lines

  1. /* START_LICENSE_HEADER
  2.  
  3. Copyright (C) 2000 Martin Piper, original design and program code
  4. Copyright (C) 2001-2005 Replica Software
  5.  
  6. This program file is copyright (C) Replica Software and can only be used under license.
  7. For more information visit: http://www.replicanet.com/
  8. Or email: info@replicanet.com
  9.  
  10. END_LICENSE_HEADER */
  11. /*
  12. This allocates a new ReplicaNet session and also handles the dialog displayed at the start of the application.
  13. The dialog displays a list of found game session for the user to join or allows a new game session to be created.
  14. */
  15. #include <windows.h>
  16. #include "Network.h"
  17. #include "Resource.h"
  18.  
  19. using namespace RNReplicaNet;
  20.  
  21. extern HINSTANCE ghInst;
  22. ReplicaNet *gNetwork;
  23.  
  24. static bool failed_to = true;
  25.  
  26. static void CreateGame(void)
  27. {
  28.     std::string url;
  29.  
  30. //    gNetwork->SetGameChannel(2000);
  31.     gNetwork->SessionCreate("Example9");
  32.     url = gNetwork->SessionExportURL();
  33.     OutputDebugString(url.c_str());
  34.     OutputDebugString("\n");
  35. }
  36.  
  37. void Network_Init(std::string url)
  38. {
  39.     // Allocate a new ReplicaNet session
  40.     gNetwork = new ReplicaNet;
  41.     gNetwork->SetCanBecomeMaster(false);
  42.     gNetwork->SetCanAcceptObjects(false);
  43.     // Turn on manual Poll() updates for the session to give us slightly better network prediction performance
  44.     gNetwork->SetManualPoll();
  45.  
  46.  
  47.     // Did we have a URL on the command line?
  48.     if (url != "")
  49.     {
  50.         // Try the URL on the command line
  51.         gNetwork->SessionJoin(url);
  52.     }
  53.     else
  54.     {
  55.         // display the network picking dialog
  56.         Network_Dialog();
  57.  
  58.         if (failed_to)
  59.         {
  60.             gNetwork->SessionFind();
  61.             // Wait a while
  62.             Sleep(500);
  63.             url = gNetwork->SessionEnumerateFound();
  64.  
  65.             if (url.find("Example9") == std::string::npos)
  66.             {
  67.                 url = "";
  68.             }
  69.  
  70.             if (url != "")
  71.             {
  72.                 char buffer[256];
  73.                 sprintf(buffer,"Trying to join %s\n",url.c_str());
  74.                 OutputDebugString(buffer);
  75.                 gNetwork->SessionJoin(url);
  76.             }
  77.             else
  78.             {
  79.                 CreateGame();
  80.             }
  81.         }
  82.     }
  83. }
  84.  
  85. BOOL CALLBACK Dlg_JoinOrNew(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
  86. {
  87.     std::string url;
  88.  
  89.     switch (msg)
  90.     {
  91.         case WM_INITDIALOG:
  92.             SetTimer(hwndDlg,1,500,0);
  93.             SendMessage(GetDlgItem(hwndDlg, IDC_LIST2), LB_RESETCONTENT, 0, 0);
  94.  
  95.             SendMessage(hwndDlg,WM_COMMAND,IDC_REFRESH,0);
  96.  
  97. //            SetWindowText (GetDlgItem (hwndDlg, IDC_NEWMAPBLOCKWIDTH), textbuf);
  98.             SetFocus(GetDlgItem(hwndDlg,IDC_NEW));
  99.             return 0;
  100.             break;
  101.         case WM_TIMER:
  102.             do
  103.             {
  104.                 url = gNetwork->SessionEnumerateFound();
  105.                 if (url.find("Example9") == std::string::npos)
  106.                 {
  107.                     url = "";
  108.                 }
  109.                 if (url != "")
  110.                 {
  111.                     SendMessage(GetDlgItem(hwndDlg, IDC_LIST2), LB_ADDSTRING, 0, (LPARAM) url.c_str());
  112.                 }
  113.             } while (url != "");
  114.             break;
  115.         case WM_COMMAND:
  116.         switch (LOWORD(wParam))
  117.         {
  118.             case IDC_LIST2:
  119.             {
  120.                 int sel = SendMessage(GetDlgItem(hwndDlg, IDC_LIST2), LB_GETCURSEL,0,0);
  121.                 if (sel != LB_ERR)
  122.                 {
  123.                     char buffer[256];
  124.                     int ret = SendMessage(GetDlgItem(hwndDlg, IDC_LIST2), LB_GETTEXT,sel,(LPARAM) buffer);
  125.                     if (ret != LB_ERR)
  126.                     {
  127.                         SetWindowText(GetDlgItem(hwndDlg,IDC_EDIT1),buffer);
  128.                     }
  129.                     SendMessage(GetDlgItem(hwndDlg, IDC_LIST2), LB_SETCURSEL,sel,0);
  130.  
  131.                 }
  132.                 break;
  133.             }
  134.             case IDC_EDIT1:
  135.                 SendMessage(GetDlgItem(hwndDlg, IDC_LIST2), LB_SETCURSEL, -1, 0);
  136.                 break;
  137.             case IDC_REFRESH:
  138.                 SendMessage(GetDlgItem(hwndDlg, IDC_LIST2), LB_RESETCONTENT, 0, 0);
  139.                 gNetwork->SessionFind();
  140.                 break;
  141.             case IDC_JOIN:
  142.             {
  143.                 int sel = SendMessage(GetDlgItem(hwndDlg, IDC_LIST2), LB_GETCURSEL,0,0);
  144.                 if (sel != LB_ERR)
  145.                 {
  146.                     char buffer[256];
  147.                     int ret = SendMessage(GetDlgItem(hwndDlg, IDC_LIST2), LB_GETTEXT,sel,(LPARAM) buffer);
  148.                     if (ret != LB_ERR)
  149.                     {
  150.                         gNetwork->SessionJoin(std::string(buffer));
  151.                         failed_to = false;
  152.                         SendMessage (hwndDlg, WM_CLOSE, 0, -1L);
  153.                         break;
  154.                     }
  155.                 }
  156.                 char buffer[256];
  157.                 int ret = GetWindowText(GetDlgItem(hwndDlg,IDC_EDIT1),buffer,sizeof(buffer));
  158.                 if (ret > 0)
  159.                 {
  160.                     gNetwork->SessionJoin(std::string(buffer));
  161.                     failed_to = false;
  162.                     SendMessage (hwndDlg, WM_CLOSE, 0, -1L);
  163.                     break;
  164.                 }
  165.                 break;
  166.             }
  167.             case IDC_NEW:
  168.             case IDOK:
  169.                 CreateGame();
  170.  
  171.                 failed_to = false;
  172.                 SendMessage (hwndDlg, WM_CLOSE, 0, -1L);
  173.                 break;
  174.             case IDCANCEL:
  175.                 SendMessage (hwndDlg, WM_CLOSE, 0, -1L);
  176.                 break;
  177.         }
  178.         break;
  179.         case WM_CLOSE:
  180.             EndDialog (hwndDlg, lParam);
  181.             KillTimer(hwndDlg,1);
  182.             break;
  183.         default:
  184.             return 0;
  185.     }
  186.  
  187.     return 1;
  188. }
  189.  
  190. void Network_Dialog(void)
  191. {
  192.     DialogBox(ghInst,MAKEINTRESOURCE(IDD_JOINORNEW),0,&Dlg_JoinOrNew);
  193. }
  194.