Advertisement
Guest User

C++ Maski

a guest
Jan 3rd, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma comment(lib ,"Msimg32.lib");
  2. #include <Windows.h>
  3. #include <math.h>
  4. #include <vector>
  5. #include <string>
  6.  
  7. PAINTSTRUCT PS;
  8. HDC hdc, hdcNowy;
  9. HBITMAP hbmOld;
  10.  
  11. RECT WndRect;
  12. int Max = 0;
  13.  
  14. HBITMAP CreateBitmapMask(HBITMAP hbmColour, COLORREF crTransparent) {
  15.     HDC hdcMem, hdcMem2;
  16.     HBITMAP hbmMask, hbmOld, hbmOld2;
  17.     BITMAP bm;
  18.  
  19.     GetObject(hbmColour, sizeof(BITMAP), &bm);
  20.     hbmMask = CreateBitmap(bm.bmWidth, bm.bmHeight, 1, 1, NULL);
  21.  
  22.     hdcMem = CreateCompatibleDC(NULL);
  23.     hdcMem2 = CreateCompatibleDC(NULL);
  24.  
  25.     hbmOld = (HBITMAP)SelectObject(hdcMem, hbmColour);
  26.     hbmOld2 = (HBITMAP)SelectObject(hdcMem2, hbmMask);
  27.  
  28.     SetBkColor(hdcMem, crTransparent);
  29.  
  30.     BitBlt(hdcMem2, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
  31.     BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem2, 0, 0, SRCINVERT);
  32.  
  33.     SelectObject(hdcMem, hbmOld);
  34.     SelectObject(hdcMem2, hbmOld2);
  35.     DeleteDC(hdcMem);
  36.     DeleteDC(hdcMem2);
  37.  
  38.     return hbmMask;
  39. }
  40.  
  41. class Ball {
  42.     POINT Direction;
  43.     POINT Position;
  44.  
  45.     HBITMAP Bitmap;
  46.     BITMAP BitmapInfo;
  47.  
  48.     HBITMAP Maska;
  49.  
  50.     int Speed = 10;
  51.     int Y_MAX;
  52.     bool STOP = false;
  53.  
  54. public:
  55.     Ball(int X, int Y, int Y_max) {
  56.         Bitmap = (HBITMAP)LoadImage(NULL, ("BM\\p.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  57.         GetObject(Bitmap, sizeof(BitmapInfo), &BitmapInfo);
  58.         Position.x = X;
  59.         Position.y = Y;
  60.         Direction.x = 0;
  61.         Direction.y = 1;
  62.         Y_MAX = Y_max;
  63.         Maska = CreateBitmapMask(Bitmap, RGB(255, 0, 255));
  64.     }
  65.  
  66.     bool MoveBall() {
  67.         if (!STOP) {
  68.             Position.x += (Direction.x*Speed);
  69.             Position.y += (Direction.y*Speed);
  70.         }
  71.         if (Position.y >= Y_MAX) {
  72.             STOP = true;
  73.             return true;
  74.         }
  75.         return false;
  76.     }
  77.  
  78.     void PaintBall(HDC &hdc, HDC &bmhdc) {
  79.         HBITMAP hbmOld = (HBITMAP)SelectObject(hdcNowy, Bitmap);
  80.  
  81.         BitBlt(hdc, Position.x, Position.y, BitmapInfo.bmWidth, BitmapInfo.bmHeight, bmhdc, 0, 0, SRCCOPY);
  82.  
  83.         SelectObject(hdcNowy, hbmOld);
  84.  
  85.     }
  86.     void PaintMasked(HDC &hdc, HDC &bmhdc) {
  87.         Bitmap = (HBITMAP)LoadImage(NULL, ("BM\\p.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  88.  
  89.         HBITMAP hbmOld = (HBITMAP)SelectObject(hdcNowy, Maska);
  90.  
  91.         BitBlt(hdc, Position.x, Position.y, BitmapInfo.bmWidth, BitmapInfo.bmHeight, hdcNowy, 0, 0, SRCAND);
  92.         SelectObject(hdcNowy, Bitmap);
  93.         BitBlt(hdc, Position.x, Position.y, BitmapInfo.bmWidth, BitmapInfo.bmHeight, hdcNowy, 0, 0, SRCPAINT);
  94.  
  95.         SelectObject(hdcNowy, hbmOld);
  96.     }
  97. };
  98.  
  99. std::vector<Ball *> balls;
  100. float MousePos_x;
  101. float MousePos_y;
  102.  
  103. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
  104.     MousePos_x = LOWORD(lParam);
  105.     MousePos_y = HIWORD(lParam);
  106.  
  107.     switch (msg) {
  108.  
  109.     case WM_LBUTTONDOWN: {
  110.         GetClientRect(hwnd, &WndRect);
  111.         balls.push_back(new Ball(MousePos_x - 25, MousePos_y - 25, WndRect.bottom - 50));
  112.     }break;
  113.  
  114.     case WM_RBUTTONDOWN: {
  115.         balls.clear();
  116.     }break;
  117.  
  118.     case WM_PAINT: {
  119.         hdc = BeginPaint(hwnd, &PS);
  120.  
  121.         hdcNowy = CreateCompatibleDC(hdc);
  122.  
  123.         for (int i = 0; i < balls.size(); i++) {
  124.             //balls[i]->PaintBall(hdc, hdcNowy);
  125.             balls[i]->PaintMasked(hdc, hdcNowy);
  126.         }
  127.  
  128.         DeleteDC(hdcNowy);
  129.  
  130.         EndPaint(hwnd, &PS);
  131.     }break;
  132.  
  133.     case WM_TIMER: {
  134.         GetClientRect(hwnd, &WndRect);
  135.         for (int i = 0; i < balls.size(); i++) {
  136.             if (balls[i]->MoveBall())
  137.                 balls.erase(balls.begin() + i);
  138.         }
  139.         InvalidateRect(hwnd, &WndRect, true);
  140.     }break;
  141.  
  142.     case WM_CLOSE: DestroyWindow(hwnd); break;
  143.     case WM_DESTROY: PostQuitMessage(0); break;
  144.     default: return DefWindowProc(hwnd, msg, wParam, lParam);
  145.     }
  146.     return 0;
  147. }
  148.  
  149. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR ilCmdLine, int nCmdShow) {
  150.     WNDCLASSEX window;
  151.     MSG msg;
  152.     TCHAR Class_Name[] = TEXT("OKNO_TEST"), Title[] = TEXT("Pilki");
  153.  
  154.     window.cbClsExtra = NULL;
  155.     window.cbSize = sizeof(WNDCLASSEX);
  156.     window.cbWndExtra = NULL;
  157.     window.hbrBackground = (HBRUSH)(COLOR_WINDOW + 10);
  158.     window.hCursor = LoadCursor(NULL, IDC_ARROW);
  159.     window.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  160.     window.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  161.     window.hInstance = hInstance;
  162.     window.lpfnWndProc = WndProc;
  163.     window.lpszClassName = Class_Name;
  164.     window.lpszMenuName = 0;
  165.     window.style = CS_VREDRAW | CS_HREDRAW;
  166.  
  167.     RegisterClassEx(&window);
  168.  
  169.     HWND hwnd = CreateWindowEx(WS_EX_WINDOWEDGE, Class_Name, Title, WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL);
  170.  
  171.     ShowWindow(hwnd, nCmdShow);
  172.     UpdateWindow(hwnd);
  173.  
  174.     SetTimer(hwnd, 1000, 20, NULL);
  175.  
  176.     while (GetMessage(&msg, NULL, 0, 0)) {
  177.         TranslateMessage(&msg);
  178.         DispatchMessage(&msg);
  179.     }
  180.  
  181.     UnregisterClass(Class_Name, hInstance);
  182.  
  183.     return msg.wParam;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement