initial commit, lordsawar source, slightly modified
[lordsawar] / src / gui / main.cpp
1 //  Copyright (C) 2007 Ole Laursen
2 //  Copyright (C) 2007, 2008, 2009 Ben Asselstine
3 //
4 //  This program is free software; you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation; either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU Library General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program; if not, write to the Free Software
16 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
17 //  02110-1301, USA.
18
19 #include <assert.h>
20 #include <memory>
21 #include <iostream>
22 #include <assert.h>
23 #include <glib.h>
24 #include <gtkmm.h>
25 #include <sigc++/trackable.h>
26 #include <sigc++/functors/mem_fun.h>
27
28 #include "main.h"
29
30 #include "driver.h"
31 #include "defs.h"
32 #include "File.h"
33 #include "Configuration.h"
34 #include "timing.h"
35
36
37 struct Main::Impl: public sigc::trackable 
38 {
39     Gtk::Main* gtk_main;
40     Driver* driver;
41
42     sigc::connection on_timer_registered(Timing::timer_slot s,
43                                          int msecs_interval);
44 };
45
46
47 static Main *singleton;
48
49 Main::Main(int &argc, char **&argv)
50     : impl(new Impl)
51 {
52   impl->driver = NULL;
53     singleton = this;
54
55     start_test_scenario = false;
56     start_stress_test = false;
57     start_robots = 0;
58     load_filename = "";
59     turn_filename = "";
60     
61     Glib::thread_init();
62     try
63     {
64         impl->gtk_main = new Gtk::Main(argc, argv);
65
66         g_set_application_name("LordsAWar!");
67
68         Timing::instance().timer_registered.connect(
69             sigc::mem_fun(*impl, &Main::Impl::on_timer_registered));
70     }
71     catch (const Glib::Error &ex) {
72         std::cerr << ex.what() << std::endl;
73     }
74 }
75
76 Main::~Main()
77 {
78     delete impl->driver;
79     delete impl->gtk_main;
80     delete impl;
81     singleton = 0;
82 }
83
84 Main &Main::instance()
85 {
86     assert(singleton != 0);
87     return *singleton;
88 }
89
90 bool Main::iterate_main_loop()
91 {
92     try
93     {
94         impl->gtk_main->iteration(false);
95     }
96     catch (const Glib::Error &ex) {
97         std::cerr << ex.what() << std::endl;
98     }
99
100     return true;
101 }
102
103 void Main::start_main_loop()
104 {
105     if (Configuration::s_decorated)
106       {
107       Gtk::RC::add_default_file(File::getMiscFile("gtkrc"));
108         Gtk::RC::reparse_all(Gtk::Settings::get_default(), true);
109       }
110
111     try
112     {
113       if (impl->driver != NULL)
114         {
115           delete impl->driver;
116           impl->driver = NULL;
117         }
118         impl->driver = new Driver(load_filename);
119         impl->gtk_main->run();
120     }
121     catch (const Glib::Error &ex) {
122         std::cerr << ex.what() << std::endl;
123     }
124 }
125
126 void Main::stop_main_loop()
127 {
128     try
129     {
130         impl->gtk_main->quit();
131     }
132     catch (const Glib::Error &ex) {
133         std::cerr << ex.what() << std::endl;
134     }
135 }
136
137 sigc::connection Main::Impl::on_timer_registered(Timing::timer_slot s,
138                                                  int msecs_interval)
139 {
140     return Glib::signal_timeout().connect(s, msecs_interval);
141 }
142