Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma comment(lib ,"Msimg32.lib");
- #include <Windows.h>
- #include <math.h>
- #include <vector>
- #include <string>
- PAINTSTRUCT PS;
- HDC hdc, hdcNowy;
- HBITMAP hbmOld;
- RECT WndRect;
- int Max = 0;
- HBITMAP CreateBitmapMask(HBITMAP hbmColour, COLORREF crTransparent) {
- HDC hdcMem, hdcMem2;
- HBITMAP hbmMask, hbmOld, hbmOld2;
- BITMAP bm;
- GetObject(hbmColour, sizeof(BITMAP), &bm);
- hbmMask = CreateBitmap(bm.bmWidth, bm.bmHeight, 1, 1, NULL);
- hdcMem = CreateCompatibleDC(NULL);
- hdcMem2 = CreateCompatibleDC(NULL);
- hbmOld = (HBITMAP)SelectObject(hdcMem, hbmColour);
- hbmOld2 = (HBITMAP)SelectObject(hdcMem2, hbmMask);
- SetBkColor(hdcMem, crTransparent);
- BitBlt(hdcMem2, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
- BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem2, 0, 0, SRCINVERT);
- SelectObject(hdcMem, hbmOld);
- SelectObject(hdcMem2, hbmOld2);
- DeleteDC(hdcMem);
- DeleteDC(hdcMem2);
- return hbmMask;
- }
- class Ball {
- POINT Direction;
- POINT Position;
- HBITMAP Bitmap;
- BITMAP BitmapInfo;
- HBITMAP Maska;
- int Speed = 10;
- int Y_MAX;
- bool STOP = false;
- public:
- Ball(int X, int Y, int Y_max) {
- Bitmap = (HBITMAP)LoadImage(NULL, ("BM\\p.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
- GetObject(Bitmap, sizeof(BitmapInfo), &BitmapInfo);
- Position.x = X;
- Position.y = Y;
- Direction.x = 0;
- Direction.y = 1;
- Y_MAX = Y_max;
- Maska = CreateBitmapMask(Bitmap, RGB(255, 0, 255));
- }
- bool MoveBall() {
- if (!STOP) {
- Position.x += (Direction.x*Speed);
- Position.y += (Direction.y*Speed);
- }
- if (Position.y >= Y_MAX) {
- STOP = true;
- return true;
- }
- return false;
- }
- void PaintBall(HDC &hdc, HDC &bmhdc) {
- HBITMAP hbmOld = (HBITMAP)SelectObject(hdcNowy, Bitmap);
- BitBlt(hdc, Position.x, Position.y, BitmapInfo.bmWidth, BitmapInfo.bmHeight, bmhdc, 0, 0, SRCCOPY);
- SelectObject(hdcNowy, hbmOld);
- }
- void PaintMasked(HDC &hdc, HDC &bmhdc) {
- Bitmap = (HBITMAP)LoadImage(NULL, ("BM\\p.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
- HBITMAP hbmOld = (HBITMAP)SelectObject(hdcNowy, Maska);
- BitBlt(hdc, Position.x, Position.y, BitmapInfo.bmWidth, BitmapInfo.bmHeight, hdcNowy, 0, 0, SRCAND);
- SelectObject(hdcNowy, Bitmap);
- BitBlt(hdc, Position.x, Position.y, BitmapInfo.bmWidth, BitmapInfo.bmHeight, hdcNowy, 0, 0, SRCPAINT);
- SelectObject(hdcNowy, hbmOld);
- }
- };
- std::vector<Ball *> balls;
- float MousePos_x;
- float MousePos_y;
- LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
- MousePos_x = LOWORD(lParam);
- MousePos_y = HIWORD(lParam);
- switch (msg) {
- case WM_LBUTTONDOWN: {
- GetClientRect(hwnd, &WndRect);
- balls.push_back(new Ball(MousePos_x - 25, MousePos_y - 25, WndRect.bottom - 50));
- }break;
- case WM_RBUTTONDOWN: {
- balls.clear();
- }break;
- case WM_PAINT: {
- hdc = BeginPaint(hwnd, &PS);
- hdcNowy = CreateCompatibleDC(hdc);
- for (int i = 0; i < balls.size(); i++) {
- //balls[i]->PaintBall(hdc, hdcNowy);
- balls[i]->PaintMasked(hdc, hdcNowy);
- }
- DeleteDC(hdcNowy);
- EndPaint(hwnd, &PS);
- }break;
- case WM_TIMER: {
- GetClientRect(hwnd, &WndRect);
- for (int i = 0; i < balls.size(); i++) {
- if (balls[i]->MoveBall())
- balls.erase(balls.begin() + i);
- }
- InvalidateRect(hwnd, &WndRect, true);
- }break;
- case WM_CLOSE: DestroyWindow(hwnd); break;
- case WM_DESTROY: PostQuitMessage(0); break;
- default: return DefWindowProc(hwnd, msg, wParam, lParam);
- }
- return 0;
- }
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR ilCmdLine, int nCmdShow) {
- WNDCLASSEX window;
- MSG msg;
- TCHAR Class_Name[] = TEXT("OKNO_TEST"), Title[] = TEXT("Pilki");
- window.cbClsExtra = NULL;
- window.cbSize = sizeof(WNDCLASSEX);
- window.cbWndExtra = NULL;
- window.hbrBackground = (HBRUSH)(COLOR_WINDOW + 10);
- window.hCursor = LoadCursor(NULL, IDC_ARROW);
- window.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- window.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
- window.hInstance = hInstance;
- window.lpfnWndProc = WndProc;
- window.lpszClassName = Class_Name;
- window.lpszMenuName = 0;
- window.style = CS_VREDRAW | CS_HREDRAW;
- RegisterClassEx(&window);
- HWND hwnd = CreateWindowEx(WS_EX_WINDOWEDGE, Class_Name, Title, WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL);
- ShowWindow(hwnd, nCmdShow);
- UpdateWindow(hwnd);
- SetTimer(hwnd, 1000, 20, NULL);
- while (GetMessage(&msg, NULL, 0, 0)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- UnregisterClass(Class_Name, hInstance);
- return msg.wParam;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement