Exemplo n.º 1
0
int main() {
	sf::Vector2i levelSize(30, 20);
	sf::Vector2i tileSize(32, 32);
	sf::RenderWindow window(sf::VideoMode(levelSize.x * tileSize.x, levelSize.y * tileSize.y), "Cellular Automata Cave Generation");
	window.setVerticalSyncEnabled(true);

	sf::Texture texture;
	if (!texture.loadFromFile("tileset.png")) {
		std::cout << "Unable to load the texture" << std::endl;
		return 0;
	}
	Tile tileset[] = {
		Tile(texture, sf::IntRect(tileSize.x, 0, tileSize.x, tileSize.y)),
		Tile(texture, sf::IntRect(0, 0, tileSize.x, tileSize.y))
	};
	sf::Font font;
	if (!font.loadFromFile("LCD_Solid.TTF")) {
		std::cout << "Unable to load the font" << std::endl;
		return 0;
	}
	sf::String str(" FPS\n\nPRESS 'G' TO GENERATE A NEW CAVE");
	sf::Text text(str, font, 10);
	text.setPosition(10, 10);
	text.setColor(sf::Color::White);

	TileMap tileMap(levelSize, tileSize, tileset);
	MapGenerator generator(levelSize.x, levelSize.y);
	generator.setIterations(4);
	generator.setDeathThreshold(3);
	generator.setBirthThreshold(4);
	generator.setChanceToStartAlive(50);
	
	int frameCount = 0;
	sf::Clock clock;
	while (window.isOpen()) {
		if (clock.getElapsedTime().asMilliseconds() >= 1000) {
			clock.restart();
			text.setString(std::to_string(frameCount) + str);
			frameCount = 0;
		}
		frameCount++;
		if (generateLevel) {
			tileMap.setMap(generator.generate());
			generateLevel = false;
		}
		processInput(window);
		window.draw(tileMap);
		window.draw(text);
		window.display();
	}
	return 0;
}
Exemplo n.º 2
0
void ut_tileloading::testFilter()
{
    QFile testFile("/usr/share/libquill-tests/images/image_16x4.jpg");
    TileCache *tileCache = new TileCache(100);
    QVERIFY(tileCache);

    QuillImageFilter *filter =
        QuillImageFilterFactory::createImageFilter(QuillImageFilter::Role_Load);
    QVERIFY(filter);
    filter->setOption(QuillImageFilter::FileName,
                      testFile.fileName());

    TileMap tileMap(QSize(8, 2), QSize(2, 2), tileCache);

    for (int i=0; i<4; i++) {
        QCOMPARE(tileMap.tile(i).fullImageSize(), QSize(8, 2));
        QuillImage tile = filter->apply(tileMap.tile(i));
        QCOMPARE(tile.fullImageSize(), QSize(8, 2));;
        tileMap.setTile(i, tile);
        QCOMPARE(tileMap.tile(i).fullImageSize(), QSize(8, 2));
    }

    QImage image("/usr/share/libquill-tests/images/image_16x4.png");
    QCOMPARE(tileMap.tile(0).area(), QRect(0, 0, 2, 2));
    test_utils::analyze((QImage)tileMap.tile(0), image.copy(0, 0, 2, 2), 13);

    QCOMPARE(tileMap.tile(1).area(), QRect(2, 0, 2, 2));
    test_utils::analyze((QImage)tileMap.tile(1), image.copy(2, 0, 2, 2), 13);

    QCOMPARE(tileMap.tile(2).area(), QRect(4, 0, 2, 2));
    test_utils::analyze((QImage)tileMap.tile(2), image.copy(4, 0, 2, 2), 13);

    QCOMPARE(tileMap.tile(3).area(), QRect(6, 0, 2, 2));
    test_utils::analyze((QImage)tileMap.tile(3), image.copy(6, 0, 2, 2), 13);
    
    delete filter;
    delete tileCache;
}
Exemplo n.º 3
0
int main()
{
	deltaTime = 0;
	sfg::SFGUI sfgui;

	// map sprite
	sf::Texture map_texture;
	sf::Sprite map_sprite;
	map_texture.loadFromFile("resources/map.png");
	map_sprite.setTexture(map_texture);

	// window
	window.create(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 32), "Runestone");
	view.setSize(sf::Vector2f(WINDOW_WIDTH, WINDOW_HEIGHT));
	view.setCenter(sf::Vector2f(0, 0));
	window.setView(view);

	// timer
	sf::Clock clock;

	// thor actions
	thor::Action quit(sf::Event::Closed);
	thor::Action left(sf::Keyboard::A, thor::Action::Hold);
	thor::Action right(sf::Keyboard::D, thor::Action::Hold);
	thor::Action up(sf::Keyboard::W, thor::Action::Hold);
	thor::Action down(sf::Keyboard::S, thor::Action::Hold);
	thor::Action mouse_down(sf::Mouse::Left, thor::Action::PressOnce);
	thor::Action mouse_up(sf::Mouse::Left, thor::Action::ReleaseOnce);

	// thor action map
	thor::ActionMap<std::string> action_map;
	action_map["quit"] = quit;
	action_map["left"] = left;
	action_map["right"] = right;
	action_map["up"] = up;
	action_map["down"] = down;
	action_map["mouse_down"] = mouse_down;
	action_map["mouse_up"] = mouse_up;
	thor::ActionMap<std::string>::CallbackSystem action_callbacks;
	action_callbacks.connect("quit", &OnQuit);
	action_callbacks.connect("left", &OnLeft);
	action_callbacks.connect("right", &OnRight);
	action_callbacks.connect("up", &OnUp);
	action_callbacks.connect("down", &OnDown);
	action_callbacks.connect("mouse_down", &OnMouseDown);
	action_callbacks.connect("mouse_up", &OnMouseUp);

	// thor particles
	sf::Texture particleTexture;
	particleTexture.loadFromFile("Media/particle.png");
	emitter.setEmissionRate(100.0f);
	emitter.setParticleLifetime(sf::seconds(1.0f));

	particle_system.setTexture(particleTexture);

	// sfgui
	auto sfgui_window = sfg::Window::Create();
	sfgui_window->SetTitle("Runestones: ");
	auto rune_one = sfg::Button::Create("Rune One");
	rune_one->GetSignal(sfg::Button::OnMouseLeftPress).Connect(std::bind(&OnRuneOne));
	rune_one->GetSignal(sfg::Button::OnMouseLeftPress).Connect(std::bind(&OnButtonClicked));
	auto rune_two = sfg::Button::Create("Rune Two");
	rune_two->GetSignal(sfg::Button::OnMouseLeftPress).Connect(std::bind(&OnRuneTwo));
	rune_two->GetSignal(sfg::Button::OnMouseLeftPress).Connect(std::bind(&OnButtonClicked));
	auto rune_three = sfg::Button::Create("Rune Three");
	rune_three->GetSignal(sfg::Button::OnMouseLeftPress).Connect(std::bind(&OnRuneThree));
	rune_three->GetSignal(sfg::Button::OnMouseLeftPress).Connect(std::bind(&OnButtonClicked));
	auto rune_four = sfg::Button::Create("Rune Four");
	rune_four->GetSignal(sfg::Button::OnMouseLeftPress).Connect(std::bind(&OnRuneFour));
	rune_four->GetSignal(sfg::Button::OnMouseLeftPress).Connect(std::bind(&OnButtonClicked));
	auto hbox = sfg::Box::Create(sfg::Box::Orientation::HORIZONTAL, 4.0f);
	hbox->Pack(rune_one);
	hbox->Pack(rune_two);
	hbox->Pack(rune_three);
	hbox->Pack(rune_four);
	sfgui_window->Add(hbox);
	sfgui_window->SetStyle(!sfg::Window::Style::RESIZE);
	sfgui_window->SetPosition(sf::Vector2f((WINDOW_WIDTH / 2.0f) -
		(sfgui_window->GetAllocation().width / 2.0f), WINDOW_HEIGHT - sfgui_window->GetAllocation().height));

	// tileset
	sf::Texture tileTexture;
	tileTexture.loadFromFile("resources/tiles.png");
	rf::TileMap tileMap(4, 8);
	tileMap.setTexture(tileTexture);
	int layout[4][8] = 
	{
		{-1, -1, -1, 1, 1, -1, -1, -1},
		{1,1,1,1,1,1,1,1},
		{-1,-1,1,1,1,1,-1,-1},
		{1,1,1,1,1,1,1,1}
	};

	for (size_t i = 0; i < 4; i++)
	{
		for (size_t j = 0; j < 8; j++)
		{
			tileMap.setTile(layout[i][j], i, j);
		}
	}

	while (window.isOpen())
	{
		deltaTime = clock.restart().asSeconds();
		action_map.clearEvents();

		sf::Event event;
		while (window.pollEvent(event))
		{
			sfgui_window->HandleEvent(event);
			action_map.pushEvent(event);
		}

		particle_system.update(sf::seconds(deltaTime));

		sfgui_window->Update(deltaTime);
		action_map.invokeCallbacks(action_callbacks, &window);
		window.setView(view);
		window.clear(sf::Color::Black);
		window.draw(map_sprite);
		window.draw(tileMap);
		window.draw(particle_system);
		sfgui.Display(window);
		window.display();
	}

	return 0;
}