File: evolvotron.cpp

package info (click to toggle)
evolvotron 0.7.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,320 kB
  • sloc: cpp: 10,425; python: 162; sh: 138; makefile: 8
file content (256 lines) | stat: -rw-r--r-- 8,592 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**************************************************************************/
/*  Copyright 2012 Tim Day                                                */
/*                                                                        */
/*  This file is part of Evolvotron                                       */
/*                                                                        */
/*  Evolvotron is free software: you can redistribute it and/or modify    */
/*  it under the terms of the GNU General Public License as published by  */
/*  the Free Software Foundation, either version 3 of the License, or     */
/*  (at your option) any later version.                                   */
/*                                                                        */
/*  Evolvotron is distributed in the hope that it will be useful,         */
/*  but WITHOUT ANY WARRANTY; without even the implied warranty of        */
/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         */
/*  GNU General Public License for more details.                          */
/*                                                                        */
/*  You should have received a copy of the GNU General Public License     */
/*  along with Evolvotron.  If not, see <http://www.gnu.org/licenses/>.   */
/**************************************************************************/

/*! \file
  \brief Application code for evolvotron executable (contains "main").
*/

/*! \mainpage Evolvotron : image evolver

  \author Tim Day 

  \section introduction Introduction
  "Evolvotron" is an interactive tool for producing "generative art".
  Images are generated from function trees, which are then mutated and evolved through a process of user selection.

  \todo For new features to be added, see the TODO file.
 */

#include "evolvotron_main.h"
#include "platform_specific.h"

#include <boost/program_options.hpp>

