Advertisement
name22

GDI32 - Forest Fire

Mar 9th, 2013
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.21 KB | None | 0 0
  1. #include <windows.h>
  2. #include <time.h>
  3.  
  4. //Author: name22 (www.autoit.de)
  5.  
  6. #define WIDTH 600
  7. #define HEIGHT 600
  8. #define ID_VOID 0
  9. #define ID_TREE 1
  10. #define ID_FIRE 2
  11. #define COLOR_VOID RGB(0, 0, 0)
  12. #define COLOR_TREE RGB(0, 255, 0)
  13. #define COLOR_FIRE RGB(255, 0, 0)
  14.  
  15. #define P_INIT 0.55
  16. #define P_GROW 0.008
  17. #define P_FIRE 0.000001
  18.  
  19. HDC hDC_Bitmap;
  20. BYTE aGrid[WIDTH][HEIGHT], aGridOld[WIDTH][HEIGHT];
  21.  
  22. VOID OnPaint(HDC);
  23. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  24.  
  25. INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PSTR, INT iCmdShow)
  26. {
  27.     HWND                hWnd;
  28.     MSG                 msg;
  29.     WNDCLASS            wndClass;
  30.     BOOL                bQuit = FALSE;
  31.  
  32.     HDC                 hDC_Window;
  33.     HBITMAP             hBitmap;
  34.     HGDIOBJ             hOldObj;
  35.  
  36.     wndClass.style          = CS_HREDRAW | CS_VREDRAW;
  37.     wndClass.lpfnWndProc    = WndProc;
  38.     wndClass.cbClsExtra     = 0;
  39.     wndClass.cbWndExtra     = 0;
  40.     wndClass.hInstance      = hInstance;
  41.     wndClass.hIcon          = LoadIcon(NULL, IDI_APPLICATION);
  42.     wndClass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  43.     wndClass.hbrBackground  = (HBRUSH)GetStockObject(WHITE_BRUSH);
  44.     wndClass.lpszMenuName   = NULL;
  45.     wndClass.lpszClassName  = TEXT("GDIWindow");
  46.  
  47.     RegisterClass(&wndClass);
  48.  
  49.     hWnd = CreateWindow(
  50.                TEXT("GDIWindow"),   // window class name
  51.                TEXT("GDI32 - Forest Fire"),  // window caption
  52.                WS_OVERLAPPEDWINDOW,      // window style
  53.                CW_USEDEFAULT,            // initial x position
  54.                CW_USEDEFAULT,            // initial y position
  55.                WIDTH,                    // initial x size
  56.                HEIGHT,                   // initial y size
  57.                NULL,                     // parent window handle
  58.                NULL,                     // window menu handle
  59.                hInstance,                // program instance handle
  60.                NULL);                    // creation parameters
  61.  
  62.     hDC_Window = GetDC(hWnd);
  63.     hDC_Bitmap = CreateCompatibleDC(hDC_Window);
  64.     hBitmap = CreateCompatibleBitmap(hDC_Window, WIDTH, HEIGHT);
  65.     hOldObj = SelectObject(hDC_Bitmap, hBitmap);
  66.  
  67.     //Fill grids with zeros
  68.     memset(aGrid, ID_VOID, (sizeof aGrid[0][0]) * WIDTH * HEIGHT);
  69.     memset(aGridOld, ID_VOID, (sizeof aGridOld[0][0]) * WIDTH * HEIGHT);
  70.  
  71.     //Seed random-number generator with time
  72.     srand((unsigned) time(NULL));
  73.  
  74.     //Plant trees
  75.     for (int y = 0; y < HEIGHT; y++) for (int x = 0; x < WIDTH; x++) if (rand() < RAND_MAX * P_INIT)
  76.     {
  77.         aGrid[x][y] = ID_TREE;
  78.         SetPixelV(hDC_Bitmap, x, y, COLOR_TREE);
  79.     }
  80.     memcpy(aGridOld, aGrid, sizeof aGrid);
  81.  
  82.     ShowWindow(hWnd, iCmdShow); //Make GUI visible
  83.  
  84.     BitBlt(hDC_Window, 0, 0, WIDTH, HEIGHT, hDC_Bitmap, 0, 0, SRCCOPY); //Swap buffers
  85.  
  86.     while(!bQuit)
  87.     {
  88.         if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  89.         {
  90.             if (msg.message == WM_QUIT) bQuit = TRUE;
  91.             else
  92.             {
  93.                 TranslateMessage(&msg);
  94.                 DispatchMessage(&msg);
  95.             }
  96.         }
  97.         else
  98.         {
  99.             for (int y = 0; y < HEIGHT; y++)
  100.             {
  101.                 for (int x = 0; x < WIDTH; x++)
  102.                 {
  103.                     switch (aGridOld[x][y])
  104.                     {
  105.                     case ID_VOID: //Cell is void
  106.                         if (rand() < RAND_MAX * P_GROW) //Grow tree with propability of P_GROW
  107.                         {
  108.                             aGrid[x][y] = ID_TREE;
  109.                             SetPixelV(hDC_Bitmap, x, y, COLOR_TREE);
  110.                         }
  111.                         break;
  112.                     case ID_TREE: //Cell is a tree
  113.                         for (int i = -1; i < 2; i++) for (int j = -1; j < 2; j++) //8 bounding neighbours
  114.                         {
  115.                             if (i != 0 && j != 0 && x + i >= 0 && x + i < WIDTH && y + j >= 0 && y + j < HEIGHT) //Check if neighbouring cell is valid
  116.                             {
  117.                                 if (aGridOld[x + i][y + j] == ID_FIRE) //Set cell on fire if a neighbouring cell is on fire
  118.                                 {
  119.                                     aGrid[x][y] = ID_FIRE;
  120.                                     SetPixelV(hDC_Bitmap, x, y, COLOR_FIRE);
  121.                                     goto CELL_DONE;
  122.                                 }
  123.                             }
  124.                             else
  125.                             {
  126.                                 if (rand() < RAND_MAX * P_FIRE) //Set cell on fire anyway with propability of 0.01%
  127.                                 {
  128.                                     aGrid[x][y] = ID_FIRE;
  129.                                     SetPixelV(hDC_Bitmap, x, y, COLOR_FIRE);
  130.                                     goto CELL_DONE;
  131.                                 }
  132.                             }
  133.                         }
  134.                         break;
  135.                     case ID_FIRE: //Cell is on fire -> decay into void
  136.                         aGrid[x][y] = ID_VOID;
  137.                         SetPixelV(hDC_Bitmap, x, y, COLOR_VOID);
  138.                         break;
  139.                     }
  140.                     CELL_DONE:;
  141.                 }
  142.             }
  143.             BitBlt(hDC_Window, 0, 0, WIDTH, HEIGHT, hDC_Bitmap, 0, 0, SRCCOPY); //Swap buffers
  144.             memcpy(aGridOld, aGrid, sizeof aGrid); //Copy new grid into old one
  145.         }
  146.     }
  147.  
  148.     SelectObject(hDC_Bitmap, hOldObj);
  149.     ReleaseDC(hWnd, hDC_Window);
  150.     DeleteDC(hDC_Bitmap);
  151.     DeleteObject(hBitmap);
  152.  
  153.     return msg.wParam;
  154. }  // WinMain
  155.  
  156. VOID OnPaint(HDC hDC)
  157. {
  158.     BitBlt(hDC, 0, 0, WIDTH, HEIGHT, hDC_Bitmap, 0, 0, SRCCOPY);
  159. }
  160.  
  161. LRESULT CALLBACK WndProc(HWND hWnd, UINT message,
  162.                          WPARAM wParam, LPARAM lParam)
  163. {
  164.     HDC          hDC;
  165.     PAINTSTRUCT  ps;
  166.  
  167.     switch(message)
  168.     {
  169.     case WM_PAINT:
  170.         hDC = BeginPaint(hWnd, &ps);
  171.         OnPaint(hDC);
  172.         EndPaint(hWnd, &ps);
  173.         return 0;
  174.     case WM_DESTROY:
  175.         PostQuitMessage(0);
  176.         return 0;
  177.     default:
  178.         return DefWindowProc(hWnd, message, wParam, lParam);
  179.     }
  180. } // WndProc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement