/********************************************************************\ * generic.c: Source code for generic * * * * Comments: Generic Win32-based Application * * * * Functions: * * WinMain - Application entry point * * MainWndProc - main window procedure * * AboutDlgProc - dialog procedure for About dialog * * * * * \********************************************************************/ /********************* Header Files *********************/ #include #include #include #include #include "prj2.h" /********************* Prototypes ***********************/ LRESULT WINAPI MainWndProc( HWND, UINT, WPARAM, LPARAM ); /******************* Global Variables ********************/ HANDLE ghInstance; /********************************************************************\ * Function: int PASCAL WinMain(HINSTANCE, HINSTANCE, LPSTR, int) * * * * Purpose: Initializes Application * * * * Comments: Register window class, create and display the main * * window, and enter message loop. * * * * * \********************************************************************/ int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow ) { WNDCLASS wc; MSG msg; HWND hWnd; srand( (unsigned)time( NULL ) ); if( !hPrevInstance ) { wc.lpszClassName = "Battleship"; wc.lpfnWndProc = MainWndProc; wc.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW; wc.hInstance = hInstance; wc.hIcon = LoadIcon( NULL, IDI_APPLICATION ); wc.hCursor = LoadCursor( NULL, IDC_ARROW ); wc.hbrBackground = (HBRUSH)( GetStockObject(BLACK_BRUSH) ); wc.lpszMenuName = NULL; wc.cbClsExtra = 0; wc.cbWndExtra = 0; RegisterClass( &wc ); } ghInstance = hInstance; hWnd = CreateWindow( "Battleship", "ITSK2323 Battleship!", WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX, 0, 0, 613, 500, NULL, NULL, hInstance, NULL ); ShowWindow( hWnd, nCmdShow ); while( GetMessage( &msg, NULL, 0, 0 ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } return msg.wParam; } /********************************************************************\ * Function: LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM) * * * * Purpose: Processes Application Messages * * * * Comments: The following messages are processed * * * * WM_PAINT * * WM_COMMAND * * WM_DESTROY * * * * * \********************************************************************/ LRESULT CALLBACK MainWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ) { PAINTSTRUCT ps; HDC hdc; TEXTMETRIC tm; char str[255]; int i, j, X = 0, Y = 0; static int maxX, maxY; static HDC memdc, bitdc; static HBITMAP hbit, hbmp; HBRUSH hBrush; HPEN hPen, hPenOld; fstream fp; switch( msg ) { /**************************************************************\ * WM_CREATE: * \**************************************************************/ case WM_CREATE: fp.open("test.txt", ios::out); for(i=0; i < 8; i++) for(j=0; j < 8; j++) fp << PlayerBoard[i][j] << " "; fp << endl; for(i=0; i < 8; i++) for(j=0; j < 8; j++) fp << OpponentBoard[i][j] << " "; fp.close(); fp.open("test.txt", ios::in); for(i=0; i < 8; i++) for(j=0; j < 8; j++) fp >> PlayerBoard[i][j]; for(i=0; i < 8; i++) for(j=0; j < 8; j++) fp >> OpponentBoard[i][j]; fp.close(); maxX = GetSystemMetrics(SM_CXSCREEN); maxY = GetSystemMetrics(SM_CYSCREEN); hdc = GetDC(hWnd); memdc = CreateCompatibleDC(hdc); hbit = CreateCompatibleBitmap(hdc, maxX, maxY); SelectObject(memdc, hbit); hBrush = (HBRUSH) GetStockObject(BLACK_BRUSH); hPen = CreatePen(PS_SOLID, 3, RGB(127, 127, 255)); SelectObject(memdc, hBrush); hPenOld = (HPEN) SelectObject(memdc, hPen); PatBlt(memdc, 0, 0, maxX, maxY, PATCOPY); GetTextMetrics(hdc, &tm); strcpy(str, "Your ships"); SetTextColor(memdc, RGB(255,255,255)); SetBkColor(memdc, RGB(0,0,0)); TextOut(memdc, 115, 5, str, strlen(str)); Y = tm.tmHeight + tm.tmExternalLeading+10; MoveToEx(memdc, 115, Y,(LPPOINT)NULL); LineTo(memdc, 185, Y); strcpy(str, "Opponent ships"); SetTextColor(memdc, RGB(255,255,255)); SetBkColor(memdc, RGB(0,0,0)); TextOut(memdc, 400, 5, str, strlen(str)); MoveToEx(memdc, 400, Y,(LPPOINT)NULL); LineTo(memdc, 500, Y); for(i=0; i < 9; i++) { MoveToEx(memdc, 34*i+18, Y+20,(LPPOINT)NULL); LineTo(memdc, 34*i+18, Y+292); } for(i=0; i < 9; i++) { MoveToEx(memdc, 18, 34*i+Y+20,(LPPOINT)NULL); LineTo(memdc, 289, 34*i+Y+20); } for(i=0; i < 9; i++) { MoveToEx(memdc, 300+34*i+18, Y+20,(LPPOINT)NULL); LineTo(memdc, 300+34*i+18, Y+292); } for(i=0; i < 9; i++) { MoveToEx(memdc, 300+18, 34*i+Y+20,(LPPOINT)NULL); LineTo(memdc, 300+289, 34*i+Y+20); } hbmp = (HBITMAP) LoadImage((HINSTANCE)ghInstance, "test.bmp", IMAGE_BITMAP, 128, 32, LR_LOADFROMFILE); bitdc = CreateCompatibleDC(memdc); SelectObject(bitdc, hbmp); for(i=0; i < 8; i++) for(j=0; j < 8; j++) { BitBlt(memdc, 34*i+19, 34*j+Y+21, 32, 32, bitdc, (PlayerBoard[i][j])*32, 0, SRCCOPY); BitBlt(memdc, 300+34*i+19, 34*j+Y+21, 32, 32, bitdc, (OpponentBoard[i][j])*32, 0, SRCCOPY); } ReleaseDC(hWnd, hdc); SelectObject(memdc, hPenOld); DeleteObject(hPen); InvalidateRect(hWnd, NULL, 0); break; /**************************************************************\ * WM_PAINT: * \**************************************************************/ case WM_PAINT: hdc = BeginPaint( hWnd, &ps ); BitBlt(hdc, ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right - ps.rcPaint.left, // width ps.rcPaint.bottom - ps.rcPaint.top, // height memdc, ps.rcPaint.left, ps.rcPaint.top, SRCCOPY); EndPaint( hWnd, &ps ); break; /**************************************************************\ * WM_DESTROY: PostQuitMessage() is called * \**************************************************************/ case WM_DESTROY: DeleteDC(memdc); DeleteDC(bitdc); DeleteObject(hbit); DeleteObject(hbmp); PostQuitMessage(0); break; /**************************************************************\ * Let the default window proc handle all other messages * \**************************************************************/ default: return( DefWindowProc( hWnd, msg, wParam, lParam )); } return 0; }