Beispiel #1
0
int main (int argc, char * const argv[]) {
	GameObjectManager* objectManager = GameObjectManager::getInstance();	
	NodeManager *nodeManager = NodeManager::getInstance();
	{
		
		Node *node;
		for (int y = 0; y <= 14; y++) {
			for (int x = 0; x <= 19; x++) {
				node = new Node(x * 32, y * 32);
				nodeManager->addNode(node);
			}
		}
	}
	
    Hill* hill1 = new Hill(StrainYellow, nodeManager->getEmptyRandomNode());
    Hill* hill2 = new Hill(StrainRed, nodeManager->getEmptyRandomNode());
    Hill* hill3 = new Hill(StrainBlue, nodeManager->getEmptyRandomNode());
    objectManager->add(hill1);
    objectManager->add(hill2);
    objectManager->add(hill3);
	
    Ants ants;
    Strain antStrains[4] = { StrainYellow, StrainRed, StrainBlue, StrainRed };
    for (int i = 0; i < 4; i++) {
        Ant* ant = new Ant(antStrains[i], nodeManager->getEmptyRandomNode());
        objectManager->add(ant);
        ants.push_back(ant);
    }

    for (int i = 0; i < 10; i++) {
        Spice *spice = new Spice(nodeManager->getEmptyRandomNode());
        objectManager->addSpice(spice);
    }
    
    sf::RenderWindow App(sf::VideoMode(640, 480), "Ants");
    App.SetFramerateLimit(60);
    while (App.IsOpened())
    {
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            if (Event.Type == sf::Event::Closed)
                App.Close();
            if (Event.Type == sf::Event::MouseButtonPressed) {
                const sf::Input& Input = App.GetInput();
                cout << "Mouse cursor position: " << Input.GetMouseX() 
                     <<  "/" << Input.GetMouseY() << endl;
                sf::Vector2f mouse(Input.GetMouseX(), Input.GetMouseY());
                bool shiftDown = Input.IsKeyDown(sf::Key::LShift) ||
                                 Input.IsKeyDown(sf::Key::RShift);
                
                // cycle all gameobjects
                // if position matches mouse input -> select
                // if nothing matches -> deselect all
                
                GameObjects allObjects = objectManager->getObjects();
                GameObjects::iterator i = allObjects.begin();
                for (; i < allObjects.end(); i++) {
                    GameObject* object = *i;
                    sf::Vector2f left_upper = object->getSprite()->GetPosition();
					sf::Vector2f right_lower = left_upper;
					// TODO: clean up with new vector2f class
                    left_upper.x -= 16;
                    left_upper.y -= 16;
					right_lower.x += PIXELS_PER_NODE/2;
					right_lower.y += PIXELS_PER_NODE/2;
					if (left_upper.x <= mouse.x && right_lower.x >= mouse.x &&
					    left_upper.y <= mouse.y && right_lower.y >= mouse.y) {
                        
                        object->setSelectionStatus(true);
                        
					} else if (!shiftDown) {
                        object->setSelectionStatus(false);
					}
                }
            }
        }
		
        App.Clear();
		
		// trigger updates
        objectManager->trigger();

        //display nodes
        Nodes nodes = nodeManager->getNodes();
        Nodes::iterator n = nodes.begin();
        for (; n < nodes.end(); n++) {
			App.Draw(*(*n)->getSprite());
        }
		
		// update ants
        Ants::iterator a = ants.begin();
        for(; a < ants.end(); a++) {
            (*a)->handleCurrentAction(App.GetFrameTime());
        }
		
		// display game objects
		GameObjects objects = objectManager->getObjects();
		GameObjects::iterator i = objects.begin();
		for (; i < objects.end(); i++) {
			GameObject* object = *i;
			if (object->hasSprite()) {
				App.Draw(*object->getSprite());
				if (object->isSelected()) {
				    // draw rect
                    sf::Vector2f position = object->getSprite()->GetPosition();
					sf::Vector2f new_position = position;
					// TODO: clean up with new vector2f class
                    position.x -= 16;
                    position.y -= 16;
					new_position.x += PIXELS_PER_NODE/2;
					new_position.y += PIXELS_PER_NODE/2;
                    sf::Shape rect = sf::Shape::Rectangle(position, 
                        new_position, object->getSelectionColor(), 
                        true, object->getSelectionColor());
                    rect.EnableFill(false);
					App.Draw(rect);
				}
			}
		}
		
        App.Display();
    }
	
	
	
    return EXIT_SUCCESS;
}
Beispiel #2
0
TableInfo phuffman::utility::BuildTable(InputIterator first, InputIterator last, Codes& table) {
    using namespace std;
    typedef DepthCounterNode Node;
    typedef multimap<size_t, Node*> HuffmanTree;
    typedef vector<Node*> Nodes;

    assert(distance(first, last) <= constants::MAXIMUM_DATABLOCK_SIZE);
    assert(table.size() >= constants::ALPHABET_SIZE);

    HuffmanTree tree;
    Nodes leafs;
    TableInfo info;

    // Initialize tree
    {
        Frequencies frequencies = CountFrequencies(first, last);
        Frequencies::const_iterator first = frequencies.begin(), last = frequencies.end();
        while (first != last) {
            Node* leaf = new Node(first->symbol);
            tree.insert(make_pair(first->frequency, leaf));
            leafs.push_back(leaf);
            ++first;
        }
    }

    // Build tree
    {
        for (size_t i=0, size=tree.size(); i<size-1; ++i) {
            HuffmanTree::iterator first = tree.begin(), second = tree.begin();
            ++second;
            size_t freq = first->first + second->first; // Calculate freq for a node
            Node* node = new Node(first->second, second->second); 
            ++second;
            tree.erase(first, second); // Remove two nodes with the smallest frequency
            tree.insert(make_pair(freq, node)); // Add node that points to previosly removed nodes
        }
        assert(tree.size() == 1);
    }

    // Count codelengths
    // In fact, codelengths are already counted in the 'depth' member of a node
    // There is only one exception: if the tree contains only one object, we need to set it's depth manually
    Node* root = tree.begin()->second;
    root->depth = 1;

    // Sort nodes by codelength
    sort(leafs.begin(), leafs.end(), TreeComparator);

    // Build table
    {
        Nodes::const_iterator first = leafs.begin(), last = leafs.end();
        Node *curNode = *first;
        info.maximum_codelength = curNode->depth;
        Code curCode = CodeMake(curNode->depth, 0);
        table[curNode->element] = curCode;
        ++first;
        while (first != last) {
            assert(curNode->depth >= curCode.codelength);
            curNode = *first;
            // If current codeword and next codeword have equal lengths
            if (curNode->depth == curCode.codelength) {
            // Just increase codeword by 1
                curCode.code += 1;
            }
            // Otherwise
            else {
                // Increase codeword by 1 and _after_ that shift codeword right
                curCode.code = (curCode.code + 1) >> (curNode->depth - curCode.codelength);
            }
            curCode.codelength = curNode->depth;
            table[curNode->element] = curCode;
            ++first;
        }
    }
    
    delete root;

    return info;
}