Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- #include <windows.h>
- #include <thread>
- #include <mutex>
- using namespace std;
- std::mutex mtx;
- std::mutex newmtx;
- LONG WINAPI WndProc(HWND, UINT, WPARAM, LPARAM);
- const int r = 30;
- int progress[2] = { r, r };
- int winSize[2] = { 500, 500 };
- int speed[2] = { 10, 5 };
- bool check[] = { true, true };
- int phase[2];
- void Phase() {
- while (true) {
- phase[0] = 1;
- phase[1] = 2;
- Sleep(5000);
- phase[0] = 0;
- phase[1] = 0;
- Sleep(5000);
- phase[0] = 2;
- phase[1] = 1;
- Sleep(5000);
- phase[0] = 0;
- phase[1] = 0;
- Sleep(5000);
- }
- }
- void THfunction(HWND hwnd, WNDCLASS w, int k)
- {
- int x = r;
- bool locked = false;
- bool unlocked = false;
- bool flocked = false;
- bool funlocked = false;
- /*newmtx.lock();*/
- while (check[0] || check[1]) {
- /*newmtx.unlock();*/
- x += speed[k];
- progress[k] = x;
- Sleep(20);
- if (x >= winSize[k] + r) {
- OutputDebugStringW(L"Check");
- check[k] = false;
- break;
- }
- if (locked == false && x >= winSize[k] / 2 - r * 2.5) {
- if (phase[k] == 1)
- while (phase[k] == 1) Sleep(20);
- mtx.lock();
- locked = true;
- }
- if (unlocked == false && x >= winSize[k] / 2 + r * 2.5) {
- mtx.unlock();
- unlocked = true;
- }
- if (flocked == false && x >= winSize[k] / 2 - r * 4) {
- speed[k] -= 2;
- flocked = true;
- }
- if (funlocked == false && x >= winSize[k] / 2 + r * 3) {
- speed[k] += 2;
- funlocked = true;
- }
- /*newmtx.lock();*/
- }
- /*newmtx.unlock();*/
- progress[k] = x;
- }
- void DrawBall(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 DrawTL(HDC hdc, RECT rectClient, int x, int y, int colour)
- {
- int height, width;
- HBRUSH hBrush; hBrush = CreateSolidBrush(RGB(0,1,1));
- switch (colour) {
- case(1): { //красный
- hBrush = CreateSolidBrush(RGB(200, 0, 0));
- break;
- }
- case(2): { //зеленый
- hBrush = CreateSolidBrush(RGB(0, 200, 0));
- break;
- }
- case(0): { //синий
- hBrush = CreateSolidBrush(RGB(200, 180, 100));
- break;
- }
- }
- SelectObject(hdc, hBrush);
- Ellipse(hdc, x - r, y - r, x + r, y + r);
- DeleteObject(hBrush);
- }
- int WINAPI WinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
- {
- WNDCLASS w;
- memset(&w, 0, sizeof(WNDCLASS));
- w.style = CS_HREDRAW | CS_VREDRAW;
- w.lpfnWndProc = WndProc;
- w.hInstance = hInstance;
- w.hbrBackground = CreateSolidBrush(0x00FFFFFF);
- w.lpszClassName = L"My Class";
- RegisterClass(&w);
- HWND hwnd;
- hwnd = CreateWindow(L"My Class", L"Катится мячик",
- WS_OVERLAPPEDWINDOW,
- 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), NULL, NULL,
- hInstance, NULL);
- std::thread TH2(Phase);
- ShowWindow(hwnd, nCmdShow);
- while (true) {
- /*speed[0] = rand() % 10 + 5;
- speed[1] = rand() % 10 + 5;*/
- std::thread TH0(THfunction, hwnd, w, 0);
- std::thread TH1(THfunction, hwnd, w, 1);
- OutputDebugStringW(L"Start");
- /*newmtx.lock();*/
- while (check[0] || check[1]) {
- /*newmtx.unlock();*/
- InvalidateRect(hwnd, 0, 1);
- UpdateWindow(hwnd);
- Sleep(20);
- /*newmtx.lock();*/
- }
- /*newmtx.unlock();*/
- OutputDebugStringW(L"Start join");
- TH0.join();
- TH1.join();
- check[0] = true; check[1] = true;
- OutputDebugStringW(L"End join");
- }
- /*DestroyWindow(hwnd);*/
- }
- LONG WINAPI WndProc(HWND hwnd, UINT Message,
- WPARAM wparam, LPARAM lparam)
- {
- HDC hdc;
- PAINTSTRUCT ps;
- HPEN hpen;
- switch (Message)
- {
- case WM_PAINT: {
- hdc = BeginPaint(hwnd, &ps);
- int width = winSize[0] = ps.rcPaint.right - ps.rcPaint.left;
- int height = winSize[1] = ps.rcPaint.bottom - ps.rcPaint.top;
- DrawBall(hdc, ps.rcPaint, progress[0], height / 2);
- DrawBall(hdc, ps.rcPaint, width / 2, progress[1]);
- DrawTL(hdc, ps.rcPaint, (width / 2) + 300, (height / 2) + 100, phase[0]);
- OutputDebugStringW(L"HELP");
- DrawTL(hdc, ps.rcPaint, (width / 2) - 100, (height / 2) + 300, phase[1]);
- hpen = CreatePen(PS_SOLID, 5, 0);
- SelectObject(hdc, hpen);
- int offset = r * 2;
- MoveToEx(hdc, width / 2 - offset, 0, 0);
- LineTo(hdc, width / 2 - offset, height / 2 - offset);
- MoveToEx(hdc, width / 2 + offset, 0, 0);
- LineTo(hdc, width / 2 + offset, height / 2 - offset);
- MoveToEx(hdc, width / 2 - offset, height, 0);
- LineTo(hdc, width / 2 - offset, height / 2 + offset);
- MoveToEx(hdc, width / 2 + offset, height, 0);
- LineTo(hdc, width / 2 + offset, height / 2 + offset);
- MoveToEx(hdc, 0, height / 2 - offset, 0);
- LineTo(hdc, width / 2 - offset, height / 2 - offset);
- MoveToEx(hdc, 0, height / 2 + offset, 0);
- LineTo(hdc, width / 2 - offset, height / 2 + offset);
- MoveToEx(hdc, width, height / 2 - offset, 0);
- LineTo(hdc, width / 2 + offset, height / 2 - offset);
- MoveToEx(hdc, width, height / 2 + offset, 0);
- LineTo(hdc, width / 2 + offset, height / 2 + offset);
- EndPaint(hwnd, &ps);
- break;
- }
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hwnd, Message, wparam, lparam);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement