mamamaria

Bus

Apr 17th, 2021 (edited)
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.38 KB | None | 0 0
  1. // DrawingShuttle.cpp : Определяет точку входа для приложения.
  2. //
  3. #include <thread>
  4. #include <mutex>
  5. #include "framework.h"
  6. #include "DrawingShuttle.h"
  7. #include <cmath>
  8. #include <string>
  9. #include <ctime>
  10. #define MAX_LOADSTRING 100
  11. using namespace std;
  12. // Глобальные переменные:
  13. HINSTANCE hInst;                                // текущий экземпляр
  14. WCHAR szTitle[MAX_LOADSTRING];                  // Текст строки заголовка
  15. WCHAR szWindowClass[MAX_LOADSTRING];            // имя класса главного окна
  16.  
  17. //МОИ ГЛОБАЛЬНЫЕ ПЕРЕМЕННЫЕ
  18. HWND hWnd;
  19. int r = 50;
  20. int rr = 250;
  21. double speed = 0.02;
  22. double angle = 0;
  23. int x = 0;
  24. int y = 0;
  25. const double pi = 3.1415926535;
  26. const int WINDOW_SIZE = 800;
  27. int busStop[4] = { 0,0,0,0 };
  28. const int capacity = 13;
  29. int freeBusPlaces = capacity;
  30.  
  31. // Отправить объявления функций, включенных в этот модуль кода:
  32. ATOM                MyRegisterClass(HINSTANCE hInstance);
  33. BOOL                InitInstance(HINSTANCE, int);
  34. LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
  35.  
  36.  
  37. void DrawStuff(HDC hdc, RECT rectClient, int x, int y) {
  38.     int height, width;
  39.     HBRUSH hBrush;
  40.     hBrush = CreateSolidBrush(RGB(50, 100, 100));
  41.     SelectObject(hdc, hBrush);
  42.     Ellipse(hdc, x - r, y - r, x + r, y + r);
  43.     DeleteObject(hBrush);
  44. }
  45.  
  46.  
  47. void MovingBusByRound() {
  48.     PAINTSTRUCT ps;
  49.     int x0 = WINDOW_SIZE / 2;
  50.     int y0 = WINDOW_SIZE / 2;
  51.     bool flag[4] = { 0,0,0,0 };
  52.     int in = 0;
  53.     int out = 0;
  54.     while (true) {
  55.         if (angle > 2 * pi) {
  56.             angle -= 2 * pi;
  57.             flag[0] = 0; flag[1] = 0; flag[2] = 0; flag[3] = 0;
  58.         }
  59.         if (!flag[0] && angle > pi / 4) {
  60.             flag[0] = true;
  61.             Sleep(2000);
  62.             //нужен мьютекс
  63.             if (freeBusPlaces != 0) {
  64.                 out = rand() % freeBusPlaces;
  65.                 while (out!=0) {
  66.                     out--;
  67.                     freeBusPlaces--;
  68.                     Sleep(100);
  69.                 }
  70.             }
  71.             Sleep(2000);
  72.             if (freeBusPlaces != capacity && busStop[1] > 0) {
  73.                 while (freeBusPlaces!=capacity ) {
  74.                     freeBusPlaces++;
  75.                     busStop[1]--;
  76.                 }
  77.             }
  78.             Sleep(2000);
  79.         }
  80.         if (!flag[1] && angle > 3*pi / 4) {
  81.             flag[1] = true;
  82.             Sleep(2000);
  83.             //нужен мьютекс
  84.             if (freeBusPlaces != 0) {
  85.                 out = rand() % freeBusPlaces;
  86.                 while (out != 0) {
  87.                     out--;
  88.                     freeBusPlaces--;
  89.                     Sleep(100);
  90.                 }
  91.             }
  92.             Sleep(2000);
  93.             if (freeBusPlaces != capacity && busStop[2] > 0) {
  94.                 while (freeBusPlaces != capacity) {
  95.                     freeBusPlaces++;
  96.                     busStop[2]--;
  97.                 }
  98.             }
  99.             Sleep(2000);
  100.         }
  101.         if (!flag[2] && angle > 5*pi / 4) {
  102.             flag[2] = true;
  103.             Sleep(2000);
  104.             //нужен мьютекс
  105.             if (freeBusPlaces != 0) {
  106.                 out = rand() % freeBusPlaces;
  107.                 while (out != 0) {
  108.                     out--;
  109.                     freeBusPlaces--;
  110.                     Sleep(100);
  111.                 }
  112.             }
  113.             Sleep(2000);
  114.             if (freeBusPlaces != capacity && busStop[0] > 0) {
  115.                 while (freeBusPlaces != capacity) {
  116.                     freeBusPlaces++;
  117.                     busStop[0]--;
  118.                 }
  119.             }
  120.             Sleep(2000);
  121.         }
  122.         if (!flag[3] && angle > 7*pi / 4) {
  123.             flag[3] = true;
  124.             Sleep(2000);
  125.             //нужен мьютекс
  126.             if (freeBusPlaces != 0) {
  127.                 out = rand() % freeBusPlaces;
  128.                 while (out != 0) {
  129.                     out--;
  130.                     freeBusPlaces--;
  131.                     Sleep(100);
  132.                 }
  133.             }
  134.             Sleep(2000);
  135.             if (freeBusPlaces != capacity && busStop[3] > 0) {
  136.                 while (freeBusPlaces != capacity) {
  137.                     freeBusPlaces++;
  138.                     busStop[3]--;
  139.                 }
  140.             }
  141.             Sleep(2000);
  142.         }
  143.         angle += speed;
  144.         x = x0 + rr * cos(angle);
  145.         y = y0 + rr * sin(angle);
  146.         Sleep(20);
  147.     }
  148. }
  149.  
  150. void peoplesCounter() {
  151.     int k;
  152.     while (true) {
  153.         k = rand() % 4;
  154.         busStop[k] += rand() % 3;
  155.         Sleep(1000);
  156.     }
  157. }
  158.  
  159. void RedrawerThread() {
  160.     while (true) {
  161.         InvalidateRect(hWnd,0,1);
  162.         UpdateWindow(hWnd);
  163.         Sleep(30);
  164.     }
  165. }
  166. int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
  167.     _In_opt_ HINSTANCE hPrevInstance,
  168.     _In_ LPWSTR    lpCmdLine,
  169.     _In_ int       nCmdShow)
  170. {
  171.     UNREFERENCED_PARAMETER(hPrevInstance);
  172.     UNREFERENCED_PARAMETER(lpCmdLine);
  173.  
  174.     // TODO: Разместите код здесь.
  175.  
  176.     MyRegisterClass(hInstance);
  177.  
  178.     // Выполнить инициализацию приложения:
  179.     if (!InitInstance(hInstance, nCmdShow))
  180.     {
  181.         return FALSE;
  182.     }
  183.  
  184.     MSG msg;
  185.     srand(time(0));
  186.     // Цикл основного сообщения:
  187.     thread TH0(MovingBusByRound);
  188.     thread TH1(RedrawerThread);
  189.     thread TH2(peoplesCounter);
  190.     while (GetMessage(&msg, nullptr, 0, 0))
  191.     {
  192.         TranslateMessage(&msg);
  193.         DispatchMessage(&msg);
  194.     }
  195.     TH2.detach();
  196.     TH1.detach();
  197.     TH0.detach();
  198.  
  199.     return (int)msg.wParam;
  200. }
  201.  
  202.  
  203.  
  204. //
  205. //  ФУНКЦИЯ: MyRegisterClass()
  206. //
  207. //  ЦЕЛЬ: Регистрирует класс окна.
  208. //
  209. ATOM MyRegisterClass(HINSTANCE hInstance)
  210. {
  211.     WNDCLASSEXW wcex;
  212.  
  213.     wcex.cbSize = sizeof(WNDCLASSEX);
  214.  
  215.     wcex.style = CS_HREDRAW | CS_VREDRAW;
  216.     wcex.lpfnWndProc = WndProc;
  217.     wcex.cbClsExtra = 0;
  218.     wcex.cbWndExtra = 0;
  219.     wcex.hInstance = hInstance;
  220.     wcex.hIcon = NULL;
  221.     wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
  222.     wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  223.     wcex.lpszMenuName = NULL;
  224.     wcex.lpszClassName = L"MyClass";
  225.     wcex.hIconSm = NULL;
  226.  
  227.     return RegisterClassExW(&wcex);
  228. }
  229.  
  230. //
  231. //   ФУНКЦИЯ: InitInstance(HINSTANCE, int)
  232. //
  233. //   ЦЕЛЬ: Сохраняет маркер экземпляра и создает главное окно
  234. //
  235. //   КОММЕНТАРИИ:
  236. //
  237. //        В этой функции маркер экземпляра сохраняется в глобальной переменной, а также
  238. //        создается и выводится главное окно программы.
  239. //
  240.  
  241. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  242. {
  243.     hInst = hInstance; // Сохранить маркер экземпляра в глобальной переменной
  244.  
  245.     RECT rect;
  246.     rect.left = 0;
  247.     rect.right = WINDOW_SIZE;
  248.     rect.top = 0;
  249.     rect.bottom = WINDOW_SIZE;
  250.  
  251.     DWORD dwStyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
  252.     AdjustWindowRect(&rect, dwStyle, false);
  253.  
  254.  
  255.     hWnd = CreateWindowW(L"MyClass", L"Shuttle", dwStyle,
  256.         CW_USEDEFAULT, 0, rect.right - rect.left, rect.bottom - rect.top, nullptr, nullptr, hInstance, nullptr);
  257.  
  258.     if (!hWnd)
  259.     {
  260.         return FALSE;
  261.     }
  262.  
  263.     ShowWindow(hWnd, nCmdShow);
  264.     UpdateWindow(hWnd);
  265.  
  266.     return TRUE;
  267. }
  268.  
  269. //
  270. //  ФУНКЦИЯ: WndProc(HWND, UINT, WPARAM, LPARAM)
  271. //
  272. //  ЦЕЛЬ: Обрабатывает сообщения в главном окне.
  273. //
  274. //  WM_COMMAND  - обработать меню приложения
  275. //  WM_PAINT    - Отрисовка главного окна
  276. //  WM_DESTROY  - отправить сообщение о выходе и вернуться
  277. //
  278. //
  279. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  280. {
  281.     switch (message)
  282.     {
  283.     case WM_PAINT:
  284.     {
  285.         PAINTSTRUCT ps;
  286.         HDC hdc = BeginPaint(hWnd, &ps);
  287.         // TODO: Добавьте сюда любой код прорисовки, использующий HDC...
  288.         DrawStuff(hdc, ps.rcPaint, x, y);
  289.         DrawStuff(hdc, ps.rcPaint, (WINDOW_SIZE / 2) - 250, (WINDOW_SIZE / 2) - 250);
  290.         DrawStuff(hdc, ps.rcPaint, (WINDOW_SIZE / 2) + 250, (WINDOW_SIZE / 2) + 250);
  291.         DrawStuff(hdc, ps.rcPaint, (WINDOW_SIZE / 2) - 250, (WINDOW_SIZE / 2) + 250);
  292.         DrawStuff(hdc, ps.rcPaint, (WINDOW_SIZE / 2) + 250, (WINDOW_SIZE / 2) - 250);
  293.         TextOutA(hdc, (WINDOW_SIZE / 2) - 260, (WINDOW_SIZE / 2) - 260, to_string(busStop[0]).c_str(), 2);
  294.         TextOutA(hdc, (WINDOW_SIZE / 2) + 260, (WINDOW_SIZE / 2) + 260, to_string(busStop[1]).c_str(), 2);
  295.         TextOutA(hdc, (WINDOW_SIZE / 2) - 260, (WINDOW_SIZE / 2) + 260, to_string(busStop[2]).c_str(), 2);
  296.         TextOutA(hdc, (WINDOW_SIZE / 2) + 260, (WINDOW_SIZE / 2) - 260, to_string(busStop[3]).c_str(), 2);
  297.         TextOutA(hdc, (WINDOW_SIZE / 2), (WINDOW_SIZE / 2), to_string(freeBusPlaces).c_str(), 2);
  298.  
  299.  
  300.         EndPaint(hWnd, &ps);
  301.     }
  302.     break;
  303.     case WM_DESTROY:
  304.         PostQuitMessage(0);
  305.         break;
  306.     default:
  307.         return DefWindowProc(hWnd, message, wParam, lParam);
  308.     }
  309.     return 0;
  310. }
Add Comment
Please, Sign In to add comment