TouchEvent GameScreen::newTouchEvent() { if (m_touchEventsPool.size() == 0) { return TouchEvent(0, 0, Touch_Type::DOWN); } else { TouchEvent touchEvent = m_touchEventsPool.back(); m_touchEventsPool.pop_back(); return touchEvent; } }
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_CREATE: WindowInit(hWnd, wParam, lParam); break; case WM_PAINT: Render(hWnd); break; case WM_KEYDOWN: KeyDown(hWnd, wParam, lParam); break; case WM_KEYUP: break; case WM_MOUSEMOVE: MouseMove(hWnd, wParam, lParam); break; case WM_LBUTTONDOWN: LButtonDown(hWnd, wParam, lParam); break; case WM_TOUCH: return TouchEvent(hWnd, message, wParam, lParam); case WM_TIMER: TimerUpdate(hWnd, wParam, lParam); break; case WM_DESTROY: RemoveFontResource(_T("res/font/fantiquefour.ttf")); PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); break; } return 0; }
bool PickingContainer::performTouchPick(TouchEvent* e) { if (!pickingEnabled()) return false; std::vector<TouchPoint>& touchPoints = e->getTouchPoints(); // Clear the picked touch point map pickedTouchPoints_.clear(); if (touchPoints.size() > 1 || touchPoints[0].state() != TouchPoint::TOUCH_STATE_ENDED) touchPickingOn_ = true; else touchPickingOn_ = false; std::unordered_map<int, PickingObject*>::iterator touchPickObjs_it; std::unordered_map<PickingObject*, std::vector<TouchPoint>>::iterator pickedTouchPoints_it; auto touchPoint = touchPoints.begin(); while (touchPoint != touchPoints.end()) { bool isAssociated = false; if (touchPoint->state() == TouchPoint::TOUCH_STATE_STARTED) { // Find out if new touch point is touching inside a picking object uvec2 coord = mousePosToPixelCoordinates(touchPoint->getPos(), e->canvasSize()); PickingObject* pickObj = findPickingObject(coord); // If it is, put it in the TouchIDPickingMap if (pickObj) { touchPickObjs_.insert(std::pair<int, PickingObject*>(touchPoint->getId(), pickObj)); // Associate touch point with picking object // which can already have other associated touch points. pickedTouchPoints_it = pickedTouchPoints_.find(pickObj); if (pickedTouchPoints_it != pickedTouchPoints_.end()){ pickedTouchPoints_it->second.push_back(*touchPoint); } else{ pickedTouchPoints_.insert(std::pair<PickingObject*, std::vector<TouchPoint>>(pickObj, std::vector<TouchPoint>{*touchPoint})); } isAssociated = true; } } else if (touchPoint->state() == TouchPoint::TOUCH_STATE_ENDED) { // Erase touch point from TouchIDPickingMap size_t numberOfErasedElements = touchPickObjs_.erase(touchPoint->getId()); isAssociated = (numberOfErasedElements > 0); } else { // Find out if touch point is in the TouchIDPickingMap // If it exists, associate touch point with picking object touchPickObjs_it = touchPickObjs_.find(touchPoint->getId()); if (touchPickObjs_it != touchPickObjs_.end()){ // Associate touch point with picking object // which can already have other associated touch points. pickedTouchPoints_it = pickedTouchPoints_.find(touchPickObjs_it->second); if (pickedTouchPoints_it != pickedTouchPoints_.end()){ pickedTouchPoints_it->second.push_back(*touchPoint); } else{ pickedTouchPoints_.insert(std::pair<PickingObject*, std::vector<TouchPoint>>(touchPickObjs_it->second, std::vector<TouchPoint>{*touchPoint})); } isAssociated = true; } } // Removed touch point from the actual event if it was associated with a picking object if (isAssociated) touchPoint = touchPoints.erase(touchPoint); else ++touchPoint; } // Build touch event for all picking objects with associated touch points for (pickedTouchPoints_it = pickedTouchPoints_.begin(); pickedTouchPoints_it != pickedTouchPoints_.end(); ++pickedTouchPoints_it){ // Treat one touch point the same as mouse event, for now if (pickedTouchPoints_it->second.size() == 1){ uvec2 coord = mousePosToPixelCoordinates(pickedTouchPoints_it->second[0].getPos(), e->canvasSize()); if (pickedTouchPoints_it->second[0].state() & TouchPoint::TOUCH_STATE_STARTED){ pickedTouchPoints_it->first->setPickingPosition(normalizedCoordinates(coord)); pickedTouchPoints_it->first->setPickingDepth(pickedTouchPoints_it->second[0].getDepth()); pickedTouchPoints_it->first->setPickingMove(vec2(0.f, 0.f)); } else{ uvec2 prevCoord = mousePosToPixelCoordinates(pickedTouchPoints_it->second[0].getPrevPos(), e->canvasSize()); pickedTouchPoints_it->first->setPickingMove(pixelMoveVector(prevCoord, coord)); } // One touch point is currently treated as mouse event as well... // So prepare for that prevMouseCoord_ = coord; mousePickObj_ = pickedTouchPoints_it->first; mousePickingOngoing_ = true; } pickedTouchPoints_it->first->setPickingTouchEvent(TouchEvent(pickedTouchPoints_it->second, e->canvasSize())); } // One touch point is currently treated as mouse event as well... // So prepare for that if (touchPoints.size() == 1){ prevMouseCoord_ = mousePosToPixelCoordinates(touchPoints[0].getPos(), e->canvasSize()); touchPickingOn_ = false; } // Mark all picking objects in TouchIDPickingMap as picked. for (touchPickObjs_it = touchPickObjs_.begin(); touchPickObjs_it != touchPickObjs_.end(); ++touchPickObjs_it) touchPickObjs_it->second->picked(); return !touchPickObjs_.empty(); }
void GameScreen::init() { m_touchEventsPool.push_back(TouchEvent(0, 0, Touch_Type::DOWN)); m_touchEventsPool.push_back(TouchEvent(0, 0, Touch_Type::DOWN)); m_touchEventsPool.push_back(TouchEvent(0, 0, Touch_Type::DOWN)); m_touchEventsPool.push_back(TouchEvent(0, 0, Touch_Type::DOWN)); m_touchEventsPool.push_back(TouchEvent(0, 0, Touch_Type::DOWN)); m_touchEventsPool.push_back(TouchEvent(0, 0, Touch_Type::DOWN)); m_touchEventsPool.push_back(TouchEvent(0, 0, Touch_Type::DOWN)); m_touchEventsPool.push_back(TouchEvent(0, 0, Touch_Type::DOWN)); m_touchEventsPool.push_back(TouchEvent(0, 0, Touch_Type::DOWN)); m_touchEventsPool.push_back(TouchEvent(0, 0, Touch_Type::DOWN)); m_touchEventsPool.push_back(TouchEvent(0, 0, Touch_Type::DOWN)); m_touchEventsPool.push_back(TouchEvent(0, 0, Touch_Type::DOWN)); m_touchEventsPool.push_back(TouchEvent(0, 0, Touch_Type::DOWN)); m_touchEventsPool.push_back(TouchEvent(0, 0, Touch_Type::DOWN)); m_touchEventsPool.push_back(TouchEvent(0, 0, Touch_Type::DOWN)); m_touchEventsPool.push_back(TouchEvent(0, 0, Touch_Type::DOWN)); m_backgroundElements = std::unique_ptr<BackgroundElements>(new BackgroundElements(m_iLevelIndex)); World::getInstance()->reset(m_iLevelIndex, m_iDifficulty); m_timeButton = std::unique_ptr<TimeButton>(new TimeButton(1.1925f, 10.40375f, 2.385f, 1.1925f)); m_pausedDialog = std::unique_ptr<Dialog>(new Dialog("Paused!", "Exit", "Resume")); m_gameOverDialog = std::unique_ptr<Dialog>(new Dialog("Game Over!", "Exit", "Retry")); m_touchPoint = std::unique_ptr<Vector2D>(new Vector2D()); m_touchCursor = std::unique_ptr<TouchCursor>(new TouchCursor(0, 0)); m_iFunds = LevelUtils::calculateStartingFunds(m_iLevelIndex, m_iDifficulty); std::stringstream ss; ss << "$" << m_iFunds; m_iScore = 0; std::string scoreString = "00000000"; m_iScoreLength = scoreString.length(); m_scoreText = std::unique_ptr<Text>(new Text(scoreString, 0.15f, 9.6f, 0.30f, 0.30f, 1, 1, 1, 1)); m_statusText = std::unique_ptr<Text>(new Text("Wave: 0", 0.15f, 9.28f, 0.30f, 0.30f, 1, 1, 1, 1)); m_fundsText = std::unique_ptr<Text>(new Text(ss.str(), 14.605f, 10.675f, 0.30f, 0.30f, 0, 1, 0, 1)); m_adjustFundsText = std::unique_ptr<Text>(new Text("", m_fundsText->getX(), m_fundsText->getY() - 0.32f, m_fundsText->getWidth(), m_fundsText->getHeight(), 1, 0, 0, 1)); updateFundsTextAndTowerCursors(); m_gameState = RUNNING; m_iScreenState = SCREEN_STATE_NORMAL; }