示例#1
0
Game* Game_Create( Keyboard* const keyboard, Mouse* const mouse)
{
	Mat4 tmpProjectionMatrix;
	
	OBJFile* objFile = OBJFile_Load( "assets/models/cubeModel.obj");
	MTLFile* mtlFile = MTLFile_Load( "assets/models/cubeModel.mtl");

	Game* game = malloc( sizeof(Game));
	memset( game, 0, sizeof(Game));

	Keyboard_Initialize( keyboard);
	Mouse_Initialize( mouse);

	game->Keyboard = keyboard;
	game->Mouse = mouse;

	// load content
	game->DiffuseTexture = Texture_Load("assets/textures/checkerboard.png");
	game->CubeModel = Mesh_CreateFromOBJGroup( objFile->Objects, objFile->Objects->Groups);
	
	// unload temp data
	OBJFile_Unload(objFile);
	MTLFile_Unload(mtlFile);

	// setup camera
	Mat4_LoadPerspective( &tmpProjectionMatrix, 70.0f, (float)SCREEN_WIDTH / (float)SCREEN_HEIGHT, 0.1f, 100.0f);
	game->Camera = Camera_Create( &tmpProjectionMatrix);

	return game;
}
示例#2
0
文件: main.cpp 项目: maq777/Grorld
/**
 * @brief Grorld entry point
 * 
 * Grorld application entry point and main loop. It also contains
 * the logic to automatically grind CivWorld bonus resources!
 */
int main(const int argc, const char **argv)
{
	std::cout << "Grorld, version 1" << std::endl;

	// Create a pseudorandom number generator instance
	// (http://en.wikipedia.org/wiki/C%2B%2B0x#Extensible_random_number_facility)
	std::mt19937 engine(time(NULL));

	// Initialize mouse & screen, put the target window in front
	Mouse_Initialize();
	XImage *grab = Screen_Initialize("CivWorld on Facebook");
	assert(grab);

	// Create the macthing algoritm object
	Match m(grab);

	// Load images that we want to match/find on the screen
	const cv::Mat bonus = Match::loadTemplate("assets/bonus.png");
	const cv::Mat city = Match::loadTemplate("assets/city.png");

	// Main loop (http://en.wikipedia.org/wiki/Event_loop)
	while (true)
	{
		// Grab a new frame (much like doing a screenshot)
		Screen_Get();
		// Prepare the matching algoritm with the new frame...
		m.prepare();

		// Search (via a template matching algorithm) for a bonus bubbles
		std::tuple<cv::Point, double> mr = m.match(bonus);
#ifndef TEST
		if (std::get<1>(mr) > MATCHING_THRESHOLD)
		{
			// We got a hit on the serach image, translate that point into a screen coordinate for the mouse to hover
			Screen_TranslateCoordinates(&std::get<0>(mr).x, &std::get<0>(mr).y);

			// Hover the mouse over it (use some randomness for the pointer placement...)
			Mouse_SetCoords(std::get<0>(mr).x + std::uniform_int_distribution<int>(0, bonus.size().width)(engine), std::get<0>(mr).y + std::uniform_int_distribution<int>(0, bonus.size().height)(engine));

			std::cout << time(NULL) << "\tBONUS: at " << std::get<0>(mr).x << "x" << std::get<0>(mr).y << " (score: " << std::get<1>(mr) << ")" << std::endl;
			continue;
		}

		// Once in a while, check if we need to press the city button...
		else
		{
			std::tuple<cv::Point, double> mr = m.match(city);
			if (std::get<1>(mr) > MATCHING_THRESHOLD)
			{
				// We got a hit on the serach image, translate that point into a screen coordinate for the mouse to hover
				Screen_TranslateCoordinates(&std::get<0>(mr).x, &std::get<0>(mr).y);

				// Hover the mouse over it and click
				Mouse_SetCoords(std::get<0>(mr).x + (city.size().width / 2), std::get<0>(mr).y + (city.size().height / 2));
				Mouse_Click(Button1);

				std::cout << time(NULL) << "\tCITY: at " << std::get<0>(mr).x << "x" << std::get<0>(mr).y << " (score: " << std::get<1>(mr) << ")" << std::endl;

				// Wait for the window to redraw (it's slow) before trying something clever
				struct timespec delay_click = millis_to_timespec(std::uniform_int_distribution<int>(2000, 4000)(engine));
				clock_nanosleep(CLOCK_MONOTONIC, 0, &delay_click, NULL);
				continue;
			}
		}
#endif

		// Give the computer some time to rest before processing the next frame
		struct timespec sleep = millis_to_timespec(std::uniform_int_distribution<int>(40, 60)(engine));
		clock_nanosleep(CLOCK_MONOTONIC, 0, &sleep, NULL);
	}

	// Clean up and exit
	Screen_Deinitialize();
	Mouse_Deinitialize();

	return EXIT_SUCCESS;
}