Advertisement
Cieslin

PIU_Snieg_by_Cieslin

Dec 5th, 2017
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.02 KB | None | 0 0
  1. #include <windows.h>
  2. #include <vector>
  3. #include <ctime>
  4.  
  5. #define SCREEN_WIDTH 1240  
  6. #define SCREEN_HEIGHT 720
  7. #define MAP_X 100
  8. #define MAP_Y 80
  9. #define SNOW_SIZE 1
  10. #define TIMER_ID 1
  11.  
  12. const COLORREF backgroundColor = RGB(0, 0, 128);
  13. const COLORREF snowColor = RGB(255, 255, 255);
  14.  
  15. RECT windowRect;
  16.  
  17. struct Snow {
  18.     POINT SnowPos;
  19.     UINT snowVel;
  20.     BOOL isFloor;
  21.     UINT timeOnFloor;
  22. };
  23.  
  24.  
  25. std::vector<Snow>snowFlakes;
  26.  
  27. HINSTANCE hInstance;
  28.  
  29. LRESULT CALLBACK ProceduraOkna(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
  30.  
  31. bool RejestracjaKlasy(WNDCLASSEX &window)
  32. {
  33.     window.cbSize = sizeof(WNDCLASSEX);
  34.     window.style = CS_VREDRAW | CS_HREDRAW;
  35.     window.lpfnWndProc = ProceduraOkna;
  36.     window.cbClsExtra = 0;
  37.     window.cbWndExtra = 0;
  38.     window.hInstance = hInstance;
  39.     window.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  40.     window.hCursor = LoadCursor(NULL, IDC_ARROW);
  41.     window.hbrBackground = (HBRUSH)CreateSolidBrush(backgroundColor);
  42.     window.lpszMenuName = NULL;
  43.     window.lpszClassName = "StandardoweOkno";
  44.     window.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  45.  
  46.     if (!RegisterClassEx(&window))
  47.     {
  48.         MessageBox(NULL, "Problem z rejestracją okna", "Rejestracja okna", MB_ICONERROR | MB_OK);
  49.         return false;
  50.     }
  51.     return true;
  52. }
  53.  
  54.  
  55.  
  56. void initSnow(RECT &windowRect)
  57. {
  58.     Snow s;
  59.     s.SnowPos.x = rand() % MAP_X;
  60.     s.SnowPos.y = 0;
  61.     s.isFloor = 0;
  62.     s.snowVel = 1;
  63.     s.timeOnFloor = 0;
  64.  
  65.     snowFlakes.push_back(s);
  66. }
  67.  
  68.  
  69.  
  70. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  71. {
  72.     WNDCLASSEX window;
  73.  
  74.     if (!RejestracjaKlasy(window))
  75.         return 1;
  76.  
  77.     HWND uchwytOkna = 0;
  78.  
  79.     uchwytOkna = CreateWindowEx(
  80.         WS_EX_APPWINDOW | WS_EX_CLIENTEDGE,
  81.         "StandardoweOkno",
  82.         "Pierwsze okno",
  83.         WS_OVERLAPPEDWINDOW,
  84.         CW_USEDEFAULT,
  85.         CW_USEDEFAULT,
  86.         SCREEN_WIDTH,
  87.         SCREEN_HEIGHT,
  88.         0,
  89.         0,
  90.         hInstance,
  91.         0);
  92.  
  93.     ShowWindow(uchwytOkna, nCmdShow);
  94.     UpdateWindow(uchwytOkna);
  95.  
  96.     MSG msg;
  97.  
  98.     for (; ;)
  99.     {
  100.         if (0 != GetMessage(&msg, 0, 0, 0))
  101.         {
  102.             TranslateMessage(&msg);
  103.             DispatchMessage(&msg);
  104.         }
  105.         if (msg.message == WM_QUIT) break;
  106.     }
  107.  
  108.     return (int)msg.wParam;
  109. }
  110.  
  111. LRESULT CALLBACK ProceduraOkna(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  112. {
  113.     HDC hdc;
  114.     static PAINTSTRUCT paint;
  115.     static HBRUSH whiteBrush;
  116.     static HPEN whitePen;
  117.     switch (message)
  118.     {
  119.     case WM_CREATE:
  120.         GetClientRect(hWnd, &windowRect);
  121.         whiteBrush = CreateSolidBrush(snowColor);
  122.         whitePen = CreatePen(PS_SOLID, 0, snowColor);
  123.         SetTimer(hWnd, 100, TIMER_ID, NULL);
  124.         srand((unsigned)time(NULL));
  125.         break;
  126.     case WM_PAINT:
  127.     {
  128.         hdc = BeginPaint(hWnd, &paint);
  129.  
  130.         SetMapMode(hdc, MM_ANISOTROPIC);
  131.         SetViewportExtEx(hdc, windowRect.right, windowRect.bottom, NULL);
  132.         SetViewportOrgEx(hdc, 0, 0, NULL);
  133.         SetWindowExtEx(hdc, MAP_X, MAP_Y, NULL);
  134.         SetWindowOrgEx(hdc, 0, 0, NULL);
  135.  
  136.         SelectObject(hdc, whiteBrush);
  137.         SelectObject(hdc, whitePen);
  138.  
  139.         if (snowFlakes.size() > 0)
  140.             for (std::vector<Snow>::iterator it = snowFlakes.begin(); it != snowFlakes.end(); it++)
  141.                 Rectangle(hdc, it->SnowPos.x, it->SnowPos.y, it->SnowPos.x + SNOW_SIZE, it->SnowPos.y + SNOW_SIZE);
  142.         EndPaint(hWnd, &paint);
  143.     }
  144.     break;
  145.     case WM_SIZE:
  146.         GetClientRect(hWnd, &windowRect);
  147.         break;
  148.     case WM_TIMER:
  149.     {
  150.         initSnow(windowRect);
  151.         if (snowFlakes.size() > 0)
  152.             for (std::vector<Snow>::iterator it = snowFlakes.begin(); it != snowFlakes.end(); it++)
  153.                 if (it->SnowPos.y < MAP_Y - SNOW_SIZE)
  154.                     it->SnowPos.y += it->snowVel;
  155.                 else
  156.                     it->isFloor = true;
  157.         ////////////////////////////////////////////////////////////////////////////////////////////////
  158.         int x = -1;
  159.  
  160.         if (snowFlakes.size() > 0)
  161.             for (std::vector<Snow>::iterator it = snowFlakes.begin(); it != snowFlakes.end(); it++)
  162.                 if (it->isFloor)
  163.                 {
  164.                     it->timeOnFloor++;
  165.                     if (it->timeOnFloor >= 10)
  166.                     {
  167.                         x = it->SnowPos.x;
  168.                         snowFlakes.erase(it);
  169.                         break;
  170.                     }
  171.                 }
  172.         ///////////////////////////////////////////////////////////////////////////////////////////////////
  173.         if (snowFlakes.size() > 0)
  174.             for (std::vector<Snow>::iterator it = snowFlakes.begin(); it != snowFlakes.end(); it++)
  175.                 for (std::vector<Snow>::iterator ite = snowFlakes.begin(); ite != snowFlakes.end(); ite++)
  176.                     if (it != ite && it->SnowPos.y + 1 == ite->SnowPos.y && it->SnowPos.x == ite->SnowPos.x)
  177.                         it->snowVel = 0;
  178.         ///////////////////////////////////////////////////////////////////////////////////////////////////
  179.         if (snowFlakes.size() > 0 && x > -1)
  180.             for (std::vector<Snow>::iterator it = snowFlakes.begin(); it != snowFlakes.end(); it++)
  181.                 if (it->SnowPos.x == x)
  182.                     it->snowVel = 1;
  183.         InvalidateRect(hWnd, NULL, 1);
  184.     }
  185.         break;
  186.     case WM_CLOSE:
  187.         if (MessageBox(hWnd, "Czy chcesz zakonczyc dzialanie programu?", "Dzialanie programu", MB_YESNO | MB_ICONERROR) == IDNO)
  188.             break;
  189.     case WM_DESTROY:
  190.         KillTimer(hWnd, TIMER_ID);
  191.         PostQuitMessage(0);
  192.         break;
  193.     case WM_LBUTTONDOWN:
  194.         snowFlakes.clear();
  195.         break;
  196.     default:
  197.         return DefWindowProc(hWnd, message, wParam, lParam);
  198.     }
  199.     return 0;
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement