Advertisement
Cieslin

PIU_Snieg

Dec 3rd, 2017
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.83 KB | None | 0 0
  1. #define SCREEN_WIDTH 700
  2. #define SCREEN_HEIGHT 500
  3. #define SNOW_SIZE 1
  4. #define TIMER_ID 1
  5. #define MAP_X 100
  6. #define MAP_Y 80
  7.  
  8. #include <windows.h>
  9. #include <deque>
  10. #include <time.h>
  11.  
  12. TCHAR className[] = TEXT("śnieg iksde");
  13. TCHAR appName[] = TEXT("JAZDA Z KURWAMI HYHY !!!!!!!");
  14.  
  15. HWND hwnd;
  16. MSG msg;
  17.  
  18. const COLORREF backgroundColor = RGB(0, 0, 128);
  19. const COLORREF snowColor = RGB(255, 255, 255);
  20.  
  21. RECT clientRect;
  22.  
  23. struct Snow {
  24.     UINT x, y;
  25.     UINT yVel;
  26.     bool floor;
  27.     UINT time;
  28. };
  29.  
  30. std::deque<Snow> snow;
  31.  
  32. void initNewElement() {
  33.     Snow s;
  34.     s.x = rand() % MAP_X;
  35.     s.y = 0;
  36.     s.yVel = 1;
  37.     s.time = 0;
  38.     s.floor = 0;
  39.  
  40.     snow.push_back(s);
  41. }
  42.  
  43. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
  44.     static PAINTSTRUCT ps;
  45.     static HBRUSH whiteBrush;
  46.     static HPEN whitePen;
  47.  
  48.     switch (msg){
  49.     case WM_CREATE: {
  50.         SetTimer(hwnd, 100, TIMER_ID, 0);
  51.         GetClientRect(hwnd, &clientRect);
  52.  
  53.         srand(time(NULL));
  54.  
  55.         whiteBrush = CreateSolidBrush(snowColor);
  56.         whitePen = CreatePen(PS_SOLID, 0, snowColor);
  57.     }break;
  58.  
  59.     case WM_SIZE:
  60.         GetClientRect(hwnd, &clientRect);
  61.         break;
  62.  
  63.     case WM_TIMER: {
  64.         initNewElement();
  65.  
  66.         if (snow.size() > 0)
  67.             for (std::deque<Snow>::iterator it = snow.begin(); it != snow.end(); ++it)
  68.                 if (it->y < MAP_Y - SNOW_SIZE)
  69.                     it->y += it->yVel;
  70.                 else
  71.                     it->floor = true;
  72.  
  73.         int x = -1;
  74.  
  75.         if (snow.size() > 0)
  76.             for (std::deque<Snow>::iterator it = snow.begin(); it != snow.end(); ++it)
  77.                 if (it->floor) {
  78.                     it->time++;
  79.  
  80.                     if (it->time >= 10) {
  81.                         x = it->x;
  82.                         snow.erase(it);
  83.                         break;
  84.                     }
  85.                 }
  86.  
  87.         if (snow.size() > 0)
  88.             for (std::deque<Snow>::iterator it = snow.begin(); it != snow.end(); ++it)
  89.                 for (std::deque<Snow>::iterator ite = snow.begin(); ite != snow.end(); ++ite)
  90.                     if (it != ite && it->y + 1 == ite->y && it->x == ite->x)
  91.                         it->yVel = 0;
  92.  
  93.         if (snow.size() > 0 && x > -1)
  94.             for (std::deque<Snow>::iterator it = snow.begin(); it != snow.end(); ++it)
  95.                 if (it->x == x)
  96.                     it->yVel = 1;
  97.  
  98.         InvalidateRect(hwnd, NULL, 1);
  99.     }break;
  100.  
  101.     case WM_PAINT: {
  102.         HDC hdc = BeginPaint(hwnd, &ps);
  103.  
  104.         SetMapMode(hdc, MM_ANISOTROPIC);
  105.         SetViewportExtEx(hdc, clientRect.right, clientRect.bottom, NULL);
  106.         SetViewportOrgEx(hdc, 0, 0, NULL);
  107.         SetWindowExtEx(hdc, MAP_X, MAP_Y, NULL);
  108.         SetWindowOrgEx(hdc, 0, 0, NULL);
  109.  
  110.         SelectObject(hdc, whiteBrush);
  111.         SelectObject(hdc, whitePen);
  112.  
  113.         if (snow.size() > 0)
  114.             for (std::deque<Snow>::iterator it = snow.begin(); it != snow.end(); ++it)
  115.                 Rectangle(hdc, it->x, it->y, it->x + SNOW_SIZE, it->y + SNOW_SIZE);
  116.  
  117.         EndPaint(hwnd, &ps);
  118.     }break;
  119.  
  120.     case WM_LBUTTONDOWN:
  121.         snow.clear();
  122.     break;
  123.  
  124.     case WM_CLOSE:
  125.         DestroyWindow(hwnd);
  126.         break;
  127.  
  128.     case WM_DESTROY: {
  129.         DeleteObject(whiteBrush);
  130.         DeleteObject(whitePen);
  131.  
  132.         KillTimer(hwnd, TIMER_ID);
  133.         PostQuitMessage(0);
  134.         }break;
  135.  
  136.     default:
  137.         return DefWindowProc(hwnd, msg, wParam, lParam);
  138.     }
  139.  
  140.     return 0;
  141. }
  142.  
  143. INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
  144.     WNDCLASSEX wc;
  145.     wc.cbSize = sizeof(WNDCLASSEX);
  146.     wc.style = 0;
  147.     wc.cbClsExtra = 0;
  148.     wc.cbWndExtra = 0;
  149.     wc.hInstance = hInstance;
  150.     wc.hbrBackground = (HBRUSH)CreateSolidBrush(backgroundColor);
  151.     wc.lpfnWndProc = WndProc;
  152.     wc.lpszClassName = className;
  153.     wc.lpszMenuName = NULL;
  154.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  155.     wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  156.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  157.  
  158.     if (RegisterClassEx(&wc) == 0) {
  159.         MessageBox(NULL, TEXT("Some problem with RegisterClassEx"), className, MB_OK);
  160.         return 1;
  161.     }
  162.  
  163.     hwnd = CreateWindowEx(WS_EX_APPWINDOW | WS_EX_CLIENTEDGE, className, appName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, hInstance, 0);
  164.     if (hwnd == NULL) {
  165.         MessageBox(NULL, TEXT("Some problem with CreateWindowEx"), className, MB_OK);
  166.         return 1;
  167.     }
  168.  
  169.     ShowWindow(hwnd, nCmdShow);
  170.     UpdateWindow(hwnd);
  171.  
  172.     while (GetMessage(&msg, NULL, 0, 0) > 0){
  173.         TranslateMessage(&msg);
  174.         DispatchMessage(&msg);
  175.     }
  176.  
  177.     UnregisterClass(className, hInstance);
  178.  
  179.     return msg.wParam;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement