Exemple #1
0
void Level::initCommon(GameInterface *game)
{
    this->game = game;

    banana_in_hand = true;
    banana_type = BANANA_TYPE_1;
    just_throwed = false;
    bananas_shot = 0;
    time_playing = 0;
    time_banana_in_air = 0;
    cur_score = 0;
    best_score = 0;

    Rect t_rect(0, 0, SCREEN_W, SCREEN_H);
    this->background.init(game, "./sources/game_background.png", t_rect);

    t_rect.init(1166, 0, 200, 60);
    this->btn.init(game, "./sources/game_btn_pause.png", t_rect, &this->goToPauseMenu);

    t_rect.init(100, 535, 200, 200);
    this->monkey.init(game, "./sources/monkey.png", t_rect);

    t_rect.init(112, 580, 35, 70);
    this->banana[BANANA_TYPE_1].init(game, "./sources/banana.png", t_rect);

    t_rect.init(112, 580, 35, 70);
    this->banana[BANANA_TYPE_2].init(game, "./sources/banana2.png", t_rect);

    t_rect.init(112, 580, 35, 70);
    this->banana[BANANA_TYPE_3].init(game, "./sources/banana3.png", t_rect);

    t_rect.init(112, 580, 35, 70);
    this->banana[BANANA_TYPE_4].init(game, "./sources/banana4.png", t_rect);

    t_rect.init(1200, 650, 100, 100);
    this->target.init(game, "./sources/target.png", t_rect);

    t_rect.init(100, 502, 250, 250);
    this->cage.init(game, "./sources/cage.png", t_rect);

    SDL_Color color = {0,0,0};
    t_rect.init(0, 5, 250, 50);
    text_bananas_shot.init(game,"Bananas shot: ", color, "./sources/font.ttf", t_rect);

    t_rect.init(250, 5, 20, 50);
    shot.init(game, 0, color, "./sources/font.ttf", t_rect);

    t_rect.init(500, 5, 200, 50);
    text_score.init(game,"Score: ", color, "./sources/font.ttf", t_rect);

    t_rect.init(700, 5, 100, 50);
    score.init(game, 0 , color, "./sources/font.ttf", t_rect);
}
void CLEyeCameraCapture::Run()
{	
	// Create camera instance
	_cam = CLEyeCreateCamera(_cameraGUID, _mode, _resolution, _fps);
	if(_cam == NULL)		return;
	// Get camera frame dimensions
	CLEyeCameraGetFrameDimensions(_cam, w, h);
	// Depending on color mode chosen, create the appropriate OpenCV image
	if(_mode == CLEYE_COLOR_PROCESSED || _mode == CLEYE_COLOR_RAW)
		pCapImage = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 4);
	else
		pCapImage = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 1);

	// Set some camera parameters
	//CLEyeSetCameraParameter(_cam, CLEYE_GAIN, 20);
	//CLEyeSetCameraParameter(_cam, CLEYE_EXPOSURE, 511);
	CLEyeSetCameraParameter(_cam, CLEYE_AUTO_GAIN, true);
	CLEyeSetCameraParameter(_cam, CLEYE_AUTO_EXPOSURE, true);
	CLEyeSetCameraParameter( _cam, CLEYE_HFLIP, true);

	// Start capturing
	CLEyeCameraStart(_cam);
	cvGetImageRawData(pCapImage, &pCapBuffer);
	pCapture = pCapImage;
	long frames = 0;
	long count = GetTickCount();
	long prevCount = 0;
	double fps = 0;
	// image capturing loop
	Mat src_gray, subImage, subImage_gray;

	vector<Vec3f> circles;
	Point center;
	Point n_center;
	int radius = 0;
	int counter = 0;

	char* fpsText = new char[5];
	char* pos_text = new char[10];

	while(_running)
	{
		CLEyeCameraGetFrame(_cam, pCapBuffer);
		//check fps every 100 frames
		frames++;

		if((frames % 100) == 0){
			prevCount = count;
			count = GetTickCount();
			fps = 100000.0/(count - prevCount);
			//std::cout << "fps: " << fps << endl;
			sprintf(fpsText, "fps: %f", fps);
		}
		if(frames > 100)
			putText(pCapture, fpsText, Point(5, 20), CV_FONT_HERSHEY_PLAIN, 1, Scalar(0, 255, 0));
		else
			putText(pCapture, "calculating fps...", Point(5, 20), CV_FONT_HERSHEY_PLAIN, 1, Scalar(0, 255, 0));

		//find circle in whole area of frame first
		if(!_isTracking){
			CircleDetector(pCapture, src_gray, circles, center, radius);
			if(circles.size() != 0)
				_isTracking = true;
			n_center = center;
		}
		//dynamically move subimage area by tracking the object
		else
		{
			int subImage_size = 30;
			Point temp = FixSubImageSize(n_center, 320, 240, subImage_size);
			Rect t_rect(temp.x - subImage_size, temp.y - subImage_size, subImage_size*2, subImage_size*2);
			subImage = pCapture(t_rect);
			CircleDetector(subImage, subImage_gray, circles, center, radius);
			imshow(trackingWindowName, subImage);
			if(circles.size() == 0)
			{	
				counter++;
				if(counter == 3)
				{
					_isTracking = false;
					counter = 0;
					cout << "Lost tracking! Search whole frame." << endl;
				}

			}
			else
			{
				counter = 0;
				n_center.x = temp.x - subImage_size + center.x;
				n_center.y = temp.y - subImage_size + center.y;
				cout << "fps: " << fps << "  x:" << n_center.x << ", y:" << n_center.y << endl;
			}
		}

		sprintf(pos_text, "x=%d,y=%d", n_center.x, n_center.y);
		if(circles.size() != 0){
			putText(pCapture, pos_text, Point(n_center.x + radius, n_center.y - radius), CV_FONT_HERSHEY_PLAIN, 1, Scalar(0, 255, 0));
		}
		imshow(_windowName, pCapture);
	}

	// Stop camera capture
	CLEyeCameraStop(_cam);
	// Destroy camera object
	CLEyeDestroyCamera(_cam);
	// Destroy the allocated OpenCV image
	cvReleaseImage(&pCapImage);
	_cam = NULL;
}