Advertisement
Guest User

Jajka

a guest
Jan 3rd, 2017
100
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 <time.h>
  5. #include <vector>
  6. #include <string>
  7.  
  8. int SCORE = 0;
  9.  
  10. PAINTSTRUCT PS;
  11. HDC hdc, hdcNowy;
  12. HBITMAP hbmOld;
  13.  
  14. RECT WndRect;
  15. int Max = 0;
  16.  
  17. HBITMAP CreateBitmapMask(HBITMAP hbmColour, COLORREF crTransparent) {
  18.     HDC hdcMem, hdcMem2;
  19.     HBITMAP hbmMask, hbmOld, hbmOld2;
  20.     BITMAP bm;
  21.  
  22.     GetObject(hbmColour, sizeof(BITMAP), &bm);
  23.     hbmMask = CreateBitmap(bm.bmWidth, bm.bmHeight, 1, 1, NULL);
  24.  
  25.     hdcMem = CreateCompatibleDC(NULL);
  26.     hdcMem2 = CreateCompatibleDC(NULL);
  27.  
  28.     hbmOld = (HBITMAP)SelectObject(hdcMem, hbmColour);
  29.     hbmOld2 = (HBITMAP)SelectObject(hdcMem2, hbmMask);
  30.  
  31.     SetBkColor(hdcMem, crTransparent);
  32.  
  33.     BitBlt(hdcMem2, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
  34.     BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem2, 0, 0, SRCINVERT);
  35.  
  36.     SelectObject(hdcMem, hbmOld);
  37.     SelectObject(hdcMem2, hbmOld2);
  38.     DeleteDC(hdcMem);
  39.     DeleteDC(hdcMem2);
  40.  
  41.     return hbmMask;
  42. }
  43.  
  44. class Egg {
  45. public:
  46.     POINT Direction;
  47.     POINT Position;
  48.  
  49.     HBITMAP Bitmap;
  50.     BITMAP BitmapInfo;
  51.  
  52.     HBITMAP Maska;
  53.  
  54.     int Speed = 10;
  55.     int Y_MAX;
  56.     bool STOP = false;
  57.  
  58.  
  59.     Egg(int X, int Y, int Y_max) {
  60.         Bitmap = (HBITMAP)LoadImage(NULL, ("BM\\eggs.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  61.         GetObject(Bitmap, sizeof(BitmapInfo), &BitmapInfo);
  62.         Position.x = X;
  63.         Position.y = Y;
  64.         Direction.x = 0;
  65.         Direction.y = 1;
  66.         Y_MAX = Y_max;
  67.         Maska = CreateBitmapMask(Bitmap, RGB(0, 0, 0));
  68.     }
  69.  
  70.     bool MoveBall() {
  71.         if (!STOP) {
  72.             Position.x += (Direction.x*Speed);
  73.             Position.y += (Direction.y*Speed);
  74.         }
  75.         if (Position.y >= Y_MAX) {
  76.             STOP = true;
  77.             return true;
  78.         }
  79.         return false;
  80.     }
  81.  
  82.     void PaintMasked(HDC &hdc, HDC &bmhdc) {
  83.         Bitmap = (HBITMAP)LoadImage(NULL, ("BM\\eggs.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  84.  
  85.         HBITMAP hbmOld = (HBITMAP)SelectObject(hdcNowy, Maska);
  86.  
  87.         BitBlt(hdc, Position.x, Position.y, BitmapInfo.bmWidth, BitmapInfo.bmHeight, hdcNowy, 0, 0, SRCAND);
  88.         SelectObject(hdcNowy, Bitmap);
  89.         BitBlt(hdc, Position.x, Position.y, BitmapInfo.bmWidth, BitmapInfo.bmHeight, hdcNowy, 0, 0, SRCPAINT);
  90.  
  91.         SelectObject(hdcNowy, hbmOld);
  92.     }
  93. };
  94.  
  95. class Basket {
  96.     POINT Position;
  97.  
  98.     HBITMAP Bitmap;
  99.     BITMAP BitmapInfo;
  100.  
  101.     HBITMAP Maska;
  102. public:
  103.     Basket() {
  104.         Position.x = 300;
  105.         Position.y = 400;
  106.  
  107.         Bitmap = (HBITMAP)LoadImage(NULL, ("BM\\ba.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  108.         GetObject(Bitmap, sizeof(BitmapInfo), &BitmapInfo);
  109.         Maska = CreateBitmapMask(Bitmap, RGB(0, 0, 0));
  110.     }
  111.     void SetPos(int x) {
  112.         Position.x = x;
  113.     }
  114.  
  115.     void PaintMasked(HDC &hdc, HDC &bmhdc) {
  116.         Bitmap = (HBITMAP)LoadImage(NULL, ("BM\\ba.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  117.  
  118.         HBITMAP hbmOld = (HBITMAP)SelectObject(hdcNowy, Maska);
  119.  
  120.         BitBlt(hdc, Position.x, Position.y, BitmapInfo.bmWidth, BitmapInfo.bmHeight, hdcNowy, 0, 0, SRCAND);
  121.         SelectObject(hdcNowy, Bitmap);
  122.         BitBlt(hdc, Position.x, Position.y, BitmapInfo.bmWidth, BitmapInfo.bmHeight, hdcNowy, 0, 0, SRCPAINT);
  123.  
  124.         SelectObject(hdcNowy, hbmOld);
  125.  
  126.     }
  127.  
  128.     void Catch(std::vector<Egg> &egg) {
  129.         for (int i = 0; i < egg.size(); i++) {
  130.             if (Position.x<=egg[i].Position.x &&
  131.                 Position.x+ BitmapInfo.bmWidth >= egg[i].Position.x+egg[i].BitmapInfo.bmWidth) {
  132.                 if (egg[i].Position.y > Position.y) {
  133.                     egg.erase(egg.begin() + i);
  134.                     SCORE++;
  135.                 }
  136.             }
  137.         }
  138.     }
  139. };
  140.  
  141. std::vector<Egg> eggs;
  142. Basket *basket = new Basket();
  143. float MousePos_x;
  144. float MousePos_y;
  145.  
  146. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
  147.     //MousePos_x = LOWORD(lParam);
  148.     //MousePos_y = HIWORD(lParam);
  149.  
  150.     switch (msg) {
  151.  
  152.     case WM_RBUTTONDOWN: {
  153.         eggs.clear();
  154.     }break;
  155.  
  156.     case WM_PAINT: {
  157.         hdc = BeginPaint(hwnd, &PS);
  158.  
  159.         hdcNowy = CreateCompatibleDC(hdc);
  160.  
  161.         char buff[255];
  162.         wsprintf(buff, "Wynik: %d", SCORE);
  163.         TextOut(hdc, 5, 5, buff, std::string(buff).length());
  164.  
  165.  
  166.         for (int i = 0; i < eggs.size(); i++) {
  167.             eggs[i].PaintMasked(hdc, hdcNowy);
  168.         }
  169.         basket->PaintMasked(hdc, hdcNowy);
  170.  
  171.         DeleteDC(hdcNowy);
  172.  
  173.         EndPaint(hwnd, &PS);
  174.     }break;
  175.  
  176.     case WM_TIMER: {
  177.  
  178.         GetClientRect(hwnd, &WndRect);
  179.  
  180.         if (rand() % 100 > 95)
  181.             eggs.push_back(Egg((int)(rand() % WndRect.right - 100) + 50, 50, WndRect.bottom - 50));
  182.  
  183.         POINT p;
  184.         GetCursorPos(&p);
  185.         ScreenToClient(hwnd, &p);
  186.  
  187.         basket->SetPos(p.x -100);
  188.         basket->Catch(eggs);
  189.  
  190.         for (int i = 0; i < eggs.size(); i++) {
  191.             if (eggs[i].MoveBall()) {
  192.                 //SCORE--;
  193.                 eggs.erase(eggs.begin() + i);
  194.             }
  195.         }
  196.         InvalidateRect(hwnd, &WndRect, true);
  197.     }break;
  198.  
  199.     case WM_CLOSE: DestroyWindow(hwnd); break;
  200.     case WM_DESTROY: PostQuitMessage(0); break;
  201.     default: return DefWindowProc(hwnd, msg, wParam, lParam);
  202.     }
  203.     return 0;
  204. }
  205.  
  206. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR ilCmdLine, int nCmdShow) {
  207.     srand(time(NULL));
  208.     WNDCLASSEX window;
  209.     MSG msg;
  210.     TCHAR Class_Name[] = TEXT("OKNO_TEST"), Title[] = TEXT("Pilki");
  211.  
  212.     window.cbClsExtra = NULL;
  213.     window.cbSize = sizeof(WNDCLASSEX);
  214.     window.cbWndExtra = NULL;
  215.     window.hbrBackground = (HBRUSH)(COLOR_WINDOW + 10);
  216.     window.hCursor = LoadCursor(NULL, IDC_ARROW);
  217.     window.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  218.     window.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  219.     window.hInstance = hInstance;
  220.     window.lpfnWndProc = WndProc;
  221.     window.lpszClassName = Class_Name;
  222.     window.lpszMenuName = 0;
  223.     window.style = CS_VREDRAW | CS_HREDRAW;
  224.  
  225.     RegisterClassEx(&window);
  226.  
  227.     HWND hwnd = CreateWindowEx(WS_EX_WINDOWEDGE, Class_Name, Title, WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL);
  228.  
  229.     ShowWindow(hwnd, nCmdShow);
  230.     UpdateWindow(hwnd);
  231.  
  232.     SetTimer(hwnd, 1000, 20, NULL);
  233.  
  234.     while (GetMessage(&msg, NULL, 0, 0)) {
  235.         TranslateMessage(&msg);
  236.         DispatchMessage(&msg);
  237.     }
  238.  
  239.     UnregisterClass(Class_Name, hInstance);
  240.  
  241.     return msg.wParam;
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement