+++ /dev/null
-/******************************************************************************\r
- * [ maze ] ...\r
- *\r
- * modified: [ 03-08-15 ] Ge van Geldorp <ge@gse.nl>\r
- * ported to Reactos\r
- * modified: [ 94-10-8 ] Ge van Geldorp <Ge.vanGeldorp@lr.tudelft.nl>\r
- * ported to MS Windows\r
- * modified: [ 3-7-93 ] Jamie Zawinski <jwz@lucid.com>\r
- * added the XRoger logo, cleaned up resources, made\r
- * grid size a parameter.\r
- * modified: [ 3-3-93 ] Jim Randell <jmr@mddjmr.fc.hp.com>\r
- * Added the colour stuff and integrated it with jwz's\r
- * screenhack stuff. There's still some work that could\r
- * be done on this, particularly allowing a resource to\r
- * specify how big the squares are.\r
- * modified: [ 10-4-88 ] Richard Hess ...!uunet!cimshop!rhess\r
- * [ Revised primary execution loop within main()...\r
- * [ Extended X event handler, check_events()...\r
- * modified: [ 1-29-88 ] Dave Lemke lemke@sun.com\r
- * [ Hacked for X11...\r
- * [ Note the word "hacked" -- this is extremely ugly, but at\r
- * [ least it does the job. NOT a good programming example\r
- * [ for X.\r
- * original: [ 6/21/85 ] Martin Weiss Sun Microsystems [ SunView ]\r
- *\r
- ******************************************************************************\r
- Copyright 1988 by Sun Microsystems, Inc. Mountain View, CA.\r
-\r
- All Rights Reserved\r
-\r
- Permission to use, copy, modify, and distribute this software and its\r
- documentation for any purpose and without fee is hereby granted,\r
- provided that the above copyright notice appear in all copies and that\r
- both that copyright notice and this permission notice appear in\r
- supporting documentation, and that the names of Sun or MIT not be\r
- used in advertising or publicity pertaining to distribution of the\r
- software without specific prior written permission. Sun and M.I.T.\r
- make no representations about the suitability of this software for\r
- any purpose. It is provided "as is" without any express or implied warranty.\r
-\r
- SUN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING\r
- ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
- PURPOSE. IN NO EVENT SHALL SUN BE LIABLE FOR ANY SPECIAL, INDIRECT\r
- OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS\r
- OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE\r
- OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE\r
- OR PERFORMANCE OF THIS SOFTWARE.\r
- *****************************************************************************/\r
-\r
-#define STRICT\r
-\r
-#include <stdlib.h>\r
-#include <string.h>\r
-#include <time.h>\r
-#include <windows.h> /* required for all Windows applications */\r
-\r
-#if !defined (APIENTRY) /* Windows NT defines APIENTRY, but 3.x doesn't */\r
-#define APIENTRY far pascal\r
-#endif\r
-\r
-#if !defined(WIN32) /* Windows 3.x uses a FARPROC for dialogs */\r
-#define DLGPROC FARPROC\r
-#endif\r
-\r
-static BOOL InitInstance(HINSTANCE hInstance, HWND hParent);\r
-LRESULT CALLBACK MazeWndProc(HWND hWnd, UINT message, WPARAM uParam,\r
- LPARAM lParam);\r
-\r
-HINSTANCE hInst; /* current instance */\r
-HWND hWnd; /* Main window handle.*/\r
-HBRUSH hBrushDead;\r
-HBRUSH hBrushLiving;\r
-HPEN hPenWall;\r
-HDC hDC;\r
-static BOOL waiting;\r
-\r
-\r
-WCHAR szAppName[] = L"Maze"; /* The name of this application */\r
-WCHAR szTitle[] = L"Maze"; /* The title bar text */\r
-\r
-static int solve_delay, pre_solve_delay, post_solve_delay;\r
-\r
-#define MAX_MAZE_SIZE_X ((unsigned long) 250)\r
-#define MAX_MAZE_SIZE_Y ((unsigned long) 250)\r
-\r
-#define MOVE_LIST_SIZE (MAX_MAZE_SIZE_X * MAX_MAZE_SIZE_Y)\r
-\r
-#define WALL_TOP 0x8000\r
-#define WALL_RIGHT 0x4000\r
-#define WALL_BOTTOM 0x2000\r
-#define WALL_LEFT 0x1000\r
-\r
-#define DOOR_IN_TOP 0x800\r
-#define DOOR_IN_RIGHT 0x400\r
-#define DOOR_IN_BOTTOM 0x200\r
-#define DOOR_IN_LEFT 0x100\r
-#define DOOR_IN_ANY 0xF00\r
-\r
-#define DOOR_OUT_TOP 0x80\r
-#define DOOR_OUT_RIGHT 0x40\r
-#define DOOR_OUT_BOTTOM 0x20\r
-#define DOOR_OUT_LEFT 0x10\r
-\r
-#define START_SQUARE 0x2\r
-#define END_SQUARE 0x1\r
-\r
-#define border_x (0)\r
-#define border_y (0)\r
-\r
-#define get_random(x) (rand() % (x))\r
-\r
-static unsigned short maze[MAX_MAZE_SIZE_X][MAX_MAZE_SIZE_Y];\r
-\r
-static struct {\r
- unsigned char x;\r
- unsigned char y;\r
- unsigned char dir;\r
- unsigned char dummy;\r
-} move_list[MOVE_LIST_SIZE], save_path[MOVE_LIST_SIZE], path[MOVE_LIST_SIZE];\r
-\r
-static int maze_size_x, maze_size_y;\r
-static long sqnum, path_length;\r
-static int cur_sq_x, cur_sq_y;\r
-static int start_x, start_y, start_dir, end_x, end_y, end_dir;\r
-static int grid_width, grid_height;\r
-\r
-static int state = 1, pathi = 0;\r
-\r
-static void\r
-set_maze_sizes (width, height)\r
- int width, height;\r
-{\r
- maze_size_x = width / grid_width;\r
- maze_size_y = height / grid_height;\r
-}\r
-\r
-\r
-static void\r
-initialize_maze() /* draw the surrounding wall and start/end squares */\r
-{\r
- register int i, j, wall;\r
-\r
- /* initialize all squares */\r
- for ( i=0; i<maze_size_x; i++) {\r
- for ( j=0; j<maze_size_y; j++) {\r
- maze[i][j] = 0;\r
- }\r
- }\r
-\r
- /* top wall */\r
- for ( i=0; i<maze_size_x; i++ ) {\r
- maze[i][0] |= WALL_TOP;\r
- }\r
-\r
- /* right wall */\r
- for ( j=0; j<maze_size_y; j++ ) {\r
- maze[maze_size_x-1][j] |= WALL_RIGHT;\r
- }\r
-\r
- /* bottom wall */\r
- for ( i=0; i<maze_size_x; i++ ) {\r
- maze[i][maze_size_y-1] |= WALL_BOTTOM;\r
- }\r
-\r
- /* left wall */\r
- for ( j=0; j<maze_size_y; j++ ) {\r
- maze[0][j] |= WALL_LEFT;\r
- }\r
-\r
- /* set start square */\r
- wall = get_random(4);\r
- switch (wall) {\r
- case 0:\r
- i = get_random(maze_size_x);\r
- j = 0;\r
- break;\r
- case 1:\r
- i = maze_size_x - 1;\r
- j = get_random(maze_size_y);\r
- break;\r
- case 2:\r
- i = get_random(maze_size_x);\r
- j = maze_size_y - 1;\r
- break;\r
- case 3:\r
- i = 0;\r
- j = get_random(maze_size_y);\r
- break;\r
- }\r
- maze[i][j] |= START_SQUARE;\r
- maze[i][j] |= ( DOOR_IN_TOP >> wall );\r
- maze[i][j] &= ~( WALL_TOP >> wall );\r
- cur_sq_x = i;\r
- cur_sq_y = j;\r
- start_x = i;\r
- start_y = j;\r
- start_dir = wall;\r
- sqnum = 0;\r
-\r
- /* set end square */\r
- wall = (wall + 2)%4;\r
- switch (wall) {\r
- case 0:\r
- i = get_random(maze_size_x);\r
- j = 0;\r
- break;\r
- case 1:\r
- i = maze_size_x - 1;\r
- j = get_random(maze_size_y);\r
- break;\r
- case 2:\r
- i = get_random(maze_size_x);\r
- j = maze_size_y - 1;\r
- break;\r
- case 3:\r
- i = 0;\r
- j = get_random(maze_size_y);\r
- break;\r
- }\r
- maze[i][j] |= END_SQUARE;\r
- maze[i][j] |= ( DOOR_OUT_TOP >> wall );\r
- maze[i][j] &= ~( WALL_TOP >> wall );\r
- end_x = i;\r
- end_y = j;\r
- end_dir = wall;\r
-}\r
-\r
-static int choose_door ();\r
-static long backup ();\r
-static void draw_wall ();\r
-static void draw_solid_square(int, int, int, HDC, HBRUSH);\r
-static void enter_square(int, HDC, HBRUSH);\r
-\r
-static void\r
-create_maze() /* create a maze layout given the intiialized maze */\r
-{\r
- register int i, newdoor = 0;\r
- HDC hDC;\r
-\r
- hDC = GetDC(hWnd);\r
- do {\r
- move_list[sqnum].x = cur_sq_x;\r
- move_list[sqnum].y = cur_sq_y;\r
- move_list[sqnum].dir = newdoor;\r
- while ( ( newdoor = choose_door(hDC) ) == -1 ) { /* pick a door */\r
- if ( backup() == -1 ) { /* no more doors ... backup */\r
- ReleaseDC(hWnd, hDC);\r
- return; /* done ... return */\r
- }\r
- }\r
-\r
- /* mark the out door */\r
- maze[cur_sq_x][cur_sq_y] |= ( DOOR_OUT_TOP >> newdoor );\r
-\r
- switch (newdoor) {\r
- case 0: cur_sq_y--;\r
- break;\r
- case 1: cur_sq_x++;\r
- break;\r
- case 2: cur_sq_y++;\r
- break;\r
- case 3: cur_sq_x--;\r
- break;\r
- }\r
- sqnum++;\r
-\r
- /* mark the in door */\r
- maze[cur_sq_x][cur_sq_y] |= ( DOOR_IN_TOP >> ((newdoor+2)%4) );\r
-\r
- /* if end square set path length and save path */\r
- if ( maze[cur_sq_x][cur_sq_y] & END_SQUARE ) {\r
- path_length = sqnum;\r
- for ( i=0; i<path_length; i++) {\r
- save_path[i].x = move_list[i].x;\r
- save_path[i].y = move_list[i].y;\r
- save_path[i].dir = move_list[i].dir;\r
- }\r
- }\r
-\r
- } while (1);\r
-\r
-}\r
-\r
-\r
-static int\r
-choose_door(HDC hDC) /* pick a new path */\r
-{\r
- int candidates[3];\r
- register int num_candidates;\r
-\r
- num_candidates = 0;\r
-\r
- /* top wall */\r
- if ( maze[cur_sq_x][cur_sq_y] & DOOR_IN_TOP )\r
- goto rightwall;\r
- if ( maze[cur_sq_x][cur_sq_y] & DOOR_OUT_TOP )\r
- goto rightwall;\r
- if ( maze[cur_sq_x][cur_sq_y] & WALL_TOP )\r
- goto rightwall;\r
- if ( maze[cur_sq_x][cur_sq_y - 1] & DOOR_IN_ANY ) {\r
- maze[cur_sq_x][cur_sq_y] |= WALL_TOP;\r
- maze[cur_sq_x][cur_sq_y - 1] |= WALL_BOTTOM;\r
- draw_wall(cur_sq_x, cur_sq_y, 0, hDC);\r
- goto rightwall;\r
- }\r
- candidates[num_candidates++] = 0;\r
-\r
- rightwall:\r
- /* right wall */\r
- if ( maze[cur_sq_x][cur_sq_y] & DOOR_IN_RIGHT )\r
- goto bottomwall;\r
- if ( maze[cur_sq_x][cur_sq_y] & DOOR_OUT_RIGHT )\r
- goto bottomwall;\r
- if ( maze[cur_sq_x][cur_sq_y] & WALL_RIGHT )\r
- goto bottomwall;\r
- if ( maze[cur_sq_x + 1][cur_sq_y] & DOOR_IN_ANY ) {\r
- maze[cur_sq_x][cur_sq_y] |= WALL_RIGHT;\r
- maze[cur_sq_x + 1][cur_sq_y] |= WALL_LEFT;\r
- draw_wall(cur_sq_x, cur_sq_y, 1, hDC);\r
- goto bottomwall;\r
- }\r
- candidates[num_candidates++] = 1;\r
-\r
- bottomwall:\r
- /* bottom wall */\r
- if ( maze[cur_sq_x][cur_sq_y] & DOOR_IN_BOTTOM )\r
- goto leftwall;\r
- if ( maze[cur_sq_x][cur_sq_y] & DOOR_OUT_BOTTOM )\r
- goto leftwall;\r
- if ( maze[cur_sq_x][cur_sq_y] & WALL_BOTTOM )\r
- goto leftwall;\r
- if ( maze[cur_sq_x][cur_sq_y + 1] & DOOR_IN_ANY ) {\r
- maze[cur_sq_x][cur_sq_y] |= WALL_BOTTOM;\r
- maze[cur_sq_x][cur_sq_y + 1] |= WALL_TOP;\r
- draw_wall(cur_sq_x, cur_sq_y, 2, hDC);\r
- goto leftwall;\r
- }\r
- candidates[num_candidates++] = 2;\r
-\r
- leftwall:\r
- /* left wall */\r
- if ( maze[cur_sq_x][cur_sq_y] & DOOR_IN_LEFT )\r
- goto donewall;\r
- if ( maze[cur_sq_x][cur_sq_y] & DOOR_OUT_LEFT )\r
- goto donewall;\r
- if ( maze[cur_sq_x][cur_sq_y] & WALL_LEFT )\r
- goto donewall;\r
- if ( maze[cur_sq_x - 1][cur_sq_y] & DOOR_IN_ANY ) {\r
- maze[cur_sq_x][cur_sq_y] |= WALL_LEFT;\r
- maze[cur_sq_x - 1][cur_sq_y] |= WALL_RIGHT;\r
- draw_wall(cur_sq_x, cur_sq_y, 3, hDC);\r
- goto donewall;\r
- }\r
- candidates[num_candidates++] = 3;\r
-\r
- donewall:\r
- if (num_candidates == 0)\r
- return ( -1 );\r
- if (num_candidates == 1)\r
- return ( candidates[0] );\r
- return ( candidates[ get_random(num_candidates) ] );\r
-\r
-}\r
-\r
-\r
-static long\r
-backup() /* back up a move */\r
-{\r
- sqnum--;\r
- if (0 <= sqnum) {\r
- cur_sq_x = move_list[sqnum].x;\r
- cur_sq_y = move_list[sqnum].y;\r
- }\r
- return ( sqnum );\r
-}\r
-\r
-int bw;\r
-\r
-static void\r
-draw_solid_square(i, j, dir, hDC, hBrush) /* draw a solid square in a square */\r
- register int i, j, dir;\r
- HDC hDC;\r
- HBRUSH hBrush;\r
-{\r
- RECT rc;\r
-\r
- switch (dir) {\r
- case 0:\r
- rc.left = border_x + bw + grid_width * i;\r
- rc.right = rc.left + grid_width - (bw + bw);\r
- rc.top = border_y - bw + grid_height * j;\r
- rc.bottom = rc.top + grid_height;\r
- break;\r
- case 1:\r
- rc.left = border_x + bw + grid_width * i;\r
- rc.right = rc.left + grid_width;\r
- rc.top = border_y + bw + grid_height * j;\r
- rc.bottom = rc.top + grid_height - (bw + bw);\r
- break;\r
- case 2:\r
- rc.left = border_x + bw + grid_width * i;\r
- rc.right = rc.left + grid_width - (bw + bw);\r
- rc.top = border_y + bw + grid_height * j;\r
- rc.bottom = rc.top + grid_height;\r
- break;\r
- case 3:\r
- rc.left = border_x - bw + grid_width * i;\r
- rc.right = rc.left + grid_width;\r
- rc.top = border_y + bw + grid_height * j;\r
- rc.bottom = rc.top + grid_height - (bw + bw);\r
- break;\r
- }\r
- (void) FillRect(hDC, &rc, hBrush);\r
-}\r
-\r
-static void\r
-draw_maze_border(HWND hWnd, HDC hDC) /* draw the maze outline */\r
-{\r
- register int i, j;\r
- HBRUSH hBrush;\r
-\r
- SelectObject(hDC, hPenWall);\r
-\r
- for ( i=0; i<maze_size_x; i++) {\r
- if ( maze[i][0] & WALL_TOP ) {\r
- MoveToEx(hDC, border_x + grid_width * i, border_y, NULL);\r
- (void) LineTo(hDC, border_x + grid_width * (i + 1) - 1, border_y);\r
- }\r
- if ((maze[i][maze_size_y - 1] & WALL_BOTTOM)) {\r
- MoveToEx(hDC, border_x + grid_width * i,\r
- border_y + grid_height * (maze_size_y) - 1, NULL);\r
- (void) LineTo(hDC, border_x + grid_width * (i+1) - 1,\r
- border_y + grid_height * (maze_size_y) - 1);\r
- }\r
- }\r
- for ( j=0; j<maze_size_y; j++) {\r
- if ( maze[maze_size_x - 1][j] & WALL_RIGHT ) {\r
- MoveToEx(hDC, border_x + grid_width * maze_size_x - 1,\r
- border_y + grid_height * j, NULL);\r
- (void) LineTo(hDC, border_x + grid_width * maze_size_x - 1,\r
- border_y + grid_height * (j+1) - 1);\r
- }\r
- if ( maze[0][j] & WALL_LEFT ) {\r
- MoveToEx(hDC, border_x, border_y + grid_height * j, NULL);\r
- (void) LineTo(hDC, border_x, border_y + grid_height * (j+1) - 1);\r
- }\r
- }\r
-\r
- hBrush = GetStockObject(WHITE_BRUSH); // FIXME: do not hardcode\r
- draw_solid_square (start_x, start_y, start_dir, hDC, hBrush);\r
- draw_solid_square (end_x, end_y, end_dir, hDC, hBrush);\r
-}\r
-\r
-\r
-static void\r
-draw_wall(i, j, dir, hDC) /* draw a single wall */\r
- int i, j, dir;\r
- HDC hDC;\r
-{\r
- SelectObject(hDC, hPenWall);\r
-\r
- switch (dir) {\r
- case 0:\r
- MoveToEx(hDC, border_x + grid_width * i, border_y + grid_height * j, NULL);\r
- (void) LineTo(hDC, border_x + grid_width * (i+1),\r
- border_y + grid_height * j);\r
- break;\r
- case 1:\r
- MoveToEx(hDC, border_x + grid_width * (i+1), border_y + grid_height * j,\r
- NULL);\r
- (void) LineTo(hDC, border_x + grid_width * (i+1),\r
- border_y + grid_height * (j+1));\r
- break;\r
- case 2:\r
- MoveToEx(hDC, border_x + grid_width * i, border_y + grid_height * (j+1),\r
- NULL);\r
- (void) LineTo(hDC, border_x + grid_width * (i+1),\r
- border_y + grid_height * (j+1));\r
- break;\r
- case 3:\r
- MoveToEx(hDC, border_x + grid_width * i, border_y + grid_height * j,\r
- NULL);\r
- (void) LineTo(hDC, border_x + grid_width * i,\r
- border_y + grid_height * (j+1));\r
- break;\r
- }\r
-}\r
-\r
-static void\r
-begin_solve_maze() /* solve it with graphical feedback */\r
-{\r
- static long grayPattern[] = {\r
- 0x55555555,\r
- 0xaaaaaaaa,\r
- 0x55555555,\r
- 0xaaaaaaaa,\r
- 0x55555555,\r
- 0xaaaaaaaa,\r
- 0x55555555,\r
- 0xaaaaaaaa\r
- };\r
- static RGBQUAD argbq[] = {\r
- { 0, 0, 255, 0 },\r
- { 255, 255, 255, 0 }\r
- };\r
- BITMAPINFO *pbmi;\r
-\r
- hDC = GetDC(hWnd);\r
- pbmi = malloc(sizeof(BITMAPINFOHEADER) + sizeof(argbq) + sizeof(grayPattern));\r
- pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);\r
- pbmi->bmiHeader.biWidth = 8;\r
- pbmi->bmiHeader.biHeight = 8;\r
- pbmi->bmiHeader.biPlanes = 1;\r
- pbmi->bmiHeader.biBitCount = 1;\r
- pbmi->bmiHeader.biCompression = BI_RGB;\r
- (void) memcpy(pbmi->bmiColors, argbq, sizeof(argbq));\r
- (void) memcpy(pbmi->bmiColors + 2, grayPattern, sizeof(grayPattern));\r
-#if 0\r
- /* FIXME Pattern brushes not yet implemented in ReactOS */\r
- hBrushDead = CreateDIBPatternBrushPt(pbmi, DIB_RGB_COLORS);\r
-#else\r
- hBrushDead = CreateSolidBrush(RGB(255, 0, 0));\r
-#endif\r
-// hBrushDead = CreateHatchBrush(HS_DIAGCROSS, RGB(255, 0, 0));\r
- free(pbmi);\r
- hBrushLiving = CreateSolidBrush(RGB(0, 255, 0));\r
-\r
- /* plug up the surrounding wall */\r
- maze[start_x][start_y] |= (WALL_TOP >> start_dir);\r
- maze[end_x][end_y] |= (WALL_TOP >> end_dir);\r
-\r
- /* initialize search path */\r
- pathi = 0;\r
- path[pathi].x = end_x;\r
- path[pathi].y = end_y;\r
- path[pathi].dir = -1;\r
-}\r
-\r
-static int\r
-solve_maze() /* solve it with graphical feedback */\r
-{\r
- int ret;\r
- int action_done;\r
-\r
- do {\r
- action_done = 1;\r
- if ( ++path[pathi].dir >= 4 ) {\r
- pathi--;\r
- draw_solid_square( (int)(path[pathi].x), (int)(path[pathi].y),\r
- (int)(path[pathi].dir), hDC, hBrushDead);\r
- ret = 0;\r
- }\r
- else if ( ! (maze[path[pathi].x][path[pathi].y] &\r
- (WALL_TOP >> path[pathi].dir)) &&\r
- ( (pathi == 0) || ( (path[pathi].dir !=\r
- (int)(path[pathi-1].dir+2)%4) ) ) ) {\r
- enter_square(pathi, hDC, hBrushLiving);\r
- pathi++;\r
- if ( maze[path[pathi].x][path[pathi].y] & START_SQUARE ) {\r
- DeleteObject(hBrushLiving);\r
- DeleteObject(hBrushDead);\r
- ReleaseDC(hWnd, hDC);\r
- ret = 1;\r
- } else {\r
- ret = 0;\r
- }\r
- } else {\r
- action_done = 0;\r
- }\r
- } while (! action_done);\r
-\r
- return ret;\r
-}\r
-\r
-\r
-static void\r
-enter_square(int n, HDC hDC, HBRUSH hBrush) /* move into a neighboring square */\r
-{\r
- draw_solid_square( (int)path[n].x, (int)path[n].y,\r
- (int)path[n].dir, hDC, hBrush);\r
-\r
- path[n+1].dir = -1;\r
- switch (path[n].dir) {\r
- case 0: path[n+1].x = path[n].x;\r
- path[n+1].y = path[n].y - 1;\r
- break;\r
- case 1: path[n+1].x = path[n].x + 1;\r
- path[n+1].y = path[n].y;\r
- break;\r
- case 2: path[n+1].x = path[n].x;\r
- path[n+1].y = path[n].y + 1;\r
- break;\r
- case 3: path[n+1].x = path[n].x - 1;\r
- path[n+1].y = path[n].y;\r
- break;\r
- }\r
-}\r
-\r
-static void\r
-start_timer(HWND hWnd, int iTimeout)\r
-{\r
- waiting = TRUE;\r
- SetTimer(hWnd, 1, iTimeout, NULL);\r
-}\r
-\r
-/****************************************************************************\r
-\r
- FUNCTION: WinMain(HINSTANCE, HINSTANCE, LPSTR, int)\r
-\r
- PURPOSE: calls initialization function, processes message loop\r
-\r
- COMMENTS:\r
-\r
- Windows recognizes this function by name as the initial entry point\r
- for the program. This function calls the application initialization\r
- routine, if no other instance of the program is running, and always\r
- calls the instance initialization routine. It then executes a message\r
- retrieval and dispatch loop that is the top-level control structure\r
- for the remainder of execution. The loop is terminated when a WM_QUIT\r
- message is received, at which time this function exits the application\r
- instance by returning the value passed by PostQuitMessage().\r
-\r
- If this function must abort before entering the message loop, it\r
- returns the conventional value NULL.\r
-\r
-****************************************************************************/\r
-\r
-int APIENTRY MazeMain(\r
- HINSTANCE hInstance,\r
- HINSTANCE hPrevInstance,\r
- LPSTR lpCmdLine,\r
- HWND hParent)\r
-{\r
- MSG msg;\r
- HDC hDC;\r
-\r
- /* Perform initializations that apply to a specific instance */\r
-\r
- if (!InitInstance(hInstance, hParent)) {\r
- return (FALSE);\r
- }\r
-\r
- waiting = FALSE;\r
- state = 1;\r
-\r
- /* Acquire and dispatch messages until a WM_QUIT message is received. */\r
-\r
- while (0 != state) {\r
- if (waiting) {\r
- (void) WaitMessage();\r
- }\r
- while (0 != state && PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {\r
- if (WM_QUIT == msg.message) {\r
- state = 0;\r
- } else {\r
- DispatchMessage(&msg); /* Dispatches message to window */\r
- }\r
- }\r
- switch (state) {\r
- case 1:\r
- initialize_maze();\r
- state = 2;\r
- break;\r
- case 2:\r
- hDC = GetDC(hWnd);\r
- SendMessage(hWnd, WM_ERASEBKGND, (WPARAM) hDC, (LPARAM) 0);\r
- draw_maze_border(hWnd, hDC);\r
- ReleaseDC(hWnd, hDC);\r
- state = 3;\r
- break;\r
- case 3:\r
- create_maze();\r
- state = 4;\r
- break;\r
- case 4:\r
- start_timer(hWnd, pre_solve_delay);\r
- state = 5;\r
- break;\r
- case 5:\r
- if (! waiting) {\r
- state = 6;\r
- }\r
- break;\r
- case 6:\r
- begin_solve_maze();\r
- if (0 != solve_delay) {\r
- start_timer(hWnd, solve_delay);\r
- state = 7;\r
- } else {\r
- state = 8;\r
- }\r
- break;\r
- case 7:\r
- if (! waiting) {\r
- state = 8;\r
- }\r
- break;\r
- case 8:\r
- if (! solve_maze()) {\r
- if (0 != solve_delay) {\r
- start_timer(hWnd, solve_delay);\r
- state = 7;\r
- }\r
- } else {\r
- state = 9;\r
- }\r
- break;\r
- case 9:\r
- start_timer(hWnd, post_solve_delay);\r
- state = 10;\r
- break;\r
- case 10:\r
- if (! waiting) {\r
- state = 11;\r
- }\r
- break;\r
- case 11:\r
- state = 1;\r
- break;\r
- }\r
- }\r
-\r
- return (msg.wParam); /* Returns the value from PostQuitMessage */\r
-}\r
-\r
-\r
-/****************************************************************************\r
-\r
- FUNCTION: InitInstance(HINSTANCE, int)\r
-\r
- PURPOSE: Saves instance handle and creates main window\r
-\r
- COMMENTS:\r
-\r
- This function is called at initialization time for every instance of\r
- this application. This function performs initialization tasks that\r
- cannot be shared by multiple instances.\r
-\r
- In this case, we save the instance handle in a static variable and\r
- create and display the main program window.\r
-\r
-****************************************************************************/\r
-\r
-static BOOL InitInstance(\r
- HINSTANCE hInstance,\r
- HWND hParent)\r
-{\r
- RECT rect;\r
-\r
- /* Save the instance handle in static variable, which will be used in\r
- many subsequence calls from this application to Windows. */\r
-\r
- hInst = hInstance; /* Store instance handle in our global variable */\r
-\r
- GetClientRect(hParent, &rect);\r
-#if 0\r
- /* Create a main window for this application instance. */\r
- hWnd = CreateWindow(\r
- szAppName, /* See RegisterClass() call. */\r
- szTitle, /* Text for window title bar. */\r
- WS_CHILD,/* Window style. */\r
- 0, 0, rect.right/2, rect.bottom/2, /* Use default positioning */\r
- hParent, /* We use a Parent. */\r
- NULL, /* Use the window class menu. */\r
- hInstance, /* This instance owns this window. */\r
- NULL /* We don't use any data in our WM_CREATE */\r
- );\r
-#endif\r
-hWnd = hParent;\r
- // If window could not be created, return "failure"\r
- if (!hWnd) {\r
- return (FALSE);\r
- }\r
-\r
- // Make the window visible; update its client area; and return "success"\r
- ShowWindow(hWnd, SW_SHOW); // Show the window\r
- UpdateWindow(hWnd); // Sends WM_PAINT message\r
-\r
- hPenWall = CreatePen(PS_SOLID, 3, RGB(150,150,150));\r
-\r
- return (TRUE); // We succeeded...\r
-\r
-}\r
-\r
-static BOOL\r
-OnCreate(HWND hWnd, LPCREATESTRUCT lpCreateStruct)\r
-{\r
- RECT rc;\r
- int size;\r
-\r
- srand((unsigned) time(NULL));\r
-\r
-#if 0\r
- /* FIXME GetPrivateProfileInt not yet implemented in ReactOS */\r
- size = GetPrivateProfileInt("maze", "gridsize", 0, "maze.ini");\r
- pre_solve_delay = GetPrivateProfileInt("maze", "predelay", 5000,\r
- "maze.ini");\r
- post_solve_delay = GetPrivateProfileInt("maze", "postdelay", 5000,\r
- "maze.ini");\r
- solve_delay = GetPrivateProfileInt("maze", "solvedelay", 10,\r
- "maze.ini");\r
-#else\r
- size = 10;\r
- pre_solve_delay = 5000;\r
- post_solve_delay = 5000;\r
- solve_delay = 1;\r
-#endif\r
-\r
- if (size < 2) {\r
- size = 7 + (rand() % 30);\r
- }\r
- grid_width = grid_height = size;\r
- bw = (size > 6 ? 3 : (size-1)/2);\r
-\r
- GetClientRect(hWnd, &rc);\r
- set_maze_sizes(rc.right - rc.left, rc.bottom - rc.top);\r
-\r
- return TRUE;\r
-}\r
-\r
-void OnTimer(HWND hwnd, UINT id)\r
-{\r
- waiting = FALSE;\r
-}\r
-\r
-/****************************************************************************\r
-\r
- FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)\r
-\r
- PURPOSE: Processes messages\r
-\r
- MESSAGES:\r
-\r
- WM_DESTROY - destroy window\r
-\r
- COMMENTS:\r
-\r
-****************************************************************************/\r
-\r
-LRESULT CALLBACK MazeWndProc(\r
- HWND hWnd, // window handle\r
- UINT message, // type of message\r
- WPARAM wParam, // additional information\r
- LPARAM lParam) // additional information\r
-{\r
- PAINTSTRUCT ps;\r
-\r
- switch (message) {\r
- case WM_CREATE:\r
- OnCreate(hWnd, (LPCREATESTRUCT) lParam);\r
- break;\r
- case WM_PAINT:\r
- BeginPaint(hWnd, &ps);\r
- state = 1;\r
- EndPaint(hWnd, &ps);\r
- case WM_TIMER:\r
- OnTimer(hWnd, wParam);\r
- break;\r
- case WM_DESTROY: // message: window being destroyed\r
- PostQuitMessage(0);\r
- break;\r
-\r
- default: // Passes it on if unproccessed\r
- return (DefWindowProc(hWnd, message, wParam, lParam));\r
- }\r
- return (0);\r
-}\r