//! Application code
int main(int argc,char* argv[])
{
  QApplication app(argc,argv);
  
  // General options
  bool autocool;
  bool fullscreen;
  std::string grid;
  bool help;
  bool jitter;
  bool menuhide;
  uint multisample;
  bool spheremap;
  std::vector<std::string> startup;
  bool startup_shuffle;
  boost::program_options::options_description options_desc("General options");
  {
    using namespace boost::program_options;
    options_desc.add_options()
      ("autocool,a"   ,bool_switch(&autocool)                         ,"Enable autocooling")
      ("fullscreen,F" ,bool_switch(&fullscreen)                       ,"Fullscreen window")
      ("grid,g"       ,value<std::string>(&grid)->default_value("6x5"),"Columns x rows in image grid")
      ("help,h"       ,bool_switch(&help)                             ,"Print command-line options help message and exit")
      ("jitter,j"     ,bool_switch(&jitter)                           ,"Enable rendering jitter")
      ("multisample,m",value<uint>(&multisample)->default_value(1)    ,"Multisampling grid (NxN)")
      ("menuhide,M"   ,bool_switch(&menuhide)                         ,"Hide menus")
      ("spheremap,p"  ,bool_switch(&spheremap)                        ,"Generate spheremaps")
      ("startup,S"    ,value<std::vector<std::string> >(&startup)     ,"Startup function (multiples allowed, or positional)")
      ("shuffle,U"    ,bool_switch(&startup_shuffle)                  ,"Shuffle startup functions")
      ;
  }

  // Animation options
  int framerate;
  int frames;
  bool linear;
  boost::program_options::options_description animation_options_desc("Animation options");
  {
    using namespace boost::program_options;
    animation_options_desc.add_options()
      ("frames,f"     ,value<int>(&frames)->default_value(1)      ,"Frames in an animation")
      ("linear,l"     ,bool_switch(&linear)                       ,"Sweep z linearly in animations")
      ("fps,s"        ,value<int>(&framerate)->default_value(8)   ,"Animation speed (frames-per-second)")
      ;
  }

  // Advanced options
  bool debug;
  bool enlargement_threadpool;
  std::string favourite;
  int niceness_enlargement;
  int niceness_grid;
  uint threads;
  bool unwrapped;
  bool verbose;

  boost::program_options::options_description advanced_options_desc("Advanced options");
  {
    using namespace boost::program_options;
    advanced_options_desc.add_options()
      ("debug,D"                 ,bool_switch(&debug)                    ,"Enable function debug mode")
      ("enlargement-threadpool,E",bool_switch(&enlargement_threadpool)   ,"Enlargements computed using a separate threadpool")
      ("nice,n"                  ,value<int>(&niceness_grid)->default_value(4)
       ,"Niceness of compute threads for image grid")
      ("Nice,N"                  ,value<int>(&niceness_enlargement)->default_value(8)
       ,"Niceness of compute threads for enlargements (if separate pool)")
      ("threads,t"               ,value<uint>(&threads)->default_value(get_number_of_processors())
       ,"Number of threads in a thread pool")
      ("unwrapped,u"             ,bool_switch(&unwrapped)                ,"Don't wrap favourite function")
      ("verbose,v"               ,bool_switch(&verbose)                  ,"Log some details to stderr")
      ("favourite,x"             ,value<std::string>(&favourite)         ,"Favourite function")
      ;
  }

  boost::program_options::positional_options_description positional_options_desc;
  positional_options_desc.add("startup",-1);

  options_desc.add(animation_options_desc);
  options_desc.add(advanced_options_desc);
  
  boost::program_options::variables_map options;
  boost::program_options::store(
    boost::program_options::command_line_parser(
      argc,argv
    ).options(
      options_desc
    ).positional(
      positional_options_desc
    ).run()
    ,options
  );
  boost::program_options::notify(options);

  if (help)
    {
      std::cerr << options_desc;
      return 0;
    }
  
  if (verbose)
    std::clog.rdbuf(std::cerr.rdbuf());
  else
    std::clog.rdbuf(sink_ostream.rdbuf());

  //! \todo Could be done better maybe (set 'x' as input separator)
  const std::string::size_type p=grid.find("x");
  if (p==std::string::npos || p==0 || p==grid.size()-1)
    {
      std::cerr << "--grid option argument isn't in <rows>x<cols> format\n";
      return 1;
    }
  else
    {
      grid[p]=' ';
    }
  int cols=6;
  int rows=5;
  std::stringstream(grid) >> cols >> rows;

  if (cols*rows<2)
    {
      std::cerr << "Must be at least 2 display grid cells\n";
      return 1;
    }

  if (frames<1)
    {
      std::cerr << "Must specify at least 1 frame\n";
      return 1;
    }

  if (framerate<1)
    {
      std::cerr << "Must specify framerate of at least 1\n";
      return 1;
    }

  std::clog
    << "Evolvotron version "
    << stringify(EVOLVOTRON_BUILD)
    << " starting with " 
    << cols 
    << " by " 
    << rows 
    << " display cells and " 
    << threads
    << " compute threads per farm (niceness "
    << niceness_grid
    << " and "
    << niceness_enlargement
    << ")\n";

  if (!startup.empty()) {
    std::clog << "Startup functions to be loaded: ";
    for (size_t i=0;i<startup.size();++i) {
      if (i>0) std::clog << ", ";
      std::clog << startup[i];
    }
    std::clog << "\n";
  }

  EvolvotronMain*const main_widget=new EvolvotronMain
      (
       0,
       QSize(cols,rows),
       frames,
       framerate,
       threads,
       enlargement_threadpool,
       niceness_grid,
       niceness_enlargement,
       fullscreen,
       menuhide,
       autocool,
       jitter,
       multisample,
       debug,
       linear,
       spheremap,
       startup,
       startup_shuffle
       );

  main_widget->mutation_parameters().function_registry().status(std::clog);

  if (!favourite.empty())
    {
      std::clog
	<< "Selected specific function: "
	<< favourite
	<< (unwrapped ? " (unwrapped)" : " (wrapped)")
	<< "\n";

      if (!main_widget->favourite_function(favourite))
	{
	  std::cerr << "Unrecognised function name specified for -x/-X option\n";
	  return 1;
	}

      main_widget->favourite_function_unwrapped(unwrapped);
    }
  
  main_widget->show();
  
  // NB Do this here rather than in constructor so that compute threads aren't being fired off during general GUI set-up
  
  std::clog << "Resetting main widget...\n";
  main_widget->reset_cold();
  
  // NB No need to worry about deleting EvolvotronMain... QApplication seems to do it for us.
  std::clog << "Commencing main loop...\n";
  return app.exec();
  std::clog << "...returned from main loop\n";
  
  return 0;
}