Advertisement
peterzig

[PIU] Gwiazdki v2

Nov 8th, 2016
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.97 KB | None | 0 0
  1. #define SCREEN_WIDTH 800
  2. #define SCREEN_HEIGHT 600
  3. #define SQUARE_SIZE 50
  4. #define COLOURS 40
  5.  
  6. #include <Windows.h>
  7. #include <time.h>
  8. #include <deque>
  9. #include <tchar.h>
  10.  
  11. using namespace std;
  12.  
  13. MSG msg;
  14. TCHAR className[] = TEXT("Tak");
  15. HWND hwnd;
  16. RECT clientRect;
  17.  
  18. struct Container {
  19.     int rotate;
  20.     int angle;
  21.     RECT rect;
  22.     COLORREF brushColor;
  23.     COLORREF penColor;
  24. };
  25.  
  26. deque<Container> starContainer;
  27.  
  28. void initStars() {
  29.     int amount = (rand() % 31) + 20;
  30.  
  31.     for (UINT i = 0; i < amount; i++) {
  32.         int size = (rand() % 50) + 20;
  33.         int x = (rand() % (clientRect.right - clientRect.left - size)) + clientRect.left;
  34.         int y = (rand() % (clientRect.bottom - clientRect.top - size)) + clientRect.top;
  35.  
  36.         Container container;
  37.         container.angle = (rand() % 21) - 10;
  38.         container.rotate = rand() % 91;
  39.         container.rect.left = x;
  40.         container.rect.top = y;
  41.         container.rect.right = x + size;
  42.         container.rect.bottom = y + size;
  43.         container.brushColor = RGB(rand() % 255, rand() % 255, rand() % 255);
  44.         container.penColor = RGB(rand() % 255, rand() % 255, rand() % 255);
  45.  
  46.         starContainer.push_back(container);
  47.     }
  48. }
  49.  
  50. void drawRectangle(const HDC hdc, const RECT rect, const float rotate) {
  51.     const float x = rect.left;
  52.     const float y = rect.top;
  53.     const float sizeX = rect.right - rect.left;
  54.     const float sizeY = rect.bottom - rect.top;
  55.  
  56.     const float xPos = x + (sizeX / 2);
  57.     const float yPos = y + (sizeY / 2);
  58.  
  59.     const float rad = (float)rotate / (360.0f / (2 * 3.1416f));
  60.  
  61.     POINT point[8];
  62.  
  63.     point[0].x = cos(rad) * (x - xPos) - sin(rad) * (y - yPos) + xPos;
  64.     point[0].y = sin(rad) * (x - xPos) + cos(rad) * (y - yPos) + yPos;
  65.  
  66.     point[1].x = cos(rad) * ((x - xPos) + (sizeX / 2)) - sin(rad) * ((y - yPos) + (sizeY / 3)) + xPos;
  67.     point[1].y = sin(rad) * ((x - xPos) + (sizeX / 2)) + cos(rad) * ((y - yPos) + (sizeY / 3)) + yPos;
  68.  
  69.     point[2].x = cos(rad) * ((x - xPos) + sizeX) - sin(rad) * (y - yPos) + xPos;
  70.     point[2].y = sin(rad) * ((x - xPos) + sizeX) + cos(rad) * (y - yPos) + yPos;
  71.  
  72.     point[3].x = cos(rad) * ((x - xPos) + sizeX - (sizeX / 3)) - sin(rad) * ((y - yPos) + (sizeY / 2)) + xPos;
  73.     point[3].y = sin(rad) * ((x - xPos) + sizeX - (sizeX / 3)) + cos(rad) * ((y - yPos) + (sizeY / 2)) + yPos;
  74.  
  75.     point[4].x = cos(rad) * ((x - xPos) + sizeX) - sin(rad) * ((y - yPos) + sizeY) + xPos;
  76.     point[4].y = sin(rad) * ((x - xPos) + sizeX) + cos(rad) * ((y - yPos) + sizeY) + yPos;
  77.  
  78.     point[5].x = cos(rad) * ((x - xPos) + (sizeX / 2)) - sin(rad) * ((y - yPos) + sizeY - (sizeY / 3)) + xPos;
  79.     point[5].y = sin(rad) * ((x - xPos) + (sizeX / 2)) + cos(rad) * ((y - yPos) + sizeY - (sizeY / 3)) + yPos;
  80.  
  81.     point[6].x = cos(rad) * (x - xPos) - sin(rad) * ((y - yPos) + sizeY) + xPos;
  82.     point[6].y = sin(rad) * (x - xPos) + cos(rad) * ((y - yPos) + sizeY) + yPos;
  83.  
  84.     point[7].x = cos(rad) * ((x - xPos) + (sizeX / 3)) - sin(rad) * ((y - yPos) + (sizeY / 2)) + xPos;
  85.     point[7].y = sin(rad) * ((x - xPos) + (sizeX / 3)) + cos(rad) * ((y - yPos) + (sizeY / 2)) + yPos;
  86.  
  87.     Polygon(hdc, point, 8);
  88. }
  89.  
  90. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
  91.     static PAINTSTRUCT ps;
  92.  
  93.     switch (msg)
  94.     {
  95.     case WM_CREATE:
  96.         SetTimer(hwnd, 1, 100, 0);
  97.         GetClientRect(hwnd, &clientRect);
  98.         initStars();
  99.         break;
  100.  
  101.     case WM_PAINT: {
  102.         HDC hdc = BeginPaint(hwnd, &ps);
  103.  
  104.         HBRUSH brushBox, brush;
  105.         HPEN penBox, pen;
  106.  
  107.         for (deque<Container>::iterator it = starContainer.begin(); it != starContainer.end(); ++it) {
  108.             brush = CreateSolidBrush(it->brushColor);
  109.             brushBox = (HBRUSH)SelectObject(hdc, brush);
  110.  
  111.             pen = CreatePen(PS_SOLID, 3, it->penColor);
  112.             penBox = (HPEN)SelectObject(hdc, pen);
  113.  
  114.             drawRectangle(hdc, it->rect, it->rotate);
  115.             SelectObject(hdc, brushBox);
  116.             SelectObject(hdc, penBox);
  117.  
  118.             DeleteObject(brushBox);
  119.             DeleteObject(brush);
  120.             DeleteObject(penBox);
  121.             DeleteObject(pen);
  122.         }
  123.  
  124.         EndPaint(hwnd, &ps);
  125.     }break;
  126.  
  127.     case WM_TIMER:
  128.         for (deque<Container>::iterator it = starContainer.begin(); it != starContainer.end(); ++it) {
  129.             int moveX = (rand() % 11) - 5;
  130.             int moveY = (rand() % 11) - 5;
  131.  
  132.             if (it->rect.left + moveX < clientRect.left || it->rect.right + moveX > clientRect.right)
  133.                 moveX = -moveX;
  134.  
  135.             if (it->rect.top + moveY < clientRect.top || it->rect.bottom + moveY < clientRect.bottom)
  136.                 moveY = -moveY;
  137.  
  138.             it->rect.left += moveX;
  139.             it->rect.right += moveX;
  140.  
  141.             it->rect.top -= moveY;
  142.             it->rect.bottom -= moveY;
  143.  
  144.             it->rotate += it->angle;
  145.  
  146.             if (it->rotate > 360) it->rotate = 0;
  147.             else if (it->rotate < -360) it->rotate = 0;
  148.         }
  149.  
  150.         InvalidateRect(hwnd, NULL, 1);
  151.         break;
  152.  
  153.     case WM_DESTROY:
  154.         PostQuitMessage(0);
  155.         break;
  156.  
  157.     case WM_CLOSE:
  158.         DestroyWindow(hwnd);
  159.         break;
  160.  
  161.     default:
  162.         return DefWindowProc(hwnd, msg, wParam, lParam);
  163.     }
  164.  
  165.     return 0;
  166. }
  167.  
  168. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  169. {
  170.     WNDCLASSEX wc;
  171.     wc.cbSize = sizeof(WNDCLASSEX);
  172.     wc.style = 0;
  173.     wc.cbClsExtra = 0;
  174.     wc.cbWndExtra = 0;
  175.     wc.hInstance = hInstance;
  176.     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  177.     wc.lpfnWndProc = WndProc;
  178.     wc.lpszClassName = className;
  179.     wc.lpszMenuName = NULL;
  180.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  181.     wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  182.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  183.  
  184.     if (!RegisterClassEx(&wc))
  185.     {
  186.         MessageBox(NULL, TEXT("Nie udało się zarejestrować klasy okna!"), TEXT("Error 404"), MB_OK | MB_ICONERROR);
  187.         return 1;
  188.     }
  189.  
  190.     hwnd = CreateWindowEx(WS_EX_APPWINDOW | WS_EX_CLIENTEDGE,
  191.             className, TEXT("Okno"), WS_OVERLAPPEDWINDOW,
  192.                 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  193.                 NULL, NULL,  hInstance, NULL);
  194.     if (hwnd == NULL) {
  195.         MessageBox(NULL, TEXT("Nie udało się utworzyć okna !"), TEXT("CreateWindowEx"), MB_OK | MB_ICONERROR);
  196.         return 1;
  197.     }
  198.  
  199.     ShowWindow(hwnd, nCmdShow);
  200.     UpdateWindow(hwnd);
  201.  
  202.     while (GetMessage(&msg, NULL, 0, 0))
  203.     {
  204.         TranslateMessage(&msg);
  205.         DispatchMessage(&msg);
  206.     }
  207.  
  208.     UnregisterClass(className, hInstance);
  209.  
  210.     return msg.wParam;
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement