SHARE
TWEET

Untitled

a guest Nov 9th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /****************************************************
  2.     Virtual Collaborative Teams - The base program
  3.     The main module
  4.     ****************************************************/
  5.  
  6. #include <windows.h>
  7. #include <math.h>
  8. #include <time.h>
  9. #include <gl\gl.h>
  10. #include <gl\glu.h>
  11. #include <iterator>
  12. #include <map>
  13.  
  14. #include "objects.h"
  15. #include "graphics.h"
  16. #include "net.h"
  17. using namespace std;
  18.  
  19. FILE *f = fopen("vct_log.txt", "w");               // log file handle
  20.  
  21.  
  22. MovableObject *my_vehicle;                         // vehicle controlled in this application
  23. Environment env;                                   // environment: terrain, things (trees, coins etc.)
  24.  
  25. map<int, MovableObject*> movable_objects;          // objects from other applications indexed by iID  
  26.  
  27. float fDt;                                         // average time between two consecutive cycles of world (simulation + rendering)
  28.                                                    // is used to synchronize simulated phisical time in each application despite of
  29.                                                    // hardware or processes number differences between computers
  30. long time_of_cycle, number_of_cyc;                 // variables supported fDt calculation
  31.  
  32.  
  33. unicast_net *uni_reciv;                        // object (see net module) to recive messages from other applications
  34. unicast_net *uni_send;                         // ...  to send messages ...
  35.  
  36. HWND main_window;                                  // a handle to main window of application
  37. HANDLE threadReciv;                                // a handle of thread for reciving messages and actions execution depend on message type
  38. bool if_SHIFT_pressed = false;
  39. bool if_ID_visible = true;                        
  40. bool if_mouse_control = false;                     // vehicle control by mouse
  41. int mouse_cursor_x = 0, mouse_cursor_y = 0;        // position of mouse cursor
  42.  
  43. extern ViewParams viewpar;                         // view parameters (camera position, camera angle, etc)
  44.  
  45.  
  46. struct Frame                                       // The main structure of net communication between aplications. Homogenous for simpicity.
  47. {
  48.     int type;                                      // frame type  
  49.     int iID;                                       // object identifier
  50.     ObjectState state;                             // object state values (see object module)
  51.    
  52.     //long send_time;                                // time stamp
  53.     //int iID_receiver;                              // not used
  54. };
  55.  
  56.  
  57. //******************************************************
  58. // The function of handling the message receiving thread
  59. DWORD WINAPI ReceiveThreadFun(void *ptr)
  60. {
  61.     unicast_net *pmt_net = (unicast_net*)ptr;  // the pointer to the object of multicast_net class (see net module)
  62.     Frame frame;
  63.  
  64.  
  65.     //192.168.1.124 adres Jonasza
  66.    
  67.     while (1)
  68.     {
  69.         char strAddr[] = "192.168.1.124";
  70.         unsigned long ip = inet_addr(strAddr);
  71.         int frame_size = pmt_net->reciv((char*)&frame,&ip, sizeof(Frame));   // waiting for frame
  72.         ObjectState state = frame.state;
  73.  
  74.         fprintf(f, "received state of object with iID = %d\n", frame.iID);
  75.  
  76.         if (frame.iID != my_vehicle->iID)                     // if the frame isn't my own frame
  77.         {
  78.             if (movable_objects[frame.iID] == NULL)           // object hasn't registered up till now
  79.                                         // == nullptr (C++ 11) it is not available in older versions than VC 2013
  80.             {
  81.                 MovableObject *ob = new MovableObject();
  82.                 ob->iID = frame.iID;
  83.                 movable_objects[frame.iID] = ob;              // registration of new object
  84.                  
  85.                 //fprintf(f, "alien object ID = %d was registred\n", ob->iID);
  86.             }
  87.             movable_objects[frame.iID]->ChangeState(state);   // updating the state of the object
  88.  
  89.         }
  90.     }  // while(1)
  91.     return 1;
  92. }
  93.  
  94. // *******************************************************************************
  95. // ****    All events which should be initialized once at the start of application  
  96. void InteractionInitialisation()
  97. {
  98.     DWORD dwThreadId;
  99.  
  100.     my_vehicle = new MovableObject();    // creating my own object
  101.  
  102.     time_of_cycle = clock();             // current time
  103.  
  104.     // net multicast communication objects with virtual collaborative teams IP and port number
  105.     uni_reciv = new unicast_net(10001);      // object for receiving messages
  106.     uni_send = new unicast_net(10001);       // object for sending messages
  107.  
  108.  
  109.     // creating a thread to handling messages from other aplications
  110.     threadReciv = CreateThread(
  111.         NULL,                            // no security attributes
  112.         0,                               // use default stack size
  113.         ReceiveThreadFun,                // thread function
  114.         (void *)uni_reciv,             // argument to thread function
  115.         NULL,                            // use default creation flags
  116.         &dwThreadId);                    // returns the thread identifier
  117.  
  118. }
  119.  
  120.  
  121. // *****************************************************************
  122. // ****    All things (without graphics) to do as frequent as possible due to world cycle
  123. void VirtualWorldCycle()
  124. {
  125.     number_of_cyc++;
  126.  
  127.     // average time - fDt between two consequtive world cycles calculation:
  128.     if (number_of_cyc % 50 == 0)        
  129.     {                              
  130.         char text[256];
  131.         long prev_time = time_of_cycle;
  132.         time_of_cycle = clock();
  133.         float fFps = (50 * CLOCKS_PER_SEC) / (float)(time_of_cycle - prev_time);
  134.         if (fFps != 0) fDt = 1.0 / fFps; else fDt = 1;
  135.  
  136.         sprintf(text, "VCT-lab 2018/19 topic 1, version g (%0.0f fps  %0.2fms) ", fFps, 1000.0 / fFps);
  137.         SetWindowText(main_window, text);          
  138.     }
  139.  
  140.     my_vehicle->Simulation(fDt);                      // simulation of own object based of previous state, forces and
  141.                                                       // other actions and fDt - average time elapsed since the last simulation
  142.     Frame frame;
  143.     frame.state = my_vehicle->State();                // state of my own object
  144.     frame.iID = my_vehicle->iID;                      // my object identifier
  145.  
  146.     //char strAddr[] = "192.168.1.120";
  147.     char strAddr[] = "192.168.1.124";
  148.     unsigned long ip = inet_addr(strAddr);
  149.     uni_send->send((char*)&frame,ip, sizeof(Frame));   // sending a message to other application
  150. }
  151.  
  152. // *****************************************************************
  153. // ****    All things to do at the end of aplication
  154. void EndOfInteraction()
  155. {
  156.     fprintf(f, "End of interaction\n");
  157.     fclose(f);
  158. }
  159.  
  160. // window messages handling function - the prototype
  161. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  162.  
  163.  
  164. HDC g_context = NULL;               // a handel of graphical context
  165.  
  166.  
  167.  
  168. //Window main thread function
  169. int WINAPI WinMain(HINSTANCE hInstance,
  170.     HINSTANCE hPrevInstance,
  171.     LPSTR     lpCmdLine,
  172.     int       nCmdShow)
  173. {
  174.     MSG message;         
  175.     WNDCLASS main_class;
  176.  
  177.     static char class_name[] = "Main Class";
  178.  
  179.     main_class.style = CS_HREDRAW | CS_VREDRAW;
  180.     main_class.lpfnWndProc = WndProc;
  181.     main_class.cbClsExtra = 0;
  182.     main_class.cbWndExtra = 0;
  183.     main_class.hInstance = hInstance;
  184.     main_class.hIcon = 0;
  185.     main_class.hCursor = LoadCursor(0, IDC_ARROW);
  186.     main_class.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
  187.     main_class.lpszMenuName = "Menu";
  188.     main_class.lpszClassName = class_name;
  189.  
  190.     RegisterClass(&main_class);
  191.  
  192.     main_window = CreateWindow(class_name, "VCT-lab 2018/19 topic 1 - Network Architectures, version g", WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
  193.         100, 50, 750, 850, NULL, NULL, hInstance, NULL);
  194.  
  195.  
  196.     ShowWindow(main_window, nCmdShow);
  197.  
  198.     UpdateWindow(main_window);
  199.  
  200.     ZeroMemory(&message, sizeof(message));
  201.     while (message.message != WM_QUIT)
  202.     {
  203.         if (PeekMessage(&message, NULL, 0U, 0U, PM_REMOVE))
  204.         {
  205.             TranslateMessage(&message);
  206.             DispatchMessage(&message);
  207.         }
  208.         else
  209.         {
  210.             VirtualWorldCycle();    
  211.             InvalidateRect(main_window, NULL, FALSE);
  212.         }
  213.     }
  214.  
  215.     return (int)message.wParam;
  216. }
  217.  
  218. //********************************************************************
  219. // Operating system message handling function:
  220. LRESULT CALLBACK WndProc(HWND main_window, UINT message_code, WPARAM wParam, LPARAM lParam)
  221. {
  222.  
  223.     switch (message_code)
  224.     {
  225.     case WM_CREATE:  // message which is sent when window is creating
  226.     {
  227.  
  228.         g_context = GetDC(main_window);
  229.  
  230.         srand((unsigned)time(NULL));
  231.         int result = GraphicsInitialisation(g_context);
  232.         if (result == 0)
  233.         {
  234.             printf("cannot open graphical window!\n");
  235.             //exit(1);
  236.         }
  237.  
  238.         InteractionInitialisation();
  239.  
  240.         SetTimer(main_window, 1, 10, NULL);
  241.  
  242.         return 0;
  243.     }
  244.  
  245.  
  246.     case WM_PAINT:
  247.     {
  248.         PAINTSTRUCT paint;
  249.         HDC context;
  250.         context = BeginPaint(main_window, &paint);
  251.  
  252.         DrawScene();
  253.         SwapBuffers(context);
  254.  
  255.         EndPaint(main_window, &paint);
  256.  
  257.         return 0;
  258.     }
  259.  
  260.     case WM_TIMER:
  261.  
  262.         return 0;
  263.  
  264.     case WM_SIZE:
  265.     {
  266.         int cx = LOWORD(lParam);
  267.         int cy = HIWORD(lParam);
  268.  
  269.         WindowResize(cx, cy);
  270.  
  271.         return 0;
  272.     }
  273.  
  274.     case WM_DESTROY:    // window close message
  275.  
  276.         EndOfInteraction();
  277.         EndOfGraphics();
  278.  
  279.         ReleaseDC(main_window, g_context);
  280.         KillTimer(main_window, 1);
  281.  
  282.         DWORD ExitCode;
  283.         GetExitCodeThread(threadReciv, &ExitCode);
  284.         TerminateThread(threadReciv, ExitCode);
  285.  
  286.         PostQuitMessage(0);
  287.         return 0;
  288.  
  289.     case WM_LBUTTONDOWN: // mouse left button press message
  290.     {
  291.         int x = LOWORD(lParam);
  292.         int y = HIWORD(lParam);
  293.         if (if_mouse_control)
  294.             my_vehicle->F = 30.0;        // the force pushing vehicle forward
  295.         break;
  296.     }
  297.     case WM_RBUTTONDOWN:
  298.     {
  299.         int x = LOWORD(lParam);
  300.         int y = HIWORD(lParam);
  301.         if (if_mouse_control)
  302.             my_vehicle->F = -5.0;        // the force pushing vehicle backward
  303.         break;
  304.     }
  305.     case WM_MBUTTONDOWN:
  306.     {
  307.         if_mouse_control = 1 - if_mouse_control;
  308.         if (if_mouse_control) my_vehicle->if_keep_steer_wheel = true;
  309.         else my_vehicle->if_keep_steer_wheel = false;
  310.  
  311.         mouse_cursor_x = LOWORD(lParam);
  312.         mouse_cursor_y = HIWORD(lParam);
  313.         break;
  314.     }
  315.     case WM_LBUTTONUP:
  316.     {
  317.         if (if_mouse_control)
  318.             my_vehicle->F = 0.0;        
  319.         break;
  320.     }
  321.     case WM_RBUTTONUP:
  322.     {
  323.         if (if_mouse_control)
  324.             my_vehicle->F = 0.0;      
  325.         break;
  326.     }
  327.     case WM_MOUSEMOVE:
  328.     {
  329.         int x = LOWORD(lParam);
  330.         int y = HIWORD(lParam);
  331.         if (if_mouse_control)
  332.         {
  333.             float wheel_angle = (float)(mouse_cursor_x - x) / 20;
  334.             if (wheel_angle > 60) wheel_angle = 60;
  335.             if (wheel_angle < -60) wheel_angle = -60;
  336.             my_vehicle->state.steering_angle = PI*wheel_angle / 180;
  337.         }
  338.         break;
  339.     }
  340.     case WM_KEYDOWN:
  341.     {
  342.  
  343.         switch (LOWORD(wParam))
  344.         {
  345.         case VK_SHIFT:
  346.         {
  347.             if_SHIFT_pressed = 1;
  348.             break;
  349.         }
  350.         case VK_SPACE:
  351.         {
  352.             my_vehicle->breaking_factor = 1.0;       // breaking degree factor
  353.             break;                                   // if 1.0 then wheels are blocked
  354.         }
  355.         case VK_UP:
  356.         {
  357.             my_vehicle->F = 50.0;                    // the force pushing vehicle forward
  358.             break;
  359.         }
  360.         case VK_DOWN:
  361.         {
  362.             my_vehicle->F = -20.0;
  363.             break;
  364.         }
  365.         case VK_LEFT:
  366.         {
  367.             if (my_vehicle->steer_wheel_speed < 0){
  368.                 my_vehicle->steer_wheel_speed = 0;
  369.                 my_vehicle->if_keep_steer_wheel = true;
  370.             }
  371.             else{
  372.                 if (if_SHIFT_pressed) my_vehicle->steer_wheel_speed = 0.5;
  373.                 else my_vehicle->steer_wheel_speed = 0.25 / 8;
  374.             }
  375.  
  376.             break;
  377.         }
  378.         case VK_RIGHT:
  379.         {
  380.             if (my_vehicle->steer_wheel_speed > 0){
  381.                 my_vehicle->steer_wheel_speed = 0;
  382.                 my_vehicle->if_keep_steer_wheel = true;
  383.             }
  384.             else{
  385.                 if (if_SHIFT_pressed) my_vehicle->steer_wheel_speed = -0.5;
  386.                 else my_vehicle->steer_wheel_speed = -0.25 / 8;
  387.             }
  388.             break;
  389.         }
  390.         case 'I':  
  391.         {
  392.             if_ID_visible = 1 - if_ID_visible;
  393.             break;
  394.         }
  395.         case 'W':  // far view
  396.         {
  397.             if (viewpar.cam_distance > 0.5) viewpar.cam_distance /= 1.2;
  398.             else viewpar.cam_distance = 0;
  399.             break;
  400.         }
  401.         case 'S':   // near viev
  402.         {
  403.             if (viewpar.cam_distance > 0) viewpar.cam_distance *= 1.2;
  404.             else viewpar.cam_distance = 0.5;
  405.             break;
  406.         }
  407.         case 'Q':   // top view
  408.         {
  409.             if (viewpar.tracking) break;
  410.             viewpar.top_view = 1 - viewpar.top_view;
  411.             if (viewpar.top_view)
  412.             {
  413.                 viewpar.cam_pos_1 = viewpar.cam_pos; viewpar.cam_direct_1 = viewpar.cam_direct; viewpar.cam_vertical_1 = viewpar.cam_vertical;
  414.                 viewpar.cam_distance_1 = viewpar.cam_distance; viewpar.cam_angle_1 = viewpar.cam_angle;
  415.                 viewpar.cam_pos = viewpar.cam_pos_2; viewpar.cam_direct = viewpar.cam_direct_2; viewpar.cam_vertical = viewpar.cam_vertical_2;
  416.                 viewpar.cam_distance = viewpar.cam_distance_2; viewpar.cam_angle = viewpar.cam_angle_2;
  417.             }
  418.             else
  419.             {
  420.                 viewpar.cam_pos_2 = viewpar.cam_pos; viewpar.cam_direct_2 = viewpar.cam_direct; viewpar.cam_vertical_2 = viewpar.cam_vertical;
  421.                 viewpar.cam_distance_2 = viewpar.cam_distance; viewpar.cam_angle_2 = viewpar.cam_angle;
  422.                 viewpar.cam_pos = viewpar.cam_pos_1; viewpar.cam_direct = viewpar.cam_direct_1; viewpar.cam_vertical = viewpar.cam_vertical_1;
  423.                 viewpar.cam_distance = viewpar.cam_distance_1; viewpar.cam_angle = viewpar.cam_angle_1;
  424.             }
  425.             break;
  426.         }
  427.         case 'E':  
  428.         {
  429.             viewpar.cam_angle += PI * 5 / 180;
  430.             break;
  431.         }
  432.         case 'D':  
  433.         {
  434.             viewpar.cam_angle -= PI * 5 / 180;
  435.             break;
  436.         }
  437.         case 'A':   // switching on/off object tracking
  438.         {
  439.             viewpar.tracking = 1 - viewpar.tracking;
  440.             if (viewpar.tracking)
  441.             {
  442.                 viewpar.cam_distance = viewpar.cam_distance_3; viewpar.cam_angle = viewpar.cam_angle_3;
  443.             }
  444.             else
  445.             {
  446.                 viewpar.cam_distance_3 = viewpar.cam_distance; viewpar.cam_angle_3 = viewpar.cam_angle;
  447.                 viewpar.top_view = 0;
  448.                 viewpar.cam_pos = viewpar.cam_pos_1; viewpar.cam_direct = viewpar.cam_direct_1; viewpar.cam_vertical = viewpar.cam_vertical_1;
  449.                 viewpar.cam_distance = viewpar.cam_distance_1; viewpar.cam_angle = viewpar.cam_angle_1;
  450.             }
  451.             break;
  452.         }
  453.         case VK_F1:  // help page
  454.         {
  455.             char lan[1024], lan_bie[1024];
  456.             GetCurrentDirectory(1024, lan_bie);
  457.             strcpy(lan, "C:\\Program Files\\Internet Explorer\\iexplore ");
  458.             strcat(lan, lan_bie);
  459.             strcat(lan, "\\help.htm");
  460.             int wyni = WinExec(lan, SW_NORMAL);
  461.             if (wyni < 32)  
  462.             {
  463.                 strcpy(lan, "C:\\Program Files\\Mozilla Firefox\\firefox ");
  464.                 strcat(lan, lan_bie);
  465.                 strcat(lan, "\\help.htm");
  466.                 wyni = WinExec(lan, SW_NORMAL);
  467.                 if (wyni < 32)
  468.                 {
  469.                     char lan_win[1024];
  470.                     GetWindowsDirectory(lan_win, 1024);
  471.                     strcat(lan_win, "\\notepad help.txt ");
  472.                     wyni = WinExec(lan_win, SW_NORMAL);
  473.                 }
  474.             }
  475.             break;
  476.         }
  477.         case VK_ESCAPE:
  478.         {
  479.             SendMessage(main_window, WM_DESTROY, 0, 0);
  480.             break;
  481.         }
  482.         } // switch under keys
  483.  
  484.         break;
  485.     }
  486.     case WM_KEYUP:
  487.     {
  488.         switch (LOWORD(wParam))
  489.         {
  490.         case VK_SHIFT:
  491.         {
  492.             if_SHIFT_pressed = 0;
  493.             break;
  494.         }
  495.         case VK_SPACE:
  496.         {
  497.             my_vehicle->breaking_factor = 0.0;
  498.             break;
  499.         }
  500.         case VK_UP:
  501.         {
  502.             my_vehicle->F = 0.0;
  503.             break;
  504.         }
  505.         case VK_DOWN:
  506.         {
  507.             my_vehicle->F = 0.0;
  508.             break;
  509.         }
  510.         case VK_LEFT:
  511.         {
  512.             my_vehicle->Fb = 0.00;
  513.             //my_vehicle->state.steering_angle = 0;
  514.             if (my_vehicle->if_keep_steer_wheel) my_vehicle->steer_wheel_speed = -0.25/8;
  515.             else my_vehicle->steer_wheel_speed = 0;
  516.             my_vehicle->if_keep_steer_wheel = false;
  517.             break;
  518.         }
  519.         case VK_RIGHT:
  520.         {
  521.             my_vehicle->Fb = 0.00;
  522.             //my_vehicle->state.steering_angle = 0;
  523.             if (my_vehicle->if_keep_steer_wheel) my_vehicle->steer_wheel_speed = 0.25 / 8;
  524.             else my_vehicle->steer_wheel_speed = 0;
  525.             my_vehicle->if_keep_steer_wheel = false;
  526.             break;
  527.         }
  528.  
  529.         }
  530.  
  531.         break;
  532.     }
  533.  
  534.     default:
  535.         return DefWindowProc(main_window, message_code, wParam, lParam);
  536.     }
  537.  
  538.  
  539. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top