#include "graphics.hpp"
#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <vector>
using namespace genv;
using namespace std;

const unsigned int X = 500;
const unsigned int Y = 500;

struct pehely {
    private:
        int x,y;
        int r, g, b;
    public:
        bool kint = false;
        pehely()
        {
            x = rand() % X;
            y = rand() % Y;
            r = 255;
            g = 255;
            b = 255;
        }
        pehely(unsigned int kattX, unsigned int kattY)
        {
            x = kattX + rand() % 20;
            y = kattY + rand() % 20;
            r = 255;
            g = 255;
            b = 255;
        }
        void rajzol()
        {
            gout << move_to(x, y) << color(r,g,b) << box(2, 2);
        }
        void mozog()
        {
            x += (rand() % 5) -  2;
            y += rand() % 3;
            x = (x+X)%X;
            if (y > Y) kint = true;
            rajzol();
        }
        ~pehely()
        {
            cout << "deleted " << this << endl;
        }
};

int main()
{
    gout.open(X,Y);
    vector<pehely *> ho;
    for (unsigned int i = 0; i < 1000; i++)
        ho.push_back( new pehely);

    gout << refresh;
    event ev;
    gin.timer(40);
    while (gin >> ev)
    {
        if (ev.type == ev_timer)
        {
            gout << move_to(0, 0) << color (0,0,0) << box(X, Y);
            for (unsigned int i = 0; i < ho.size(); i++)
            {
                if (ho[i])
                    if (ho[i]->kint)
                    {
                        delete ho[i];
                        ho[i] = ho[ho.size()-1];
                        ho.pop_back();
                    }
                    else ho[i]->mozog();
            }
            cout << "tomb merete: " << ho.size() << endl;
            gout << refresh;
        }
        else if (ev.type == ev_mouse && ev.button > 0)
            for (int i = 0; i < 50; i++)
                ho.push_back( new pehely(ev.pos_x,ev.pos_y));
    }
    return 0;
}

