Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <time.h>
- //Author: name22 (www.autoit.de)
- #define WIDTH 600
- #define HEIGHT 600
- #define ID_VOID 0
- #define ID_TREE 1
- #define ID_FIRE 2
- #define COLOR_VOID RGB(0, 0, 0)
- #define COLOR_TREE RGB(0, 255, 0)
- #define COLOR_FIRE RGB(255, 0, 0)
- #define P_INIT 0.55
- #define P_GROW 0.008
- #define P_FIRE 0.000001
- HDC hDC_Bitmap;
- BYTE aGrid[WIDTH][HEIGHT], aGridOld[WIDTH][HEIGHT];
- VOID OnPaint(HDC);
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PSTR, INT iCmdShow)
- {
- HWND hWnd;
- MSG msg;
- WNDCLASS wndClass;
- BOOL bQuit = FALSE;
- HDC hDC_Window;
- HBITMAP hBitmap;
- HGDIOBJ hOldObj;
- wndClass.style = CS_HREDRAW | CS_VREDRAW;
- wndClass.lpfnWndProc = WndProc;
- wndClass.cbClsExtra = 0;
- wndClass.cbWndExtra = 0;
- wndClass.hInstance = hInstance;
- wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
- wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
- wndClass.lpszMenuName = NULL;
- wndClass.lpszClassName = TEXT("GDIWindow");
- RegisterClass(&wndClass);
- hWnd = CreateWindow(
- TEXT("GDIWindow"), // window class name
- TEXT("GDI32 - Forest Fire"), // window caption
- WS_OVERLAPPEDWINDOW, // window style
- CW_USEDEFAULT, // initial x position
- CW_USEDEFAULT, // initial y position
- WIDTH, // initial x size
- HEIGHT, // initial y size
- NULL, // parent window handle
- NULL, // window menu handle
- hInstance, // program instance handle
- NULL); // creation parameters
- hDC_Window = GetDC(hWnd);
- hDC_Bitmap = CreateCompatibleDC(hDC_Window);
- hBitmap = CreateCompatibleBitmap(hDC_Window, WIDTH, HEIGHT);
- hOldObj = SelectObject(hDC_Bitmap, hBitmap);
- //Fill grids with zeros
- memset(aGrid, ID_VOID, (sizeof aGrid[0][0]) * WIDTH * HEIGHT);
- memset(aGridOld, ID_VOID, (sizeof aGridOld[0][0]) * WIDTH * HEIGHT);
- //Seed random-number generator with time
- srand((unsigned) time(NULL));
- //Plant trees
- for (int y = 0; y < HEIGHT; y++) for (int x = 0; x < WIDTH; x++) if (rand() < RAND_MAX * P_INIT)
- {
- aGrid[x][y] = ID_TREE;
- SetPixelV(hDC_Bitmap, x, y, COLOR_TREE);
- }
- memcpy(aGridOld, aGrid, sizeof aGrid);
- ShowWindow(hWnd, iCmdShow); //Make GUI visible
- BitBlt(hDC_Window, 0, 0, WIDTH, HEIGHT, hDC_Bitmap, 0, 0, SRCCOPY); //Swap buffers
- while(!bQuit)
- {
- if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
- {
- if (msg.message == WM_QUIT) bQuit = TRUE;
- else
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
- else
- {
- for (int y = 0; y < HEIGHT; y++)
- {
- for (int x = 0; x < WIDTH; x++)
- {
- switch (aGridOld[x][y])
- {
- case ID_VOID: //Cell is void
- if (rand() < RAND_MAX * P_GROW) //Grow tree with propability of P_GROW
- {
- aGrid[x][y] = ID_TREE;
- SetPixelV(hDC_Bitmap, x, y, COLOR_TREE);
- }
- break;
- case ID_TREE: //Cell is a tree
- for (int i = -1; i < 2; i++) for (int j = -1; j < 2; j++) //8 bounding neighbours
- {
- if (i != 0 && j != 0 && x + i >= 0 && x + i < WIDTH && y + j >= 0 && y + j < HEIGHT) //Check if neighbouring cell is valid
- {
- if (aGridOld[x + i][y + j] == ID_FIRE) //Set cell on fire if a neighbouring cell is on fire
- {
- aGrid[x][y] = ID_FIRE;
- SetPixelV(hDC_Bitmap, x, y, COLOR_FIRE);
- goto CELL_DONE;
- }
- }
- else
- {
- if (rand() < RAND_MAX * P_FIRE) //Set cell on fire anyway with propability of 0.01%
- {
- aGrid[x][y] = ID_FIRE;
- SetPixelV(hDC_Bitmap, x, y, COLOR_FIRE);
- goto CELL_DONE;
- }
- }
- }
- break;
- case ID_FIRE: //Cell is on fire -> decay into void
- aGrid[x][y] = ID_VOID;
- SetPixelV(hDC_Bitmap, x, y, COLOR_VOID);
- break;
- }
- CELL_DONE:;
- }
- }
- BitBlt(hDC_Window, 0, 0, WIDTH, HEIGHT, hDC_Bitmap, 0, 0, SRCCOPY); //Swap buffers
- memcpy(aGridOld, aGrid, sizeof aGrid); //Copy new grid into old one
- }
- }
- SelectObject(hDC_Bitmap, hOldObj);
- ReleaseDC(hWnd, hDC_Window);
- DeleteDC(hDC_Bitmap);
- DeleteObject(hBitmap);
- return msg.wParam;
- } // WinMain
- VOID OnPaint(HDC hDC)
- {
- BitBlt(hDC, 0, 0, WIDTH, HEIGHT, hDC_Bitmap, 0, 0, SRCCOPY);
- }
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message,
- WPARAM wParam, LPARAM lParam)
- {
- HDC hDC;
- PAINTSTRUCT ps;
- switch(message)
- {
- case WM_PAINT:
- hDC = BeginPaint(hWnd, &ps);
- OnPaint(hDC);
- EndPaint(hWnd, &ps);
- return 0;
- case WM_DESTROY:
- PostQuitMessage(0);
- return 0;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- } // WndProc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement