Пример #1
0
int main(int argc, char *argv[])
{
    int width = 640;
    int height = 480;

    int t_time = 0;

    Image *source = NULL;
    std::string basename = "noname";
    std::string workfilename;

    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        std::cerr << "Unable to initialize SDL!\n";
        return -1;
    }

    atexit(SDL_Quit);

    if (argc >= 2) {
        try {
            source = new Image(argv[1]);
        }
        catch (const std::string &err) {
            std::cerr << "Error in image creation: " << err << "\n";
            return -1;
        }

        width = source->Width();
        height = source->Height();
        basename = argv[1];

        if (argc == 3)
            workfilename = argv[2];
    }
    else {
        std::cerr << "**** IMAGE NOT SPECIFIED ****!\n";
        return -1;
    }

    SDL_Surface *video = SDL_SetVideoMode(width * 2, height, 32, SDL_SWSURFACE);

    source->BlitTo(video, width, 0);
    SDL_UpdateRect(video, width, 0, width, height);

    if (!video) {
        std::cerr << "Unable to open window!\n";
        return -1;
    }

    Settings::ScreenHeight = source->Height();
    Settings::ScreenWidth = source->Width();

    uint64_t counter = 0;
    time_t start = ::time(NULL);
    bool running = true;
    time_t last = start;
    ::srand(start);

    /* Set time measure */
    struct timeval t_start, t_stop;
    unsigned long t_elapsed;
    gettimeofday(&t_start, NULL);

    GeneticTest gen(*source);

    if (!workfilename.empty()) {
        std::cerr << "Loading progresses from <" << workfilename << ">...\n";
        if (FILE *f = ::fopen(workfilename.c_str(), "rb")) {
            SerialLoader l(f);
            l & gen;
            ::fclose(f);
            gen.Display(video);
        }
    }

    while (running) {
        if (gen.Step()) {
            time_t now = time(NULL);

            if (now > last) {
                gen.Display(video);
                last = now;
            }
        }

        counter++;

        if ((counter % 100) == 0) {
            SDL_Event e;
            while (SDL_PollEvent(&e)) {
                if (e.type == SDL_QUIT )
                    running = false;
                else if (e.type == SDL_KEYDOWN) {
                    if (e.key.keysym.sym == SDLK_q)
                        running = false;
                    else if (e.key.keysym.sym == SDLK_s) {
                        if (FILE *f = fopen((basename + ".idx").c_str(), "wb")) {
                            SerialSaver s(f);
                            s & gen;
                            fclose(f);
                            std::cerr << "Saved progresses to <" << basename << ".idx>.\n";
                            SDL_WM_SetCaption(("Saved to " + basename + ".idx").c_str(), NULL);
                        }
                    }
                    else if (e.key.keysym.sym == SDLK_b) {
                        gen.Save(basename + ".out.bmp");
                        std::cerr << "Saved BMP to <" << basename << ".out.bmp>.\n";
                        SDL_WM_SetCaption(("Saved BMP to " + basename + ".out.bmp").c_str(), NULL);
                    }
                    counter = 1;
                }
            }
        }
        if ((counter % 2000) == 0) {
            time_t now = time(NULL);
            now -= start;

            /* Get elapsed time */
            gettimeofday(&t_stop, NULL);
            t_elapsed = 1000000 * (t_stop.tv_sec - t_start.tv_sec);
            t_elapsed += t_stop.tv_usec - t_start.tv_usec;

            t_time += t_elapsed;

            if (now > 0) {
                // std::cerr << "Iteration:" << counter 
                //           << " Poly:" << gen.Poly() 
                //           << " IPS:" << (counter / now)
                //           << " Error:" << gen.Error()
                //           << " ET:" << t_elapsed
                //           << " Generation:" << gen.Generation() << "\n";
                std::ostringstream os;

                os << "Gen:" << gen.Generation() << " IPS:" << (counter / now) << " Err:" << gen.Error() << "Pol: " << gen.Poly();
                SDL_WM_SetCaption(os.str().c_str(), NULL);
            }

            /* Restart */
            gettimeofday(&t_start, NULL);
        
            /* If it is over 15 polygons */
            if (gen.Poly() > 15) {
                std::cout << "Elapsed time: " << t_time << ", after " << gen.Generation() << " generations.\n";

                return 0;
            }
        }
    }
}
Пример #2
0
 void Display(SDL_Surface *video) {
     drawing_.Render(dest_);
     dest_.BlitTo(video);
     SDL_UpdateRect(video, 0, 0, dest_.Width(), dest_.Height());
 }