#include //#include #include #include #include "string" #include #include #include #include "Leg.h" #include using namespace std; #define DRAW WM_USER + 1 #pragma region ConverToInt #ifdef UNICODE typedef std::wostringstream tstringstream; #else typedef std::ostringstream tstringstream; #endif LPCTSTR convertInt(const int number) { char str[512]; sprintf(str,"%d",number); return str; } #pragma endregion ConverToInt #pragma region Global HWND ghMainWnd = 0; HINSTANCE ghAppInst = 0; bool gMouseDown = 0; HDC backDC; HDC BackDC2; HBITMAP backBufferBMP; HBITMAP backBufferBMP2; int backBufferCX; int backBufferCY; Leg *La; D3DXVECTOR3 Coordinates( 10, 15, 0); D3DXVECTOR3 SuccessfulCoordinate; D3DXVECTOR3 Offset( 250, 400, 0); D3DXVECTOR3 Size( 15, 15, 15 ); #pragma endregion Global #pragma region OtherFunctions clock_t Fpres; bool DD ( ) { clock_t t = clock(); if ((t - Fpres) <= 200) return 1; else { Fpres = t; return 0; } } inline void draw() { HDC winhdc = GetDC( ghMainWnd ) ; // Now copy the back buffer to the front buffer. BitBlt( winhdc, // destination buffer 0, 0, // x, y to start writing to in the destination buffer // counted from top left corner of dest HDC (window) // Width, height to copy for backBufferCX, backBufferCY, backDC, // SOURCE buffer. We're taking from that back canvas 0, 0, // x pt, y pt to START copying from // SOURCE. counted from top left again // And we just want a straight copy. SRCCOPY ); ReleaseDC( ghMainWnd, winhdc ) ; } inline void copybtf( HDC back, HDC front ) { // copy the back buffer to the front buffer. BitBlt( front, // destination buffer 0, 0, // x, y to start writing to in the destination buffer // counted from top left corner of dest HDC (window) // Width, height to copy for backBufferCX, backBufferCY, back, // SOURCE buffer. We're taking from that back canvas 0, 0, // x pt, y pt to START copying from // SOURCE. counted from top left again // And we just want a straight copy. SRCCOPY ); } void InitBackBuffer() { backBufferCX = 700; backBufferCY = 850; HDC winhdc = GetDC( ghMainWnd ); /* Imagine you have 2 things: - a canvas on which to draw (call this your HBITMAP) - set of brushes you will use to draw (call this your HDC) 1. HBITMAP. Canvas ___________________ | * | | * | | HBITMAP | | draw here | |_________________| 2. HDC. Set of brushes. <-* []-* ()-* */ // Create the HBITMAP "canvas", or surface on which we will draw. backBufferBMP = CreateCompatibleBitmap( winhdc, backBufferCX, backBufferCY ); if(backBufferBMP == NULL) printf( "failed to create backBufferBMP" ); // Create the HDC "device context", or collection of tools // and brushes that we can use to draw on our canvas backDC = CreateCompatibleDC( winhdc ); if(backDC == NULL) printf( "failed to create the backDC" ); // Permanently associate the surface on which to draw // ("canvas") (HBITMAP), with the "set of brushes" (HDC) // "select in" the backBufferBMP into the backDC HBITMAP oldbmp = (HBITMAP)SelectObject( backDC, backBufferBMP ); // Delete the "canvas" that the backDC was // going to draw to. We don't care about it. DeleteObject( oldbmp ); /// Release the dc of the main window itself ReleaseDC( ghMainWnd, winhdc ); } void InitBackBuffer2() { backBufferCX = 700; backBufferCY = 850; HDC winhdc = GetDC( ghMainWnd ); // Create the HBITMAP "canvas", or surface on which we will draw. backBufferBMP2 = CreateCompatibleBitmap( winhdc, backBufferCX, backBufferCY ); if(backBufferBMP2 == NULL) printf( "failed to create backBufferBMP" ); // Create the HDC "device context", or collection of tools // and brushes that we can use to draw on our canvas BackDC2 = CreateCompatibleDC( winhdc ); if(BackDC2 == NULL) printf( "failed to create the backDC" ); // Permanently associate the surface on which to draw // ("canvas") (HBITMAP), with the "set of brushes" (HDC) // "select in" the backBufferBMP into the backDC HBITMAP oldbmp = (HBITMAP)SelectObject( BackDC2, backBufferBMP2 ); // Delete the "canvas" that the backDC was // going to draw to. We don't care about it. DeleteObject( oldbmp ); /// Release the dc of the main window itself ReleaseDC( ghMainWnd, winhdc ); } inline void ClearBackDC() { // clear by filling out the back buffer with black rectangle HBRUSH oldBrush = (HBRUSH)SelectObject( backDC, GetStockObject( BLACK_BRUSH ) ) ; Rectangle( backDC, 0, 0, backBufferCX, backBufferCY ); SelectObject( backDC, oldBrush ) ; // put the old brush back, not bothering // to catch the system BLACK_BRUSH } inline void ClearBackDC2() { // clear by filling out the back buffer with black rectangle HBRUSH oldBrush = (HBRUSH)SelectObject( BackDC2, GetStockObject( BLACK_BRUSH ) ) ; Rectangle( BackDC2, 0, 0, backBufferCX, backBufferCY ); SelectObject( BackDC2, oldBrush ) ; // put the old brush back, not bothering // to catch the system BLACK_BRUSH } #pragma endregion OtherFunctions LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { HDC hdc = 0; PAINTSTRUCT ps; int mx = LOWORD(lParam); int my = HIWORD(lParam); D3DXVECTOR3 test; switch( msg ) { #pragma region CREATE case WM_CREATE: { La = new Leg; La->Offset( Offset ); La->Size( Size ); InitBackBuffer(); La->GetHDC( backDC ); InitBackBuffer2(); La->GetHDCSecondBuffer( BackDC2 ); La->GetPossibleMoveSpace(); La->PrintMoveSpaceOnWorldSpace(); PostMessage( hWnd, DRAW, wParam, lParam ); break; } #pragma endregion CREATE case DRAW: { #pragma region DRAW ClearBackDC(); copybtf( BackDC2, backDC ); La->Coordinates( Coordinates ); La->CalculateLegRight(); if( La->Error == true ) { La->Coordinates( SuccessfulCoordinate ); La->CalculateLegRight(); La->CalculateWorldSpaceLineModel(); La->Print3DLinesOnWorldSpace(); La->PrintWorldSpaceLineModel(); } else { La->CalculateWorldSpaceLineModel(); La->Print3DLinesOnWorldSpace(); La->PrintWorldSpaceLineModel(); SuccessfulCoordinate = La->Coordinate; } #pragma region Status RECT rcWind; POINT ptDiff; GetWindowRect(hWnd, &rcWind); ptDiff.x = (rcWind.right - rcWind.left); ptDiff.y = (rcWind.bottom - rcWind.top); //TextOut( backDC, ptDiff.x - 44, 0, convertInt( D->O1 ), ( sizeof( convertInt( D->O1 ) ) / sizeof( char ) - 1) ); //TextOut( backDC, ptDiff.x - 44, 20, convertInt( D->O2 ), ( sizeof( convertInt( D->O2 ) ) / sizeof( char ) - 1) ); //TextOut( backDC, ptDiff.x - 44, 40, convertInt( D->O3 ), ( sizeof( convertInt( D->O3 ) ) / sizeof( char ) - 1) ); TextOut( backDC, ptDiff.x - 44, 70, convertInt( Coordinates.x ), ( sizeof( convertInt( Coordinates.x ) ) / sizeof( char ) - 1) ); TextOut( backDC, ptDiff.x - 44, 90, convertInt( Coordinates.y ), ( sizeof( convertInt( Coordinates.y ) ) / sizeof( char ) - 1) ); TextOut( backDC, ptDiff.x - 44, 110, convertInt( Coordinates.z ), ( sizeof( convertInt( Coordinates.z ) ) / sizeof( char ) - 1) ); //TextOut( backDC, ptDiff.x - 44, 140, convertInt( D->X2 ), ( sizeof( convertInt( D->X2 ) ) / sizeof( char ) - 1) ); //TextOut( backDC, ptDiff.x - 44, 160, convertInt( D->Y2 ), ( sizeof( convertInt( D->Y2 ) ) / sizeof( char ) - 1) ); //TextOut( backDC, ptDiff.x - 44, 180, convertInt( D->Z2 ), ( sizeof( convertInt( D->Z2 ) ) / sizeof( char ) - 1) ); #pragma endregion Status #pragma endregion DRAW break; } case WM_KEYUP: #pragma region KeyMove if( GetAsyncKeyState( VK_UP ) == 1 ) Coordinates.y--; if( GetAsyncKeyState( VK_DOWN ) == 1 ) Coordinates.y++; if( GetAsyncKeyState( VK_LEFT ) == 1 ) Coordinates.x--; if( GetAsyncKeyState( VK_RIGHT ) == 1 ) Coordinates.x++; if( GetAsyncKeyState( VK_SUBTRACT ) == 1 ) { Coordinates.z--; ClearBackDC2(); La->MoveSpaceRenderd = 0; La->GetPossibleMoveSpace(); La->PrintMoveSpaceOnWorldSpace(); } if( GetAsyncKeyState( VK_ADD ) == 1 ) { Coordinates.z++; ClearBackDC2(); La->MoveSpaceRenderd = 0; La->GetPossibleMoveSpace(); La->PrintMoveSpaceOnWorldSpace(); } #pragma endregion KeyMove PostMessage( hWnd, DRAW, wParam, lParam ); return 0; case WM_LBUTTONDOWN: SetCapture(hWnd); gMouseDown = true; break; case WM_RBUTTONDOWN: return 0; case WM_MOUSEMOVE: if( gMouseDown ) { Coordinates.x = ((float) mx - Offset.x ) / Size.x; Coordinates.y = ((float) my - Offset.y) / Size.y; PostMessage( hWnd, DRAW, wParam, lParam ); } break; case WM_LBUTTONUP: ReleaseCapture(); gMouseDown = false; break; case WM_RBUTTONUP: ReleaseCapture(); gMouseDown = false; UpdateWindow (hWnd); break; #pragma region PAINT case WM_PAINT: { PAINTSTRUCT ps; HDC winhdc = BeginPaint(hWnd, &ps); // Do nothing here. All drawing happens in draw(), // and you draw to the backbuffer, NOT the // front buffer. EndPaint(hWnd, &ps); break; } #pragma endregion PAINT case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, msg, wParam, lParam); } } #pragma region Functions int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR cmdLine, int showCmd) { ghAppInst = hInstance; WNDCLASS wc; wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = ghAppInst; wc.hIcon = ::LoadIcon(0, IDI_APPLICATION); wc.hCursor = ::LoadCursor(0, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW)+1; wc.lpszMenuName = 0; wc.lpszClassName = "Hexapod"; RegisterClass( &wc ); ghMainWnd = ::CreateWindow("Hexapod", "Hexapod FrontView", WS_OVERLAPPEDWINDOW, 50, 50, 700, 850, 0, 0, ghAppInst, 0); if(ghMainWnd == 0) { ::MessageBox(0, "CreateWindow - Failed", 0, 0); return false; } ShowWindow(ghMainWnd, showCmd); UpdateWindow(ghMainWnd); MSG msg; /*ZeroMemory(&msg, sizeof(MSG)); while( GetMessage(&msg, 0, 0, 0) ) { TranslateMessage(&msg); DispatchMessage(&msg); }*/ while(1) { if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { if(msg.message == WM_QUIT) { PostQuitMessage(0); break; } TranslateMessage(&msg); // translates 'character' keyboard messages DispatchMessage(&msg); // send off to WndProc for processing } else { // could put more update code in here // before drawing. draw(); } } //return (int)msg.wParam; return 0; }