// DRobot.cpp : definisce il punto di ingresso dell'applicazione. // #define _CRT_SECURE_NO_WARNINGS #include using namespace std; #include "framework.h" #include "DRobot.h" #include "clsMaze.h" #include "Robot.h" #include //era 100 #define MAX_LOADSTRING MAX_PATH // Variabili globali: HINSTANCE hInst; // istanza corrente TCHAR szTitle[MAX_LOADSTRING]; // Testo della barra del titolo TCHAR szWindowClass[MAX_LOADSTRING]; // nome della classe di finestre principale char *sFile = NULL; int orgRow=0; int orgCol=0; int CELL_WIDTH=50; int CELL_HEIGHT=50; //extern POINT ptcRobot; //extern int dirRobot; //extern int delay; //extern int iList; //extern int List[]; #define BUFF_SIZE (MAX_PATH * 4) char buff[BUFF_SIZE]; clsMaze *objMaze=NULL; //num pareti x cella //#define NW 4 // Dichiarazioni con prototipo delle funzioni incluse in questo modulo di codice: ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM); LRESULT CALLBACK Params(HWND , UINT , WPARAM , LPARAM ); char *DlgOpenFile(HWND hWnd); void Play(HWND hWnd); void SetupMapMode(HWND hWnd,HDC hdc) { if (NULL == objMaze) return; //int worldWidth=(objMaze->Cols/ objMaze->NW) * objMaze->CELL_WIDTH ,worldHeight=(objMaze->Rows/ objMaze->NW) * objMaze->CELL_HEIGHT; int worldWidth = (objMaze->Cols) * objMaze->CELL_WIDTH; int worldHeight = (objMaze->Rows) * objMaze->CELL_HEIGHT; RECT cliRect; //HWND hWnd = WindowFromDC(hdc); GetClientRect(hWnd,&cliRect); int wClient = cliRect.right - cliRect.left; int hClient = -cliRect.top + cliRect.bottom; float yMin=0., yMax=(float)worldHeight; float xMin=0., xMax=(float)worldWidth; SetMapMode(hdc,MM_ISOTROPIC); SetWindowOrgEx(hdc,-abs(objMaze->offsetWidth),-abs(objMaze->offsetHeight),0); SetWindowExtEx(hdc,(int)xMax,(int)yMax, NULL); SetViewportOrgEx(hdc, 0,0,NULL); SetViewportExtEx(hdc, wClient, hClient, NULL); } LONG WINAPI UnhandledException(LPEXCEPTION_POINTERS exceptionInfo) { MessageBoxA(0, "An exception occured which wasnt handled!", "Error!", MB_OK); return EXCEPTION_EXECUTE_HANDLER; } int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { // TODO: inserire qui il codice. SetUnhandledExceptionFilter(UnhandledException); MSG msg; HACCEL hAccelTable; // Inizializzare le stringhe globali LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_DROBOT, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // Eseguire l'inizializzazione dall'applicazione: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_DROBOT); // Ciclo di messaggi principale: while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return (int) msg.wParam; } // // FUNZIONE: MyRegisterClass() // // SCOPO: registra la classe di finestre. // // COMMENTI: // // Questa funzione e il relativo utilizzo sono necessari solo se si desidera che il codice // sia compatibile con i sistemi Win32 precedenti alla funzione 'RegisterClassEx' // aggiunta a Windows 95. È importante chiamare questa funzione // in modo da ottenere piccole icone in formato corretto associate // all'applicazione. // ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = (WNDPROC)WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_DROBOT); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = (LPCTSTR)IDC_DROBOT; wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL); return RegisterClassEx(&wcex); } // // FUNZIONE: InitInstance(HANDLE, int) // // SCOPO: salva l'handle di istanza e crea la finestra principale // // COMMENTI: // // In questa funzione l'handle di istanza viene salvato in una variabile globale e // la finestra di programma principale viene creata e visualizzata. // BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { HWND hWnd; hInst = hInstance; // Memorizzare l'handle di istanza nella variabile globale hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); if (!hWnd) { return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } // // FUNZIONE: WndProc(HWND, unsigned, WORD, LONG) // // SCOPO: elabora i messaggi per la finestra principale. // // WM_COMMAND - elabora il menu dell'applicazione // WM_PAINT - Disegna la finestra principale // WM_DESTROY - inserisce un messaggio di uscita e restituisce un risultato // // LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; PAINTSTRUCT ps; HDC hdc; switch (message) { case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // Analizzare le selezioni di menu: switch (wmId) { case IDM_ABOUT: DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About); break; case IDM_STRUMENTI_OPZIONI: DialogBox(hInst, (LPCTSTR)IDD_PARAMS, hWnd, (DLGPROC)Params); break; case IDM_STRUMENTI_PLAY: Play(hWnd); break; case IDM_FILE_OPEN: sFile=DlgOpenFile(hWnd); if (NULL != sFile) { delete objMaze; objMaze = NULL; if (sFile) { WideCharToMultiByte(CP_ACP, 0, szTitle, lstrlen(szTitle), buff, BUFF_SIZE, NULL, NULL); string sTitle; sTitle.append(buff); sTitle.append(" - "); sTitle.append(sFile); SetWindowTextA(hWnd,sTitle.c_str()); objMaze = new clsMaze(CELL_WIDTH,CELL_HEIGHT,orgCol,orgRow); objMaze->Load(sFile); Robot_Init(objMaze->CELL_WIDTH,objMaze->CELL_HEIGHT,N) ;//,orgCol,orgRow } InvalidateRect(hWnd,NULL,true); } break; case IDM_EXIT: DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } break; case WM_PAINT: { InvalidateRect(hWnd,NULL,true);//Cancella lo sfondo hdc = BeginPaint(hWnd, &ps); //SetupMapMode(hdc); //POINT PT; //PT.x=0; //PT.y=0; //DPtoLP(hdc,&PT,1); //POINT PT2; //PT2.x=0; //PT2.y=0; //LPtoDP(hdc,&PT2,1); if(objMaze) { RECT cr; HBITMAP hBmp; GetClientRect(hWnd, &cr); int width = cr.right - cr.left; int height = cr.bottom - cr.top; HDC hdcMem = CreateCompatibleDC(hdc); //SetMapMode(hdcMem, GetMapMode(hdc)); SetupMapMode(hWnd,hdcMem); hBmp = CreateCompatibleBitmap(hdc, width, height); SelectObject(hdcMem, hBmp); SetBkColor(hdcMem, RGB(255, 0, 255)); Rectangle(hdcMem, -1, -1, width, height); //////////new2 int iStep = 0; //iStep = Robot_SolveDFS(hdcMem,ptcRobot_x,ptcRobot_y,dirRobot); objMaze->Draw(hdcMem); Robot_Draw(hdcMem); //Ellipse(hdcMem, 0, 0, 100, 100); BitBlt(hdc, 0, 0, width, height, hdcMem, 0, 0, SRCCOPY); DeleteObject(hBmp); DeleteDC(hdcMem); } EndPaint(hWnd, &ps); } break; case WM_KEYDOWN: switch (wParam) { case VK_SPACE: { Play(hWnd); break; } } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } // Gestore dei messaggi della finestra Informazioni su. LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_INITDIALOG: return TRUE; case WM_COMMAND: if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { EndDialog(hDlg, LOWORD(wParam)); return TRUE; } break; } return FALSE; } LRESULT CALLBACK Params(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { HWND cboDirs; const char *arDirs[] = { "N", "E", "S", "W"}; switch (message) { case WM_INITDIALOG: { cboDirs = GetDlgItem(hDlg, IDC_DIRS); for(int i = 0; i < 4; i++) { SendMessage(cboDirs, CB_ADDSTRING, 0, reinterpret_cast((LPCTSTR)arDirs[i])); } for (int i=0; i < 4; i++) if(i==dirRobot) { SendDlgItemMessage(hDlg,IDC_DIRS,CB_SETCURSEL,i,0); break; } SetDlgItemTextA(hDlg,IDC_CELL_WIDTH,_itoa(CELL_WIDTH,buff,10)); SetDlgItemTextA(hDlg,IDC_CELL_HEIGHT,_itoa(CELL_HEIGHT,buff,10)); SetDlgItemTextA(hDlg,IDC_DELAY,_itoa(delay,buff,10)); SetDlgItemTextA(hDlg,IDC_ROBOT_ROW,_itoa(orgRow,buff,10)); SetDlgItemTextA(hDlg,IDC_ROBOT_COL,_itoa(orgCol,buff,10)); return TRUE; } case WM_COMMAND: if (LOWORD(wParam) == IDOK) { try{ int i = (int)SendDlgItemMessage(hDlg,IDC_DIRS,CB_GETCURSEL,0,0); dirRobot = i; GetDlgItemTextA(hDlg,IDC_CELL_WIDTH,buff,sizeof(buff)); CELL_WIDTH = atoi(buff); GetDlgItemTextA(hDlg,IDC_CELL_HEIGHT,buff,sizeof(buff)); CELL_HEIGHT = atoi(buff); GetDlgItemTextA(hDlg,IDC_DELAY,buff,sizeof(buff)); delay = atoi(buff); GetDlgItemTextA(hDlg,IDC_ROBOT_ROW,buff,sizeof(buff)); int orgR = atoi(buff); GetDlgItemTextA(hDlg,IDC_ROBOT_COL,buff,sizeof(buff)); int orgC = atoi(buff); if(objMaze && ((orgC > objMaze->Cols) || (orgR > objMaze->Rows))) throw exception("Out of Bounds"); orgRow = orgR; orgCol = orgC; Robot_Init(CELL_WIDTH,CELL_HEIGHT,dirRobot);//orgCol,orgRow, if(objMaze) { objMaze->offsetWidth=-orgCol*CELL_WIDTH; objMaze->offsetHeight=-orgRow*CELL_HEIGHT; } EndDialog(hDlg, LOWORD(wParam)); InvalidateRect(GetParent(hDlg),NULL,true); } catch (exception &ex) { MessageBoxA(NULL,ex.what(),"Exception",MB_OK); } return TRUE; } if (LOWORD(wParam) == IDCANCEL) { EndDialog(hDlg, LOWORD(wParam)); return TRUE; } break; } return FALSE; } char *DlgOpenFile(HWND hWnd) { OPENFILENAMEA ofn; char szFile[MAX_PATH]; ZeroMemory(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = hWnd; ofn.lpstrFile = szFile; ofn.lpstrFile[0] = '\0'; ofn.nMaxFile = sizeof(szFile); ofn.lpstrFilter = "All\0*.*\0Text\0*.TXT\0"; ofn.nFilterIndex = 1; ofn.lpstrFileTitle = NULL; ofn.nMaxFileTitle = 0; ofn.lpstrInitialDir = NULL; ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; if (GetOpenFileNameA(&ofn)==TRUE) { char * sTmp; sTmp = (char *)malloc((strlen(ofn.lpstrFile)+1)*sizeof(char)); strcpy(sTmp, ofn.lpstrFile); return sTmp; } return NULL; } void Play(HWND hWnd) { if(NULL == objMaze) return; //InvalidateRect(hWnd,NULL,true);//Cancella lo sfondo HDC hdc = GetDC(hWnd); //SetupMapMode(hWnd,hdc); Robot_Init(CELL_WIDTH,CELL_HEIGHT,dirRobot,delay);//orgRow,orgCol, ////////////new1 RECT cr; HBITMAP hBmp; GetClientRect(hWnd, &cr); int width = cr.right - cr.left; int height = cr.bottom - cr.top; HDC hdcMem = CreateCompatibleDC(hdc); //SetMapMode(hdcMem, GetMapMode(hdc)); SetupMapMode(hWnd, hdcMem); hBmp = CreateCompatibleBitmap(hdc, width, height); SelectObject(hdcMem, hBmp); SetBkColor(hdcMem, RGB(255, 0, 255)); Rectangle(hdcMem, -1, -1, width, height); objMaze->Draw(hdcMem); Robot_Draw(hdcMem); //////////new2 int iStep = 0; iStep = Robot_SolveDFS(hdc,hdcMem,ptcRobot_x,ptcRobot_y,dirRobot); //Ellipse(hdcMem, 0, 0, 100, 100); BitBlt(hdc, 0, 0, width, height, hdcMem, 0, 0, SRCCOPY); DeleteObject(hBmp); DeleteDC(hdcMem); //////////new2 //int iStep = 0; //iStep = Robot_SolveDFS(hdcMem,ptcRobot_x,ptcRobot_y,dirRobot); /////BitBlt(hdc, 50, 50, width, height, hdcMem, 0, 0, SRCCOPY); //DeleteObject(hBmp); //DeleteDC(hdcMem); /////////////////_itoa(iStep,buff,10); sprintf(buff, "%d", iStep); HBRUSH NewBrush = CreateSolidBrush(RGB(125, 125, 25)); HBRUSH hBrushOri = (HBRUSH)SelectObject(hdc, NewBrush); for(int i=0; i < iList;i++) { int x; int y; List_toInt(i,x,y); Rectangle(hdc,x-CELL_WIDTH/8,y-CELL_HEIGHT/8,x+CELL_WIDTH/8,y+CELL_HEIGHT/8); } DeleteObject(NewBrush); SelectObject(hdc,hBrushOri); ReleaseDC(hWnd,hdc); MessageBoxA(hWnd,buff,"Steps",MB_OK); }