Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <vector>
- #include <ctime>
- #define SCREEN_WIDTH 1240
- #define SCREEN_HEIGHT 720
- #define MAP_X 100
- #define MAP_Y 80
- #define SNOW_SIZE 1
- #define TIMER_ID 1
- const COLORREF backgroundColor = RGB(0, 0, 128);
- const COLORREF snowColor = RGB(255, 255, 255);
- RECT windowRect;
- struct Snow {
- POINT SnowPos;
- UINT snowVel;
- BOOL isFloor;
- UINT timeOnFloor;
- };
- std::vector<Snow>snowFlakes;
- HINSTANCE hInstance;
- LRESULT CALLBACK ProceduraOkna(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
- bool RejestracjaKlasy(WNDCLASSEX &window)
- {
- window.cbSize = sizeof(WNDCLASSEX);
- window.style = CS_VREDRAW | CS_HREDRAW;
- window.lpfnWndProc = ProceduraOkna;
- window.cbClsExtra = 0;
- window.cbWndExtra = 0;
- window.hInstance = hInstance;
- window.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- window.hCursor = LoadCursor(NULL, IDC_ARROW);
- window.hbrBackground = (HBRUSH)CreateSolidBrush(backgroundColor);
- window.lpszMenuName = NULL;
- window.lpszClassName = "StandardoweOkno";
- window.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
- if (!RegisterClassEx(&window))
- {
- MessageBox(NULL, "Problem z rejestracją okna", "Rejestracja okna", MB_ICONERROR | MB_OK);
- return false;
- }
- return true;
- }
- void initSnow(RECT &windowRect)
- {
- Snow s;
- s.SnowPos.x = rand() % MAP_X;
- s.SnowPos.y = 0;
- s.isFloor = 0;
- s.snowVel = 1;
- s.timeOnFloor = 0;
- snowFlakes.push_back(s);
- }
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
- {
- WNDCLASSEX window;
- if (!RejestracjaKlasy(window))
- return 1;
- HWND uchwytOkna = 0;
- uchwytOkna = CreateWindowEx(
- WS_EX_APPWINDOW | WS_EX_CLIENTEDGE,
- "StandardoweOkno",
- "Pierwsze okno",
- WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- SCREEN_WIDTH,
- SCREEN_HEIGHT,
- 0,
- 0,
- hInstance,
- 0);
- ShowWindow(uchwytOkna, nCmdShow);
- UpdateWindow(uchwytOkna);
- MSG msg;
- for (; ;)
- {
- if (0 != GetMessage(&msg, 0, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- if (msg.message == WM_QUIT) break;
- }
- return (int)msg.wParam;
- }
- LRESULT CALLBACK ProceduraOkna(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- HDC hdc;
- static PAINTSTRUCT paint;
- static HBRUSH whiteBrush;
- static HPEN whitePen;
- switch (message)
- {
- case WM_CREATE:
- GetClientRect(hWnd, &windowRect);
- whiteBrush = CreateSolidBrush(snowColor);
- whitePen = CreatePen(PS_SOLID, 0, snowColor);
- SetTimer(hWnd, 100, TIMER_ID, NULL);
- srand((unsigned)time(NULL));
- break;
- case WM_PAINT:
- {
- hdc = BeginPaint(hWnd, &paint);
- SetMapMode(hdc, MM_ANISOTROPIC);
- SetViewportExtEx(hdc, windowRect.right, windowRect.bottom, NULL);
- SetViewportOrgEx(hdc, 0, 0, NULL);
- SetWindowExtEx(hdc, MAP_X, MAP_Y, NULL);
- SetWindowOrgEx(hdc, 0, 0, NULL);
- SelectObject(hdc, whiteBrush);
- SelectObject(hdc, whitePen);
- if (snowFlakes.size() > 0)
- for (std::vector<Snow>::iterator it = snowFlakes.begin(); it != snowFlakes.end(); it++)
- Rectangle(hdc, it->SnowPos.x, it->SnowPos.y, it->SnowPos.x + SNOW_SIZE, it->SnowPos.y + SNOW_SIZE);
- EndPaint(hWnd, &paint);
- }
- break;
- case WM_SIZE:
- GetClientRect(hWnd, &windowRect);
- break;
- case WM_TIMER:
- {
- initSnow(windowRect);
- if (snowFlakes.size() > 0)
- for (std::vector<Snow>::iterator it = snowFlakes.begin(); it != snowFlakes.end(); it++)
- if (it->SnowPos.y < MAP_Y - SNOW_SIZE)
- it->SnowPos.y += it->snowVel;
- else
- it->isFloor = true;
- ////////////////////////////////////////////////////////////////////////////////////////////////
- int x = -1;
- if (snowFlakes.size() > 0)
- for (std::vector<Snow>::iterator it = snowFlakes.begin(); it != snowFlakes.end(); it++)
- if (it->isFloor)
- {
- it->timeOnFloor++;
- if (it->timeOnFloor >= 10)
- {
- x = it->SnowPos.x;
- snowFlakes.erase(it);
- break;
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- if (snowFlakes.size() > 0)
- for (std::vector<Snow>::iterator it = snowFlakes.begin(); it != snowFlakes.end(); it++)
- for (std::vector<Snow>::iterator ite = snowFlakes.begin(); ite != snowFlakes.end(); ite++)
- if (it != ite && it->SnowPos.y + 1 == ite->SnowPos.y && it->SnowPos.x == ite->SnowPos.x)
- it->snowVel = 0;
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- if (snowFlakes.size() > 0 && x > -1)
- for (std::vector<Snow>::iterator it = snowFlakes.begin(); it != snowFlakes.end(); it++)
- if (it->SnowPos.x == x)
- it->snowVel = 1;
- InvalidateRect(hWnd, NULL, 1);
- }
- break;
- case WM_CLOSE:
- if (MessageBox(hWnd, "Czy chcesz zakonczyc dzialanie programu?", "Dzialanie programu", MB_YESNO | MB_ICONERROR) == IDNO)
- break;
- case WM_DESTROY:
- KillTimer(hWnd, TIMER_ID);
- PostQuitMessage(0);
- break;
- case WM_LBUTTONDOWN:
- snowFlakes.clear();
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement