Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // DrawingShuttle.cpp : Определяет точку входа для приложения.
- //
- #include <thread>
- #include <mutex>
- #include "framework.h"
- #include "DrawingShuttle.h"
- #include <cmath>
- #include <string>
- #include <ctime>
- #define MAX_LOADSTRING 100
- using namespace std;
- // Глобальные переменные:
- HINSTANCE hInst; // текущий экземпляр
- WCHAR szTitle[MAX_LOADSTRING]; // Текст строки заголовка
- WCHAR szWindowClass[MAX_LOADSTRING]; // имя класса главного окна
- //МОИ ГЛОБАЛЬНЫЕ ПЕРЕМЕННЫЕ
- HWND hWnd;
- int r = 50;
- int rr = 250;
- double speed = 0.02;
- double angle = 0;
- int x = 0;
- int y = 0;
- const double pi = 3.1415926535;
- const int WINDOW_SIZE = 800;
- int busStop[4] = { 0,0,0,0 };
- const int capacity = 13;
- int freeBusPlaces = capacity;
- // Отправить объявления функций, включенных в этот модуль кода:
- ATOM MyRegisterClass(HINSTANCE hInstance);
- BOOL InitInstance(HINSTANCE, int);
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- void DrawStuff(HDC hdc, RECT rectClient, int x, int y) {
- int height, width;
- HBRUSH hBrush;
- hBrush = CreateSolidBrush(RGB(50, 100, 100));
- SelectObject(hdc, hBrush);
- Ellipse(hdc, x - r, y - r, x + r, y + r);
- DeleteObject(hBrush);
- }
- void MovingBusByRound() {
- PAINTSTRUCT ps;
- int x0 = WINDOW_SIZE / 2;
- int y0 = WINDOW_SIZE / 2;
- bool flag[4] = { 0,0,0,0 };
- int in = 0;
- int out = 0;
- while (true) {
- if (angle > 2 * pi) {
- angle -= 2 * pi;
- flag[0] = 0; flag[1] = 0; flag[2] = 0; flag[3] = 0;
- }
- if (!flag[0] && angle > pi / 4) {
- flag[0] = true;
- Sleep(2000);
- //нужен мьютекс
- if (freeBusPlaces != 0) {
- out = rand() % freeBusPlaces;
- while (out!=0) {
- out--;
- freeBusPlaces--;
- Sleep(100);
- }
- }
- Sleep(2000);
- if (freeBusPlaces != capacity && busStop[1] > 0) {
- while (freeBusPlaces!=capacity ) {
- freeBusPlaces++;
- busStop[1]--;
- }
- }
- Sleep(2000);
- }
- if (!flag[1] && angle > 3*pi / 4) {
- flag[1] = true;
- Sleep(2000);
- //нужен мьютекс
- if (freeBusPlaces != 0) {
- out = rand() % freeBusPlaces;
- while (out != 0) {
- out--;
- freeBusPlaces--;
- Sleep(100);
- }
- }
- Sleep(2000);
- if (freeBusPlaces != capacity && busStop[2] > 0) {
- while (freeBusPlaces != capacity) {
- freeBusPlaces++;
- busStop[2]--;
- }
- }
- Sleep(2000);
- }
- if (!flag[2] && angle > 5*pi / 4) {
- flag[2] = true;
- Sleep(2000);
- //нужен мьютекс
- if (freeBusPlaces != 0) {
- out = rand() % freeBusPlaces;
- while (out != 0) {
- out--;
- freeBusPlaces--;
- Sleep(100);
- }
- }
- Sleep(2000);
- if (freeBusPlaces != capacity && busStop[0] > 0) {
- while (freeBusPlaces != capacity) {
- freeBusPlaces++;
- busStop[0]--;
- }
- }
- Sleep(2000);
- }
- if (!flag[3] && angle > 7*pi / 4) {
- flag[3] = true;
- Sleep(2000);
- //нужен мьютекс
- if (freeBusPlaces != 0) {
- out = rand() % freeBusPlaces;
- while (out != 0) {
- out--;
- freeBusPlaces--;
- Sleep(100);
- }
- }
- Sleep(2000);
- if (freeBusPlaces != capacity && busStop[3] > 0) {
- while (freeBusPlaces != capacity) {
- freeBusPlaces++;
- busStop[3]--;
- }
- }
- Sleep(2000);
- }
- angle += speed;
- x = x0 + rr * cos(angle);
- y = y0 + rr * sin(angle);
- Sleep(20);
- }
- }
- void peoplesCounter() {
- int k;
- while (true) {
- k = rand() % 4;
- busStop[k] += rand() % 3;
- Sleep(1000);
- }
- }
- void RedrawerThread() {
- while (true) {
- InvalidateRect(hWnd,0,1);
- UpdateWindow(hWnd);
- Sleep(30);
- }
- }
- int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
- _In_opt_ HINSTANCE hPrevInstance,
- _In_ LPWSTR lpCmdLine,
- _In_ int nCmdShow)
- {
- UNREFERENCED_PARAMETER(hPrevInstance);
- UNREFERENCED_PARAMETER(lpCmdLine);
- // TODO: Разместите код здесь.
- MyRegisterClass(hInstance);
- // Выполнить инициализацию приложения:
- if (!InitInstance(hInstance, nCmdShow))
- {
- return FALSE;
- }
- MSG msg;
- srand(time(0));
- // Цикл основного сообщения:
- thread TH0(MovingBusByRound);
- thread TH1(RedrawerThread);
- thread TH2(peoplesCounter);
- while (GetMessage(&msg, nullptr, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- TH2.detach();
- TH1.detach();
- TH0.detach();
- return (int)msg.wParam;
- }
- //
- // ФУНКЦИЯ: MyRegisterClass()
- //
- // ЦЕЛЬ: Регистрирует класс окна.
- //
- ATOM MyRegisterClass(HINSTANCE hInstance)
- {
- WNDCLASSEXW wcex;
- wcex.cbSize = sizeof(WNDCLASSEX);
- wcex.style = CS_HREDRAW | CS_VREDRAW;
- wcex.lpfnWndProc = WndProc;
- wcex.cbClsExtra = 0;
- wcex.cbWndExtra = 0;
- wcex.hInstance = hInstance;
- wcex.hIcon = NULL;
- wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
- wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
- wcex.lpszMenuName = NULL;
- wcex.lpszClassName = L"MyClass";
- wcex.hIconSm = NULL;
- return RegisterClassExW(&wcex);
- }
- //
- // ФУНКЦИЯ: InitInstance(HINSTANCE, int)
- //
- // ЦЕЛЬ: Сохраняет маркер экземпляра и создает главное окно
- //
- // КОММЕНТАРИИ:
- //
- // В этой функции маркер экземпляра сохраняется в глобальной переменной, а также
- // создается и выводится главное окно программы.
- //
- BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
- {
- hInst = hInstance; // Сохранить маркер экземпляра в глобальной переменной
- RECT rect;
- rect.left = 0;
- rect.right = WINDOW_SIZE;
- rect.top = 0;
- rect.bottom = WINDOW_SIZE;
- DWORD dwStyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
- AdjustWindowRect(&rect, dwStyle, false);
- hWnd = CreateWindowW(L"MyClass", L"Shuttle", dwStyle,
- CW_USEDEFAULT, 0, rect.right - rect.left, rect.bottom - rect.top, nullptr, nullptr, hInstance, nullptr);
- if (!hWnd)
- {
- return FALSE;
- }
- ShowWindow(hWnd, nCmdShow);
- UpdateWindow(hWnd);
- return TRUE;
- }
- //
- // ФУНКЦИЯ: WndProc(HWND, UINT, WPARAM, LPARAM)
- //
- // ЦЕЛЬ: Обрабатывает сообщения в главном окне.
- //
- // WM_COMMAND - обработать меню приложения
- // WM_PAINT - Отрисовка главного окна
- // WM_DESTROY - отправить сообщение о выходе и вернуться
- //
- //
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message)
- {
- case WM_PAINT:
- {
- PAINTSTRUCT ps;
- HDC hdc = BeginPaint(hWnd, &ps);
- // TODO: Добавьте сюда любой код прорисовки, использующий HDC...
- DrawStuff(hdc, ps.rcPaint, x, y);
- DrawStuff(hdc, ps.rcPaint, (WINDOW_SIZE / 2) - 250, (WINDOW_SIZE / 2) - 250);
- DrawStuff(hdc, ps.rcPaint, (WINDOW_SIZE / 2) + 250, (WINDOW_SIZE / 2) + 250);
- DrawStuff(hdc, ps.rcPaint, (WINDOW_SIZE / 2) - 250, (WINDOW_SIZE / 2) + 250);
- DrawStuff(hdc, ps.rcPaint, (WINDOW_SIZE / 2) + 250, (WINDOW_SIZE / 2) - 250);
- TextOutA(hdc, (WINDOW_SIZE / 2) - 260, (WINDOW_SIZE / 2) - 260, to_string(busStop[0]).c_str(), 2);
- TextOutA(hdc, (WINDOW_SIZE / 2) + 260, (WINDOW_SIZE / 2) + 260, to_string(busStop[1]).c_str(), 2);
- TextOutA(hdc, (WINDOW_SIZE / 2) - 260, (WINDOW_SIZE / 2) + 260, to_string(busStop[2]).c_str(), 2);
- TextOutA(hdc, (WINDOW_SIZE / 2) + 260, (WINDOW_SIZE / 2) - 260, to_string(busStop[3]).c_str(), 2);
- TextOutA(hdc, (WINDOW_SIZE / 2), (WINDOW_SIZE / 2), to_string(freeBusPlaces).c_str(), 2);
- EndPaint(hWnd, &ps);
- }
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment