//! returns true if the button is touched bool Button::IsTouched() const { Matrix44 transformation = Matrix44::Identity; transformation.Translate(-Vector3::Create(m_vCenter.X, m_vCenter.Y, 0.0f)); transformation.Scale(Vector3::Create(m_vScale.X, m_vScale.Y, 1.0f)); transformation.Rotate(Vector3::Create(0.0f, 0.0f, m_fRotation*Math::DegToRadFactor)); transformation.Translate(Vector3::Create(m_vOriginalPosition.X, m_vOriginalPosition.Y, 0.0f)); if(Entity2D* p2DEntity = GetAncestor<Entity2D>()) { transformation *= p2DEntity->GetTransformationMatrix(); } Matrix44 inverse; if(transformation.GetInverse(inverse)) { Vector2 vTouchPos = InputManager::Instance()->GetTouchState().vPosition; Vector3 invTouchPos3D = inverse.TransformVect(Vector3::Create(vTouchPos.X, vTouchPos.Y, 0.0f)); Vector2 vInvTouchPos(invTouchPos3D.X, invTouchPos3D.Y); return GetBoundingBox().Contains(vInvTouchPos); } return false; }
/** TODO: avoid recalculating when nothing changed */ Matrix44 Entity2D::GetCenterTransformationMatrix() { Matrix44 matrix = Matrix44::Identity; matrix.Scale(Vector3::Create(m_vScale.X, m_vScale.Y, 1.0f)); matrix.Rotate(Vector3::Create(0.0f, 0.0f, m_fRotation*Math::DegToRadFactor)); matrix.Translate(Vector3::Create(m_vPosition.X, m_vPosition.Y, 0.0f)); // TODO: use type checking at initialization time in AddChild() and only use static_cast here if(Entity2D* pEntity2D = GetAncestor<Entity2D>()) { matrix *= pEntity2D->GetTransformationMatrix(); } return matrix; }
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_MY2DTRANSFORM, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // vertices1 // (-50,-50) ----------------- (+50, -50) // | | // | + | // | | // (-50,+50) ----------------- (+50, +50) const float w = 50.f; g_vertices1.push_back( Vector3(-w,-w,1) ); g_vertices1.push_back( Vector3(w,-w,1) ); g_vertices1.push_back( Vector3(w,w,1) ); g_vertices1.push_back( Vector3(-w,w,1) ); g_vertices1.push_back( Vector3(-w,-w,1) ); // vertices2 // +(0,0) ----------------- (+100, 0) // | | // | | // | | // (0,+100) ------------- (+1-0, +100) const float w2 = 100.f; g_vertices2.push_back( Vector3(0,0,1) ); g_vertices2.push_back( Vector3(w2,0,1) ); g_vertices2.push_back( Vector3(w2,w2,1) ); g_vertices2.push_back( Vector3(0,w2,1) ); g_vertices2.push_back( Vector3(0,0,1) ); g_matWorld1.SetIdentity(); g_matWorld1.Translate(Vector3(150,200,0)); g_matLocal1.SetIdentity(); g_matWorld2.SetIdentity(); g_matWorld2.Translate(Vector3(400,200,0)); g_matLocal2.SetIdentity(); g_matWorld3.SetIdentity(); g_matWorld3.Translate(Vector3(600,200,0)); g_matLocal3.SetIdentity(); if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } MSG msg; HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY2DTRANSFORM)); int oldT = GetTickCount(); while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } const int curT = GetTickCount(); const int elapseT = min(curT - oldT, 100); oldT = curT; MainLoop(elapseT); } return (int) msg.wParam; }
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_SOFTWARERENDERER, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // Y Z // | / // | / // -----------------------> X const float axisLineLength = 300.f; g_axises.reserve(8); g_axises.push_back( Vector3(0,0,0) ); g_axises.push_back( Vector3(axisLineLength,0,0) ); // x-axis g_axises.push_back( Vector3(0,0,0) ); g_axises.push_back( Vector3(0,axisLineLength,0) ); // y-axis g_axises.push_back( Vector3(0,0,0) ); g_axises.push_back( Vector3(0,0,axisLineLength) ); // z-axis // vertices1 // (-50,+50, +50) ----------------- (+50, +50, +50) // | | // | + | // | | // (-50,+50, -50) ----------------- (+50, +50, -50) // // (-50,-50, +50) ----------------- (+50, -50, +50) // | | // | + | // | | // (-50,-50, -50) ----------------- (+50, -50, -50) const float w = 30.f; g_vertices.reserve(128); g_vertices.push_back( Vector3(-w,w,w) ); g_vertices.push_back( Vector3(w,w,w) ); g_vertices.push_back( Vector3(w,w,-w) ); g_vertices.push_back( Vector3(-w,w,-w) ); g_vertices.push_back( Vector3(-w,-w,w) ); g_vertices.push_back( Vector3(w,-w,w) ); g_vertices.push_back( Vector3(w,-w,-w) ); g_vertices.push_back( Vector3(-w,-w,-w) ); g_indices.reserve(128); // top g_indices.push_back(0); g_indices.push_back(2); g_indices.push_back(3); g_indices.push_back(0); g_indices.push_back(1); g_indices.push_back(2); // front g_indices.push_back(3); g_indices.push_back(2); g_indices.push_back(7); g_indices.push_back(2); g_indices.push_back(6); g_indices.push_back(7); // back g_indices.push_back(1); g_indices.push_back(4); g_indices.push_back(5); g_indices.push_back(1); g_indices.push_back(0); g_indices.push_back(4); // left g_indices.push_back(3); g_indices.push_back(4); g_indices.push_back(0); g_indices.push_back(7); g_indices.push_back(4); g_indices.push_back(3); // right g_indices.push_back(2); g_indices.push_back(5); g_indices.push_back(6); g_indices.push_back(2); g_indices.push_back(1); g_indices.push_back(5); // bottom g_indices.push_back(4); g_indices.push_back(7); g_indices.push_back(6); g_indices.push_back(4); g_indices.push_back(6); g_indices.push_back(5); g_matWorld.SetIdentity(); g_matWorld.Translate(Vector3(0,0,0)); g_matLocal.SetIdentity(); g_matView.SetIdentity(); Vector3 dir = g_cameraLookat - g_cameraPos; dir.Normalize(); g_matView.SetView(g_cameraPos, dir, Vector3(0,1,0)); g_matProjection.SetProjection( MATH_PI / 4.f, 1.0f, 1.0f, 100.0f ); const float width = 800.f; const float height = 600.f; g_matViewPort.SetIdentity(); g_matViewPort._11 = width/2; g_matViewPort._22 = -height/2; g_matViewPort._33 = 0; g_matViewPort._41 = width/2; g_matViewPort._42 = height/2; g_matViewPort._43 = 0; if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } MSG msg; HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_SOFTWARERENDERER)); int oldT = GetTickCount(); while (1) { if (PeekMessageA(&msg, NULL, 0, 0, PM_REMOVE)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } if(msg.message == WM_QUIT) break; } const int curT = GetTickCount(); const int elapseT = curT - oldT; if (elapseT > 15) { oldT = curT; MainLoop(elapseT); } } return (int) msg.wParam; }